Full name: Nested Vectored Interrupt Controller.
Definition
The NVIC (Nested Vectored Interrupt Controller) is the interrupt §6.11 controller built into Arm Cortex-M processors. It receives the interrupt requests of the chip’s devices, keeps an enable bit, a pending bit, an active bit and a priority for each interrupt, and decides which request the processor takes next. Vectored means that the processor fetches each interrupt handler’s §6.12 address directly from a table, without software asking which device interrupted. Nested means that a request of higher priority can interrupt a handler of lower priority that is still running.
The idea
A chip has dozens of devices that can interrupt: timers, a radio, serial ports, a USB controller. Some events cannot wait, such as a radio deadline measured in microseconds; others can wait milliseconds, such as a button press. The processor needs one place that sees every request, knows how urgent each one is, and picks the next handler quickly and the same way every time. Without vectoring, one shared handler would have to read device after device to find the cause, and that time would pass before any useful work started.
The NVIC is that place. A hospital triage desk is a fair picture: every arriving patient gets a recorded urgency, the most urgent waiting case is seen next, and a sufficiently urgent case interrupts a doctor who is treating a less urgent one. The picture stops in two places. A case of the same urgency never interrupts; it waits until the current treatment ends. And the ordinary office work of the hospital, standing for the threads of the operating system, is not on the triage list at all: any patient whom the desk admits outranks all of it.
How it works
For each interrupt the NVIC keeps four pieces of state. Privileged code, code at the processor’s higher privilege level §6.10, reads and writes them through registers that the CMSIS §6.43 (Common Microcontroller Software Interface Standard) library wraps in functions:
enabled: whether the interrupt may be taken at all. The functions
NVIC_EnableIRQ()andNVIC_DisableIRQ()set and clear it.pending: a request has arrived and has not yet been served. Software can set the bit with
NVIC_SetPendingIRQ()and clear it withNVIC_ClearPendingIRQ().active: the handler has started and has not yet returned, possibly because it was itself interrupted.
NVIC_GetActive()reads the bit.priority: a number in which a smaller value means more urgent.
NVIC_SetPriority()sets it.
Each priority is stored in an 8-bit field, but a chip implements only the top few bits of it; the Armv7-M architecture allows 3 to 8. With 3 bits there are levels, stored as 0x00, 0x20, 0x40 and so on up to 0xE0; the unimplemented low bits always read as zero. The CMSIS function NVIC_SetPriority() takes the level number, 0 to 7 in this case, and shifts it into the top bits itself. Arm calls every event that makes the processor run a handler an exception; interrupts are the exceptions that devices request, and system exceptions are those the processor raises itself, such as faults and system calls. The system exceptions use the same priority encoding, set through other registers, except three whose priorities are fixed above every configurable level: Reset at , the NMI (non-maskable interrupt) at and HardFault, the catch-all fault exception, at .
The NVIC and the processor decide as follows.
Execution priority. The execution priority is the priority of the code running now: the most urgent priority among the handlers that are active, or, if none is active, a level below every configurable priority. Masks raise it. Setting
PRIMASKraises it to 0, so that only the NMI and HardFault can interrupt; settingFAULTMASKraises it to ; and a nonzeroBASEPRIraises it to the value ofBASEPRI, if that value is more urgent.Selection. Among the pending, enabled interrupts, the most urgent one is selected. Ties go to the one with the smaller subpriority, explained below, and then to the one with the smaller exception number.
Preemption. The selected request is taken if its priority is more urgent, that is numerically smaller, than the execution priority. A request of equal priority does not interrupt; it waits.
Entry. The processor saves registers on the stack, marks the interrupt active, clears its pending bit, and reads the handler’s address from the vector table §6.14: interrupt number is exception number , whose address sits at byte offset in the table.
Return. When the handler returns, the active bit clears. If a pending request is more urgent than the code being returned to, the processor enters that request’s handler directly, without restoring and saving the registers in between (tail-chaining).
A chip with more priority bits can split them. The field PRIGROUP in the register AIRCR sets a binary point in the 8-bit priority field. The bits above the point form the group priority, which alone decides whether one interrupt may interrupt another. The bits below the point form the subpriority, which only orders pending requests that have the same group priority. CMSIS sets the split with NVIC_SetPriorityGrouping().
A real-time operating system (RTOS) works beside the NVIC, not inside it. Its threads, the independent sequences of instructions that it schedules, run in Thread mode, the processor’s state for code outside exception handlers §6.15, at the execution priority below every configurable level. So every enabled, unmasked interrupt can interrupt every thread. The scheduler picks the thread to run by thread priority, and Cortex-M kernels commonly perform the switch between threads in the exception PendSV, set to the least urgent priority, so that switching happens only after all pending handlers have finished. Many kernels also draw a priority line: the kernel’s own critical sections §9.4, stretches of kernel code that must not be interrupted, mask interrupts only up to that line, so handlers above it run even then, and in exchange they must not call the kernel. FreeRTOS, a widely used open-source RTOS, sets the line with configMAX_SYSCALL_INTERRUPT_PRIORITY; the Zephyr RTOS §6.33 calls the handlers above it zero-latency interrupts.
Worked example
A Cortex-M4 §6.3 chip implements 3 priority bits, as the nRF52840 §6.4 microcontroller §6.1 does, so there are 8 levels, from 0 (most urgent) to 7. All three bits are group priority. A radio interrupt R has level 1, a UART §6.23 (universal asynchronous receiver-transmitter) interrupt U has level 3, and a timer interrupt T also has level 3. In the 8-bit priority fields they are stored as (0x20) and (0x60), because each level is shifted left by bits, and .
U’s handler is running when R is requested. R’s level 1 is smaller than U’s level 3, so R interrupts U at once, and U continues after R returns.
R’s handler is running when U is requested. U stays pending until R returns, and then the processor goes straight into U’s handler.
U’s handler is running when T is requested. T has the same level, 3, so T does not interrupt; it runs after U returns.
The thread with the most urgent thread priority that the RTOS allows is running when T is requested. T interrupts the thread at once, because every configurable interrupt priority is more urgent than thread code.
A common mistake mixes the two encodings. A programmer who wants level 3 for U calls NVIC_SetPriority() with 0x60, the shifted byte. CMSIS shifts again: , which is 0xC00. Only the low 8 bits fit in the field, and those are 0x00. U now has level 0, the most urgent level on the chip, more urgent than the radio.
Stated precisely
Let the 8-bit priority field of exception hold the value , and let be the value of PRIGROUP, from 0 to 7. The group priority is , where means rounded down; it is the value of the bits above bit . The subpriority is , the value of bit and the bits below it. Let be the execution priority, expressed on the same scale as the group priority. A pending, enabled exception preempts the running code exactly when . When several are pending, the one with the smallest is taken first, then the one with the smallest , then the one with the smallest exception number.
For example, with 4 implemented bits and , the values 0x90 (144) and 0xB0 (176) both have group priority 2, since and both round down to 2. Their subpriorities are and . Neither preempts the other, and 0x90 goes first when both are pending. The value 0x40 (64) has group priority and preempts both.
Thread priority is not part of this rule. Thread code runs only when no handler is active, and the RTOS’s thread priority orders threads among themselves only.
Limits and common mistakes
Numbers from the two domains cannot be compared. In Zephyr, a smaller thread priority number also means more urgent, and negative numbers mark cooperative threads §6.37, which the scheduler never preempts in favor of another thread. In FreeRTOS, a larger task priority number means more urgent. Neither number says anything about interrupts.
Equal group priority never preempts. A handler that must interrupt another needs a strictly smaller group priority, and subpriority never causes preemption.
Urgency is not a deadline. A level-0 interrupt still waits while code masks interrupts, while a fixed-priority exception runs, and while another level-0 handler runs. A bound on its delay needs the longest masked stretch and the worst-case execution time §9.33 of every handler that can run first.
Every interrupt, even the least urgent, delays every thread, even the most urgent. A flood of low-priority interrupts can keep all threads from running.
The number of priority bits differs between chips: Armv6-M processors have 2, Armv7-M processors 3 to 8. Code that hard-codes shifted priority values breaks when it moves to a chip with a different number of bits.
A handler placed above the kernel’s priority line is never delayed by the kernel’s critical sections, but it must not call any kernel service, because the kernel’s protection does not cover it.
Questions answered
How do interrupt priority and thread priority differ?
Interrupt priority and thread priority order different kinds of code, are enforced by different mechanisms, and are not comparable numbers. Interrupt priority, on Arm Cortex-M processors a value held in the NVIC (Nested Vectored Interrupt Controller), decides in hardware which pending interrupt is taken first and whether it may interrupt a handler that is already running; a smaller number is more urgent, and only a strictly more urgent group priority interrupts. Thread priority is a number that the scheduler of a real-time operating system (RTOS) uses, in software, to choose which ready thread runs when no interrupt handler is running.
Because threads run only when no handler is active, every enabled and unmasked interrupt, even the least urgent, interrupts every thread, even the most urgent. The two scales also differ in range and direction. A Cortex-M4 chip with 3 priority bits has 8 interrupt levels, 0 being the most urgent. Zephyr thread §6.35 priorities also count smaller as more urgent but use a separate range that includes negative numbers, and FreeRTOS counts larger task priorities as more urgent. The two domains meet in only two places: thread code can mask interrupts for a short time, and the kernel draws a priority line above which handlers may not call kernel services. A thread’s response time §9.32 therefore includes the time of every interrupt handler that can run while the thread waits.
Related entries: Priority inversion (§9.16), Schedulability (§9.37), Interrupt (§6.11), Interrupt service routine (ISR) (§6.12), Vector table (§6.14), Cooperative and preemptive scheduling (§6.37). Further reading: [5, 14, 81, 175].