Skip to content
Tactiverge

School/□ Systems Engineer/Week 33

From source code to running hardware, and testing the join

The code compiled, the tests passed, and the robot still twitched. Where in the chain did it go wrong?

About 40 min · 3 goals · Checked by automated tests

1.Why should I care?

Optimising a system means changing code and measuring hardware. Knowing what happens between the two (and where timing, memory and configuration can silently change) is what makes the measurements trustworthy.

  • ◇ Trace the build chain: source, compiler, assembler, linker, binary image, flashing, boot.
  • ◇ Explain how optimisation settings change speed, size and debuggability.
  • ◇ Plan integration tests that check hardware and software together, including timing.

2.Discover

Write the multiplication program for the Nibble machine and step through fetch, decode and execute. Count the instructions executed for 3 × 4. Then think about what a compiler would have to produce from 'c = a * b' for this machine.

Can't use the interactive? Read a description instead

In the virtual computer Build's Level 4 the learner writes a short program (LOAD, ADD, STORE, JNZ, HALT) that multiplies by repeated addition and steps through fetch, decode and execute for each instruction. Counting instructions executed links source code to the work the hardware actually does.

3.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1

Which tool decides the actual memory addresses where code and data end up?

4.Apply it

Write an integration test plan (five tests) for the greenhouse controller firmware: what is stimulated, what is measured, the pass criterion with a number, and which build configuration is tested.

Open BUILD 008: Create a Virtual Computer →

5.Understand

The chain

Compiler: C or Rust → assembly for the target. Assembler: assembly → machine-code object files. Linker: combines objects and libraries, places code and data at real addresses using a linker script (flash here, RAM there). Image: a binary or hex file. Flashing: a programmer writes it into the chip's flash. Boot: the reset vector jumps to start-up code that sets up the stack and memory, then calls main.

Optimisation levels are trade-offs

-O0: slow, large, every variable visible to the debugger. -O2: fast, usually smaller. -Os: smallest. -O3: aggressive inlining, can be larger. On a microcontroller with 64 KB of flash, size may decide. Benchmark the configuration you will ship: timings measured on a debug build can differ greatly.

What changes between builds

Library versions, compiler versions, flags, linker scripts and configuration headers can all change behaviour. Reproducible builds pin them all and record a version or commit hash in the firmware itself, so a measurement can always be traced to the exact binary that produced it.

Integration testing

Unit tests check functions in isolation on a computer. Hardware-in-the-loop tests run the real firmware on the real board while a test rig feeds sensor signals and measures outputs and timing. Test the joins: clock configuration, interrupt latency under load, behaviour at power-up, and what happens when a sensor is unplugged.

Explain it another way

A recipe (source) becomes a set of kitchen instructions (assembly), which become a printed card filed in the right drawer (linking), which a chef follows from the first step (boot). A mistake can creep in at any stage.

Worked example 1

A firmware image is 58 KB at -O2 and 49 KB at -Os on a chip with 64 KB flash. The -Os build runs a control loop 12% slower. The loop has 30% timing margin. Which do you ship, and what do you record?

-Os leaves room for growth and the 12% slowdown fits within the 30% margin. Record the compiler version, flags and commit hash in the image, and re-run the timing integration test on the -Os build.

Common mix-ups

  • If it compiles and the unit tests pass, the firmware works.
    ✓ Configuration, timing and hardware interaction need integration tests on the real target.
  • Compiler optimisation changes what the program does.
    ✓ It preserves defined behaviour; failures after optimising usually expose bugs such as missing volatile or undefined behaviour.

6.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 2

You benchmark a filter on a -O0 debug build and report its timing. What's wrong?

Practice 3

What can a hardware-in-the-loop test catch that a unit test on a laptop cannot?

Practice 4 · a fresh version every time

After warm-up, the baseline takes a mean of 24 ms per run and the optimised version 10 ms (same machine, same input, interleaved runs). What is the speed-up, baseline time ÷ new time? (3 significant figures.)

×

7.Predict

You switch the compiler from -O0 (no optimisation) to -O2. What usually happens?

Guess first. Being wrong here is how the surprise works.

8.Reflect

Concepts: Inside a microcontroller · Testing and debugging · Real-time computing on robots