|
40 | 40 | #define USBD_LANGID_STRING 0x409 |
41 | 41 | #define USBD_MANUFACTURER_STRING "MicroPython" |
42 | 42 | #define USBD_PRODUCT_HS_STRING "Pyboard Virtual Comm Port in HS Mode" |
43 | | -#define USBD_SERIALNUMBER_HS_STRING "000000000010" |
44 | 43 | #define USBD_PRODUCT_FS_STRING "Pyboard Virtual Comm Port in FS Mode" |
45 | | -#define USBD_SERIALNUMBER_FS_STRING "000000000011" |
46 | 44 | #define USBD_CONFIGURATION_HS_STRING "Pyboard Config" |
47 | 45 | #define USBD_INTERFACE_HS_STRING "Pyboard Interface" |
48 | 46 | #define USBD_CONFIGURATION_FS_STRING "Pyboard Config" |
@@ -158,11 +156,26 @@ STATIC uint8_t *USBD_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t |
158 | 156 | * @retval Pointer to descriptor buffer |
159 | 157 | */ |
160 | 158 | STATIC uint8_t *USBD_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { |
161 | | - if(speed == USBD_SPEED_HIGH) { |
162 | | - USBD_GetString((uint8_t *)USBD_SERIALNUMBER_HS_STRING, USBD_StrDesc, length); |
163 | | - } else { |
164 | | - USBD_GetString((uint8_t *)USBD_SERIALNUMBER_FS_STRING, USBD_StrDesc, length); |
165 | | - } |
| 159 | + // This document: http://www.usb.org/developers/docs/devclass_docs/usbmassbulk_10.pdf |
| 160 | + // says that the serial number has to be at least 12 digits long and that |
| 161 | + // the last 12 digits need to be unique. It also stipulates that the valid |
| 162 | + // character set is that of upper-case hexadecimal digits. |
| 163 | + // |
| 164 | + // The onboard DFU bootloader produces a 12-digit serial number based on |
| 165 | + // the 96-bit unique ID, so for consistency we go with this algorithm. |
| 166 | + // You can see the serial number if you do: |
| 167 | + // |
| 168 | + // dfu-util -l |
| 169 | + // |
| 170 | + // See: https://my.st.com/52d187b7 for the algorithim used. |
| 171 | + |
| 172 | + uint8_t *id = (uint8_t *)0x1fff7a10; |
| 173 | + char serial_buf[16]; |
| 174 | + snprintf(serial_buf, sizeof(serial_buf), |
| 175 | + "%02X%02X%02X%02X%02X%02X", |
| 176 | + id[11], id[10] + id[2], id[9], id[8] + id[0], id[7], id[6]); |
| 177 | + |
| 178 | + USBD_GetString((uint8_t *)serial_buf, USBD_StrDesc, length); |
166 | 179 | return USBD_StrDesc; |
167 | 180 | } |
168 | 181 |
|
|
0 commit comments