Search GimmeJob

Type at least 2 characters. Enter also searches by meaning.
GimmeJob
Sign in
Embedded & IoT · Chapter 08 / 08

Embedded & IoT

Embedded systems need several kinds of evidence because no single test environment reproduces CPU behavior, electronics, radio conditions, physical sensors, cloud services and long-term timing at once.

This chapter explains the engineering toolchain from source-level debugging to hardware-in-the-loop (HIL) and automated device labs.

1. Debugging vs testing

Debugging asks why a known failure happens.

Testing asks whether expected behavior holds across planned conditions and whether unknown failures can be exposed.

They use many of the same tools, but the purpose is different.

2. Build an evidence ladder

A practical embedded verification ladder is:

host/unit tests
      ↓
software simulation / fake peripherals
      ↓
target firmware on MCU with controlled dependencies
      ↓
SIL / emulation where useful
      ↓
HIL with real controller + simulated/controlled plant
      ↓
real sensors/actuators/radios
      ↓
full end-to-end product environment

Higher realism usually means slower execution, higher cost and harder diagnosis.

The goal is not to move everything to HIL. Put each behavior at the lowest layer that can prove it correctly.

3. Host-based unit tests

Logic that does not require actual hardware should often be testable on a development machine.

Examples:

  • packet/parser functions;
  • state machines;
  • unit conversions;
  • filtering;
  • retry policy;
  • command validation;
  • configuration migration;
  • CRC/checksum logic.

Advantages:

  • milliseconds instead of seconds/minutes;
  • easy CI execution;
  • sanitizers/fuzzers available;
  • simple fault injection;
  • deterministic reproduction.

Do not hide pure logic behind hardware access unnecessarily.

4. Hardware abstraction and dependency boundaries

Testable firmware often separates hardware access from domain logic.

Conceptual structure:

application/state machine
        ↓
service/domain interfaces
        ↓
hardware abstraction / drivers
        ↓
MCU registers/peripherals

A fake sensor implementation can drive the same application logic used with the real driver.

This is not only a testing trick; it clarifies architecture.

5. Simulation and emulation

Terms vary by tool, but useful categories are:

Functional simulation

Models behavior without reproducing the target CPU/electronics exactly.

CPU/platform emulation

Runs target binaries/instructions against an emulated machine/peripheral model, e.g. QEMU-supported platforms.

Native RTOS simulation

Some environments let application/RTOS code execute as a host process with simulated drivers.

Simulation is excellent for repeatability but only proves what the model includes.

6. SIL

Software-in-the-loop (SIL) puts production/control software against a software model of its surrounding system.

Example:

motor-control algorithm
        ↕
software motor model

SIL is useful before real power electronics or mechanical hardware is available.

It does not reveal real ADC noise, wiring faults or transceiver behavior unless explicitly modeled.

7. HIL

Hardware-in-the-loop connects real target hardware to controlled equipment that stimulates inputs and observes outputs.

Example:

HIL controller / DAQ
   ├─ generates sensor voltage
   ├─ toggles digital fault input
   ├─ simulates CAN messages
   └─ measures PWM/output
              ↕
        real ECU / MCU board

The device firmware runs on the actual processor/peripherals while the surrounding plant is simulated or controlled.

8. What HIL can simulate

Depending on equipment, HIL can provide:

  • analog voltages/currents;
  • digital inputs;
  • PWM/frequency signals;
  • resistive sensor equivalents;
  • CAN/LIN traffic;
  • serial buses through dedicated interfaces;
  • loads or electronic load models;
  • power supply variation;
  • fault insertion;
  • timing measurement.

Some physical systems cannot be safely or economically reproduced and remain real-device/system tests.

9. DAQ

DAQ = Data Acquisition.

A DAQ device converts between software-controlled test equipment and electrical signals.

It can expose analog inputs/outputs, digital I/O, counters and timing functions.

DAQ is not automatically a full HIL system. HIL adds the real-time plant model, switching/fault hardware, bus simulation and automation architecture needed for the product.

10. JTAG and SWD

JTAG and SWD are common hardware debug/programming interfaces.

