Embedded firmware development
How embedded firmware is written for microcontrollers, covering peripherals, interrupts, timing, real-time operating systems, debugging and the reliability practices needed for devices that run unattended.
What it is
Embedded firmware is the software that runs directly on a microcontroller (MCU) or small processor inside a device, such as a thermostat, motor drive, medical sensor, drone controller, instrument or appliance. Unlike programs on a computer, firmware runs without a conventional operating system, on very limited memory (kilobytes to a few megabytes) and processing power, interacts directly with hardware through peripherals, often has strict timing requirements, and must run for months or years without a restart. It is typically written in C or C++ (Rust and MicroPython are growing), for architectures such as ARM Cortex-M (STM32, nRF52, RP2040), ESP32 (Xtensa/RISC-V), AVR and PIC. Firmware engineering turns circuit boards and sensors into working products and research instruments.
It answers questions such as: How does a program read a sensor, control a motor, or talk to another chip? How can a device respond to events within microseconds? How can it run for a year on a battery? How can it be updated safely in the field, and how can it recover from faults? Because mistakes can freeze, damage or endanger a device, discipline and testing matter.
Fundamentals
Microcontroller, memory and startup
An MCU integrates a CPU core, flash memory (non-volatile, holds the program and constants), SRAM (volatile, holds variables, stack and heap), clocks and peripherals on one chip. At reset the hardware reads a vector table, which gives the initial stack pointer and the address of the reset handler; the startup code copies initialized data from flash to RAM, zeroes uninitialized variables, configures clocks and calls main(). A linker script defines the memory map. Memory is scarce, so avoid dynamic allocation after startup (heap fragmentation), watch the stack depth (stack overflow is a classic cause of mysterious crashes), and use const data in flash.
Peripherals and interfaces
- GPIO: digital pins as inputs (with pull-up or pull-down) or outputs; mechanical buttons need debouncing (hardware or a 5 to 20 ms software filter).
- Timers and PWM: generate periodic interrupts, measure time and drive motors or LEDs; PWM duty cycle D = ton/T, and the average output is D × Vsupply.
- ADC: converts voltages to numbers; one least-significant bit equals Vref/2N, so a 12-bit ADC with a 3.3 V reference resolves about 0.8 mV; sample rate, source impedance, anti-aliasing and noise limit real accuracy. DAC does the reverse.
- UART (asynchronous serial; both sides must agree on baud rate, with error kept below about 2 to 3%), SPI (fast, four wires, with clock polarity and phase modes 0 to 3), I2C (two wires, addressed devices, needs pull-up resistors), plus CAN, USB, Ethernet and wireless radios.
Interrupts, concurrency and real-time operating systems
Two main program structures exist. A bare-metal super-loop runs initialization, then an endless loop that polls inputs and does tasks in sequence, plus interrupt service routines (ISRs) that respond to hardware events. It is simple but timing becomes hard to guarantee as the program grows. A real-time operating system (RTOS) such as FreeRTOS or Zephyr provides a scheduler that runs multiple tasks by priority, with queues, semaphores, mutexes and timers for communication and synchronization; it makes deadlines easier to meet and code easier to organize, at the cost of RAM and some complexity. Rules for ISRs: keep them short (set a flag or push data to a queue and return), never block or wait inside them, and mark variables shared between an ISR and main code as volatile and protect multi-step updates (disable interrupts briefly or use atomic operations or queues), because race conditions cause rare, hard-to-reproduce faults. In an RTOS, beware of priority inversion (a low-priority task holding a lock needed by a high-priority one; mitigated by priority inheritance) and deadlocks. Real-time means predictable timing, not simply speed: worst-case response time must be known and bounded, so avoid unbounded loops and long critical sections.
Debugging, testing and reliability
Tools: a debug probe over SWD or JTAG with GDB or an IDE debugger (breakpoints, register and memory views, trace), serial logging over UART (keep logging cheap in time-critical code), a logic analyzer and oscilloscope for signals and timing, and a multimeter and power analyzer for current. Test the logic by writing it independently of hardware so that it runs in unit tests on a host computer, with hardware-in-the-loop tests for the integration; use static analysis and coding standards such as MISRA C, compiler warnings as errors, and code review. Reliability features: a watchdog timer that resets the MCU if the software stops “kicking” it; brown-out detection; input validation and sanity checks on sensor values; safe default states for outputs at reset; non-volatile storage with wear-leveling and checksums; a bootloader with signed, verified images and rollback for over-the-air (OTA) updates; and secure boot and readout protection for security.
Typical applications and limits
Limits: constrained resources, hardware-dependent behavior, difficult observability, long edit-flash-debug cycles, and the need for rigorous processes when failures have serious consequences.
Basic operation
- Define the requirements. State functions, timing, power, interfaces and failure behavior.
- Choose the MCU and toolchain. Check peripherals, memory, power and available libraries.
- Bring up the board. Configure clocks, blink an LED and print a serial message.
- Write and test drivers. Verify each peripheral with simple tests and instruments.
- Build the application. Structure it as a super-loop or RTOS tasks with interrupts for events.
- Test and debug. Use unit tests on a host, a debugger, logging and a logic analyzer.
- Harden. Add a watchdog, input checks, safe states and an update mechanism.
- Collect the output. You obtain a flashed, tested firmware image; interrupt design and RTOS use are covered in the video.
Good practice
- Read the datasheet and errata. Hardware quirks cause many bugs.
- Keep ISRs short. Defer work to the main loop or tasks.
- Protect shared data. Use volatile, atomics or queues.
- Use a watchdog. Make recovery from lockups automatic.
- Use version control and automated builds. Tag the exact firmware released.
- Design for safe failure. Outputs that heat, move or deliver energy need defined fail-safe states, independent hardware limits and tests of fault behavior, with applicable safety standards followed for medical, automotive and industrial products, and updates must be authenticated to resist tampering.
Key terms
- Microcontroller — a chip with processor, memory and peripherals for embedded control.
- Interrupt — a hardware signal that temporarily diverts the processor to handle an event.
- RTOS — an operating system with a scheduler designed for predictable timing.
- Peripheral — a hardware block such as a timer, ADC or UART that the firmware controls.
- Watchdog timer — a timer that resets the device if the software fails to service it.
- Bootloader — a small program that starts the device and can load updates.
- Race condition — a fault arising from the timing of concurrent accesses to shared data.
Video coming soon
About five minutes on Embedded firmware development: how it works in detail, how the data is acquired, and how it is analyzed.
More in 4.5 Signal Processing, Control and Embedded Systems
