Skip to main content

Subsection 8.2.6 Part 6: Programming an Interrupt

Polling the flag freed the loop from blocking, but the loop still spends every iteration asking. The pre-class videoโ€™s answer is the interrupt: let the update event call a function: push the machine state, vector to your handler, pop, resume (Figureย 8.3.6). One reminder from the video before the mechanics, because it collides with a habit: the ISR we are about to write is an Interrupt Service Routine, a function, not an interrupt and status register like ADC1->ISR; same letters, different thing.
Enable the interrupt in the timer. The timer must be told to raise an interrupt at each update event, not just set the flag. This new behavior is enabled via the UIE bit (update interrupt enable) in TIM14_DIER:
TIM14->DIER |= TIM_DIER_UIE;   // update event now raises an interrupt request
UIE (in DIER) enables the request, meaning the hardware will now request to interrupt the processor when an update event happens; UIF (in SR) reports that the event happened.
Figure 8.2.14. The register map again, with todayโ€™s addition: UIE in TIM14_DIER (interrupt enable register) makes each update event raise an interrupt request. CEN and UIF, from the polled version, are dimmed: they keep their jobs.
Enable the interrupt in the NVIC. When an update event happens the timer hardware now issues a request to interrupt the processor. This request first goes to the NVIC, which, remember, ignores every source until told otherwise. CMSIS (Common Microcontroller Software Interface Standard) provides the function, and the device header provides a named constant for each interrupt line:
NVIC_EnableIRQ(TIM14_IRQn);    // allow TIM14's requests through the NVIC

Activity 8.2.4. Where the Names Come From.

Both TIM14_IRQn (the name we needed to register with the NVIC) and the handler name you are about to need to write the ISR come from different places.

(a)

Before looking: the vector table lists every interrupt source on the chip, numbered from 0. Write down a guess: roughly where in the list does TIM14 sit?

(b)

Now open the reference manual, RM0490 ยง11.3, Table 40, Vector table, and find the TIM14 row. Note its position number, its acronym and its default priority.
Figure 8.2.15. An excerpt of RM0490โ€™s Table 40, the vector table: positions 14 through 23, with the header row and the TIM14 row boxed. TIM14โ€™s interrupt is position 19, its priority is 26 and its vector, the address the CPU fetches the handlerโ€™s location from, is 0x0000_008C. TIM3, TIM16, TIM17, and I2C1 occupy nearby positions.
TIM14 is the name of the hardware. To interact with it in code we need to add on to this name according to somewhat regular patterns.
To register with the NVIC we use TIM14_IRQn: we simply add on _IRQn. This is defined in the device header and it is what NVIC_EnableIRQ() takes.
Figure 8.2.16. The IRQn_Type enumeration in the device header stm32c031xx.h, with the TIM14 row boxed. Every populated row of RM0490โ€™s Table 40 gets one constant here, and its value is the position number from that table: TIM14_IRQn = 19. The gaps in the numbering (1, 8, 15, 17, 18 are absent) are the reserved positions, which have no interrupt on this part.
The third name is what you need to call your ISR. The handler function is TIM14_IRQHandler, and that name is not yours to invent: it is listed in the projectโ€™s startup file, startup_stm32c031c6tx.s: an assembly file, generated with your project, that runs before main() and wires each official handler name into its vector-table slot. It sits in your project tree alongside Src. You will read that file, never edit it: find the table with the IRQ (interrupt request) handler names, select the one you need, copy it.

Activity 8.2.5. Predict: One Character Off.

Suppose you copy the handler name but mistype a single letter, so the function you write is not quite TIM14_IRQHandler. Predict what happens when you build the project, and what the board does when the timerโ€™s update event fires.
A name that is even one character off still compiles and still links: the linker simply never places your function in the table, and the slot keeps the default handler the startup file installed there. Your ISR never runs. Depending on what that default handler does, the board may also stop responding entirely (no LED, no serial output) so a dead board and a dark LED are the same diagnosis, and the handlerโ€™s spelling is the first thing to check for either one.
Figure 8.2.17. The vector table inside the startup file startup_stm32c031c6tx.s, with the TIM14 row boxed. Each .word is one slot, in position order: counting from WWDG_IRQHandler as position 0, TIM14_IRQHandler lands on 19, the same 19 as Table 40 and as TIM14_IRQn. The .word 0 entries are the reserved positions. Note that a handler name is not its _IRQn constant with the ending swapped: the header declares ADC1_IRQn, I2C1_IRQn and SPI1_IRQn, while the handlers here are ADC_IRQHandler, I2C_IRQHandler and SPI_IRQHandler. This is why the handler name is copied from this file rather than constructed.
Give the ISR a way to talk to main. The videoโ€™s flag pattern (written version: Subsectionย 8.3.6) is exactly what todayโ€™s ISR needs, so bring it back to mind: a global flag, declared volatile because the compiler cannot see the ISR ever being called from your code. The ISR sets this variable as a signal to your main code; the loop checks it, clears it, and does the work. Without volatile the optimizer (part of the compiler) may decide the flag never changes from its initial value and optimize it away. As a result the blink simply doesnโ€™t happen, with no error anywhere.
One last piece of housekeeping, visible in the code you are about to complete: the initialization is bracketed by __disable_irq() and __enable_irq(), which turn all interrupts off globally during setup so nothing fires while the timer is half-configured. The ISR does the same around its body, to make sure no interrupt is serviced while another is being serviced. The processor itself is capable of nested interrupts, an interrupt interrupting an interrupt, but imagine keeping track of that. We will not use that capability in this course: every ISR we write disables interrupts globally on entry and re-enables them on exit.