5.5.20 · D1Embedded Systems & Real-Time Software

Foundations — Software testing in embedded — unit tests on host, HIL testing

2,145 words10 min readBack to topic

Before you can read the parent note, you must own every word and squiggle it uses. We build them one at a time — each one earns its place before the next arrives.


1. The two machines: host vs target

The parent note keeps saying "host" and "target". These are just two different computers.

Look at the two boxes below. Same program, two very different homes.

Why the topic needs this: every sentence in the parent is about which of these two machines runs a given test. "Test on host" means the left box; "HIL" means the right box wired to real sensors.


2. Microcontroller (MCU), and the numbers "32KB RAM"

The parent writes "32KB RAM". Let's earn every piece:


3. Peripherals: the parts that touch the world

The parent throws around GPIO, ADC, PWM, UART, CAN, timer, interrupt, ISR. These are all peripherals — little helper circuits inside or beside the MCU that connect it to the physical world. Take them one at a time.

Why the topic needs all this: these peripherals are exactly the parts that do not exist on your laptop. That is the whole problem the topic solves — and the reason for the next idea.


4. The register: talking to hardware directly

The parent's "BAD" code says ADC1->DR and TIM2->CCR1. These are registers.

  • ADC1->DR = read the ADC's Data Register → get the latest converted number.
  • TIM2->CCR1 = write timer 2's Compare register → set a PWM level.

5. Abstraction, HAL, mock, stub

These four words are the beating heart of "unit tests on host".

Now the trick: because everyone knocks on the door and not the machinery, we can put different machinery behind the same door depending on where we run.

Why the topic needs this: the whole "test on host" tier only works because behind each HAL door we can swap the real register-poker for a mock we control from our test.


6. Symbols inside the example algorithms

The parent's worked examples use two real control algorithms. You don't need to master them, but you must recognise their symbols.

6a. The PID controller symbols

A PID controller steers a measured value toward a target. Its update line uses these:

Why the topic needs this: the parent's PID test checks fabs(pid.integral) < 0.01. fabs is just absolute value (distance from zero, sign removed). The test says: after perfect tracking, the memory-of-past-error should not drift away — a pure-math property provable on the host.

6b. The PWM / throttle numbers

The HIL example maps a throttle voltage to a PWM percentage. Two conversions appear:

Why the topic needs this: the parent asserts pwm_duty should be within of , and that ADC should cap PWM at . These are the exact numbers we verify below.


7. Timing symbols: , , μs, ms

Why the topic needs this: host tests mock time — they pretend time passes — so they physically cannot measure real microseconds. That gap is the entire reason HIL exists.


8. HIL vocabulary

Related tools you'll meet later: Real-Time Operating Systems (RTOS) (schedules the tasks whose timing HIL checks), Fault Injection Testing (deliberately breaking inputs), and Continuous Integration for Embedded (running all this automatically).


Prerequisite map

Host vs Target machines

MCU with tiny RAM

Peripherals GPIO ADC PWM UART

Registers touch hardware

HAL doors over registers

Mocks and Stubs swap machinery

Unit tests on Host

Interrupts and ISR timing

Real-time deadlines in us and ms

HIL testing on real board

Two-tier testing strategy


Equipment checklist

Answer each aloud before reading the parent note.

What is the difference between the host and the target?
Host = the roomy computer you develop on; target = the tiny MCU your firmware finally runs on.
How many bytes is 32KB?
bytes.
What does an ADC do?
Turns a real smooth voltage into a whole number the MCU can compute with.
What does a DAC do?
The reverse — turns a number into a real voltage on a wire.
What is PWM in one line?
Switching a pin on/off fast so the average acts like an in-between power level.
Why can't a function that reads ADC1->DR run on your laptop?
There is no such register on the host; that address points to nothing.
What is a HAL?
A tidy set of hal_... "doors" so code never touches registers directly, letting us swap implementations.
Mock vs stub?
Stub returns a fixed hardcoded value; a mock is programmable and records how it was called.
What is fabs?
Absolute value — the distance from zero, sign removed.
Does real-time mean fast?
No — it means on time, before the deadline, every time.
How many microseconds are in one millisecond?
.
What voltage is 50% of a 3.3V supply?
.
What number does a 12-bit ADC give at full scale?
(that is ).
What is the DUT in a HIL rig?
The Device Under Test — the real board running your firmware.