ITM 机制.
1. 工程配置 - debug
选择 J-Link/J-Trace Cortex.
点击settiing 按钮, 在设置窗口 - Debug 选项页
Adapter Port 选项选择 SW. 不是JTAG.
在设置窗口 - Trace 选项页
Trace Settings 的 Enable 打钩
设置当前芯片的主频, 如我的F407ZG 为 168MHz
ITM Stimulus Ports 中一般只需要勾选0 即可. 即
Enable=0x00000001
Privilege= 0x0000000
2. 代码中加入:
点击(此处)折叠或打开
-
/*
-
使用 JLINK V9 中的 JLINK CDC UART作为串口输出, 同时开启 ARM 中的 ITM 调试机制.
-
*/
-
// 1. 定义寄存器
-
#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)
-
-
//2. 添加 fputc 函数, 以便于把数据写入到 ITM Port0 寄存器.
-
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);
-
}
-
- //3. 增加#include <stdio.h>, 然后代码加入 printf("Hello world, %d. \n", 5); 等输出语句
3. 调试时, 菜单 View -> serial windows - Debug(printf) viewer. 则在 Debug 时即可看到
是不是很好用.