stm32/usbdev: Combine all str descriptor accessor funcs into one func.
There's no need to have these as separate functions, they just take up unnecessary code space and combining them allows to factor common code, and also allows to support arbitrary string descriptor indices.
This commit is contained in:
parent
1d4246a2e8
commit
583472e068
@ -44,7 +44,6 @@
|
||||
#define USBD_MAX_NUM_INTERFACES 1
|
||||
#define USBD_MAX_NUM_CONFIGURATION 1
|
||||
#define USBD_MAX_STR_DESC_SIZ 0x100
|
||||
#define USBD_SUPPORT_USER_STRING 0
|
||||
#define USBD_SELF_POWERED 0
|
||||
#define USBD_DEBUG_LEVEL 0
|
||||
|
||||
|
@ -103,115 +103,82 @@ STATIC uint8_t *USBD_DeviceDescriptor(USBD_HandleTypeDef *pdev, uint16_t *length
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the LangID string descriptor.
|
||||
* @param speed: Current device speed
|
||||
* @brief Returns a string descriptor
|
||||
* @param idx: Index of the string descriptor to retrieve
|
||||
* @param length: Pointer to data length variable
|
||||
* @retval Pointer to descriptor buffer
|
||||
* @retval Pointer to descriptor buffer, or NULL if idx is invalid
|
||||
*/
|
||||
STATIC uint8_t *USBD_LangIDStrDescriptor(USBD_HandleTypeDef *pdev, uint16_t *length) {
|
||||
*length = sizeof(USBD_LangIDDesc);
|
||||
return (uint8_t*)USBD_LangIDDesc; // the data should only be read from this buf
|
||||
}
|
||||
STATIC uint8_t *USBD_StrDescriptor(USBD_HandleTypeDef *pdev, uint8_t idx, uint16_t *length) {
|
||||
char str_buf[16];
|
||||
const char *str = NULL;
|
||||
|
||||
/**
|
||||
* @brief Returns the product string descriptor.
|
||||
* @param speed: Current device speed
|
||||
* @param length: Pointer to data length variable
|
||||
* @retval Pointer to descriptor buffer
|
||||
*/
|
||||
STATIC uint8_t *USBD_ProductStrDescriptor(USBD_HandleTypeDef *pdev, uint16_t *length) {
|
||||
uint8_t *str_desc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->usbd_str_desc;
|
||||
if (pdev->dev_speed == USBD_SPEED_HIGH) {
|
||||
USBD_GetString((uint8_t *)USBD_PRODUCT_HS_STRING, str_desc, length);
|
||||
} else {
|
||||
USBD_GetString((uint8_t *)USBD_PRODUCT_FS_STRING, str_desc, length);
|
||||
switch (idx) {
|
||||
case USBD_IDX_LANGID_STR:
|
||||
*length = sizeof(USBD_LangIDDesc);
|
||||
return (uint8_t*)USBD_LangIDDesc; // the data should only be read from this buf
|
||||
|
||||
case USBD_IDX_MFC_STR:
|
||||
str = USBD_MANUFACTURER_STRING;
|
||||
break;
|
||||
|
||||
case USBD_IDX_PRODUCT_STR:
|
||||
if (pdev->dev_speed == USBD_SPEED_HIGH) {
|
||||
str = USBD_PRODUCT_HS_STRING;
|
||||
} else {
|
||||
str = USBD_PRODUCT_FS_STRING;
|
||||
}
|
||||
break;
|
||||
|
||||
case USBD_IDX_SERIAL_STR: {
|
||||
// This document: http://www.usb.org/developers/docs/devclass_docs/usbmassbulk_10.pdf
|
||||
// says that the serial number has to be at least 12 digits long and that
|
||||
// the last 12 digits need to be unique. It also stipulates that the valid
|
||||
// character set is that of upper-case hexadecimal digits.
|
||||
//
|
||||
// The onboard DFU bootloader produces a 12-digit serial number based on
|
||||
// the 96-bit unique ID, so for consistency we go with this algorithm.
|
||||
// You can see the serial number if you use: lsusb -v
|
||||
//
|
||||
// See: https://my.st.com/52d187b7 for the algorithim used.
|
||||
|
||||
uint8_t *id = (uint8_t *)MP_HAL_UNIQUE_ID_ADDRESS;
|
||||
snprintf(str_buf, sizeof(str_buf),
|
||||
"%02X%02X%02X%02X%02X%02X",
|
||||
id[11], id[10] + id[2], id[9], id[8] + id[0], id[7], id[6]);
|
||||
|
||||
str = str_buf;
|
||||
break;
|
||||
}
|
||||
|
||||
case USBD_IDX_CONFIG_STR:
|
||||
if (pdev->dev_speed == USBD_SPEED_HIGH) {
|
||||
str = USBD_CONFIGURATION_HS_STRING;
|
||||
} else {
|
||||
str = USBD_CONFIGURATION_FS_STRING;
|
||||
}
|
||||
break;
|
||||
|
||||
case USBD_IDX_INTERFACE_STR:
|
||||
if (pdev->dev_speed == USBD_SPEED_HIGH) {
|
||||
str = USBD_INTERFACE_HS_STRING;
|
||||
} else {
|
||||
str = USBD_INTERFACE_FS_STRING;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
// invalid string index
|
||||
return NULL;
|
||||
}
|
||||
return str_desc;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the manufacturer string descriptor.
|
||||
* @param speed: Current device speed
|
||||
* @param length: Pointer to data length variable
|
||||
* @retval Pointer to descriptor buffer
|
||||
*/
|
||||
STATIC uint8_t *USBD_ManufacturerStrDescriptor(USBD_HandleTypeDef *pdev, uint16_t *length) {
|
||||
uint8_t *str_desc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->usbd_str_desc;
|
||||
USBD_GetString((uint8_t *)USBD_MANUFACTURER_STRING, str_desc, length);
|
||||
return str_desc;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the serial number string descriptor.
|
||||
* @param speed: Current device speed
|
||||
* @param length: Pointer to data length variable
|
||||
* @retval Pointer to descriptor buffer
|
||||
*/
|
||||
STATIC uint8_t *USBD_SerialStrDescriptor(USBD_HandleTypeDef *pdev, uint16_t *length) {
|
||||
// This document: http://www.usb.org/developers/docs/devclass_docs/usbmassbulk_10.pdf
|
||||
// says that the serial number has to be at least 12 digits long and that
|
||||
// the last 12 digits need to be unique. It also stipulates that the valid
|
||||
// character set is that of upper-case hexadecimal digits.
|
||||
//
|
||||
// The onboard DFU bootloader produces a 12-digit serial number based on
|
||||
// the 96-bit unique ID, so for consistency we go with this algorithm.
|
||||
// You can see the serial number if you do:
|
||||
//
|
||||
// dfu-util -l
|
||||
//
|
||||
// See: https://my.st.com/52d187b7 for the algorithim used.
|
||||
|
||||
uint8_t *id = (uint8_t *)MP_HAL_UNIQUE_ID_ADDRESS;
|
||||
char serial_buf[16];
|
||||
snprintf(serial_buf, sizeof(serial_buf),
|
||||
"%02X%02X%02X%02X%02X%02X",
|
||||
id[11], id[10] + id[2], id[9], id[8] + id[0], id[7], id[6]);
|
||||
|
||||
uint8_t *str_desc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->usbd_str_desc;
|
||||
USBD_GetString((uint8_t *)serial_buf, str_desc, length);
|
||||
return str_desc;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the configuration string descriptor.
|
||||
* @param speed: Current device speed
|
||||
* @param length: Pointer to data length variable
|
||||
* @retval Pointer to descriptor buffer
|
||||
*/
|
||||
STATIC uint8_t *USBD_ConfigStrDescriptor(USBD_HandleTypeDef *pdev, uint16_t *length) {
|
||||
uint8_t *str_desc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->usbd_str_desc;
|
||||
if (pdev->dev_speed == USBD_SPEED_HIGH) {
|
||||
USBD_GetString((uint8_t *)USBD_CONFIGURATION_HS_STRING, str_desc, length);
|
||||
} else {
|
||||
USBD_GetString((uint8_t *)USBD_CONFIGURATION_FS_STRING, str_desc, length);
|
||||
}
|
||||
return str_desc;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the interface string descriptor.
|
||||
* @param speed: Current device speed
|
||||
* @param length: Pointer to data length variable
|
||||
* @retval Pointer to descriptor buffer
|
||||
*/
|
||||
STATIC uint8_t *USBD_InterfaceStrDescriptor(USBD_HandleTypeDef *pdev, uint16_t *length) {
|
||||
uint8_t *str_desc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->usbd_str_desc;
|
||||
if (pdev->dev_speed == USBD_SPEED_HIGH) {
|
||||
USBD_GetString((uint8_t *)USBD_INTERFACE_HS_STRING, str_desc, length);
|
||||
} else {
|
||||
USBD_GetString((uint8_t *)USBD_INTERFACE_FS_STRING, str_desc, length);
|
||||
}
|
||||
USBD_GetString((uint8_t*)str, str_desc, length);
|
||||
return str_desc;
|
||||
}
|
||||
|
||||
const USBD_DescriptorsTypeDef USBD_Descriptors = {
|
||||
USBD_DeviceDescriptor,
|
||||
USBD_LangIDStrDescriptor,
|
||||
USBD_ManufacturerStrDescriptor,
|
||||
USBD_ProductStrDescriptor,
|
||||
USBD_SerialStrDescriptor,
|
||||
USBD_ConfigStrDescriptor,
|
||||
USBD_InterfaceStrDescriptor,
|
||||
USBD_StrDescriptor,
|
||||
};
|
||||
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
||||
|
@ -172,9 +172,6 @@ typedef struct _Device_cb
|
||||
uint8_t *(*GetFSConfigDescriptor)(struct _USBD_HandleTypeDef *pdev, uint16_t *length);
|
||||
uint8_t *(*GetOtherSpeedConfigDescriptor)(struct _USBD_HandleTypeDef *pdev, uint16_t *length);
|
||||
uint8_t *(*GetDeviceQualifierDescriptor)(struct _USBD_HandleTypeDef *pdev, uint16_t *length);
|
||||
#if (USBD_SUPPORT_USER_STRING == 1)
|
||||
uint8_t *(*GetUsrStrDescriptor)(struct _USBD_HandleTypeDef *pdev ,uint8_t index, uint16_t *length);
|
||||
#endif
|
||||
|
||||
} USBD_ClassTypeDef;
|
||||
|
||||
@ -199,12 +196,7 @@ struct _USBD_HandleTypeDef;
|
||||
typedef struct
|
||||
{
|
||||
uint8_t *(*GetDeviceDescriptor)(struct _USBD_HandleTypeDef *pdev, uint16_t *length);
|
||||
uint8_t *(*GetLangIDStrDescriptor)(struct _USBD_HandleTypeDef *pdev, uint16_t *length);
|
||||
uint8_t *(*GetManufacturerStrDescriptor)(struct _USBD_HandleTypeDef *pdev, uint16_t *length);
|
||||
uint8_t *(*GetProductStrDescriptor)(struct _USBD_HandleTypeDef *pdev, uint16_t *length);
|
||||
uint8_t *(*GetSerialStrDescriptor)(struct _USBD_HandleTypeDef *pdev, uint16_t *length);
|
||||
uint8_t *(*GetConfigurationStrDescriptor)(struct _USBD_HandleTypeDef *pdev, uint16_t *length);
|
||||
uint8_t *(*GetInterfaceStrDescriptor)(struct _USBD_HandleTypeDef *pdev, uint16_t *length);
|
||||
uint8_t *(*GetStrDescriptor)(struct _USBD_HandleTypeDef *pdev, uint8_t idx, uint16_t *length);
|
||||
} USBD_DescriptorsTypeDef;
|
||||
|
||||
/* USB Device handle structure */
|
||||
|
@ -347,42 +347,13 @@ static void USBD_GetDescriptor(USBD_HandleTypeDef *pdev ,
|
||||
break;
|
||||
|
||||
case USB_DESC_TYPE_STRING:
|
||||
switch ((uint8_t)(req->wValue))
|
||||
{
|
||||
case USBD_IDX_LANGID_STR:
|
||||
pbuf = pdev->pDesc->GetLangIDStrDescriptor(pdev, &len);
|
||||
break;
|
||||
|
||||
case USBD_IDX_MFC_STR:
|
||||
pbuf = pdev->pDesc->GetManufacturerStrDescriptor(pdev, &len);
|
||||
break;
|
||||
|
||||
case USBD_IDX_PRODUCT_STR:
|
||||
pbuf = pdev->pDesc->GetProductStrDescriptor(pdev, &len);
|
||||
break;
|
||||
|
||||
case USBD_IDX_SERIAL_STR:
|
||||
pbuf = pdev->pDesc->GetSerialStrDescriptor(pdev, &len);
|
||||
break;
|
||||
|
||||
case USBD_IDX_CONFIG_STR:
|
||||
pbuf = pdev->pDesc->GetConfigurationStrDescriptor(pdev, &len);
|
||||
break;
|
||||
|
||||
case USBD_IDX_INTERFACE_STR:
|
||||
pbuf = pdev->pDesc->GetInterfaceStrDescriptor(pdev, &len);
|
||||
break;
|
||||
|
||||
default:
|
||||
#if (USBD_SUPPORT_USER_STRING == 1)
|
||||
pbuf = pdev->pClass->GetUsrStrDescriptor(pdev, (req->wValue) , &len);
|
||||
break;
|
||||
#else
|
||||
USBD_CtlError(pdev , req);
|
||||
pbuf = pdev->pDesc->GetStrDescriptor(pdev, req->wValue & 0xff, &len);
|
||||
if (pbuf == NULL) {
|
||||
USBD_CtlError(pdev, req);
|
||||
return;
|
||||
#endif
|
||||
}
|
||||
break;
|
||||
|
||||
case USB_DESC_TYPE_DEVICE_QUALIFIER:
|
||||
|
||||
if(pdev->dev_speed == USBD_SPEED_HIGH )
|
||||
|
Loading…
x
Reference in New Issue
Block a user