This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

bit addressable arrays in ARM9

Note: This was originally posted on 5th January 2009 at http://forums.arm.com

plz help me by telling hw we can use bit addressable arrays menas arrays that can be used bitwise..while programming through keil microvision , is there any option available??? plz help..
  • Note: This was originally posted on 6th January 2009 at http://forums.arm.com

    thanx for attending my problem...actually we had implemented the same thing in 8051 microcontroller in C  by using 'bdata' and 'sbit' keyword... is there any similar thing available in ARM9..??
    the application requirement is quite specific and it requires to access each bit of data type seprately if not particularly array....can u guide me in that direction???
  • Note: This was originally posted on 5th January 2009 at http://forums.arm.com

    I don't know of any language that natively supports bit-addressable arrays, as they are only really useful in very specific circumstances. In most languages, you will need to use bit-fiddling logic to implement bit-addressable arrays. The code to implement them is fairly simple but often not worthwhile.

    Are you using the C language? If so, consider a solution using a combination of a simple array, bit shifts and some form of fixed-point arithmetic to act as the addressing mechanism. You'll need to write special functions or macros to act as accessors as C's array notation will not suffice by itself here.
  • Note: This was originally posted on 6th January 2009 at http://forums.arm.com

    They are not C keywords, so you were using some non-standard extensions to the C compiler.

    As I recommended before, you will need to use simple arrays and bit shifts to achieve the same result. For example:

    // An example function to clear a bit in a bitwise array.
    void Clearbit(unsigned int *puiBitArray, unsigned int uiID)
    {
    // Derive the array element in which the requested bit resides and the
    // actual offset of the bit.
    // 8 * sizeof(unsigned int) will be 32 with ARM compilers, but I have used
    // sizeof so that your code will be slightly more portable. Note that this
    // assumes that a byte is 8 bits.
    unsigned int uiArrayElement = uiID / (8 * sizeof(unsigned int));
    unsigned int uiElementBit = uiID % (8 * sizeof(unsigned int));

    // Clear the relevant bit.
    puiBitArray[uiArrayElement] &= ~(1 << uiElementBit);
    }

    You should be able to use that to work out the other functions you'll need.

    Your code will run faster if you use macros rather than functions for this stuff, as the function call overhead is significant in comparison to the work you're doing. If you can use inline functions, that's semantically better, though it's often hard to get compilers to do that for you, particularly if you have an old compiler.

    To be clear: This is not a restriction or feature of the ARM9 processor, this is simply the way that the C language works. The solution I propose will work on pretty much any processor.
  • Note: This was originally posted on 6th January 2009 at http://forums.arm.com

    ARM has no "access per bit" instruction - so the compiler will need to acccess bytes and then extract the data it needs using shifts / masks. While you could use language extensions to do this, writing it yourself shouldn't be too hard.

    That said... Cortex-M3 is a special case which supports a bit-banded memory region (each bit is addressable as one word in the address map, removing the need for the mask and shift). There is a 32MB bit-band memory allocated in the address map, which allows addressing of a 1MB array of memory.

    It depends on the MCU implementation on how much of this bit-band is allocated to real memory.

    Cheers,

    Iso