Line 31: Line 31:
 
[[File:TaskSwitching 01.JPG]]
 
[[File:TaskSwitching 01.JPG]]
 
#The controller starts the execution from setup functions. The Serial port is initialized at 9600 baud rate and setup message is printed.<br>
 
#The controller starts the execution from setup functions. The Serial port is initialized at 9600 baud rate and setup message is printed.<br>
#Later 3-Tasks(Task1, Task2 and Idle) are created in order and at the end of the Setup function, scheduler/kernel takes the control.
+
#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.
 
#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 50ms.  
 
#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 50ms.  
 
#Now 2-tasks are available for scheduler and it chooses Task1 as it is the available higher priority task.<br>
 
#Now 2-tasks are available for scheduler and it chooses Task1 as it is the available higher priority task.<br>

Revision as of 17:32, 27 June 2016

In this tutorial we will be looking all the possible freeRtos configuration.

Task States

In the FreeRTOS a task can be in either of four different states viz., Running, Ready, Blocked and Suspended as shown in below image. FreeRtos States.png

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.


API Details

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

1.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.

2.xTaskDelay(): 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.

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 state if its priority is higher compared to the currently running task.

Output

TaskSwitching 01.JPG

  1. The controller starts the execution from setup functions. 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 50ms.
  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.
  6. CPU is left out with only Idle task and it starts running.
  7. 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.