Notes /Cortex-M /volatile
volatile
The purpose of 'volatile' is to provide a hint to the compiler not to make certain assumptions about a variable. In particular, suppose on a given architecture, you have a piece of code that reads a word from memory into a register. Twenty instructions later, the code uses that variable again, and a smart compiler will say "oh, I already loaded this variable into a register, and I know I haven't touched it since then, so I don't need to load it again."
That's all fine and wonderful in a single-threaded application, but what happens if you have multiple threads that can access the same variable: ie a global that is accessed by two different tasks, or by a mainloop and an interrupt? What happens if your code has a loop that's waiting for somebody else to change that variable? You'd never see it change because you're working off a copy of the data, and not the data as currently stored in memory. "Volatile" provides a way to force the compiler to reload the variable every time it's accessed.