Introduction: FreeRTOS With Arduino 02 : Task Switching

We will see what is a task and its different states. Later we will see how to create a FreeRTOS tasks and finally we will write a simple example to see how the task works and task switching happens.

Step 1: What Is a Task?

Task is a small piece of program which competes with other tasks to get the CPU resources.Every task has an entry point and runs continuously within an infinite loop. A freeRTOS task will not have an exit point but can be deleted once its job is done.

Step 2: Task States :

In the FreeRTOS a task can be in one of four different states viz., Running, Ready, Blocked and Suspended as shown in above image.

1.Running: The task which is executing currently is said to be in running state. It owns the CPU.

2.Ready: The task which is neither suspended nor blocked but still not executing will be in ready state. It's not in running state because either a high priority or equal priority task is executing.

3.Blocked: A task will go in blocked state whenever it is waiting for an event to happen. The event can be completing a delay period or availability of a resource. The blocked tasks are not available for scheduling.

4.Suspended: When vTaskSuspend() is called, the task goes in suspended state. It can be resumed by calling xTaskResume(). The suspended tasks are also not available for scheduling.

Step 3: Creating a Task :

Every task has a distinct set of parameters which needs to be provided while creating a task. The parameters include unique task name, priority, stack memory, a task routine and optional input parameters to task routine.

Below is the prototype of xTaskCreate() function.

BaseType_t xTaskCreate( TaskFunction_t pvTaskCode,
const char * const pcName, unsigned short usStackDepth, void *pvParameters, UBaseType_t uxPriority, TaskHandle_t *pxCreatedTask );

pvTaskCode: Pointer to the task entry function (just the name of the function that implements the task, see the example below).

pcName: A descriptive name for the task. This is mainly used to facilitate debugging, but can also be used to obtain a task handle. The maximum length of a task's name is set using the configMAX_TASK_NAME_LEN parameter in FreeRTOSConfig.h.

usStackDepth: The number of words (not bytes!) to allocate for use as the task's stack. For example, if the stack is 16-bits wide and usStackDepth is 100, then 200 bytes will be allocated for use as the task's stack. As another example, if the stack is 32-bits wide and usStackDepth is 400 then 1600 bytes will be allocated for use as the task's stack. The stack depth multiplied by the stack width must not exceed the maximum value that can be contained in a variable of type size_t.

pvParameters: A value that will be passed into the created task as the task's parameter. If pvParameters is set to the address of a variable then the variable must still exist when the created task executes - so it is not valid to pass the address of a stack variable.

uxPriority: The priority at which the created task will execute. Systems that include MPU support can optionally create a task in a privileged (system) mode by setting bit portPRIVILEGE_BIT in uxPrriority. For example, to create a privileged task at priority 2 set uxPriority to ( 2 | portPRIVILEGE_BIT ).

pxCreatedTask: Used to pass a handle to the created task out of the xTaskCreate() function. pxCreatedTask is optional and can be set to NULL. Returns: If the task was created successfully then pdPASS is returned. Otherwise, errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY is returned.

Step 4: API Details :

Here we will discuss some of the most frequently used APIs related to tasks.

xTaskCreate(): This interface is used to create a new Task, if the task is successfully created then it returns pdPass(1) or else errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY(-1). Check this link for more details.

vTaskDelay(): This function is used to delay/block the task for specified delay time(ticks). INCLUDE_vTaskDelay needs to be set to 1 in FreeRtosConfig.h file for using this function. Check this link for more details.

Step 5: Example1 :

In this example, we are going to create 3-Tasks with different priorities. Each task will run for some time and then goes to blocked state allowing the other tasks to run. Once the waiting time is elapsed the scheduler will bring the task to ready state and eventually run the state if its priority is higher compared to the currently running task.

/***************************************************************************************************
ExploreEmbedded Copyright Notice **************************************************************************************************** * File: 01-TaskSwitching * Version: 15.0 * Author: ExploreEmbedded * Website: http://www.exploreembedded.com/wiki * Description: File contains the free rtos example to demonstarte the task switching.

This code has been developed and tested on ExploreEmbedded boards. We strongly believe that the library works on any of development boards for respective controllers. Check this link http://www.exploreembedded.com/wiki for awesome tutorials on 8051,PIC,AVR,ARM,Robotics,RTOS,IOT. ExploreEmbedded invests substantial time and effort developing open source HW and SW tools, to support consider buying the ExploreEmbedded boards. The ExploreEmbedded libraries and examples are licensed under the terms of the new-bsd license(two-clause bsd license). See also: http://www.opensource.org/licenses/bsd-license.php

EXPLOREEMBEDDED DISCLAIMS ANY KIND OF HARDWARE FAILURE RESULTING OUT OF USAGE OF LIBRARIES, DIRECTLY OR INDIRECTLY. FILES MAY BE SUBJECT TO CHANGE WITHOUT PRIOR NOTICE. THE REVISION HISTORY CONTAINS THE INFORMATION RELATED TO UPDATES.

Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that this copyright notices appear in all copies and that both those copyright notices and this permission notice appear in supporting documentation. **************************************************************************************************/

#include

void setup() { Serial.begin(9600); Serial.println(F("In Setup function"));

/* Create two tasks with priorities 1 and 2. An idle task is also created, which will be run when there are no tasks in RUN state */

xTaskCreate(MyTask1, "Task1", 100, NULL, 1, NULL); xTaskCreate(MyTask2, "Task2", 100, NULL, 2, NULL); xTaskCreate(MyIdleTask, "IdleTask", 100, NULL, 0, NULL); }

void loop() { // DO nothing }

/* Task1 with priority 1 */ static void MyTask1(void* pvParameters) { while(1) { Serial.println(F("Task1")); vTaskDelay(100/portTICK_PERIOD_MS); } }

/* Task2 with priority 2 */ static void MyTask2(void* pvParameters) { while(1) { Serial.println(F("Task2")); vTaskDelay(150/portTICK_PERIOD_MS); } }

/* Idle Task with priority Zero */ static void MyIdleTask(void* pvParameters) { while(1) { Serial.println(F("Idle state")); delay(50); } }

Step 6: Output :

  1. The controller starts the execution from setup function. The Serial port is initialized at 9600 baud rate and setup message is printed.
  2. Later 3-Tasks(Task1, Task2 and Idle) are created in order with priorities 2,1,0. At the end of the Setup function, scheduler/kernel takes the control.
  3. There are 3-tasks in the Ready state and since Task2 has the highest priority it will run first and goes to block state for 150ms.

  4. Now 2-tasks are available for scheduler and it chooses Task1 as it is the available higher priority task.

  5. Now Task1 runs for some time and then goes to blocked state for 100ms.

  6. CPU is left out with the Idle task and it starts running.

  7. Task2 will be in blocked state for 150ms and task1 will 100ms. So task1 will come out of blocked state first.

  8. After Task1 waiting time is elapsed, it comes to the Ready state. Since it has got higher priority compared to the Idle task, it will preempt Idle task and starts running. It again goes to blocked state for 100ms.

  9. Now CPU is left out with IDLE task and it starts running it.

  10. After Task2 waiting time is elapsed, it comes to the Ready state. Since it has got higher priority compared to the Idle task, it will preempt Idle task and starts running. It again goes to blocked state for 150ms.

  11. Same thing continues.

Step 7: Downloads :

Download the complete project and libraries from here.

Comments

About This Instructable

845views

9favorites

License:

Bio: We make Embedded Design tools, boards, sensors, peripherals, DIY kits and awesome tutorials. We also help companies and individuals prototype embedded electronics.
More by ExploreEmbedded:Power and Temperature Data Logger With ESP32 and AWS IOTSecure IOT With AWS and Hornbill ESP32 Using Arduino.ESP32 Bluetooth Ambience Light Controller With WS2812 LED Strips.
Add instructable to: