一、前言
在嵌入式開發程序中,經常會通過列印輸出一些除錯資訊來除錯引數、查找問題等,通常我的做法都是使用芯片的串口硬體設備配合串口助手軟體來進行除錯,但是這次專案的PCB硬體設計并未預留串口除錯介面,所以想使用串口除錯就不方便了,經過查找資料發現Segger 提供了一種非常方便的除錯方式——自家的J-Link硬體配合J-Link RTT Viewer軟體進行資訊輸入輸出除錯,
二、組件添加
要使用該除錯組件,需要添加Segger提供的SEGGER_RTT組件代碼,該代碼位于J-Link軟體安裝目錄下,而且在用戶手冊中提供了詳細的說明,
組件原始碼位置:

用戶手冊位置:

在手冊的16章節對RTT組件進行了說明:

將組件原始碼復制到工程目錄下,工程中添加相關檔案及包含路徑:

最后在需要使用列印除錯資訊的檔案中包含#include "SEGGER_RTT.h"頭檔案就可以使用該組件了,
三、組件應用
關于組件提供的各API函式在手冊中都有相應的說明,舉例應用如下:
輸出測驗:
int a = 3;
SEGGER_RTT_TerminalOut(0,RTT_CTRL_BG_BLACK""RTT_CTRL_TEXT_BRIGHT_GREEN"SEGGER_RTT_TerminalOut 0\r\n");
SEGGER_RTT_TerminalOut(1,RTT_CTRL_BG_BLUE""RTT_CTRL_TEXT_BRIGHT_YELLOW"SEGGER_RTT_TerminalOut 1\r\n");
SEGGER_RTT_SetTerminal(2);
SEGGER_RTT_printf(0,RTT_CTRL_BG_WHITE""RTT_CTRL_TEXT_BRIGHT_BLACK"SEGGER_RTT_printf 2\r\n");
SEGGER_RTT_SetTerminal(3);
SEGGER_RTT_printf(0,"SEGGER_RTT_printf %d\r\n", a);
打開J-Link RTT Viewer軟體

RTT Viewer列印結果如下:

輸入測驗:
int a;
while(1)
{
if ((a = SEGGER_RTT_WaitKey()) > 0)
{
SEGGER_RTT_SetTerminal(0);
SEGGER_RTT_printf(0, "SEGGER_RTT_GetKey = %c\r\n", a);
}
}
RTT Viewer列印結果如下:

四、擴展應用
經過上面對SEGGER_RTT的使用,發現其確實非常的方便,大多數除錯都能夠替代串口除錯實作了,但是SEGGER_RTT_printf()函式無法列印浮點數,手冊中對該函式列出了支持的轉換說明符如下:
| Conversion specifier | Meaning |
|---|---|
| c | Print the argument as one char |
| d | Print the argument as a signed integer |
| u | Print the argument as an unsigned integer |
| x | Print the argument as an hexadecimal integer |
| s | Print the string pointed to by the argument |
| p | Print the argument as an 8-digit hexadecimal integer. (Argument shall be a pointer to void.) |
其中并沒有浮點數f選項,但是可以使用SEGGER_RTT輸出函式自己修改一個printf函式,這樣就可以使用完整的printf函式了,
添加自己修改的printf函式如下:
/*********************************************************************
*
* rtt_printf()
*
* Function description
* print a formatted string using RTT and standard library formatting.
**********************************************************************/
int rtt_printf(const char *fmt,...)
{
int n;
char aBuffer[256]; //根據應用需求調整大小
va_list args;
va_start (args, fmt);
n = vsnprintf(aBuffer, sizeof(aBuffer), fmt, args);
if (n > (int)sizeof(aBuffer)) {
SEGGER_RTT_Write(0, aBuffer, sizeof(aBuffer));
} else if (n > 0) {
SEGGER_RTT_Write(0, aBuffer, n);
}
va_end(args);
return n;
}
接下來測驗浮點數列印:
double fa = 0.1f;
double fb = 2.0f;
while(1)
{
fa += 0.0001f;
fb -= 0.0002f;
rtt_printf("floating test:\tfa = %f, fb = %f\r\n", fa, fb);
delay(0x0fffffff);
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/237399.html
標籤:嵌入式
上一篇:Linux :LCD應用層編程