Capabilities can include:

  • flash programming;
  • breakpoints;
  • register/memory inspection;
  • stepping;
  • watchpoints;
  • reset control;
  • trace depending on target/probe.

Common probes include vendor/debugger tools such as J-Link, ST-LINK and CMSIS-DAP devices.

Debugging can change timing, especially when execution is halted, so a bug that disappears under a breakpoint may be timing-sensitive.

11. Serial console and logs

UART/USB CDC logs are simple and valuable.

Useful logs include:

  • boot version/build ID;
  • reset cause;
  • state transitions;
  • connection changes;
  • update status;
  • fault counters;
  • timestamps/uptime.

Logging must be bounded so it does not block real-time work, overflow buffers or leak secrets.

12. Logic analyzer

A logic analyzer captures digital signals and can decode protocols such as UART, I²C and SPI.

Use it to answer:

  • Did the MCU actually drive chip select?
  • Was the I²C address correct?
  • Did the sensor NACK?
  • Is the UART baud/framing correct?
  • Is transaction timing consistent with the datasheet?

13. Oscilloscope

An oscilloscope shows the analog electrical waveform.

Use it for:

  • voltage levels;
  • rise/fall time;
  • ringing/reflection;
  • clock quality;
  • PWM timing;
  • reset pulses;
  • power rail droop;
  • current measurement with suitable probes/shunts.

If a logic analyzer says a bit is wrong, the oscilloscope can reveal that the edge never reached a valid voltage in time.

14. Protocol-specific tools

Useful examples:

CAN

  • SocketCAN;
  • candump/cansend;
  • CANoe/CANalyzer;
  • CAN adapters/analyzers.

BLE

  • phone BLE scanner apps;
  • BlueZ tools;
  • nRF Connect-class tools;
  • BLE packet sniffers.

MQTT

  • mosquitto_pub / mosquitto_sub;
  • broker logs;
  • MQTT explorer clients.

IP traffic

  • Wireshark;
  • tcpdump;
  • curl;
  • OpenSSL tools.

Choose the tool closest to the failing layer.

15. Flashing and provisioning automation

A repeatable hardware lab needs deterministic setup.

Automation can include:

  1. identify connected board/serial number;
  2. erase/flash known firmware;
  3. set configuration;
  4. provision test credentials;
  5. power-cycle/reset;
  6. verify boot/version;
  7. run scenario;
  8. collect logs/artifacts;
  9. restore/clean state.

A test that depends on whatever firmware happened to be on the board is not reproducible.

16. Power control

Remote-controlled power is extremely useful.

Options include programmable power supplies, USB power switches, relays or lab controllers.

It enables:

  • cold boot;
  • brown-out scenarios;
  • reboot-loop recovery;
  • OTA interruption;
  • current/power measurements;
  • overnight unattended tests.

Reset-pin toggling is not always equivalent to removing power.

17. Fault injection

Embedded systems should be exercised with controlled failures.

Examples:

  • disconnect sensor;
  • return invalid sensor values;
  • corrupt bus frame;
  • delay response;
  • drop MQTT connection;
  • remove Wi-Fi;
  • exhaust buffer;
  • power off during flash update;
  • force CAN bus-off condition with proper equipment;
  • simulate stuck actuator feedback.

Fault injection should be safe for people and equipment.

18. Test doubles vs real hardware

Use a fake when the behavior under test is not the electrical behavior itself.

Use real hardware when the evidence depends on:

  • timing/peripheral implementation;
  • electrical levels;
  • radio behavior;
  • memory layout;
  • bootloader/flash;
  • real sensor physics;
  • hardware revision interactions.

A good suite intentionally combines both.

19. Test pyramid for embedded systems

A useful shape:

             few full-system/device tests
          HIL + real-hardware integration
       target/peripheral integration tests
    many host unit/property/fuzz tests

This is a cost/feedback model, not a required percentage formula.

If all tests require one physical board, feedback becomes slow and the lab becomes a bottleneck.

20. Determinism and flakiness

Hardware tests become flaky when they depend on uncontrolled timing/state.

