32bitSaviour
DIIDevHeads IoT Integration Server
•Created by Afuevu on 12/21/2024 in #code-review
Struggled with a Bug on My Energy Meter Project
Certainly.
1. So volatile values are used when the those values can change unexpectedly so we do not want the compiler to make any assumptions about the value of the variable.
2. Non-volatile pointer to volatile values. We can use this to read memory-mapped peripheral registers. We know that the pointer will not change but the values stored at that location change and compilers should not be allowed to make any assumptions and optimize things away.
uint32_t volatile *UART_BASE = (uint32_t *) 0x7200005E;
3. Volatile pointer to non-volatile values. A pointer can change but the value in the memory remains unchanged.
uint32_t *volatile ptr = data
. This can be useful when the pointer address might change in a ISR but the data itself is safe to assume as not changing.
4. Volatile pointer to volatile values. Both the pointer and the value are assumed to change unexpectedly. This can be useful when working in embedded environments where for example dynamic memory may change during runtime.
5 replies