LUA interpreter into the firmware of ARM MCU.

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I wish to include the LUA interpreter in my firmware to allow user to run simple scripts and configurations on the device. I am using Atmel Start generated code and a standalone makefile to build my firmware. So far I have not been able to get the compiler to link the LUA source into my firmware. I tried to copy all of the source files into the firmware folder and added all of them to the makefile but it still could not compile.

 

On PC i was able to build a simple project that successfully included the LUA library, but for that I needed to precompile LUA and include it as an argument to gcc.

I used the instructions on the LUA website to build the source, https://www.lua.org/download.html and it worked fine. to compile I just ran gcc main.c -llua -lm -ldl

Even in this PC example I was not able to just include the header and source files, that way the project just would not compile!

 

This was the PC example I got running.

#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

int main(int argc, char ** argv) {

    lua_State *L = luaL_newstate();
    luaL_openlibs(L);

    // Our Lua code, it simply prints a Hello, World message
    char * code = "print('Hello, World')";

    // Here we load the string and use lua_pcall for run the code
    if (luaL_loadstring(L, code) == LUA_OK) {
        if (lua_pcall(L, 0, 0, 0) == LUA_OK) {
            // If it was executed successfuly we 
            // remove the code from the stack
            lua_pop(L, lua_gettop(l));
        }
    }

    lua_close(L);
    return 0;
}

 

Now I wish to link it to the arm firmware project using arm-none-eabi-gcc as the compiler, however it cannot find -llua. I think this is because when I compiled LUA from the source, I compiled it to run on my computer's architecture, not on ARM. I don't know how to compile it to ARM, so the linker can find it...

 

Does anyone have experience in linking LUA into a firmware project?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There is Lua support for the ESP8266 in firmware form:  https://nodemcu.readthedocs.io/e... Perhaps they way they do their build would be an example.

 

I don't think I've ever seen Lua for an embedded ARM chip.  Note the ESP8266 modules tend to have ~4MByte of flash memory, and the Lua Firmware is over 400k bytes.  It may be quite challenging to fit it most ARM chips.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sukuwc wrote:
Does anyone have experience in linking LUA into a firmware project?

Probably best asked on the Lua forums?

 

Don't  they have a "porting guide" ?

 

EDIT

 

Googling "Lua Cortex-M" reminds me that there is an eLua project - which might be more appropriate?

 

http://www.eluaproject.net/doc/v0.8/en_tc_cortex.html

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Wed. Apr 7, 2021 - 09:06 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Lua has fallen off the radar in recent times. Micropython, ulisp and espruino are more recent and have better community support. From what I recall, elua was over 200k on a cortex m4, so you want a dair slab of flash available

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Looks like after a couple of day pulling my hair out, I finally got Lua running. I pre-compiled it using arm-none-eabi-gcc and used the generated lualib.a file in my project. I had to use a couple of magic linker parameters but it's finally running. I will add more information later, when I actually understand what I did :D

 

Quick benchmark:

120MHz SAMD51

Lua source compiled to around 100kb 300kb of program memory!

7ms to start the Lua VM execute a command and close the VM. (Obviously you don't want to close the VM after every command, but you can!)

A simple arithmetic expression takes around 190us!

Last Edited: Tue. Apr 13, 2021 - 05:39 PM