Designing USB Devices for proper current and MaxPower

From DP

Jump to: navigation , search

There are some common misconceptions about USB Device design with regard to the amount of current available and the MaxPower field of the USB Descriptors. Many DIY developers simply enter the maximum 500 mA value without consideration for the actual rules and ramifications. Some may think they're playing conservative by entering 100 mA instead, without realizing that they may still be in violation. This topic is intended to illustrate the situation as quickly as possible and open up a discussion if necessary.

Increasing the advertised current value in the USB Descriptors will not give you more current. The USB Device electronics are actually mostly in control of their current draw. The USB Descriptors are just there to warn the USB host computer about what to expect before your device is fully enabled beyond the default 100 mA. The OS version and USB driver versions, as well as any other USB devices plugged into the same port (or internal to the host computer) play their part in the overall current negotiations.

USB is not designed to limit current based upon device request. What actually happens is that a USB host is supposed to have a driver that is aware of the total current capabilities of each port. As each device is plugged in, the USB host driver is supposed to add the requested current to the total. If the requested current goes over the total limit, then the USB host will simply refuse to enable your USB device configuration.

To fully understand USB current limitations, things can easily get very complicated. There are two basic categories, the second of which has a few subcategories.

0) All USB devices (except the new category of 2.5A chargers) are limited to 500 mA. It's not hard to design a USB device that pulls more than 500 mA, and many Apple desktops will gladly supply over 800 mA. It's your responsibility to make sure your circuit does not exceed the limit. Some USB hosts will have current limiters on their ports, but those should not be activated when using properly-designed USB devices. Don't rely on protection: design your circuits to do the proper thing at all times.

1) The first category is a USB device which draws 100 mA or less. This sort of device needs no switching because every device is allowed 100 mA as soon as it is plugged in. There used to be a rule that when the USB device was put to sleep, it would have to reduce its current below 100 mA, but so many devices failed to implement this correctly that USB 2 relaxed the rules significantly. Unfortunately, this probably means that your laptop battery drains faster when its sleeping if you leave such USB device plugged in.

A very important consideration here is that nothing physically enforces this 100 mA limit - certainly not the USB host hardware. The number one thing to remember is that this 100 mA limit is a rule that you must design into your circuit. If you create a circuit that draws more than 100 mA under any conditions, then your device fails to meet USB specifications. All it takes is a resistor of the right value range across the USB 5V supply and you could be in violation without ever realizing it. It's actually rather tough in the DIY world to confirm that you're at or below 100 mA under all conditions.

2a) The next category is for USB devices which need more than 100 mA. This is where the circuit can get complicated, because there must be some sort of software switch that is off by default, and will never turn on unless the USB host grants permission for more than 100 mA. It is your responsibility to make sure that your USB device design does not pull more than 100 mA until it has received permission from the USB host. The USB IR Toy basically needs to keep the IR LED off until the USB host has activated the device. Other circuits might need a special regulator chip with a SHUTDOWN input that controls peripheral circuits so that only the main regulator and CPU are enabled (and hopefully under 100 mA, else you're screwed) by default. In other words, SHUTDOWN must be guaranteed to be active at startup, which may require pull-up or pull-down resistors in case the I/O ports are tri-stated. Then, once the CPU detects that the USB host has granted more current, the CPU can flip some I/O pin that disables the SHUTDOWN and suddenly pulls more power. You must measure the maximum current you need and advertise this value in the USB Descriptors. If the USB host doesn't have enough current to spare, then it will not send all of the USB control messages to your device to fully enable it. Asking for the maximum of 500 mA just means that the USB host will think it's overloaded before it really is, and that reduces the number of devices you can have active on a port.

Again, nothing is watching your device to keep the current below the limit. If your device accidently exceeds 100 mA before it is allowed, then maybe nothing will happen, or maybe the USB port will shut down. You must be totally familiar with all of the functions that your circuit can perform and make sure that nothing can pull current unless the total doesn't exceed the USB specifications.

2b) A special subcategory is when your device might prefer a larger current, but can still operate with limited functions on a lower current. Maybe your device prefers to operate closer to 500 mA, but in the event that the USB host might deny that current request, maybe your circuit could get by with 250 mA if half the functions were shut down and remain shut down. In these situations, your USB Descriptors would have to include multiple Configurations. You'll noticed that the current is not listed in the Device descriptors (where the assumption is no more than 100 mA), but rather each Configuration has a unique current in MaxPower. What the USB host is supposed to do is enable the first Configuration with the highest current request if the total current is not beyond its capabilities. If that first Configuration is too much, then the USB host will instead select the next Configuration with a lower current until it finds one that is not too much for the situation at the moment. When there are more devices attached to one port (via an unpowered hub), there will be less current budget available.

Basically, in every one of these examples, your USB Device should never pull more than 100 mA until the USB Host activates a specific Configuration that advertises more current. By activating a particular Configuration, the USB Host is giving the USB Device permission to draw as much current as was advertised, but no more. As I've stressed above, though, nothing is specifically minding the current of your individual device (other than perhaps a maximum current overload fuse).

Also, when your 500 mA USB Device is suspended (sleep), the current must drop back to 100 mA or less. Simpler Devices can just stop sending data to peripherals, but make sure the I/O pins aren't left in an active state that pulls a lot of current. More complicated Devices with multiple regulators will need to activate the SHUTDOWN line to take those high-current extras off line until the USB Host resumes.

In other words, if your USB Device exceeds the default 100 mA, but you do not design it to control the various current levels as they are enabled, then there is really nothing that the USB Host can do to limit your current. The USB specifications are designed with the assumption that USB Devices will properly advertise their current needs, and properly control their current draw based upon listening to what the USB Host permits. The only thing that the USB Host does is say "Yes" or "No" to a given USB Configuration. After that, it's up to the USB Device to control the current.

Finally, the thing to remember is that current is trickier to deal with than voltage. Have you ever seen those bumper stickers that say SHIT HAPPENS.? Well, I like to paraphrase that by saying CURRENT HAPPENS. Nearly all electronic circuits work by controlling voltages. The current that flows is therefore a side-effect of the load attached to the voltage. In other words, circuit makes voltages, but current happens as a result of those voltages and the specific loads. Your USB Device is the load on the USB Host's port. Your USB Device is therefore entirely in control over the amount of current that is pulled. If you don't pay careful attention and take responsibility for keeping the current within the limits, then unpredictable results will occur.

Post Script:

I have no idea what happens if you plug in more than 5 USB Devices on the same port. Since each gets 100 mA by default, 5 devices would already be at 500 mA. Perhaps the idea is that you can't really go beyond 5 because you'd need a 4-port hub to reach that number anyway. But if you plug a USB hub into a USB hub, it seems like you could quickly exceed the normal limits. I'm sure that the USB specifications say what should happen, but I've never needed to know, and so that's something I'd have to search for.

I also cannot remember whether USB Configurations must occur in order from highest current to lowest current. The USB Host will certainly enable the "first" Configuration that it finds, assuming it doesn't ask for too much current, but I don't know whether you could get fancy and switch up the order based upon some variable besides the current. The few USB Device firmwares that I've developed with more than one Configuration managed to confuse Windows so much that the OS refused to work at all.