Álvaro Prieto Profile picture
Feb 21, 2022 16 tweets 7 min read Twitter logo Read on Twitter
Ever wanted to debug your microcontroller over USB-C?
(I'm talking JTAG/SWD, not just USB!) If you already have a USB-C connector, there's no need to have an additional connector to program (or tag-connect pads, which are super nice)

Here's one way to do it: 🧵 Image
The USB-C specification(usb.org/sites/default/…) has a section B - Debug Accessory Mode. It explains how to "support" DAM. In short, if both CC1 and CC2 are pulled up (usually, only a single one is) a device can enter DAM. While in DAM, the following signals can be repurposed: Image
Normally, CC1/CC2 are used to determine orientation as well as power delivery information. For this thread, I'll just focus on "dumb" PD with termination resistors. Depending on the source's pull up resistor (Rp) value, the source can advertise various charge currents. Image
USB-C cables only pass through CC1. In order to use DAM, we need to have a USB-C plug (or custom cable) to connect both CC1 and CC2. If the device is in DAM, how do you know the charge current? There's another table for that :D Image
DAM doesn't specify any standard pinout. It's up to the designer to figure that out. Here's what I came up with:

RX2- - SWO
SBU1 - VCC(target)

USB D+/D- stay connected the usual way.
This pinout means that the connector is not reversible. Using the TX and RX lines in a particular way would allow for that, but I chose not to do that (I'll explain why later.)
Technically speaking, a device should not enter DAM unless CC1/CC2 are both pulled up.
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. Image
The comparators are needed because the CCx pull-up might be as high as 56kOhms, which results in a CC voltage of ~0.4V, which is likely below the AND gate's threshold. A simple resistor divider is used to set the comparator threshold level (I set it ~0.2V)
In order to test all this, I first used a USB-C plug breakout board with a few wires 😬
On the other side, I hooked up the circuit mentioned above. (I can't show the main circuit, unfortunately).
The setup worked though! Time to make some custom boards :D ImageImage
I decided to make a board that passes through power and USB, but hijacks the CC1/CC2 lines to enable DAM. I used an attiny24a(in stock!) to read in the input CCx lines to determine the input charge currents and set the appropriate downstream CC1/CC2. I used the wrong footprint 🤦‍♂️ ImageImage
I hard-wired the CC resistors and it worked :D
Here's the fixed version. It uses the SBU1/SBU2 signals (VCC/GND) to light up a green or red LED to let you know if it's plugged in wrong. It has standard SWD pins for debugger. The only problem is... I have to program the attiny24! ImageImage
So I made another programmer over USB-C 😂
This one uses SPI instead of SWD. I also got the MISO/MOSI lines flipped, so had to use some jumper wires to program. ImageImage
I decided to make a test target with all these features. It's just an STM32L4 😎 with USB and a couple of LEDS (along with all the CCx detection fun). You can see how the LED is red when it's plugged in backwards.
After much pain soldering on those USB-C connectors, it all works! ImageImageImageImage
Ok, so back to why I didn't want to make the pinout reversible. Following the USB spec is nice and all, but for hobby projects... 🤷
What if we ignore all of the CCx detection stuff and just permanently wire the SWD lines to the USB port? Saves us several components and space!
If we'd used the TX1/2 lines of the USB-C port, the host would send some test pulses on connection to try and negotiate USB3 (or something else). Since we only use the RX lines, there's a low chance of the host signals causing unintentional trouble over SWD!
If we chose to go this direction, the footprint/component requirements to do this are zero! You'll still need an adapter board to connect your debugger, but that's it! (Maaaaybe I'll start selling these soon if there's any interest)

Hope this was useful! :D

• • •

Missing some Tweet in this thread? You can try to force a refresh

Keep Current with Álvaro Prieto

Álvaro Prieto Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!


Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @alvaroprieto

Nov 19, 2022
Ever wonder if your USB C cable is USB2 or USB3? Do you have some charge-only cables hiding in a box just waiting for the right moment to spoil your project plans?!
Fear no more!

I’ve made a simple board to test most of the things!
Connect your cable to both sides and the signal LEDs will light up if they are connected. Here’s an example of a USB2 cable and a thunderbolt 3 cable.
Want to test a USBA to C cable? How about a USBA to MicroB? USBC to B? MiniB? You’ve got it!
Read 8 tweets
Jun 19, 2022
Wanted to make your own game controller that works on iOS? You probably ran into issues since Apple requires them to be MFi certified. There is a way though! No jailbreak required ;)

Here it is running on my iPad with IPadOS 15.5. I’m using a Teensy LC for it.
How does it work?
I read somewhere that they’ve added DualShock 4 controller support (from the PS4). I assumed that Sony did not include an MFi chip internally, so they must have an exception for it! Here it is working over USB
This even works on an iPhone with the lightning camera adapter! I needed to have a charger plugged in, otherwise it says the accessory uses too much power 😅
Read 8 tweets
Mar 17, 2020
A few days ago, I found my dad’s college Digital Systems project notes. The task was to build a clock with 7400 series logic.
I decided to try and re-create the project with components we bought together many years ago.
I’ll do a quick thread explaining how it all works
The project starts with a 555 timer to generate a 10kHz clock source using two resistors and a capacitor.
🤦I should probably show the block diagram first.
In summary, we start with a 10kHz clock and divide it down by 10 until we have a 1Hz clock. From there, we have second, minute, and hour counters, display drivers, and 7-segment LED displays.
Read 18 tweets
May 30, 2018
Fridge not working.
Step 1: open fridge.
Step 2: Remove controller board.
Step 3: Solder programming header and figure out pinout.
Step 4: Dump firmware using avrdude and buspirate
Step 5: Start questioning myself
Ste.... ImageImageImageImage
They seem to have used the most brittle plastic possible. Tried re-attaching the board and the posts broke off.
Had to improvise with zip ties until I can come up with a better solution ImageImageImageImage
The temperature sensors seem to be ok. The error only shows up when I plug in the fans... Fans seem to be running ok, but maybe causing some other electrical issue.
Error seems to be E-1 though, which indicates temperature sensor issue :-/ Image
Read 14 tweets

Did Thread Reader help you today?

Support us! We are indie developers!

This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!


0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy


3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!