Programming a microcontroller with Lua

eLua is a full version of the Lua programming language for microcontrollers, running on bare metal. Lua provides a modern high level dynamically typed language, with first class functions, coroutines and an API for interacting with C code, and yet which is very small and can run in a memory constrained environment.

Programming a microcontroller with Lua

Justin Cormack @justincormack
http://www.londonlua.org

Lua

designed to be simple, small, portable, fast, and easily embedded into applications

What is Lua?

Lua...

Lua...

Programming in Lua

One of the best introductions to a programming language there is. Written alongside each major release, if the feature cannot be explained clearly, it needs fixing.

Uses for Lua

Why Lua for embedded systems?

How about eLua?

How small is Lua?

Libraries


chunk ::= {stat [`;´]} [laststat [`;´]]
block ::= chunk
stat ::=  varlist `=´ explist | 
     functioncall | 
     do block end | 
     while exp do block end | 
     repeat block until exp | 
     if exp then block {elseif exp then block} [else block] end | 
     for Name `=´ exp `,´ exp [`,´ exp] do block end | 
     for namelist in explist do block end | 
     function funcname funcbody | 
     local function Name funcbody | 
     local namelist [`=´ explist] 
laststat ::= return [explist] | break
funcname ::= Name {`.´ Name} [`:´ Name]
varlist ::= var {`,´ var}
var ::=  Name | prefixexp `[´ exp `]´ | prefixexp `.´ Name 
namelist ::= Name {`,´ Name}
explist ::= {exp `,´} exp
exp ::=  nil | false | true | Number | String | `...´ | function | 
     prefixexp | tableconstructor | exp binop exp | unop exp 
prefixexp ::= var | functioncall | `(´ exp `)´
functioncall ::=  prefixexp args | prefixexp `:´ Name args 
args ::=  `(´ [explist] `)´ | tableconstructor | String 
function ::= function funcbody
funcbody ::= `(´ [parlist] `)´ block end
parlist ::= namelist [`,´ `...´] | `...´
tableconstructor ::= `{´ [fieldlist] `}´
fieldlist ::= field {fieldsep field} [fieldsep]
field ::= `[´ exp `]´ `=´ exp | Name `=´ exp | exp
fieldsep ::= `,´ | `;´
binop ::= `+´ | `-´ | `*´ | `/´ | `^´ | `%´ | `..´ | 
     `<´ | `<=´ | `>´ | `>=´ | `==´ | `~=´ | 
     and | or
unop ::= `-´ | not | `#´


pio.pin.setdir(pio.OUTPUT, pio.PD_15)

for i = 1, 100 do
  pio.pin.sethigh(pio.PD_15)
  tmr.delay(0, 50000)
  pio.pin.setlow(pio.PD_15)
  tmr.delay(0, 50000)
end

local tmrid, crtduty, incr = 0, 10, 5
for pwmid = 0, 3 do pwm.setclock(pwmid, 25000000) end
tmr.start(tmrid)
for pwmid = 0, 3 do pwm.start(pwmid) end
for i = 1, 1000 do
  if crtduty == 95 or crtduty == 5 then incr = -incr end
  crtduty = crtduty + incr
  for pwmid = 0, 3 do
    pwm.setup(pwmid, 50000, (crtduty + 25 * pwmid) % 90 + 5)  
  end
  tmr.delay(tmrid, 50000)  
end

Ways to run code interactively

  • interactively in the interpreter
  • xmodem file transfer from console or Lua
  • RPC from desktop
  • Autoboot from SD card (some machines with storage)
  • There are editors written in Lua
  • USB host filesystem access would be nice...

Ways to run code in production

  • Extend eLua to support your custom hardware either in Lua or C
  • It is quite possible to write many drivers just in Lua
  • Build your functions into the eLua build
  • Flash your code along with the Lua code
  • Lua and C interoperate with no problems!

Open Hardware shipping with eLua

  • SimpleMachines in Italy make Mizar32 open hardware boards shipping with eLua.
  • CERN Open Hardware License
  • Based on 66MHz AVR32 chipset, 128-512kB flash, 32-64kB internal RAM, 32MB external RAM
  • Micro SD card, USB, JTAG
  • Lots of expansion boards are available - LCD, ethernet etc

Community

The general Lua community is relatively small, but growing.

The Eclipse Foundation is becoming a centre for open source projects around embedded Lua, including the Mihini project, started by Sierra Wireless.

I organize (irregular) London Lua group meetups.

Resources