AnsweredAssumed Answered

STM32CubeMX USB CDC(com port) BUG

Question asked by hosken.david on Jul 14, 2015
Latest reply on Jul 16, 2015 by hosken.david
The USBD_static_malloc() function in "usbd_conf.c" is broken! Can someone at ST please fix this! Causes USB CDC to NOT work.

This function currently looks like this:

void *USBD_static_malloc(uint32_t size)
{
  //static uint32_t mem[sizeof(USBD_CDC_HandleTypeDef)];
  static uint8_t mem[512];
  return mem;
}

It is used to get size of (and create object) USBD_CDC_HandleTypeDef. This is done in "usbd_conf.c":
pdev->pClassData = USBD_malloc(sizeof (USBD_CDC_HandleTypeDef));

Problem is that USBD_CDC_HandleTypeDef is larger than 512. See definition in "usbd_conf.h". This causes very hard to find memory corruption when writing to members of USBD_CDC_HandleTypeDef!

Also, this serious bug has been reported couple of months earlier:
https://my.st.com/8013a5af

The commented line in USBD_static_malloc() seems nearly correct. Not sure why it was replaced by fixed definition of 512 bytes? Following should work
void *USBD_static_malloc(uint32_t size)
{
  static uint32_t mem[(sizeof(USBD_CDC_HandleTypeDef)/4)+1];  //On 32-bit boundary
  return mem;
}





Outcomes