From 77724a6d9a1f5bf8db690b39c608843974f65fcd Mon Sep 17 00:00:00 2001 From: whowechina Date: Tue, 10 Oct 2023 22:34:08 +0800 Subject: [PATCH] Use pico's board id as USB SN --- firmware/src/CMakeLists.txt | 2 +- firmware/src/usb_descriptors.c | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/firmware/src/CMakeLists.txt b/firmware/src/CMakeLists.txt index 574aca1..2685fa6 100644 --- a/firmware/src/CMakeLists.txt +++ b/firmware/src/CMakeLists.txt @@ -20,7 +20,7 @@ function(make_firmware board board_def) target_link_libraries(${board} PRIVATE pico_multicore pico_stdlib hardware_pio hardware_pwm hardware_flash - hardware_adc hardware_i2c hardware_watchdog + hardware_adc hardware_i2c hardware_watchdog pico_unique_id tinyusb_device tinyusb_board) pico_add_extra_outputs(${board}) diff --git a/firmware/src/usb_descriptors.c b/firmware/src/usb_descriptors.c index 8a9fc3f..eb601d1 100644 --- a/firmware/src/usb_descriptors.c +++ b/firmware/src/usb_descriptors.c @@ -24,7 +24,7 @@ */ #include "usb_descriptors.h" - +#include "pico/unique_id.h" #include "tusb.h" /* A combination of interfaces must have a unique product id, since PC will save @@ -173,12 +173,14 @@ uint8_t const* tud_descriptor_configuration_cb(uint8_t index) { // String Descriptors //--------------------------------------------------------------------+ +static char serial_number_str[24] = "123456\0"; + // array of pointer to string descriptors -const char *string_desc_arr[] = { +static const char *string_desc_arr[] = { (const char[]){0x09, 0x04}, // 0: is supported language is English (0x0409) "WHowe" , // 1: Manufacturer "Mai Pico Controller", // 2: Product - "123456", // 3: Serials, should use chip ID + serial_number_str, // 3: Serials, use chip ID "Mai Pico Joystick", "Mai Pico LED", "Mai Pico NKRO", @@ -200,6 +202,12 @@ uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid) return _desc_str; } + if (index == 3) { + pico_unique_board_id_t board_id; + pico_get_unique_board_id(&board_id); + sprintf(serial_number_str, "%016llx", board_id); + } + const size_t base_num = sizeof(string_desc_arr) / sizeof(string_desc_arr[0]); const char *colors[] = {"Blue", "Red", "Green"}; char str[64];