This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Using SWO with nRF52 (redux)

I know this question has been asked before, but I couldn't find an answer...

I'm looking for an example of how to use the nRF52832's SWO feature with the SEGGER JLink product. I have a PCA10040 board that I can program and debug via Eclipse/GCC/Jlink. I would now like to use the SWO feature to simulate a virtual UART. (I realize that I could use the PCA10040's real UART but my eventual target doesn't have the UART so I want to use the SWO feature instead.)

I have set the following additional defines in my otherwise unchanged Nordic sample code: CFLAGS += -DENABLE_DEBUG_LOG_SUPPORT

CFLAGS += -DNRF_LOG_USES_RTT=1

CFLAGS += -DENABLE_SWO

With these settings I can single step through the initialization and write attempts to the SEGGAR functions found in the nordic SDK (external/segger_rtt/SEGGER_RTT*). This all looks fine.

I have tried to view the RTT output using JLinkGDBServer and JLinkSWOVIewer. Neither of these applications sees any output from my target.

Here is the output from JLinkGDBServer console:

SEGGER J-Link GDB Server V5.12e Command Line Version

JLinkARM.dll V5.12e (DLL compiled Apr 29 2016 15:06:43)

-----GDB Server start settings-----

GDBInit file: none

GDB Server Listening port: 2331

SWO raw output listening port: 2332

Terminal I/O port: 2333

Accept remote connection: yes

Generate logfile: off

Verify download: off

Init regs on start: off

Silent mode: off

Single run mode: off

Target connection timeout: 0 ms

------J-Link related settings------

J-Link Host interface: USB

J-Link script: none

J-Link settings file: none

------Target related settings------

Target device: nRF52832_xxAA

Target interface: SWD

Target interface speed: 4000kHz

Target endian: little

Connecting to J-Link...

J-Link is connected.

Firmware: J-Link OB-SAM3U128-V2-NordicSemi compiled Mar 15 2016 18:03:17

Hardware: V1.00

S/N: 682957762

Checking target voltage...

Target voltage: 3.30 V

Listening on TCP/IP port 2331

Connecting to target...Connected to target

Waiting for GDB connection...Connected to 127.0.0.1

Reading all registers

Read 4 bytes @ address 0x0001D2AA (Data = 0xEB0A45BB)

Reading 8 bytes @ address 0x2000FEB8

...

gdb> mon SWO EnableTarget 0 0 1 0

SWO enabled succesfully.

gdb> mon SWO Start 0 900000

SWO started.

--- END OF QUOTE ---

I have connected to both SWO related telnet ports (2332 and 2333) and nothing comes out. I am guessing that I am not sending the correct commands to JLink monitor for this board but I can't seem to find any documentation that describes what it should be.

Any help would be appreciated.

Jack

Parents
  • Hello Jprofit,

    The following steps showing how to configure the SWO, hope it can satisfy your question : D

    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...
    
  • Tested and approved, thanks Rick ! For my use case, I write 'retarget' functions:

    int _write(int file, char *ptr, int len) {
    	UNUSED_PARAMETER(file);
    	for (int i = 0; i < len; i++)
    		ITM_SendChar(ptr[i]);
    	return len;
    }
    
    int _write_r(struct _reent *r, int file, char *ptr, int len) {
    	UNUSED_PARAMETER(r);
    	UNUSED_PARAMETER(file);
    	for (int i = 0; i < len; i++)
    		ITM_SendChar(ptr[i]);
    	return len;
    }
    
Reply Children
No Data
Related