2024-08-01 01:18 PM - edited 2024-08-02 03:25 AM
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
Solved! Go to Solution.
2024-08-05 01:51 AM
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 Accept as Solution on the reply which solved your issue or answered your question.
2024-08-01 02:29 PM
Yes. If you don't know why your software works, you have a potential risk.
2024-08-02 02:18 AM
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 Accept as Solution on the reply which solved your issue or answered your question.
2024-08-02 03:21 AM - edited 2024-08-02 03:26 AM
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
2024-08-05 01:51 AM
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 Accept as Solution on the reply which solved your issue or answered your question.