{"id":1222,"date":"2013-09-11T01:00:00","date_gmt":"2013-09-10T23:00:00","guid":{"rendered":"https:\/\/www.fussylogic.co.uk\/blog\/?p=1222"},"modified":"2013-09-24T09:53:43","modified_gmt":"2013-09-24T08:53:43","slug":"elua-and-stm32f4","status":"publish","type":"post","link":"https:\/\/www.fussylogic.co.uk\/blog\/?p=1222","title":{"rendered":"eLua and STM32F4"},"content":{"rendered":"<p>I am sufficiently taken with <a href=\"?p=1208\">Lua<\/a> that I went looking for interesting Lua projects. I came across <a href=\"http:\/\/www.eluaproject.net\/\">eLua<\/a>. eLua is a project to build a version of Lua that can be used in embedded projects. There is certainly potential there for making life easier. Often in embedded projects, the vast majority of the work is getting the hardware to play nicely. You gather inputs, make a decision and set outputs (broadly). The \u00e2\u20ac\u0153decision\u00e2\u20ac\u009d part of that sequence could easily be done by Lua scripts; with all the hardware work done by low-level C code.<\/p>\n<p>It\u00e2\u20ac\u2122s an interesting enough idea that it made me want to try. My interest was particularly piqued by the presence of support for the <code>STM32F4DISCOVERY<\/code> board; which is the big brother to the <code>STM32F0DISCOVERY<\/code> board I wrote about <a href=\"?p=994\">earlier<\/a> <a href=\"?p=1001\">this<\/a> <a href=\"?p=1006\">year<\/a>.<\/p>\n<p>Grab a copy of eLua :<\/p>\n<pre><code>$ git clone https:\/\/github.com\/elua\/elua.git\n$ git describe\nv0.9-94-g02705ec<\/code><\/pre>\n<p>Then we\u00e2\u20ac\u2122ll be following the build instructions, with the <code>STM32F4<\/code> as our platform and <code>STM32F4DISCOVERY<\/code> as our board. The documentation on the <a href=\"http:\/\/www.eluaproject.net\/doc\/v0.9\/en_building.html\">website<\/a>, unfortunately, describes how to build using the <code>scons<\/code> system. The eLua developers seem to be in the process of removing <code>scons<\/code>; so the build instructions on the website are wrong. Instead see the build instructions in the <a href=\"https:\/\/github.com\/elua\/elua\/blob\/master\/doc\/en\/building.txt\">git repository<\/a> itself.<\/p>\n<h1 id=\"build-system\">Build System<\/h1>\n<p>Some of the required Lua libraries are already in Debian.<\/p>\n<pre><code>$ apt-get install lua5.1 lua-filesystem lua-md5<\/code><\/pre>\n<p>One of them isn\u00e2\u20ac\u2122t, <code>lpack<\/code>. We\u00e2\u20ac\u2122ll have to use lua\u00e2\u20ac\u2122s own package manager for that (note that Debian\u00e2\u20ac\u2122s <code>luarocks<\/code>, at time of writing, uses lua 5.1 not 5.2).<\/p>\n<pre><code>$ apt-get install luarocks\n$ luarocks install lpack\nInstalling http:\/\/luarocks.org\/repositories\/rocks\/lpack-20070629-1.src.rock...\nUsing http:\/\/luarocks.org\/repositories\/rocks\/lpack-20070629-1.src.rock... switching to &#39;build&#39; mode\nArchive:  \/tmp\/luarocks_luarocks-rock-lpack-20070629-1-3149\/lpack-20070629-1.src.rock\n  inflating: lpack-20070629-1.rockspec  \n extracting: lpack.tar.gz            \nDo not use &#39;module&#39; as a build type. Use &#39;builtin&#39; instead.\ngcc -O2 -fPIC -I\/usr\/include\/lua5.1 -c lpack.c -o lpack.o\ngcc -shared -o pack.so -L\/usr\/local\/lib lpack.o\nUpdating manifest for \/usr\/local\/lib\/luarocks\/rocks\n\nlpack 20070629-1 is now built and installed in \/usr\/local\/ (license: Public domain)<\/code><\/pre>\n<h1 id=\"building\">Building<\/h1>\n<p>Here\u00e2\u20ac\u2122s my first build attempt<\/p>\n<pre><code>$ .\/build_elua.lua board=stm32f4discovery\n[CONFIG] Found board description file at boards\/known\/stm32f4discovery.lua\n[CONFIG] Generated board header file at boards\/headers\/board_stm32f4discovery.h\nUnable to find an usable toolchain in your path.\nList of accepted toolchains (for STM32F407VG): arm-gcc,codesourcery,devkitarm,arm-eabi-gcc<\/code><\/pre>\n<p>This is happening to me because I don\u00e2\u20ac\u2122t keep my arm compiler in the <code>PATH<\/code>, I prefer to specify a toolchain prefix that includes the path (since I have to do that anyway, I figure why slow down normal command line <code>PATH<\/code>, I might as well include the path in the prefix). Unfortunately for me, eLua doesn\u00e2\u20ac\u2122t offer a prefix option (and that\u00e2\u20ac\u2122s going to come back and bite them, I\u00e2\u20ac\u2122ll bet). In fact, it doesn\u00e2\u20ac\u2122t even include the correct prefix as an option for the ARM-maintained gcc from <a href=\"https:\/\/launchpad.net\/gcc-arm-embedded\/\">launchpad.net<\/a>. No matter; I just edited <code>build_data.lua<\/code> and added this to the <code>toolchain_list<\/code>:<\/p>\n<pre><code>diff --git a\/build_data.lua b\/build_data.lua\nindex e0ad79e..ad1f577 100644\n--- a\/build_data.lua\n+++ b\/build_data.lua\n@@ -23,6 +23,17 @@ local toolchain_list =\n     cross_lualong = &#39;int 32&#39;,\n     version = &#39;--version&#39;\n   },\n+  [ &#39;arm-none-eabi-gcc&#39; ] = {\n+    compile = &#39;\/opt\/gcc-arm-none-eabi-4_7-2013q2\/bin\/arm-none-eabi-gcc&#39;,\n+    link = &#39;\/opt\/gcc-arm-none-eabi-4_7-2013q2\/bin\/arm-none-eabi-ld&#39;,\n+    asm = &#39;\/opt\/gcc-arm-none-eabi-4_7-2013q2\/bin\/arm-none-eabi-as&#39;,\n+    bin = &#39;\/opt\/gcc-arm-none-eabi-4_7-2013q2\/bin\/arm-none-eabi-objcopy&#39;,\n+    size = &#39;\/opt\/gcc-arm-none-eabi-4_7-2013q2\/bin\/arm-none-eabi-size&#39;,\n+    cross_cpumode = &#39;little&#39;,\n+    cross_lua = &#39;float 64&#39;,\n+    cross_lualong = &#39;int 32&#39;,\n+    version = &#39;--version&#39;\n+  },\n   [ &#39;arm-eabi-gcc&#39; ] = {\n     compile = &#39;arm-eabi-gcc&#39;,\n     link = &#39;arm-eabi-ld&#39;,\n@@ -96,7 +107,7 @@ local arch_data = {\n local toolchain_map = {\n   arm = { &#39;arm-gcc&#39;, &#39;codesourcery&#39;, &#39;devkitarm&#39;, &#39;arm-eabi-gcc&#39; },\n   cortexm3 = { &#39;arm-gcc&#39;, &#39;codesourcery&#39;, &#39;devkitarm&#39;, &#39;arm-eabi-gcc&#39; },\n-  cortexm4 = { &#39;arm-gcc&#39;, &#39;codesourcery&#39;, &#39;devkitarm&#39;, &#39;arm-eabi-gcc&#39; },\n+  cortexm4 = { &#39;arm-gcc&#39;, &#39;codesourcery&#39;, &#39;devkitarm&#39;, &#39;arm-eabi-gcc&#39;, &#39;arm-none-eabi-gcc&#39; },\n   avr32 = { &#39;avr32-gcc&#39;, &#39;avr32-unknown-none-gcc&#39; },\n   i386 =  { &#39;i686-gcc&#39; }\n }<\/code><\/pre>\n<p>The build script will then happily find a toolchain that\u00e2\u20ac\u2122s acceptable to it:<\/p>\n<pre><code>$ .\/build_elua.lua board=stm32f4discovery<\/code><\/pre>\n<p>This went much better.<\/p>\n<pre><code>-rwxrwxr-x  1 andyp andyp 417682 Sep 11 13:51 elua_lua_stm32f4discovery.elf\n-rw-rw-r--  1 andyp andyp 718346 Sep 11 13:51 elua_lua_stm32f4discovery.map<\/code><\/pre>\n<h1 id=\"flashing\">Flashing<\/h1>\n<p>I used <code>objcopy<\/code> to make a binary from the elf,<\/p>\n<pre><code>$ \/opt\/gcc-arm-none-eabi-4_7-2013q2\/bin\/arm-none-eabi-objcopy \\\n    -O binary \\\n    elua_lua_stm32f4discovery.elf \\\n    elua_lua_stm32f4discovery.bin<\/code><\/pre>\n<p>then <code>openocd<\/code> to flash the binary to the board.<\/p>\n<pre><code>$ openocd \\\n   -f \/usr\/share\/openocd\/scripts\/board\/stm32f4discovery.cfg \\\n   -c &quot;init&quot; \\\n   -c &quot;reset halt&quot; \\\n   -c &quot;sleep 100&quot; \\\n   -c &quot;wait_halt 2&quot; \\\n   -c &quot;echo \\&quot;--- Writing elua_lua_stm32f4discovery.bin\\&quot;&quot; \\\n   -c &quot;flash write_image erase elua_lua_stm32f4discovery.bin 0x08000000&quot; \\\n   -c &quot;sleep 100&quot; \\\n   -c &quot;echo \\&quot;--- Verifying\\&quot;&quot; \\\n   -c &quot;verify_image elua_lua_stm32f4discovery.bin 0x08000000&quot; \\\n   -c &quot;sleep 100&quot; \\\n   -c &quot;echo \\&quot;--- Done\\&quot;&quot; \\\n   -c &quot;resume&quot; \\\n   -c &quot;shutdown&quot;\nOpen On-Chip Debugger 0.7.0 (2013-05-15-12:03)\nLicensed under GNU GPL v2\nFor bug reports, read\n        http:\/\/openocd.sourceforge.net\/doc\/doxygen\/bugs.html\nsrst_only separate srst_nogate srst_open_drain connect_deassert_srst\nInfo : This adapter doesn&#39;t support configurable speed\nInfo : STLINK v2 JTAG v14 API v2 SWIM v0 VID 0x0483 PID 0x3748\nInfo : Target voltage: 2.868772\nInfo : stm32f4x.cpu: hardware has 6 breakpoints, 4 watchpoints\ntarget state: halted\ntarget halted due to debug-request, current mode: Thread \nxPSR: 0x01000000 pc: 0x080002c0 msp: 0x20000400\n--- Writing elua_lua_stm32f4discovery.bin\nauto erase enabled\nInfo : device id = 0x10016413\nInfo : flash size = 1024kbytes\nwrote 262144 bytes from file elua_lua_stm32f4discovery.bin in 14.261276s (17.951 KiB\/s)\n--- Verifying\nverified 232464 bytes in 2.129896s (106.585 KiB\/s)\n--- Done\nshutdown command invoked<\/code><\/pre>\n<h1 id=\"testing\">Testing<\/h1>\n<p>We should now have an <code>STM32F4DISCOVERY<\/code> board with eLua running on it. The debug UART on this board is not made available via the programming USB (Mini USB socket at the top of the board). The Lua terminal is supplied as a CDC USB serial device on the application port on the Micro USB port at the bottom of the board.<\/p>\n<pre><code>usb 2-3: new full-speed USB device number 10 using ohci_hcd\nusb 2-3: New USB device found, idVendor=0483, idProduct=5740\nusb 2-3: New USB device strings: Mfr=1, Product=2, SerialNumber=3\nusb 2-3: Product: STM32 Virtual ComPort in FS Mode\nusb 2-3: Manufacturer: STMicroelectronics\nusb 2-3: SerialNumber: 00000000050C\ncdc_acm 2-3:1.0: This device cannot do calls on its own. It is not a modem.\ncdc_acm 2-3:1.0: ttyACM0: USB ACM device<\/code><\/pre>\n<p>Run a terminal on it at 115200 8n1 and you\u00e2\u20ac\u2122ll get the eLua shell prompt.<\/p>\n<pre><code>eLua# help\nShell commands:\n  help   - shell help\n  lua    - start a Lua session\n  ls     - lists files and directories\n  dir    - lists files and directories\n  cat    - list the contents of a file\n  type   - list the contents of a file\n  recv   - receive files via XMODEM\n  cp     - copy files\n  mv     - move\/rename files\n  rm     - remove files\n  ver    - show version information\n  mkdir  - create directories\n  exit   - exit the shell\nFor more information use &#39;help &lt;command&gt;&#39;.<\/code><\/pre>\n<p>Magical. This is not yet Lua; this is the embedded equivalent of a bash shell. We can run an interactive Lua session of course\u00e2\u20ac\u00a6<\/p>\n<pre><code>eLua# lua\nPress CTRL+Z to exit Lua\nLua 5.1.4  Copyright (C) 1994-2011 Lua.org, PUC-Rio\n&gt; print &quot;Hello, World!&quot;\nHello, World!<\/code><\/pre>\n<p>eLua comes with a <a href=\"http:\/\/www.eluaproject.net\/doc\/master\/en_refman_gen.html\">standard library<\/a> appropriate for controlling the on-chip peripherals (ADC, CAN, PIO, PWM, SPI, timers, UART). You can experiment with these if you wish. Here\u00e2\u20ac\u2122s a few snippets as an <a href=\"https:\/\/github.com\/elua\/examples\">example<\/a>:<\/p>\n<pre><code>&gt; print(pd.board() .. &quot;\/&quot; .. pd.platform() .. &quot;\/&quot; .. pd.cpu())\nSTM32F4DISCOVERY\/STM32F4\/STM32F407VG\n\n&gt; ledpin = pio.PD_13\n&gt; pio.pin.setdir(pio.OUTPUT, ledpin)\n&gt; pio.pin.sethigh(ledpin)\n\n&gt; pb = pio.PA_0\n&gt; pio.pin.setdir(pio.INPUT, pb)\n&gt; print( pio.pin.getval(pb) )\n0\n&gt; print( pio.pin.getval(pb) )\n1<\/code><\/pre>\n<p>I pushed the \u00e2\u20ac\u0153user\u00e2\u20ac\u009d button between those last two.<\/p>\n<p>Next time we\u00e2\u20ac\u2122ll look at customising the build and including our own Lua source.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am sufficiently taken with Lua that I went looking for interesting Lua projects. I came across eLua. eLua is a project to build a version of Lua that can be used in embedded projects. There is certainly potential there for making life easier. Often in embedded projects, the vast majority of the work is\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.fussylogic.co.uk\/blog\/?p=1222\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[109,42,113,6],"_links":{"self":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1222"}],"collection":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1222"}],"version-history":[{"count":7,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1222\/revisions"}],"predecessor-version":[{"id":1243,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1222\/revisions\/1243"}],"wp:attachment":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}