usb-descriptors: use flash ID as USB serial

Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
This commit is contained in:
cxxcoder 2022-11-03 18:12:56 +01:00 committed by Álvaro Fernández Rojas
parent 05e4815f6b
commit 6aa7cf2958
4 changed files with 21 additions and 1 deletions

View file

@ -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)

View file

@ -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_ */

View file

@ -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);

View file

@ -9,6 +9,7 @@
* Copyright (c) 2019 Damien P. George
*/
#include <hardware/flash.h>
#include <tusb.h>
#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 = {
@ -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",
};
@ -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;
@ -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]);
}