Saturday, July 30, 2016

Using SWO on nRF52

在 Nordic Semiconductor 的產品討論區上,看到有使用者詢問如何使用 SWO 來輸出資訊。在花了一點時間嘗試後,將使用方式記載如下:(原文網址

The following steps showing how to configure the SWO
  1. add the additional define "ENABLE_SWO" to the project setting, which will set the nRF52 P0.18 as SWO instead of been a common GPIO
  2. set the TRACECONFIG register for trace port debug interface; the detail can be found in here. If you are using JLink-OB on the PCA10040, the maximum SWO sampling frequencey is 7.5MHz, so it is needed to configure the TRACEPORTSPEED to 4MHz.
  3. Set the trace enable register and trace control register which in the "Instrumentation Trace Macrocell" to turn on the ITM stimulus
  4. call the ITM_SendChar(uint32_t ch) to transmit the character via ITM channel 0, the detail can be found in the /components/toolchain/CMSIS/Include/core_cm4.h
Sample code:


 #include <stdint.h>  
 #include "nrf.h"  
 int main(void)  
 {  
   // step 2  
   NRF_CLOCK->TRACECONFIG = (NRF_CLOCK->TRACECONFIG & ~CLOCK_TRACECONFIG_TRACEPORTSPEED_Msk) |  
     (CLOCK_TRACECONFIG_TRACEPORTSPEED_4MHz << CLOCK_TRACECONFIG_TRACEPORTSPEED_Pos);  
   // step 3  
   ITM->TCR |= 1;  
   ITM->TER |= 1;  
   uint8_t c = 65;  
   while (true)  
   {  
     // step 4    
     ITM_SendChar(c);  
     if( c >= 90){  
       c = 65;  
     }else{  
       c++;  
     }  
   }  
 }  

After flash the code, execute the JLinkExe and set the SWO speed to 4MHz, then you will see the result on the screen:

 ~$ JLinkExe -if swd -device nrf52 -speed auto  
 JLink>connect  
 JLink>swoview 4000000  
 Receiving SWO data @ 4000 kHz  
 Data from stimulus port 0:  
 --------------------------------------------------------  
 ABCD...  

Using the app_trace module for debug log output in nRF5x SDK

前段時間在 facebook 上看到有人討論 nRF52 的使用以及 SDK 的 debug message log 功能,因此自己寫了一份說明解釋如下:(先簡短記載,再找時間修篇幅)

如果要從UART輸出log的話,以nRF5_SDK_11的gcc的template為例,必須完成以下步驟:
1. 在Makefile確認以下參數
CFLAGS += -DENABLE_DEBUG_LOG_SUPPORT

CFLAGS += -DNRF_LOG_USES_UART=1
ASMFLAGS += -DNRF_LOG_USES_UART=1
(上面兩個通常預設是-DNRF_LOG_USES_RTT=1)

2. 在main.c中有引入app_trace.h,並且有加入app_trace_init()

3. 預設UART輸出log的設定是有流量控制的,可以修改
nRF5_SDK_11/components/libraries/util/nrf_log.c
中的log_uart_init(), 將流量設定改成APP_UART_FLOW_CONTROL_DISABLED

4. 如果發現app_trace_log()只會印出第一個字,需要修正Makefile
(1.)在C_SOURCE_FILES中,移除
../../../../../../components/libraries/uart/app_uart.c \
加入
../../../../../../components/libraries/uart/app_uart_fifo.c \
../../../../../../components/libraries/fifo/app_fifo.c \
(2.)在INC_PATH中加入
INC_PATHS += $(abspath../../../../../../components/libraries/fifo)