简体   繁体   English

在 Nucleo STM32 板上设置 SWV printf (C++)

[英]Setting up SWV printf on a Nucleo STM32 board (C++)

I am using an STM32G431KB, which compared to other stm32 Nucleo, has the SWO wired.我使用的是 STM32G431KB,与其他 stm32 Nucleo 相比,它连接了 SWO。 I found this question Setting up SWV printf on a Nucleo STM32 board and followed the first answer.我发现这个问题在 Nucleo STM32 板上设置 SWV printf并遵循第一个答案。 Thereby, I got the SWV running under C. But as soon as I switch to C++, there is no output.因此,我在 C 下运行了 SWV。但是一旦我切换到 C++,就没有输出。

I used a new project for C, switched Debug to "Trace Asynchronous SW", added:我为 C 使用了一个新项目,将 Debug 切换到“Trace Asynchronous SW”,并补充说:

/* 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.然后我在调试配置中打开 SWV 并将核心时钟设置为 170 Mhz。 Lastly, I turn off the timestep in the SWV setting and enable port 0.最后,我关闭 SWV 设置中的时间步长并启用端口 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.但是当我将项目切换到 C++ 并将 main.c 重命名为 main.cpp 时。 The project runs, but without any output.项目运行,但没有任何输出。

Because your _write function is not _write anymore as its name was mangled by the C++ compiler.因为你的_write函数不再是_write ,因为它的名字被 C++ 编译器修改了。 So you link with the "old" one which does nothing You need to declare it a extern "C"因此,您与什么都不做的“旧”链接链接您需要将其声明为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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM