cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamic memory allocation in USBD_CDC_Init

Alvin Sun
Associate

MCU: stm32f427

Firmware Version: F4_v1.25.2

CubeMX Version: 6.0.1

The generated code for USB Virtual Com Class dynamically allocates memory (calls malloc) in USBD_CDC_Init. However, this init function is called under a IRQ context, and I don't think doing malloc in an interrupt handler is a wise choice.

Could you please consider making it static in future firmware releases? I don't see a necessity in dynamically allocating a handle object.

I encountered this when trying to retarget malloc to the FreeRTOS routine using the compiler option -Wl,--wrap=malloc. The reason for that is to interface with the Eigen library. However, if I do such redirection, a pvPortMalloc is called by this USBD_CDC_Init in an IRQ handler, and the program hangs due to incorrect locking under interrupt context.

1 REPLY 1
Piranha
Chief II

Even if ST will remove the malloc() call, it will not fix the ridiculous design of processing everything in ISR. The real solution is a stack designed by competent people - TinyUSB:

https://github.com/hathach/tinyusb