Skip to content

Commit 42fba4d

Browse files
committed
update get_serialnum func for usb device descriptor (#497)
1 parent 48cb131 commit 42fba4d

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

stm32-modules/flex-stacker/firmware/host_comms_task/usbd_desc.c

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -233,18 +233,18 @@ uint8_t *USBD_Class_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *le
233233
*/
234234
static void Get_SerialNum(void)
235235
{
236-
uint32_t deviceserial0, deviceserial1, deviceserial2;
236+
uint32_t sg_0, sg_1;
237+
uint32_t pointer = USB_STRING_SERIAL_BASE;
238+
// Serial number is stored in 6 32-bit words
239+
for (uint8_t idx = 0; idx < 4; idx++) {
240+
sg_0 = *(uint32_t *)(pointer + 4);
241+
sg_1 = *(uint32_t *)(pointer);
237242

238-
deviceserial0 = *(uint32_t *)(UID_BASE+4);
239-
deviceserial1 = *(uint32_t *)(UID_BASE+8);
240-
deviceserial2 = *(uint32_t *)(UID_BASE+12);
243+
if (sg_0 == 0U) break;
241244

242-
deviceserial0 += deviceserial2;
243-
244-
if (deviceserial0 != 0U)
245-
{
246-
IntToUnicode(deviceserial0, &USBD_StringSerial[2], 8U);
247-
IntToUnicode(deviceserial1, &USBD_StringSerial[18], 4U);
245+
IntToUnicode(sg_0, &USBD_StringSerial[2 + 16*idx], 4U);
246+
IntToUnicode(sg_1, &USBD_StringSerial[10 + 16*idx], 4U);
247+
pointer += 8;
248248
}
249249
}
250250

@@ -259,20 +259,15 @@ static void IntToUnicode(uint32_t value, uint8_t *pbuf, uint8_t len)
259259
{
260260
uint8_t idx = 0U;
261261

262-
for (idx = 0U ; idx < len ; idx ++)
262+
for (idx = 0U; idx < len; idx++)
263263
{
264-
if (((value >> 28)) < 0xAU)
265-
{
266-
pbuf[ 2U * idx] = (value >> 28) + '0';
267-
}
268-
else
269-
{
270-
pbuf[2U * idx] = (value >> 28) + 'A' - 10U;
271-
}
272-
273-
value = value << 4;
274-
264+
// Extract the most significant byte
265+
pbuf[2U * idx] = (value >> 24) & 0xFF;
266+
// Add a null byte for Unicode (UTF-16)
275267
pbuf[2U * idx + 1] = 0U;
268+
269+
// Shift the value to process the next byte
270+
value = value << 8;
276271
}
277272
}
278273

stm32-modules/flex-stacker/firmware/host_comms_task/usbd_desc.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626

2727
/* Exported types ------------------------------------------------------------*/
2828
/* Exported constants --------------------------------------------------------*/
29-
30-
#define USB_SIZ_STRING_SERIAL 0x1A
29+
#define USB_SIZ_STRING_SERIAL 0x32
30+
#define USB_STRING_SERIAL_BASE 0x0807F800
3131

3232
/* Exported macro ------------------------------------------------------------*/
3333
/* Exported functions ------------------------------------------------------- */

0 commit comments

Comments
 (0)