Home > Blockchain >  Setting up SWV printf on a Nucleo STM32 board (C )
Setting up SWV printf on a Nucleo STM32 board (C )

Time:11-02

I am using an STM32G431KB, which compared to other stm32 Nucleo, has the SWO wired. I found this question Setting up SWV printf on a Nucleo STM32 board and followed the first answer. Thereby, I got the SWV running under C. But as soon as I switch to C , there is no output.

I used a new project for C, switched Debug to "Trace Asynchronous SW", added:

/* USER CODE BEGIN Includes */
#include "stdio.h"
/* USER CODE END Includes */


/* USER CODE BEGIN 0 */
int _write(int file, char *ptr, int len)
 {
     int DataIdx;
     for (DataIdx = 0; DataIdx < len; DataIdx  )
     {
         ITM_SendChar(*ptr  );
     }
     return len;
 }
/* USER CODE END 0 */

and to the main loop

  /* USER CODE BEGIN 2 */
  int i = 0;
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    printf("%d Hello World!\n",   i);
    /* USER CODE END WHILE */
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */

Then I turn on SWV in the Debug Configuration and set the core clock to 170 Mhz. Lastly, I turn off the timestep in the SWV setting and enable port 0.

When I now run the project everything works and I get an output.

But when I then switch the project to C and rename the main.c to main.cpp. The project runs, but without any output.

CodePudding user response:

Because your _write function is not _write anymore as its name was mangled by the C compiler. So you link with the "old" one which does nothing You need to declare it a extern "C"

extern "C" {
void ITM_SendChar(char par); 
int _write(int file, char *ptr, int len)
{
     int DataIdx;
     for (DataIdx = 0; DataIdx < len; DataIdx  )
     {
         ITM_SendChar(*ptr  );
     }
     return len;
}
} /*extern "C"
  • Related