Conventional bit manipulation (read, modify, write)
If you want to change only one bit of RAM or register, the Cortex-M3 allows you to do so with a single instruction, using the bitband method.Traditionally, if you wanted to change just one bit of RAM or register, you had to read, modify, and write in byte units.
When an event such as an interrupt occurs during the read, modify, or write process, the process is interrupted, and in some cases, the data being modified may change.In the conventional method, the external event (interrupt) is first disallowed, the byte containing the bit to be changed is read, the bits other than the bit to be changed are masked, and only the bit to be changed is changed.Then I had to put it back in place and finally enable an external event (interrupt), which was a hassle.
With the bit-band method, this sequence of work can be done with a single instruction (a conventional instruction that is not a new instruction).
Simply put, the bit-band method has an alias area where each bit of RAM and register can be specified in address units, and the original bit is automatically changed by changing the alias.Since an alias area has one address for each bit, the original bit can be changed with only one instruction that writes the value after the change to that address. (That’s a pretty lavish use of memory, isn’t it?)
The memory map has two 32MB alias regions that are mapped to two 1MB bit-band regions.
- The 1MB of SRAM bitband area is mapped to the 32MB of SRAM alias area. Specifically, the 1MB alias of 20000000h to 200FFFFFh is mapped to 22000000h to 23FFFFFh.
- A 1MB of peripheral bitband area is mapped to a 32MB of peripheral alias area. Specifically, the 1MB alias of 40000000h to 400FFFFFh is mapped to 42000000h to 43FFFFFh.
The mapping formula shows how each word in the alias region (32 bits) is referred to the corresponding bit in the bitband region (the bit of the target).The mapping formula is as follows.
bit_word_offset = (byte_offset x 32) + (bit_number × 4)
bit_word_addr = bit_band_base + bit_word_offset
|The position of the target bit in the bitband region.|
|Address of the word to which the target bit is mapped in the alias area.|
|Start address of an alias area|
|The position (number of bytes) of the byte containing the target bit in the bitband area.|
|Bit position (0-7) in the byte containing the target bit (in the bitband region)|
Here is an example of the bitband mapping between the alias region of SRAM and the bitband region of SRAM.
- The bit  of the byte in the bitband region at 200FFFFFh is mapped to the word of the alias at 23FFFFE0h.
23FFFFE0h = 22000000h + (FFFFFh*32) + 0*4
- The bit  of a byte in the bitband region at 200FFFFFh is mapped to a word in the alias at 23FFFFFCh.
23FFFFFCh = 22000000h + (FFFFFh*32) + 7*4
Writing a word to an alias region has the same effect as a read-modify-write operation on the target bit in the bitband region.The bit of the value written to the word in the alias area determines the value written to the target bit in the bitband area.
When the value of bit  set to 1 is written, 1 is written to the corresponding bit in the bit band area, and
When the bit  is written to a value cleared to 0, a 0 is written to the corresponding bit in the bit band area.
The relationship between the bit-band and alias regions
Bits [31:1] in words in the alias region do not affect bits in the bitband region.
A 0x01 write has the same effect as a 0xFF write.
A 0x00 write has the same effect as a 0x0E write.
Reading a word in an alias area returns 0x01 or 0x00. (On a read, the bit [31:1] is always 0.)
A value of 0x01 indicates that the target bit in the bitband region is set to 1.
A value of 0x00 indicates that the target bit has been cleared to 0.
This post is also available in: Japanese