TASK MANAGEMENT IN FREE RTOS

Introduction

The real time projects which uses real time operating system (RTOS) are generally structured as a set of independent tasks. In a single core processor only one task can be executed at one time. But when we deal with the concept of multitasking we can make each task execute simultaneously by quickly switching between the task.

fig-1

Figure 1 shows the relation between different task and the execution time. In the first part of the figure 1 it seems that all the three task are executing at the same interval of time but actually the task is executed as shown in second part where different task are performed in different short interval of time. Due due to very quick switching between the task the output appears as first part of figure 1.

Scheduler

Any real time application contain a number of independent task as one task is to be performed in one time interval Scheduler is a part of Kernel which is responsible for selecting which task to execute at any particular interval of time. It choose the task from the ready state list to the running state.

fig -2

Types of Scheduling policies:

  1. Preemptive Scheduling: In this type of scheduling, tasks run with equal time slice without considering the priorities.
  2. Priority-based Preemptive: High priority task will run first.
  3. Co-operative Scheduling: Context switching will happen only with the co-operation of running tasks. Task will run continuously until task yield is called.

Free RTOS Task

The real time task are generally in running state or not running state .The not running state of task is further classified into three category.

  • Suspended state
  • Ready state
  • Blocked state
FreeRTOS tasks state
fig 3

Suspended State

It is one of the state of not running task. We can suspend any task by using the function vTaskSuspend() and to use the same task again we need to can resume the task by using the function vTaskResume().

Blocked State

Any task can be in blocked state due to

  • if the task is delayed
  • interrupt waiting
  • resources waiting

Ready state

For any task to be in running state it must be in ready state first where scheduling is done. All the task in the ready state are ready for execution as soon as processor picks them as per the scheduling policy.

How To Create Task In FreeRTOS?

When we deal with FreeRTOS, one of the important concept is task. In FreeRTOS one whole program is divided in different tasks which perform its own specific function.

Example:

If we want to write a program for reading the temperature of the environment from the temperature sensor and displaying it in the lcd then our task can be divided as

  1. Read the ADC value at any ADC pin of the micro controller where temperature sensor is connected.
  2. Convert the ADC value to equivalent temperature value.
  3. Display the temperature value to LCD.

After creating the task we can set the priority according to the critical nature of the task here to read the ADC value has the highest priority and display the value to lcd has lowest priority.

Free RTOS task creation

In freeRTOS we create task by using xCreateTask . It takes 5 arguments which defines various features of the task.

xTaskCreate(MyTask_pointer, "task_name", stack depth, Parameter, Priority, TaskHandle);

MyTask_pointer:

It is pointer to task entry function. we need to define the task with certain function so, it is just the name of the function which is used while creating a task.

task_name

A descriptive name for the task. This is mainly used to facilitate debugging.

stack depth

The size of the task stack specified as the number of variables the stack can hold – not the number of bytes. we should make sure to select stack size according to the complexity of computation. For example, if the stack is 16 bits wide and usStackDepth is defined as 100, 200 bytes will be allocated for stack storage.

Parameter

Pointer that will be used as the parameter for the task being created.If we want to pass a pointer to a variable as an argument to the task function, we can use this argument. Otherwise, we can pass the NULL value.

Priority

If we create any task than its priority is very important to be set. The priority of any task is set by user according to its complexity and the requirement of the project.For example, if we create four tasks and assign them priority 0, 1,2 and 3. Hence, zero means the lowest priority and 3 means the highest priority.

TaskHandle

It is used to pass a handle to the created task. This argument keeps the handle of the function that we can use to change function features such as the deletion of a task, changing its priority, etc.

Important Functions In FREE RTOS

xTaskCreate()– for task creation

vTaskSuspend()– for task suspension

vTaskResume()– for resuming the task

vTaskDelay()– for delaying a task

vTaskDelete()– for deleting a task

CODING

Code for Running two task

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

extern "C" void app_main();

TaskHandle_t myTask1Handle = NULL;
TaskHandle_t myTask2Handle = NULL;

 void task1(void *arg)
{
while(1)
{
printf("hello from task 1 \n");
vTaskDelay(1000 / portTICK_RATE_MS);
}
}
void task2(void *arg)
{
while(1)
{
printf("hello from task 2 \n");
vTaskDelay(1000 / portTICK_RATE_MS);
}
}



void app_main(void)
{

 xTaskCreate(task1, "task1", 4096, NULL,10,&myTask1Handle);
 xTaskCreate(task2, "task2", 4096, NULL, 9,&myTask2Handle);
 

}

Output in serial port

Code for Running Different Function of FreeRTOS

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
extern "C" void app_main();
TaskHandle_t myTask1Handle = NULL;
TaskHandle_t myTask2Handle = NULL;
int j=0;
int i=0;
 void task1(void *arg)
{
while(1)
 {
   i++;
   vTaskDelay(1000 / portTICK_RATE_MS);
   printf("hello from task 1 is %d \n",i);
 }

}
void task2(void *arg)
{

  while(1){
    j++;
    printf("hello from task 2 is %d \n",j);
    vTaskDelay(1000 / portTICK_RATE_MS);
    if(j==8)
    {
      vTaskSuspend(myTask1Handle);
       printf("task1 is suspended \n");
    }
    if(j==13)
    {
      vTaskResume(myTask1Handle);
       printf("task1 is resumed\n");
    }
    if(j==20)
    {
      vTaskDelete(myTask1Handle);
       printf("task1 is deleted \n");
    }

  }
}

void app_main(void)
{

xTaskCreate(task1, "task1", 4096, NULL,1,&myTask1Handle);
xTaskCreate(task2, "task2", 4096, NULL,1,&myTask2Handle);
}

OUTPUTS

fig-4

There are two tasks in the program and the value j is increasing in task 2 when the value of j reach 8 task 1 is suspended , when the value of j reach 13 task 1 is again resumed and when value of j reaches 20 task 1 is deleted.

All the conditions are as shown in the figure below:

fig-5
fig-6

Recommended For You

About the Author: admin

Leave a Reply

Your email address will not be published.