Skip to main content
Associate
January 19, 2025
Solved

FreeRTOS with USB CDC

  • January 19, 2025
  • 4 replies
  • 2670 views

I have read some posts about this topic already but unclear if this works or not. I used the STMCubeMX to create a project using FreeRTOS and USB Device CDC (USB-Serial) together and some LED GPIO's.

I can only get one or the other to work, but never at the same time. I have tried adjusting the memory settings heap/stack as suggested in some of the forums but that didn't help. Just wanted to check if anyone has this working or if I should switch to a different scheduler/OS? I am using a STM32F303RE; any help would be appreciated. Thank you

Best answer by FBL

Hello again @jayduino 

Did you manage to use USB VCP on your F3 standalone (without RTOS)? If not, check the example provided here. It might help you. Also, make sure to use external pull-up resistor on USB_DP, since this line doesn't embed. Otherwise, the USB assumes that there is nothing connected to the bus.

4 replies

Technical Moderator
January 20, 2025

Hi @jayduino 

I don't have example projects for all products. The attached one should be helpful in your case. You need to change the target and all dependencies to port the project properly to STM32F3.

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.Best regards,FBL
jayduinoAuthor
Associate
January 20, 2025

Thank you FBl.

 

I will give it a shot and get back to you!

jayduinoAuthor
Associate
January 24, 2025

I was able to review your example and I didn't see anything different in your code so I tried to run FreeRTOS with USB on another development kit with a STM32F405 and that ran perfectly fine right out of CubeMX!

When I try the same setup on my custom device using the STM32F303RE device (just a default task with osDelay(), no other extra code), it does not work. I also disabled USB and it still did not work. When I try to debug it, it shows that it is getting stuck inside this:

 

Default_Handler:
Infinite_Loop:
b Infinite_Loop
.size Default_Handler, .-Default_Handler

 

When comparing the code between the STM32F4 and the STM32F3, I did notice that 

configTOTAL_HEAP_SIZE ((size_t)15360) was a lot smaller on the F3 device, so I bumped that up to match the 15360 with no change, still getting stuck in the default handler. 

Is there something else that could cause the STM32 to jump into the default handler? The code does make it to the osdelay(), but it never makes it back a second time.

jayduinoAuthor
Associate
January 24, 2025

Never mind, I had to change to CMSIS_V1, and now the code is running but the USB COM port does not work. When I go to open the port, it will not open even though it does show up on my PC. Why would it work on the F4, but not on the F3?

 

 

FBLBest answer
Technical Moderator
January 27, 2025

Hello again @jayduino 

Did you manage to use USB VCP on your F3 standalone (without RTOS)? If not, check the example provided here. It might help you. Also, make sure to use external pull-up resistor on USB_DP, since this line doesn't embed. Otherwise, the USB assumes that there is nothing connected to the bus.

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.Best regards,FBL
jayduinoAuthor
Associate
January 27, 2025

Hi FBL, yes it does work without RTOS.

Let me look into the pull up design to see if that is setup correct. I will check and report back.

Visitor II
July 10, 2025

The main problem with USB and FreeRTOS is that the USB initialization process might fail if another task is running a different function at the same time. This happens in the order of 100ns. The host requests the descriptor and all that. For me, the solution was to avoid sending messages until the USB is fully initialized, and to use snprintf only inside the if block that checks whether the USB is configured.

 

void Start_USB_Task(void *argument)

{

for(;;){

 

 

 

if (hUsbDeviceFS.dev_state == USBD_STATE_CONFIGURED) {

snprintf(buffer, sizeof(buffer), "Temperatura %.2f Humedad %.2f", t_degC, rh_pRH);

CDC_Transmit_FS((uint8_t*)buffer, strlen(buffer));

 

}

osDelay(1000);

}

 

}