Unit content
Volatile hardware-register access in C
A memory-mapped device register can change because of hardware and can perform an action when software reads or writes it. Those accesses are therefore observable effects, not ordinary memory operations that a compiler may freely remove or merge.
In C, a volatile-qualified object tells the compiler that accesses to that object must occur according to the language's rules for volatile evaluation.
A typical register definition may expose a fixed address through a pointer to volatile storage, then use masks to update documented fields.
volatile is deliberately limited. It does not make a multi-step operation atomic, does not protect data shared between concurrent tasks, and is not a general replacement for hardware memory-ordering primitives.
The qualifier is appropriate when the program must perform accesses because something outside ordinary C execution can observe or change the object, such as a peripheral register.
Correct register access therefore combines the device's hardware semantics with the language's rules about what machine accesses the compiler must preserve.