Skip to content

Commit

Permalink
fixup! refactor(core): improve USB events handling, more extensible i…
Browse files Browse the repository at this point in the history
…mplementation
  • Loading branch information
TychoVrahe committed Dec 10, 2024
1 parent a355c19 commit d45da63
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions core/embed/io/usb/stm32/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,21 @@ secbool usb_init(const usb_dev_info_t *dev_info) {
// Device descriptor
drv->dev_desc.bLength = sizeof(usb_device_descriptor_t);
drv->dev_desc.bDescriptorType = USB_DESC_TYPE_DEVICE;
drv->dev_desc.bcdUSB =
(sectrue == drv->usb21_enabled) ? 0x0210 : 0x0200; // USB 2.1 or USB 2.0
// USB 2.1 or USB 2.0
drv->dev_desc.bcdUSB = (sectrue == drv->usb21_enabled) ? 0x0210 : 0x0200;
drv->dev_desc.bDeviceClass = dev_info->device_class;
drv->dev_desc.bDeviceSubClass = dev_info->device_subclass;
drv->dev_desc.bDeviceProtocol = dev_info->device_protocol;
drv->dev_desc.bMaxPacketSize0 = USB_MAX_EP0_SIZE;
drv->dev_desc.idVendor = dev_info->vendor_id;
drv->dev_desc.idProduct = dev_info->product_id;
drv->dev_desc.bcdDevice = dev_info->release_num;
drv->dev_desc.iManufacturer =
USBD_IDX_MFC_STR; // Index of manufacturer string
drv->dev_desc.iProduct = USBD_IDX_PRODUCT_STR; // Index of product string
drv->dev_desc.iSerialNumber =
USBD_IDX_SERIAL_STR; // Index of serial number string
// Index of manufacturer string
drv->dev_desc.iManufacturer = USBD_IDX_MFC_STR;
// Index of product string
drv->dev_desc.iProduct = USBD_IDX_PRODUCT_STR;
// Index of serial number string
drv->dev_desc.iSerialNumber = USBD_IDX_SERIAL_STR;
drv->dev_desc.bNumConfigurations = 1;

// String table
Expand All @@ -161,19 +162,19 @@ secbool usb_init(const usb_dev_info_t *dev_info) {
// Configuration descriptor
drv->config_desc->bLength = sizeof(usb_config_descriptor_t);
drv->config_desc->bDescriptorType = USB_DESC_TYPE_CONFIGURATION;
drv->config_desc->wTotalLength =
sizeof(usb_config_descriptor_t); // will be updated later via
// usb_alloc_class_descriptors()
drv->config_desc->bNumInterfaces =
0; // will be updated later via usb_set_iface_class()
// will be updated later via usb_alloc_class_descriptors()
drv->config_desc->wTotalLength = sizeof(usb_config_descriptor_t);
// will be updated later via usb_set_iface_class()
drv->config_desc->bNumInterfaces = 0;
drv->config_desc->bConfigurationValue = 0x01;
drv->config_desc->iConfiguration = 0;
drv->config_desc->bmAttributes =
0x80; // 0x80 = bus powered; 0xC0 = self powered
drv->config_desc->bMaxPower = 0x32; // Maximum Power Consumption in 2mA units
// 0x80 = bus powered; 0xC0 = self powered
drv->config_desc->bmAttributes = 0x80;
// Maximum Power Consumption in 2mA units
drv->config_desc->bMaxPower = 0x32;

drv->configured =
sectrue; // starting with this flag set, to avoid false warnings
// starting with this flag set, to avoid false warnings
drv->configured = sectrue;
drv->initialized = sectrue;

return sectrue;
Expand Down Expand Up @@ -324,14 +325,11 @@ usb_event_t usb_get_event(void) {
void usb_get_state(usb_state_t *state) {
usb_driver_t *drv = &g_usb_driver;

if (drv->initialized != sectrue) {
// The driver is not initialized
return;
}

usb_state_t s = {0};

s.configured = drv->configured == sectrue;
if (drv->initialized == sectrue) {
s.configured = drv->configured == sectrue;
}

*state = s;
}
Expand Down

0 comments on commit d45da63

Please sign in to comment.