ENع
←Firmware tests
Lesson S5 · Special topics

Testing firmware on your laptop

12 min10 steps5 questionsIntermediate
About this lesson

Trying every change on the board is slow, and a cold room or an exact set point is hard to make on demand. Split the thermostat's firmware so its logic has no pins, compile that logic on your laptop, swap the sensor for a fake, and check it with Unity, a C test framework for embedded code. Watch a test catch a < that became <=, then see what a laptop cannot catch: on an Arduino Uno R3 an int is 16 bits, on the laptop 32. The ideas of a unit test and an assertion are covered in the Software lesson “Unit testing” and the Python lesson “Testing with pytest”; here we apply them to C firmware.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig S5 · Interactive
01 / 10

Testing on the board is slow

To try a change on the board you compile, upload over USB, then watch the serial monitor. A cold room is hard to make on demand. And nothing checks the old cases again for you.

edit -> compile -> upload -> watch serial output
every try needs the board, the cable and the sensor
cold, hot, the exact set point: hard to make for real
Loading figure…
or ← →
Read the lesson step by step
  1. Testing on the board is slow

    To try a change on the board you compile, upload over USB, then watch the serial monitor. A cold room is hard to make on demand. And nothing checks the old cases again for you.

    edit -> compile -> upload -> watch serial output
    every try needs the board, the cable and the sensor
    cold, hot, the exact set point: hard to make for real
  2. Split logic from hardware

    Split the code. tempC and needHeat are plain C: no pins. Only read_raw() touches hardware, and on the Uno it calls analogRead(A0). That one thin function is the seam where hardware plugs in.

    temp.c   the logic, no pins
      int tempC(int raw) {
        return raw*500L/1024 - 50;
      }
      bool needHeat(void) {
        return tempC(read_raw()) < 20;
      }
    
    hal_uno.c   the hardware layer (on the board)
      int read_raw(void) {
        return analogRead(A0);
      }
  3. Compile it on the laptop

    The laptop's own compiler, such as gcc, turns temp.c into an ordinary program for the laptop. Same source file, different processor. A program built to run where it was compiled is called native.

    temp.c  -> avr-gcc  -> firmware for the ATmega328P
    temp.c  -> gcc      -> a native program on the laptop
    same source, two compilers, two processors
  4. Swap in a fake sensor

    The test build links a fake read_raw() instead. It just returns fake_raw, a variable the test sets. Now any temperature is one line away, with no board and no sensor. A stand-in like this is called a test double.

    hal_fake.c   linked only into the test program
      int fake_raw;
      int read_raw(void) {
        return fake_raw;
      }
    the logic calls read_raw() and never knows the difference
  5. Arrange, then act

    test_cold arranges: fake_raw = 141. Then it acts: needHeat() reads 141 from the fake, tempC turns it into 18 degrees, and 18 < 20, so it returns true.

    arrange  fake_raw = 141
    act      needHeat()
             read_raw()  -> 141
             tempC(141)  = 141*500/1024 - 50 = 18
             18 < 20  -> true
  6. Assert: Unity checks it

    TEST_ASSERT_TRUE(on) is the assert step. Unity, a C test framework built for embedded code, checks the value and reports PASS. Had needHeat() returned false, Unity would report FAIL for test_cold.

    assert   TEST_ASSERT_TRUE(on)   on = true
    Unity reports test_cold: PASS
    
    main() runs every test:
      UNITY_BEGIN();
      RUN_TEST(test_cold);
      return UNITY_END();
  7. Test the edge: exactly 20

    Bugs hide at edges. test_edge sets fake_raw = 144, which is exactly 20 degrees. 20 is not below 20, so it expects false. A real room would almost never sit there on cue.

    void test_edge(void) {
      fake_raw = 144;          // 144*500/1024 - 50 = 20
      TEST_ASSERT_FALSE(needHeat());
    }
    20 < 20  -> false   PASS
  8. A broken change, caught

    Someone edits line 5 to say <= instead of <. Now needHeat() returns true at 20 degrees. test_edge fails on the laptop seconds later, before any firmware reaches the board.

    line 5   return tempC(read_raw()) <= 20;
    test_edge  20 <= 20  -> true, expected false
    Unity reports test_edge: FAIL
    fix the line, run again: PASS
  9. Run them after every change

    Because the tests are native, the whole set reruns after every edit, and a build server can run them on each commit too. Old cases stay checked, so a change that breaks one shows up straight away.

    pio test -e native        (PlatformIO, on the laptop)
    the same tests on a CI server, on every commit
    tests: test_cold, test_edge, ...  all rerun every time
  10. What the laptop cannot catch

    On the Uno an int is 16 bits; on the laptop, 32. Drop the L from 500L and raw*500 overflows on the board for raw above 65, yet every laptop test still passes. So run some tests on the board too.

    int on the Uno     16 bits, at most 32,767
    int on the laptop  32 bits
    raw*500 with raw = 66:  33,000  > 32,767  overflow on the Uno
    500L makes it a long, at least 32 bits on both (32 on the Uno, usually 64 on a laptop), so the multiply fits on both
    timing, interrupts, real pins: test on the board

Quiz Unlocks after the last step.