之前写过 STM32开发 – Jlink常用命令 ,今天遇到需要SEGGER RTT 配置,就再写一下吧。
一、下载并安装JLink
下载: J-Link / J-Trace Downloads
可选择需要的版本下载:
二、SEGGER RTT
包含文件
得到 SEGGER_RTT_V794m.zip 解压如下图:
SEGGER RTT Sources
https://www.segger.com/products/debug-probes/j-link/technology/about-real-time-transfer
https://wiki.segger.com/RTT
Included files
RTT/
SEGGER_RTT.c
- Main module for RTT.SEGGER_RTT.h
- Main header for RTT.SEGGER_RTT_ASM_ARMv7M.S
- Assembly-optimized implementation of RTT functions for ARMv7M processors.SEGGER_RTT_Printf.c
- Simple implementation of printf (SEGGER_RTT_Printf()
) to write formatted strings via RTT.
Syscalls/
SEGGER_RTT_Syscalls_*.c
- Low-level syscalls to retargetprintf()
to RTT with different toolchains.
Config/
SEGGER_RTT_Conf.h
- RTT configuration file.
Examples/
Main_RTT_InputEchoApp.c
- Example application which echoes input on Channel 0.Main_RTT_MenuApp.c
- Example application to demonstrate RTT bi-directional functionality.Main_RTT_PrintfTest.c
- Example application to test RTT’s simple printf implementation.Main_RTT_SpeedTestApp.c
- Example application to measure RTT performance. (Requires embOS)
移植
直接将整个 RTT 文件夹拖入工程即可
使用
1、在主函数文件,添加头文件名
#include "SEGGER_RTT.h"
2、在主函数添加初始化SEGGER代码,放到系统时钟初始化 后面在初始化。
SEGGER_RTT_ConfigUpBuffer(0, NULL, NULL, 0, SEGGER_RTT_MODE_NO_BLOCK_SKIP);
3、在主函数或其它任务 内调用”SEGGER_RTT_printf“,即可测试输出
SEGGER_RTT_printf(0, "printf Test: %%s, \"RTT\" : %s.\r\n", "RTT");
定义 LOG.H 文件方便调用 SEGGER_API 接口
常规简易 LOG.H
#ifndef LOG_H
#define LOG_H
#include "SEGGER_RTT.h"
// 日志级别定义
#define LOG_LEVEL_NOLOG 0 // 无日志输出
#define LOG_LEVEL_ERROR 1 // 只输出错误
#define LOG_LEVEL_WARN 2 // 输出警告和错误
#define LOG_LEVEL_INFO 3 // 输出信息、警告和错误
#define LOG_LEVEL_DEBUG 4 // 输出调试信息、信息、警告和错误
// 设置当前日志级别
#define LOG_LEVEL LOG_LEVEL_DEBUG
// 常用文本颜色控制宏
#define LOG_RESET RTT_CTRL_RESET
#define LOG_RED RTT_CTRL_TEXT_RED
#define LOG_GREEN RTT_CTRL_TEXT_GREEN
#define LOG_YELLOW RTT_CTRL_TEXT_YELLOW
#define LOG_BLUE RTT_CTRL_TEXT_BLUE
#define LOG_MAGENTA RTT_CTRL_TEXT_MAGENTA
#define LOG_CYAN RTT_CTRL_TEXT_CYAN
#define LOG_WHITE RTT_CTRL_TEXT_WHITE
// 日志输出宏定义
#if LOG_LEVEL >= LOG_LEVEL_DEBUG
#define LOG(fmt, ...) SEGGER_RTT_printf(0, LOG_RESET "[DEBUG]: " fmt "\r\n", ##__VA_ARGS__)
#else
#define LOG(fmt, ...)
#endif
#if LOG_LEVEL >= LOG_LEVEL_INFO
#define LOG_INFO(fmt, ...) SEGGER_RTT_printf(0, LOG_GREEN "[INFO]: " fmt "\r\n", ##__VA_ARGS__)
#else
#define LOG_INFO(fmt, ...)
#endif
#if LOG_LEVEL >= LOG_LEVEL_WARN
#define LOG_WARN(fmt, ...) SEGGER_RTT_printf(0, LOG_YELLOW "[WARN]: " fmt "\r\n", ##__VA_ARGS__)
#else
#define LOG_WARN(fmt, ...)
#endif
#if LOG_LEVEL >= LOG_LEVEL_ERROR
#define LOG_ERROR(fmt, ...) SEGGER_RTT_printf(0, LOG_RED "[ERROR]: " fmt "\r\n", ##__VA_ARGS__)
#else
#define LOG_ERROR(fmt, ...)
#endif
#endif // LOG_H
带有时间戳的 LOG.H 文件,注意需要定义时间源!
#ifndef LOG_H
#define LOG_H
#include "SEGGER_RTT.h"
#if defined(USE_HAL_DRIVER)
extern uint32_t HAL_GetTick(void);
extern uint32_t HAL_GetUs(void);
#define SYS_MS HAL_GetTick()
#define SYS_US HAL_GetUs()
#else
#define SYS_MS 0 // 如果不是使用HAL库 则使用自定义的时间源
#define SYS_US 0
#endif
#define USE_LOG_DEBUG 1
#define PRINT_TIMESTAMP 1
typedef enum {
LOG_TERMINAL0,
LOG_TERMINAL1,
LOG_TERMINAL2,
LOG_TERMINAL3,
LOG_TERMINAL4,
LOG_TERMINAL5,
} LogTerminal_e;
typedef enum {
LOG_ARR_BYTE1,
LOG_ARR_BYTE2,
LOG_ARR_BYTE4,
LOG_ARR_FLOAT,
LOG_ARR_INT,
} LogArrayType_e;
#if USE_LOG_DEBUG
#if PRINT_TIMESTAMP
#define LOG_PROTO(type, color, format, ...) \
SEGGER_RTT_printf(0, "[%02d:%03d:%03d] %s%s" format "%s\r\n", (SYS_MS / 1000) % 60, SYS_MS % 1000, SYS_US % 1000, color, type, \
##__VA_ARGS__, RTT_CTRL_RESET);
// #define LOG_PROTO(type, color, format, ...) \
// SEGGER_RTT_printf(0, "[%02d:%02d:%02d:%03d:%03d] %s%s" format "%s\r\n", (SYS_MS / (60 * 60 * 1000)) % 24, (SYS_MS / (60 * 1000)) % 60, \
// (SYS_MS / 1000) % 60, SYS_MS % 1000, SYS_US % 1000, color, type, ##__VA_ARGS__, RTT_CTRL_RESET);
#else
#define LOG_PROTO(type, color, format, ...) SEGGER_RTT_printf(0, "%s%s" format "%s\r\n", color, type, ##__VA_ARGS__, RTT_CTRL_RESET);
#endif
#define LOG_INFO(format, ...) LOG_PROTO("[INFO]:", "", format, ##__VA_ARGS__) // 无颜色日志输出
#define LOG_DEBUG(format, ...) LOG_PROTO("[DEBUG]:", RTT_CTRL_TEXT_BRIGHT_GREEN, format, ##__VA_ARGS__) // 绿色日志输出
#define LOG_WARN(format, ...) LOG_PROTO("[WARN]:", RTT_CTRL_TEXT_BRIGHT_YELLOW, format, ##__VA_ARGS__) // 黄色日志输出
#define LOG_ERROR(format, ...) LOG_PROTO("[ERROR]:", RTT_CTRL_TEXT_BRIGHT_RED, format, ##__VA_ARGS__) // 红色日志输出
#define LOG_CLEAR() SEGGER_RTT_WriteString(0, "\r\n" RTT_CTRL_CLEAR) // 清屏
// 打印数组
#define LOG_ARRAY(pArr, len, logArrayType, terminal) \
do { \
SEGGER_RTT_SetTerminal(terminal); \
for (int i = 0; i < len; i++) { \
switch (logArrayType) { \
case LOG_ARR_BYTE1: \
SEGGER_RTT_printf(0, "%02x ", *(uint8_t *)(pArr + i)); \
break; \
case LOG_ARR_BYTE2: \
SEGGER_RTT_printf(0, "%04x ", *(uint16_t *)(pArr + i)); \
break; \
case LOG_ARR_BYTE4: \
SEGGER_RTT_printf(0, "%08x ", *(uint32_t *)(pArr + i)); \
break; \
case LOG_ARR_FLOAT: \
SEGGER_RTT_printf(0, "%f ", *(float *)(pArr + i)); \
break; \
case LOG_ARR_INT: \
SEGGER_RTT_printf(0, "%d ", *(int *)(pArr + i)); \
break; \
default: \
break; \
} \
} \
SEGGER_RTT_printf(0, "\r\n"); \
SEGGER_RTT_SetTerminal(0); \
} while (0)
// 初始化
#define LOG_INIT() \
do { \
SEGGER_RTT_Init(); \
LOG_CLEAR(); \
LOG_INFO("RTT Inited!"); \
} while (0)
#else
#define LOG_INFO(format, ...)
#define LOG_DEBUG(format, ...)
#define LOG_WARN(format, ...)
#define LOG_ERROR(format, ...)
#define LOG_CLEAR()
#define LOG_ARRAY(pArr, len, logArrayType, terminal)
#define LOG_INIT()
#endif
#endif
调试
打开J-LinkRTTViewer.exe
选择所使用电路的芯片型号和连接方式速度:
连接信息:
打印信息: