Quick Answer
Effective STM32 debugging requires hardware debug probes (ST-Link, J-Link), software tools (GDB, IDE debuggers), serial output for printf debugging, and understanding of fault handlers (HardFault, MemManage). Use SWD for basic debugging; upgrade to JTAG with trace for complex issues.
Introduction
Debugging is an essential skill for embedded developers. Unlike desktop applications where you can easily print to console or use extensive logging, embedded systems require specialized tools and techniques to diagnose problems in code running on constrained hardware with limited visibility.
Core Content
1. Hardware Debug Probes
ST-Link (STMicroelectronics)
- ST-Link/V2: Standard debugger, SWD and SWV support
- ST-Link/V3: Faster, supports JTAG, SWD, SWV, higher speeds
- Built-in: On Nucleo and Discovery boards
- Speed: Up to 4 MHz (V2), 24 MHz (V3)
- Cost: $10-25 standalone, free on dev boards
J-Link (SEGGER)
- Features: JTAG, SWD, ETM trace, RTT, high-speed flash download
- Speed: Up to 50 MHz JTAG clock
- Software: J-Link GDB Server, Ozone debugger, RTT
- Cost: $400-600 (J-Link Plus)
- Value: Professional development, faster debugging
2. Printf Debugging and Logging
UART Printf
// Redirect printf to UART
int __io_putchar(int ch) {
HAL_UART_Transmit(&huart1, (uint8_t*)&ch, 1, HAL_MAX_DELAY);
return ch;
}
// Usage
printf("Sensor value: %d\n", sensor_reading);
SEGGER RTT (Real-Time Transfer)
RTT provides extremely fast logging via debug probe without UART overhead:
- Speed: Up to 2 MB/s (vs. 115.2 kbps UART)
- Non-intrusive: Minimal CPU overhead
- No extra pins: Uses debug interface
- Bidirectional: Both output and input
3. Fault Handling
Common Fault Types
- HardFault: Generic fault (null pointer, stack overflow)
- MemManage: Memory protection violation
- BusFault: Bus error (invalid address)
- UsageFault: Undefined instruction, divide by zero
FAQ
Why can’t I connect to my STM32 with the debugger?
Common causes: Low-power mode, SWD pins reconfigured, chip in reset, or power issue. Solution: Use “Connect under reset” option, check power supply, ensure BOOT0 pin is correct.
Can I debug without halting the processor?
Yes, with trace and RTT. ETM trace provides non-intrusive instruction tracing. RTT provides fast logging with minimal overhead. Both allow observing system behavior without stopping execution.
Conclusion
Effective STM32 debugging requires a combination of tools and techniques:
- Hardware: ST-Link or J-Link with SWD/JTAG interface
- Software: IDE debugger with GDB backend
- Logging: UART printf, RTT, or ITM for visibility
- Fault Handling: Implement handlers with register dumps
- Prevention: Compiler warnings, assertions, static analysis
Need Expert Debugging Assistance?
InnovChip provides embedded development and debugging services. Contact us today for help with challenging bugs, firmware development, or embedded system design.
