Wednesday, February 25, 2015

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


Tuesday, February 17, 2015

Web Integration and BDD testing with Selenium, Specflow and MSTest

I occasionally participate in a workshop the LitheSpeed folks where we teach Test First Development using standard unit tests and Business Driven Development using Gherkin.  C# and Java developers participate in a mixed class. The first set of labs involves building a library first using developer style unit tests. The second set of labs involves building a library using BDD techniques.  The class wraps up with building a BDD style set of tests against an existing web site.

I've created a small shell project on GitHub that contains both a basic Selenium unit test and a basic Specflow driven BDD style Selenium test. This makes it easy to start testing without the overhead of creating a new VS solution.  I built this project in VS2013 which has good NuGet, Specflow and Selenium plugin and Nuget support.

The project uses NuGet Package Restore to pull down the necessary dependencies in an almost Maven like fashion. The Selenium Web Testing development team continues to improve their project integrating with NuGet so that you can run Selenium tests without PATH configuration or additional installation. The sample project includes references to the ChromeDriver and the IEDriver in case you want to run the same test in either browser.

Simple Selenium Google search

This class file creates a new test browser instance every time the class is called to run all the tests in it.  It tears down the connection after every test in this class has run. We could run one instance for all tests in all classes or run a new browser instance for each tests.  The "one instance per class" provides some level of isolation while providing decent performance.

This very simple test opens a Google search window, enters a search string, submits the search and then verifies the search string is in the window title.   It is a pretty simple test



Simple Specflow driven Selenium Google search

Specflow BDD tests consist of two parts. The first part is the feature stored in a .feature file that describes the scenario in business terms using the Gherkin syntax.

GoogleSeleniumSpecflowtest.feature in the test project has the following scenario.  Each line in the scenario maps to a step in the test functions. 
Scenario: Search for Something
    Given I want to search with Google
    When When I search for "freemansoft"
    Then That should be in the title bar

SpecFlow breaks the scenario down into a set of cooperating methods where each item in blue text becomes its own method. Scenarios can mix and match Given/When/Then clauses from other tests re-using the code that implements those pieces.



We can create the same scenario for searching with Bing.
Scenario: Search for Something
    Given I want to search with Bing
    When When I search for "freemansoft"
    Then That should be in the title bar
We reuse all the When/Then steps creating only a new Given method



Specflow Note

This example uses the Specflow Context to pass data between the different sections of a scenario.  This is the recommended mechanism for passing data between steps since it lets Specflow manage the lifetime of the data.


This  sample on GitHub is really meant to act as a starting place for writing your own tests in whichever fashion you chose. Hopefully you find it useful for jumpstarting your efforts at improving your product by writing good UI  and functional tests.

Created 2/17/2015