PlatformIO Community

JLink ‘Stopping in Setup()'

Hi,
I am at whits end trying to fix this and I am getting nowhere.

I have a Link EDU Mini connected to a MKRWAN 1310 via the pads on the bottom, both connected to my Mac via separate USB cables and I can’t get debug to work. I say I can’t get it to work, that is not quite right, it gets so far and then just stops, no explanation, no error, just nothing.

So far I have removed Arduino IDE completely, installed a fresh version of VSCode, PlatformIO, installed the GCC toolchain, tried various settings, different applications etc and I get to the same point every time.

I start debug, and after a short time of compiling, I get to a code showing the debug controls and sitting at the function init(). I click on the over to progress debugging and it moves forward and opens setup(), at which point it just stops. This suggests the debugger is working and connected to the Segger, but as to how to progress, I have no idea.

Below are the configuration files:

here is my c_cpp_properties file:-

{
"configurations": [
{
"name": "!!! WARNING !!! AUTO-GENERATED FILE, PLEASE DO NOT MODIFY IT AND USE [docs.platformio.org/page/proje…nv_build.html#build-flags](https://docs.platformio.org/page/projectconf/section_env_build.html#build-flags)"
},
{
"name": "Mac",
"macFrameworkPath": [],
"includePath": [
"/Users/matthew/Documents/PlatformIO/Projects/arduino_jlink/include",
"/Users/matthew/Documents/PlatformIO/Projects/arduino_jlink/src",
"/Users/matthew/.platformio/packages/framework-cmsis@1.40500.0/CMSIS/Include",
"/Users/matthew/.platformio/packages/framework-cmsis-atmel/CMSIS/Device/ATMEL",
"/Users/matthew/.platformio/packages/framework-arduino-samd/cores/arduino",
"/Users/matthew/.platformio/packages/framework-arduino-samd/variants/mkrwan1300",
"/Users/matthew/.platformio/packages/framework-arduino-samd/libraries/HID",
"/Users/matthew/.platformio/packages/framework-arduino-samd/libraries/I2S/src",
"/Users/matthew/.platformio/packages/framework-arduino-samd/libraries/SAMD_AnalogCorrection/src",
"/Users/matthew/.platformio/packages/framework-arduino-samd/libraries/SAMD_BootloaderUpdater/src",
"/Users/matthew/.platformio/packages/framework-arduino-samd/libraries/SDU/src",
"/Users/matthew/.platformio/packages/framework-arduino-samd/libraries/SPI",
"/Users/matthew/.platformio/packages/framework-arduino-samd/libraries/USBHost/src",
"/Users/matthew/.platformio/packages/framework-arduino-samd/libraries/Wire",
"/Users/matthew/.platformio/packages/tool-unity",
""
],
"browse": {
"limitSymbolsToIncludedHeaders": true,
"path": [
"/Users/matthew/Documents/PlatformIO/Projects/arduino_jlink/include",
"/Users/matthew/Documents/PlatformIO/Projects/arduino_jlink/src",
"/Users/matthew/.platformio/packages/framework-cmsis@1.40500.0/CMSIS/Include",
"/Users/matthew/.platformio/packages/framework-cmsis-atmel/CMSIS/Device/ATMEL",
"/Users/matthew/.platformio/packages/framework-arduino-samd/cores/arduino",
"/Users/matthew/.platformio/packages/framework-arduino-samd/variants/mkrwan1300",
"/Users/matthew/.platformio/packages/framework-arduino-samd/libraries/HID",
"/Users/matthew/.platformio/packages/framework-arduino-samd/libraries/I2S/src",
"/Users/matthew/.platformio/packages/framework-arduino-samd/libraries/SAMD_AnalogCorrection/src",
"/Users/matthew/.platformio/packages/framework-arduino-samd/libraries/SAMD_BootloaderUpdater/src",
"/Users/matthew/.platformio/packages/framework-arduino-samd/libraries/SDU/src",
"/Users/matthew/.platformio/packages/framework-arduino-samd/libraries/SPI",
"/Users/matthew/.platformio/packages/framework-arduino-samd/libraries/USBHost/src",
"/Users/matthew/.platformio/packages/framework-arduino-samd/libraries/Wire",
"/Users/matthew/.platformio/packages/tool-unity",
""
]
},
"defines": [
"PLATFORMIO=50003",
"ARDUINO_SAMD_MKRWAN1310",
"__SAMD21G18A__",
"USE_ARDUINO_MKR_PIN_LAYOUT",
"USE_BQ24195L_PMIC",
"VERY_LOW_POWER",
"ARDUINO=10805",
"F_CPU=48000000L",
"USBCON",
"USB_VID=0x2341",
"USB_PID=0x8059",
"USB_PRODUCT=\"Arduino MKR WAN 1310\"",
"USB_MANUFACTURER=\"Arduino\"",
"ARDUINO_ARCH_SAMD",
""
],
"intelliSenseMode": "gcc-x64",
"cStandard": "c11",
"cppStandard": "c++11",
"compilerPath": "/Users/matthew/.platformio/packages/toolchain-gccarmnoneeabi@1.70201.0/bin/arm-none-eabi-gcc",
"compilerArgs": [
"-mcpu=cortex-m0plus",
"-mthumb",
""
]
],
"version": 4

My platformio.ini file

[env:mkrwan1310]
platform = atmelsam
board = mkrwan1310
framework = arduino

debug_tool = jlink
upload_protocol = jlink

My launch.json:
// AUTOMATICALLY GENERATED FILE. PLEASE DO NOT MODIFY IT MANUALLY
// PIO Unified Debugger
//
// Documentation: [docs.platformio.org/page/plus/debugging.html](https://docs.platformio.org/page/plus/debugging.html)
// Configuration: [docs.platformio.org/page/projectconf/section_env_debug.html](https://docs.platformio.org/page/projectconf/section_env_debug.html)

{
"version": "0.2.0",
"configurations": [
{
"type": "platformio-debug",
"request": "launch",
"name": "PIO Debug",
"executable": "/Users/matthew/Documents/PlatformIO/Projects/arduino_jlink/.pio/build/mkrwan1310/firmware.elf",
"projectEnvName": "mkrwan1310",
"toolchainBinDir": "/Users/matthew/.platformio/packages/toolchain-gccarmnoneeabi@1.70201.0/bin",
"svdPath": "/Users/matthew/.platformio/platforms/atmelsam/misc/svd/ATSAMD21G18A.svd",
"preLaunchTask": {
"type": "PlatformIO",
"task": "Pre-Debug"
},
"internalConsoleOptions": "openOnSessionStart"
},
{
"type": "platformio-debug",
"request": "launch",
"name": "PIO Debug (skip Pre-Debug)",
"executable": "/Users/matthew/Documents/PlatformIO/Projects/arduino_jlink/.pio/build/mkrwan1310/firmware.elf",
"projectEnvName": "mkrwan1310",
"toolchainBinDir": "/Users/matthew/.platformio/packages/toolchain-gccarmnoneeabi@1.70201.0/bin",
"svdPath": "/Users/matthew/.platformio/platforms/atmelsam/misc/svd/ATSAMD21G18A.svd",
"internalConsoleOptions": "openOnSessionStart"
}
]

PlatformIO is completely separate from any Arduino IDE installation that may or may not be on the system. Also, installing a GCC toolchain on the computer is also not used, as PlatformIO will always use its own compiler toolchains from its package repository, locally stored in /Users/matthew/.platformio/packages/ in your case.

Can you show with screenshots exactly what you’re doing, step-by-step? If it reaches a breakpoint in setup() then that sounds perfectly fine. The debugger will only break on further breakpoints you set (by clicking on the line number in the code panel which will place a red dot), or, if you are already at a breakpoint, if you use the “Step into” or “Step over” (line of code) buttons, or if you pause the processor manually by pressing the pause button.

When the code stops in setup(), does pressing the “Step over” button advance execution by one line?

Also see documentation: https://docs.platformio.org/en/latest/integration/ide/vscode.html#debugging

HI maxfgerhardt,

Thanks for coming back to me so quickly.

I agree, there should be no link and therefore was completely unnecessary, but I lost nothing by trying.

After clicking on the little green triangle in the top right next to PIO Debug, it compilers and I am presented with this screenshot
image

I can now StepOver and it appears to work as intended with the cursor incrementing through the core code and eventually after a few clicks I am presented with this screenshot. Interestingly the debugger has gone into Setup and not StepOver as I clicked

image

At this point, no amount of clicking progresses any further, regardless of the button pressed. I would have expected it to progress into loop(), but it never does.

Any ideas?

Please show a screenshot of what the VSCode IDE looks like when you press “step over” after your last screenshot.

Next, place a breakpoint at line 20 and restart debugging. Use the “run” button (first) to forward execution. Does it hit the breakpoint?

I also note that you’re using Serial.println("hello"); without having called Serial.begin(9600); (example) first, and you’re also not using a while(!Seriail) yield(); loop after that to wait for a USB connection. Can you remove the Serial.println() line and retry? (Because Serial.println() might attempt to initialize the USB-CDC peripheral and trigger the USB enumeration, thus throwing off the debugger hard.)

If none of these work, you may be running into the issue described in Problems starting debug session with jlink on feather M0, which is specific to Jlink + SAMD21G18, the microcontroller chip on your MKRWAN1310. In that case I can walk you through a solution attempt, derived from the linked post.

1 Like

Hi Maxgerhardt,

It looks no different, it doesn’t change.

image

image

I have removed the Serial.println, no difference.

What I have noticed is that if I put

break main.cpp:12

It progresses a bit further, stopping in the first line of the function pinMode.

It really feels like there is a communication problem between the Segger and the SAMD21G18, so I can see why the boot loader may be an issue. I’m not confident in programming it, but I will take a look. Interestingly in the boot loader section of ArduinoCore-Samd, there isn’t one for mkrwan1310, there are others, but not this one.

thanks again for helping.

Actually before the no-bootloader method then, can you put debug_init_break = break loop in the platformio.ini (docs) and restart debugging? It should attempt to not stop at main() then anymore but directly in loop().

Hi,

I added a debug_init_break = break main.cpp:12 in platformio, at which point it makes it further, but still stops after a few commands, as it heads into pinMode function when I click on StepOver.

I know this may be a complete read herring, but I am programming the mkrwan via the normal method, not via the segger, does that make a difference?

If after clicking on StepOver for several times to see if the LED actually blinks, if I click on StepOut, I get a message ’Starting Target CPU…’ at it just freezes :-

image

Here is my platformio file:

[env:mkrwan1310]
platform = atmelsam
board = mkrwan1310
framework = arduino
debug_tool = jlink
upload_protocol = jlink
debug_init_break = break main.cpp:12

In investigating the boot loader tool, the Atmel Studio is only available on Windows, which is a little challenging as we only use Mac in the house! I’ll need to download and setup a virtual image to reprogram the boot loader!

Hm the way I see it, there is one for a MKR WAN 1300 at https://github.com/adafruit/ArduinoCore-samd/tree/master/bootloaders/mkrwan1300, so it should be available in the Arduino IDE on Mac OS too.

The MKR WAN 1300 and MKR WAN 1310 use the same ATSAMD21G18A chip after all, the 1310 just has a flash chip more and an I2C connector on the side, as I’m reading it from the schematics of both boards. Bootloader should be the same.

You may be able to dump the complete chip content first using the JLink and the JLink tool on the command line tools (located in your /User/<user>/.platformio/packages/tool-jlink folder), with docs from here and here [firmware size should be 0x40000 bytes (256kByte), starting from flash address 0x0000000]. So with the saved firmware dump, it should include the bootloader as well and allow you to have an emergency backup in all cases to flash back.

Another pragmatic way, one that would also be intersting to the community, would be to see if this is really a problem with the combination of JLink + SAMD218G18. An ST-Link clone is very cheap (4-6€ delivered here in Germany) and also has the capability to debug any ARM chip. PlatformIO has support for it via setting debug_tool = stlink (also upload_protocol). If you can’t make progress with the JLink, an ST-Link might be an alternative to try.

Someone made progress on the JLink + SAMD21G18 side: Difficulties to debug Seeed Wio Terminal with JLink Probe

YOu can have a look at that thread and the recommendations for restarting the debug session and/or commenting out some USB code.

Hello, the Wio Terminal is a ATSAMD51P19.
However there were some issues with the Adfruit Feather M0 which is a SAMD21G18.

The solution of @prototypicalpro worked for me.

Oh you’re right – I also have to correct this in some issues. But my basic gut feeling is that it has something to do in general with the microcontrollers that their own USB peripheral to do USB-CDC. In fact I’ve already linked your other project above with the SAMD21G18 MCU :smiley:

1 Like

I don’t quite believe this, but at least it is an answer to my problem, although it suggests a coding issue somewhere in the Segger / VSCode / PlatformIO world.

Following the advice of Continue, Restart, Continue, it appears that I now have working debugging, at least on this very simple script.

Thanks for your help in diagnosing and helping me with this one - I had become quite frustrated at being stuck without an answer.