retrieve serial from flash ID

This commit is contained in:
cxxcoder 2022-07-04 12:38:41 +02:00
parent 05e4815f6b
commit 758cec2b5f
4 changed files with 24 additions and 2 deletions

View file

@ -17,6 +17,7 @@ target_include_directories(uart_bridge PUBLIC
target_link_libraries(uart_bridge
pico_multicore
pico_stdlib
hardware_flash
tinyusb_device)
pico_add_extra_outputs(uart_bridge)

3
serial.h Normal file
View file

@ -0,0 +1,3 @@
#pragma once
extern char serial[17];

View file

@ -10,6 +10,9 @@
#include <pico/stdlib.h>
#include <string.h>
#include <tusb.h>
#include <hardware/flash.h>
#include "serial.h"
#if !defined(MIN)
#define MIN(a, b) ((a > b) ? b : a)
@ -263,12 +266,23 @@ void init_uart_data(uint8_t itf) {
parity_usb2uart(ud->usb_lc.parity));
}
static inline void init_serial() {
uint8_t id[8];
flash_get_unique_id(id);
for (int i = 0; i < 8; ++i) {
sprintf(serial + 2 * i, "%X", id[i]);
}
serial[16] = '\0';
}
int main(void)
{
int itf;
set_sys_clock_khz(250000, false);
init_serial();
for (itf = 0; itf < CFG_TUD_CDC; itf++)
init_uart_data(itf);

View file

@ -11,6 +11,8 @@
#include <tusb.h>
#include "serial.h"
#define DESC_STR_MAX 20
#define USBD_VID 0x2E8A /* Raspberry Pi */
@ -68,10 +70,12 @@ static const uint8_t usbd_desc_cfg[USBD_DESC_LEN] = {
USBD_CDC_IN_OUT_MAX_SIZE),
};
static const char *const usbd_desc_str[] = {
char serial[17];
static char *const usbd_desc_str[] = {
[USBD_STR_MANUF] = "Raspberry Pi",
[USBD_STR_PRODUCT] = "Pico",
[USBD_STR_SERIAL] = "000000000000",
[USBD_STR_SERIAL] = serial,
[USBD_STR_CDC] = "Board CDC",
};