May 15, 2015

FreeRTOS Hello World

New to FreeRTOS? Here you can see how to run a simple hello world, to blink a GPIO pin, using the Keil uVision IDE Simulator with ARM Cortex-M3.

Download and extract FreeRTOS, go to the folders "FreeRTOS\Source" and "FreeRTOS\Source\portable\RVDS\ARM_CM3", then copy these files to a new directory for your project:

  

These are all the needed files to work with FreeRTOS, now go to the uVision IDE and create a new project:


Give a name and save it in the folder where you copied the FreeRTOS files, then choose the ARM model:


Double click "Source Group 1" and add all .c files:


Click "Manage Run-Time Environment":


And check these items:


If you don't have some of them just click 'Resolve'. Create a new file save as "main.c" with this basic hello world code:

#include <stm32f10x.h>
#include <stdio.h>
#include <stm32f10x_usart.h>
#include <stm32f10x_gpio.h>
#include <stm32f10x_rcc.h>

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

GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure; 

//-----------------------------------------
void blink_gpio_task(void *pvParameters)
{
    while(1) {
        GPIO_WriteBit(GPIOA, GPIO_Pin_0, Bit_SET);
        vTaskDelay(500/portTICK_RATE_MS);
  
        GPIO_WriteBit(GPIOA, GPIO_Pin_0, Bit_RESET);
        vTaskDelay(500/portTICK_RATE_MS);
 }
}

//-----------------------------------------
int main()
{ 
 SystemInit();
 
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); 
 
 //GPIO
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //GPIOA pin 0 output push pull
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
 GPIO_Init(GPIOA, &GPIO_InitStructure);
 
 //Create task to blink gpio
 xTaskCreate(blink_gpio_task, (const char *)"Blink gpio", configMINIMAL_STACK_SIZE, (void *)NULL, tskIDLE_PRIORITY, NULL);
 
 //Start the scheduler
 vTaskStartScheduler();
 
 //Should never reach here
 while(10);
}

The code above creates a thread that sets the GPIOA pin 0 high/3.3v for 500ms and then low/0v for 500ms, using the CMSIS library that can be seem here. At this point you should have something like this:

Now configure the ARM startup file to work with FreeRTOS and build (F7) the project:

Build target 'Target 1'
compiling heap_2.c...
compiling list.c...
compiling main.c...
compiling port.c...
compiling queue.c...
compiling tasks.c...
compiling misc.c...
compiling stm32f10x_gpio.c...
compiling stm32f10x_rcc.c...
assembling startup_stm32f10x_md.s...
compiling system_stm32f10x.c...
linking...
Program Size: Code=6532 RO-data=268 RW-data=136 ZI-data=19240  
".\test.axf" - 0 Error(s), 0 Warning(s).

Let's run! start a debug session:


Select to see the GPIOA peripherals:


Run (F5) and you should see the GPIOA pin 0 blinking:


That's all!

Download:
About the versions:
  • FreeRTOS 8.1.2
  • Keil uVision 5.11.1.0
  • STM ARM Cortex-M3 STM32F103C8T6

4 comments :

  1. your project not available for download

    ReplyDelete
    Replies
    1. yes, dropbox screwed the public folder for everyone. New server, post updated!

      Delete
  2. HI,

    I am getting compilation error with the files I added. Could you let me know what is going on here please?

    *** Using Compiler 'V5.06 update 4 (build 422)', folder: 'C:\Keil_v5\ARM\ARMCC\Bin'
    Rebuild target 'Target 1'
    compiling port.c...
    files\portmacro.h(222): error: #20: identifier "configMAX_SYSCALL_INTERRUPT_PRIORITY" is undefined
    uint32_t ulNewBASEPRI = configMAX_SYSCALL_INTERRUPT_PRIORITY;
    files\portmacro.h(249): error: #20: identifier "configMAX_SYSCALL_INTERRUPT_PRIORITY" is undefined
    uint32_t ulReturn, ulNewBASEPRI = configMAX_SYSCALL_INTERRUPT_PRIORITY;
    files\port.c(83): error: #35: #error directive: configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0. See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html
    #error configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0. See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html
    files\port.c: 0 warnings, 3 errors
    compiling list.c...
    files\portmacro.h(222): error: #20: identifier "configMAX_SYSCALL_INTERRUPT_PRIORITY" is undefined
    uint32_t ulNewBASEPRI = configMAX_SYSCALL_INTERRUPT_PRIORITY;
    files\portmacro.h(249): error: #20: identifier "configMAX_SYSCALL_INTERRUPT_PRIORITY" is undefined
    uint32_t ulReturn, ulNewBASEPRI = configMAX_SYSCALL_INTERRUPT_PRIORITY;
    files\list.c: 0 warnings, 2 errors
    compiling heap_2.c...
    files\portmacro.h(222): error: #20: identifier "configMAX_SYSCALL_INTERRUPT_PRIORITY" is undefined
    uint32_t ulNewBASEPRI = configMAX_SYSCALL_INTERRUPT_PRIORITY;
    files\portmacro.h(249): error: #20: identifier "configMAX_SYSCALL_INTERRUPT_PRIORITY" is undefined
    uint32_t ulReturn, ulNewBASEPRI = configMAX_SYSCALL_INTERRUPT_PRIORITY;
    files\heap_2.c: 0 warnings, 2 errors
    compiling queue.c...
    files\portmacro.h(222): error: #20: identifier "configMAX_SYSCALL_INTERRUPT_PRIORITY" is undefined
    uint32_t ulNewBASEPRI = configMAX_SYSCALL_INTERRUPT_PRIORITY;
    files\portmacro.h(249): error: #20: identifier "configMAX_SYSCALL_INTERRUPT_PRIORITY" is undefined
    uint32_t ulReturn, ulNewBASEPRI = configMAX_SYSCALL_INTERRUPT_PRIORITY;
    files\queue.c: 0 warnings, 2 errors
    compiling main.c...
    main.c(1): error: #5: cannot open source input file "stm32f103x.h": No such file or directory
    #include
    main.c: 0 warnings, 1 error
    compiling tasks.c...
    files\portmacro.h(222): error: #20: identifier "configMAX_SYSCALL_INTERRUPT_PRIORITY" is undefined
    uint32_t ulNewBASEPRI = configMAX_SYSCALL_INTERRUPT_PRIORITY;
    files\portmacro.h(249): error: #20: identifier "configMAX_SYSCALL_INTERRUPT_PRIORITY" is undefined
    uint32_t ulReturn, ulNewBASEPRI = configMAX_SYSCALL_INTERRUPT_PRIORITY;
    files\tasks.c: 0 warnings, 2 errors
    compiling misc.c...
    compiling stm32f10x_gpio.c...
    compiling stm32f10x_rcc.c...
    assembling startup_stm32f10x_md.s...
    compiling system_stm32f10x.c...
    ".\Objects\freeRTOS_test.axf" - 12 Error(s), 0 Warning(s).
    Target not created.
    Build Time Elapsed: 00:00:03

    Thanks

    ReplyDelete
    Replies
    1. Hello, the compiler is not finding some symbols and files, you need include them, check the folder and also make sure to match with the versions used in this tutorial

      Delete