一、概述
使用 ST-JLink V2调试器,在MDK的“Debug (printf) Viewer”窗口下输出自定义的调试信息。
二、过程
1、硬件支持
ST-Link V2,SW调试模式下,除了正常的SWIO和SWCKL接到对应的引脚,还需要将调试器上的TDO引脚接到芯片上JTDO脚上,如果无法满足此硬件条,则在MDK上是无法使用此功能的。
2、MDK配置
选择 Project -> Options for Target -> Debug -> 选择 Use: ST-Link Debugger ; 点击Settings -> Trace -> 选中 Trace Enable ; 设置 Core Clock 和系统时钟频率保持一致(比如STM32标准库中的 SYSCLK_Frequency),并非目标系统的晶振频率;设置ITM Stimulus Ports 如下所示。
3、程序配置
新建 .c 文件,将此文件导入项目中。此段程序的作用是使能ITM的端口1。此文件插入代码如下:
#include <stdio.h>
// 添加 ITM 端口定义
#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
// 使能printf()函数
struct __FILE { int handle; /* Add whatever you need here */ };
FILE __stdout;
FILE __stdin;
int fputc(int ch, FILE *f) {
if (DEMCR & TRCENA) {
while (ITM_Port32(0) == 0);
ITM_Port8(0) = ch;
}
return(ch);
}
4、运行
导入 <stdio.h>。在程序中需要输出调试信息的地方插入 printf() 函数,此函数的用法和标准C语言函数一致。
MDK debug模式下,打开 View -> Serial Windows -> Debug (printf) Viewer 窗口查看数据。
三、其它
1、使用 JLink 调试的时候,出现“Trace: No Synchronization”错误,很大可能是 Core Clock 设置错误。必须保证 Core Clock 和系统时钟保持一致,而不是和晶振频率一致。具体的系统时钟怎么看,需要结合特定的芯片+外围电路进行分析。
2、官网链接如下:
J-Link/J-Trace User’s Guide
ULINK: Trace: No Synchronization
J-Link/J-Trace User’s Guide
MDK Support