@@ -122,6 +122,10 @@ void Adafruit_USBD_Device::setProductDescriptor(const char *s) {
122
122
_desc_str_arr[STRID_PRODUCT] = s;
123
123
}
124
124
125
+ void Adafruit_USBD_Device::setSerialDescriptor (const char *s) {
126
+ _desc_str_arr[STRID_SERIAL] = s;
127
+ }
128
+
125
129
void Adafruit_USBD_Device::task (void ) { tud_task (); }
126
130
127
131
bool Adafruit_USBD_Device::mounted (void ) { return tud_mounted (); }
@@ -206,6 +210,7 @@ void Adafruit_USBD_Device::clearConfiguration(void) {
206
210
_desc_str_arr[STRID_LANGUAGE] = (const char *)((uint32_t )USB_LANGUAGE);
207
211
_desc_str_arr[STRID_MANUFACTURER] = USB_MANUFACTURER;
208
212
_desc_str_arr[STRID_PRODUCT] = USB_PRODUCT;
213
+ _desc_str_arr[STRID_SERIAL] = nullptr ;
209
214
// STRID_SERIAL is platform dependent
210
215
211
216
_desc_str_count = 4 ;
@@ -285,24 +290,30 @@ bool Adafruit_USBD_Device::begin(uint8_t rhport) {
285
290
return true ;
286
291
}
287
292
293
+ static int strcpy_utf16 (const char *s, uint16_t *buf, int bufsize);
294
+
288
295
uint8_t Adafruit_USBD_Device::getSerialDescriptor (uint16_t *serial_utf16) {
289
- uint8_t serial_id[16 ] __attribute__ ((aligned (4 )));
290
- uint8_t const serial_len = TinyUSB_Port_GetSerialNumber (serial_id);
291
296
292
- for (uint8_t i = 0 ; i < serial_len; i++) {
293
- for (uint8_t j = 0 ; j < 2 ; j++) {
294
- const char nibble_to_hex[16 ] = {' 0' , ' 1' , ' 2' , ' 3' , ' 4' , ' 5' , ' 6' , ' 7' ,
295
- ' 8' , ' 9' , ' A' , ' B' , ' C' , ' D' , ' E' , ' F' };
297
+ if (!_desc_str_arr[STRID_SERIAL]) {
298
+ uint8_t serial_id[16 ] __attribute__ ((aligned (4 )));
299
+ uint8_t const serial_len = TinyUSB_Port_GetSerialNumber (serial_id);
300
+
301
+ for (uint8_t i = 0 ; i < serial_len; i++) {
302
+ for (uint8_t j = 0 ; j < 2 ; j++) {
303
+ const char nibble_to_hex[16 ] = {' 0' , ' 1' , ' 2' , ' 3' , ' 4' , ' 5' , ' 6' , ' 7' ,
304
+ ' 8' , ' 9' , ' A' , ' B' , ' C' , ' D' , ' E' , ' F' };
296
305
297
- uint8_t nibble = (serial_id[i] >> (j * 4 )) & 0xf ;
298
- serial_utf16[1 + i * 2 + (1 - j)] = nibble_to_hex[nibble]; // UTF-16-LE
306
+ uint8_t nibble = (serial_id[i] >> (j * 4 )) & 0xf ;
307
+ serial_utf16[1 + i * 2 + (1 - j)] = nibble_to_hex[nibble]; // UTF-16-LE
308
+ }
299
309
}
300
- }
301
310
302
- return 2 * serial_len;
311
+ return 2 * serial_len;
312
+ } else {
313
+ return strcpy_utf16 (_desc_str_arr[STRID_SERIAL], serial_utf16 + 1 , 32 );
314
+ }
303
315
}
304
316
305
- static int strcpy_utf16 (const char *s, uint16_t *buf, int bufsize);
306
317
uint16_t const *Adafruit_USBD_Device::descriptor_string_cb (uint8_t index,
307
318
uint16_t langid) {
308
319
(void )langid;
0 commit comments