Skip to content

Commit

Permalink
expose target name via usb descriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
bkleiner committed Jul 4, 2024
1 parent 474a2b7 commit 48b1d00
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 18 deletions.
1 change: 0 additions & 1 deletion lib/at32usb/inc/cdc_desc.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ extern "C" {
* @brief usb string define(vendor, product configuration, interface)
*/
#define USBD_CDC_DESC_MANUFACTURER_STRING "QUICKSILVER"
#define USBD_CDC_DESC_PRODUCT_STRING "QUICKSILVER"
#define USBD_CDC_DESC_CONFIGURATION_STRING "Virtual ComPort Config"
#define USBD_CDC_DESC_INTERFACE_STRING "Virtual ComPort Interface"

Expand Down
13 changes: 1 addition & 12 deletions lib/at32usb/src/cdc_desc.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static usbd_desc_t *get_device_configuration(void);
static usbd_desc_t *get_device_other_speed(void);
static usbd_desc_t *get_device_lang_id(void);
static usbd_desc_t *get_device_manufacturer_string(void);
static usbd_desc_t *get_device_product_string(void);
extern usbd_desc_t *get_device_product_string(void);
static usbd_desc_t *get_device_serial_string(void);
static usbd_desc_t *get_device_interface_string(void);
static usbd_desc_t *get_device_config_string(void);
Expand Down Expand Up @@ -384,17 +384,6 @@ static usbd_desc_t *get_device_manufacturer_string(void)
return &vp_desc;
}

/**
* @brief get product descriptor
* @param none
* @retval usbd_desc
*/
static usbd_desc_t *get_device_product_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_CDC_DESC_PRODUCT_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}

/**
* @brief get serial descriptor
Expand Down
22 changes: 22 additions & 0 deletions src/driver/at32/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,28 @@ static void usb_enable_clock() {
acc_calibration_mode_enable(ACC_CAL_HICKTRIM, TRUE);
}

usbd_desc_t *get_device_product_string(void) {
static usbd_desc_t vp_desc;
static uint8_t desc_buffer[64];

uint16_t str_len = 0, id_pos = 2;
uint8_t *tmp_str = target.name;

while (*tmp_str != '\0' && str_len < 32) {
str_len++;
desc_buffer[id_pos++] = *tmp_str++;
desc_buffer[id_pos++] = 0x00;
}

str_len = str_len * 2 + 2;
desc_buffer[0] = (uint8_t)str_len;
desc_buffer[1] = USB_DESCIPTOR_TYPE_STRING;

vp_desc.length = str_len;
vp_desc.descriptor = desc_buffer;
return &vp_desc;
}

void usb_drv_init() {
gpio_config_t gpio_init;
gpio_init.mode = GPIO_ALTERNATE;
Expand Down
21 changes: 16 additions & 5 deletions src/driver/stm32/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@ static const struct cdc_config config_desc = {

static const struct usb_string_descriptor lang_desc = USB_ARRAY_DESC(USB_LANGID_ENG_US);
static const struct usb_string_descriptor manuf_desc_en = USB_STRING_DESC("QUICKSILVER");
static const struct usb_string_descriptor prod_desc_en = USB_STRING_DESC("QUICKSILVER");
static struct usb_string_descriptor prod_desc_en = {
.bLength = 0,
.bDescriptorType = USB_DTYPE_STRING,
.wString = {[0 ... 32] = 0},
};
static const struct usb_string_descriptor *const dtable[] = {
&lang_desc,
&manuf_desc_en,
Expand Down Expand Up @@ -183,13 +187,20 @@ static usbd_respond cdc_getdesc(usbd_ctlreq *req, void **address, uint16_t *leng
desc = &config_desc;
len = sizeof(config_desc);
break;
case USB_DTYPE_STRING:
if (dnumber < 3) {
desc = dtable[dnumber];
} else {
case USB_DTYPE_STRING: {
if (dnumber >= 3) {
return usbd_fail;
}

const uint32_t target_name_length = strnlen((const char *)target.name, 32);
for (uint32_t i = 0; i < target_name_length; i++) {
prod_desc_en.wString[i] = target.name[i];
}
prod_desc_en.bLength = target_name_length * 2 + 2;

desc = dtable[dnumber];
break;
}
default:
return usbd_fail;
}
Expand Down

0 comments on commit 48b1d00

Please sign in to comment.