Hello elua

概述

下文会简单的介绍elua,然后编译固件,烧录到STM32F4 Nucleo板子上,最后用lua来编写一个简单的hello程序

注意事项

在继续阅读之前,请看下面几点

  1. elua项目的版本比较多,代码文档和网站文档不一致,建议参考代码中的文档说明;本文档代码采用github master主分支0.9版本
  2. 本文以linux平台为例来说明编译过程,实际上,我是用树莓派来编译elua的,并用树莓派烧录,调试STM32;elua可以跨平台编译,具体请参考https://github.com/elua/elua/tree/master/doc/en building章节

elua是什么

What is eLua?
eLua stands for Embedded Lua and the project offers the full implementation of the Lua Programming Language to the embedded world, extending it with specific features for efficient and portable software embedded development.

eLua offers the full features of the regular Lua desktop version and uses Lua’s native mechanisms to extend it with embedded development optimized and specific features.

Some aspects of eLua are:

  • Full control of the platform
    eLua runs on the “bare-metal”. There is no Operating System between your programs and the microcontroller.
  • Source code portability
    Like in Lua, you program in C, Lua or a mixture of both and your program runs in a wide varied of (sometimes radically different) platforms and architectures supported.
  • Transform hardware in commodity
    Design and code your products for eLua and make them hardware-independent. Upgrade or completely change your hardware in the future and save time and money invested on the previous code development.
  • Development on targets
    Fully functional Lua and a dedicated command shell on the microcontroller itself. No need to install development environments on the PC side, other than a serial or ehternet console/terminal emulator. Use any text editor, save your programs in sd/mmc and other media and use them directly in your platforms.
  • Flexible products
    Add modern high level script-language capabilities to your projects, resulting in highly adaptable, field-programable and reconfigurable designs. Efficient (and cheap!) future evolution to your systems.
  • Shorter TTM
    Optimizes Time to Market, shorter time to revenue, improved ability to hit critical market windows, agility to survive in turbulent market conditions
  • Embedded RAD
    Prototype and experiment on a Rapid Aplication Develop model. Test your ideas directly on the target platforms and cheap development kits. No need for simulators or future code adaptations.
  • Ready to use kits
    A big (and growing) number of Open Source hardware and commercially available platforms are supported (see here ). Prototype cheap and fast and design your final hardware later using the produced code.
  • Longevity
    Add user configuration and scripting capabilities to your projects, making them adaptable to the always changing contexts of industrial processes, evolving engineering, automation standards, field optimizations etc…
  • Learn embedded
    Simple interactive and interpreted experimenting cycle. Use your desktop programming skills to become an embedded systems developer in no time and with a lot of fun.
  • Worry-free Licence
    eLua is free and open-source software and we promote it as much as we can. But our MIT licence (the same as Lua’s) allows you to use eLua in your commercial and private-code products as well. Nothing to ask, no royalties to pay, just tell the world you’re using eLua.

elua有什么优点

lua让嵌入式支持动态脚本语言,跨平台开发,开发者无需安装任何IDE,任何编译系统,就可以控制嵌入式系统

elua可以加速系统开发,并省去一个外部MCU,降低系统成本

编译elua

elua支持跨平台编译,下面以linux系统为例子来演示编译步骤

提示 旧版本lua(0.8以下)用scons来编译系统,新版本(0.9)用lua来编译系统

如果自己的linux机器上没有装git,请用下面命令安装git

1
sudo apt-get install git

下载elua代码

1
git clone git@github.com:elua/elua.git elua

下面的所有操作,都是以在elua目录中进行的

1
cd elua

编译lua需要交叉编译工具,这里推荐使用arm的arm-none-eabi编译工具链,因为其他的工具链对浮点数的支持有问题,会导致编译失败

1
2
sudo apt-get install gcc-arm-none-eabi
sudo apt-get install build-essential

安装完成后,用下面的命令来检查安装是否成功

1
sudo arm-none-eabi-gcc --print-multi-lib

我在树莓派上的输出结果如下所示

1
root@raspberrypi:/home/pi/Templates/elua/ sudo arm-none-eabi-gcc --print-multi-lib
.;
thumb;@mthumb
fpu;@mfloat-abi=hard
armv6-m;@mthumb@march=armv6s-m
armv7-m;@mthumb@march=armv7-m
armv7e-m;@mthumb@march=armv7e-m
armv7-ar/thumb;@mthumb@march=armv7
armv7e-m/softfp;@mthumb@march=armv7e-m@mfloat-abi=softfp@mfpu=fpv4-sp-d16
armv7e-m/fpu;@mthumb@march=armv7e-m@mfloat-abi=hard@mfpu=fpv4-sp-d16
armv7-ar/thumb/softfp;@mthumb@march=armv7@mfloat-abi=softfp@mfpu=vfpv3-d16
armv7-ar/thumb/fpu;@mthumb@march=armv7@mfloat-abi=hard@mfpu=vfpv3-d16

