cancel
Showing results for 
Search instead for 
Did you mean: 

DFU USB serial number and runtime STCubeMX generated USB serial number differ.

Areslane
Visitor

When connecting via USB to a STM32 board in DFU mode, it is possible to read a serial number from the instance path.

However, in runtime, the serial number exposed via USB must be programed by the user... It would be nice then if ST provided the algorithm they used to create this string.

We do have some hints though. When generating a STCube project, by default a `Get_SerialNum` function is generated in usbd_desc.c.

However, it does not produce the same string as the bootloader does.

 

Can anyone please tell us what is the algorithm stored in ROM of each type STM32 to generate the USB serial number string based on the 96-bit UID?

On STM32F76/STM32F77, I have found that this function outputs a string that matches the one produced in ROM:

`static void Get_SerialNum(void)

{
    uint32_t deviceserial0, deviceserial1, deviceserial2;

    deviceserial0 = *(uint32_t *)DEVICE_ID1;
    deviceserial1 = *(uint32_t *)DEVICE_ID2;
    deviceserial2 = *(uint32_t *)DEVICE_ID3;

    deviceserial0 += deviceserial2;

    if (deviceserial0 != 0)
    {
        // --- Apply offset to mimic DFU vs APP difference ---
        // Example: add 0xA to the lowest byte of deviceserial0
        uint8_t *p = (uint8_t *)&deviceserial0;
        p[0] = (uint8_t)(p[0] + 0x0A);

        // Now convert to Unicode string as before
        IntToUnicode(deviceserial0, &USBD_StringSerial[2], 8);
        IntToUnicode(deviceserial1, &USBD_StringSerial[18], 4);
    }
}`
0 REPLIES 0