Ubuntu下OpenOCD+ST-Link开发STM32F407:如何将SWO日志直接输出到控制台?
Great question! I’ve dealt with this exact setup (Ubuntu + OpenOCD + ST-Link + STM32F407-Discovery) before, so let’s break down how to get your SWO/ITM logs showing up right in the OpenOCD console, just like semihosting output.
Key Setup Steps
First, let’s make sure you have the basics covered:
- Hardware Connection: Ensure your ST-Link’s SWO pin (usually pin 4 on the ST-Link connector) is connected to the STM32F407’s PA0 pin—this is the dedicated SWO output pin for the F407 series.
- Clock Configuration: Your STM32F407 runs at 168MHz, which you already have correct in your existing command.
Modify the TPIU Command for Console Output
Instead of directing output to a temporary file (/tmp/swo.out), you can tell OpenOCD to send SWO data directly to standard output (the console) using - as the output target.
Run these commands in the OpenOCD console after connecting to your target:
# Configure TPIU to output to console, sync mode, 168MHz core clock tpiu config internal - uart off 168000000 # Enable ITM port 0 (adjust the port number if your code uses a different one) itm port 0 on
Verify and Test
After running those commands, reset or resume your STM32 program. Any ITM logs sent from your code (e.g., via ITM_SendChar() or higher-level logging functions that use ITM) will now appear directly in the OpenOCD console window.
Common Troubleshooting Tips
- No Output? Double-check your SWO pin connection and ensure the STM32’s PA0 is configured for AF0 (SWO) in your code.
- Garbled Text? Make sure the core clock value in the TPIU command matches your actual system clock (168MHz is correct for F407-Discovery, but confirm your code’s clock setup).
- ST-Link Compatibility: Ensure you’re using ST-Link V2 or V2-1—older V1 models don’t support SWO.
Quick Note vs Semihosting
Unlike semihosting (which uses debug interface memory access), SWO is a dedicated hardware pin for logging, so it’s more efficient and doesn’t interfere with your program’s execution as much. This makes it a better choice for high-volume logging.
内容的提问来源于stack exchange,提问作者aaa SA

