Skip to main content
MK..1
Associate III
August 1, 2024
Solved

USB CDC and FreeRTOS

  • August 1, 2024
  • 3 replies
  • 1341 views

Hi

in different posts in forum I read ST's USB library is not thread safe. There are no examples with USB CDC and FreeRTOS.

Is that true?

Thx

 

Best answer by FBL

Hi @MK..1 

ST USB middleware is not explicitly using malloc() and free(), but it is crucial to configure it to use static memory allocation to ensure thread safety.

/**
 * @brief Static single allocation.
 * @param size: Size of allocated memory
 * @retval None
 */
void *USBD_static_malloc(uint32_t size)
{
 static uint32_t mem[(sizeof(USBD_CDC_HandleTypeDef)/4)+1];/* On 32-bit boundary */
 return mem;
}

/**
 * @brief Dummy memory free
 * @param p: Pointer to allocated memory address
 * @retval None
 */
void USBD_static_free(void *p)
{

}

 

3 replies

Pavel A.
Super User
August 1, 2024

Yes. If you don't know why your software works, you have a potential risk.

 

Technical Moderator
August 2, 2024

Hi @MK..1 

Usage of malloc(), free(), or any other function implicitly calling these functions is not safe in multi-threaded systems even in bare metal or RTOS systems. AN5731 provides some guidelines for ensuring thread safety.

MK..1
MK..1Author
Associate III
August 2, 2024

Hi @FBL 

how does AN5731 relate to ST USB library as it is not using malloc()?

I didn't find any malloc or free in the ST USB library!

Is there a specific reason why there is no CDC and FreeRTOS example project?

Thanks

FBLBest answer
Technical Moderator
August 5, 2024

Hi @MK..1 

ST USB middleware is not explicitly using malloc() and free(), but it is crucial to configure it to use static memory allocation to ensure thread safety.

/**
 * @brief Static single allocation.
 * @param size: Size of allocated memory
 * @retval None
 */
void *USBD_static_malloc(uint32_t size)
{
 static uint32_t mem[(sizeof(USBD_CDC_HandleTypeDef)/4)+1];/* On 32-bit boundary */
 return mem;
}

/**
 * @brief Dummy memory free
 * @param p: Pointer to allocated memory address
 * @retval None
 */
void USBD_static_free(void *p)
{

}