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

0 comentários :

Post a Comment