This tutorial will show you how to debug an Arduino sketch using an Arduino MKR board and the Segger J-Link probe. It will go through how to connect these and use the Arduino IDE 2.0 to then debug a sketch.
J-Link debug probes are one of the most widely used line of debug and programming probes in the market. They have provide solid value to embedded development for over a decade. J-Link supports a wide range of CPUs and architectures, everything from single 8051 to mass market Cortex-M to high-end cores like Cortex-A. One of the main features of J-Link debug probes is its support of several debugging/programming interfaces like JTAG, SWD and SWO. For debugging SAM-based ArduinoĀ® boards with A J-Link probe, you are going to use its SWD interface.
JTAG is the de facto interface for debugging and accessing a processor registers. This interface has been used for many years, and it is still largely used today. But JTAG has a drawback: it uses many signals (or pins), at least four. To address this drawback, ARMĀ® created an alternative debug interface called the Serial Wire Debug (SWD) interface.
The SWD interface is a two-pin (SWDIO/SWCLK) electrical alternative to the JTAG interface that has the same JTAG protocol on top. The SWD interface can be used to program the firmware of a processor and access its registers for debugging purposes. This interface and its associated protocol are now available in nearly all ARMĀ® CortexĀ®-A, CortexĀ®-R, and CortexĀ®-M processors.
Before you can connect your J-Link probe to your ArduinoĀ® MKR board, you must prepare the SWD interface pins of the board. The ArduinoĀ® MKR boards have the SWD interface pins, SWDIO and SWCLK, connected to the J2 header. The J2 header of the ArduinoĀ® MKR boards is located on the underside of the board in the exposed pads.
Note: The goal is to reach the exposed pads that are located on the underside of the board, feel free to choose one of the two methods that are explained in this tutorial or to implement another solution not mentioned here.
A method for connecting that doesn't requires any soldering, uses a MKR Proto Shield and 6 2.54mm straight male headers to access the exposed pads of the MKR board as shown in the images below:
Besides not requiring any soldering, with this method the MKR Proto Shield remains fully reusable. Now you can connect the mini-squid cable to the straight headers in the shield.
It is also possible to solder connectors onto the exposed pads on the back of the MKR board, that will then allow us to connect the J-Link adapter to the board. Refer to the illustration below to see where the connectors need to be soldered.
If there is an adapter or cable already plugged into the J-link, remove the FLAT cable or J Link adapter and expose the male pins. Connect the MKR to the J-link using female to female jumper wires, use the illustration below to know which pins to connect.
When the connections have been made, simply power the J-link probe and the MKR via USB (micro USB and type A cables).
If you haven't already, download and install the Arduino IDE 2.0, this will be used to debug our sketch. You will also need to download the J-Link GDB Server software, here. The J-Link GDB Server is a remote server for the GNU Debugger (GDB) which allows to use a J-Link probe with GDB or any toolchain which uses GDB as debugging interface, like the Arduino IDE 2.0.
Note: write down where you installed the J-Link GDB Server software in your computer, this install path will be useful in the next step.
Now we are ready to start debugging our sketch. Connect the power to the MKR board and the J-link to power them up. Start Arduino IDE 2.0 and select your MKR board in the deployable menu where you specify the board and port.
Then create or open the sketch that you want to debug. If you don't already have a sketch in mind, feel free to use the example sketch found at the end of this tutorial.
Now go to the folder where the sketch is located. Add a
.json
file in the same folder as your sketch and name it debug_custom.json
. The easiest way would be to create a text file and rename it debug_custom.json
. In the .json
file, add the following lines: 1{2 "servertype": "jlink", 3 "device": "ATSAMD21G18",4 "interface": "SWD",5 "serverpath": "C:/Program Files (x86)/SEGGER/JLink/JLinkGDBServer"6}
The
"serverpath"
needs to be changed to the location where you installed the J-link package in the previous step. When this is done, click on the debugging icon.
You should see a window similar to the one shown below. Do not close this window, just minimize it and you are now ready to start debugging. You can add breakpoints, inspect variables, halt the execution and more.
In this tutorial you learned how to connect your MKR board to a Segger J-Link probe. And then how to use this setup to debug a sketch with Arduino IDE 2.0.
Now that you have your hardware set up, you can learn how to use the IDE 2.0 Debugger through the Debugging with the Arduino IDE 2.0 tutorial. This tutorial goes through some key features of the Debugger, and includes pointers to get started.
1#include<WiFiNINA.h>2#include<utility/wifi_drv.h>3
4int countVar1 = {0};5int countVar2 = {0};6
7unsigned long timer1 = {0};8unsigned long timer2 = {0};9int period1 = 1000;10int period2 = 500;11
12void setup() {13 Serial.begin(9600);14 WiFiDrv::pinMode(25, OUTPUT); //GREEN15 WiFiDrv::pinMode(26, OUTPUT); //RED16 WiFiDrv::pinMode(27, OUTPUT); //BLUE17
18 WiFiDrv::digitalWrite(27, HIGH); //Turn On Blue light19 delay(1000);20 WiFiDrv::digitalWrite(27, LOW); //Turn Off Blue light21 delay(1000);22 WiFiDrv::digitalWrite(26, HIGH); //Turn On Red light23 delay(1000);24 WiFiDrv::digitalWrite(26, LOW); //Turn Off Red light25 delay(1000);26 WiFiDrv::digitalWrite(25, HIGH); //Turn On Green light27 delay(1000);28 WiFiDrv::digitalWrite(25, LOW); //Turn Off Green light29 delay(1000);30}31
32 void loop() {33
34 if (millis() >= timer1 + period1) {35 timer1 = millis();36 countVar1++;37 WiFiDrv::digitalWrite(27, !digitalRead(27));38 }39
40 if (millis() >= timer2 + period2) {41 timer2 = millis();42 countVar2++;43 WiFiDrv::digitalWrite(26, !digitalRead(26));44 }45 }