@@ -233,18 +233,18 @@ uint8_t *USBD_Class_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *le
233233 */
234234static 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
0 commit comments