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 <angus@redyak.com.au>
This commit is contained in:
Angus Gratton
2023-11-08 10:34:57 +11:00
committed by Damien George
parent 5e3f0e7f85
commit f567a9255a
4 changed files with 19 additions and 18 deletions

View File

@@ -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