Viewing SWO output within PIO?

As a workaround I used to configure openocd (that ships with pio) like this (platformio.ini):

debug_server = $PLATFORMIO_CORE_DIR/packages/tool-openocd/bin/openocd
  -f $PLATFORMIO_CORE_DIR/packages/tool-openocd/scripts/board/st_nucleo_l4.cfg
  -f $PLATFORMIO_CORE_DIR/packages/tool-openocd/scripts/interface/stlink-v2-1.cfg
  -c "tpiu config internal - uart off 80000000"
  -c "itm ports on"
  -c "tcl_port 6666"

What is does, it turns all itm ports on and reopen tcl server that is disabled by default. You have of course to change the interface and board cfg to the one of your board. Also change 80000000 to the clock of your microcontroller.

Use this python script to listen on tcl server (port 6666):

You can open this script inside of vscode, so it is as near as having it “builtin” to vscode.

With this setup you can debug your program with pio debugger and simultaniously listen to itm packages. Of course you can overwrite _io_putchar to use printf:

extern "C"
{
int __io_putchar(int ch){
	//HAL_UART_Transmit(&huart2,(uint8_t *)&ch, 1, 10);
	ITM_SendChar(ch);
	return ch;
}
}
3 Likes