Difference between revisions of "RTOS Basics : TASK"
m |
m |
||
Line 120: | Line 120: | ||
Arguments : | Arguments : | ||
− | :pxTaskToSuspend : handle to the task to be suspended | + | :pxTaskToSuspend : handle to the task to be suspended. Pass NULL to suspend calling task |
− | + | ||
− | + | ||
Return Type : None | Return Type : None | ||
Line 129: | Line 127: | ||
Set INCLUDE_vTaskSuspend to use this API. | Set INCLUDE_vTaskSuspend to use this API. | ||
− | |||
====Task Resume Function==== | ====Task Resume Function==== |
Revision as of 15:46, 7 April 2015
Contents
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.
Now, you may get question, how the application works ?
Here the kernel comes in picture. To know more about kernel check RTOS basics tutorial.
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 in time slice manner.
API Details
Here we will discuss some of the mostly frequently used APIs related to task.
Task Create function
portBASE_TYPE xTaskCreate ( pdTASK_CODE pvTaskCode, const portCHAR * const pcName, unsigned portSHORT usStackDepth, void *pvParameters, unsigned portBASE_TYPE uxPriority, xTaskHandle *pvCreatedTask);
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.
- usStackDepth : The stack size of the task in terms of number of variables not the number of bytes
- pvParameters : void pointer which will be used as the parameter for the task
- uxPriority : The task priority
- pvCreatedTask : Used to pass back a handle using which the task can be referenced
Return type : Returns TRUE on successful creation of task and adding it to ready list
- Returns FALSE otherwise.
Function : It creates a task and adds it to ready list.
Task Delay Function
void vTaskDelay( portTickType xTicksToDelay )
Name : vTaskDelay()
Arguments : Number of ticks for which task is to be delayed.
Return type : None
Function : 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.
Task Delete Function
void vTaskDelete( xTaskHandle pxTask )
Name : vTaskDelete()
Arguments : The handle (of type xTaskHandle) of the task to be deleted. Pass NULL to delete calling task.
Return type : None
Function : 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.
Task Suspend Function
void vTaskSuspend( xTaskHandle pxTaskToSuspend );
Name : vTaskSuspend()
Arguments :
- pxTaskToSuspend : handle to the task to be suspended. Pass NULL to suspend calling task
Return Type : None
Function : It suspends the specified task
Set INCLUDE_vTaskSuspend to use this API.
Task Resume Function
void vTaskResume( xTaskHandle pxTaskToSuspend );
Name : vTaskResume()
Arguments :
- pxTaskToSuspend : handle to the task to be resumed. Pass NULL to suspend calling task
Return Type : None
Function : It resumes the specified task
Set INCLUDE_vTaskSuspend to use this API.