Skip to content
This repository has been archived by the owner on Jan 6, 2023. It is now read-only.

Realtime debugging with Segger J Link and CLion

Phillip Schuster edited this page Jan 24, 2018 · 3 revisions

From Rev 0.2 it's hard to debug Printrhub and it takes quite some time to flash MK20 (through OTA updates using ESP). But as we have the SWD signals exposed we can setup CLion (C/C++ IDE from Jetbrains) for debugging as you love it (step by step, change and look into variables).

Prerequisites

This software needs to be installed at the given location. If you choose to install them to another directory you'll have to adjust some settings, that are obvious though. It's expected that you use a Mac.

Printrhub Rev 0.2+

Any Printrhub from Rev 0.2 will do it as the SWD signals are exposed. From Rev 0.4 they are exposed via special test points, while on Rev 0.2 and Rev 0.3 some wires will be need to soldered on ESP pins, but they are large enough that it's simple and a quite robust connection.

CLion

You can download CLion as a free 30 day trial here: https://www.jetbrains.com/clion/. It's not cheap but very powerful, good looking and very fast. And whatever coding style you love, there is a style/template or plugin available.

Installation directory: /Applications

Arduino IDE 1.6.7 with Teensyduino

Download Arduino IDE 1.6.7 here: http://arduino.cc/download_handler.php?f=/arduino-1.6.7-macosx.zip. A later version should work fine, too. But it's important for your version to have the same folder structure as 1.6.7 (Arduino changed them a few times). As we don't use the IDE, we don't care about the version number

Installation directory: /Applications (renamed from Arduino to Arduino_1.6.7 so we don't conflict with your current installation

Teensyduino

Download Teensyduino here: https://www.pjrc.com/teensy/td_130/TeensyduinoInstall.dmg. Just run the installer and point the target directory to /Applications/Arduino_1.6.7 (that you created in the previous step). You don't need to install any libraries, as we are only interested in the binary tools.

Installation directory: /Applications/Arduino_1.6.7

SEGGER J-Link

Download and install SEGGER J-Link software package from: https://www.segger.com/downloads/jlink/JLink_MacOSX_V610.pkg.

Installation directory: /Applications/SEGGER

There are the important files we care about and why we installed all that stuff: /Applications/Arduino_1.6.7.app/Contents/Java/hardware/tools/arm/bin/arm-none-eabi-gdb /Applications/SEGGER/J-Link/JLinkGDBServer

Running and testing in CLion

Start CLion and open the folder mk20 from where you checked out your display repository. I.e. I have checked out the display to: ~/Documents/Programming/Printrbot/Printrhub, therefore I need to open the folder ~/Documents/Programming/Printrbot/Printrhub/mk20 in CLion

CLion opens the project and due to the CMake configuration created using the platformio command line (see http://docs.platformio.org/en/stable/ide/clion.html) that are already available in the repository you should be able to compile the project.

Make sure you enable the toolbar in Menu->View->Toolbar. This reveals the configuration drop down. Choose PLATFORMIO_BUILD configuration and run the build with Menu->Run->Build.

The project should successfully be compiled now.

Wiring

You have to attach the J-Link debug probe to your Printrhub Rev 0.2+.

  • Pin 1: Connect to 3.3V on Printer expansion port or any other 3.3V supply pin.
  • Pin 7: Connect to ESP Pad (not GPIO!) 5 (Signal SWD_IO)
  • Pin 9: Connect to ESP Pad (not GPIO!) 14 (Signal SWD_CLK)
  • Pin 15: Connect to RESET_B (there is a test point named RES_B)
  • Pin 4: Connect to GND pin (GND on Printer expansion port is a good place)

Setup remote debugging

Now is the last step. What we do now setting up remote debugging. Remote debugging allows for debugging using TCP/IP. As Segger provides a GDB server via TCP we can use that to debug with the J-Link probe.

Open the debug configuration dialog using Menu->Run->Edit Configurations. You should see this dialog. Click on the + icon on the top left corner of the window and choose "GDB Remote Debug". Give it the name "J-Link".

Now fill in these values that it finally looks like this:

GDB: Click on the .. icon on the right side and in the next window, click on the + to add a new file. Choose the gdb binary from the Teensyduino file, if you used the installation directories described above choose this file: /Applications/Arduino_1.6.7.app/Contents/Java/hardware/tools/arm/bin/arm-none-eabi-gdb

'target remote args': tcp:localhost:2331

Symbol file: Enter (or click the .. icon to choose with file browser) the folder of the firmware.elf file generated by CLion when compiling the project for the first time. As I have checked out my project in ~/Documents/Programming/Printrbot/Printrhub I had to enter this folder: /Users/pschuster/Documents/Programming/Printrbot/Printrhub/mk20/.pioenvs/teensy31/firmware.elf

In the "Before launch" area click on the + icon and add the PLATFORMIO_BUILD configuration so every time we debug we make sure we the firmware.elf file is up to date by building the project.

Start debugging

You need to start the GDB remote server provided by SEGGER so CLion can connect to it. This documentation expects that you run CLion and SEGGER on the same machine, basically you could even debug on another machine.

Make sure you connected the J-Link to your USB port. Also make sure wires are correctly attached.

Open terminal and enter this command: JLinkGDBServer -if SWD -device MK20DX256VLH7

If you have done everything right, you should see this:

GDB server runs and awaits connections from CLion now, if you see messages like "Cannot halt device" or other errors, just hard reset it by removing power supply for a second. After that it should work. Sometimes, from previous sessions MK20 is left in a state where it needs to be restarted.

Now, in CLion, choose the Remote GDB configuration you just created (should be named "J-Link"). Set a breakpoint in Application::loop and start debugging in the menu by running Menu->Run->Debug 'J-Link'. This should compile the latest firmware version, initiates GDB debugging and uploads the firmware and stops at the breakpoint you set. Now you can run the code step-by-step and watch variables.