SoC
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
  • Arm Cortex M3
  • Contact
Reading: Hardware Support for Atomic Bit Manipulation in ARM Cortex M3
SUBSCRIBE
SoCSoC
Font ResizerAa
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
Search
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
Have an existing account? Sign In
Follow US
  • Looking for Something?
  • Privacy Policy
  • About Us
  • Sitemap
  • Contact Us
© S-O-C.ORG, All Rights Reserved.
Arm

Hardware Support for Atomic Bit Manipulation in ARM Cortex M3

Mike Johnston
Last updated: October 5, 2023 9:55 am
Mike Johnston 6 Min Read
Share
SHARE

The ARM Cortex M3 processor provides hardware support for atomic bit manipulation through its bit-banding feature. This allows developers to safely set, clear or toggle individual bits in memory without the need for locks or other synchronization primitives. The bit-banding hardware acts as an atomic read-modify-write cycle, ensuring thread safety for bit manipulation operations.

Contents
Overview of Bit-Banding in Cortex M3Using Bit-Banding AliasesExample Code for Atomic Bit ManipulationBenefits of Using Bit-BandingUse Cases for Atomic Bit ManipulationLimitations of Bit-BandingConclusion

Overview of Bit-Banding in Cortex M3

The Cortex M3 processors include a bit-banding region in memory that maps each word in the alias region to a bit in the bit-band region. Writes to bit-band aliases result in an atomic read-modify-write cycle on the mapped bit. This removes the need to disable interrupts or acquire locks when manipulating bits, as the operation is guaranteed to be atomic by the hardware.

The bit-band region in Cortex M3 is 1MB in size, and maps to 32MB of memory space (with each bit mapping to a 4Kb address space). Bit-band aliases can be generated for SRAM, peripheral registers or any other memory region. This provides great flexibility for atomic bit access without locks.

Using Bit-Banding Aliases

To use bit-banding, aliases to the individual bits must be generated using the bit-band region base address and bit-band alias offset formula. Each word in the alias region maps to 32 bits in the bit-band region. The formula to calculate the alias address is: Bit_Alias = Bit_Band_Base + (Byte_Offset * 32) + (Bit_Number * 4)

Where Byte_Offset is the byte address of the targeted word, Bit_Number is the targeted bit in that word (0-31), and Bit_Band_Base is the starting address of the bit-band region (vendor-specific).

Once aliases are generated, individual bits can be directly accessed atomically using these addresses. Setting, clearing or toggling bits simply requires a read-modify-write sequence on the bit alias.

Example Code for Atomic Bit Manipulation

Here is an example showing how atomic bit manipulation can be done in C code using bit-banding: #define BITBAND_SRAM_BASE 0x20000000 // Peripheral SRAM bit-band base volatile uint32_t *myFlagPtr = (uint32_t*)0x20000020; // SRAM location void setFlagBitAtomically() { uint32_t *flagAlias = BITBAND_SRAM_BASE + ((uint32_t)myFlagPtr – SRAM_BASE) * 32 + 5 * 4; // Flag bit 5 *flagAlias = 1; // Atomic bit set } void clearFlagBitAtomically() { uint32_t *flagAlias = BITBAND_SRAM_BASE + ((uint32_t)myFlagPtr – SRAM_BASE) * 32 + 5 * 4; *flagAlias = 0; // Atomic bit clear } void toggleFlagBitAtomically() { uint32_t *flagAlias = BITBAND_SRAM_BASE + ((uint32_t)myFlagPtr – SRAM_BASE) * 32 + 5 * 4; *flagAlias ^= 1; // Atomic bit toggle }

This allows thread-safe manipulation of the flag bit without any synchronization constructs. The generated aliases map to the flag bit in the SRAM location, and writes to them result in an atomic read-modify-write cycle.

Benefits of Using Bit-Banding

Some of the benefits of using bit-banding for atomic bit manipulation are:

  • Removes need for interrupt disabling or synchronization primitives like mutexes
  • Guaranteed atomic access to individual bits
  • Allows multiple threads/cores to safely manipulate same bits
  • No risk of torn reads or incomplete writes to bits
  • Easy to set, clear or toggle bits atomically
  • No software overhead for synchronization
  • Leverages hardware to provide atomicity

This improves performance and scalability for multi-threaded applications that need atomic bit manipulation. The hardware atomicity relieves software from implementing and managing locks or spinlocks.

Use Cases for Atomic Bit Manipulation

Some example use cases where atomic bit manipulation via bit-banding is useful:

  • Thread synchronization flags
  • Lock-free data structures
  • Set/clear status flags safely
  • Thread or core signaling
  • Atomic counting semaphores
  • Driver status flags
  • Peripheral interrupt enable/disable

Essentially any scenario where thread-safe access to individual bits is needed can benefit from bit-banding support in Cortex M3.

Limitations of Bit-Banding

Some limitations to note when using bit-banding are:

  • Only single bit atomic access, no atomic operations on whole words
  • Aliases needed to be generated, not natural addresses
  • Limited bit-band region size (1MB)
  • Only SRAM, peripherals and external memories supported
  • No atomic read access, only atomic read-modify-write
  • No atomic support for clearing/setting bit fields

So bit operations are limited to single bit sets, clears and toggles. For multi-bit fields, traditional locking and synchronization is still needed.

Conclusion

The bit-banding feature of ARM Cortex M3 allows atomic manipulation of single bits in memory without locks. This enables efficient synchronization of threads and atomic access to peripheral and status flags. Correct usage can remove software overhead related to locks and threading. However, care must be taken to generate proper aliases, and limitations exist on what types of atomic operations are possible. Overall, bit-banding is a useful hardware feature to simplify concurrent code when implementing atomic bit manipulation on Cortex M3 processors.

Newsletter Form (#3)

More ARM insights right in your inbox

 


Share This Article
Facebook Twitter Email Copy Link Print
Previous Article Why ARM Cortex M3 Bit Manipulation is Atomic?
Next Article Leveraging Bit Banding for Atomic Register Access in ARM Cortex M3
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

2k Followers Like
3k Followers Follow
10.1k Followers Pin
- Sponsored-
Ad image

You Might Also Like

What is Instruction TCM (ITCM) Memory in Arm Cortex-M series?

Instruction TCM (ITCM) is a small, fast memory region that…

7 Min Read

Does GCC work on ARM?

Yes, the GNU Compiler Collection (GCC) does work on ARM…

9 Min Read

Debugging issues when porting the Cortex-M1 DesignStart project

Porting the Cortex-M1 DesignStart project to a new hardware platform…

9 Min Read

How to implement atomic operations on multi-core Cortex-M0/M0+?

Atomic operations allow thread-safe access to shared resources without the…

7 Min Read
SoCSoC
  • Looking for Something?
  • Privacy Policy
  • About Us
  • Sitemap
  • Contact Us
Welcome Back!

Sign in to your account