Debugging on nrf52840 with GDB from CLI on linux

Today I tried to find the better ways to debug program on nRF52840 PDK from CLI on linux.

The ways that I tried

  1. [Failed] openocd (both stlink and jlink) & gdb
  2. [Succeeded] JLink & JLinkGDBServer & gdb

OpenOCD (failed)

I could connect to nrf52840 with openocd (jlink).

openocd -f interface/jlink.cfg  -c 'adapter_khz 1000; transport select swd' -f target/nrf51.cfg

gdb
gdb> target remote localhost:3333
gdb> load ${binary}

But there was a issue when I debug a program with GDB.

nrf52.cpu -- clearing lockup after double fault

I cound not resolve this.

reference

  • https://devzone.nordicsemi.com/question/78890/programming-nrf52-with-openocd/
  • https://primalcortex.wordpress.com/2017/06/10/setting-up-openocd-for-programming-the-nordic-nrf52832-chip/

JLink & JLinkGDBServer (success)

In my environment, JLink could not connect to board by using transport 'jtag'. So I used 'swd'.

$ JLinkExe
SEGGER J-Link Commander V6.22a (Compiled Nov 28 2017 17:57:05)
DLL version V6.22a, compiled Nov 28 2017 17:57:00

Connecting to J-Link via USB...FAILED: Cannot connect to J-Link via USB.
J-Link>connect
J-Link connection not established yet but required for command.
Connecting to J-Link via USB...FAILED: Cannot connect to J-Link via USB.
J-Link>
Unknown command. '?' for help.

J-Link>connect
J-Link connection not established yet but required for command.
Connecting to J-Link via USB...O.K.
Firmware: J-Link OB-SAM3U128-V2-NordicSemi compiled Jul 24 2017 17:30:12
Hardware version: V1.00
S/N: 683825097
VTref = 3.300V
Please specify device / core. <Default>: NRF52832_XXAA
Type '?' for selection dialog
Device>
Please specify target interface:
  J) JTAG (Default)
  S) SWD
TIF>S
Specify target interface speed [kHz]. <Default>: 4000 kHz
Speed>
Device "NRF52832_XXAA" selected.


Connecting to target via SWD
Found SW-DP with ID 0x2BA01477
Found SW-DP with ID 0x2BA01477
Scanning AP map to find all available APs
AP[2]: Stopped AP scan as end of AP map has been reached
AP[0]: AHB-AP (IDR: 0x24770011)
AP[1]: JTAG-AP (IDR: 0x02880000)
Iterating through AP map to find AHB-AP to use
AP[0]: Core found
AP[0]: AHB-AP ROM base: 0xE00FF000
CPUID register: 0x410FC241. Implementer code: 0x41 (ARM)
Found Cortex-M4 r0p1, Little endian.
FPUnit: 6 code (BP) slots and 2 literal slots
CoreSight components:
ROMTbl[0] @ E00FF000
ROMTbl[0][0]: E000E000, CID: B105E00D, PID: 000BB00C SCS-M7
ROMTbl[0][1]: E0001000, CID: B105E00D, PID: 003BB002 DWT
ROMTbl[0][2]: E0002000, CID: B105E00D, PID: 002BB003 FPB
ROMTbl[0][3]: E0000000, CID: B105E00D, PID: 003BB001 ITM
ROMTbl[0][4]: E0040000, CID: B105900D, PID: 000BB9A1 TPIU
ROMTbl[0][5]: E0041000, CID: B105900D, PID: 000BB925 ETM
Cortex-M4 identified.

After connected JLink, In another terminal,

JLinkGDBServer -if swd -speed 1000 -device nrf51822 -if swd -speed 1000 -port 2331

Now, gdb can connect via localhost:2331,

(gdb) target remote localhost:2331
Remote debugging using localhost:2331
0x00002ee2 in ?? ()
(gdb) load
Loading section .text, size 0x88ec lma 0x12000
Loading section .log_const_data, size 0x78 lma 0x1a8ec
Loading section .pwr_mgmt_data, size 0x4 lma 0x1a964
Loading section .sdh_stack_observers, size 0x10 lma 0x1a968
Loading section .sdh_ant_observers, size 0x10 lma 0x1a978
Loading section .ARM.exidx, size 0x8 lma 0x1a988
Loading section .data, size 0x60c lma 0x1a990
Loading section .log_dynamic_data, size 0xb4 lma 0x1af9c
Start address 0x186a0, load size 36944
Transfer rate: 36078 KB/sec, 3694 bytes/write.
(gdb) 

It works! Now I can run a program in gdb.

reference

  • https://devzone.nordicsemi.com/question/29222/debugging-under-linux-solved/