Skip to content

Commit 6aa7cf2

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 6aa7cf2

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
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)

tusb_config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@
1616
#define CFG_TUD_CDC_RX_BUFSIZE 256
1717
#define CFG_TUD_CDC_TX_BUFSIZE 256
1818

19+
void usbd_serial_init(void);
20+
1921
#endif /* _TUSB_CONFIG_H_ */

uart-bridge.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ int main(void)
269269

270270
set_sys_clock_khz(250000, false);
271271

272+
usbd_serial_init();
273+
272274
for (itf = 0; itf < CFG_TUD_CDC; itf++)
273275
init_uart_data(itf);
274276

usb-descriptors.c

Lines changed: 16 additions & 1 deletion
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 = {
@@ -68,10 +70,12 @@ static const uint8_t usbd_desc_cfg[USBD_DESC_LEN] = {
6870
USBD_CDC_IN_OUT_MAX_SIZE),
6971
};
7072

73+
static char usbd_serial[USBD_STR_SERIAL_LEN] = "000000000000";
74+
7175
static const char *const usbd_desc_str[] = {
7276
[USBD_STR_MANUF] = "Raspberry Pi",
7377
[USBD_STR_PRODUCT] = "Pico",
74-
[USBD_STR_SERIAL] = "000000000000",
78+
[USBD_STR_SERIAL] = usbd_serial,
7579
[USBD_STR_CDC] = "Board CDC",
7680
};
7781

@@ -95,6 +99,7 @@ const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid)
9599
len = 1;
96100
} else {
97101
const char *str;
102+
char serial[USBD_STR_SERIAL_LEN];
98103

99104
if (index >= sizeof(usbd_desc_str) / sizeof(usbd_desc_str[0]))
100105
return NULL;
@@ -108,3 +113,13 @@ const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid)
108113

109114
return desc_str;
110115
}
116+
117+
void usbd_serial_init(void)
118+
{
119+
uint8_t id[8];
120+
121+
flash_get_unique_id(id);
122+
123+
snprintf(usbd_serial, USBD_STR_SERIAL_LEN, "%02X%02X%02X%02X%02X%02X%02X%02X",
124+
id[0], id[1], id[2], id[3], id[4], id[5], id[6], id[7]);
125+
}

0 commit comments

Comments
 (0)