m
m
 
(21 intermediate revisions by the same user not shown)
Line 7: Line 7:
 
Each tasks runs continuously i.e. in infinite loop.  
 
Each tasks runs continuously i.e. in infinite loop.  
  
Now, you may get question, how the application works ?
+
The kernel is responsible for the management of all the tasks.
 
+
Check this tutorial to know working of kernel and task scheduling.
Here the kernel comes in picture. To know more about kernel check RTOS basics tutorial.
+
  
 
==Different states of a task==
 
==Different states of a task==
Line 17: Line 16:
  
 
*Running
 
*Running
The task which is executing currently is said to be in running state. It owns the CPU.
+
::The task which is executing currently is said to be in running state. It owns the CPU.
  
 
*Ready
 
*Ready
The task which is neither suspended nor blocked but still not executing will be ready state.  
+
::The task which is neither suspended nor blocked but still not executing will be ready state. Its not in running state because either a high priority or equal priority task is executing.
Its not in running state because either a high priority or equal priority task is executing.
+
  
 
*Blocked
 
*Blocked
A task will go in blocked state whenever its waiting for a event to happen.  
+
::A task will go in blocked state whenever its waiting for a event to happen. The event can be completing a delay period or availability of a resource. The blocked tasks are not available for scheduling.
The event can be completing a delay period or availability of a resource.
+
The blocked tasks are not available for scheduling.
+
  
 
*Suspended
 
*Suspended
When vTaskSuspend() is called, the task goes in suspended state. To resume it, xTaskResume() is called.
+
::When vTaskSuspend() is called, the task goes in suspended state. To resume it, xTaskResume() is called. The suspended tasks are also not available for scheduling.
The suspended tasks are also not available for scheduling.
+
 
+
It seems that the tasks in both Blocked and Suspended are in waiting state.
+
But the difference is, a suspended task can't come in Ready state unless xTaskResume() is called.
+
Time out period can be specified only for Blocked tasks but not for suspended tasks.
+
  
 +
::It seems that the tasks in both Blocked and Suspended are in waiting state. But the difference is, a suspended task can't come in Ready state unless xTaskResume() is called. Time out period can be specified only for Blocked tasks but not for suspended tasks.
  
 
==Deciding Priority of a task ==
 
==Deciding Priority of a task ==
Line 44: Line 36:
 
FreeRTOS also always you to have multiple tasks with same priority.  
 
FreeRTOS also always you to have multiple tasks with same priority.  
 
In that case it executes the task with same priority in time slice manner.
 
In that case it executes the task with same priority in time slice manner.
 +
  
 
==API Details==
 
==API Details==
 
Here we will discuss some of the mostly frequently used APIs related to task.
 
Here we will discuss some of the mostly frequently used APIs related to task.
 
====Task Create function====
 
====Task Create function====
<syntaxhighlight>
 
portBASE_TYPE xTaskCreate
 
( pdTASK_CODE pvTaskCode,
 
const portCHAR * const pcName,
 
unsigned portSHORT usStackDepth,
 
void *pvParameters,
 
unsigned portBASE_TYPE uxPriority,
 
xTaskHandle *pvCreatedTask);
 
</syntaxhighlight>
 
 
Name : xTaskCreate()
 
 
Arguments :
 
 
:pvTaskCode : Pointer to the task entry function
 
 
 
:pcName : A descriptive name for the task. It is a character pointer and is mainly used for debugging.
+
{|{{Widget:LibCol}}
 +
{{#Widget:LibTable}}
 +
|-
 +
|Defination ||portBASE_TYPE xTaskCreate (
 +
:pdTASK_CODE pvTaskCode,
 +
:const portCHAR * const pcName,
 +
:unsigned portSHORT usStackDepth,
 +
:void *pvParameters,
 +
:unsigned portBASE_TYPE uxPriority,
 +
:xTaskHandle *pvCreatedTask);
 +
|-
 +
| Input Arguments ||
 +
#pdTASK_CODE : Pointer to the task entry function
 
 
:usStackDepth : The stack size of the task in terms of number of variables not the number of bytes
+
#const portCHAR * : A descriptive name for the task. It is a character pointer and is mainly used for debugging.
 
 
:pvParameters : void pointer which will be used as the parameter for the task
+
#unsigned portSHORT : The stack size of the task in terms of number of variables not the number of bytes
 
 
:uxPriority : The task priority
+
#void* : void pointer which will be used as the parameter for the task  
 
 
:pvCreatedTask : Used to pass back a handle using which the task can be referenced
+
#unsigned portBASE_TYPE : The task priority
 +
 +
#xTaskHandle* : Used to pass back a handle using which the task can be referenced
 +
|-
 +
| Return Value|| Returns TRUE on successful creation of task and adding it to ready list FALSE otherwise.
 +
|-
 +
| Description || It creates a task and adds it to ready list.
 +
|-
 +
| Usage || xTaskHandle TaskHandle_1;
  
Return type : Returns TRUE on successful creation of task and adding it to ready list
+
static void MyTask1(void* pvParameters);
+
:::Returns FALSE otherwise.
+
  
Function : It creates a task and adds it to ready list.
+
xTaskCreate( MyTask1, ( signed char * )"Task1", configMINIMAL_STACK_SIZE, NULL, 1, &TaskHandle_1 );
 +
|}
  
 
====Task Delay Function====
 
====Task Delay Function====
<syntaxhighlight>void vTaskDelay( portTickType xTicksToDelay )</syntaxhighlight>
 
  
Name : vTaskDelay()
+
{|{{Widget:LibCol}}
 
+
{{#Widget:LibTable}}
Arguments : Number of ticks for which task is to be delayed.
+
|-
 
+
|Defination ||void vTaskDelay( portTickType xTicksToDelay )
Return type : None
+
|-
 
+
| Input Arguments ||portTickType : Number of ticks for which task is to be delayed.
Function : It delays the calling task for specified number of ticks.
+
|-
 +
| Return Value|| None
 +
|-
 +
| Description || It delays the calling task for specified number of ticks.
  
 
Set the macro INCLUDE_vTaskDelay to use this API.
 
Set the macro INCLUDE_vTaskDelay to use this API.
Line 95: Line 94:
 
Use the constant portTICK_RATE_MS in the file portmacro.h to calculate real time delay.  
 
Use the constant portTICK_RATE_MS in the file portmacro.h to calculate real time delay.  
 
Its resolution is one tick period.
 
Its resolution is one tick period.
 +
|-
 +
| Usage || vTaskDelay(100);
 +
|}
  
 
====Task Delete Function====
 
====Task Delete Function====
<syntaxhighlight>void vTaskDelete( xTaskHandle pxTask )</syntaxhighlight>
 
  
Name : vTaskDelete()
+
{|{{Widget:LibCol}}
 +
{{#Widget:LibTable}}
 +
|-
 +
|Defination ||void vTaskDelete( xTaskHandle pxTask )
 +
|-
 +
| Input Arguments ||xTaskHandle : handle to the task to be deleted. Pass NULL to suspend calling task
 +
|-
 +
| Return Value|| None
 +
|-
 +
| Description || It removes the specified  task from the RTOS real time kernels management.
 +
But it does not frees the memory allocated by the task code.
  
Arguments : The handle (of type xTaskHandle) of the task to be deleted. Pass NULL to delete calling task.
+
Set the macro INCLUDE_vTaskDelete to use this API.
 +
|-
 +
| Usage || vTaskDelete(TaskHandle_1);
 +
|}
  
Return type : None
+
====Task Suspend Function====
  
Function : It removes the specified  task from the RTOS real time kernels management.
+
{|{{Widget:LibCol}}
But it does not frees the memory allocated by the task code.
+
{{#Widget:LibTable}}
 +
|-
 +
|Defination ||void vTaskSuspend( xTaskHandle pxTaskToSuspend );
 +
|-
 +
| Input Arguments ||xTaskHandle : handle to the task to be suspended. Pass NULL to suspend calling task
 +
|-
 +
| Return Value|| None
 +
|-
 +
| Description || It suspends the specified task
  
Set the macro INCLUDE_vTaskDelete to use this API.
+
Set INCLUDE_vTaskSuspend to use this API.
 +
|-
 +
| Usage || vTaskSuspend(TaskHandle_1);
 +
|}
  
 +
====Task Resume Function====
  
====Task Suspend Function====
+
{|{{Widget:LibCol}}
<syntaxhighlight>
+
{{#Widget:LibTable}}
void vTaskSuspend( xTaskHandle pxTaskToSuspend );
+
|-
</syntaxhighlight>
+
|Defination ||void vTaskResume( xTaskHandle pxTaskToSuspend );
 +
|-
 +
| Input Arguments ||xTaskHandle : handle to the task to be resumed. Pass NULL to suspend the calling task.
 +
|-
 +
| Return Value|| None
 +
|-
 +
| Description || It resumes the specified task
  
Name : vTaskSuspend()
+
Set INCLUDE_vTaskSuspend to use this API.
 +
|-
 +
| Usage || vTaskResume(TaskHandle_1);
 +
|}
  
Arguments :
+
====All Tasks Suspend Function====
  
:pxTaskToSuspend : handle to the task to be suspended. Pass NULL to suspend calling task
+
{|{{Widget:LibCol}}
 +
{{#Widget:LibTable}}
 +
|-
 +
|Defination ||void vTaskSuspendAll(void);
 +
|-
 +
| Input Arguments ||None
 +
|-
 +
| Return Value|| None
 +
|-
 +
| Description || It suspends all real time kernel activity while keeping interrupts (including the kernel tick) enabled.
 +
After calling vTaskSuspendAll () the calling task will continue to execute until xTaskResumeAll () is called.
 +
|-
 +
| Usage || vTaskSuspendAll();
 +
|}
  
Return Type : None
+
====All Tasks Resume Function====
  
Function : It suspends the specified task
+
{|{{Widget:LibCol}}
 +
{{#Widget:LibTable}}
 +
|-
 +
|Defination ||portBASE_TYPE xTaskResumeAll( void );
 +
|-
 +
| Input Arguments ||None
 +
|-
 +
| Return Value||  Returns TRUE if resuming the scheduler caused a context switch, FALSE otherwise
 +
|-
 +
| Description || Resumes real time kernel activity following a call to vTaskSuspendAll ()
 +
|-
 +
| Usage || vTaskSuspendAll();
  
Set INCLUDE_vTaskSuspend to use this API.
+
.
  
====Task Resume Function====
+
.
<syntaxhighlight>
+
void vTaskResume( xTaskHandle pxTaskToSuspend );
+
</syntaxhighlight>
+
  
Name : vTaskResume()
+
.
  
Arguments :
+
xTaskResumeAll();
 +
|}
  
:pxTaskToSuspend : handle to the task to be resumed. Pass NULL to suspend calling task
+
====Get Task Priority====
  
Return Type : None
+
{|{{Widget:LibCol}}
 +
{{#Widget:LibTable}}
 +
|-
 +
|Defination ||unsigned portBASE_TYPE uxTaskPriorityGet ( xTaskHandle pxTaskToSuspend );
 +
|-
 +
| Input Arguments ||xTaskHandle : handle to the task to be queried. Pass NULL to query the calling task.
 +
|-
 +
| Return Value|| unsigned portBASE_TYPE : the priority of the specified task.
 +
|-
 +
| Description || It is used to get the priority of a task.
 +
Set the constant INCLUDE_vTaskPriorityGet to 1.
 +
|-
 +
| Usage ||
 +
unsigned int task_prio;
 +
 
 +
task_prio = xTaskPriorityGet (TaskHandle_1);
 +
|}
 +
 
 +
====Set Task Priority====
 +
 
 +
{|{{Widget:LibCol}}
 +
{{#Widget:LibTable}}
 +
|-
 +
|Defination ||void vTaskPrioritySet( xTaskHandle pxTask, unsigned portBASE_TYPE
 +
uxNewPriority )
 +
|-
 +
| Input Arguments ||
 +
#xTaskHandle : Handle to the task of which the priority is being set. Pass NULL to set the priority of calling task
 +
#unsigned portBASE_TYPE : priority to be set
 +
|-
 +
| Return Value|| None
 +
|-
 +
| Description || It is used to set the priority of a task.
 +
Set the constant INCLUDE_vTaskPrioritySet to 1.
 +
|-
 +
| Usage ||
 +
xTaskPrioritySet (TaskHandle_1, 4);
 +
|}
  
Function : It resumes the specified task
 
  
Set INCLUDE_vTaskSuspend to use this API.
 
  
 
{{DISQUS}}
 
{{DISQUS}}

Latest revision as of 16:26, 20 April 2015

Amruta (talk) 13:41, 8 April 2015 (IST)


Intro

In RTOS implementation of a design, the program is divided into different independent functions what we call as a task. These functions are not called anywhere in the program, they are just created. Each tasks runs continuously i.e. in infinite loop.

The kernel is responsible for the management of all the tasks. Check this tutorial to know working of kernel and task scheduling.

Different states of a task

In the FreeRTOS a task can be in either of four different states viz., Running, Ready, Blocked and Suspended.

Lets see each state one by one.

  • Running
The task which is executing currently is said to be in running state. It owns the CPU.
  • Ready
The task which is neither suspended nor blocked but still not executing will be ready state. Its not in running state because either a high priority or equal priority task is executing.
  • Blocked
A task will go in blocked state whenever its waiting for a event to happen. The event can be completing a delay period or availability of a resource. The blocked tasks are not available for scheduling.
  • Suspended
When vTaskSuspend() is called, the task goes in suspended state. To resume it, xTaskResume() is called. The suspended tasks are also not available for scheduling.
It seems that the tasks in both Blocked and Suspended are in waiting state. But the difference is, a suspended task can't come in Ready state unless xTaskResume() is called. Time out period can be specified only for Blocked tasks but not for suspended tasks.

Deciding Priority of a task

One general rule to decide the task priority is RMS ( Rate Monotonic Scheduling ). According to this rule, the most frequently executed task is given highest priority.

In FreeRTOS, higher is the priority number, higher is the task priority. FreeRTOS also always you to have multiple tasks with same priority. In that case it executes the task with same priority in time slice manner.


API Details

Here we will discuss some of the mostly frequently used APIs related to task.

Task Create function

{{#Widget:LibTable}}
Defination portBASE_TYPE xTaskCreate (
pdTASK_CODE pvTaskCode,
const portCHAR * const pcName,
unsigned portSHORT usStackDepth,
void *pvParameters,
unsigned portBASE_TYPE uxPriority,
xTaskHandle *pvCreatedTask);
Input Arguments
  1. pdTASK_CODE : Pointer to the task entry function
  1. const portCHAR * : A descriptive name for the task. It is a character pointer and is mainly used for debugging.
  1. unsigned portSHORT : The stack size of the task in terms of number of variables not the number of bytes
  1. void* : void pointer which will be used as the parameter for the task
  1. unsigned portBASE_TYPE : The task priority
  1. xTaskHandle* : Used to pass back a handle using which the task can be referenced
Return Value Returns TRUE on successful creation of task and adding it to ready list FALSE otherwise.
Description It creates a task and adds it to ready list.
Usage xTaskHandle TaskHandle_1;

static void MyTask1(void* pvParameters);

xTaskCreate( MyTask1, ( signed char * )"Task1", configMINIMAL_STACK_SIZE, NULL, 1, &TaskHandle_1 );

Task Delay Function

{{#Widget:LibTable}}
Defination void vTaskDelay( portTickType xTicksToDelay )
Input Arguments portTickType : Number of ticks for which task is to be delayed.
Return Value None
Description It delays the calling task for specified number of ticks.

Set the macro INCLUDE_vTaskDelay to use this API.

Use the constant portTICK_RATE_MS in the file portmacro.h to calculate real time delay. Its resolution is one tick period.

Usage vTaskDelay(100);

Task Delete Function

{{#Widget:LibTable}}
Defination void vTaskDelete( xTaskHandle pxTask )
Input Arguments xTaskHandle : handle to the task to be deleted. Pass NULL to suspend calling task
Return Value None
Description It removes the specified task from the RTOS real time kernels management.

But it does not frees the memory allocated by the task code.

Set the macro INCLUDE_vTaskDelete to use this API.

Usage vTaskDelete(TaskHandle_1);

Task Suspend Function

{{#Widget:LibTable}}
Defination void vTaskSuspend( xTaskHandle pxTaskToSuspend );
Input Arguments xTaskHandle : handle to the task to be suspended. Pass NULL to suspend calling task
Return Value None
Description It suspends the specified task

Set INCLUDE_vTaskSuspend to use this API.

Usage vTaskSuspend(TaskHandle_1);

Task Resume Function

{{#Widget:LibTable}}
Defination void vTaskResume( xTaskHandle pxTaskToSuspend );
Input Arguments xTaskHandle : handle to the task to be resumed. Pass NULL to suspend the calling task.
Return Value None
Description It resumes the specified task

Set INCLUDE_vTaskSuspend to use this API.

Usage vTaskResume(TaskHandle_1);

All Tasks Suspend Function

{{#Widget:LibTable}}
Defination void vTaskSuspendAll(void);
Input Arguments None
Return Value None
Description It suspends all real time kernel activity while keeping interrupts (including the kernel tick) enabled.

After calling vTaskSuspendAll () the calling task will continue to execute until xTaskResumeAll () is called.

Usage vTaskSuspendAll();

All Tasks Resume Function

{{#Widget:LibTable}}
Defination portBASE_TYPE xTaskResumeAll( void );
Input Arguments None
Return Value Returns TRUE if resuming the scheduler caused a context switch, FALSE otherwise
Description Resumes real time kernel activity following a call to vTaskSuspendAll ()
Usage vTaskSuspendAll();

.

.

.

xTaskResumeAll();

Get Task Priority

{{#Widget:LibTable}}
Defination unsigned portBASE_TYPE uxTaskPriorityGet ( xTaskHandle pxTaskToSuspend );
Input Arguments xTaskHandle : handle to the task to be queried. Pass NULL to query the calling task.
Return Value unsigned portBASE_TYPE : the priority of the specified task.
Description It is used to get the priority of a task.

Set the constant INCLUDE_vTaskPriorityGet to 1.

Usage

unsigned int task_prio;

task_prio = xTaskPriorityGet (TaskHandle_1);

Set Task Priority

{{#Widget:LibTable}}
Defination void vTaskPrioritySet( xTaskHandle pxTask, unsigned portBASE_TYPE

uxNewPriority )

Input Arguments
  1. xTaskHandle : Handle to the task of which the priority is being set. Pass NULL to set the priority of calling task
  2. unsigned portBASE_TYPE : priority to be set
Return Value None
Description It is used to set the priority of a task.

Set the constant INCLUDE_vTaskPrioritySet to 1.

Usage

xTaskPrioritySet (TaskHandle_1, 4);