Connecting to Target

OK you've done the compiling, now you are ready to connect!

In this case, I'll be connecting to an Atmel ATSAMD21G18 Cortex-M0 over SWD and uploading the Arduino bootloader to it. You can, of course, connect to any processor that OpenOCD supports but this is the one I've got handy

Wire up the target to SWD

Of course connections must be made! Note that we are using the "BCM" pin numbering convention


  • Target GND to Pi GND
  • Target SWDIO to Raspberry Pi #24
  • Target SWCLK to Raspberry Pi #25
  • Target Reset to Raspberry Pi #18 (may not be required)
  • If powering the chip directly from the Pi, connect 3.3V to 3.3V (I'm just powering the chip over USB)

Of course, this assumes that your chip is running at 3.3V logic. For 1.8 or 5V logic, level shifting may be required.

You can later change the pins used in the interfaces configuration file but for now, I suggest just going with the default

Create OpenOCD config

The easiest way to connect is creating a new directory in your home dir

cd ~
mkdir bootloader
cd bootloader

and then putting the file you want to program there, in this case I'm going to just grab the latest Arduino Zero bootloader (of course, substitute your own binary or hex!)


In the same directory, make a new file called openocd.cfg

nano openocd.cfg

and put the following into it:

source [find interface/raspberrypi2-native.cfg]
transport select swd
set CHIPNAME at91samd21g18
source [find target/at91samdXX.cfg]
# did not yet manage to make a working setup using srst
#reset_config srst_only
reset_config  srst_nogate
adapter_nsrst_delay 100
adapter_nsrst_assert_width 100
reset halt

Change raspberrypi2-native.cfg to whatever config you are using, e.g. for a Pi Zero or 1 use raspberrypi1-native.cfg or raspberrypi-native.cfg

If you're using a Pi Zero/1 you may also need to add

bcm2835gpio_swd_nums 25 24
bcm2835gpio_trst_num 7
bcm2835gpio_srst_num 18

You may need to also comment out reset_config  srst_nogate, some people report that is required to make it work

Save the config file and then run sudo openocd (no other args, its all in the config!) in the directory. You should get the following indicating a good connection

In particular make sure you get that target state:halted to you know it was able to connect!

Hit control-C to cancel out of openocd.

If you get unknown for state, or other errors, check your wiring! You may also need to powercycle or disconnect parts from the chip to get it into a good programming state. You may also need to change the programming frequency

Hit control-C to cancel out of openocd (or you can telnet 4444 if you want to send commands, won't be covered here.

Now you can change the openocd.cfg with nano to add commands for burning the binary file. At the bottom put in:

reset halt
at91samd bootloader 0
program samd21_sam_ba verify
at91samd bootloader 8192

This will init, look for targets, reset and halt the chip, turn off bootloader protection, burn in the bootloader file and verify it, re-turn-on bootloader protection, reset and shutdown openocd

You can skip the bootloader protection parts if you are not burning in a bootloader, of course

Of course, change the commands if you have a different file name, different chip, etc.

Save the file and run sudo openocd again:

Zoom! Programmed the bootloader in 0.02 seconds!

More Options

If you don't want to set up the configure file, you can actually do it all from the command line:

 sudo openocd -f interface/raspberrypi2-native.cfg -c "transport select swd; set WORKAREASIZE 0; adapter_nsrst_delay 100; adapter_nsrst_assert_width 100; source [find target/nrf51.cfg]" -c "init; reset; halt; nrf51 mass_erase; reset" -c "shutdown"

This will, for example, erase and reset a Nordic nRF51822 (which is a pretty finicky chip by the way, you may need to do hard resets to get it to talk to openocd)

This guide was first published on Mar 16, 2016. It was last updated on Mar 08, 2024.

This page (Wiring and Test) was last updated on Mar 16, 2016.

Text editor powered by tinymce.