In GeMRTOS on Altera FPGA platforms with Nios V processors, a task is the fundamental unit of execution. Every task consists of three components: a GS_TCB (Task Control Block) that stores task management data, the task code (a subroutine function loaded into system memory), and a task stack that preserves the suspended task’s state so it can be resumed correctly. Stack memory is reserved at task creation time. This guide covers the five GeMRTOS task types and the core task API functions.
GeMRTOS Task Types #
GeMRTOS defines five task types. Additional types can be defined to extend the system:
- G_TCBType_OneShot — the task code executes once per release. If the task code does not contain an infinite loop, the task must be re-released for each subsequent execution. Initialization tasks are commonly implemented this way. When implemented as an infinite loop, the task runs continuously and can monopolize processor time — use one of these strategies to prevent starvation:
- Assign the lowest priority so the task runs only when higher-priority tasks are idle.
- Suspend the task inside the loop waiting for a timed or trigger event.
- Reduce the task priority inside the loop to yield to newly ready higher-priority tasks.
- Define a round-robin scheduling mechanism on the scheduling list to share processor time equally.
- G_TCBType_Periodic — the task code executes periodically. The period and initial offset are configured when the type is specified and determine all future release times. If a previous invocation does not complete before the next release, it can be configured to abort the previous invocation or skip the next release. Periodic tasks are suited for cyber-physical applications requiring compliance with the Nyquist-Shannon sampling theorem. Schedulability analysis is recommended to avoid deadline misses under high load.
- G_TCBType_ISR — the task is associated with a trigger resource and acts as an Interrupt Service Routine. The task type is set after creation using the
gu_TriggerRegisterTaskfunction. - G_TCBType_IDLE — the task executed by a processor when no other task requires execution. One IDLE task exists per system processor. By default, the IDLE task puts the processor into sleep mode to save energy and reduce system bus utilization.
- G_TCBType_Periodic_Skip — declared in the task-type enumeration alongside
G_TCBType_Periodic. The kernel currently schedules it identically toG_TCBType_Periodic.
Task API Reference #
Creating a Task #
Create a new task with default settings using:
void *gu_TaskCreate(void *TaskCode, void *p_arg, char *format, ...);The gu_TaskCreate function creates a task and returns a pointer to its GS_TCB structure. Task parameters can be adjusted before creation by modifying default settings, or after creation using task-related functions. The function accepts an optional printf-style format string to assign a human-readable description to the task.
| Parameter | Description |
|---|---|
TaskCode | Pointer to the function that implements the task’s code (the task entry point). |
p_arg | Pointer to the argument passed to TaskCode on each invocation. Cast to the required type within the task code. |
format | Printf-style format string to create a task description (up to G_TCB_DESCRIPTION_LENGTH characters). Supports format specifiers replaced by subsequent arguments. |
Returns: pointer to the GS_TCB structure of the newly created task. NULL indicates failure.
Starting Task Execution #
Schedule a created task to begin execution with an optional time offset 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, with an optional delay before its first execution. The offset is measured from the time the function is called.
| Parameter | Description |
|---|---|
ptcb | Pointer to the GS_TCB structure of the task to start (returned by gu_TaskCreate). |
hours | Hours component of the start offset. |
minutes | Minutes component of the start offset. |
seconds | Seconds component of the start offset. |
ms | Milliseconds component of the start offset. |
Returns: G_TRUE on successful task startup.
Suspending a Task for a Time Interval #
Suspend the currently running task for a specified duration using:
G_INT32 gu_TaskDelay(G_INT32 hours, G_INT32 minutes, G_INT32 seconds, G_INT32 ms);The gu_TaskDelay function suspends the calling task for the specified time interval. Commonly used inside a G_TCBType_OneShot infinite loop to create periodic-like behavior without using a periodic task type.
| Parameter | Description |
|---|---|
hours | Hours to suspend. |
minutes | Minutes to suspend. |
seconds | Seconds to suspend. |
ms | Milliseconds to suspend. |
Returns: always G_TRUE.
Key Takeaways #
- Every GeMRTOS task on Nios V FPGA is defined by three components: a GS_TCB control block, task code (subroutine function), and a task stack allocated at creation time.
- G_TCBType_OneShot tasks execute once per release — use priority assignment, event suspension, priority reduction, or round-robin scheduling to prevent infinite-loop variants from starving other tasks.
- G_TCBType_Periodic tasks are released at fixed intervals and are the correct choice for signal-processing applications that must comply with the Nyquist-Shannon theorem.
- G_TCBType_ISR tasks are linked to trigger resources via
gu_TriggerRegisterTask; G_TCBType_IDLE tasks run when no other task is ready and put the processor to sleep by default. - Use gu_TaskCreate to allocate a task, gu_TaskStartWithOffset to schedule its first execution, and gu_TaskDelay inside the task body to yield the processor for a fixed interval.