Skip to main content

Chapter 9 GPIO Interrupts

When we talked about timers and interrupts on Thursday, the thing that interrupted the CPU was a timer. Everything else about that class (the NVIC admitting the request, the vector table naming the handler, the ISR staying short, the volatile flag communicating with main()) was the interrupt mechanism, and none of it changes when the interrupt comes from other places. This chapter changes only the interrupt source. A pin can interrupt too, and once it can, a button press is noticed the instant it happens rather than whenever the main loop next gets around to checking.
Compared to timer interrupts, GPIO interrupts need one more piece of hardware configured: the EXTI, the Extended Interrupt and Event Controller, which sits between the pins and the NVIC. We’ll need to configure three registers and use a fourth one inside the ISR. We will also introduce one idea that is not about interrupts at all, but that only becomes visible once you have them: when an ISR and main() both touch the same register, “read it, change it, write it back” can silently lose an update. The last part of the chapter shows how that happens and what the GPIO hardware provides to avoid it.