Subsection 9.3.5 What These Rules Do and Do Not Cover
The argument about changing shared data inside an ISR is correct for the programs in this chapter, and three of its edges are worth drawing before you carry it somewhere else.
Word size. The claim that
main() cannot be caught half-way through reading buttonPushed holds because buttonPushed is a plain int, a naturally aligned 32-bit value, which this compiler loads with a single LDR and stores with a single STR on this core, and the processor takes an interrupt only between instructions. Widen the variable and the argument stops holding: a uint64_t counter, or a struct holding a timestamp and a value, is loaded in pieces, and an ISR that writes it between those pieces leaves the reader with half of one update and half of another. The rule is not “reads are safe”; it is “a read the machine performs in one instruction is safe”.
The consumer’s window. The flag pattern removes the lost-update failure on the producing side and leaves a smaller one on the consuming side: a press arriving between
if (buttonPushed) and buttonPushed = 0; is merged with the press being handled. If you need every event rather than the knowledge that at least one happened (counting presses instead of reacting to them) the test and the clear have to happen with no window between them, which on this chip means bracketing them with __disable_irq() and __enable_irq().
What
__disable_irq() buys, and what it costs. Every ISR in this chapter brackets its body with it, following the course’s drivers, and it is worth being clear that this does not close the read-modify-write race described above. By the time the ISR’s __disable_irq() executes, main() has already been interrupted mid-instruction-sequence; the damage is done by the return, not during the handler. What the bracket does is prevent another interrupt from being serviced while this one runs, and the price is that every other source waits. With one interrupt source that price is invisible. Tonight’s homework puts two in one program, and from there on, the time spent inside a handler is time the other source is not being served. That is the trade production code makes deliberately, usually by masking only the specific interrupt that must not intervene rather than all of them.
