How to unbrick a dead xiao using raspberry pi [ GUIDE ][openocd]

So guys, finally, i was able to revive my dead XIAO after hours of research on google. It can be done by following this guide from Adafruit.

https://learn.adafruit.com/programming-microcontrollers-using-openocd-on-raspberry-pi/overview

Things needed

  1. Raspberry pi
  2. Some jumper wires
  3. Soldering skills to break out the SWDIO and SWDCLK pins from the bottom of the board :stuck_out_tongue: (i had 2 XIAOs bricked. The first one’s pads came off trying to solder :stuck_out_tongue: )

IMPORTANT

  1. Download and keep the original XIAO bootloader froim here:

https://github.com/Seeed-Studio/ArduinoCore-samd/blob/master/bootloaders/XIAOM0/bootloader-XIAO_m0-v3.7.0-33-g90ff611-dirty.bin

  1. Follow the Adafruit guide to build openocd on raspberry pi and make the connections as mentioned. However, in the last stage of burning bootloader, the openocd commands need a change.

Instead of the command

program samd21_sam_ba verify

use the command

flash write_image “bootloader-XIAO_m0-v3.7.0-33-g90ff611-dirty.bin”

Also, i had to add the following lines in my openocd cfg file - though guide mentiontions its needed only for pi zero/1/2

bcm2835gpio_swd_nums 25 24
bcm2835gpio_trst_num 7
bcm2835gpio_srst_num 18

The chip was not getting discovered without those commands.

Once you execute the openocd command (sudo openocd -f openocd.cfg ),
you should see the amber LED pulsing :slight_smile: . You can now connect the XIAO to computer and upload your code using arduino.

Here is my config file for reference,

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 400
adapter_nsrst_assert_width 100

bcm2835gpio_swd_nums 25 24
bcm2835gpio_trst_num 7
bcm2835gpio_srst_num 18

#bcm2835gpio_peripheral_base 0xFE000000
#bcm2835gpio_speed_coeffs 236181 60

init
targets
reset halt
at91samd chip-erase
at91samd bootloader 0
flash write_image “bootloader-XIAO_m0-v3.7.0-33-g90ff611-dirty.bin”
#at91samd bootloader 8192
reset
shutdown

Let me know if you have any questions regarding the procedure (Note: I am not an openocd/swd expert)

2 Likes

@Baozhu @jiachenglu

pls share the guide to help folks with a bricked xiao

Hi @John_Doe
Thank you share this guide to us, I will add a link to this forum in Xiao’s Wiki so that more people can see your solution.

1 Like

@John_Doe thanks for this guide. You have quotes around the binary file name for the flash write_image command and they should not be included if you are using Linux. Other than that, all worked fine to unbrick two XIAOs with RPiZW.

You are welcome… I have kind of identified why XIAO is unstable…Its because of the UF2 bootloader it ships with. I will post soon about it today… @jiachenglu

2 Likes

Hi, I tried this but the xiao is still bricked. This was the output:
Open On-Chip Debugger 0.11.0-rc1+dev-00001-g0dd3b7fa6-dirty (2020-12-11-15:07)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
Info : BCM2835 GPIO JTAG/SWD bitbang driver
Info : clock speed 400 kHz
Info : SWD DPIDR 00000000

So I am not sure it did it. Yellow led not going and pc does not recognize it.
I am using a Pi-3B.

My Config:
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 srst delay 400
adapter srst pulse_width 100

bcm2835gpio_swd_nums 25 24
bcm2835gpio_trst_num 7
bcm2835gpio_srst_num 18

#bcm2835gpio_peripheral_base 0xFE000000
#bcm2835gpio_speed_coeffs 236181 60

init
targets
reset halt
at91samd chip-erase
at91samd bootloader 0
flash write_image “bootloader-XIAO_m0-v3.7.0-33-g90ff611-dirty.bin”
#at91samd bootloader 8192
reset
shutdown

Any insight would be appreciated.

Edit: I ran with debug and it looks like all the swd cmds received no valid ack’s back, so I guess the chip is gone? Very disturbing because all I did to kill it was to unplug from the pc. When next plugged in, was dead??? Something is extremely unstable.

Well, Dummy me tried this on a good one. Now have two that are bricked. Software revised and broke?
This is the output:
Open On-Chip Debugger 0.11.0-rc1+dev-00001-g0dd3b7fa6-dirty (2020-12-21-17:00)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
Info : BCM2835 GPIO JTAG/SWD bitbang driver
Info : clock speed 400 kHz
Info : SWD DPIDR 0x0bc11477
Info : at91samd21g18.cpu: hardware has 4 breakpoints, 2 watchpoints
Info : starting gdb server for at91samd21g18.cpu on 3333
Info : Listening on port 3333 for gdb connections
target halted due to debug-request, current mode: Thread
xPSR: 0xf0000000 pc: 0x213c0a0a msp: 0x0a0a0a08
Info : SAMD MCU: SAMD21G18A (256KB Flash, 32KB RAM)
Error: at91samd21g18.cpu – clearing lockup after double fault
target halted due to debug-request, current mode: Handler HardFault
xPSR: 0xf1000003 pc: 0xfffffffe msp: 0x0a0a09e8
shutdown command invoked

