Bit-Band is a feature of the memory architecture in Arm Cortex-M series microcontrollers that allows individual bits within the memory to be accessed and modified atomically. It provides faster bit manipulation by treating a bit within a word in memory as if it has its own address. This avoids the need to perform read-modify-write operations when accessing single bits.
How Bit-Band Works
In Cortex-M microcontrollers, the SRAM and peripheral registers are mapped into the processor’s physical address space. By default, accessing a memory address reads or writes an entire word (32-bits or 64-bits depending on the architecture). With Bit-Band, certain regions of the SRAM and peripheral memory space are further mapped into a “bit-band region” within the processor’s address space.
The bit-band region is divided into 32-bit (or 64-bit) segments, each of which maps to a single bit in the original SRAM or peripheral register. By accessing an address in the bit-band region, the processor can now read or write to individual bits instead of the entire word.
For example, if a peripheral register REG1 maps to address 0x4000 0000 in memory, its 32 bits will be aliased to 32 words in the bit-band region from 0x2200 0000 to 0x2200 001F. To set the 5th bit of REG1, the processor simply writes to the address 0x2200 0004 corresponding to that bit.
Benefits of Using Bit-Band
The key benefits of using Bit-Band memory in Cortex-M devices are:
- Atomic bit manipulation: Individual bits can be set and cleared without the need for read-modify-write sequences. This avoids race conditions when multiple threads try to access the same bit.
- Faster access: Bit-Band memory is mapped into the processor’s physical address space. Bit manipulation using Bit-Band avoids the higher overhead of register access via opcodes.
- Easy bit masking: Setting, clearing or toggling bits becomes a simple matter of accessing an address instead of shifts and masks.
- Bit aliasing: Bits from any peripheral register or SRAM word can be aliased to the Bit-Band region for easy access.
Usage in Firmware
Here are some common use cases of Bit-Band memory in firmware for Cortex-M devices:
Atomic Status Bits
Status flags that can be accessed from multiple threads can be implemented as individual bits in the Bit-Band region. Each thread can check or set the flags atomically without any locking. #define STATUS_BIT1 (*((volatile unsigned long *)(SRAM_BITBAND_ADDR + (1<<5)))) void thread1() { if(STATUS_BIT1) { // take action } } void thread2() { STATUS_BIT1 = 1; // set bit atomically }
Peripheral Control Bits
Individual control bits in peripheral registers (enable, reset, clock, etc) can be aliased into Bit-Band for easy manipulation without read-modify-write. #define UART_TX_ENABLE (*((volatile unsigned long *)(UART_BITBAND_ADDR + (1<<2)))) void enableUARTTx() { UART_TX_ENABLE = 1; // enable transmit }
Thread Synchronization
Bit-Band bits make efficient flags for thread synchronization and signaling without mutexes or semaphores. #define THREAD_SYNC_BIT (*((volatile unsigned long *)(SYNC_BITBAND_ADDR + (1<<0)))) void thread1() { // wait for sync bit while(!THREAD_SYNC_BIT); // … critical section … THREAD_SYNC_BIT = 0; // reset sync bit } void thread2() { // … do work … THREAD_SYNC_BIT = 1; // signal thread 1 }
Setup and Configuration
To use Bit-Band in a Cortex-M device:
- The Bit-Band region must be enabled and mapped in the memory architecture.
- Aliased addresses for peripheral and SRAM bits must be defined using device header files.
- The aliased addresses can then be directly accessed in the firmware.
Bit-Band is configured via the Memory Protection Unit (MPU) settings. The ARMv7-M Architecture Reference Manual provides detailed guidelines for setup.
Vendors like STMicroelectronics, NXP, etc. provide ready-to-use header files that define the aliased bit addresses for their Cortex-M devices. Refer to the device datasheet and programming manual for definitions and usage.
Limitations of Bit-Band
While Bit-Band offers many benefits, it also has some limitations:
- Only single bit manipulation is possible – entire words in memory cannot be accessed.
- Additional memory space is required for the bit-band region.
- Implementation is not consistent across Cortex-M vendors.
- Limited number of peripheral and SRAM bits can be aliased.
- Not recommended for frequently changing data bits.
In some cases, bit-banding might not be the most optimal solution – factors like memory density, peripheral availability, etc. should be evaluated.
Conclusion
Bit-Band memory enables atomic bit manipulation in Arm Cortex-M devices by mapping individual bits from the SRAM and peripherals into a separate address region. This allows threads to efficiently set, clear and toggle control and status bits without locks or read-modify-write sequences. When applicable, Bit-Band can significantly simplify code and improve performance for bit twiddling tasks in embedded firmware.