Task Manager

From chipKIT
(Redirected from TMOverview)
Jump to: navigation, search

The task manager allows the creation and operation of background tasks. A task is a user defined function that is executed automatically at specified intervals or at a specified time. Using tasks can simplify the logic of programs that need to perform periodic operations.

Scheduling of tasks for execution is done prior to each execution of the user function loop() and during time spent in the delay() function. In order for task scheduling to work reliably, the time to execute the user loop() function should be short; preferably less than a millisecond, but no more than a few milliseconds if the timing accuracy of task scheduling needs to be precise. Time spent in task functions contributes to the total execution time of the loop() function, as all task functions currently scheduled for execution will be called before loop() is called.

The actual time at which a task will be run can vary by -0/+N milliseconds from the scheduled time, where N is the longest time that it takes for execution of the user loop() function plus the longest execution time for all tasks that can be scheduled for execution at the same time. The interval from when a task is created or enabled to the time of first execution can be +/- 1 millisecond in addition to the above noted error due to the fact that the user program’s execution is asynchronous to when the millisecond tick counter is updated.

The scheduling times for task functions is based on the system millisecond tick counter. This is the value returned by the millis() function. This tick counter runs continuously and gives the number of milliseconds elapsed since the system started running.

Here is a sample blink sketch using the task manager:

  Blink Task
  Blink LED1 with a 500ms cycle (2Hz)
  Blink LED2 with a 510ms cycle (1.96Hz)
  Utlizing the chipKIT task manager rather than delay functions.
  This example code is in the public domain.
 int blink1_id;
 int blink2_id;
 unsigned long blink1_var;
 unsigned long blink2_var;
 void blink_task1(int id, void * tptr) {
   digitalWrite(PIN_LED1, !digitalRead(PIN_LED1)); // Toggle pin state
 void blink_task2(int id, void * tptr) {
   digitalWrite(PIN_LED2, !digitalRead(PIN_LED2)); // Toggle pin state
 void setup() {
   // initialize the digital pin as an output.
   // Pin 13 has an LED connected on most Arduino boards:
   pinMode(PIN_LED1, OUTPUT);
   pinMode(PIN_LED2, OUTPUT);
   blink1_id = createTask(blink_task1, 250, TASK_ENABLE, &blink1_var);
   blink2_id = createTask(blink_task2, 255, TASK_ENABLE, &blink2_var);
 void loop() {