Skip to content

Commit

Permalink
usb-descriptors: use flash ID as USB serial
Browse files Browse the repository at this point in the history
Signed-off-by: Álvaro Fernández Rojas <[email protected]>
  • Loading branch information
cxxcoder authored and Noltari committed Nov 4, 2022
1 parent 05e4815 commit 6aa7cf2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ target_include_directories(uart_bridge PUBLIC
pico-sdk/lib/tinyusb/src)

target_link_libraries(uart_bridge
hardware_flash
pico_multicore
pico_stdlib
tinyusb_device)
Expand Down
2 changes: 2 additions & 0 deletions tusb_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@
#define CFG_TUD_CDC_RX_BUFSIZE 256
#define CFG_TUD_CDC_TX_BUFSIZE 256

void usbd_serial_init(void);

#endif /* _TUSB_CONFIG_H_ */
2 changes: 2 additions & 0 deletions uart-bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ int main(void)

set_sys_clock_khz(250000, false);

usbd_serial_init();

for (itf = 0; itf < CFG_TUD_CDC; itf++)
init_uart_data(itf);

Expand Down
17 changes: 16 additions & 1 deletion usb-descriptors.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Copyright (c) 2019 Damien P. George
*/

#include <hardware/flash.h>
#include <tusb.h>

#define DESC_STR_MAX 20
Expand Down Expand Up @@ -36,6 +37,7 @@
#define USBD_STR_MANUF 0x01
#define USBD_STR_PRODUCT 0x02
#define USBD_STR_SERIAL 0x03
#define USBD_STR_SERIAL_LEN 17
#define USBD_STR_CDC 0x04

static const tusb_desc_device_t usbd_desc_device = {
Expand Down Expand Up @@ -68,10 +70,12 @@ static const uint8_t usbd_desc_cfg[USBD_DESC_LEN] = {
USBD_CDC_IN_OUT_MAX_SIZE),
};

static char usbd_serial[USBD_STR_SERIAL_LEN] = "000000000000";

static const char *const usbd_desc_str[] = {
[USBD_STR_MANUF] = "Raspberry Pi",
[USBD_STR_PRODUCT] = "Pico",
[USBD_STR_SERIAL] = "000000000000",
[USBD_STR_SERIAL] = usbd_serial,
[USBD_STR_CDC] = "Board CDC",
};

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

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

return desc_str;
}

void usbd_serial_init(void)
{
uint8_t id[8];

flash_get_unique_id(id);

snprintf(usbd_serial, USBD_STR_SERIAL_LEN, "%02X%02X%02X%02X%02X%02X%02X%02X",
id[0], id[1], id[2], id[3], id[4], id[5], id[6], id[7]);
}

0 comments on commit 6aa7cf2

Please sign in to comment.