ARM JLINK V9 新特征 - 自带 UART, 支持Printf

4620阅读 0评论2018-06-21 iibull
分类:其他平台

参考


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. 代码中加入:
    

点击(此处)折叠或打开

  1. /*
  2. 使用 JLINK V9 中的 JLINK CDC UART作为串口输出, 同时开启 ARM 中的 ITM 调试机制.
  3. */
  4. // 1. 定义寄存器
  5. #define ITM_PORT8(n)        (*((volatile unsigned char *)(0xE0000000+4*n)))
  6. #define ITM_PORT16(n)        (*((volatile unsigned short *)(0xE0000000+4*n)))
  7. #define ITM_PORT32(n)        (*((volatile unsigned long *)(0xE0000000+4*n)))

  8. #define DEMCR                        (*((volatile unsigned long *)(0xE000EDFC)))
  9. #define TRCENA                    (0x01000000)

  10. //2. 添加 fputc 函数, 以便于把数据写入到 ITM Port0 寄存器.
  11. struct __FILE{int handle; /* add whatever you need here */};
  12. FILE __stdout;
  13. FILE __stdin;

  14. int fputc(int ch, FILE *f) {
  15.     if (DEMCR & TRCENA) {
  16.         while (ITM_PORT32(0) == 0) {
  17.             ITM_PORT8(0) = ch;
  18.         }
  19.     }
  20.     return (ch);
  21. }

  22. //3. 增加#include <stdio.h>, 然后代码加入 printf("Hello world, %d. \n", 5); 等输出语句

3.  调试时, 菜单 View -> serial windows - Debug(printf) viewer.  则在 Debug 时即可看到

是不是很好用.
上一篇: MODBUS-RTU数据帧格式、报文实例
下一篇:win10 上 安装 android 模拟器.