Subsection 8.3.4 The Interrupt Mechanism, in Full
The pre-class video walks this machinery in motion; this is the written version. Start with what the CPU does normally (Figure 8.3.4): the program counter (PC) points at the next instruction in flash; the instruction is fetched, decoded, and executed; the PC advances; repeat. You met this machinery (the register file, SP, LR, and PC) in Subsection 1.6.1.
A function call interrupts that straight line deliberately (Figure 8.3.5). The code for a function is stored in a different place in flash than the code that calls it. The processor stores the address of the next instruction after the call, so that it can return to it, and loads the PC with the function’s start address. Compiler-generated code at the top of the function then saves whichever registers that function will disturb, and the function runs. On return, the saved registers are restored and execution continues at the instruction after the call. What gets saved is the compiler’s decision, made when the code was built, and the stack is what makes the round trip safe: everything needed to pick up where the program left off was saved before it was disturbed.
f() (2). Compiler-generated code at the start of f() pushes the registers f() will disturb onto the stack (3), and f() runs (4). At the end those registers are popped (5), the address to return to is loaded into the PC, and the processor resumes at the next instruction after the call (6). Solid arrows show something being moved or stored; dashed ones show the instruction the PC points at.An interrupt is a function call that hardware makes (Figure 8.3.6). When a peripheral raises its interrupt signal, the CPU finishes the instruction it is on, pushes the machine state onto the stack, much as a function call saves its registers, and then looks up where to jump in a vector table: a table in flash, at a fixed location, holding the starting address of one handler function per interrupt source. The CPU loads that address into the PC and runs the function, called an Interrupt Service Routine. When it returns, the saved state is popped and the interrupted program resumes, unaware anything happened. An interrupt can arrive at any instruction, even in the middle of
delay_ms(), and the stack machinery makes the detour invisible to the interrupted code.
TIM14_IRQHandler(), at the address it finds in the vector table (3), and the ISR runs (4). When it returns, the machine state is popped (5) and execution resumes where it left off, at the next instruction (6). Solid arrows show something being moved, stored or signaled; dashed ones show the instruction the PC points at.
Rule 1: the name is fixed, and it must be exact. You never call an ISR (hardware does, through the vector table) so the toolchain must know which of your functions to wire into which table slot. It does this by name: each interrupt source has one official handler name, and defining a function with exactly that name places it in the table. Get one character wrong and nothing complains: your function is simply never wired in. In class you will look up where these names come from rather than taking them on faith. Notice what is absent here: there is no
register_handler(my_function) call anywhere, and nothing to pass a function to at run time: the linkage happens once, at build time, when the linker matches your function’s name to the table slot. For the same reason, an ISR takes no arguments and returns nothing: hardware, not your code, is the caller, and it has nothing to pass and nowhere to put a result.
Between the peripherals and the CPU sits a referee: the Nested Vectored Interrupt Controller (NVIC), shown in Figure 8.3.7. Every interrupt request (IRQ), from timers, UARTs, GPIO pins, the ADC, arrives at the NVIC, which decides whether and when the CPU sees it. Two of its jobs matter to us now. First, enabling: each interrupt source has an enable switch in the NVIC, off by default, so no peripheral can interrupt until you allow it. Second, ordering: if two interrupts arrive together, the NVIC services the higher priority one first. The Cortex-M0+ allows you to change an IRQ’s priority level; in this course we will leave every source at its reset default priority.

