m
m
Line 90: Line 90:
 
}
 
}
 
}
 
}
 
+
</syntaxhighlight>
 
{{DISQUS}}
 
{{DISQUS}}

Revision as of 14:41, 7 April 2015



Intro

This is our first tutorial with FreeRTOS so start with a simple example of how to create and use a task.

Before going to this tutorial its recommended to first go through the FreeRTOS basics tutorial.

In main function we will create 3 tasks viz., MyTask1, MyTask2 and MyTask3 with priorities 1,2 and 3 respectively.

An idle task is also created, which will be run when there are no tasks in RUN state.

Only creating task is not sufficient. Tasks will be executed once the scheduler is started. So call vTaskStartScheduler() to start the scheduler.

Code

For making it easier to understand what is happening, we are displaying some data on UART.

You can find the function prototypes in the code libraries.

/* Scheduler include files */
#include "FreeRtOSConfig.h"
#include "FreeRTOS.h"
#include "task.h"
#include "croutine.h"
#include "uart.h"	// Explore Embedded UART library
 
/* Local Tasks declaration */
static void MyTask1(void* pvParameters);
static void MyTask2(void* pvParameters);
static void MyTask3(void* pvParameters);
static void MyIdleTask(void* pvParameters);
 
int main(void)
{   
	SystemInit();		/* Initialize the micro-controller */
	UART_Init(38400);	/* Initialize the UART module with baud rate 38400 */	
 
	/* Create the three tasks with priorities 1,2,3. Only tasks will be created. */
 
	xTaskCreate( MyTask1, ( signed char * )"Task1", configMINIMAL_STACK_SIZE, NULL, 1, NULL );
	xTaskCreate( MyTask2, ( signed char * )"Task2", configMINIMAL_STACK_SIZE, NULL, 2, NULL );
	xTaskCreate( MyTask3, ( signed char * )"Task3", configMINIMAL_STACK_SIZE, NULL, 3, NULL );
	xTaskCreate( MyIdleTask, ( signed char * )"IdleTask", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
 
	UART_Printf("\n\rIn the main");
 
	/* Start the schedular */
	vTaskStartScheduler();
 
	while(1);
}
 
static void MyTask1(void* pvParameters)		/* Task1 with priority 1 */
{
	while(1)
	{
		UART_Printf("\n\rTask1");
		vTaskDelay(100);
	}
}
 
static void MyTask2(void* pvParameters)		/* Task1 with priority 2 */
{
	while(1)
	{
		UART_Printf("\n\rTask2");
		vTaskDelay(250);
	}
}
 
static void MyTask3(void* pvParameters)		/* Task1 with priority 3 */
{
	while(1)
	{
		UART_Printf("\n\rTask3");
		vTaskDelay(600);
	}
}
 
static void MyIdleTask(void* pvParameters)	/* Task1 with priority 4 */
{
	while(1)
	{
		UART_Printf("\n\rIn idle state");
	}
}