Skip to content

Commit

Permalink
Merge pull request #92 from konnected-io/lfs
Browse files Browse the repository at this point in the history
move application to LFS
  • Loading branch information
heythisisnate authored Apr 25, 2019
2 parents ea9ac35 + 57f87a0 commit a18e7f2
Show file tree
Hide file tree
Showing 29 changed files with 211 additions and 81 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Compiled Lua sources
luac.out

# LFS image
src/lfs/lfs.img

# luarocks build files
*.src.rock
*.zip
Expand Down
4 changes: 2 additions & 2 deletions firmware/2.2.0/app/include/user_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
// the size of the store that you need. This can be any multiple of 4kB up to
// a maximum 256Kb.

//#define LUA_FLASH_STORE 0x10000
#define LUA_FLASH_STORE 0x10000


// By default Lua executes the file init.lua at start up. The following
Expand All @@ -72,7 +72,7 @@
// gives the fastest start-up and imaging times.

#define BUILD_SPIFFS
#define SPIFFS_FIXED_LOCATION 0xf0000
#define SPIFFS_FIXED_LOCATION 0x100000
#define SPIFFS_MAX_FILESYSTEM_SIZE 128000
//#define SPIFFS_SIZE_1M_BOUNDARY
#define SPIFFS_CACHE 1 // Enable if you use you SPIFFS in R/W mode
Expand Down
4 changes: 2 additions & 2 deletions firmware/2.2.0/app/include/user_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
#define NODE_VERSION_STR(x) #x
#define NODE_VERSION_XSTR(x) NODE_VERSION_STR(x)

#define NODE_VERSION "Konnected firmware 2.2.6\r\nNodeMCU " ESP_SDK_VERSION_STRING "." NODE_VERSION_XSTR(NODE_VERSION_INTERNAL)
#define NODE_VERSION "Konnected firmware 2.2.7\r\nNodeMCU " ESP_SDK_VERSION_STRING "." NODE_VERSION_XSTR(NODE_VERSION_INTERNAL)

#ifndef BUILD_DATE
#define BUILD_DATE "20190203"
#define BUILD_DATE "20190424"
#endif

extern char SDK_VERSION[];
Expand Down
Binary file added firmware/konnected-filesystem-0x100000-2-2-7.img
Binary file not shown.
Binary file added firmware/konnected-firmware-2-2-7.bin
Binary file not shown.
28 changes: 17 additions & 11 deletions scripts/build-firmware
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
#!/bin/bash

FIRMWARE_PATH=${PWD}/../nodemcu-firmware
IMAGE_NAME=konnected-firmware-2-2-6
IMAGE_NAME=konnected-firmware-2-2-7

