Notes / Firmware Design & Quality
Logging and debug output
From printf debugging to ring-buffer logs and real-time tracing with SWO/RTT. How to see inside without spoiling real-time behaviour.
The cost of printf
Sending 50 characters over a 115200 bps UART takes about 4.3 ms. Put a blocking printf inside a 1 ms control loop and the loop breaks.
Methods, step by step
| Method | Advantage | Disadvantage |
|---|---|---|
| Blocking UART printf | Easiest | Slow, ruins timing |
| Ring buffer + DMA UART | Little effect on the loop | Loss when the buffer overflows |
| SEGGER RTT | Very fast, goes through the debugger | Needs a J-Link (an ST-LINK can be converted) |
| SWO / ITM | Built into the core, one pin | Needs debugger support |
| GPIO toggle + logic analyzer | Almost zero overhead | Little information |
Log design
- Levels: ERROR, WARN, INFO, DEBUG. Release builds log WARN and above only
- Module tag and timestamp:
[1234ms][MOTOR] overcurrent 25.3A - Sending numbers in binary and decoding them on the PC is much faster than converting to strings
Field records
For fatal errors (HardFault, watchdog reset, overcurrent stop), keep a few records of cause code and time in Flash, and even faults that cannot be reproduced can be traced.