SWI Library
The TI-RTOS Energia MT is built upon includes four major types of threads:
SWIs and tasks can have up to 32 levels of priority. For more information, please refer to the SYS/BIOS (TI-RTOS Kernel) v6.41 User's Guide. |
Unfortunately, the underlying SWI libraries are no longer included in the Energia MT distribution. |
The rtosGlobals.h header file contains all the elements shared across the different tasks and includes the mains setup function rtosSetup().
|
// *** Header rtosGlobals.h #ifndef rtosGlobals_h #define rtosGlobals_h // Core library #include "Energia.h" // Include application, user and local libraries #include "Semaphore.h" #include "SWI.h" #include "Event.h" // Prototypes // Define variables and constants Semaphore mySemaphore; SWI mySWI; uint32_t chrono; Event myEvent; |
The function associated with the mySWI event posts the myEvent event.
The function attached to the falling PUSH1 event posts the mySWI event. |
// SWI function void functionSWI() { chrono = millis(); myEvent.send(); } // PUSH1 function void functionPUSH1() { mySWI.post(); } |
The main setup function, rtosSetup(),
Function functionPUSH1 has the highest priority, followed by function functionSWI and then the 4 tasks similar to the example for the semaphore library. |
// Add optional rtosSetup function void rtosSetup() { Serial.begin(115200); mySemaphore.begin(1); mySWI.begin(functionSWI); myEvent.begin();
mySemaphore.waitFor(); Serial.println("rtosSetup"); mySemaphore.post();
pinMode(PUSH1, INPUT_PULLUP); attachInterrupt(PUSH1, functionPUSH1, FALLING); } #endif |
The specific sketch greenLED.ino initialises the GREEN_LED output and waits for the event myEvent to blink the green LED.
The other sketches or tasks are similar to the example for the semaphore library. |
// *** Sketck greenLED.ino // Setup void greenLED_setup() { pinMode(GREEN_LED, OUTPUT); } // Loop void greenLED_loop() { myEvent.waitFor();
blink(GREEN_LED, 3, 333);
digitalWrite(RED_LED, LOW); digitalWrite(BLUE_LED, LOW);
mySemaphore.waitFor();
Serial.print(millis(), DEC); Serial.println("\t4\t*");
mySemaphore.post(); } |
SWI with Trigger Library Variant
The SWI offers an interesting option called trigger.
The SWI mySWItrigger is defined with a trigger, here 3. The function functionSWItrigger associated with mySWItrigger event is fired
So the button PUSH1 needs be pressed and released three times before the function functionSWItrigger is fired and myEvent sent. The example includes additional LEDs,
Once functionSWItrigger, has run, the count resumes the initial value of the trigger, here 3. |
// *** Header rtosGlobals.h
#include "SWItrigger.h"
SWItrigger mySWItrigger; // SWItrigger function void functionSWItrigger() { chrono = millis(); digitalWrite(BLUE_LED, LOW); digitalWrite(RED_LED, HIGH); myEvent.send(); }
void functionPUSH1() { digitalWrite(RED_LED, LOW); digitalWrite(BLUE_LED, HIGH); mySWItrigger.dec(); } // Add optional rtosSetup function void rtosSetup() { pinMode(RED_LED, OUTPUT); pinMode(BLUE_LED, OUTPUT);
Serial.begin(115200); mySemaphore.begin(1); mySWItrigger.begin(3, functionSWItrigger); myEvent.begin();
mySemaphore.waitFor(); Serial.println("rtosSetup"); mySemaphore.post();
// Solution 2: Energia interrupt pinMode(PUSH1, INPUT_PULLUP); attachInterrupt(PUSH1, functionPUSH1, FALLING); } #endif |