FreeRTOS Configuration for ST Microcontrollers - umrover/embedded-testbench GitHub Wiki

FreeRTOS Configuration for ST Microcontrollers

Mark Gibney Owen Park ESW 2023

Background:

FreeRTOS on ST Microcontrollers are Round-robin based, meaning that the user sets a delay time between tasks before the task re-runs in main.c.

Visual Model of Round-robin Scheduler:

“It’s important to understand how STM32CubeIDE has bundled FreeRTOS. While FreeRTOS is an underlying software framework that allows for switching tasks, scheduling, etc., we won’t be making calls to FreeRTOS directly.

ARM has created the CMSIS-RTOS library, which allows us to make calls to an underlying RTOS, thus improving the portability of code among various ARM processors….

SysTick is a special timer in most ARM processors that’s generally reserved for operating system purposes. By default, SysTick in an STM32 will trigger an interrupt every 1 ms. If we’re using the STM32 HAL, by default, SysTick will be used for things like HAL_Delay() and HAL_GetTick(). As a result, the STM32 HAL framework gives SysTick a very high priority. However, FreeRTOS needs SysTick for its scheduler, and it requires SysTick to be a much lower priority.

We can fix this conflict in a few ways, but the easiest is to assign another, unused timer as the timebase source for HAL. Timers 6 and 7 in most STM32 microcontrollers are usually very basic, which makes them perfect as a timebase for HAL.”

-Shawn Hymel, Digi-Key Electronics

Procedure to Install FreeRTOS on an ST Microcontroller:

  1. Create a new C project with CubeIDE and open the .ioc file.
  2. Travel to Categories -> Middleware -> FREERTOS. Under Mode click on Interface and change to CMSIS_V2.
    • CMSIS_V2 is a more up-to-date version of CMSIS_V1. Use CMSIS_V2 to assure that any bugs between FreeRTOS and the microcontroller are minimal.
  3. Still in FREERTOS, under Configuration, open Tasks and Queues. Edit and/or add tasks to your project’s necessities.
    • This is where you identify and generate your threads with the .ioc file.
    • When adding a task/thread, give it a name (i.e. taskName) and an entry function name (i.e. startTaskName).
    • Be sure to set the Priority of each task/thread to a priority appropriate to the project. For example, set your highest priority task/thread to “osPriorityNormal” and your next highest priority task/thread to “osPriorityBelowNormal”. The options under Priority are self-explanatory.
      • This is important because, should any of your task delays overlap and attempt to run concurrently, the task with the higher priority will run, avoiding a system crash.
    • Depending on the size of your task/thread, you may have to increase the stack size for the task/thread during this step as well. Stack Size (Words) is located directly under the Priority setting.
  4. Travel to System Core -> SYS. Under Mode, click on Timebase Source and change to TIM6.
    • See Background above for explanation and justification of this step.
  5. Save and Generate your code.
  6. Open main.c and find your tasks/threads near the bottom of the file. Use the following code below as a template/reference for each thread.
    • This method of tracking the delay time tends to work the best.
void startTaskName(void *argument)
{
	TickType_t xLastWakeTime;
	const TickType_t xFrequency = 5000/portTICK_PERIOD_MS; //VALUE IN MILLISECONDS (i.e. 5000 = 5 sec).
	 xLastWakeTime = xTaskGetTickCount();
  for(;;)
  {
             //TASK CODE BEGINS HERE
             //TASK CODE ENDS HERE
  	 vTaskDelayUntil( &xLastWakeTime, xFrequency );
  }
  osThreadTerminate(NULL); //FAILSAFE SHOULD PROGRAM EXIT LOOP.
}

FreeRTOS is successfully installed on your project!

Additional Options:

FreeRTOS is round-robin based with preemption enabled by default on CubeIDE.

Disable/Enable Preemption:

  1. After generating code from the .ioc file, find the file “FreeRTOSConfig.h” (under Core/Inc/)
  2. Where it says “#define configUSE_PREEMPTION”, change the adjacent value from “1” to “0”.

Resources:

Bibliography:

Hymel, Shawn. “Getting Started with STM32 - Introduction to FreeRTOS.” Maker.io, Digi-Key Electronics, https://www.digikey.com/en/maker/projects/getting-started-with-stm32-introduction-to-freertos/ad275395687e4d85935351e16ec575b1.

Additional Supporting Links:

Video Tutorial of STM Configuration: https://www.youtube.com/watch?v=CdpgqpuPSyQ&t=310s

CMSIS_V1 vs. CMSIS_V2: https://github.com/STMicroelectronics/cmsis_core/issues/1

⚠️ **GitHub.com Fallback** ⚠️