Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand All @@ -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)
50 changes: 50 additions & 0 deletions src/get_serial.c
Original file line number Diff line number Diff line change
@@ -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 <stdint.h>
#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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or you could just use sprintf ?

Copy link
Contributor Author

@newbrain newbrain Feb 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With a cast to uint64_t?
Yes, type punning would in this case be 100% safe (and even guaranteed by the standard), and printf is already brought in so that won't make much of a difference.
But, it will display the flash ID in reverse with respect to the "wire order". Not that it makes that much of a difference, I just find it weird.

Otherwise, it would be sprintf in a loop on the id[] array (quite an expensive thing) or a monster call (ugly), and not really any simpler in both cases - as you can see it's four simple lines.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sprintf in a loop on the id[] array (quite an expensive thing)

With the Pico running at 133MHz, I guess I was just being lazy 😉

}
}
35 changes: 35 additions & 0 deletions src/get_serial.h
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -41,6 +42,7 @@
int main(void) {

board_init();
usb_serial_init();
cdc_uart_init();
tusb_init();
probe_init();
Expand Down
3 changes: 2 additions & 1 deletion src/usb_descriptors.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/

#include "tusb.h"
#include "get_serial.h"


//--------------------------------------------------------------------+
Expand Down Expand Up @@ -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];
Expand Down