在 Nordic Semiconductor 的產品討論區上,看到有使用者詢問如何使用 SWO 來輸出資訊。在花了一點時間嘗試後,將使用方式記載如下:(原文網址)
After flash the code, execute the JLinkExe and set the SWO speed to 4MHz, then you will see the result on the screen:
The following steps showing how to configure the SWO
- 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
- 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.
- Set the trace enable register and trace control register which in the "Instrumentation Trace Macrocell" to turn on the ITM stimulus
- 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...