Energia: Code on a Timer with Tiva / Stellaris Launchpads

Do you wish you could smoothly fade LEDs without having to code that into the main loop of your program?  Do you wish you could sample sensors or input ports an a regular basis while doing other activities in the main part of your Energia program?  Are you looking for simple ways of creating periodic activities without having to worry about timing loops or how long other processing takes in your loop() function?  If so, then you want to use a timer interrupts!

This post describes how to run time based code in a Tiva/Stellaris Launchpad with 1ms precision. The same code runs on the MSP430 Launchpad with 2ms precision. Your function will be invoked at the interval you register without having to integrate any code in your run loop. The timer is in github along with a simple blinking light demo program.


AnyMsTimer calls a function()  on regular intervals independent of work you are doing in the the loop() function or the functions it calls. Register your function with AnyMsTimer along with the millisecond interval desired between calls.  Start the timer when you're ready to start receiving messages.


  1. The 2015/02 implementation supports only one timer function.
  2. You should do as little as possible in your timer function.  Other processor activities like UART or communication may be paused during your handler.
  3. The MSP430 timer behavior is different since there is no SysTick feature.

Basic operation

Energia configures Systick  to invoke functions in SysTickHandler every 1 ms.  You setup AnyMsTimer, passing your function and the desired calling interval. SysTickHandle() calls AnyMsTimer every millisecond. It counts the number of millisconds until it reaches the configured threshold when it calls your function. This continues process repeats

More info on Underlying Architecture

SysTick functionality is part of the core ARM standard.  All CPUs Invoke a function whose address is stored in the ARM Interrupt vector table and the SysTick address. Energia defines the interrupt vector table in startup_gcc.c.  It puts the address of SysTickIntHandler() in the interrupt vector table. 

User programs register their call back functions with registerSysTickCb().  That function stores the registered function in the SysTickCbFuncs[] array. AnyTimerMs (2/2015) only supports one timer function even though registerSysTickCb() appears to support up to 8 functions.

SysTickIntHandler is implemented in wiring.c. SysTickIntHandler is invoked every tick cycle, one time per millisecond on the LM4F and other boards.  SysTickIntHandler() sequentially invokes void function(uint323_t)  functions stored in SysTickCbFuncs[].  

The vector entry cannot be moved on ARM CPUs.  SysTickIntHandler() cannot be changed without changing the Energia library code.

Related Energia code in GitHub

Last Modified