Skip to content

Build, program and debug NRF52 project with JLink, CMake and CLion

JF002 edited this page Oct 27, 2019 · 2 revisions

Introduction

This tutorial explains the steps I took to build, program and debug my NRF52 project using CMake, CLion and a J-Link debug probe. I've tested these steps on the NRF52-DK (and its embedded J-Link probe), and I'm using the NRF52-SDK version 15.3.0.

CMake configuration & build

First, we need to write some CMake in order to configure the project, the toolchain, find all include and source files from the SDK and specifiy all compilation switches. Luckily, someone has already done the job : https://github.com/Polidea/cmake-nRF5x. This repo provides multiple CMake files that configure the toolchain and the SDK. I've integrated this work in my base project for NRF52 : https://github.com/JF002/nrf52-baseproject

Here is how to use this baseproject using CMake in command line:

  • Download and unzip arm-none-eabi and NRF52 SDK

  • Clone this repo

  • Call CMake with the following command line argument

      -DARM_NONE_EABI_TOOLCHAIN_PATH=[Path to the toolchain]
      -DNRF5_SDK_PATH=[Path to the SDK]
      -DNRFJPROG=[Path to NRFJProg]
    
  • CMake:

$ mkdir build
$ cd build
$ cmake -DCMAKE_BUILD_TYPE=Debug -DARM_NONE_EABI_TOOLCHAIN_PATH=... -DNRF5_SDK_PATH=... -DNRFJPROG=... ../
  • Make
$ make -j

And with CLion:

  • Download and unzip arm-none-eabi and NRF52 SDK
  • Clone this repo
  • Open the project with CLion

Open project in CLion

  • Add variable definition for CMake : go to Settings -> Build, Execution, Deployment -> CMake and specify the following variables:

      -DARM_NONE_EABI_TOOLCHAIN_PATH=[Path to the toolchain]
      -DNRF5_SDK_PATH=[Path to the SDK]
      -DNRFJPROG=[Path to NRFJProg]
    

  • Reload the CMake project and build the application (application-example in the case of the base project

Program & debug the MCU with your software

I've found 2 ways to doing that : using the custom target FLASH_application-example generated by the CMake scripts from Polidea, or using the Embedded GDB Server functionality from CLion.

Using the custom target FLASH_application-example

This target has been created by the CMake scripts provided by Polidea. All you have to do is selecting the target FLASH_application-example in the target list and hit the button build:

Using an embedded configuration in CLion

This method is my favorite, as it's more integrated in CLion, and it allows to build, program and debug your application. For this, you need to create a new Embedded GDB Server configuration:

  • Select Edit configurations... in the list of targets
  • Click the '+' symbol on the top left to create a new configuration and select Embedded GDB Server and fill the form:
    • Target and executable should point to your application
    • Download Executable allows you to specify if it should upload the binary to the MCU everytime, only if updated or never
    • Target remote args is the connection string to the JLinkGDBServer : tcp:localhost:2331
    • GDBServer is the software CLion will launch for the programmation and debug : JLinkGDBServer
    • Specify the following parameters : -device nrf52 -strict -timeout 0 -nogui -if swd -speed 1000 -endian little

Now, you see that the embedded configuration is added to the configuration list:

You can build this configuration and then program and debug it (with the debug button). While in debugging, you can add breakpoints, run step by step, read variables,..

Read logs with RTT

RTT is a feature from Segger's JLink devices that allows bidirectionnal communication between the debugger and the target. This feature can be used to get the logs from the embedded software on the development computer.

  • Program the MCU with the code (see above)
  • Start JLinkExe ...
$ JLinkExe -device nrf52 -if swd -speed 4000 -autoconnect 1
  • ... or JLinkGDBServer (CLion automatically run it when you debug an embedded configuration)

  • Start JLinkRTTClient

$ JLinkRTTClient
###RTT Client: ************************************************************ 
###RTT Client: *               SEGGER Microcontroller GmbH                * 
###RTT Client: *   Solutions for real time microcontroller applications   * 
###RTT Client: ************************************************************ 
###RTT Client: *                                                          * 
###RTT Client: *       (c) 2012 - 2016  SEGGER Microcontroller GmbH       * 
###RTT Client: *                                                          * 
###RTT Client: *     www.segger.com     Support: support@segger.com       * 
###RTT Client: *                                                          * 
###RTT Client: ************************************************************ 
###RTT Client: *                                                          * 
###RTT Client: * SEGGER J-Link RTT Client   Compiled Oct  2 2019 09:46:59 * 
###RTT Client: *                                                          * 
###RTT Client: ************************************************************ 

###RTT Client: -----------------------------------------------
###RTT Client: Connecting to J-Link RTT Server via localhost:19021 ...
###RTT Client: Connected.

SEGGER J-Link V6.52a - Real time terminal output
J-Link OB-SAM3U128-V2-NordicSemi compiled Jan  7 2019 14:07:15 V1.0, SN=682579153
Process: JLinkGDBServerCLExe
<info> app: coucou

<info> app: coucou

<info> app: coucou