cancel
Showing results for 
Search instead for 
Did you mean: 

STM32CubeMX USB Serial Number String from UID

Waldemar Faist
Associate II
Posted on March 30, 2018 at 18:03

Hello,

I have a device based on STM32F1 series that's using the USB CDC interface. TrueSTUDIO project is generated with STM32CubeMX. Everything is working fine except one last thing: Setting the serial number string from code. I can set the serial number string from STM32CubeMX or by overriding the define in Code

#define USBD_SERIALNUMBER_STRING_FS '00000000001A'�?

But I need the string to be unique per device, so using the MCU UID to generate an unique number would be the best solution. Generated parts are:

[...]
USBD_DescriptorsTypeDef FS_Desc =
{
 USBD_FS_DeviceDescriptor
, USBD_FS_LangIDStrDescriptor
, USBD_FS_ManufacturerStrDescriptor
, USBD_FS_ProductStrDescriptor
, USBD_FS_SerialStrDescriptor
, USBD_FS_ConfigStrDescriptor
, USBD_FS_InterfaceStrDescriptor
};
[...]
uint8_t * USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
{
 if(speed == USBD_SPEED_HIGH)
 {
 USBD_GetString((uint8_t *)USBD_SERIALNUMBER_STRING_FS, USBD_StrDesc, length);
 }
 else
 {
 USBD_GetString((uint8_t *)USBD_SERIALNUMBER_STRING_FS, USBD_StrDesc, length);
 }
 return USBD_StrDesc;
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?
[...]�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

I see no way to insert user code into theUSBD_FS_SerialStrDescriptor function, since there is no USER CODE field. What is the

STM32CubeMX

way to use a serial number from the UID? Am I missing something here? #usb-cdc #usb-device #stm32cubemx
7 REPLIES 7
Waldemar Faist
Associate II
Posted on April 09, 2018 at 18:28

Anyone? Someone from ST? There must be a intended way, to use the UID as USB serial. 

Posted on April 09, 2018 at 19:11

The compiler builds the code it is presented.

Just add your own code to convert the part's Unique ID into a string and provide that as a descriptor. The goal with CubeMX is that you don't keep pressing the 'rebuild button' over and over, rather that you keep your working project in it's own safe tree, and sandbox experimental/incremental changes which you can then merge into the main tree.

>>There must be a intended way, to use the UID as USB serial. 

I wouldn't assume that anyone is really thinking this out beyond the horizon of their own task list.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Ben K
Senior III
Posted on April 10, 2018 at 00:46

It's funny that all the USB Device example projects in the Cube repository use the device UID as USB serial, and yet the code generator is incapable of providing such solution.

You could simply copy the usbd_desc.c file from e.g. {F1 repo}/Projects/STM3210E_EVAL/Applications/USB_Device/CDC_Standalone/Src to your project and call USBD_Init() with VCP_Desc parameter instead of FS_Desc.

Waldemar Faist
Associate II
Posted on April 10, 2018 at 10:19

Turvey.Clive.002

‌ So, CubeMX was not designed to regenerate the code once the project has been generated? I doubt that, it would make it useless, since requirements are changing in the development process. But that's not the point, I don't expect that CubeMX generates me a proper function to use the UID as serial, but I do expect that I can override the default behaviour of using the define as serial. As I mentioned, there is no

USER CODEarea in usbd_desc.c and especially not in theUSBD_FS_SerialStrDescriptor function which is responsible to build the serial portion of FS_Desc. If I make any changes to the settings in CubeMX and regenerate the project while 'Keep User Code when re-generating' is set, I'm loosing everything that is not in an

USER CODE area.

Kupper.Benedek

‌ Unfortunatelythese examples are not for CubeMX. If I just copy the whole file, it would be just replaced on the next re-generate from CubeMX.

Posted on April 10, 2018 at 10:47

Unfortunately these examples are not for CubeMX. If I just copy the whole file, it would be just replaced on the next re-generate from CubeMX.

It's only true if you put it in the same directory as the generated file.

Posted on April 10, 2018 at 11:10

Of course, but I don't see the point. If I move my own 

usbd_desc.c or just rename it, I would need to include it at least from usb_device.c which is generated or edit the project's includes which are generated. I mean almost every other generated function from CubeMX provides USER CODE areas and I had no problems at all to implement everything I need there. It seems 

usbd_desc.c kinda does not fit the pattern.

martonmiklos
Senior

It is also entertaining to see that that the two cases of the speed check if does the same 🙂