diff options
| author | hathach <[email protected]> | 2023-08-03 20:42:34 +0700 |
|---|---|---|
| committer | hathach <[email protected]> | 2023-08-03 20:42:34 +0700 |
| commit | 041f510f909f0ce8923389c847a8d55e0b7eeee2 (patch) | |
| tree | cb669dc6e1f4945898f2f0822a86731c293d3dd0 /hw/bsp/board_api.h | |
| parent | 1324c2862df921d9634e90a78048f5d71a77d894 (diff) | |
add board_get_unique_id() for serial number
implemented board_get_unique_id() for rp2040 and L4
Diffstat (limited to 'hw/bsp/board_api.h')
| -rw-r--r-- | hw/bsp/board_api.h | 40 |
1 files changed, 36 insertions, 4 deletions
diff --git a/hw/bsp/board_api.h b/hw/bsp/board_api.h index 7249f618d..53296cd56 100644 --- a/hw/bsp/board_api.h +++ b/hw/bsp/board_api.h @@ -60,12 +60,13 @@ void board_led_write(bool state); // a '1' means active (pressed), a '0' means inactive. uint32_t board_button_read(void); -// Get characters from UART -// Return number of read bytes +// Get board unique ID for USB serial number. Return number of bytes. Note max_len is typically 16 +TU_ATTR_WEAK size_t board_get_unique_id(uint8_t id[], size_t max_len); + +// Get characters from UART. Return number of read bytes int board_uart_read(uint8_t *buf, int len); -// Send characters to UART -// Return number of sent bytes +// Send characters to UART. Return number of sent bytes int board_uart_write(void const *buf, int len); #if CFG_TUSB_OS == OPT_OS_NONE @@ -108,6 +109,37 @@ static inline void board_led_off(void) { board_led_write(false); } +// Get USB Serial number string from unique ID if available. Return number of character. +// Input is string descriptor from index 1 (index 0 is type + len) +static inline size_t board_usb_get_serial(uint16_t desc_str1[], size_t max_chars) { + uint8_t serial_id[16] TU_ATTR_ALIGNED(4); + size_t serial_len; + + if ( board_get_unique_id ) { + serial_len = board_get_unique_id(serial_id, sizeof(serial_id)); + }else { + // fixed serial string is 01234567889ABCDEF + *((uint32_t*)(uintptr_t) serial_id) = 0x67452301; + *((uint32_t*)(uintptr_t) (serial_id+4)) = 0xEFCDAB89; + serial_len = 8; + } + + if ( serial_len > max_chars / 2 ) serial_len = max_chars / 2; + + for ( size_t i = 0; i < serial_len; i++ ) { + for ( size_t j = 0; j < 2; j++ ) { + const char nibble_to_hex[16] = { + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' + }; + uint8_t const nibble = (serial_id[i] >> (j * 4)) & 0xf; + desc_str1[i * 2 + (1 - j)] = nibble_to_hex[nibble]; // UTF-16-LE + } + } + + return 2*serial_len; +} + // TODO remove static inline void board_delay(uint32_t ms) { uint32_t start_ms = board_millis(); |
