2.5. Bit-banding

Bit-banding maps a complete word of memory onto a single bit in the bit-band region. For example, writing to one of the alias words will set or clear the corresponding bit in the bitband region.

This allows every individual bit in the bit-banding region to be directly accessible from a word-aligned address using a single LDR instruction. It also allows individual bits to be toggled from C without performing a read-modify-write sequence of instructions.

Address translation

The Cortex-M3 has two 32MB regions that map onto the two 1MB bit-band regions. The two regions are separate, one in the SRAM region and one in the peripheral region.

Each bit in the bit-band region is addressed sequentially in the 32MB alias region. For example, the eighth bit in the bit-band region can be accessed using the eighth word in the 32MB alias region.

Reading and writing to the bit-banding region

When writing to the alias regions bit 0 of the 32 bit word is used to set the value at the bit-banding region. Reading from the alias address will return the value from the bit-band region in bit 0 and the other bits will be cleared.

You can also access the base bit-band region itself in the same way as normal memory, using word, half word, and byte accesses.

Using bit-banding from C code

Bit-banding can be used from C code using pre-processor macros to perform the accesses. Example 9 demonstrates how you can do this.

Example 9. Using bit-banding from C code using pre-processor macros

#define BITBAND_SRAM_REF 0x20000000#define BITBAND_SRAM_BASE 0x22000000#define BITBAND_SRAM(a,b) ((BITBAND_SRAM_BASE + (a-BITBAND_SRAM_REF)*32 \    + (b*4))) // Convert SRAM address#define BITBAND_PERI_REF 0x40000000#define BITBAND_PERI_BASE 0x42000000#define BITBAND_PERI(a,b) ((BITBAND_PERI_BASE + (a-BITBAND_PERI_REF)*32 \    + (b*4))) // Convert PERI address
#define MAILBOX 0x20004000#define TIMER 0x40004000// Mailbox bit 0#define MBX_B0 *((volatile unsigned int *)(BITBAND_SRAM(MAILBOX,0)))// Mailbox bit 7#define MBX_B7 *((volatile unsigned int *)(BITBAND_SRAM(MAILBOX,7)))// Timer bit 0#define TIMER_B0 *((volatile unsigned char *)(BITBAND_PERI(TIMER,0)))// Timer bit 7#define TIMER_B7 *((volatile unsigned char *)(BITBAND_PERI(TIMER,7)))
int main(void){    unsigned int temp = 0;
    MBX_B0 = 1; // Word write    temp = MBX_B7; // Word read    TIMER_B0 = temp; // Byte write    return TIMER_B7; // Byte read}
Copyright © 2007. All rights reserved.ARM DAI0179B