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.
- The 2015/02 implementation supports only one timer function.
- You should do as little as possible in your timer function. Other processor activities like UART or communication may be paused during your handler.
- The MSP430 timer behavior is different since there is no SysTick feature.
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
- startup_gcc.c: CoreTex M3 interrupt handlers on Github in startup_gcc.c
- systick.h: Energia internal SysTick related functions
- energia.h: Definition of registerSysTickSB() used by AnyTimerMs to register your handler.
- wiring.c: Timer , sleep and SysTick integration code.