They both lock up after double fault. If I request debug, it goes on and on.

Thanks much for the instructions! This got me back up and running.

I want to add to this thread:

You don’t need to solder a permanent connection to the SWDCLK and SWDIO pads. The 4 pads (SWDIO, SWDCLK, RST and GND) are exactly 2.54 mm (1/10 inch) apart. I zip tied 4 male jumpers together and held them carefully on the board. It worked great, three times in a row.

continued…

Here is programming the board:

Finally, did this on a pi instead of windows and finally got it unbricked. I used the seeduino breakout board to connect it to the pi and it went well. However, on the pi, do not put the bootloader file in quotes. There is an update bootloader file now, but it did not seem to work on my seeduino.
Also had to use these commands:
adapter srst delay 400
adapter srst pulse_width 100

Thank you guys!

1 Like

So I’m not sure if this will work for your cases, as my case involved faulty user code disabling the bootloader from being able to accept sketches (which is a fault of the bootloader for sure).

Regardless, just in case any future person happens to have the same situation happen and my case was applicable to theirs, I want them to see what I did to fix the issue without any of this type of setup necessary. Just a usb cable, and a pc.

1 Like

Thank you for this article. I managed to unbrick my dead XIAO using the following steps:

Raspberry Pi:

$ uname -a
Linux raspberrypi 5.10.103-v7+ #1529 SMP Tue Mar 8 12:21:37 GMT 2022 armv7l GNU/Linux

Note that the openocd version that comes with this version of OS did not work due to tcl errors like these:

$ openocd -f openocd.cfg
Open On-Chip Debugger 0.10.0+dev-00114-g41bcbc67d-dirty (2021-01-18-16:43)
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.org/doc/doxygen/bugs.html
/usr/bin/../share/openocd/scripts/target/swj-dp.tcl:30: Error: Unknown param: 4, try one of: -irlen, -irmask, -ircapture, -enable, -disable, -expected-id, -ignore-version, -dp-id, or -instance-id
in procedure 'script' 
at file "embedded:startup.tcl", line 26
at file "openocd.cfg", line 5
in procedure 'swj_newdap' called at file "/usr/share/openocd/scripts/target/at91samdXX.cfg", line 36
at file "/usr/bin/../share/openocd/scripts/target/swj-dp.tcl", line 30

So I first had to uninstall openocd and then compile it on my Raspberry Pi:

Update and install needed tools:

$ sudo apt-get update 
$ sudo apt-get install git autoconf libtool make pkg-config libusb-1.0-0 libusb-1.0-0-dev htop

Download OpenOCD from http://openocd.zylin.com/#/admin/projects/openocd:

$ git clone http://openocd.zylin.com/openocd

Compile:

$ cd openocd-code 
$ ./bootstrap 
$ ./configure --enable-sysfsgpio --enable-bcm2835gpio 
$ make 
$ sudo make install

openocd version:

$ openocd -v
Open On-Chip Debugger 0.11.0+dev-00656-g66335683f (2022-05-02-13:20)

Now the wiring:

              GPIO on RaspBerry Pi

               R     I G C
               S     O N L  
               T       D K
    ----------------------------------------
   | o o o o o o o o o o o o o o o o o o o o |
   | o o o o o o o o o o o o o o o o o o o o |
    ----------------------------------------


               XIAO
     _____________________ 
    |                     |
    |                     |
   _|_  IO  RST           |
  | | |  o   o            |
  | | |                   |
  |_|_|  o   o            |
    |   CLK GND           |
    |                     |
    |_____________________|

Content of openocd.cfg:

source /path/to/interface/raspberrypi2-native.cfg
transport select swd
set CHIPNAME at91samd21g18
source /path/to/target/at91samdXX.cfg
#did not yet manage to make a working setup using srst
#reset_config srst_only
reset_config srst_nogate
adapter srst delay 400
adapter srst pulse_width 100
bcm2835gpio swd_nums 25 24
bcm2835gpio trst_num 7
bcm2835gpio srst_num 18
#bcm2835gpio_peripheral_base 0xFE000000
#bcm2835gpio_speed_coeffs 236181 60
init
targets
reset halt
at91samd chip-erase
at91samd bootloader 0
flash write_image bootloader-XIAO_m0-v3.7.0-33-g90ff611-dirty.bin
#at91samd bootloader 8192
reset
shutdown

Now run:

openocd -f openocd.cfg

Hope this helps …

2 Likes

For those who don’t have a rpi on hand like me, I was able to recover my bricked Xiao with an ESP32 and Adafruit’s DAP library. Can detail the process if anyone’s interested.