We program and debug STM32 via USB Type-C port without violating the USB specification

Part 1. Port preparation

First, let’s look at the Type-C port to understand how we can extend SWD lines through it. The first thing that comes to mind is, of course, replacing the D+/D- lines with your own, but without a special adapter our device cannot be connected to a regular port.
I won’t delay and talk in detail about all the signal lines in USB Type-C; this topic is perfectly covered in this article, which I relied on during development.

In general, we are interested in the SBU1/SBU2 pins:

Picture from the above article

Picture from the above article

Yes, we are going to use these pins to route SWDIO and SWCLK along them. Yes, flashing and debugging will only work with a certain cable orientation.

J1 is the input port (you can also flash ST-Link itself through it), J2 is the output port, to your device on STM32

J1 is the input port (you can also flash ST-Link itself through it), J2 is the output port, to your device on STM32

Part 2. Testing the idea

To test the functionality of this idea, I assembled an adapter for J-Link. And here is an important note – not just any cable will do; usually these pins are not used in any way and are not physically connected.

Based on the above-mentioned article, I unearthed two cables – a USB Type-C – 3.5mm jack adapter from Google Pixel and a non-working USB Type-C – HDMI adapter. Both options had the necessary conductors, but it was impossible to power the VBUS line using the audio adapter, so I settled on a cable from an HDMI adapter.

And I soldered this:

I ask soldering aesthetes to step away from the screens
It works - don't touch it!

It works – don’t touch it!

The concept turned out to be 100% functional and this cable still works to this day.

Part 3. Programmer

Now that there was no longer any doubt, I designed an adapter board with Type-C input and output. I redrew the ST-Link diagram from the documentation for the Discovery boards.

Programmer board

Programmer board

Assembled and soldered ST-Link

Assembled and soldered ST-Link

Flashing a custom ST-Link firmware for the first time can take a fair bit of googling, so here’s my recipe.

Download the ST-Link bootloader Hereselect (in my case) Unprotected-2-0-Bootloader.bin. On my board version 2.1 it didn’t start, most likely the problem is in the additional USB renumberer circuit (I tried to add it with a surface-mounted installation, the red wire on the board is it, but it didn’t work out).

After flashing the bootloader, download the old version of ST-Link Utility (version 3.6 worked for me), new versions refuse to flash ST-Link.
Well, that’s all, now debugging via the USB Type-C port is available to you, while the usual capabilities of the port are completely preserved. I have a USB-UART converter hanging on this port, through which you can also flash your device via a system or custom bootloader.

Why bother?

In my case, I wanted to debug the device assembled, without soldered wires, in the case, right at the place where it was installed. And it’s very convenient! However, it should be remembered that for debugging the programmer must be inserted in a certain direction.

Incorrect connections do not lead to any unpleasant consequences on my boards, but this point should be kept in mind.

Patient under debugging

Patient under debugging

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *