{"id":1218,"date":"2013-09-12T01:00:00","date_gmt":"2013-09-11T23:00:00","guid":{"rendered":"https:\/\/www.fussylogic.co.uk\/blog\/?p=1218"},"modified":"2013-09-13T11:16:59","modified_gmt":"2013-09-13T10:16:59","slug":"elua-and-stm32f4-ii","status":"publish","type":"post","link":"https:\/\/www.fussylogic.co.uk\/blog\/?p=1218","title":{"rendered":"eLua and STM32F4 II"},"content":{"rendered":"<p><a href=\"?p=\">Last time<\/a> we built eLua for the <code>STM32F4DISCOVERY<\/code> board, and ran an interactive Lua session on it. This time I\u00e2\u20ac\u2122d like to look more at how you might integrate it into an application of your own.<\/p>\n<p>You might have noticed the <code>ls<\/code> command last time, when we asked for help at the eLua shell prompt. Let\u00e2\u20ac\u2122s run it now:<\/p>\n<pre><code>eLua# ls\n\/rom\nTotal on \/rom: 0 bytes<\/code><\/pre>\n<p>Nothing. What\u00e2\u20ac\u2122s significant is that <code>ls<\/code> is even possible, and that eLua tells us about a file system called <code>\/rom<\/code>. It makes it distinctly possible that we can put something in <code>\/rom<\/code>.<\/p>\n<p>eLua\u00e2\u20ac\u2122s build system makes it trivially easy to include something in <code>\/rom<\/code>. We simply include anything we want in <code>romfs\/<\/code> in the build directory.<\/p>\n<p>Save the following as <code>testpio.lua<\/code> under <code>romfs\/<\/code><\/p>\n<pre><code>print(pd.board() .. &quot;\/&quot; .. pd.platform() .. &quot;\/&quot; .. pd.cpu())\n\nledpin = pio.PD_13\npb = pio.PA_0\n\npio.pin.setdir(pio.OUTPUT, ledpin)\npio.pin.setdir(pio.INPUT, pb)\n\n-- turn the led on\npio.pin.sethigh(ledpin)\n\n-- Wait for the button\nprint(&quot;Waiting for button push&quot;)\nwhile pio.pin.getval(pb) == 0 do\n    tmr.delay( 0, 50000 )\nend\n\n-- turn the led off\npio.pin.setlow(ledpin)<\/code><\/pre>\n<p>Program the binary again; and return to your terminal.<\/p>\n<pre><code>eLua# ls\n\/rom\n  testpio.lua                   373 bytes\nTotal on \/rom: 373 bytes<\/code><\/pre>\n<p>Our script is available. Let\u00e2\u20ac\u2122s run it.<\/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; dofile(&quot;\/rom\/testpio.lua&quot;)\nSTM32F4DISCOVERY\/STM32F4\/STM32F407VG\nWaiting for button push<\/code><\/pre>\n<p>You should observe one of the LEDs light when you run the script, and extinguish when you push the button.<\/p>\n<p>I won\u00e2\u20ac\u2122t 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\u00e2\u20ac\u2122s us program that operating system. With one additional piece of information, we can make an embedded Lua system self-contained.<\/p>\n<blockquote>\n<p>This is what happens when you power up your eLua board:<\/p>\n<ol style=\"list-style-type: decimal\">\n<li>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 <em>main<\/em>.<\/li>\n<li>the first thing <em>main<\/em> does is call the platform specific initialization function (<em>platform_init<\/em>). <em>platform_init<\/em> must fully initialize the platform and return a result to main, that can be either <em>PLATFORM_OK<\/em> if the initialization succeeded or <em>PLATFORM_ERR<\/em> otherwise. If <em>PLATFORM_ERR<\/em> is returned, <em>main<\/em> blocks immediately in an infinite loop.<\/li>\n<li><em>main<\/em> then initializes the rest of the system: the ROM file system, XMODEM, and term.<\/li>\n<li>if <em>\/rom\/autorun.lua<\/em> (which is a file called <em>autorun.lua<\/em> in the ROM file system) is found, it is executed. If it returns after execution, or if it isn\u00e2\u20ac\u2122t found, the boot process continues with the next step.<\/li>\n<li>if boot is set to \u00e2\u20ac\u02dcstandard\u00e2\u20ac\u2122 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.<\/li>\n<li>if boot is set to \u00e2\u20ac\u02dcluarpc\u00e2\u20ac\u2122 an rpc server is started.<\/li>\n<\/ol>\n<\/blockquote>\n<p>Do you see the important item? <code>\/rom\/autorun.lua<\/code> is run automatically on boot. Put what you like in there and you don\u00e2\u20ac\u2122t need an attached PC with terminal to run your scripts.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last time we built eLua for the STM32F4DISCOVERY board, and ran an interactive Lua session on it. This time I\u00e2\u20ac\u2122d 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.\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.fussylogic.co.uk\/blog\/?p=1218\">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\/1218"}],"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=1218"}],"version-history":[{"count":3,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1218\/revisions"}],"predecessor-version":[{"id":1241,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1218\/revisions\/1241"}],"wp:attachment":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}