Skip to content

Commit 3141092

Browse files
committed
Explain volatile
1 parent 9174f26 commit 3141092

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -913,12 +913,13 @@ and compare generated assembly code:
913913
bx lr bx lr
914914
```
915915
916-
Long story short: if there is no `volalile`, the `delay()` function will
917-
loop forever and never return. Because it caches (optimises) the value of
918-
`s_ticks` in a register and never updates it. The generated code with
919-
`volatile`, on the other hand, loads `s_ticks` value on each iteration.
920-
So, the rule of thumb: those values in memory that get updated by interrupt
921-
handlers, or by the hardware, declare as `volatile`.
916+
Long story short: if there is no `volalile`, the `delay()` function will loop
917+
forever and never return. Because it caches (optimises) the value of `s_ticks`
918+
in a register and never updates it. A compiler does that because it doesn't
919+
know that `s_ticks` can be updated elsewhere - by the interrupt handler! The
920+
generated code with `volatile`, on the other hand, loads `s_ticks` value on
921+
each iteration. So, the rule of thumb: those values in memory that get updated
922+
by interrupt handlers, or by the hardware, declare as `volatile`.
922923
923924
Now we should add `SysTick_Handler()` interrupt handler to the vector table:
924925

0 commit comments

Comments
 (0)