nRF52 Development with CLion

Dan Shemesh
jumperiot
Published in
4 min readJun 20, 2017

--

TL;DR

If you just want to navigate (without debugging) through the nRF5-SDK using CLion, follow the following steps. For debugging go to “Debugging the nRF52 with CLion” below.

  1. (optional) Create a backup for your current nrf5-SDK directory
  2. cd into the root of the nrf5-SDK directory
  3. curl -s https://raw.githubusercontent.com/Jumperr-labs/nrf5-sdk-clion/master/nrf5-sdk-to-clion.sh | bash
  4. Open “./CMakeLists.txt” and uncomment the desired project’s path (you can uncomment multiple projects)
  5. From CLion, go to File → Open and choose the root directory of the nRF5-SDK.

You Deserve It

If you got here, you are probably an embedded software engineer as I am. If you did some work in the distant world of high-level programming such as JS or Python you probably know how helpful a good IDE can get. My favorite IDE for C development is JetBrain’s CLion, unlike other IDEs used for embedded programming, it has brilliant autocomplete, refactoring and navigation features.

I know what you’re thinking, you can’t beat IAR when it comes to the deep debugging of your insanely optimized stack. I agree that sometimes you are going to need a dedicated tool and even Eclipse with the GNU ARM Eclipse plugins can probably do more than CLion when it comes to reading registers and memory analysis. The way I see it CLion is like your everyday car, it is comfortable, easy to ride and gets you quickly to your destination, using IAR for code navigation is like driving a tank for taking your kids to school.

CLion and nRF52 — How Does it Work?

CLion is awesome, with one downside, it only works with CMake. By parsing the CMakeLists.txt files in your project, CLion, figures out where the definitions and declarations are, letting you navigate through the code (i.e. ⌘+Click, ctrl+Click). Trying to import the nRF5-SDK to CLion is not a pretty sight, most definitions and declarations are not found and the ones that are found might be incorrect or have multiple declarations.

High Level Description

You can check this GitHub repository for the source code. The script creates a Makefile that generates a CMakeLists.txt file for an existing project, then runs it for each example project in the SDK. At last it creates a main CMakeLists.txt that includes all of the other sub-projects. Since this might overload CLion, all of the sub-projects are commented out and we should uncomment the ones we want to use.

The CMakeLists.txt Generator

The Makefile reads the relevant variables from the project’s Makefile and translates them into CMake variables. It also adds the include_directories() , project() and add_executable() commands.

This CMakeLists.txt file is still not good enough, CLion is going to try and use the system’s default C compiler for generating the compiler’s macros, this results in macros such as __APPLE__ or __LINUX__ being enabled and we do not want that. Since trying to change the compiler to arm-none-eabi-gcc caused CLion to go into an infinite loop, I decided to keep the default C compiler while disabling compiler macros and adding the __GNUC__ macro instead:

list(APPEND CFLAGS "-undef" "-D__GNUC__")
list(FILTER CFLAGS EXCLUDE REGEX mcpu)

Debugging the nRF52 with CLion

Prerequisites

  1. Segger JLink
  2. ARM Embedded Toolchain

Instructions:

1. After adding your project to CLion, go to Run → Edit Configurations.

2. Click on the “+” sign and choose GDB Remote Debug.

3. Add arm-none-eabi-gdb to your list of debuggers.

4. Set ‘target remote’ args to: “tcp:localhost:2331” and Symbol file to the full path of your ”_build/nrf52832_xxaa.out” file:

5. Click OK

6. Verify that the firmware is flashed onto the device and start the JLink GDB Server: JLinkGDBServer -device nrf52 -singlerun -strict -timeout 0 -nogui -if swd -speed 1000 -endian little

7. Start the debugger from CLion:

For each debug session, repeat steps 6, 7.

Share Your Thoughts

Feel free to add your comments below and let me know what you think. If you find this interesting — signup to get updates from Jumper and join our closed beta.

--

--