Develop Multi-Tasks Projects on Energia

This section requires a compatible board running on Energia MT.

Because the underlying framework of Energia MT or Energia Multi-Tasking is TI-RTOS, a real time operating system from Texas Instruments, projects with multiple tasks are possible.

As a consequence, the project has multiple .ino files. This is the only case a project can have multiple .ino files.

By default, the project has one task on the default .ino file, with a setup() function and a loop() function.

For an introduction to RTOS,

Add a Task

Adding a new task can be done in two ways.

Create a New Task on a Specific Sketch

The recommended solution creates a specific .ino sketch file for the new task.

To create a specific .ino file for the new task.

  • Call the menu File > New > New File... or press Cmd+N.

  • Select the macOS option.

  • Scroll down to the embedXcode or embedXcode+ group.

  • Select Task Energia MT.

The Task Energia MT files defines a new task with the setup() and loop() functions.



  • Give a name to the task, TaskCode is proposed by default.

The new task contains two functions, TaskCode_setup() and TaskCode_loop(), based on the name of the task TaskCode.ino.

// Setup
void TaskCode_setup()
{
    ;
}

// Loop
void TaskCode_loop()
{
    ;
}

Add Functions to Main Sketch

An alternative solution simply adds the functions of the new task to the default main .ino sketch file.

To add a task to the default main .ino file, for example task2,

Define two new functions setupTask2() and loopTask2() on the existing main sketch.

void setupTask2()
{

}

void loopTask2()
{

}

Check New Task Functions Names

Here are the requirements for naming the setup() and loop() functions of the new task.

  • Give a different name to each task.

  • Define a setup() function and a loop() function for each task.

Include setup in the name of the setup() function of a task.

void TaskCode_setup()
void setupTaskCode()
void TaskSetupCode()
  • Include loop in the name of the loop() function of a task.
void TaskCode_loop()
void loopTaskCode()
void TaskLoopCode()
  • Check the setup() and loop() functions share the same name inside the task, here TaskCode.
void TaskCode_setup()
void TaskCode_loop()
  • Check each setup() function and each loop() function has a unique name across the whole project.

For more information about Energia MT,

For more information on TI-RTOS,

Check the Tasks File

During compilation and linking, embedXcode parses the different .ino files, selects the setup() and loop() functions, and generates the rtosTasks.h file automatically.

This file describes the different tasks. It includes calls to each .ino sketch files, except the default main .ino sketch file of the project, declares the tasks with their setup() and loop() functions, and lists the names of the tasks.

There is no #include for the default embed1.ino file of the embed1 project, because the main.cpp file already mentions it. If one function is missing, an error is raised.

==== Check project embed1 ====
ERROR   Different number of setup() and loop()
ERROR   TaskCode.ino: no setup() function

It is a good idea to have a look at it to check that all the tasks are correctly mentioned. Do not edit this file as it is generated automatically at each compilation.

For more information about Energia MT,

Here is an example of a populated rtosTasks.h file.

//
// rtosTasks.h
// Header file
// ----------------------------------
//
// List of tasks for project embed-rtos
//
// File generated by embedXcode_check
// Sat Sep 30 11:11:47 CEST 2017
//
// ----------------------------------
// DO NOT EDIT THIS FILE.
// ----------------------------------
//

// List of sketches to #include
//
#include "TaskCode.ino";
// embed_rtos.ino already #included in main.cpp

// List of setup() and loop() functions 
//
extern void TaskCode_setup();
extern void TaskCode_loop();
extern void setup();
extern void loop();

// Number of tasks
//
#define NUM_SKETCHES 2

// List of pointers to setup() and loop()
//
void (*func_ptr[NUM_SKETCHES][2])(void) = {
 { TaskCode_setup, TaskCode_loop },
 { setup, loop },
};

// Names of the tasks
//
const char *taskNames[] = {
 "TaskCode_loop",
 "loop",
};

Add Global Variables and Main Setup Function

An optional file rtosGlobals.h contains global variables and a optional main setup function rtosSetup().

This main setup function rtosSetup() runs before all other setup() functions of the tasks.

// Add optional rtosSetup function
void rtosSetup()
{
    ;
}

This is especially useful for initialising shared resources like Serial, I²C and SPI ports, and shared RTOS elements like clock, mailbox, semaphore.

All tasks in the .ino files need to include rtosGlobals.h.

// Include application, user and local libraries
#include "rtosGlobals.h"

Please note this feature is specific to embedXcode and is not supported by the Energia IDE.