cancel
Showing results for 
Search instead for 
Did you mean: 

Setup FreeRTOS and USB Host with STM32CubeIDE

MartinC
Associate

I'm new to STM32 so I am learning FreeRTOS using this video https://www.youtube.com/watch?v=OPrcpbKNSjU&list=PLEBQazB0HUySNug4eRm-73hNyMcCRViRB&index=3 but with the STM32 F4 Discovery board, so the STM32CubeIDE (v1.1.0) has both FreeRTOS and USB_Host enabled in the Device Configuration Tool window. As suggested, I've changed SYS in System Core to use TIM6.

It compiles and runs in debug mode but then halts in the HardFault_Hander() when xPortStartScheduler() is run due to osKernelStart() call. I get the same problem if the USB_HOST is configured as either Virtual Com Port or HID, but the FreeRTOS runs fine if I set Class for FS IP as Disable i.e. disabled USB.

I've read https://www.freertos.org/RTOS-Cortex-M3-M4.html and can see that the generated code has been setup to use 4 bits of priority, and LIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY is 5. But I can't apply the advice about calling NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 ) because the compiler doesn't recognise this function or constant. I also noticed that in Connectivity, USB_OTG_FS, NVIC Settings, USB On The Go FS global interrupt is enabled and has Preemption Priority 5, Sub Priority 0 and cannot be changed.

What do I have to change to get the USB Host functionality to work when using FreeRTOS?

1 REPLY 1
MartinC
Associate

Solved this myself, when browsing around this site, found that it is a common problem. Thanks to @Dave Nadler​  and his page http://www.nadler.com/embedded/newlibAndFreeRTOS.html I managed to get FreeRTOS to coexist with the STM USB stack by doing the following:

1) After using STM32CubeIDE to generate the code files, removed the heap_4.c file from the project, from Middlewares\Third_Party\FreeRTOS\Source\portable\MemMang\heap_4.c

2) In that location, copy in the heap_useNewlib.c provided on the above page

3) In Core\Inc\FreeRTOSConfig.h, at line 148, so in the user code defines section, added #define configUSE_NEWLIB_REENTRANT            1

Although I haven't seen any USB stack activity when connected to a PC, this does prevent the HardFault problem. According to https://community.st.com/s/question/0D50X0000BB1eL7SQJ/bug-cubemx-freertos-projects-corrupt-memory it looks like ST are working on this. Hope this helps the next person to have the same problem.