32F4 USB client and USB Power Descriptor - Page 1
Forum section: electronics / general

author: peter-h
author_rank: Super Contributor
timestamp: November 16, 2023, 03:58:40 pm
content: One possible (though rare) feature of a project I am doing is for the box to be USB powered. Normally it is 24V powered.It draws about 70mA typical from USB but could be 150mA or so.I know that in reality few if any USB Hosts actually limit the available current according to the client-notified requirement, but they might.The issue is that over 100mA means the USB power descriptor feature "should" be implemented. The STM USB code contains USB_CONFIG_SELF_POWERED which is set and it appears to say that the device needs no USB power.I reckon they have not implemented thisThe key field is bMaxPower which in the ST Cube IDE USB code appears only in the AUDIO device context:Does this make sense?

author: NorthGuy
author_rank: Super Contributor
timestamp: November 16, 2023, 05:40:30 pm
content: Power setting in the device descriptor specifies how much power the device wants to use. It is read at least by Windows - if you set it above 500 mA Windows will refuse enumeration. I don't know if it is really used to limit current.GET_STATUS is a request from host to find out if the device is self-powered at the very moment. I guess the intent is to find out if the host can cut power and go to sleep.

author: peter-h
author_rank: Super Contributor
timestamp: November 16, 2023, 06:53:41 pm
content: I have seen some USB power management chips which limit the current or have a trip. Some were programmable but IME nothing limits below 500mA.I'd like to do this client MaxPower thing right, just in case, but the ST code doesn't seem to support that... quite.

author: NorthGuy
author_rank: Super Contributor
timestamp: November 16, 2023, 11:43:45 pm
content: Quote from: peter-h on November 16, 2023, 06:53:41 pmThere must be a device descriptor somewhere - similar to the Audio descriptor you have posted. You simply need to edit it. If you cannot find it, look through the USB code - they should return the descriptor in response to GET_DESCRIPTOR request. Looking at the code you have posted, it's probably USBD_GetDescriptor() function. See where they pull the descriptor from. There are many different descriptors. You need a "device descriptor".

author: peter-h
author_rank: Super Contributor
timestamp: November 17, 2023, 08:40:39 am
content: Yes - I found it. It specifies 100mA.Line 89

author: SiliconWizard
author_rank: Super Contributor
timestamp: November 17, 2023, 09:10:31 am
content: Yes, that's the one. It's given in 2 mA increments, so 0x32 = 50 => 100 mA. You can increase it to whatever seems appropriate for your device. There's no CRC or checksum on the USB descriptors, so you just need to change this one value.Yes, on most desktop PCs this value doesn't matter much, but on laptops the story can be a bit different. It *can* be used by the host to limit current / detect overcurrent and just shut down the USB port. And if for nothing else, putting a realistic value in there is respectful for users. (For those that will have a look, which is all the more likely if your users are technicians and engineers.)

author: peter-h
author_rank: Super Contributor
timestamp: November 17, 2023, 09:34:19 am
content: This is the thinking someone sent me:It's very difficult to be both self powered or bus powered and to stay strictly within the spec. In practice it doesn't matter because all hosts can supply up to 500mA and usually much more on each hub.The purpose of the max power in the device descriptor is to allow the host to deny enablement of your device if it would exceed the total power on any hub. But this assumes that you are obeying the rules to start with and that, on power up, your device limits its consumption to <100mA until the host completes the negotiation and gives you permission to continue and fully power up to your max power amount. Unless you've got hardware logic to control power to the subsystems [which I do not; there isn't the MOSFET switch which one might use with say an FT232] and to detect whether self-powered or bus-powered you can't get this right. Saying that you are self-powered and then drawing current from the bus is a bit cheeky but it works in every case and is better than being shut down by a host because it doesn't have enough power left out of its 500mA budget to meet what you claim in your descriptor when you are actually self powered anyway.And if you request more than you actually need in the descriptor it can cause problems sharing a hub with other devices especially if they have also requested more than they need. So you definitely don't want to request 500mA as you might either not get enabled or could prevent other USB devices on the same hub from being enabled.As a device that is primarily self-powered it's probably more important to obey the Vbus rules for self-powered devices which this one does. This ensures that the USB data lines are never powered by the self-powered device when the host is powered down.It is an interesting point re hubs, because an unpowered hub must be limited to 100mA/port and this is why you can't get them with > 4 ports. The bigger hubs are all powered.