Notes / Test & Debug Instruments
Debugger: stopping firmware line by line
Load firmware with an ST-LINK or J-Link and find problems with breakpoints, variable watches and register views. Faster than debugging with printf.
Connection
A Cortex-M connects to the debugger through the SWD interface (SWDIO, SWCLK, GND, and NRST and VTref if needed). When designing a board, always bring the SWD pins out to a connector.
What you can do
- Breakpoint: stop at a particular line
- Stepping: one line at a time, stepping into or over functions
- Variable watch: check values while stopped; some tools update live while running
- Register / peripheral view: see whether GPIO and timer registers hold the settings you intended, decoded through the SVD file
- Memory view: buffer contents, the stack area
- Data watchpoint: stop the instant a particular variable changes. Powerful for finding who is corrupting a value
Cautions
- Stopping during real-time operation such as motor control can be dangerous, because the PWM stays as it was. Use the timer's debug-stop feature (DBGMCU) so the outputs turn off when execution stops.
- With optimisation (-O2) on, variables disappear or the line order looks scrambled. Use -Og for debugging.
- The debug connection can drop when the MCU enters a low-power mode.