在调试期间在IDE中显示printf消息

Art*_*nes 6 debugging arm cortex-m3 keil

我们有一个设备,我想使用printf函数向IDE发送消息以进行调试.设置:

  • ARM Cortex-M3设备

  • ULINK2接口

  • uVision4 IDE

我已按照此链接中的说明操作,以便能够在"Debug(printf)Viewer"中查看消息.首先,我修改了"retarget.c"文件,将输出重定向到ITM界面:

#include <stdio.h>
#include <rt_misc.h>

#pragma import(__use_no_semihosting_swi)

// Para utilização do saida de debug através do ULINK2
#define ITM_Port8(n)    (*((volatile unsigned char *)(0xE0000000+4*n)))
#define ITM_Port16(n)   (*((volatile unsigned short*)(0xE0000000+4*n)))
#define ITM_Port32(n)   (*((volatile unsigned long *)(0xE0000000+4*n)))
#define DEMCR           (*((volatile unsigned long *)(0xE000EDFC)))
#define TRCENA          0x01000000

struct __FILE { int handle; /* Add whatever you need here */ };
FILE __stdout;

// Escreve caractere na porta de Debug
int sendchar (int ch)  {          
  if (DEMCR & TRCENA) {
    while (ITM_Port32(0) == 0);
    ITM_Port8(0) = ch;
  }
  return(ch);
} 

int fputc(int ch, FILE *f) {
  return (sendchar(ch));
}

int ferror(FILE *f) {
  /* Your implementation of ferror */
  return EOF;
}


void _ttywrch(int ch) {  
  sendchar(ch);
}


void _sys_exit(int return_code) {
label:  goto label;  /* endless loop */
}
Run Code Online (Sandbox Code Playgroud)

然后我按照说明在uVision4中配置了IMT: 在此输入图像描述

该项目编译得很好.我将应用程序下载到设备并开始使用uVision4调试会话.在系统初始化之后,我尝试在main函数中使用带有简单消息的printf,但Debug Viewer仍为空.我把断点放在"sendchar"函数中,我可以看到所有行都按预期命中.

有没有人成功使用带有ULINK2和uVision4的printf?有没有人知道为什么我在Debug Viewer窗口中看不到任何输出?

UPDATE

我试图使用现有的ITM功能,从而产生更简单的"retarget.c":

int fputc(int ch, FILE *f) {
  return (ITM_SendChar((uint32_t)ch));
}
Run Code Online (Sandbox Code Playgroud)

但是,调试查看器中未显示任何输出.当我跳过printf函数调用时,IDE的底部会显示"Trace:Data Overflow",然后是"Trace:Communication Error".

Art*_*nes 9

问题出在"跟踪"配置的"CoreClock"字段中.它与设备的时钟不匹配.在我更改它以匹配设备时钟后,右侧输出显示在调试查看器中.

一些有用的链接:

uVision中的跟踪状态

如何配置跟踪,一步一步