Search code examples
cstm32freertoslibopencm3

STM32 FreeRTOS with LibOpenCM3


I have a STM32 BluePill Board (STM32F103C8 with 8Mhz Quarz) and tried to upload a small blink program made with LibOpenCM3 and FreeRTOS. But for some reason FreeRTOS hangs in vTaskStartScheduler() the sys_tick_handler also doesn't do anything.

I didn't use a debugger and just placed a gpio_reset inside the sys_tick_handler function and after the vTaskStartScheduler call to test if the code gets executed but it didn't seem to do anything and I can't figure out why. The code is available here: https://gitlab.com/feldim2425/stm32-testing

UPDATE: I debugged with OpenOCD and fount out that it jumps into the hard_fault_handler UPDATE 2: The UsageFault-Status-Register has the NOCP bit set


Solution

  • Ok I found the issue. Many examples seem to rely on compiler optimization to directly link the vPortSVCHandler, xPortPendSVHandler and xPortSysTickHandler from FreeRTOS into the vector table if you call them inside your own handler vector implementation for sv_call_handler, pend_sv_handler and sys_tick_handler. But that didn't work here, the functions have to be called directly by the processor.

    Adding these 3 Lines to the bottom of the FreeRTOSConfig.h file and removing my own function declarations for the vectors fixed the problem:

    #define vPortSVCHandler sv_call_handler
    #define xPortPendSVHandler pend_sv_handler
    #define xPortSysTickHandler sys_tick_handler
    

    The fix is described here: https://www.freertos.org/FreeRTOS_Support_Forum_Archive/January_2012/freertos_LPC1768_FreeRTOS_4964917.html

    It is described for CMSIS but the only difference (in this case) are just the names of the vectors/handler functions.