Skip to main content
Alvin Sun
Visitor II
January 16, 2021
Question

Dynamic memory allocation in USBD_CDC_Init

  • January 16, 2021
  • 1 reply
  • 924 views

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.

This topic has been closed for replies.

1 reply

Piranha
Principal III
January 19, 2021

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