From 14c9a38076206fa8256270f99066fa05ecc6b789 Mon Sep 17 00:00:00 2001 From: cxxcoder Date: Thu, 3 Nov 2022 18:12:56 +0100 Subject: [PATCH] usb-descriptors: use flash ID as USB serial MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Álvaro Fernández Rojas --- CMakeLists.txt | 1 + usb-descriptors.c | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index eda4f18..e0c286d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/usb-descriptors.c b/usb-descriptors.c index 5c10798..a45ca9d 100644 --- a/usb-descriptors.c +++ b/usb-descriptors.c @@ -9,6 +9,7 @@ * Copyright (c) 2019 Damien P. George */ +#include #include #define DESC_STR_MAX 20 @@ -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 = { @@ -71,7 +73,6 @@ static const uint8_t usbd_desc_cfg[USBD_DESC_LEN] = { static const char *const usbd_desc_str[] = { [USBD_STR_MANUF] = "Raspberry Pi", [USBD_STR_PRODUCT] = "Pico", - [USBD_STR_SERIAL] = "000000000000", [USBD_STR_CDC] = "Board CDC", }; @@ -95,11 +96,23 @@ 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; - str = usbd_desc_str[index]; + if (index == USBD_STR_SERIAL) { + uint8_t id[8]; + + flash_get_unique_id(id); + sprintf(serial, "%02X%02X%02X%02X%02X%02X%02X%02X", + id[0], id[1], id[2], id[3], id[4], id[5], id[6], id[7]); + + str = serial; + } else { + str = usbd_desc_str[index]; + } + for (len = 0; len < DESC_STR_MAX - 1 && str[len]; ++len) desc_str[1 + len] = str[len]; }