Tasks in GeMRTOS #
A task in GeMRTOS is defined as a unit of execution. The main components of a task implementation are:
- GS_TCB : this is a control block that stores the information required for task management.
- task code : this is the executable code, loaded into the system memory, that implements a task function when it is executed by a processor. Task code is implemented a subroutine function.
- task stack : this is data storage to preserve the status of a suspended task in order to resume it properly and store subroutine data. The task stack memory is reserved when the task is created. The amount of memory assigned to the task stack is defined by default.
Types of GeMRTOS tasks #
The following are the type of tasks in GeMRTOS, but more can be defined:
tcbtype
Enumerator G_TCBType_UCOS The G_TCBType_UCOS task type makes the task code to be executed just once. The task must be released once again for another execution if the task code does not contain an infinite loop. Initialization tasks may be implemented as a G_TCBType_UCOS task type without infinite loop in the task code. G_TCBType_UCOS tasks are often implemented as an infinite loop to keep them running. When a task with an infinite loop is executed, it will take as much processor time as possible. It is possible to use different strategies to prevent one or many system tasks from being overly greedy about processor time and starving the others:
- Assigning lowest priorities to tasks: in this way, infinite-loop tasks will be executed only when the highest priority tasks are not requiring for execution.
- Suspending the task until an event: the task is suspended inside the infinite loop, waiting for an event. The events may be timed events (to execute the task regularly) or trigger events (such as waiting for an interrupt).
- Reducing the task priority: the task priority may be reduced inside the infinite loop to let the new higher-priority task be executed. This tactic should be implemented in all the infinite-loop tasks of the scheduling list to dynamically preserve a valid relationship among the system task priorities.
- Defining a round-robin scheduling mechanism in the scheduling list: a round-robin mechanism will execute each task during a certain interval, granting the processor access to each task in the scheduling list.
G_TCBType_PERIODIC The G_TCBType_PERIODIC task type makes the task code to be executed periodically. The period of the task is configured when the type is specified. The period of the task and the initial offset determines the future releases times of the task. If previous invocation of the task does not completes, then the previous invocation may be defined to be aborted or the next release skipped. Periodic tasks are useful to meet Nyquist and Shannon theorems in cyber-physical applications. However, if no scheduling analysis is performed, the system may became oversaturated and the deadlines missed.
- Remarks
- G_TCBType_PERIODIC
G_TCBType_ISR The G_TCBType_ISR task type determines that the task is associated with a trigger resource. The G_TCBType_ISR type of the task should be set after the task is created using the gu_trigger_register_task function.
- Remarks
- G_TCBType_ISR
G_TCBType_IDLE The G_TCBType_IDLE task type determines the task that a processor executes when no task requires for execution. The IDLE task is a GeMRTOS system task and there is one for each system processor. By default, the G_TCBType_IDLE task turns the processor into sleep mode in order to save energy and reduce the system bus utilization.
- Remarks
- G_TCBType_IDLE
G_TCBType_UNDEFINED Task type UNDEFIEND. When not a specific type is given to the task
- Remarks
- G_TCBType_UNDEFINED
Main task related functions #
Creating and initializing a task #
A task can easily be created using:
void *gu_TaskCreate(void *TaskCode, void *p_arg, char *format, ...);
The gu_TaskCreate function creates a task with default settings and returns a pointer to its GS_TCB structure. Task parameters can be modified before creation by adjusting default settings or after creation using task-related functions. While the function requires only TaskCode and p_arg, it allows for optional task description formatting using a printf-style format string and arguments.The task is created with the following paramenters:
TaskCode: A pointer to the function that implements the task's code (the task's entry point). It is the name of the function that implements the task code.
p_arg: A pointer to an argument that will be passed to the TaskCode function each time the task is invoked. This is a void * and can be cast to other types within the task code.
format: A format string, similar to printf, used to create a description string for the task (up to G_TCB_DESCRIPTION_LENGTH characters). This string can contain format specifiers that are replaced by subsequent arguments.
The gu_TaskCreate function returns a pointer to the GS_TCB structure of the newly created task. This pointer should be used in all subsequent calls related to that task. A NULL return indicates task creation failure.Starting task execution #
A task is schedule to start execution using:
G_INT32 gu_TaskStartWithOffset(GS_TCB *ptcb, unsigned int hours, unsigned int minutes, unsigned int seconds, unsigned int ms);
The gu_TaskStartWithOffset function starts a previously created task for execution, allowing the specification of a time offset for the task's first execution. This offset determines when the task will begin running relative to the time the function is called.The task is started with the following paramenters:
ptcb: A pointer to the GS_TCB structure of the task to be started (obtained from gu_TaskCreate during task creation).
hours: The number of hours in the starting offset.
minutes: The number of minutes in the starting offset.
seconds: The number of seconds in the starting offset.
ms: The number of milliseconds in the starting offset.
The gu_TaskStartWithOffset function returns G_TRUE upon successful task startup.Suspending the execution of a task for a certain interval #
A task can be suspended for a certain interval using:
G_INT32 gu_TaskDelay(G_INT32 hours, G_INT32 minutes, G_INT32 seconds, G_INT32 ms);
The gu_TaskDelay function suspends the execution of the currently running task for a specified time interval. This function is useful within the infinite loop of a task to create periodic behavior.The task is suspended with the following paramenters:
hours: The number of hours to sleep
minutes: The number of minutes to sleep.
seconds: The number of seconds to sleep.
ms: The number of milliseconds to sleep.
The gu_TaskDelay function always returns G_TRUE.