Last time we built eLua for the
STM32F4DISCOVERY board, and ran an interactive Lua session on it. This time I’d like to look more at how you might integrate it into an application of your own.
You might have noticed the
ls command last time, when we asked for help at the eLua shell prompt. Let’s run it now:
eLua# ls /rom Total on /rom: 0 bytes
Nothing. What’s significant is that
ls is even possible, and that eLua tells us about a file system called
/rom. It makes it distinctly possible that we can put something in
eLua’s build system makes it trivially easy to include something in
/rom. We simply include anything we want in
romfs/ in the build directory.
Save the following as
print(pd.board() .. "/" .. pd.platform() .. "/" .. pd.cpu()) ledpin = pio.PD_13 pb = pio.PA_0 pio.pin.setdir(pio.OUTPUT, ledpin) pio.pin.setdir(pio.INPUT, pb) -- turn the led on pio.pin.sethigh(ledpin) -- Wait for the button print("Waiting for button push") while pio.pin.getval(pb) == 0 do tmr.delay( 0, 50000 ) end -- turn the led off pio.pin.setlow(ledpin)
Program the binary again; and return to your terminal.
eLua# ls /rom testpio.lua 373 bytes Total on /rom: 373 bytes
Our script is available. Let’s run it.
eLua# lua Press CTRL+Z to exit Lua Lua 5.1.4 Copyright (C) 1994-2011 Lua.org, PUC-Rio > dofile("/rom/testpio.lua") STM32F4DISCOVERY/STM32F4/STM32F407VG Waiting for button push
You should observe one of the LEDs light when you run the script, and extinguish when you push the button.
I won’t go any further here with this style of script; you get the idea. Write scripts, store them in flash, run them. eLua is offering us a basic operating system for our device and the ability to store and run scripts let’s us program that operating system. With one additional piece of information, we can make an embedded Lua system self-contained.
This is what happens when you power up your eLua board:
- the platform initialization code is executed. This is the code that does very low level platform setup (if needed), copies ROM to RAM, zeroes out the BSS section, sets up the stack pointer and jumps to main.
- the first thing main does is call the platform specific initialization function (platform_init). platform_init must fully initialize the platform and return a result to main, that can be either PLATFORM_OK if the initialization succeeded or PLATFORM_ERR otherwise. If PLATFORM_ERR is returned, main blocks immediately in an infinite loop.
- main then initializes the rest of the system: the ROM file system, XMODEM, and term.
- if /rom/autorun.lua (which is a file called autorun.lua in the ROM file system) is found, it is executed. If it returns after execution, or if it isn’t found, the boot process continues with the next step.
- if boot is set to ‘standard’ and the link:using.html#shell[shell] was compiled in the image, it is started, in the absence of the shell, a standard Lua interpreter is started.
- if boot is set to ‘luarpc’ an rpc server is started.
Do you see the important item?
/rom/autorun.lua is run automatically on boot. Put what you like in there and you don’t need an attached PC with terminal to run your scripts.