Skip to content

Commit f98e02c

Browse files
robert-hhpfalcon
authored andcommitted
rp2/tusb_port: Add the device unique-id to the USB id.
The number shown in the USB id is now the same as that returned by machine.unique_id(). All 8 bytes are inserted as hex into the USB id. A usb id at /dev/serial/by-id then looks like: usb-MicroPython_Board_in_FS_mode_e469b03567342f37-if00
1 parent 0728040 commit f98e02c

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

ports/rp2/tusb_port.c

+17-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626

2727
#include "tusb.h"
28+
#include "pico/unique_id.h"
2829

2930
#define USBD_VID (0x2E8A) // Raspberry Pi
3031
#define USBD_PID (0x0005) // RP2 MicroPython
@@ -77,7 +78,7 @@ static const uint8_t usbd_desc_cfg[USBD_DESC_LEN] = {
7778
static const char *const usbd_desc_str[] = {
7879
[USBD_STR_MANUF] = "MicroPython",
7980
[USBD_STR_PRODUCT] = "Board in FS mode",
80-
[USBD_STR_SERIAL] = "000000000000", // TODO
81+
[USBD_STR_SERIAL] = NULL, // generated dynamically
8182
[USBD_STR_CDC] = "Board CDC",
8283
};
8384

@@ -102,9 +103,21 @@ const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
102103
if (index >= sizeof(usbd_desc_str) / sizeof(usbd_desc_str[0])) {
103104
return NULL;
104105
}
105-
const char *str = usbd_desc_str[index];
106-
for (len = 0; len < DESC_STR_MAX - 1 && str[len]; ++len) {
107-
desc_str[1 + len] = str[len];
106+
// check, if serial is requested
107+
if (index == USBD_STR_SERIAL) {
108+
pico_unique_board_id_t id;
109+
pico_get_unique_board_id(&id);
110+
// byte by byte conversion
111+
for (len = 0; len < 16; len += 2) {
112+
const char *hexdig = "0123456789abcdef";
113+
desc_str[1 + len] = hexdig[id.id[len >> 1] >> 4];
114+
desc_str[1 + len + 1] = hexdig[id.id[len >> 1] & 0x0f];
115+
}
116+
} else {
117+
const char *str = usbd_desc_str[index];
118+
for (len = 0; len < DESC_STR_MAX - 1 && str[len]; ++len) {
119+
desc_str[1 + len] = str[len];
120+
}
108121
}
109122
}
110123

0 commit comments

Comments
 (0)