Skip to content

Instantly share code, notes, and snippets.

@todbot
Last active January 5, 2025 14:11
Show Gist options
  • Save todbot/0f284c45c38e7418bf7edbb0d1302f2f to your computer and use it in GitHub Desktop.
Save todbot/0f284c45c38e7418bf7edbb0d1302f2f to your computer and use it in GitHub Desktop.
Program a SAMD21 with OpenOCD and a JLINK
# Program a SAMD21 part with a bootloader using a JLINK / J-LINK
# 0a. Wire up your device with SWD & SWC pins to JLINK
# 0b. Also wire up Gnd and Power to Gnd & VTref on JLINK
# 0c. And separately supply power to device
# 0d. Iif you don't have openocd installed, then "brew install openocd"
#
# 1. Put this file in a directory
# 2. Get a samd21 bootloader file.bin and put it in this directory renamed as "bootloader.bin"
# 3. Unplug and replug in the JLINK (to reset it)
# 4. Then do "openocd -f ./openocd-sams21.cfg" and stand back
source [find interface/jlink.cfg]
transport select swd
source [find target/at91samdXX.cfg]
init
reset
halt
program bootloader.bin verify reset
shutdown
@todbot
Copy link
Author

todbot commented Dec 8, 2020

Another way. Uses load_image instead of program. Put this in openocd-samd21-way2.cfg and do openocd -f ./openocd-samd21-way2.cfg:

source [find interface/jlink.cfg]
transport select swd
source [find target/at91samdXX.cfg]
init
reset
halt
load_image bootloader.bin 0
verify_image bootloader.bin 0 bin
reset run
shutdown

@todbot
Copy link
Author

todbot commented Dec 8, 2020

Also, if you get the error:

Error: SAMD: NVM lock error

then add this line after the halt line:

at91samd bootloader 0

@celem
Copy link

celem commented Jan 5, 2025

The Dec 6, 2020 Hackaday link is dead.

@todbot
Copy link
Author

todbot commented Jan 5, 2025

The Dec 6, 2020 Hackaday link is dead.

The link is good but hackaday.io was having issues recently. Perhaps that's what you were experiencing.

@celem
Copy link

celem commented Jan 5, 2025

Thanks. The link now works. Too bad that its content has an important dead link that points to https://roamingthings.de/posts/use-j-link-to-change-the-boot-loader-protection-of-a-sam-d21/. Fortunately, the relevant info of the now dead site was extracted and listed in the Hackaday site.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment