基于国民N32G45x系列MCU使用JLINK的开发组件工具:RTT Viewer应用笔记
概述
在项目中没有预留串口,但在调试或者运行过程中,想要监测一些参数,或者是需要录入一些数据时,我们可以使用SEGGER的RTT技术,通过J-LINK工具与MCU进行连接,经由SWD接口实现数据实时交互的功能;它是嵌入式应用中可用于用户输入输出交互的一种新技术,与上位机交互的软件为SEGGER RTT Viewer;使用RTT功能可以既可以从MCU中打印输出信息,也可以向MCU中输入信息数据,具有非常高的传输速率,而且不会影响MCU运行的实时性。
硬件准备
- N32G45XVL-STB开发板
- J-LINK LITE调试下载工具
- JTAG转SWD转接板、数据线
硬件连接
N32G45XVL-STB开发板上板载了一个CMSIS-DAP调试工具,这个工具通过旁边的双排排针和跳帽连接到MCU的SWD接口上,我本次实验需要使用的是J-LINK工具,所以需要将这边的跳帽全部断开,通过J-LINK连接到JTAG转SWD转接板,再由转接板连接到MCU侧的排针上:
软件准备
SEGGER官方下载J-LINK软件安装包,选择与PC系统相一致的版本(下载链接:https://www.segger.com/downloads/jlink/),下载后使用默认配置选项设置进行安装,并安装在默认路径下;我们当前使用的7.70a这个版本:
移植RTT
在J-LINK软件安装完成后,我们可以在C:\Program Files\SEGGER\JLink\Samples\RTT这个路径下找到一个SEGGER_RTT_V770a.zip的文件,这个就是RTT的源代码;我们将这个压缩文件解压到我们工程目录下:N32G45XVL-STB\Project\Template\Keil uVision5\Source\Utility\SEGGER_RTT_V770a:
接着我们将RTT的源码(SEGGER_RTT.c和SEGGER_RTT_Printf.c)添加到KEIL工程中去、包含头文件(SEGGER_RTT.h和SEGGER_RTT_Conf.h)、配置相关路径:
实现功能
初始化配置RTT、通过MultiTimer组件实现对RTT Viewer终端输入数据的检测,再通过RTT输出到RTT Viewer终端上显示、显示多个终端,在不同的终端上显示不同的字符内容,不同的字符颜色,以及不同的字符背景色。
代码实现
/*******************************************************************************
* [url=home.php?mod=space&uid=247401]@brief[/url]
* @param
* @retval
* [url=home.php?mod=space&uid=93590]@Attention[/url]
*******************************************************************************/
void RTT_Init(void)
{
/* 配置通道0,上行配置(N32->RTT Viewer) */
SEGGER_RTT_ConfigUpBuffer(0, "RTTUP", NULL, 0, SEGGER_RTT_MODE_NO_BLOCK_SKIP);
/* 配置通道0,下行配置(RTT Viewer->N32) */
SEGGER_RTT_ConfigDownBuffer(0, "RTTDOWN", NULL, 0, SEGGER_RTT_MODE_NO_BLOCK_SKIP);
MultiTimerStart(&RTT_MultiTimer, 5, RTT_MultiTimerCallback, "RTT");
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void RTT_MultiTimerCallback(MultiTimer *timer, void *userData)
{
/* 判断接收缓冲区中是否有数据 */
if(SEGGER_RTT_HasKey())
{
/* 从接受缓冲区中取出一个字符 */
int GetKey = SEGGER_RTT_GetKey();
/* 读取的字符通过RTT输出 打印 */
SEGGER_RTT_SetTerminal(1);
SEGGER_RTT_printf(0, "SEGGER_RTT_GetKey = %c\r\n\r\n", GetKey);
}
MultiTimerStart(&RTT_MultiTimer, 5, RTT_MultiTimerCallback, "RTT");
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void RTT_PrintTest(void)
{
SEGGER_RTT_WriteString(0, "N32G45XVL-STB, Hello World!\r\n");
SEGGER_RTT_TerminalOut(1, RTT_CTRL_CLEAR);
SEGGER_RTT_TerminalOut(1, RTT_CTRL_TEXT_RED "N32G45XVL-STB & SEGGER RTT Viewer!\r\n\r\n");
SEGGER_RTT_TerminalOut(1, RTT_CTRL_TEXT_GREEN "N32G45XVL-STB & SEGGER RTT Viewer!\r\n\r\n");
SEGGER_RTT_TerminalOut(1, RTT_CTRL_TEXT_YELLOW "N32G45XVL-STB & SEGGER RTT Viewer!\r\n\r\n");
SEGGER_RTT_TerminalOut(1, RTT_CTRL_TEXT_BLUE "N32G45XVL-STB & SEGGER RTT Viewer!\r\n\r\n");
SEGGER_RTT_TerminalOut(1, RTT_CTRL_TEXT_MAGENTA "N32G45XVL-STB & SEGGER RTT Viewer!\r\n\r\n");
SEGGER_RTT_TerminalOut(1, RTT_CTRL_TEXT_CYAN "N32G45XVL-STB & SEGGER RTT Viewer!\r\n\r\n");
SEGGER_RTT_TerminalOut(1, RTT_CTRL_TEXT_WHITE "N32G45XVL-STB & SEGGER RTT Viewer!\r\n\r\n");
SEGGER_RTT_TerminalOut(1, RTT_CTRL_TEXT_BRIGHT_RED "N32G45XVL-STB & SEGGER RTT Viewer!\r\n\r\n");
SEGGER_RTT_TerminalOut(1, RTT_CTRL_TEXT_BRIGHT_GREEN "N32G45XVL-STB & SEGGER RTT Viewer!\r\n\r\n");
SEGGER_RTT_TerminalOut(1, RTT_CTRL_TEXT_BRIGHT_YELLOW "N32G45XVL-STB & SEGGER RTT Viewer!\r\n\r\n");
SEGGER_RTT_TerminalOut(1, RTT_CTRL_TEXT_BRIGHT_BLUE "N32G45XVL-STB & SEGGER RTT Viewer!\r\n\r\n");
SEGGER_RTT_TerminalOut(1, RTT_CTRL_TEXT_BRIGHT_MAGENTA "N32G45XVL-STB & SEGGER RTT Viewer!\r\n\r\n");
SEGGER_RTT_TerminalOut(1, RTT_CTRL_TEXT_BRIGHT_CYAN "N32G45XVL-STB & SEGGER RTT Viewer!\r\n\r\n");
SEGGER_RTT_TerminalOut(1, RTT_CTRL_TEXT_BRIGHT_WHITE "N32G45XVL-STB & SEGGER RTT Viewer!\r\n\r\n");
SEGGER_RTT_TerminalOut(2, RTT_CTRL_CLEAR);
SEGGER_RTT_TerminalOut(2, RTT_CTRL_BG_BLACK "N32G45XVL-STB & SEGGER RTT Viewer!\r\n\r\n");
SEGGER_RTT_TerminalOut(2, RTT_CTRL_BG_RED "N32G45XVL-STB & SEGGER RTT Viewer!\r\n\r\n");
SEGGER_RTT_TerminalOut(2, RTT_CTRL_BG_GREEN "N32G45XVL-STB & SEGGER RTT Viewer!\r\n\r\n");
SEGGER_RTT_TerminalOut(2, RTT_CTRL_BG_YELLOW "N32G45XVL-STB & SEGGER RTT Viewer!\r\n\r\n");
SEGGER_RTT_TerminalOut(2, RTT_CTRL_BG_BLUE "N32G45XVL-STB & SEGGER RTT Viewer!\r\n\r\n");
SEGGER_RTT_TerminalOut(2, RTT_CTRL_BG_MAGENTA "N32G45XVL-STB & SEGGER RTT Viewer!\r\n\r\n");
SEGGER_RTT_TerminalOut(2, RTT_CTRL_BG_CYAN "N32G45XVL-STB & SEGGER RTT Viewer!\r\n\r\n");
SEGGER_RTT_TerminalOut(2, RTT_CTRL_BG_WHITE "N32G45XVL-STB & SEGGER RTT Viewer!\r\n\r\n");
SEGGER_RTT_TerminalOut(2, RTT_CTRL_BG_BRIGHT_BLACK "N32G45XVL-STB & SEGGER RTT Viewer!\r\n\r\n");
SEGGER_RTT_TerminalOut(2, RTT_CTRL_BG_BRIGHT_RED "N32G45XVL-STB & SEGGER RTT Viewer!\r\n\r\n");
SEGGER_RTT_TerminalOut(2, RTT_CTRL_BG_BRIGHT_GREEN "N32G45XVL-STB & SEGGER RTT Viewer!\r\n\r\n");
SEGGER_RTT_TerminalOut(2, RTT_CTRL_BG_BRIGHT_YELLOW "N32G45XVL-STB & SEGGER RTT Viewer!\r\n\r\n");
SEGGER_RTT_TerminalOut(2, RTT_CTRL_BG_BRIGHT_BLUE "N32G45XVL-STB & SEGGER RTT Viewer!\r\n\r\n");
SEGGER_RTT_TerminalOut(2, RTT_CTRL_BG_BRIGHT_MAGENTA "N32G45XVL-STB & SEGGER RTT Viewer!\r\n\r\n");
SEGGER_RTT_TerminalOut(2, RTT_CTRL_BG_BRIGHT_CYAN "N32G45XVL-STB & SEGGER RTT Viewer!\r\n\r\n");
SEGGER_RTT_TerminalOut(2, RTT_CTRL_BG_BRIGHT_WHITE "N32G45XVL-STB & SEGGER RTT Viewer!\r\n\r\n");
}
运行结果
在打开SEGGER RTT Viewer软件时,需要做如下的配置:
- J-LINK是通过USB与PC进行连接的,所以Connection to J-Link应当选择USB
- 在Specify Target Device项中,应当选择MCU的具体型号,若找不到对应的型号,可以根据MCU的内核来进行选择
- Script file是一个可选项,我们这边为空
- Target Interface & Speed项根据硬件连接来进行配置,我们使用的是SWD接口,所以这边应当选择SWD,并选择默认的4000kHz
- 最后RTT Control Block这一项有两个选择,一个是Address,在下面需要填写具体变量在SRAM中的地址;另一项是Search Range,这个由J-Link自动去搜索,但下面需要填写芯片SRAM的起始地址和空间大小,需要与MCU一致
显示视频
软件工程源代码
Template.zip
(1.2 MB)
|
Jscope 你值得拥有。。。