Electrical and firmware engineer Álvaro Prieto has penned a guide to getting Serial Wire Debug (SWD) working for debugging your microcontroller projects — using an existing USB Type-C port, no additional connectors required.
“Have you ever wanted to debug your microcontroller over USB [Type]-C? (I’m talking JTAG/SWD, not just USB!) If you already have a USB-C connector,” Prieto explains, “there’s no need to have an additional connector to program! The main reason I started this project was due to a previous job. Our device had to be weather sealed and had a single USB connector for charging and firmware updates. As the lead firmware engineer, I wanted to be able to do ‘proper’ debugging while developing firmware using SWD, and not have to be constantly tearing open devices to connect the debugger. Since we were about to do a new design iteration, I started investigating using SWD over USB-C at home.”
Need proper debugging on a compact board design? The SWD2USBC (small board, left) provides exactly that, over an existing USB Type-C port. (📷: Álvaro Prieto)
Prieto’s space-and-parts-saving debugging trick relies on the fact that modern USB ports are a lot more than USB ports: a USB Type-C connector can support DisplayPort traffic, USB Power Delivery (PD), and a feature that sits at the heart of Prieto’s guide: Debug Accessory Mode (DAM). “In short,” he explains, “if both CC1 and CC2 lines are pulled up a device can enter DAM.
“In order to do this ‘properly,’ we can use a two comparators (op-amps in my case), an AND gate, and a switch (TMUX1511, in this case). The comparators detect when both CC1/CC2 are pulled up. If both are, the AND gate enables the switches to connect all the signals. I decided to make a board that passes through power and USB, but hijacks the CC1/CC2 lines to enable DAM. I used a [Microchip] ATtiny24a to read in the input CCx lines to determine the input charge currents and set the appropriate downstream CC1/CC2 resistors.”
The original prototypes were built to solve a real-world problem in debugging a commercial board design. (📷: Álvaro Prieto)
Prieto’s boards — programmed, amusingly, using another board that provides SPI rather than SWD over its USB connection — proved entirely successful, allowing Prieto and colleagues to debug their boards without having to find space in the design for additional dedicated connectors.
Prieto’s full write-up, which he says “started as a Twitter thread years ago,” is available on his website; board design files and firmware for both the ATtiny-powered SWD2USBC board and Raspberry Pi RP2040-powered sample targets are available on GitHub under the permissive Creative Commons Zero public domain license.