What is hardware-in-the-loop (HIL) testing, and how do you set up CI for firmware?
Short answer: CI for firmware builds every configuration and target on each commit, runs static analysis and host unit tests, checks code size, and produces versioned artifacts. Hardware-in-the-loop testing then flashes the build onto real boards (or a board farm) connected to test equipment that simulates the environment — sensors, buses, power supplies, network — and runs automated system tests that check behaviour, timing and recovery. Simulation-based testing (QEMU, Renode) sits in between: whole firmware images on virtual hardware, faster and easier to scale than real boards.
Typical pipeline
commit → build (all boards, debug + release, warnings as errors)
→ static analysis (cppcheck/clang-tidy/MISRA checker), formatting check
→ host unit tests + sanitizers + coverage report
→ size report (fail if flash/RAM grows beyond a budget)
→ simulation tests (Renode/QEMU) for boot, protocols, RTOS behaviour
→ HIL: flash test boards, run system tests (pytest + serial/CAN/SPI tools, power cycling)
→ artifacts: signed images, map files, ELF with symbols, release notes
HIL rig components: a host PC or Raspberry Pi per rack, debug probes for flashing, USB-controlled relays/power switches for power cycling, programmable supplies (brown-out tests), signal generators or DACs to simulate sensors, CAN/UART/SPI adapters, logic analysers with scripted captures, and a test framework (pytest, Robot Framework, LabGrid, Lava).
What HIL catches that unit tests cannot: timing and interrupt interactions, peripheral configuration errors, boot and update procedures (including failed updates and rollbacks), power-loss behaviour, long-duration stability (leaks, counter overflows), and hardware revision differences.
Tips: make the firmware testable — a test command shell or RPC over UART/USB, deterministic logs, and version reporting — and keep HIL tests stable (flaky tests get ignored).