44 * The MIT License (MIT)
55 *
66 * Copyright (c) 2019 Damien P. George
7+ * Copyright (c) 2022 Blake W. Felt
78 *
89 * Permission is hereby granted, free of charge, to any person obtaining a copy
910 * of this software and associated documentation files (the "Software"), to deal
2425 * THE SOFTWARE.
2526 */
2627
28+ #include "mpconfigport.h"
2729#include "tusb.h"
28- #include "pico/unique_id.h"
29-
30- #ifndef MICROPY_HW_USB_VID
31- #define MICROPY_HW_USB_VID (0x2E8A) // Raspberry Pi
32- #endif
33- #ifndef MICROPY_HW_USB_PID
34- #define MICROPY_HW_USB_PID (0x0005) // RP2 MicroPython
35- #endif
30+ #include "usbd.h"
3631
3732#if CFG_TUD_MSC
3833#define USBD_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_MSC_DESC_LEN)
6560#define USBD_STR_CDC (0x04)
6661#define USBD_STR_MSC (0x05)
6762
63+ #define USBD_DESC_STR_MAX (20)
64+ #define USBD_DESC_SERIAL_MAX (32)
65+
6866// Note: descriptors returned from callbacks must exist long enough for transfer to complete
6967
7068static const tusb_desc_device_t usbd_desc_device = {
@@ -115,8 +113,7 @@ const uint8_t *tud_descriptor_configuration_cb(uint8_t index) {
115113}
116114
117115const uint16_t * tud_descriptor_string_cb (uint8_t index , uint16_t langid ) {
118- #define DESC_STR_MAX (20)
119- static uint16_t desc_str [DESC_STR_MAX ];
116+ static uint16_t desc_str [USBD_DESC_STR_MAX ];
120117
121118 uint8_t len ;
122119 if (index == 0 ) {
@@ -128,17 +125,22 @@ const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
128125 }
129126 // check, if serial is requested
130127 if (index == USBD_STR_SERIAL ) {
131- pico_unique_board_id_t id ;
132- pico_get_unique_board_id (& id );
128+ uint8_t buffer [USBD_DESC_SERIAL_MAX ] = {0 };
129+ int buflen ;
130+ const char * hexdig = "0123456789abcdef" ;
131+
132+ buflen = usbd_serialnumber (buffer );
133133 // byte by byte conversion
134- for (len = 0 ; len < 16 ; len += 2 ) {
135- const char * hexdig = "0123456789abcdef" ;
136- desc_str [1 + len ] = hexdig [id .id [len >> 1 ] >> 4 ];
137- desc_str [1 + len + 1 ] = hexdig [id .id [len >> 1 ] & 0x0f ];
134+ len = 0 ;
135+ for (int i = 0 ; i < buflen ; i ++ ) {
136+ uint8_t val = buffer [i ];
137+ desc_str [1 + len ] = hexdig [val >> 4 ];
138+ desc_str [2 + len ] = hexdig [val & 0x0F ];
139+ len += 2 ;
138140 }
139141 } else {
140142 const char * str = usbd_desc_str [index ];
141- for (len = 0 ; len < DESC_STR_MAX - 1 && str [len ]; ++ len ) {
143+ for (len = 0 ; len < USBD_DESC_STR_MAX - 1 && str [len ]; ++ len ) {
142144 desc_str [1 + len ] = str [len ];
143145 }
144146 }
@@ -149,3 +151,7 @@ const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
149151
150152 return desc_str ;
151153}
154+
155+ void usbd_reset_descriptor (void ) {
156+ // not used yet
157+ }
0 commit comments