2015-07-13 09:05 PM
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; } #stm32cube-usb-cdc-com-port2015-07-15 02:58 AM
Hi modtronix_com,
1-Could you specify in which firmware package and example you found the previous code? 2-Did you try to increase stack and heap sizes (in startup_xxx.s file)? Regards2015-07-15 04:36 PM
This bug is present when generating USB CDC code for STM32L151RC chip. Probably also for other chips. I tested it with STM32CubeMX V4.8 and V4.9. Tested it with STM32CubeL1 v1.2.0 and v1.3.0. I think this faulty code is also generated for other chips. It has been reported previously in this forum. Increasing stack and heap size will not fix bug - please read description of error in my first post.
All ST has to do if fix the faulty USBD_static_malloc() function that is generated when ''Generate Code'' is selected in STM32CubeMX. See my first mail for fix. Only a single line that has to be replaced!