Many microcontroller datasheets and reference manuals contain instructions like the following [quoted from an ST reference manual]:
- Program the new number of wait states to the LATENCY bits in the FLASH_ACR register
- Check that the new number of wait states is taken into account to access the Flash memory by reading the FLASH_ACR register
- Modify the CPU clock source by writing the SW bits in the RCC_CFGR register...
If the reference manual specified some circumstances in which the value might not take effect immediately, or might fail to take effect at all unless the write is repeated, then code could make allowance for such possibilities. Absent any such specification, how should a diligent programmer be expected to handle step 2? Possibilities would include:
Set FLASH_ACR, check it, and signal a fatal error if the value isn't as expected.
Set FLASH_ACR once, then check it within a do-while loop, looping forever if the correct value never appears.
Set FLASH_ACR once, then check it within a do-while loop, but signal a fatal error after some number of iterations if the correct value does not appear.
Set and check FLASH_ACR within a do-while loop, looping forever if the value never gets set properly.
Set and check FLASH_ACR within a do-while loop, but signal a fatal error after some number of iterations.
Force a read of the hardware register in case that would introduce a necessary delay, and to make the value visible if a debugger breakpoint is set, but otherwise ignore the result.
Ignore step 2 except during development.
It's common for certain operations to have a delayed effect, and for systems to require that code wait for some operations to complete before initiating others. It's unclear, however, in what circumstances FLASH_ACR might fail to become set properly absent chip damage severe enough to render any attempted program execution meaningless. My guess is that the checks are primarily intended as part of a trouble-shooting process in case things don't work as intended, but are not otherwise a necessary part of production code. How do other people handle such checks?