Improve determinism by:

  • wait for observable state, not arbitrary sleep;
  • explicit timeouts;
  • stable power/reset procedure;
  • unique device identity;
  • isolated broker/cloud namespace;
  • known RF/environment assumptions;
  • clean persistent state;
  • timestamped logs;
  • retry only when retry itself is product behavior, not to hide failures.

21. Measuring timing

For microsecond/millisecond timing, host wall-clock timestamps may be insufficient.

Use appropriate instruments or target counters:

  • oscilloscope;
  • logic analyzer;
  • MCU cycle counter;
  • hardware timer capture;
  • DAQ timestamping.

Measure at the boundary the requirement actually specifies.

22. CI with hardware

Hardware CI commonly uses self-hosted runners connected to a device lab.

Typical pipeline:

build firmware
  ↓
host/unit tests
  ↓
flash reserved device
  ↓
run target smoke/integration
  ↓
optional HIL suite
  ↓
collect logs + traces + firmware metadata

Lab orchestration needs device reservation/locking so two jobs do not control the same board simultaneously.

23. Artifact capture

When a hardware test fails, retain enough evidence to reproduce it:

  • firmware hash/version;
  • hardware revision/serial;
  • test configuration;
  • reset cause;
  • serial logs;
  • bus captures;
  • power/timing traces when relevant;
  • backend/broker correlation IDs;
  • exact test command and seed.

A red CI result without device evidence wastes expensive lab time.

24. Coverage across hardware revisions

Hardware revisions can change sensors, flash chips, radios, pull-ups, oscillator, PCB routing or power circuits while firmware remains mostly shared.

Track which tests ran on which revision.

Risk-based matrix example:

Test familyRev ARev BRev C
Host unitsame build logicsamesame
Basic boot✓✓✓
Changed sensor—✓✓
RF regressionsampled✓✓
Full HILreleasereleaserelease

25. End-to-end connected-device test

A complete scenario can cross:

physical stimulus
  ↓
sensor
  ↓
firmware
  ↓
BLE/MQTT/etc.
  ↓
gateway/cloud
  ↓
API
  ↓
UI

End-to-end tests are valuable but poor at isolating root cause unless intermediate evidence is captured.

26. Practical example: automated sensor test

Conceptual Python-style flow:

power.set_voltage(3.3)
board.flash("candidate.hex")
sensor_sim.set_temperature(25.0)

assert board.wait_until_booted()
assert board.read_reported_temperature() == 25.0

sensor_sim.set_temperature(80.0)
assert board.wait_for_alarm(timeout=0.5)

The exact libraries depend on lab hardware. The important structure is controlled setup → stimulus → observable result → cleanup.

27. Practical example: communication fault

mqtt.connect()
device.wait_online()

network.block_device()
assert device.queues_telemetry()

network.restore_device()
assert backend.eventually_receives_buffered_data()
assert backend.has_no_duplicate_business_effects()

Again, this is architecture rather than a specific framework requirement.

28. QA quick reference

This is the intentionally testing-focused chapter of the track.

For an embedded/IoT feature, ask which evidence layers are required:

  • pure logic → host/unit test;
  • protocol framing/state → simulator/integration;
  • MCU peripheral behavior → real target;
  • electrical/timing → instruments/HIL;
  • physical sensor/actuator → real hardware or calibrated simulator;
  • radio range/interference → RF/real environment;
  • device-cloud behavior → controlled backend/broker;
  • recovery/lifecycle → power control + persistent state + OTA tooling.

The central rule is do not use expensive hardware to prove what a deterministic host test can prove, and do not use a mock to claim evidence about physics it does not model.

29. Practice

Exercise 1

Place each test at the lowest useful layer: CRC parser, I²C timing, BLE range, OTA rollback, temperature alarm state machine, motor stop deadline.

Exercise 2

Design a HIL rig for a controller with two analog sensors, one PWM output and CAN communication. Identify stimulus, measurement and switching hardware.

Exercise 3

A test passes with a debugger attached but fails at full speed. List timing-related reasons the debugger could mask the defect.

Exercise 4

Design the artifacts you need to diagnose an overnight hardware-CI failure without physically watching the board.

Sources