adafruit / Adafruit_TinyUSB_Arduino

Arduino library for TinyUSB
MIT License
476 stars 127 forks source link

Allow setting of USB Power parameter #421

Open DisasterAreaDesigns opened 4 months ago

DisasterAreaDesigns commented 4 months ago

Many USB hosts have power limits and will refuse to connect if a device reports a power requirement higher than this limit. For example, the Apple Camera Connection kit will report an error if the user attempts to connect a device that reports a 100mA requirement, but will succeed on a 20mA device.

This parameter appears to be set in Adafruit_USBD_Device.cpp:

// Config number, interface count, string index, total length, // attribute (bit 7 set to 1), power in mA. // Note: Total Length Interface Number will be updated later uint8_t const dev_cfg[sizeof(tusb_desc_configuration_t)] = { TUD_CONFIG_DESCRIPTOR(1, 0, 0, sizeof(tusb_desc_configuration_t), TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP | TU_BIT(7), 100), };

The 100mA power value is thus fixed in the library and can't be changed without editing the .cpp file here. That also means that any changes made to the file are replaced by library updates.

There is a define in the file, USB_CONFIG_POWER which defaults to 100. I think the idea would be to allow this to be editable in the variant file for your particular board, which makes it a lot less dangerous since it will rarely be changed.

Thus, I propose this change:

// Config number, interface count, string index, total length, // attribute (bit 7 set to 1), power in mA. // Note: Total Length Interface Number will be updated later uint8_t const dev_cfg[sizeof(tusb_desc_configuration_t)] = { TUD_CONFIG_DESCRIPTOR(1, 0, 0, sizeof(tusb_desc_configuration_t), TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP | TU_BIT(7), USB_CONFIG_POWER), };

... which would then allow a user to use:

#define USB_CONFIG_POWER <myVal>

... to change the power reporting. Note that this has nothing to do with the actual power the board requires, just like the current value of 100mA is not rooted in reality! But it's useful for setting this value for picky hosts.

I suspect most users are fine with the 100mA limitation but my application requires a device that can report a lower power requirement.