More Pages

Saturday, April 16, 2016

ARM Cortex-M3 (STM32F103) Tutorial - FreeRTOS - Task

In this tutorial, I will share how use an RTOS on STM32F103 microcontroller. I use FreeRTOS for this tutorial. RTOS is usually needs when an embedded system must responds to an event within a strictly defined time.


In this tutorial, I will make RTOS task with 3 LED that blinks at different frequency. First thing to do is to download FreeRTOS library. The next step is to make a new project in Keil uVision and setup the configuration for using FreeRTOS on STM32F103.

You have to make a new project on Keil uVision for STM32F103. If you don't know how to make a new project, you can view this tutorial. Follow this steps below to setup the configuration for using FreeRTOS.
  • Add FreeRTOS source library to your project folder. That library file is separated within a folder called FreeRTOS. In that folder is consist of these file:
  • Copy FreeRTOSConfig.h file from FreeRTOS demo file for STM32F103 using Keil (in folder CORTEX_STM32F103_Keil) to folder FreeRTOS\include in your project.
  • Not all files in the portable folder is needed, you only need MemMang folder and RVDS\ARM_CM3 folder. The ARM_CM3 folder contains SysTick definitions for STM32F103 microcontroller.
  • In the project explorer, add these FreeRTOS source code:
 
  • Go to the Include Paths settings in the Options for Target dialog and add paths to the FreeRTOS header file:
  • Open FreeRTOSConfig.h file, then add this code for map map the FreeRTOS port interrupt handlers to the CMSIS standard names:
/* Definitions that map the FreeRTOS port interrupt handlers to their CMSIS
standard names. */
#define vPortSVCHandler SVC_Handler
#define xPortPendSVHandler PendSV_Handler
#define xPortSysTickHandler SysTick_Handler
Until this step, now we are ready to make application using FreeRTOS. This is the full code for LED blinking using FreeRTOS:
#include "stm32f10x.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"

#include "FreeRTOS/include/FreeRTOS.h"
#include "FreeRTOS/include/task.h"

void ledInit(void);
// RTOS task
void vTaskLedRed(void *p);
void vTaskLedYellow(void *p);
void vTaskLedGreen(void *p);

int main(void)
{
    // Configure GPIO for LED
    ledInit();

    // Create LED blink task
    xTaskCreate(vTaskLedRed, (const char*) "Red LED Blink", 
        128, NULL, 1, NULL);
    xTaskCreate(vTaskLedYellow, (const char*) "Yellow LED Blink",
        128, NULL, 1, NULL);
    xTaskCreate(vTaskLedGreen, (const char*) "Green LED Blink",
        128, NULL, 1, NULL);
    // Start RTOS scheduler
    vTaskStartScheduler();

    return 0;
}

void ledInit()
{
    GPIO_InitTypeDef GPIO_InitStruct;

    // Configure PC13, PC14, PC15 as push-pull output
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOC, &GPIO_InitStruct);
}

void vTaskLedRed(void *p)
{
    for (;;)
    {
        GPIOC->ODR ^= GPIO_Pin_14;
        vTaskDelay(100/portTICK_RATE_MS);
    }
}

void vTaskLedYellow(void *p)
{
    for (;;)
    {
        GPIOC->ODR ^= GPIO_Pin_15;
        vTaskDelay(500/portTICK_RATE_MS);
    }
}

void vTaskLedGreen(void *p)
{
    for (;;)
    {
        GPIOC->ODR ^= GPIO_Pin_13;
        vTaskDelay(1000/portTICK_RATE_MS);
    }
}
You can also see this code on my repository.

8 comments:

  1. Good job man. I'm trying to learn STM32, particularly RTOS and this is one of the easiest and direct-to-the-point tutorial I've come across. Thanks!

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hi, thank you for the tutorial. Before finding your post, I watched an STM32 tutorial using a different board and decided to get it. How different is the code for the stm32 keil tutorial? My board has the STM32F0 controller.

    Thanks,
    George

    ReplyDelete
    Replies
    1. The library is different. My tutorial in this blog using SPL library. The "stm32 keil tutorial" uses HAL library.
      The main difference between SPL and HAL is HAL uses a more higher hardware abstraction than SPL.

      Delete
  4. Hola Amigo, muy buen tutotial para empezar a trabajar la BluePill con FreeRTOS, has trabajado FreeeRTOS en la BluePill en Atollic TRUEStudio? o tendras algun ejemplo?

    ReplyDelete