Timer Example
The TIMER class inherits from EVENT, so all timers can also be used as EVENTs. KoliadaES follows an event-driven based programming paradigm where a main loop triggers a call back to the event handler containing methods or subroutines for the Application Firmware. The TIMER class inherits from EVENT, so all timers can also be used as EVENTs. Thus, you can use a timer as an event any time. For more information on timers refer here
Hardware/Boards Required
Before you proceed with this tutorial, you must have the latest KoliadaESDK(TM) and KoliadaES environment setup. For more information on the setup check out Koliada Getting started. This example will run on any processor board that has UART serial communication and whereas this tutorial uses the PIEP CC2541, you can use any compatible processor board.
Step 1
Before working on this example, make sure you have set the latest KoliadaESDK(TM). For information on KoliadaESDK(TM) setup see here.
Note: After making sure the SDK is setup correctly, the BuildConfig needs be built & flashed only if you have changed any board configuration. You can find an example BoardConfig project here: KoliadaESDK(TM)/examples/BoardConfig.
In this particular example there are no I/O configuration changes, and we can move on to build and flash the TestTimer application.
Step 2
You find the source for TestTimer here KoliadaESDK(TM)/examples/TestTimer.
Now build the project and hit 'Download to Target'.
You should see the following output on your serial terminal
Fig. 2: Output of TestTimer Example
The output shows the TestTimer program being loaded which prints out the various information about the events and timers, currently live or fired events, running timers etc. and then keeps printing out the spin characters every time the timer is fired which is every second.
Explanation
void TestTimer() { printf(">>%s\n", __func__); // create a timer // the memory for timer is in user space // objectCreate initializes testTimer as a member of class TIMER // interval timers never stop firing until told to objectCreate(testTimer, TimerType, TICKS(1000)); // connect handler to timer // the handler is the function that will be called whenever the timer 'fires' // this is the same call as for event (inherited function) OnEvent(testTimer, (HANDLER) testTimerHandler); // this example can run on any board as long as the board config defines "LED1" word LED1 = GPIO.FIND("LED1"); // find 'LED1' if (LED1 == NULL_PIN) sys.Fatal(__FUNC__, __LINE__, "LED1 is undefined!\n"); // start the timer, passing the led to be used to the handler cmStartTimer(testTimer, LED1); // see what we made objectPrintf(testTimer); // this may not be implemented (depends on SDK) #if 1 cmShowTimers(); #else STATUS __timer_showTimers(); __timer_showTimers(); #endif #if TimerType == kSingleShotTimer #define TimerTypeSelected // wait for singleton to fire WaitEvent(testTimer, 0); #endif #if TimerType == kIntervalTimer #define TimerTypeSelected #if 0 // wait for multiple timer events word i; for (i = 0; i < 50; i++) // wait for timer to fire WaitEvent(testTimer, 0); // stop the timer from running cmStopTimer(testTimer); #else // let the timer run forever // you can optionally stop the timer from within the handler WaitEvent(0, 0);// never returns #endif #endif #ifndef TimerTypeSelected #error You must select a timer type!! #endif // delete the timer objectDelete(testTimer); // we're done printf("<<%s\n", __func__); }
We can walkthrough TestTimer.c now.
The objectCreate(testTimer, TimerType, TICKS(1000)); creates a timer under the name testTimer(which is supposed to happen when ever every 1000ms). On this timer event (OnEvent(testTimer, (HANDLER) testTimerHandler);) the testTimerHandler will be called. Also once the timer event is defined and eventhandlers are set for testtimer we need to enable/start the timer using cmStartTimer(testTimer, LED1);.
Now since all the events are set the system goes to event listening mode. This is done by using WaitEvent(0, 0); which waits for any event.
DefineTimer(testTimer); void testTimerHandler(TIMER t, word led) { // called when the timer expires // when the timer expires, KoliadaES passes the args defined for the timer // in cmStartTimer(testTimer, LED1) below, to the event handler. GPIO.XOR(led); #if 0 // see what changed objectPrintf(testTimer); #else _spinchar(); #endif }
The TestTimerHandler is called every time the timer expires which is 1 sec and it simply toggles the system LED and prints out the spin char.