Tasks

TI-RTOS Tasks are equivalent to independent threads that conceptually execute functions in parallel within a single C program. In reality, switching the processor from one task to another helps achieve concurrency. Each Task is always in one of the following modes of execution:

  • Running: task is currently running

  • Ready: task is scheduled for execution

  • Blocked: task is suspended from execution

  • Terminated: task is terminated from execution

  • Inactive: task is on inactive list

One (and only one) task is always running, even if it is only the Idle Task (see Figure 56.). The current running task can be blocked from execution by calling certain Task module functions, as well as functions provided by other modules like Semaphores. The current task can also terminate itself. In either case, the processor is switched to the highest priority task that is ready to run. See the Task module in the package ti.sysbios.knl section of the TI-RTOS Kernel (SYS/BIOS) User’s Guide for more information on these functions.

Numeric priorities are assigned to tasks, and multiple tasks can have the same priority. Tasks are readied to execute by highest to lowest priority level; tasks of the same priority are scheduled in order of arrival. The priority of the currently running task is never lower than the priority of any ready task. The running task is preempted and rescheduled to execute when there is a ready task of higher priority.

In the simple_peripheral application, the Bluetooth Low Energy protocol stack task is given the highest priority (5) and the application task is given the lowest priority (1).

Initializing a Task

When a task is initialized, it has its own runtime stack for storing local variables as well as further nesting of function calls. All tasks executing within a single program share a common set of global variables, accessed according to the standard rules of scope for C functions. This set of memory is the context of the task. The following is an example of the application task being constructed.

Listing 13. A TI-RTOS task
 1#include <xdc/std.h>
 2#include <ti/sysbios/BIOS.h>
 3#include <ti/sysbios/knl/Task.h>
 4
 5/* Task's stack */
 6uint8_t sbcTaskStack[TASK_STACK_SIZE];
 7
 8/* Task object (to be constructed) */
 9Task_Struct task0;
10
11/* Task function */
12void taskFunction(UArg arg0, UArg arg1)
13{
14    /* Local variables. Variables here go onto task stack!! */
15
16    /* Run one-time code when task starts */
17
18    while (1) /* Run loop forever (unless terminated) */
19    {
20        /*
21         * Block on a signal or for a duration. Examples:
22         *  ``Sempahore_pend()``
23         *  ``Event_pend()``
24         *  ``Task_sleep()``
25         *
26         * "Process data"
27         */
28    }
29}
30
31int main() {
32
33    Task_Params taskParams;
34
35    // Configure task
36    Task_Params_init(&taskParams);
37    taskParams.stack = sbcTaskStack;
38    taskParams.stackSize = TASK_STACK_SIZE;
39    taskParams.priority = TASK_PRIORITY;
40
41    Task_construct(&task0, taskFunction, &taskParams, NULL);
42
43    BIOS_start();
44}

The task creation is done in the main() function, before the TI-RTOS Kernel’s scheduler is started by BIOS_start(). The task executes at its assigned priority level after the scheduler is started.

TI recommends using an existing application task for application-specific processing. When adding an additional task to the application project, the priority of the task must be assigned a priority within the TI-RTOS priority-level range, defined in the TI-RTOS configuration file (.cfg).

Tip

Reduce the number of Task priority levels to gain additional RAM savings in the TI-RTOS configuration file (.cfg):

Task.numPriorities = 6;

Do not add a task with a priority equal to or higher than the Bluetooth low energy protocol stack task and related supporting tasks. See Standard Project Task Hierarchy for details on the system task hierarchy.

Ensure the task has a minimum task stack size of 512 bytes of predefined memory. At a minimum, each stack must be large enough to handle normal subroutine calls and one task preemption context. A task preemption context is the context that is saved when one task preempts another as a result of an interrupt thread readying a higher priority task. Using the TI-RTOS profiling tools of the IDE, the task can be analyzed to determine the peak task stack usage.

Note

The term created describes the instantiation of a task. The actual TI-RTOS method is to construct the task. See Creating vs. Constructing for details on constructing TI-RTOS objects.

A Task Function

When a task is initialized, a function pointer to a task function is passed to the Task_construct function. When the task first gets a chance to process, this is the function which the TI-RTOS runs. Listing 13. shows the general topology of this Task function.

In typical use cases, the task spends most of its time in the blocked state, where it calls a _pend() API such as Semaphore_pend(). Often, high priority threads such as Hwis or Swis unblock the task with a _post() API such as Semaphore_post().