Faster Prototyping with Embedded Lua

7 June 2016
| |
Reading time: 4 minutes

Scripting has landed on the microcontroller. Scripting isn’t new for many embedded developers. We’ve been doing it for years in our test system, our build system and code generators. Scripting is at the core of many embedded Linux systems. And scripting is finally well within the reach of microcontrollers.

Scriptable microcontrollers are a game-changer for prototyping: Fewer lines of code, less compiling and flashing, powerful libraries, no memory management. It allows us to focus on the problem and relieves us from all the boring detail that makes embedded tedious at times.

Looking at the economics: The smaller Cortex M3s are currently in the 1-2$ region (distributor prices). Running Lua will add ~120 kB of flash and ~30 kB of RAM to your application, so for prototyping purposes you will want its pin-compatible larger brother (e.g. 512 kB flash, 80 kB RAM, <10$). The bottom line is: Unless you’re aiming at sub-1$ controllers, there is probably a pin-compatible larger controller available that can run Lua.

Lua – What?

Lua is a small yet powerful programming language. It has been around for 20 years, but gaining momentum only as recently as 2008. It is extremely easy to embed into larger programs. This was a design goal from the start and may be its defining feature. Lua itself is written in ISO C90 and assumes nothing about the hosting environment but C and C standard libraries. Embedding Lua into a bare-metal microcontroller project is as trivial as importing a .c and .h file into the project plus ~10 lines of C-code for initialization, passing parameters and calling the script.

The eLua-Project has additional material for reducing the footprint further, libraries for interfacing with common microcontroller peripherals and even file-system support. LuaBridge helps with generating C++ glue. Lua and its ecosystem are licensed as open source, under the very liberal MIT license. Commercial use is unproblematic.

Lua powering your prototype

Beyond loading and starting a Lua script, it may be worth integrating Lua a bit further into the embedded application. We have, for example, integrated Lua with the Zühlke Embedded Platform’s process data layer and component system. Process variables are made available to components written in C/C++, as well as components written in Lua. The latter can be dynamically loaded into the running system via UART. Lua bit code can be executed from ROM, or from RAM if dynamically loaded.

-- Rapid prototyping of control algorithm
temperature = TemperatureSensor:get()
setpoint = SetpointControl:get()
        temperature, setpoint, state))

Bit code loading is what shaves off a full minute of the compile / flash / test-cycle. It may not sound like a big deal, but once you’ve waited through a thousand flash cycles, you might think otherwise. Another prominent feature is connecting Lua directly to the UART in interactive mode. This gives you a Lua shell on the running embedded system. It means live introspection and manipulation at a layer of abstraction that is not available to debuggers.

Lua powering your test set-up

Once out of the prototyping phase, with Lua-written components re-implemented in C/C++, it still pays off to keep the Lua-bindings up to date. Lua may act as a test-driver which orchestrates test runs and transmits test-results back to the continuous integration server. In such a set-up, Lua improves test execution speed, as the number of test-executables can be greatly reduced.

// C++ integration
int LuaApplication::LuaMain()
    lua_getglobal(L, "lua_main"); 
    return lua_pcall(L, 0, 0, 0);

Concluding this post, scripting on the microcontroller greatly improves our ability to test ideas and assumptions in hardware. It reduces the time to produce test-cases and prototypes. It therefore reduces lag between software department and hardware department (the hardware guys may wish to learn Lua soon). And finally it’s a skill you may want to invest in, as a few hardware generations from now we will likely skip the rewrite-in-C-step and go into production with Lua.


For more information, please contact Maximilian Lichtenegger, Philipp Steiner or me.

Comments (0)


Sign up for our Updates

Sign up now for our updates.

This field is required
This field is required
This field is required

I'm interested in:

Select at least one category
You were signed up successfully.

Receive regular updates from our blog


Or would you like to discuss a potential project with us? Contact us »