Skip to content

Commit affcbe4

Browse files
dhylandsdpgeorge
authored andcommitted
stmhal: Make USB serial number actually be unique.
1 parent 6a515b9 commit affcbe4

1 file changed

Lines changed: 20 additions & 7 deletions

File tree

stmhal/usbd_desc.c

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@
4040
#define USBD_LANGID_STRING 0x409
4141
#define USBD_MANUFACTURER_STRING "MicroPython"
4242
#define USBD_PRODUCT_HS_STRING "Pyboard Virtual Comm Port in HS Mode"
43-
#define USBD_SERIALNUMBER_HS_STRING "000000000010"
4443
#define USBD_PRODUCT_FS_STRING "Pyboard Virtual Comm Port in FS Mode"
45-
#define USBD_SERIALNUMBER_FS_STRING "000000000011"
4644
#define USBD_CONFIGURATION_HS_STRING "Pyboard Config"
4745
#define USBD_INTERFACE_HS_STRING "Pyboard Interface"
4846
#define USBD_CONFIGURATION_FS_STRING "Pyboard Config"
@@ -158,11 +156,26 @@ STATIC uint8_t *USBD_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t
158156
* @retval Pointer to descriptor buffer
159157
*/
160158
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);
166179
return USBD_StrDesc;
167180
}
168181

0 commit comments

Comments
 (0)