From 08265b116ce5882cb7943c89f508a8f8bc6b7882 Mon Sep 17 00:00:00 2001 From: "lion.chan" Date: Fri, 16 Sep 2022 17:11:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E4=BD=BF=E7=94=A8=20SEGGE?= =?UTF-8?q?R=20J-Link=20RTT.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lion.chan --- .../SEGGER/J-Link/使用_SEGGER_J-Link_RTT.md | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 Software/Development/Environment/SEGGER/J-Link/使用_SEGGER_J-Link_RTT.md diff --git a/Software/Development/Environment/SEGGER/J-Link/使用_SEGGER_J-Link_RTT.md b/Software/Development/Environment/SEGGER/J-Link/使用_SEGGER_J-Link_RTT.md new file mode 100644 index 0000000..36eef8e --- /dev/null +++ b/Software/Development/Environment/SEGGER/J-Link/使用_SEGGER_J-Link_RTT.md @@ -0,0 +1,104 @@ +--- +layout: post +title: "使用 SEGGER J-Link RTT" +subtitle: "基于 Keil 环境" +description: "对在 Keil 环境下使用 SEGGER J-Link RTT 功能进行了介绍,并对常见编译错误进行了修正。" +excerpt: "对在 Keil 环境下使用 SEGGER J-Link RTT 功能进行了介绍,并对常见编译错误进行了修正。" +date: 2022-09-16 17:09:00 +author: "Rick Chan" +tags: ["Development", "RTT", "J-Link", "SEGGER", "Keil"] +categories: ["Software"] +published: true +math: false +--- + +- [1. 工程配置](#1-工程配置) +- [2. 代码修正](#2-代码修正) +- [3. 工具使用](#3-工具使用) +- [4. 外部参考资料](#4-外部参考资料) + +With RTT it is possible to output information from the target microcontroller as well as sending input to the application at a very high speed without affecting the target's real time behavior. SEGGER RTT can be used with any J-Link model and any supported target processor which allows background memory access, which are Cortex-M and RX targets. + +## 1. 工程配置 + +首先需要向工程添加 RTT 支持代码,可以在 \/Samples/RTT 下找到该代码。 + +在 Keil 中创建 SEEGER 分组,添加 RTT 源码中的 SEGGER_RTT.h、SEGGER_RTT.c、SEGGER_RTT_printf.c 和 SEGGER_RTT_Conf.h 文件。使用以下函数初始化并打印信息: + +```cpp + +int main(void) { + // ... + SEGGER_RTT_ConfigUpBuffer(0, "RTTUP", NULL, 0, SEGGER_RTT_MODE_NO_BLOCK_SKIP); + SEGGER_RTT_ConfigDownBuffer(0, "RTTDOWN", NULL, 0, SEGGER_RTT_MODE_NO_BLOCK_SKIP); + SEGGER_RTT_SetTerminal(0); + + SEGGER_RTT_printf(0, "V\r\n"); + // ... +} +``` + +其中 SEGGER_RTT_SetTerminal() 用于选择终端编号 SEGGER J-Link RTT 支持多个终端。 + +## 2. 代码修正 + +如果 Keil 中使能了 **GNU extensions** 选项,编译 RTT 代码时会出现类似如下错误: + +```bash +warning: #47-D: incompatible redefinition of macro "SEGGER_RTT_LOCK" (declared at line 199) +warning: #47-D: incompatible redefinition of macro "SEGGER_RTT_UNLOCK" (declared at line 200) +``` + +这是因为使能 **GNU extensions** 后同时开启了“\_\_CC_ARM” 和 “\_\_GUNC\_\_”宏,导致 SEGGER_RTT_Conf.h 文件中的 SEGGER_RTT_LOCK 和 SEGGER_RTT_UNLOCK 宏重复定义。 + +此时需要修改 SEGGER_RTT_Conf.h 文件如下: + +```cpp +/********************************************************************* +* +* RTT lock configuration for SEGGER Embedded Studio, +* Rowley CrossStudio and GCC +*/ +#if (defined __SES_ARM) || (defined __CROSSWORKS_ARM) || ((defined __GNUC__) && !(defined(__CC_ARM))) +``` + +## 3. 工具使用 + +需要使用 **J-Link RTT Viewer** 工具来查看 RTT 的输出(有些环境下需要先连接该工具再在 IDE 中进行调试)。 + +打开 **J-Link RTT Viewer**,根据实际情况选择 J-Link 的连接方式,然后选择目标设备,如 STM32F407 等,然后选择 SWD 或 JTAG 接口,并设置速率,点击“OK”继续,若出现类似如下日志,则连接成功。 + +```bash +LOG: Terminal 0 added. +LOG: Device "N32L406CB" selected. +LOG: Found SW-DP with ID 0x2BA01477 +LOG: Scanning AP map to find all available APs +LOG: AP[1]: Stopped AP scan as end of AP map has been reached +LOG: AP[0]: AHB-AP (IDR: 0x24770011) +LOG: Iterating through AP map to find AHB-AP to use +LOG: AP[0]: Core found +LOG: AP[0]: AHB-AP ROM base: 0xE00FF000 +LOG: CPUID register: 0x410FC241. Implementer code: 0x41 (ARM) +LOG: Found Cortex-M4 r0p1, Little endian. +LOG: FPUnit: 6 code (BP) slots and 2 literal slots +LOG: CoreSight components: +LOG: ROMTbl[0] @ E00FF000 +LOG: ROMTbl[0][0]: E000E000, CID: B105E00D, PID: 000BB00C SCS-M7 +LOG: ROMTbl[0][1]: E0001000, CID: B105E00D, PID: 003BB002 DWT +LOG: ROMTbl[0][2]: E0002000, CID: B105E00D, PID: 002BB003 FPB +LOG: ROMTbl[0][3]: E0000000, CID: B105E00D, PID: 003BB001 ITM +LOG: ROMTbl[0][4]: E0040000, CID: B105900D, PID: 000BB9A1 TPIU +LOG: ROMTbl[0][5]: E0041000, CID: B105900D, PID: 000BB925 ETM +LOG: RTT Viewer connected. +``` + +在 Terminal 菜单中可选择“Add next terminal”,则会出现“Terminal 0”、“Terminal 1”等,代码中使用 SEGGER_RTT_SetTerminal() 函数选择好输出终端后,日志将被输出到对应编号的 Terminal 中。 + +最后,在 IDE 中进行调试,或全速运行,RTT 终端中就会产生响应输出。 + +SEGGER_RTT_printf() 的用法与 printf() 用法类似,注意换行使用 “\r\n”。 + +## 4. 外部参考资料 + +1. [SEGGER调试利器RTT,替代串口,高速数据上传](https://blog.csdn.net/qlexcel/article/details/126633180) +2. [编译SeggerRTT报错: warning: #47-D: incompatible redefinition of macro "SEGGER_RTT_LOCK"](https://blog.csdn.net/weixin_39752827/article/details/88708626)