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.
Figure8.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:
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?
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.
Figure8.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.
Figure8.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.
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.
Figure8.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.
Step 1 of 3: the timer must raise the interrupt. So far our timer has counted and set a notification via the UIF flag but it didnโt interrupt anything when it reached its counting target.
The C constant you just used, TIM14_IRQn, is defined in the device header (stm32c031xx.h): it defines one constant per populated row in the table, mirroring the position column.
Where it comes from: the startup file, startup_stm32c031c6tx.s, an assembly file generated with your project (it sits in the project tree alongside Src) that runs before main() and wires each official handler name into its vector-table slot.
The startup fileโs vector table: this is the file you copy the handler name out of. Counting from WWDG_IRQHandler as position 0, TIM14 lands on 19, the same 19 as Table 40 and as TIM14_IRQn. Note ADC_IRQHandler against the headerโs ADC1_IRQn: the handler name is not the constant with its ending swapped.
The slot keeps what the startup file put there. Standard handler names are declared as weak symbols pointing at a generic Default_Handler, an infinite loop.