Electronic – Can a USB device attempt to be bus-powered and then fall-back to self-powered

usbusb deviceusb-otg

The device has a battery, but it would be nice to be bus-powered when available. A computer will typically supply 500 mA, for instance, while a smartphone in OTG mode is only required to supply 8 mA.

Is it possible to do something like this?

  1. request 500 mA
  2. if accepted: power from computer and charge battery from computer
  3. else: request 200 mA
  4. if accepted: power from computer
  5. else: claim self-powered, power from battery

This seems to be what configurations are for:

Therefore it is possible to have two configurations, one for when the device is bus powered and another when it is mains powered. …
Once all the configurations have been examined by the host, the host will send a SetConfiguration command with a non zero value which matches the bConfigurationValue of one of the configurations. This is used to select the desired configuration.

Is it enough to specify multiple configurations in the descriptors, one for each current requirement? Does the OS automatically try them in order and move on to the next if one fails?

Multi-config USB devices and Windows seems to say that's how it works:

During enumeration, USBCCGP will first attempt to select the configuration whose descriptor is found at the specified “original” index. If the attempt fails, normally due to the configuration requiring more than 100mA while the device’s upstream hub has only bus power, then USBCCGP attempts to select the configuration found at the specified “alternate” index instead.

However this doesn't work in practice?:

“Microsoft drivers invariably work with just the first configuration of a device. The Microsoft support for composite devices won’t engage if the device has multiple configurations. Consequently, multiconfiguration devices seem to be rare in practice, and Microsoft discourages people from designing new ones.”

and:

Microsoft drivers will only ask for the first configuration – and if that asks for too much current, so be it.

and:

it is not common to have more than one configuration. Windows standard drivers will always select the first configuration so there is not a lot of point.

If multiple configurations don't work in practice, can the device detect that it's been rejected, simulate a disconnect and reconnect with a different set of descriptors?

(This is a question about the USB enumeration/communication part, not the hardware implementation of how to connect the battery, etc.)

Best Answer

That defines a normal enumeration process. If it doesn't work for you that way, and you are programming the USB peripheral side, then you could do this without issue.

Enumerate, failed at 500 mA request. The Host should signal this enumeration failure according to the Specs.

Device disconnects the usb data pins, changes its usb descriptor, and signals for enumeration again.

If failed. Repeat, changing it's descriptor to read self powered again. You must have a method to switch the power pin. A usb power switch for example. A diode OR bridge if the voltage drop is acceptable. You cannot back feed the VUSB with your battery unless you want to break something. Then signal again.

The descriptor is nothing but a few bytes that you could change via variables. As long as you can change that, you are fine.