Bit Banding

Overview

Bit banding maps a complete word in one memory space (this can be virtual) to a single bit in another memory space (this has to be a real memory space). This allows you to perform single-instruction read and write operations on individual bits. The benefit of single instruction reads and writes is that it is atomic and uses less processor time.

This technique is fully compatible with ANSI C, requiring no special compiler keywords or assembly instructions. Note that there are many solutions to the problem of performing atomic read/writes to memory, bit banding is just one of them.

Bit banding is not to be confused with another embedded programming technique called bit banging, which is when you implement what is typically done using in a hardware peripheral purely in the firmware.

Memory Regions

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 (but both of course in the same memory space). The diagram below shows the entire Cortex-M3 memory space, with the bit band locations being close to the bottom of memory.

Screen Shot 09-30-14 at 04.30 PM

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.

If you are changing many bits in the same byte at once, it may be quicker to forgo the bit banging technique, disable interrupts (only if you need atomic access of course) and perform the reads and writes as usual.