@@ -42,56 +42,15 @@ static inline void board_vbus_set(uint8_t rhport, bool state);
4242void _init (void );
4343#include "board.h"
4444
45- #ifndef LED_STATE_ON
46- #define LED_STATE_ON 1
47- #endif
48-
49- #ifndef LED_PORT_CLOCK
50- #define LED_PORT_CLOCK ID_PIOA
51- #endif
52-
53- #ifndef BUTTON_PORT_CLOCK
54- #define BUTTON_PORT_CLOCK ID_PIOA
55- #endif
56-
57- #ifndef UART_PORT_CLOCK
58- #define UART_PORT_CLOCK ID_USART1
59- #endif
60-
61- #ifndef BOARD_USART
62- #define BOARD_USART USART1
63- #endif
64-
65- #ifndef BOARD_UART_DESCRIPTOR
66- #define BOARD_UART_DESCRIPTOR edbg_com
67- #endif
68-
69- #ifndef BOARD_UART_BUFFER
70- #define BOARD_UART_BUFFER edbg_com_buffer
71- #endif
72-
73- #ifndef BUTTON_STATE_ACTIVE
74- #define BUTTON_STATE_ACTIVE 0
75- #endif
76-
77- #ifndef UART_TX_FUNCTION
78- #define UART_TX_FUNCTION MUX_PB4D_USART1_TXD1
79- #endif
80-
81- #ifndef UART_RX_FUNCTION
82- #define UART_RX_FUNCTION MUX_PA21A_USART1_RXD1
83- #endif
84-
8545#ifndef UART_BUFFER_SIZE
8646 #define UART_BUFFER_SIZE 64
8747#endif
8848
8949#define LED_STATE_OFF (1 - LED_STATE_ON)
9050
91- static struct usart_async_descriptor BOARD_UART_DESCRIPTOR ;
92- static uint8_t BOARD_UART_BUFFER [UART_BUFFER_SIZE ];
51+ static struct usart_async_descriptor edbg_com ;
52+ static uint8_t edbg_com_buffer [UART_BUFFER_SIZE ];
9353static volatile bool uart_busy = false;
94-
9554static void tx_complete_cb (const struct usart_async_descriptor * const io_descr ) {
9655 (void ) io_descr ;
9756 uart_busy = false;
@@ -121,10 +80,10 @@ void board_init(void) {
12180 gpio_set_pin_function (UART_RX_PIN , UART_RX_FUNCTION );
12281 gpio_set_pin_function (UART_TX_PIN , UART_TX_FUNCTION );
12382
124- usart_async_init (& BOARD_UART_DESCRIPTOR , BOARD_USART , BOARD_UART_BUFFER , sizeof (BOARD_UART_BUFFER ), _usart_get_usart_async ());
125- usart_async_set_baud_rate (& BOARD_UART_DESCRIPTOR , CFG_BOARD_UART_BAUDRATE );
126- usart_async_register_callback (& BOARD_UART_DESCRIPTOR , USART_ASYNC_TXC_CB , tx_complete_cb );
127- usart_async_enable (& BOARD_UART_DESCRIPTOR );
83+ usart_async_init (& edbg_com , BOARD_USART , edbg_com_buffer , sizeof (edbg_com_buffer ), _usart_get_usart_async ());
84+ usart_async_set_baud_rate (& edbg_com , CFG_BOARD_UART_BAUDRATE );
85+ usart_async_register_callback (& edbg_com , USART_ASYNC_TXC_CB , tx_complete_cb );
86+ usart_async_enable (& edbg_com );
12887
12988#if CFG_TUSB_OS == OPT_OS_NONE
13089 // 1ms tick timer (SystemCoreClock may not be correct after init)
@@ -181,10 +140,40 @@ int board_uart_write(void const *buf, int len) {
181140 while (uart_busy ) {}
182141 uart_busy = true;
183142
184- io_write (& BOARD_UART_DESCRIPTOR .io , buf , len );
143+ io_write (& edbg_com .io , buf , len );
185144 return len ;
186145}
187146
147+ // Read 128-bit unique ID via EFC STUI/SPUI commands
148+ // Must run from RAM since STUI remaps flash to the unique ID
149+ __attribute__((noinline )) TU_ATTR_SECTION (.ramfunc ) static void read_unique_id (uint32_t uid [4 ]) {
150+ // Wait for flash to be ready
151+ while (!(EFC -> EEFC_FSR & EEFC_FSR_FRDY )) {}
152+
153+ // Issue Start Read Unique Identifier command
154+ EFC -> EEFC_FCR = EEFC_FCR_FKEY_PASSWD | EEFC_FCR_FCMD_STUI ;
155+ while (EFC -> EEFC_FSR & EEFC_FSR_FRDY ) {}
156+
157+ // Read 128-bit unique ID from flash base address
158+ const volatile uint32_t * flash = (const volatile uint32_t * ) IFLASH_ADDR ;
159+ for (int i = 0 ; i < 4 ; i ++ ) {
160+ uid [i ] = flash [i ];
161+ }
162+
163+ // Issue Stop Read Unique Identifier command
164+ EFC -> EEFC_FCR = EEFC_FCR_FKEY_PASSWD | EEFC_FCR_FCMD_SPUI ;
165+ while (!(EFC -> EEFC_FSR & EEFC_FSR_FRDY )) {}
166+ }
167+
168+ size_t board_get_unique_id (uint8_t id [], size_t max_len ) {
169+ const size_t uid_len = 16 ;
170+ if (max_len < uid_len ) {
171+ return 0 ;
172+ }
173+ read_unique_id ((uint32_t * )(uintptr_t ) id );
174+ return uid_len ;
175+ }
176+
188177#if CFG_TUSB_OS == OPT_OS_NONE
189178volatile uint32_t system_ticks = 0 ;
190179
0 commit comments