Skip to content

Commit 14c9a38

Browse files
cxxcoderNoltari
authored andcommitted
usb-descriptors: use flash ID as USB serial
Signed-off-by: Álvaro Fernández Rojas <[email protected]>
1 parent 05e4815 commit 14c9a38

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ target_include_directories(uart_bridge PUBLIC
1515
pico-sdk/lib/tinyusb/src)
1616

1717
target_link_libraries(uart_bridge
18+
hardware_flash
1819
pico_multicore
1920
pico_stdlib
2021
tinyusb_device)

usb-descriptors.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* Copyright (c) 2019 Damien P. George
1010
*/
1111

12+
#include <hardware/flash.h>
1213
#include <tusb.h>
1314

1415
#define DESC_STR_MAX 20
@@ -36,6 +37,7 @@
3637
#define USBD_STR_MANUF 0x01
3738
#define USBD_STR_PRODUCT 0x02
3839
#define USBD_STR_SERIAL 0x03
40+
#define USBD_STR_SERIAL_LEN 17
3941
#define USBD_STR_CDC 0x04
4042

4143
static const tusb_desc_device_t usbd_desc_device = {
@@ -71,7 +73,6 @@ static const uint8_t usbd_desc_cfg[USBD_DESC_LEN] = {
7173
static const char *const usbd_desc_str[] = {
7274
[USBD_STR_MANUF] = "Raspberry Pi",
7375
[USBD_STR_PRODUCT] = "Pico",
74-
[USBD_STR_SERIAL] = "000000000000",
7576
[USBD_STR_CDC] = "Board CDC",
7677
};
7778

@@ -95,11 +96,23 @@ const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid)
9596
len = 1;
9697
} else {
9798
const char *str;
99+
char serial[USBD_STR_SERIAL_LEN];
98100

99101
if (index >= sizeof(usbd_desc_str) / sizeof(usbd_desc_str[0]))
100102
return NULL;
101103

102-
str = usbd_desc_str[index];
104+
if (index == USBD_STR_SERIAL) {
105+
uint8_t id[8];
106+
107+
flash_get_unique_id(id);
108+
sprintf(serial, "%02X%02X%02X%02X%02X%02X%02X%02X",
109+
id[0], id[1], id[2], id[3], id[4], id[5], id[6], id[7]);
110+
111+
str = serial;
112+
} else {
113+
str = usbd_desc_str[index];
114+
}
115+
103116
for (len = 0; len < DESC_STR_MAX - 1 && str[len]; ++len)
104117
desc_str[1 + len] = str[len];
105118
}

0 commit comments

Comments
 (0)