Creating a FreeRTOS Task for the LPC2148 Code Base
A simple example of creating a new task for FreeRTOS in the LPC2148 Code Base
Adding a new task in FreeRTOS is relatively easy. This tutorial will show you how to create a new task and properly integrate it within the LPC2148 Code Base.
Step One: Create a unique identifier for your task
In order to access your task from anywhere in the project (for example, to stop the task or check it's current status) it needs to have a unique identifier called a Task Handle. An enumeration of every task handle used in the project is defined in ProjectConfig.h. Before you can create your task, you will need to add a new task handle to the taskHandle_e enumeration with a clearly identifiable name like TASKHANDLE_MYTASK. Be sure to add the task handle before TASKHANDLE_LAST. For example:
ProjectConfig.h
typedef enum
{
TASKHANDLE_STARTUP,
TASKHANDLE_MONITOR,
TASKHANDLE_LED,
TASKHANDLE_MYTASK,
#if configGENERATE_RUN_TIME_STATS == 1
TASKHANDLE_RUNTIMESTATS,
#endif
TASKHANDLE_LAST
}
taskHandle_e;
void * taskHandles [TASKHANDLE_LAST];
Step Two: Create the header (.h) and code (.c) files for your task
Once you've added a task handle to ProjectConfig.h you can make the main header and code file(s) that will store the actual task code and functionality. A minimal example of this can be seen below:
myTask.h
#ifndef _MYTASK_H_
#define _MYTASK_H_
#include "FreeRTOS.h"
#include "task.h"
void mytaskDoSomething(void);
signed portBASE_TYPE mytaskTaskStart (void);
signed portBASE_TYPE mytaskTaskStop (void);
#endif
myTask.c
#include "myTask.h"
void mytaskDoSomething(void)
{
// Code that does something
}
/**************************************************************************/
/*!
The main code that will execute as long as the task is active.
The first parameter (vMyTask in this case) needs to match the
name used when the task is created in the method just below.
*/
/**************************************************************************/
static portTASK_FUNCTION(vMyTask, pvParameters __attribute__((unused)))
{
// Do any required initialisation or
// set up any hardware before the task
// begins executing for the first time
// ToDo: ...
// The code within the for loop is your actual
// task that will continously execute
for (;;)
{
mytaskDoSomething();
// vTaskDelay will cause the task to be delayed for
// a specified number of ticks
vTaskDelay(100); // Wait 100 ticks or 1 second
}
}
/**************************************************************************/
/*!
Creates a new task for the FreeRTOS Kernel and add it to the
scheduler.
*/
/**************************************************************************/
signed portBASE_TYPE mytaskTaskStart (void)
{
return xTaskCreate (
vMyTask,
(const signed portCHAR * const) "Task Name",
configMINIMAL_STACK_SIZE,
NULL,
(tskIDLE_PRIORITY + 1),
&taskHandles [TASKHANDLE_MYTASK]);
}
/**************************************************************************/
/*!
Stops the task and deletes it from the task scheduler.
*/
/**************************************************************************/
signed portBASE_TYPE mytaskTaskStop (void)
{
if (!taskHandles [TASKHANDLE_MYTASK])
return 0;
vTaskDelete (taskHandles [TASKHANDLE_MYTASK]);
taskHandles [TASKHANDLE_MYTASK] = NULL;
return 1;
}