Unreliable upload to ATSAMD21 chips using JLink when chip is running DMA

Hi all,

I’ve had some problems uploading to some of my custom ATSAMD21E15/16/17 boards from PlatformIO using JLink - the upload usually fails with a timeout or hangs although sometimes goes through and then it works fine.

These problems would happen when uploading to a chip which is active and running a program whilst if the chip was manually powered up or reset just before the upload command was called by the PlatformIO builder, it would work fine.

Recently whilst developing a new device I’ve noticed that a board for which upload via JLink was working fine, started having these problems when I upgraded the program to use DMA (specifically to copy data from the ADC RESULT register to a memory buffer) and was actually worse than I saw before (in this specific program DMA is running almost constantly)

I have managed to correct this problem by altering the generation of the upload.jlink file of commands that PlatformIO generates for JLink, specifically in platform-atmelsam/blob/develop/builder/main.py, what’s in line 188 should be moved to just before line 185 (i.e. the command Reset in the generated file should come before the command Halt).

This solves all JLink upload problems for my custom boards ( design here ) but I don’t have the means to check it for any SAM boards which are officially covered by this platform in PlatformIO and this builder code so I’m quite wary of just submitting this change to source control (assuming I as a new user could actualy submit code there), plus my knowledge of JLink is minimal and I just made that change as a semi-informed “let’s try this”.

So, is there somebody who can pick this up, at very least validate that having JLink reset the chip first is not ridiculous, maybe check it against other SAM devices and patch the official PlatformIO code?

Just wanted to add that following Segger’s own examples for J-Link Commander here and from a little experimentation, the change in builder/main.py on the atmelsam platform should be to copy the reset command rather than move it, so that in the JLink batch command file for uploading there’s a reset on top of the file to stop the microcontroller no matter what and the reset at the end seems to be needed to make sure the updated uploaded binary starts getting correctly executed after uploading.

In the work I’ve been doing so far, since this change I have never again had a repeat of the timeout/freeze problem when uploading via J-Link to a SAMD21 microcontroller - it now works 100% of the time.

Should I just go ahead and submit this change myself to the atmelsam project in PlatformIO’s Github?

I have now opened an issue for this in the platform repository.