From f567a9255a2f801c8e47d8a1468f1c0415971fe4 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Wed, 8 Nov 2023 10:34:57 +1100 Subject: [PATCH] shared/tinyusb: Add a helper for hex string conversion. Change the rp2 and renesas-ra ports to use the helper function. Saves copy-pasta, at the small cost of one more function call in the firmware (if not using LTO). This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton --- ports/renesas-ra/usbd.c | 11 ++--------- ports/rp2/usbd.c | 11 ++--------- shared/tinyusb/mp_usbd.c | 10 ++++++++++ shared/tinyusb/mp_usbd.h | 5 +++++ 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/ports/renesas-ra/usbd.c b/ports/renesas-ra/usbd.c index 7047503864..e7252b5f7a 100644 --- a/ports/renesas-ra/usbd.c +++ b/ports/renesas-ra/usbd.c @@ -34,15 +34,8 @@ void mp_usbd_port_get_serial_number(char *serial_buf) { const bsp_unique_id_t *id = R_BSP_UniqueIdGet(); - // convert to hex - int hexlen = sizeof(id->unique_id_bytes) * 2; - MP_STATIC_ASSERT(hexlen <= MICROPY_HW_USB_DESC_STR_MAX); - for (int i = 0; i < hexlen; i += 2) { - static const char *hexdig = "0123456789abcdef"; - serial_buf[i] = hexdig[id->unique_id_bytes[i / 2] >> 4]; - serial_buf[i + 1] = hexdig[id->unique_id_bytes[i / 2] & 0x0f]; - } - serial_buf[hexlen] = 0; + MP_STATIC_ASSERT(sizeof(id->unique_id_bytes) * 2 <= MICROPY_HW_USB_DESC_STR_MAX); + mp_usbd_hex_str(serial_buf, id->unique_id_bytes, sizeof(id->unique_id_bytes)); } #endif diff --git a/ports/rp2/usbd.c b/ports/rp2/usbd.c index 568b628416..8724f802f0 100644 --- a/ports/rp2/usbd.c +++ b/ports/rp2/usbd.c @@ -36,15 +36,8 @@ void mp_usbd_port_get_serial_number(char *serial_buf) { pico_unique_board_id_t id; pico_get_unique_board_id(&id); - // convert to hex - int hexlen = sizeof(id.id) * 2; - MP_STATIC_ASSERT(hexlen <= MICROPY_HW_USB_DESC_STR_MAX); - for (int i = 0; i < hexlen; i += 2) { - static const char *hexdig = "0123456789abcdef"; - serial_buf[i] = hexdig[id.id[i / 2] >> 4]; - serial_buf[i + 1] = hexdig[id.id[i / 2] & 0x0f]; - } - serial_buf[hexlen] = 0; + MP_STATIC_ASSERT(sizeof(id.id) * 2 <= MICROPY_HW_USB_DESC_STR_MAX); + mp_usbd_hex_str(serial_buf, id.id, sizeof(id.id)); } #endif diff --git a/shared/tinyusb/mp_usbd.c b/shared/tinyusb/mp_usbd.c index 55af3d4fb4..74b3f07492 100644 --- a/shared/tinyusb/mp_usbd.c +++ b/shared/tinyusb/mp_usbd.c @@ -62,4 +62,14 @@ static void mp_usbd_task_callback(mp_sched_node_t *node) { mp_usbd_task(); } +void mp_usbd_hex_str(char *out_str, const uint8_t *bytes, size_t bytes_len) { + size_t hex_len = bytes_len * 2; + for (int i = 0; i < hex_len; i += 2) { + static const char *hexdig = "0123456789abcdef"; + out_str[i] = hexdig[bytes[i / 2] >> 4]; + out_str[i + 1] = hexdig[bytes[i / 2] & 0x0f]; + } + out_str[hex_len] = 0; +} + #endif diff --git a/shared/tinyusb/mp_usbd.h b/shared/tinyusb/mp_usbd.h index 340637c95f..83a8f8617c 100644 --- a/shared/tinyusb/mp_usbd.h +++ b/shared/tinyusb/mp_usbd.h @@ -36,4 +36,9 @@ void mp_usbd_task(void); // Can write a string up to MICROPY_HW_USB_DESC_STR_MAX characters long, plus terminating byte. extern void mp_usbd_port_get_serial_number(char *buf); +// Most ports need to write a hexadecimal serial number from a byte array, this +// is a helper function for this. out_str must be long enough to hold a string of total +// length (2 * bytes_len + 1) (including NUL terminator). +void mp_usbd_hex_str(char *out_str, const uint8_t *bytes, size_t bytes_len); + #endif // MICROPY_INCLUDED_SHARED_TINYUSB_USBD_H