cancel
Showing results for 
Search instead for 
Did you mean: 

USB CDC and FreeRTOS

MK..1
Associate III

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
FBL
ST Employee

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.

View solution in original post

4 REPLIES 4
Pavel A.
Evangelist III

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

 

FBL
ST Employee

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.

MK..1
Associate III

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

FBL
ST Employee

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.