Ticker

6/recent/ticker-posts

Ad Code

Responsive Advertisement

USB-C: Introduction For Hackers

We’ve now had at least five years of USB-C ports in our devices. It’s a standard that many manufacturers and hackers can get behind. Initially, there was plenty of confusion about what we’d actually encounter out there, and manufacturer-induced aberrations have put some people off. However, USB-C is here to stay, and I’d like to show you how USB-C actually gets used out there, what you can expect out of it as a power user, and what you can get out of it as a hobbyist.

Modern devices have a set of common needs – they need a power input, or a power output, sometimes both, typically a USB2 connection, and often some higher-speed connectivity like a display output/input or USB 3. USB-C is an interface that aims to be able to take care of all of those. Everything aforementioned is optional, which is a blessing and a curse, but you can quickly learn to distinguish what to expect out of a device based on how it looks; if ever in doubt, I’d like to show you how to check.

Communicating Capabilities, Configurations and Caveats

We all know that USB-C can be rotated – it lets you insert the cable whichever way around — which was a significant improvement over USB-B. Let’s get down to how this works. To make this possible, there’s a CC (Configuration Channel) connection – a single-wire line in every USB-C cable that attaches to one of the two CC pins in the Type-C connector, and it is essential to making USB-C work. For simple USB-C usecases, like “get USB 2.0 and 5 V out of a port, follow a simple recipe – attach a 5.1 kΩ pulldown to each CC pin, and you will have a USB-C port that will work with all reasonable devices out there.

There’s support for stuff beyond 5 V and USB 2.0 in USB-C, of course. You can get a variety of voltages out of a USB-C port, which is quite handy for charging things like laptops. You can get USB 3, DisplayPort, and Thunderbolt. Most laptops will let you connect a docking station taking advantage of USB-C as much as possible, giving you a high-resolution display, plenty of USB ports and charging all over the same cable. Now, how does that work?

For usecases like voltages larger than 5 V (USB-PD) or high-speed connectivity beyond USB 2 and 3 (the “altmodes”, for Alternate Mode) you have to invoke them through the same CC connection. You have a single-wire for two-way communications, which makes the CC wire a half-duplex channel; it’s a fixed-rate 300 kbps Ethernet-like protocol. There’s plenty of ICs that talk this protocol to implement a set of defined features, and quite a few ICs and microcontroller peripherals will help you say anything you want over the protocol.

With Great Power…

USB-C ports have pins for four high-speed differential pairs, eight pins in total. Many simpler USB-C devices have them disconnected, but there’s a lot of power in these ports. For a start, you can usually get USB 3.1 or 3.2 out of them when you see such a port on a laptop. A USB3 port usually takes up two differential pairs out of four, but some devices support 2 x 2 USB3 links, doubling the amount of pairs and the transfer speed. More and more often, you can get DisplayPort out of your USB-C ports, too – with either two or four lanes, you can drive some pretty-high-resolution displays.

Then, there’s Thunderbolt, a half-proprietary technology that also uses the high-speed pairs on a USB-C connector. It can tunnel USB3, DisplayPort and even PCIe within itself, though it’s none of these three things. With Thunderbolt, you can have a docking station but on steroids, with more DisplayPort options, better and faster ports, or perhaps even an external GPU! You might guess that it’s the rarest and most expensive option out of all of these.

Given these high-speed and high-power capabilities, the unification of everything into one connector has strong upsides. Your USB-C laptop charger can also charge your phone in times of need, and thanks to aggressive standardization, there’s not much proprietary stuff involved in charging anymore.

If you have a USB-C Nintendo Switch dock, it will also theoretically act as a dock for your laptop, assuming you can mechanically make it connect, and laptop docks will work with your Steam Deck. In general, docking stations are becoming reasonable – you use a single cable for everything, it works with most USB-C devices out there, and that’s that.

Each year, there’s less and less that USB-C cannot do. Last year, they announced EPR, which raises USB-C port capability up to 240 W, at 48 V @ 5 A – as opposed to SPR, where a USB-C port would be limited to 100 W, 20 V @ 5 A. This basically eliminates the need for barrel plug adapters on laptops, where 100 W of USB-C used to be a barrier – no longer should we need all the “universal” power supplies with an assortment of barrel jack adapters. Then, there’s USB4, an upcoming standard, which is just like Thunderbolt, but not quite, but better, but worse? Either way, we’re going to see more USB4 support in computers, and hopefully one day every laptop will be equipped with a high-speed interface, even the cheapest ones.

Overall, USB-C has a bright future, and it’s reasonably well-designed in many aspects – learning from mistakes we’ve been making throughout the decades of ports, cables and standards, and leaving enough for upcoming additions.

Of course, USB-C also created a whole new field to make mistakes in.

…Comes A Memorable Track Record

USB-C horror stories are everywhere – basically everyone who has used USB-C, can tell you about that one time when USB-C failed them, or perhaps, multiple times over. There’s more upsides to USB-C than there are downsides, but I firmly believe it’s important we remember the ways it used to fail us, and the ways it still does, so that we can learn from these ways and figure out how to avoid them.

