SoC
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
  • Arm Cortex M3
  • Contact
Reading: Atomicity of 32-bit writes on 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

Atomicity of 32-bit writes on ARM Cortex M3

Neil Salmon
Last updated: October 5, 2023 9:55 am
Neil Salmon 6 Min Read
Share
SHARE

The ARM Cortex M3 processor implements atomic 32-bit writes, meaning writes of 32-bit values are guaranteed to complete without interruption. This ensures data integrity when writing to memory-mapped peripherals or shared memory locations.

Contents
Introduction to AtomicityAtomicity in the ARM Cortex M3Implementation DetailsEffects on CodeWhen Atomicity MattersAccessing Memory-Mapped PeripheralsSharing Data Between TasksStack OperationsWhen Atomicity is Not NeededDisabling AtomicitySummaryConclusion

Introduction to Atomicity

Atomicity refers to whether an operation can be interrupted midway through its execution. An atomic operation will either complete fully or not at all – it cannot be left partially finished. This is critical for operations like reading or writing data, where an incomplete write could corrupt the data.

For example, consider writing a 32-bit value to a peripheral register. This may require multiple bus transactions – writing the upper 16 bits, then the lower 16 bits. If the write gets interrupted after the first 16 bits, the register will contain corrupted data. Atomicity prevents this by ensuring the full 32-bit write completes without interruption.

Atomicity in the ARM Cortex M3

The ARM Cortex M3 processor core implements atomicity for 32-bit writes in hardware. This applies to any 32-bit write, including:

  • Writing to memory-mapped peripheral registers
  • Writing to shared memory locations
  • Storing 32-bit values to the stack

The processor ensures these operations complete atomically regardless of interrupts or context switches. For example, a 32-bit write will never be interrupted halfway and resumed later.

Implementation Details

The Cortex M3 achieves atomic 32-bit writes by detecting access sizes and blocking interrupts as needed. If a 32-bit write is detected, interrupts are disabled for the duration of the write. This prevents the write being interrupted partway through.

The processor also contains transaction buffers to queue pending writes. This allows subsequent writes to proceed while an earlier atomic 32-bit write completes. Overall, this hardware implementation provides efficient atomic 32-bit writes without impacting interrupt latency.

Effects on Code

Atomic 32-bit writes happen automatically in hardware, so Cortex M3 code does not have to do anything special to benefit. Any 32-bit store instruction will be atomic, including:

  • STR – Store 32-bit register to memory
  • PUSH – Store 32-bit register(s) to stack
  • STM – Store multiple 32-bit registers to memory

Code does not have to check for atomicity or disable interrupts – the processor takes care of it. This simplifies coding and provides reliable atomic access.

When Atomicity Matters

Atomic 32-bit writes are useful in several cases:

Accessing Memory-Mapped Peripherals

Many peripherals like UARTs and timers use 32-bit registers mapped into the CPU’s memory space. Writing to these registers requires atomic 32-bit accesses to avoid corrupting the peripheral state.

For example, writing a 32-bit timer reload value could require two 16-bit writes. If interrupted in the middle, the timer would be left with an incorrect reload count, causing timing issues. The Cortex M3’s atomic writes prevent this.

Sharing Data Between Tasks

In a RTOS environment, atomicity is very important when sharing data between tasks. If a 32-bit variable is written by one task and read by another, it must be accessed atomically.

The Cortex M3 ensures each 32-bit read or write completes indivisibly. This prevents corrupted data when switching between tasks.

Stack Operations

Pushing/popping 32-bit values to the stack must also be atomic. If interrupted during a multi-word push or pop, the stack pointer could be left in an invalid state leading to hard faults.

The Cortex M3 hardware avoids this by performing all stack operations (push, pop, store, load) as atomic 32-bit accesses, preventing stack corruption.

When Atomicity is Not Needed

While atomic 32-bit accesses are useful in many cases, they are not always needed. Some cases where atomicity is not required:

  • Writing to non-shared variables only accessed by one task
  • Reading 32-bit values (reads are naturally atomic on Cortex M3)
  • 16-bit or 8-bit accesses (performed non-atomically)
  • Memory-backed peripheral registers (non-shared so non-atomic access is safe)

In these cases, the overhead of atomic accesses is unnecessary. The Cortex M3 performs these accesses without blocking interrupts, reducing latency.

Disabling Atomicity

In rare cases, atomicity may need to be disabled to reduce interrupt blocking. For example, with very short ISRs where interrupt latency must be minimized.

This can be done globally by clearing the Programmer’s Model > Atomicity Enforcement bit in the Auxiliary Control Register. However, atomicity should only be disabled with great care as it can quickly lead to data corruption issues.

Summary

The Cortex M3 processor implements automatic atomic access for 32-bit data writes, ensuring they complete indivisibly. This prevents data corruption and simplifies software development. Atomicity should be leveraged when accessing shared data structures, peripheral registers, stacks, and other 32-bit values that require an uninterrupted write. For non-shared data, atomicity can be disabled to prioritize low-latency interrupt handling.

Conclusion

Atomic 32-bit writes are a key feature of the Cortex M3 architecture. By understanding how, when, and why to use them, developers can write efficient embedded software that ensures data integrity. Atomic accesses handle the intricacies of shared memory access, simplifying concurrent and RTOS programming.

Overall, the Cortex M3’s atomic write support enables robust and reliable software without imposing overhead during single-threaded or non-shared memory access. This combination of performance, determinism, and reliability makes the Cortex M3 a versatile processor for demanding embedded applications.

Newsletter Form (#3)

More ARM insights right in your inbox

 


Share This Article
Facebook Twitter Email Copy Link Print
Previous Article What is the Function of the ARM Cortex M3?
Next Article Using Mutexes for Thread Safety on 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 the stack frame of the ARM Cortex exception?

The ARM Cortex exception stack frame is the region of…

9 Min Read

Step-by-Step Guide to Cortex-M0 PendSV Exception Handling

The Cortex-M0 processor implements a basic exception model with 7…

6 Min Read

FreeRTOS Context Switch

A context switch is a fundamental part of multitasking real-time…

5 Min Read

Changing Interrupt Priority on Cortex-M Microcontrollers

Cortex-M microcontrollers allow developers to assign different priority levels to…

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

Sign in to your account