summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrobert-hh <robert@hammelrath.com>2021-04-29 16:56:28 +0200
committerDamien George <damien@micropython.org>2021-05-02 23:38:01 +1000
commit1e2f0d2809c5431c9baf1d6f447162013f58baac (patch)
tree995ae4cd556de824599da8e8ced8b678f8e5b86f
parentd80a037e6bdcc78fbda76ce04e9ded41f335c635 (diff)
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
-rw-r--r--ports/rp2/tusb_port.c21
1 files changed, 17 insertions, 4 deletions
diff --git a/ports/rp2/tusb_port.c b/ports/rp2/tusb_port.c
index 94856ab45..874d837b9 100644
--- a/ports/rp2/tusb_port.c
+++ b/ports/rp2/tusb_port.c
@@ -25,6 +25,7 @@
*/
#include "tusb.h"
+#include "pico/unique_id.h"
#define USBD_VID (0x2E8A) // Raspberry Pi
#define USBD_PID (0x0005) // RP2 MicroPython
@@ -77,7 +78,7 @@ static const uint8_t usbd_desc_cfg[USBD_DESC_LEN] = {
static const char *const usbd_desc_str[] = {
[USBD_STR_MANUF] = "MicroPython",
[USBD_STR_PRODUCT] = "Board in FS mode",
- [USBD_STR_SERIAL] = "000000000000", // TODO
+ [USBD_STR_SERIAL] = NULL, // generated dynamically
[USBD_STR_CDC] = "Board CDC",
};
@@ -102,9 +103,21 @@ const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
if (index >= sizeof(usbd_desc_str) / sizeof(usbd_desc_str[0])) {
return NULL;
}
- const char *str = usbd_desc_str[index];
- for (len = 0; len < DESC_STR_MAX - 1 && str[len]; ++len) {
- desc_str[1 + len] = str[len];
+ // check, if serial is requested
+ if (index == USBD_STR_SERIAL) {
+ pico_unique_board_id_t id;
+ pico_get_unique_board_id(&id);
+ // byte by byte conversion
+ for (len = 0; len < 16; len += 2) {
+ const char *hexdig = "0123456789abcdef";
+ desc_str[1 + len] = hexdig[id.id[len >> 1] >> 4];
+ desc_str[1 + len + 1] = hexdig[id.id[len >> 1] & 0x0f];
+ }
+ } else {
+ const char *str = usbd_desc_str[index];
+ for (len = 0; len < DESC_STR_MAX - 1 && str[len]; ++len) {
+ desc_str[1 + len] = str[len];
+ }
}
}