cancel
Showing results for 
Search instead for 
Did you mean: 

[CubeMX feature request]Add user code section to the Get_SerialNum of the HAL based USB slave code

martonmiklos
Senior

Dear CubeMX developers!

I would like to suggest you to add an user code section around the function body of the Get_SerialNum in the usbd_desc.c file generated with the CubeMX for the STM32F1 family:

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)
  {
    IntToUnicode(deviceserial0, &USBD_StringSerial[2], 8);
    IntToUnicode(deviceserial1, &USBD_StringSerial[18], 4);
  }
}

As seen in the snipplet above the current implementation puts the die ID to the serial number.

I think it is widespread to program an own unique serial number to the device at the manufacturing stage. In this case the user needs to override this function which got overwritten each time when the CubeMX generates code. Introducing a new user section here would solve this use case. Example snipplet (after adding custom serial number:)

static void Get_SerialNum(void)
{
  /* USER CODE BEGIN Get_SerialNum */
  uint16_t length;
  USBD_GetString((uint8_t *)serialNumberStr, USBD_StringSerial, &length);
  /* USER CODE END Get_SerialNum */
}

Thanks in advance!

3 REPLIES 3
Khouloud ZEMMELI
ST Employee

​Hello @martonmiklos​ ,

This is reported internally for further check.

Best Regards,

Khouloud.

Cool, thanks!

martonmiklos
Senior

I just seen that there is an USBD_FS_SerialStrDescriptor section there, so I consider this issue partially solved. For the shake of optimalization I would suggest to move the Get_SerialNum(); call to the user section. In this case if the end user wants to use their own serial number (which is not dependant on the device's ID) could easily replace it and it won't consume program memory.