USB notes
More broadly
Connectors
See Common plugs and connectors#USB
USB versions
- USB(1) exists since 1996, 1.1 since 1998
- only type A and type B plugs were technically in the specs
- 1.5 Mbps ('Low speed') and 12 Mbps ('Full speed')
- USB2 exists since 2000-ish
- used roughly the same cabling (though specs are a little more demanding)
- also started introduced various of the mini and micro
- adds 480 Mbps ('High Speed')
- USB3 exists since 2008ish (USB3.1 since 2013, USB3.2 since 2017)
- 3.0 adds SuperSpeed (5 Gbit/s), 3.1 adds SuperSpeed+ (10 Gbit/s), 3.2 adds a flavour of SuperSpeed+ that does 20Gbit/s
- often recognizable by the blue plastic in the plugs and sockets, or SS or SS+ (for Super Speed and its Plus variant, the modes that USB3 adds)
- micro plugs/sockets are often not blue, possibly because it would be hard to see anyway(verify)), and because they're less ambiguous that PC-side A sockets
- USB4 exists since 2019
Pin-wise:
- USB1 and USB2 were four pins:
- power lines are 5V and ground
- data is 3.3V differential
- USB micro has a fifth pin
- USB-3's 9-pin versions of A, B, micro, and more
- ...leaves all those four USB2 pins there that still speak USB2, and adds five pins to do most of the USB3 on:[1]
- two differential pairs
- and a ground
- in fact, technically the USB3 part is entirely separated from USB2 part (great for backwards compatibility, though adds more footnotes about e.g. hub implementations)
(verify)
- USB-C is 24 pins, duplicated to be invertible (with some interesting details)
- so roughly 10 different pins overall (six of which are in three pairs)
- USB-C carries USB-3's functionality (including the physically separate USB-2?), but some some details differently
- even if the pins are not strictly a superset of the 9 USB-3 pins
This also means that USB-C to USB-A adapters may be able to speak superspeed (though some are only wired for USB2 so High Speed) and do basic carging -- but nothing USB-C such as power delivery or alternate modes (or anything else that relies on CC or SBU pins).
Host hardware variants
You can think of HCI (Host Controller Interface) as a API of how an OS will communicate with a USB host device. Hardware and OSes think about backwards compatibility so this is mostly invisible to users.
HCI variants are not unique to a USB version, but each variant can be used to carry a set of USB specs, so it roughly seems that way.
Perhaps the USB2 situation helps illustrate this best: EHCI specced only USB2 high-speed, so hardware that supported USB2 and USB1 (basically everything) would actually present two controllers, and the OS spoke EHCI to one and OHCI/UHCI to the other.(verify)
xHCI seems designed to avoid this, and presents USB3, USB2, USB1 (and in future versions USB4) to the OS in a single interface.
- it is relatively rate for e.g. motherboards might present its USB ports in both xHCI and older things, though it has some value for legacy support, especially early days
PC controllers will be one of
- OHCI (Open Host Controller Interface)
- USB 1
- Open standard
- UHCI (Universal Host Controller Interface)
- USB 1
- Proprietary standard
- Intel and VIA are usually UHCI
- EHCI (Enhanced Host Controller Interface)
- USB 2
- public (open?) standard
- xHCI (eXtensible Host Controller Interface)
- USB 3
- in theory replaces OHCI, UHCI, and EHCI
See also:
Speeds
- USB1 "Low speed"
- ~178KByte/s (1.5 Mbit/s) of wire speed, max max ~120KByte/s of carried data due to 8b/10b coding (not yet accounting for protocol overhead)
- Meant for devices that will never need much speed, such as keyboards
- USB1 "Full speed"
- 1.5MByte/s (12 Mbit/s) wire speed, ~1.2MByte/s of carried data
- USB2 "High speed"
- 60MByte/s (=480Mbit/s) wire speed, ~48MByte/s carried data
- expect no more than 30MByte/s, and 25MB/s is more common (apparently because of offloading(verify))
- USB3 "Superspeed+"
- 625MByte/s (5Gbps) of wire speed, ~500MByte/s (4Gbps) carried data
- Note: May do USB2 devices a little faster (as in, may reach 30-40MByte/s where many USB2 controller may stop at ~25MByte/s) (verify)
- expect no more than ~400MB/s usually
- USB3.1 "Superspeed+"
- 1250MByte/s (10Gbps) of wire speed, before 128b/132b coding, ~1200MByte/s (~9Gbps) carried
- expect no more than ~900MByte/s usually
Notes:
- Hubs share speed.
- PC USB ports often come as two ports side by side on a hub, so share their speed
- This means e.g. copying between two adjacent ports may happen at half the speed you expected.
- (at least up to USB2 - USB3 became smarter about a few cases(verify))
- USB3 modes are full-duplex, everything before was half-duplex
- I'm ignoring USB4 for now
Spec stuff
https://www.beyondlogic.org/usbnutshell/usb1.shtml
Everyday USB
Maximum cable length
Unplugging USB
"Do you need to 'Safely Remove Hardware'?"
tl;dr:
- depends on the driver
- depends on the device
- often it's fine, though it's not a guarantee, and a few specific things can cause issues
- if it involves storage, you may break off in the middle of a write.
For the USB bus itself, it's fine.
USB was made to be hot-pluggable.
Nothing will spark, and the host will almost certainly figure out a device left.
That says nothing about the function.
- If the driver was not considering the possibility of a device leaving, that may cause trouble.
- this is rare; it's part of the USB SDK to handle this
- If an application was not considering the possibility of a device leaving, that may cause trouble.
- many APIs (video, audio, whatnot) also include devices leaving. It's something you ought to handle, but some programs do not.
The real question is what the thing on top is doing.
- If this is a keyboard, mouse, webcam, MIDI interface, the data isn't as important as the fact that the device is no longer there to be used
- If the thing the device was doing involved storage, there is a chance you broke off in the middle of writing
- and that may leave that storage in a bad state
- (with some minor footnotes, such as that standard USB storage / video / HID class are more of a know quantity than drivers for custom hardware that to you has the same function)
For storage
For a sound card, webcam, keyboard or mouse or such, you won't care to completely lose the packets it's currently transferring.
For storage, on the other hand, you usually will - anything from a USB stick, memory card, external hard drive, connected cameras in storage mode.
Disconnecting a drive while running originally previously wasn't a thing, so filesystem APIs assume that a file stays open and available until the program that opened it says it is done.
Yanking while reading from a external storage will almost certainly make the reading program unhappy,
yet the storage itself is typically fine.
Yanking while writing is usually bad.
- Most filesystems write file data in-place (meaning they do not implement a guaranteed recovery)
- A number of filesystems are a little more robust about filesystem metadata - but you shouldn't assume it
There is a common optimization in "keep the last seconds worth of data before actually writing it" (see writeback versus writethrough).
This optimization is usually disabled for USB-connected storage - except when it's not.
Also, certain kinds of storage (non-ssd flash) erases a block before rewriting it,
so there is a time at which only the host knows the data.
So you can expect USB flash sticks to corrupts a little more easily while writing (SSDs, while based on Flash as well, have a more complex, smarter controller that avoids at part of that issue and should be more recoverable).
USB Device Not Recognized / Device has malfunctioned
Shown in a balloon near the task bar, with the addition that "One of the USB devices attached to this computer has malfunctioned, and windows does not recognize it."
(In some cases, if the balloon message is disabled, the device only shows up as an "Unknown Device" in the device manager.)(verify)
This is a generic error, there are many possible causes, and it's often hard to diagnose what will help most, or even whether it is solvable.
Possible problems/solutions
Things you can check easily enough:
- broken cable (cables get stepped, yanked, chewed, plug got bent, plug isn't held in in anymore, etc.)
- if it's interchangeable, try another one (maybe try this first, in that it is the easiest fix if this is the cause)
- If it's not interchangeable, try wiggling it to see if it accidentally makes a good contact.
- It's not a very good test in that it not working means nothing, but if it turns up temporarily, you know that a bit of DIYing may well fix the problem.
- broken device
- test: try in other computers - and to be sure, also with varying OSes (some devices don't have drivers for older/specific OSes)
- broken PC-side socket/controller
- Test: Try other things in the same port, and try this device in other ports
- Note: each pair of ports is usually the same internal hub, so if trying multiple, try ones at some distance rather than the one right next to it
- Basic mice may be the most useful test devices, as they are low-power and do not require drivers to work
- USB port in a weird state (e.g. the previous device drew too much power and triggered the polyfuse. But there is another reason or two)
- (some are more sensitive/careful than others. Macbook's protection was apparently known for triggering very quickly)
- Test/fix: Turn computer off completely, and then leave the power cord/battery out for a few minutes (that much is rarely necessary, but sometimes it is and it takes less time to wait a bit longer once than to reboot again)
- USB knows you are crossing the limit of power it can give out, and refuses the next device
- more likely to happen on a non-powered hub
- Test: try plugging the device into the computer itself (or into a powered hub that has no high-powered devices)
- Front ports may sometimes be non-powered hubs, so a back (motherboard) port would be a better test. On laptops there may be less difference, but try multiple.
- Test: try a mouse in the same port - these are typically low-power and so very likely to work if the port's not broken.
More work and/or less likely to help:
- bad or missing driver for the particular device
- Test: see if it works anywhere ( different PC, different OS), try with a different driver (but forcing this can be difficult)
- annoying to text exhaustively, though
- some devices don't like sleep/hibernate, regardless of version of anything
- and may stay confused until a reboot
- Some battery-powered devices draw most power from the battery, not the USB connection, and for such devices the problem may simply be that such a device does not have enough charge to respond (or it has decided it won't try because power levels are so low).
- It seems that Windows's collection of drivers for USB devices (one or more) can get wonky.
- A lot of work for usually no result, and it is hard to tell whether you've done this properly
- This tends to be more of a last-resort thing you only want to do if you have indication it's not a hardware thing, but
- it may be that reinstalling all of these can help.
- Test/fix: Uninstall as many related drivers as you can before plugging the devices back in.
- Remove controllers drives last, so peripherals drivers first, then reboot and let it re-detect and re-install everything.
See also
- http://dan.hersam.com/2005/07/18/usb-device-not-recognized/
- http://support.microsoft.com/default.aspx?scid=kb;en-us;817900
- http://support.microsoft.com/?kbid=873169
Why do some devices only work on PC ports, not from hubs?
Notes on slow USB3
PTP, MTP, and more
USB OTG
USB power and charging
It seems like USB only accidentally became a standard for charging things, which is why the history of transferring more power through it is interesting.
USB3 and USB-C as a plug made things more capable -- but also more negotiated and more varied,
so harder to understand and to explain.
Attempt at shortish answer
Power and charging before USB-C
- PC ports :
- USB1 or USB2 root hubs (≈the port inside a PC/laptop)
- are more or less guaranteed to give 100mA without ever running into trouble
- negotiation may allow (5*100mA=)500mA, which at 5V is (0.5A*5V=)2.5W.
- USB3 root are similar but slightly higher:
- guarantees 150mA
- negotiation may allow (6*150mA=)900mA after negotiation, which at 5V is (0.9A*5V=)4.5W.
- external hubs on PC
- if they have their own power supply, they may act much like described above
- if not, they may not allow more than 100mA
- (seems to be part of why they often only have 3 or 4 ports(verify) -- one layer of hub could ask for 500mA and provide into four 100mA devices)
- generic USB-A wallplug chargers
- when a device detects that the 'host' side doesn't present as a host at all, it is up to devices to behave reasonably
- before PD and BC stuff, we relied on conventions, which were messy and not very predictable
- but also, most chargers can easily provide 0.5A so most devices would often assume that. Devices tended to draw 500mA, though 100mA would be safer.
- USB Battery Charging (BC) added a way to signal 1.5A
- there were some variants that fake BC (it's fairly simple), leading some devices to draw up to 1.5A (often fine even if not to standards/spec)
- ...so you can probably assume this is at least (5V*0.5A=)5W, and probably at most (5V*1.5A=)15W
- device-specific specific USB-A chargers
- ...could add something proprietary on top of the generic behaviour, that just their own chargers+chargees would understand
- some variants negotiating for a higher voltage as well.
- they may draw up to 1A or 2A, at 5V or sometimes higher.
- The classic range was often 5W to at most 20W
- and rarely more, in part because the spec for USB2 cables meant most cables couldn't safely safely support a lot more. Technically, the spec means you can't assume the cheapest wire isn't thicker than 28AWG which, even if you don't care so much about USB specs, would probably only be carrying something in the 8..12W range, and 20W is really pushing it.
- there were various ways of doing this, most of them not standardized
- though none of them would try to engage without being signaled (via voltage dividers on the data line, not not Battery Charging or Power Delivery, they came later)
- Names seem to include
- QuickCharge, QC (from Quallcomm)
- Adaptive Fast Charging, AFC (from Samsung) (apparently based on Qualcomm Quick Charge?)
- FastCharge Procotol, FCP (from Huawei)
- SuperCharge, SCP (from Huawei), apparently a successor to FastCharge Procotol, FCP
- Pump Express, PE (from Mediatek) (sometimes abbreviated MTK(verify))
- TurboPower (from Motorola)
- VOOC (from OPPO), a.k.a. Dash Charge
- PowerIQ (from Anker)
- AiPower (from Aukey)
- VoltIQ (from TronSmart)
- iSmart (from RavPower)
- ...and more variations as well as rebrandings.
- introduced later (since 2007?), at least after many not-so-standards mentioned above were introduced
- up to 1.5A (which is 7.5W), but devices may still play it safe and don't try more than 0.5A (2.5W).
- BC seems to have no specific logo, so it either has a longer label, or you have to know(verify)
- also less flexible, e.g. doesn't touch on allowed voltage drop, whereas PD seems to require charged devices to monitor VBus(verify)
- USB Power Delivery (since 2012, though frankly the 2014 version (2.0) for USB-C seems like the first interesting one)
- introduced more fault tolerance (but also a lot more footnotes, not least of which the eMarker stuff)
- most USB-C cables should be able to carry 3A, allowing (20V*3A=)60W via PD
- ...and more in specific cases, currently mostly up to (20V*5A=)100W
Power and charging with USB-C
- Without PD, assume you are limited to USB2 and USB3 limits (500mA; 900mA or 1,500mA for dual-lane) (verify)
- ...at 5V, that may mean any of 2.5W, 4.5W, 7.5W
- USB-C with Power Delivery
- and standard cable, it probably won't try for more than 3A at 20V (for 60W)
- if it can tell there is a heavier cable in place, negotiation may go up to 5A at 20V for 100W (though a lot of devices won't ask for that much)
- ..but there may be practical reasons any one case may not go that high - such as that most devices don't need that, or don't request it
- Some of those are now enough to meet the basic power draw of a laptop, meaning that laptops can now choose to use one of their USB-C plugs as a power plug
- at the same time not all chargers or cables will be equal here (and it's possibly there are preferred ports to do this on(verify))
- various smarter things may choose to charge slower
- (e.g. when it notices the adapter or cable doesn't seem to manage)
- ...so it can be hard to estimate how much faster something will charge, particularly for smarter devices
PPS
Using USB to charge batteries
In general, don't count on classical USB supplying more than 2.5W (standard)
...or 5W or 10W if slightly special (not so standard).
Since phones may have batteries on the order of 10Wh, that means charge will rarely take less than 4 hours - and easily double that when some part of the system is being careful
Since laptops have batteries on the order of ~100Wh, that means a charge would take dozens of hours -- which is why we, before USB-C, we didn't try to use USB to charge them
PC port limits
It's often mentioned that USB ports have a polyfuse - a physical component specced so that drawing more than e.g. 500mA will lead to that power being disconnected.
(There are other solutions. A few implementations actually monitor current, and use transistors to disconnect(verify), and in some rare cases the protection is missing)
Implementations play it a little wider than that, to avoid disconnecting devices due to transients and slight deviation -- because polyfuses may, by nature, not reset until power is removed, which is why you sometimes need to completely power off a computer (and sometimes even take the laptop's battery out) to get that port working again.
So USB2 polyfuses seem to have a trip current around 1.5A, and also a higher-than-500mA hold current (I've seen 750mA mentioned)
Roughly twice that for regular USB3 ports (ports can supply 0.9A as per specs)
More so for charging ports (how much?(verify)).
DIYers will know from trial and error that you can get away with just ignoring the intended negotiation and drawing 0.5A from a USB2 PC port without requesting it (0.9A from USB3 as per specs)
...because most hosts/hubs just assume USB devices act according to specs, they assume you would always ask not just draw it. And if you ever made your project into a product, it would need to actually request it to pass USB certification.
Note also that since you are defeating the thing that lets USB guarantee power to its devices, you may be able to cause all USB devices that sharing that power to brown out, something normally prevented by in-spec USB (at least in theory).
Hub limits
USB-C won't charge
USB-C to USB-C doesn't charge
USB-A to USB-C doesn't charge
USB-C and DIY
Why do some cables charge faster than others?
Lower level USB
Transfer types
On bandwidth and latency
tl;dr
On transaction translators (USB1 speeds inn USB2)
"Does a similar thing happen with USB3 hubs having to deal with USB2?"
Other notes to bandwidth and latency
Can we push USB latency lower?
"But I've seen figures in microseconds"
Isn't USB3 a lot better?
On poll rate
The OS side
More on scheduling
"Could we synchronize clocks across USB??"
"Which microcontrollers can I get to poll at 1000hz?"
Considerations of making a very fast responding device
Alternate modes
"Hey I have this USB-C thing that does DisplayPort - does it carry video from the laptop, or does it contain a video card?"
USB sniffing
Related to programming
A primer, some terminology
On classes, and default class drivers
on libusb and platforms
HID
Keyboard limitations
Can I write and use my own USB drivers for a device?
Can I give my DIY device a custom name?
tl;dr:
- not really, at least not easily and probably not without paying (with a few informal exceptions)
- To get it to look distinct, you would really need a distinct VID-and-PID, and registration of such.
This would mostly just matter to what is shown in device manager.
This might be nice for the convenience of admins.
Or, in theory, to avoid getting confused which of the many attached Arduinos you need to upload to, except that the name for a device is essentially settled by its VID and PID, so that won't work.
A thing that does a thing - that you probably don't want
In Windows there is a registry trick to effectively override the name reported by a driver.
But that would be for all devices that that driver applies to, so won't help you e.g. distinguish one arduino from another, nor will it make your project look distinct on any other PC.
(you don't need to alter the driver itself, just the INF around it[2] which is within reach. Also, it will not do that on anyone else's PC.)
If you are a company
To start, yes,
- you can get a lot of USB-capable microcontrollers to communicate arbitrary VID and PID,
- you can register it somewhere (without paying a lot of money)
This still is only half the solution, because it is now a new, unknown device, and now you would have to supply your own drivers.
Especially in Windows, if you want automatic install, you have to deal with Microsoft driver certification[3].
This is probably only interesting for people who own a company and want to sell the result.
(Apple has similar checks, though there are a little lighter)
There's an inbetween if you can get people to accept your own custom drivers, but it is fidgety.
I have seen installers do this to their devices.