RTT調試不占用串口,也不用接多余的線,在調試時比uart打印日志更加方便。
在nordic的zigbee/ble/thread 3.2 SDK(v3.2.0_9fade31),
修改目標工程為
C:\nordic_zigbee_v3.2.0_9fade31\examples\multiprotocol\ble_zigbee\ble_zigbee_dynamic_door_lock_nus\pca10056\s140\arm5_no_packs\ble_zigbee_dynamic_door_lock_pca10056_s140.uvprojx
第一步 增加代碼
在工程中添加rtt相關的C代碼文件
源文件的路徑在
X:\nordic_zigbee_v3.2.0_9fade31\external\segger_rtt
第二步 修改config
sdk_config.h文件中增加啟用RTT的宏,這部分在3.2以后的版本可能已經增加,使用時請注意區分。
增加的代碼段如下:
//========================================================== // <e> NRF_LOG_BACKEND_RTT_ENABLED - nrf_log_backend_rtt - Log RTT backend //========================================================== #ifndef NRF_LOG_BACKEND_RTT_ENABLED #define NRF_LOG_BACKEND_RTT_ENABLED 1 #endif // <o> NRF_LOG_BACKEND_RTT_TEMP_BUFFER_SIZE - Size of buffer for partially processed strings. // <i> Size of the buffer is a trade-off between RAM usage and processing. // <i> if buffer is smaller then strings will often be fragmented. // <i> It is recommended to use size which will fit typical log and only the // <i> longer one will be fragmented.
#ifndef NRF_LOG_BACKEND_RTT_TEMP_BUFFER_SIZE #define NRF_LOG_BACKEND_RTT_TEMP_BUFFER_SIZE 128 #endif
// <o> NRF_LOG_BACKEND_RTT_TX_RETRY_DELAY_MS - Period before retrying writing to RTT #ifndef NRF_LOG_BACKEND_RTT_TX_RETRY_DELAY_MS #define NRF_LOG_BACKEND_RTT_TX_RETRY_DELAY_MS 1 #endif
// <o> NRF_LOG_BACKEND_RTT_TX_RETRY_CNT - Writing to RTT retries. // <i> If RTT fails to accept any new data after retries // <i> module assumes that host is not active and on next // <i> request it will perform only one write attempt. // <i> On successful writing, module assumes that host is active // <i> and scheme with retry is applied again.
#ifndef NRF_LOG_BACKEND_RTT_TX_RETRY_CNT #define NRF_LOG_BACKEND_RTT_TX_RETRY_CNT 3 #endif
// </e> |
第二段代碼
// <h> nRF_Segger_RTT
//========================================================== // <h> segger_rtt - SEGGER RTT
//========================================================== // <o> SEGGER_RTT_CONFIG_BUFFER_SIZE_UP - Size of upstream buffer. // <i> Note that either @ref NRF_LOG_BACKEND_RTT_OUTPUT_BUFFER_SIZE // <i> or this value is actually used. It depends on which one is bigger.
#ifndef SEGGER_RTT_CONFIG_BUFFER_SIZE_UP #define SEGGER_RTT_CONFIG_BUFFER_SIZE_UP 14000 #endif
// <o> SEGGER_RTT_CONFIG_MAX_NUM_UP_BUFFERS - Size of upstream buffer. #ifndef SEGGER_RTT_CONFIG_MAX_NUM_UP_BUFFERS #define SEGGER_RTT_CONFIG_MAX_NUM_UP_BUFFERS 2 #endif
// <o> SEGGER_RTT_CONFIG_BUFFER_SIZE_DOWN - Size of upstream buffer. #ifndef SEGGER_RTT_CONFIG_BUFFER_SIZE_DOWN #define SEGGER_RTT_CONFIG_BUFFER_SIZE_DOWN 16 #endif
// <o> SEGGER_RTT_CONFIG_MAX_NUM_DOWN_BUFFERS - Size of upstream buffer. #ifndef SEGGER_RTT_CONFIG_MAX_NUM_DOWN_BUFFERS #define SEGGER_RTT_CONFIG_MAX_NUM_DOWN_BUFFERS 2 #endif
// <o> SEGGER_RTT_CONFIG_DEFAULT_MODE - RTT behavior if the buffer is full.
// <i> The following modes are supported: // <i> - SKIP - Do not block, output nothing. // <i> - TRIM - Do not block, output as much as fits. // <i> - BLOCK - Wait until there is space in the buffer. // <0=> SKIP // <1=> TRIM // <2=> BLOCK_IF_FIFO_FULL
#ifndef SEGGER_RTT_CONFIG_DEFAULT_MODE #define SEGGER_RTT_CONFIG_DEFAULT_MODE 0 #endif
// </h> //==========================================================
// </h> //==========================================================
|
增加代碼完成之后,可視化的界面(僅截取相關部分)如下
第三步 頭文件路徑
由於segger_rtt是新添加的,還要在工程中將該文件夾的路徑增加到搜索區域。一般使用相對路徑,配置的方法如下圖:
在SDK中,工程不作修改的情況下可以使用以下路徑:
..\..\..\..\..\..\..\external\segger_rtt
第四步 臨門一腳
在main()函數,增加一個代碼進行測試。
增加的代碼如下
SEGGER_RTT_printf(0,"haha, this is a rtt log\r\n");
第五步 大功告成
將上面的工程編譯后,下載到目標板,(注意,先不要處於Deubg狀態),打開J-Link RTT Viewer v6.60c(也可以是其他版本),這里是windows10,其他操作系統請百度。並且按下F2連接到當前的Jlink。復位目標板,在RTT Viewer 將顯示日志如下:
所有的工作完成!
這個方法適用於Nordic semi芯片的Keil工程,比如nrf52832, 52840等,這里以zigbee-ble混合工程為例,ble / thread工作應該也可以用。不同的IDE可以參照實現。