Not all parts of the USB-C standard are equally well-thought-out. The cable and port situation is the first one to come to mind. When you see a USB-C port, you can hardly ever know at a glance what it supports, and the same goes for cables. The situation is pretty dire, as it stands – using USB-C can, at times, require plenty of guesswork for someone who hasn’t found a decent explanation yet. There are guidelines to how you can distinguish cables, and I will show some tricks along the way. That said, they should have introduced a compelling visual marking scheme from the start.

The USB-C standard is somewhat complicated to implement, with the numerous state machines and peculiarities involved. The USB-C specification is known for infamously long PDF documents: the connector and cables document is 350 pages and the USB-PD document is 600+ pages long. Many manufacturers have tried over the years, in good faith, and still created devices with noticeable and bizarre edgecases. Laptops that only work with certain chargers and vice-versa, docks that only work with certain laptops or only in combination with a certain charger, cables that work in one orientation only or devices that work differently depending on cable orientation – there’s no shortage of mystery.

In addition to that, there is an infinite amount of ways to misuse USB-C, and certain manufacturers try hard! It’s not USB-C standard’s fault that others misuse it — when defining a complex standard, you can only implement a limited amount of protections. However, we still have a whole new category of problems for us to watch out for. Some USB-C sins are hard to forgive and worth zoning in on, and some are less obvious – we’ll go through many of these along the way.

Myriad Ways To Hack On

It’s important you keep this in mind – USB-C will only become more coherent as time goes on; by force, if necessary. Likewise, it will only become more hackable. Because as over time, we will collect more and more building blocks – pieces of hardware and knowledge alike. In addition to that, the USB-C ecosystems in our homes are only growing within each day. If you’re designing something today, you should strongly consider USB-C for your usecases.

Here, let’s recap what you need to add a USB-C port to make sure your device gets 5 V with up to 3 A possible, with USB 2.0 data, and full rotation support for both.

Symbol: USB_C_Receptacle_USB2.0; footprint: USB_C_Receptacle_HRO_TYPE-C-31-M-12

That’s it. Compared to MicroUSB, it’s only two extra resistors, and the pins are easier to solder. Wire the resistors up specifically as pictured on the schematic; don’t join the CC pins like Raspberry Pi 4 did, and don’t omit the resistors either. If you omit the resistors, the upstream Type-C port will not provide 5 V to your device – a lot of cheap devices omit them. There’s no power provided without the 5.1 kΩ resistors, unless you use an USB-A to USB-C cable to power your device. If you omit one of the resistors, only one rotation of the port will work – some cheap devices only have one resistor.

Every USB-C device that wants to receive 5 V at any point has those resistors, either onboard or inside an USB-C communications IC. For a simple “5 V and USB 2.0” purpose, you can just use 1% 5.1 kΩ resistors. That said, in pinch, you can parallel two 10K resistors and it actually works. Personally, I just ordered a reel of 5.1 kΩ resistors and they’ve been of big help. If you have a port where the designer forgot to add these resistors, you can also order some FPC shims that help solder such resistors onto a popular kind of connectors!

You have plenty of USB-C connectors to pick from, and the 16-pin ones are a crowd favourite – with a ton of pin-compatible ones available for purchase anywhere, and reasonably easy to hand-solder, especially if you have solder wick to fix mistakes with. Make sure you don’t use connectors without CC pins – as you can’t connect pulldown resistors, it’s not possible to make such connectors work with actual Type-C ports, and they’ll only ever work with an USB-A to USB-C cable, which is a significant limitation.

Of course, there is way more to USB-C than just 5 V @ 3 A and USB 2.0, as I’ve mentioned, and I will show you how you can get to all the other possibilities. However, it’s important that you remember the formula shown, as it’s the one you will use the most often, and it will help you understand other formulae as well. In the meantime, you can use SBU pins to expose some debug connections likea UART, because nothing except DisplayPort uses SBU. However, it can be tricky to find plug breakouts that expose SBU in a hand solderable form-factor. And, if you ever enter the USB-C port hacker big leagues, you can actually implement debugging in a standards-compliant way, too!

You Don’t Always Have To Comply

And even full compliance won’t always keep you safe.

Compliance to USB-C standards for a hacker is a double-edged sword. If you don’t comply, you might destroy a laptop, or it might be that you will benefit hugely without dangers or downsides. Some scenarios are “outlawed” not because they’re actually dangerous, but because they are deemed confusing for an average user or won’t result in tip-top functionality, and in many cases, the worst outcome for non-compliance is that someone on the Internet might get angry at you over a nitpick.

In upcoming installments, I will discuss things that are outright terrible and should never have been made. I will also show you how to gently (or harshly) violate the standards where it could benefit you or it would outright make sense, and where it might look like you could violate the standards, but you really, really shouldn’t. Here’s a guideline – when you hear that something you want to use violates the USB-C standards, it very much matters what the consequences of that are in your specific application.

Let’s learn, propagate, and violate the USB-C standard together! See you next week.

Enregistrer un commentaire

0 Commentaires