Bare metal: the super-loop and interrupts
The simplest firmware is a main loop and a few interrupt handlers, with no operating system. It handles more than you might expect.
Read firstMicrocontroller or microprocessor?
Key points
- A bare-metal program runs one main loop that polls inputs and does work in turn, with no operating system; interrupts let hardware events pre-empt the loop.1
- Arm Cortex-M cores include a Nested Vectored Interrupt Controller (NVIC), which lets a higher-priority interrupt pre-empt a lower-priority one that is already running.2
- Interrupt handlers should do the minimum, such as capturing data and setting a flag, and leave processing to the main loop; long handlers delay every other interrupt of equal or lower priority.3421
Common mistakes
- Sharing a variable between an interrupt and the main loop without protecting it, then chasing a bug that shows up once a day.
Where to go next
- Real-time operating systems builds on this
- Debug probes and bench instruments builds on this
Spotted a mistake, or found this useful?
Tell usSources
- Building Bare-Metal ARM Systems with GNU (foreground/background architecture) · Quantum Leaps (Miro Samek) · Reference
- Armv7-M Architecture Reference Manual (DDI 0403E.b), B1.5.4 and B3.4 · Arm · Reference
- FreeRTOS: Deferred interrupt handling · FreeRTOS (AWS) · Project docs
- Mastering the FreeRTOS Real Time Kernel (FreeRTOS Kernel Book) · FreeRTOS (AWS) · Project docs