From 0fe6a09d8bcd8e620ba7f0939cacda0dbefc821e Mon Sep 17 00:00:00 2001 From: newbrain Date: Wed, 3 Feb 2021 10:19:04 +0100 Subject: [PATCH] Unique serial numbers for picoprobe (#8) Co-authored-by: newbrain Co-authored-by: Liam Fraser --- CMakeLists.txt | 3 ++- src/get_serial.c | 50 +++++++++++++++++++++++++++++++++++++++++++ src/get_serial.h | 35 ++++++++++++++++++++++++++++++ src/main.c | 2 ++ src/usb_descriptors.c | 3 ++- 5 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 src/get_serial.c create mode 100644 src/get_serial.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 7629d9f..252bf4e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,7 @@ add_executable(picoprobe src/usb_descriptors.c src/probe.c src/cdc_uart.c + src/get_serial.c ) if (DEFINED ENV{PICOPROBE_LED}) @@ -25,6 +26,6 @@ pico_generate_pio_header(picoprobe ${CMAKE_CURRENT_LIST_DIR}/src/probe.pio) target_include_directories(picoprobe PRIVATE src) -target_link_libraries(picoprobe PRIVATE pico_stdlib tinyusb_device tinyusb_board hardware_pio) +target_link_libraries(picoprobe PRIVATE pico_stdlib pico_unique_id tinyusb_device tinyusb_board hardware_pio) pico_add_extra_outputs(picoprobe) diff --git a/src/get_serial.c b/src/get_serial.c new file mode 100644 index 0000000..7bf72e3 --- /dev/null +++ b/src/get_serial.c @@ -0,0 +1,50 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2021 Federico Zuccardi Merli + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + +#include +#include "pico/unique_id.h" +#include "get_serial.h" + +/* C string for iSerialNumber in USB Device Descriptor, two chars per byte + terminating NUL */ +char usb_serial[PICO_UNIQUE_BOARD_ID_SIZE_BYTES * 2 + 1]; + +/* Why a uint8_t[8] array inside a struct instead of an uint64_t an inquiring mind might wonder */ +static pico_unique_board_id_t uID; + +void usb_serial_init(void) +{ + pico_get_unique_board_id(&uID); + + for (int i = 0; i < PICO_UNIQUE_BOARD_ID_SIZE_BYTES * 2; i++) + { + /* Byte index inside the uid array */ + int bi = i / 2; + /* Use high nibble first to keep memory order (just cosmetics) */ + uint8_t nibble = (uID.id[bi] >> 4) & 0x0F; + uID.id[bi] <<= 4; + /* Binary to hex digit */ + usb_serial[i] = nibble < 10 ? nibble + '0' : nibble + 'A' - 10; + } +} diff --git a/src/get_serial.h b/src/get_serial.h new file mode 100644 index 0000000..ebad45e --- /dev/null +++ b/src/get_serial.h @@ -0,0 +1,35 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2021 Federico Zuccardi Merli + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + +#ifndef GET_SERIAL_H_ +#define GET_SERIAL_H_ + +/* Contains unique serial number string (NUL terminated) after call to init_usb_serial */ +extern char usb_serial[]; + +/* Fills unique_serial with the flash unique id */ +extern void usb_serial_init(void); + +#endif diff --git a/src/main.c b/src/main.c index b67bef7..790386b 100644 --- a/src/main.c +++ b/src/main.c @@ -33,6 +33,7 @@ #include "picoprobe_config.h" #include "probe.h" #include "cdc_uart.h" +#include "get_serial.h" #include "led.h" // UART0 for Picoprobe debug @@ -41,6 +42,7 @@ int main(void) { board_init(); + usb_serial_init(); cdc_uart_init(); tusb_init(); probe_init(); diff --git a/src/usb_descriptors.c b/src/usb_descriptors.c index eddfa7a..dd6b4f0 100644 --- a/src/usb_descriptors.c +++ b/src/usb_descriptors.c @@ -24,6 +24,7 @@ */ #include "tusb.h" +#include "get_serial.h" //--------------------------------------------------------------------+ @@ -106,7 +107,7 @@ char const* string_desc_arr [] = (const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409) "Raspberry Pi", // 1: Manufacturer "Picoprobe", // 2: Product - "123456", // 3: Serials, should use chip ID + usb_serial, // 3: Serial, uses flash unique ID }; static uint16_t _desc_str[32];