EMBEDDED SYSTEMS & MICROCONTROLLERS · PAGE 45
Testing Firmware
Prove it works before it ships.
Put pure logic, such as filters and parsers, in functions that do not touch hardware. You can then compile and test them on a normal computer with simple checks.
On the device, add self-tests that run at start-up, such as reading a sensor identity. For hardware behaviour, measure with a meter, a scope or a logic analyser.
Keep a list of known test cases and run it after every change.
EXAMPLE
#include <assert.h>
int clamp(int x, int lo, int hi) { return x < lo ? lo : (x > hi ? hi : x); }
int main(void) {
assert(clamp(5, 0, 10) == 5);
assert(clamp(-3, 0, 10) == 0);
assert(clamp(99, 0, 10) == 10);
return 0;
}