chipKIT® Development Platform

Inspired by Arduino™

chipKIT Task Manager Example

Posted 2015-09-16 13:34:49 by Majenko

FubarinoMiniTaskMangement.jpg

Besides the many Arduino-based core functions available to you, the chipKIT platform also provides chipKIT-specific core functions for the core timer service and task management.

In this post, we focus on task management, by providing a simple example. Task Management essentially allows for multiple tasks to run in the background, which can simplify the programming in your loop() function. Read the overview on Task Management for more information. The example below animates an LED in the background by using the createTask() core function. Notice how this task-management functionality provides for a simple RTOS-like environment.

#include <SoftPWMServo.h>

char dim = 0;

/* Using the Software-based PWM Servo library, 
 *  the UpdateLED() function below dims the 
 *  PIN_LED1 (You can reference the actual pin 
 *  number for your board using the Board_Defs.h 
 *  file for your particular chipKIT board)
 */
void UpdateLED(int id, void * tptr) 
{
    SoftPWMServoPWMWrite(PIN_LED1, dim++);

    if (dim == 70) {
        dim = 0;
  }
}

void setup() {

    /* createTask() is a chipKIT "Task Management" 
     *  core function. In this case, it takes the  
     *  function we defined above, UpdateLED(), as a 
     *  task to perform. It provides a scheduling interval
     *  of 20 milliseconds and initializes the enable 
     *  state of the task. When the board is turned on
     *  this setup() will run, and the new task will be
     *  enabled.
     */
    createTask(UpdateLED, 20, TASK_ENABLE, NULL);
}

/*
 * Notice, there is no need to run anything else in the loop() 
 *  portion for the UpdateLED() "task" to run in the background.
 */
void loop() {}