Skip to content

Commit 089ef8f

Browse files
committed
Better note about volatile
1 parent 2cec9f6 commit 089ef8f

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,9 @@ from A0 to A15, to input mode:
156156
* (volatile uint32_t *) (0x40020000 + 0) = 0; // Set A0-A15 to input mode
157157
```
158158

159-
Note the volatile specifier. The meaning of it will be covered later. By
160-
setting individual bits, we can selectively set specific pins to a desired
159+
> Note the `volatile` specifier. Its meaning will be covered later.
160+
161+
By setting individual bits, we can selectively set specific pins to a desired
161162
mode. For example, this snippet sets pin A3 to output mode:
162163

163164
```c
@@ -868,6 +869,12 @@ void SysTick_Handler(void) {
868869
}
869870
```
870871
872+
> The `volatile` specifier is required here becase `s_ticks` is modified by the
873+
> interrupt handler. `volatile` prevents the compiler to optimise/cache
874+
> `s_ticks` value in a CPU register: instead, generated code always accesses
875+
> memory. That is why you will find `volatile` keywords later in the
876+
> peripheral, too.
877+
871878
Note the `volatile` specifier for `s_ticks`. Any variable that is changed
872879
by the IRQ handler, must be marked as `volatile`, in order to prevent the
873880
compiler to optimize the operation by caching its value in a register.

0 commit comments

Comments
 (0)