Testing firmware on your laptop
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
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.
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
اقرأ الدرس خطوة بخطوة
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 realSplit 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); }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 processorsSwap 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 differenceArrange, 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 -> trueAssert: 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();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 PASSA 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: PASSRun 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 timeWhat 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
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01Why can tempC() and needHeat() be compiled and run on a laptop?
The logic is plain C. The one function that talks to hardware, read_raw(), is kept apart, so the laptop's compiler can build everything else.
02In the test build, what does the fake read_raw() do?
It is a test double: it returns whatever the test put in fake_raw, so any temperature is one line away.
03test_edge sets fake_raw = 144, which gives exactly 20 degrees. Someone changes < 20 to <= 20. What happens?
20 <= 20 is true, but the test expects false, so Unity reports FAIL. test_cold (18 degrees) still passes: only the edge case notices.
04In test_cold, which line is the assert step?
fake_raw = 141 arranges, needHeat() acts, and TEST_ASSERT_TRUE(on) asserts: Unity checks the value and reports PASS or FAIL.
05Without the L, raw*500 is an int multiply. Why would every laptop test pass while the Uno gives wrong answers for raw above 65?
The largest 16-bit int is 32,767. On the laptop the multiply fits easily, so its tests cannot see the overflow. Writing 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, and some tests should still run on the board.
المسرد
- Target and host
- The target is the device the firmware is for (here the Uno); the host is the computer you develop on (here a laptop).
- Native build
- A program compiled to run on the same kind of machine that compiled it, such as building firmware logic with gcc to run on your laptop.
- Seam
- A place where you can swap one piece of code for another without editing the code that uses it, such as read_raw().
- Test double (fake)
- A stand-in for a real part in a test. A fake read_raw() returns a value the test chooses instead of reading a sensor.
- Unity
- A small unit-test framework for C, built to work with embedded toolchains. Tests use assertions such as TEST_ASSERT_TRUE and TEST_ASSERT_EQUAL_INT.
- Arrange, act, assert
- The three parts of a test: set up the inputs, call the code, check the result.
- Edge case
- An input at a boundary, such as exactly the set point, where off-by-one bugs like < versus <= show up.
- Integer overflow
- A result too big for its type. On the Uno an int holds at most 32,767; going past that gives an unpredictable result.
الملاحظات والمصادر
- The cards show only the logic and one test. The test file also includes unity.h and temp.c's header, and has a main() that calls UNITY_BEGIN(), RUN_TEST for each test and UNITY_END(). Unity also wants setUp() and tearDown() functions, which may be empty. bool comes from stdbool.h.
- Swapping read_raw() at link time is one way to make a seam. Others pass a function pointer in, or use a mocking tool that also checks how a function was called. Grenning's book covers all of these.
- PlatformIO calls laptop tests "native" and board tests "embedded": the runner builds the test firmware, flashes the board and reads the results back over Serial. Unity is one of the frameworks it supports.
- Signed int overflow is undefined behaviour in C, and the Arduino reference says the result "is unpredictable". With raw = 66, raw*500 = 33,000 does not fit in 32,767, so writing 500L (a long constant) makes the multiply a long: at least 32 bits on both machines (32 on the Uno, usually 64 on a 64-bit macOS or Linux laptop), so it fits on both.
- tempC assumes a TMP36 (500 mV at 0 °C, 10 mV per °C) and a 5 V, 10-bit reading, so raw * 500 / 1024 is the voltage in tens of millivolts. Integer division truncates, so 141 gives 18, not 18.8.
- ThrowTheSwitch Unity: unit testing for C (README)
- Unity Getting Started Guide
- PlatformIO docs: Unit testing, Test Runner (native and embedded tests)
- PlatformIO docs: Unit testing, Testing frameworks
- J. Grenning, Test-Driven Development for Embedded C (Pragmatic Bookshelf)
- Arduino language reference: int (source)
- Arduino language reference: analogRead() (source)
- Analog Devices TMP35/TMP36/TMP37 datasheet
اختبار يتاح بعد الخطوة الأخيرة.