简介
EGGER RTT支持使用J-link调试器输出来自目标微控制器的信息,也可以接收输入,并且在高速度交互的同时不会影响目标处理器的实时性,可以省掉平常打印日志用的串口。
RTT工具支持 ARM Cortex-M0/M0+/M1/M3/M4/M7/M23/M33 内核和瑞萨 RX100/200/600 内核。
RTT工具的源码实现提供了完整的功能,可以自由使用。RTT的源码是作为J-Link软件包的一部分,在Jlink安装目录中的 Sample/RTT中,默认安装路径如下
本章使用环境:
正点原子stm32F4探索者
代码工程使用正点原子HAL库 实验0-1 Template工程模板-新建工程章节使用
工程移植
将压缩包中的RTT文件夹复制到工程目录中,然后再keil中添加该目录中的文件,并包含路径
到这里移植已经完成了,然后我们可以再main函数中测试一下
简单使用
#include "SEGGER_RTT.h" // 包含的头文件
// main函数中添加
SEGGER_RTT_printf(0, RTT_CTRL_TEXT_BRIGHT_BLACK"Hello, SEGGER RTT---%d!\r\n",i++);
SEGGER_RTT_printf(0, RTT_CTRL_TEXT_BRIGHT_RED"Hello, SEGGER RTT---%d!\r\n",i++);
SEGGER_RTT_printf(0, RTT_CTRL_TEXT_BRIGHT_GREEN"Hello, SEGGER RTT---%.2f!\r\n",1.23);
然后我们打开JLinkRTTViewer工具
选择USB模式,芯片选择自己对应的芯片,Interfance和speed根据自己的连接方式选择,Control Bolck选择Auto模式
Jlink支持三个虚拟串口,可以通过下面方式打开
程序中也需要进行修改通过SEGGER_RTT_SetTerminal(0);函数来修改虚拟串口号
SEGGER_RTT_SetTerminal(0);
SEGGER_RTT_printf(0, RTT_CTRL_TEXT_BRIGHT_BLACK"Hello, SEGGER RTT---%d!\r\n",i++);
SEGGER_RTT_SetTerminal(1);
SEGGER_RTT_printf(0, RTT_CTRL_TEXT_BRIGHT_RED"Hello, SEGGER RTT---%d!\r\n",i++);
SEGGER_RTT_SetTerminal(2);
SEGGER_RTT_printf(0, RTT_CTRL_TEXT_BRIGHT_GREEN"Hello, SEGGER RTT---%.2f!\r\n",1.23);
颜色打印有以下定义
#define RTT_CTRL_TEXT_BLACK "\x1B[2;30m"
#define RTT_CTRL_TEXT_RED "\x1B[2;31m"
#define RTT_CTRL_TEXT_GREEN "\x1B[2;32m"
#define RTT_CTRL_TEXT_YELLOW "\x1B[2;33m"
#define RTT_CTRL_TEXT_BLUE "\x1B[2;34m"
#define RTT_CTRL_TEXT_MAGENTA "\x1B[2;35m"
#define RTT_CTRL_TEXT_CYAN "\x1B[2;36m"
#define RTT_CTRL_TEXT_WHITE "\x1B[2;37m"
#define RTT_CTRL_TEXT_BRIGHT_BLACK "\x1B[1;30m"
#define RTT_CTRL_TEXT_BRIGHT_RED "\x1B[1;31m"
#define RTT_CTRL_TEXT_BRIGHT_GREEN "\x1B[1;32m"
#define RTT_CTRL_TEXT_BRIGHT_YELLOW "\x1B[1;33m"
#define RTT_CTRL_TEXT_BRIGHT_BLUE "\x1B[1;34m"
#define RTT_CTRL_TEXT_BRIGHT_MAGENTA "\x1B[1;35m"
#define RTT_CTRL_TEXT_BRIGHT_CYAN "\x1B[1;36m"
#define RTT_CTRL_TEXT_BRIGHT_WHITE "\x1B[1;37m"
#define RTT_CTRL_BG_BLACK "\x1B[24;40m"
#define RTT_CTRL_BG_RED "\x1B[24;41m"
#define RTT_CTRL_BG_GREEN "\x1B[24;42m"
#define RTT_CTRL_BG_YELLOW "\x1B[24;43m"
#define RTT_CTRL_BG_BLUE "\x1B[24;44m"
#define RTT_CTRL_BG_MAGENTA "\x1B[24;45m"
#define RTT_CTRL_BG_CYAN "\x1B[24;46m"
#define RTT_CTRL_BG_WHITE "\x1B[24;47m"
#define RTT_CTRL_BG_BRIGHT_BLACK "\x1B[4;40m"
#define RTT_CTRL_BG_BRIGHT_RED "\x1B[4;41m"
#define RTT_CTRL_BG_BRIGHT_GREEN "\x1B[4;42m"
#define RTT_CTRL_BG_BRIGHT_YELLOW "\x1B[4;43m"
#define RTT_CTRL_BG_BRIGHT_BLUE "\x1B[4;44m"
#define RTT_CTRL_BG_BRIGHT_MAGENTA "\x1B[4;45m"
#define RTT_CTRL_BG_BRIGHT_CYAN "\x1B[4;46m"
#define RTT_CTRL_BG_BRIGHT_WHITE "\x1B[4;47m"
在正点原子的工程中已经重定向printf了,所以我们需要在该文件中去修改
SEGGER_RTT_PutChar(0, ch); // 需要包含头文件使用