# Copy firmware configuration from this repository to the nodemcu-firmware repo
cp firmware/2.2.0/app/include/* $FIRMWARE_PATH/app/include/

rm $FIRMWARE_PATH/local/fs/*
cp src/* $FIRMWARE_PATH/local/fs

docker run \
--rm -ti -v $FIRMWARE_PATH:/opt/nodemcu-firmware \
-v ${PWD}/scripts:/scripts marcelstoer/nodemcu-build bash /scripts/cross-compile

rm $FIRMWARE_PATH/local/fs/*.lua
rm $FIRMWARE_PATH/local/fs/init.lc
cp src/init.lua $FIRMWARE_PATH/local/fs

# Build NodeMCU firmware image
docker run -e "INTEGER_ONLY=1" \
-e "IMAGE_NAME=${IMAGE_NAME}" \
--rm -ti -v $FIRMWARE_PATH:/opt/nodemcu-firmware marcelstoer/nodemcu-build build

# Build LFS image with application files
docker run -e "IMAGE_NAME=lfs" \
--rm -ti -v $FIRMWARE_PATH:/opt/nodemcu-firmware \
-v ${PWD}/src/lfs:/opt/lua marcelstoer/nodemcu-build lfs-image

mv src/lfs/LFS_float_lfs.img src/lfs/lfs.img

# Build SPIFFS image
docker run \
--rm -ti -v $FIRMWARE_PATH:/opt/nodemcu-firmware \
-v ${PWD}/scripts:/scripts \
-v ${PWD}/src:/opt/lua marcelstoer/nodemcu-build bash /scripts/build-spiffs

cp ${FIRMWARE_PATH}/bin/nodemcu_integer_${IMAGE_NAME}.bin firmware/${IMAGE_NAME}.bin
cp ${FIRMWARE_PATH}/bin/0xf0000-0x1f400.img firmware/konnected-filesystem-0xf0000-2-2-6.img
cp ${FIRMWARE_PATH}/bin/konnected-filesystem-0x100000.img firmware/konnected-filesystem-0x100000-2-2-7.img
3 changes: 3 additions & 0 deletions scripts/build-spiffs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

/opt/nodemcu-firmware/tools/spiffsimg/spiffsimg -f /opt/nodemcu-firmware/bin/konnected-filesystem-0x%x.img -S 2MB -U 0x100000 -r /scripts/spiffs.lst -d
6 changes: 3 additions & 3 deletions scripts/flash
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/bin/bash

FIRMWARE_NAME=konnected-firmware-2-2-6.bin
FILESYSTEM_NAME=konnected-filesystem-0xf0000-2-2-6.img
FIRMWARE_NAME=konnected-firmware-2-2-7.bin
FILESYSTEM_NAME=konnected-filesystem-0x100000-2-2-7.img
PORT=/dev/cu.wchusbserial1410

esptool.py --port=${PORT} write_flash --flash_mode dio 0x00000 firmware/${FIRMWARE_NAME}
esptool.py --port=${PORT} write_flash --flash_mode dio 0xf0000 firmware/${FILESYSTEM_NAME}
esptool.py --port=${PORT} write_flash --flash_mode dio 0x100000 firmware/${FILESYSTEM_NAME}

tput bel
10 changes: 10 additions & 0 deletions scripts/spiffs.lst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import /opt/lua/actuators.lua actuators.lua
import /opt/lua/dht_sensors.lua dht_sensors.lua
import /opt/lua/ds18b20_sensors.lua ds18b20_sensors.lua
import /opt/lua/enduser_setup.html.gz enduser_setup.html.gz
import /opt/lua/http_favicon.ico.gz http_favicon.ico.gz
import /opt/lua/http_index.html.gz http_index.html.gz
import /opt/lua/init.lua init.lua
import /opt/lua/sensors.lua sensors.lua
import /opt/lua/settings.lua settings.lua
import /opt/lua/lfs/lfs.img lfs.img
70 changes: 9 additions & 61 deletions src/init.lua
Original file line number Diff line number Diff line change
@@ -1,68 +1,16 @@
print("Heap: ", node.heap(), "Initializing Konnected (" .. string.gsub(wifi.sta.getmac(), ":", "") .. ")")

-- load the application in LFS if needed
if node.flashindex() == nil then
node.flashreload("lfs.img")
end

pcall(node.flashindex("_init"))
require("start")
print("Heap: ", node.heap(), "Version: ", require("device").swVersion)
print("Heap: ", node.heap(), "Connecting to Wifi..")
print("Heap: ", node.heap(), "Application Version: ", require("device").swVersion)

-- hack to ensure pin D8 stays low after boot so it can be used with a high-level trigger relay
gpio.mode(8, gpio.OUTPUT)
gpio.write(8, gpio.LOW)

local startWifiSetup = function()
print("Heap: ", node.heap(), "Entering Wifi setup mode")
wifi.eventmon.unregister(wifi.eventmon.STA_DISCONNECTED)
wifiFailTimer:unregister()
wifiFailTimer = nil
enduser_setup.manual(false)
enduser_setup.start()
failsafeTimer:start()
end

-- wait 30 seconds before entering wifi setup mode in case of a momentary outage
wifiFailTimer = tmr.create()
wifiFailTimer:register(30000, tmr.ALARM_SINGLE, function() startWifiSetup() end)

-- failsafe: reboot after 5 minutes in case of extended wifi outage
failsafeTimer = tmr.create()
failsafeTimer:register(300000, tmr.ALARM_SINGLE, function() node.restart() end)

wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)
print("Heap: ", node.heap(), "Cannot connect to WiFi ", T.SSID, 'Reason Code:', T.reason)

if T.reason == wifi.eventmon.reason.AUTH_EXPIRE then
-- wifi password is incorrect, immediatly enter setup mode
print("Heap: ", node.heap(), "Wifi password is incorrect")
startWifiSetup()
else
wifiFailTimer:start()
end
end)

if wifi.sta.getconfig() == "" then
print("Heap: ", node.heap(), "WiFi not configured")
startWifiSetup()
print("Heap: ", node.heap(), "WiFi Setup started")
end

local _ = tmr.create():alarm(900, tmr.ALARM_AUTO, function(t)
require("led_flip").flip()
if wifi.sta.getip() then
wifi.eventmon.unregister(wifi.eventmon.STA_DISCONNECTED)
t:unregister()
t = nil
if wifiFailTimer then
wifiFailTimer:unregister()
wifiFailTimer = nil
end
failsafeTimer:unregister()
failsafeTimer = nil
print("Heap: ", node.heap(), "Wifi connected with IP: ", wifi.sta.getip())

gpio.write(4, gpio.HIGH)
enduser_setup.stop()
require("server")
print("Heap: ", node.heap(), "Loaded: ", "server")
require("application")
print("Heap: ", node.heap(), "Loaded: ", "application")
end
end)

dofile("wifi.lua")
100 changes: 100 additions & 0 deletions src/lfs/_init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
--
-- File: _init.lua
--[[
This is a template for the LFS equivalent of the SPIFFS init.lua.
It is a good idea to such an _init.lua module to your LFS and do most of the LFS
module related initialisaion in this. This example uses standard Lua features to
simplify the LFS API.
The first section adds a 'LFS' table to _G and uses the __index metamethod to
resolve functions in the LFS, so you can execute the main function of module
'fred' by executing LFS.fred(params), etc. It also implements some standard
readonly properties:
LFS._time The Unix Timestamp when the luac.cross was executed. This can be
used as a version identifier.
LFS._config This returns a table of useful configuration parameters, hence
print (("0x%6x"):format(LFS._config.lfs_base))
gives you the parameter to use in the luac.cross -a option.
LFS._list This returns a table of the LFS modules, hence
print(table.concat(LFS._list,'\n'))
gives you a single column listing of all modules in the LFS.
---------------------------------------------------------------------------------]]

local index = node.flashindex

local lfs_t = {
__index = function(_, name)
local fn_ut, ba, ma, size, modules = index(name)
if not ba then
return fn_ut
elseif name == '_time' then
return fn_ut
elseif name == '_config' then
local fs_ma, fs_size = file.fscfg()
return {lfs_base = ba, lfs_mapped = ma, lfs_size = size,
fs_mapped = fs_ma, fs_size = fs_size}
elseif name == '_list' then
return modules
else
return nil
end
end,

__newindex = function(_, name, value)
error("LFS is readonly. Invalid write to LFS." .. name, 2)
end,

}

local G=getfenv()
G.LFS = setmetatable(lfs_t,lfs_t)

--[[-------------------------------------------------------------------------------
The second section adds the LFS to the require searchlist, so that you can
require a Lua module 'jean' in the LFS by simply doing require "jean". However
note that this is at the search entry following the FS searcher, so if you also
have jean.lc or jean.lua in SPIFFS, then this SPIFFS version will get loaded into
RAM instead of using. (Useful, for development).
See docs/en/lfs.md and the 'loaders' array in app/lua/loadlib.c for more details.
---------------------------------------------------------------------------------]]

package.loaders[3] = function(module) -- loader_flash
local fn, ba = index(module)
return ba and "Module not in LFS" or fn
end

--[[-------------------------------------------------------------------------------
You can add any other initialisation here, for example a couple of the globals
are never used, so setting them to nil saves a couple of global entries
---------------------------------------------------------------------------------]]

G.module = nil -- disable Lua 5.0 style modules to save RAM
package.seeall = nil

--[[-------------------------------------------------------------------------------
These replaces the builtins loadfile & dofile with ones which preferentially
loads the corresponding module from LFS if present. Flipping the search order
is an exercise left to the reader.-
---------------------------------------------------------------------------------]]

local lf, df = loadfile, dofile
G.loadfile = function(n)
local mod, ext = n:match("(.*)%.(l[uc]a?)");
local fn, ba = index(mod)
if ba or (ext ~= 'lc' and ext ~= 'lua') then return lf(n) else return fn end
end

G.dofile = function(n)
local mod, ext = n:match("(.*)%.(l[uc]a?)");
local fn, ba = index(mod)
if ba or (ext ~= 'lc' and ext ~= 'lua') then return df(n) else return fn() end
end

File renamed without changes.
4 changes: 2 additions & 2 deletions src/device.lua → src/lfs/device.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
local me = {
id = "uuid:8f655392-a778-4fee-97b9-4825918" .. string.format("%x", node.chipid()),
name = "Konnected",
hwVersion = "2.2.7.beta1",
swVersion = "2.2.6",
hwVersion = "2.2.7",
swVersion = "2.2.7",
http_port = math.floor(node.chipid()/1000) + 8000,
urn = "urn:schemas-konnected-io:device:Security:1"
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
60 changes: 60 additions & 0 deletions src/lfs/wifi.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
print("Heap: ", node.heap(), "Connecting to Wifi..")
local startWifiSetup = function()
print("Heap: ", node.heap(), "Entering Wifi setup mode")
wifi.eventmon.unregister(wifi.eventmon.STA_DISCONNECTED)
wifiFailTimer:unregister()
wifiFailTimer = nil
enduser_setup.manual(false)
enduser_setup.start()
failsafeTimer:start()
end

-- wait 30 seconds before entering wifi setup mode in case of a momentary outage
wifiFailTimer = tmr.create()
wifiFailTimer:register(30000, tmr.ALARM_SINGLE, function() startWifiSetup() end)

-- failsafe: reboot after 5 minutes in case of extended wifi outage
failsafeTimer = tmr.create()
failsafeTimer:register(300000, tmr.ALARM_SINGLE, function() node.restart() end)

wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)
print("Heap: ", node.heap(), "Cannot connect to WiFi ", T.SSID, 'Reason Code:', T.reason)

if T.reason == wifi.eventmon.reason.AUTH_EXPIRE then
-- wifi password is incorrect, immediatly enter setup mode
print("Heap: ", node.heap(), "Wifi password is incorrect")
startWifiSetup()
else
wifiFailTimer:start()
end
end)

if wifi.sta.getconfig() == "" then
print("Heap: ", node.heap(), "WiFi not configured")
startWifiSetup()
print("Heap: ", node.heap(), "WiFi Setup started")
end

local _ = tmr.create():alarm(900, tmr.ALARM_AUTO, function(t)
require("led_flip").flip()
if wifi.sta.getip() then
wifi.eventmon.unregister(wifi.eventmon.STA_DISCONNECTED)
t:unregister()
t = nil
if wifiFailTimer then
wifiFailTimer:unregister()
wifiFailTimer = nil
end
failsafeTimer:unregister()
failsafeTimer = nil
print("Heap: ", node.heap(), "Wifi connected with IP: ", wifi.sta.getip())

gpio.write(4, gpio.HIGH)
enduser_setup.stop()
require("server")
print("Heap: ", node.heap(), "Loaded: ", "server")
require("application")
print("Heap: ", node.heap(), "Loaded: ", "application")
end
end)

File renamed without changes.

0 comments on commit a18e7f2

Please sign in to comment.