SWD Programming stick

2024/05/21

Okay, use your imagination and picture yourself the following:

You made some electronics for a project and have transcended beyond the realm of development boards like Arduino and co, and have built yourself your very own custom PCB with a bare microcontroller (MCU) in it. It has all the best stuff on it. A nice purple solder mask, silk screen that seriously impresses all tattoo artists in a 5km radius and of course the choicest components available (yes, that is a word!).

The Software

Cool, you got your PCB, now it is time for the software. There are two options here:

  1. You program it in C using whatever toolkit is available for your MCU and bang your head against the wall because all documentation sucks.

Or, if you are a bit like me, you can:

  1. Program it in Rust and bang your head at several walls at the same time because the borrow checker is giving you shit, the person that wrote the HAL failed to understand that yes, UART transmissions can have 9 data bits and no, those do not fit in an 8 bit value !!! AND the documentation STILL sucks.

Yikes. Programming is fun. Anyway. Once properly compiled your toolchain wil spit out a hex file. This file contains the program memory that needs to be transferred to your MCU.

But how?

You might have noticed the lack of any USB port on your design (if you do have one, wow look at mr. fancypants dishing out the extra euros for their full-speed USB peripheral. Remember: it is illegal to bitbang USB! NO!!). No USB means you can’t upload your hex file to it like you can with an Arduino. Bummer.

The programmer

Your MCU has a specialized mechanism for reading and writing the program memory content. If it doesn’t, well then you need to seriously rethink your life choices and wonder how you’ve come to this exact scenario. This mechanism is usually some kind of serial interface. Cue the programmer: This little device interfaces between this serial interface and your PC. The PC sends a hex file to the programmer, which then does a funny dance and a somersault to switch your MCU to programming mode and let’r rip!

Exceptions

Alright clever guy, you don’t always need a separate programmer. You can have a programmer onboard, or maybe a fancy bootloader that performs a self-programming.

My problem

I have issues. I mean, the issues I want to talk about is the ARM microcontroller. They use the Serial Wire Debug protocol (SWD) to program and debug the chip. There are programmers available that talk this protocol but they are too expensive. What I used to do is use my Raspberry Pi with OpenOCD and let it bitbang SWD. This works perfectly fine but it’s finicky to setup with all the GPIO wiring and I’d like to use my Pi for something else.

Artist’s impression of a Pi used as an SWD programmer

While browsing the internet for things, i came across this neat thing: SWD Programmer Stick by Stefan Wagner. Perfect! Now I just need to have the PCB manufactured, order the parts and solder them!

Except alas.. This was after the 2020-2023 chip shortage.. That specific model SAMD is not available anymore :( I need something else. After thinking for a bit I found out that the software hex file is 1-to-1 compatible with a different footprint of the same chip (which is available!) I knew what I needed to do: Build my own design.

>> Home