下面安装lua和附属库

1
2
3
4
5
sudo apt-get install lua5.1
sudo apt-get install luarocks
sudo luarocks install luafilesystem
sudo luarocks install lpack
sudo luarocks install md5

编译lua跨平台编译器

1
lua cross-lua.lua

编译STM32F4 nucleo板子的elua代码

1
lua build_elua.lua board=stm32f4_nucleo prog

编译完成后,我们会看到elua目录下多出来4个文件
elua_lua_stm32f4-nucleo.bin,elua_lua_stm32f4-nucleo.elf,elua_lua_stm32f4-nucleo.hex,elua_lua_stm32f4-nucleo.map

接下来我们烧录bin文件到STM32F4 Nucleo开发板中

烧录,运行

在烧录之前,我们需要先编译安装st-link的工具
请确保系统已经有autoconf工具,如果没有则通过下面命令来安装

1
sudo apt-get install autoconf

在elua上一级目录,执行下面命令

1
2
git clone https://github.com/texane/stlink stlink
cd stlink

st-link代码依赖libusb-1.0和pkg-config,请加入下面代码进行安装

1
2
sudo apt-get install libusb-1.0
sudo apt-get install pkg-config

然后开始编译st-link代码

1
2
3
4
cd stlink
./autogen.sh
./configure
make -j8

安装st-link工具

1
2
make install
sudo cp *.rules /etc/udev/rules.d

重启linux

1
sudo reboot

插入ST-link,输入lsusb,不出意外的情况下,会发现ST-Link设备

然后进入elua目录,输入下面命令,烧写bin文件到stm32f4 nucleo板子

1
st-flash write elua_lua_stm32f4-nucleo.bin 0x8000000

输出结果如下所示

1
root@raspberrypi:/home/pi/Templates/elua# st-flash write elua_lua_stm32f4-nucleo.bin 0x8000000
2015-10-19T02:05:46 INFO src/stlink-common.c: Loading device parameters....
2015-10-19T02:05:46 INFO src/stlink-common.c: Device connected is: F446 device, id 0x10006421
2015-10-19T02:05:46 INFO src/stlink-common.c: SRAM size: 0x20000 bytes (128 KiB), Flash: 0x80000 bytes (512 KiB) in pages of 131072 bytes
2015-10-19T02:05:46 INFO src/stlink-common.c: Attempting to write 212952 (0x33fd8) bytes to stm32 address: 134217728 (0x8000000)
EraseFlash - Sector:0x0 Size:0x4000
Flash page at addr: 0x08000000 erasedEraseFlash - Sector:0x1 Size:0x4000
Flash page at addr: 0x08004000 erasedEraseFlash - Sector:0x2 Size:0x4000
Flash page at addr: 0x08008000 erasedEraseFlash - Sector:0x3 Size:0x4000
Flash page at addr: 0x0800c000 erasedEraseFlash - Sector:0x4 Size:0x10000
Flash page at addr: 0x08010000 erasedEraseFlash - Sector:0x5 Size:0x20000
Flash page at addr: 0x08020000 erased
2015-10-19T02:05:51 INFO src/stlink-common.c: Finished erasing 6 pages of 131072 (0x20000) bytes
2015-10-19T02:05:51 INFO src/stlink-common.c: Starting Flash write for F2/F4/L4
2015-10-19T02:05:51 INFO src/stlink-common.c: Successfully loaded flash loader in sram
enabling 32-bit flash writes
size: 32768
size: 32768
size: 32768
size: 32768
size: 32768
size: 32768
size: 16344
2015-10-19T02:05:56 INFO src/stlink-common.c: Starting verification of write complete
2015-10-19T02:05:57 INFO src/stlink-common.c: Flash written and verified! jolly good!
root@raspberrypi:/home/pi/Templates/elua#

至此,编译烧录完成

Hello, lua

默认情况下,板子采用Arduino的UART接口来输出信息,也就是板子右下方的TX/D1RX/D0,而Nucleo系统默认通过ST-link来虚拟出来一个串口,所以我们用杜邦线将上面两个UART脚和CN3的TX,RX交叉连接,这样我们就可以直接向电脑发送数据

注意:默认情况下,如果要开启wofs功能,请在board/known/stm32f4-nucleo.lua中,增加下面wofs=true,然后重新编译下载到板子

1
2
3
4
5
6
7
return {
cpu = 'stm32f401re',
components = {
sercon = { uart = "1", speed = 115200 },
romfs = true,
wofs = true, // Add Write Once FileSystem
cdc = false,

打开串口工具,找到ttyACM0虚拟串口,设置波特率115200, 8-N-1,复位板子,你就可以看到elua的输出信息

enjoy it

参考资料

  1. https://github.com/elua/elua
  2. http://www.eluaproject.net/doc/v0.9/en_using.html
  3. http://www.eluaproject.net/doc/master/en_building.html
  4. http://www.lua.org
  5. https://github.com/elua/elua/tree/master/doc/en