Defining task functions

Each of the tasks, that is, RedTask, BlueTask, and GreenTask, has a function associated with it. Remember – a task is really just an infinite while loop with its own stack and a priority. Let's cover them one by one.

GreenTask sleeps for a little while (1.5 seconds) with the Green LED on and then deletes itself. There are a few noteworthy items here, some of which are as follows:

  • Normally, a task will contain an infinite while loop so that it doesn't return. GreenTask still doesn't return since it deletes itself.
  • You can easily confirm vTaskDelete doesn't allow execution past the function call by looking at the Nucleo board. The green light will only be on for 1.5 seconds before shutting off permanently. Take a look at the following example, which is an excerpt from main_taskCreation.c:
void GreenTask(void *argument)
{
SEGGER_SYSVIEW_PrintfHost("Task1 running
while Green LED is on ");
GreenLed.On();
vTaskDelay(1500/ portTICK_PERIOD_MS);
GreenLed.Off();

//a task can delete itself by passing NULL to vTaskDelete
vTaskDelete(NULL);

//task never get's here
GreenLed.On();
}

BlueTask blinks the blue LED rapidly for an indefinite period of time, thanks to the infinite while loop. However, the blue LED blinks are cut short because RedTask will delete BlueTask after 1 second. This can be seen in the following example, which is an excerpt from Chapter_7/Src/main_taskCreation.c:

void BlueTask( void* argument )
{
while(1)
{
SEGGER_SYSVIEW_PrintfHost("BlueTaskRunning ");
BlueLed.On();
vTaskDelay(200 / portTICK_PERIOD_MS);
BlueLed.Off();
vTaskDelay(200 / portTICK_PERIOD_MS);
}
}

RedTask deletes BlueTask on its first run and then continues to blink the red LED indefinitely. This can be seen in the following excerpt from Chapter_7/Src/main_taskCreation.c:

void RedTask( void* argument )
{
uint8_t firstRun = 1;

while(1)
{
lookBusy();

SEGGER_SYSVIEW_PrintfHost("RedTaskRunning ");
RedLed.On();
vTaskDelay(500/ portTICK_PERIOD_MS);
RedLed.Off();
vTaskDelay(500/ portTICK_PERIOD_MS);

if(firstRun == 1)
{
vTaskDelete(blueTaskHandle);
firstRun = 0;
}
}
}

So, the preceding functions don't look like anything special  and they're not. They are simply standard C functions, two of which have infinite while loops in them. How do we go about creating FreeRTOS tasks out of these plain old functions? 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.189.177