Ground Embedded KO Subscribe

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.

GROUND EMBEDDED · NOTEFirmware Design & Quality 07Logging and debug output 1volatile uint32_t *ODR 2 = (uint32_t*)0x40020014; 3*ODR |= (1u << 5); 4while (1) { 5 if (flag) toggle(); 6}

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

printf → UARTsimple, slowRing buffer + DMAfew stallsSWO · RTTvia the debugger, very fast
Output methods. Further right means less effect on execution.
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
RoadmapSee where this note sits in Firmware Design & Quality→

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.

FREE PDFGet the 3 free ebooksIf you want to keep reading this topic as one book: BLDC motor control, serial communication and sensors at book length. PDFs are in Korean.Get them free →