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

USB CDC and FreeRTOS

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

 

This topic has been closed for replies.
FBLBest answer
ST 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)
{

}

 

To give better visibility on the answered topics, please click on "Best answer" on the reply which solved your issue or answered your question.Best regards,FBL

4 replies

Pavel A.
August 1, 2024

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

 

ST 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.

To give better visibility on the answered topics, please click on "Best answer" on the reply which solved your issue or answered your question.Best regards,FBL
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
ST 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)
{

}

 

To give better visibility on the answered topics, please click on "Best answer" on the reply which solved your issue or answered your question.Best regards,FBL