lfs_fragments.lua 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. -- First time image boot to discover the confuration
  2. --
  3. -- If you want to use absolute address LFS load or SPIFFS imaging, then boot the
  4. -- image for the first time bare, that is without either LFS or SPIFFS preloaded
  5. -- then enter the following commands interactively through the UART:
  6. --
  7. local _,mapa,fa=node.flashindex(); return ('0x%x, 0x%x, 0x%x'):format(
  8. mapa,fa,file.fscfg())
  9. --
  10. -- This will print out 3 hex constants: the absolute address used in the
  11. -- 'luac.cross -a' options and the flash adresses of the LFS and SPIFFS.
  12. --
  13. --[[ So you would need these commands to image your ESP module:
  14. USB=/dev/ttyUSB0 # or whatever the device of your USB is
  15. NODEMCU=~/nodemcu # The root of your NodeMCU file hierarchy
  16. SRC=$NODEMCU/local/lua # your source directory for your LFS Lua files.
  17. BIN=$NODEMCU/bin
  18. ESPTOOL=$NODEMCU/tools/esptool.py
  19. $ESPTOOL --port $USB erase_flash # Do this is you are having load funnies
  20. $ESPTOOL --port $USB --baud 460800 write_flash -fm dio 0x00000 \
  21. $BIN/0x00000.bin 0x10000 $BIN/0x10000.bin
  22. #
  23. # Now restart your module and use whatever your intective tool is to do the above
  24. # cmds, so if this outputs 0x4027b000, -0x7b000, 0x100000 then you can do
  25. #
  26. $NODEMCU/luac.cross -a 0x4027b000 -o $BIN/0x7b000-flash.img $SRC/*.lua
  27. $ESPTOOL --port $USB --baud 460800 write_flash -fm dio 0x7b000 \
  28. $BIN/0x7b000-flash.img
  29. # and if you've setup a SPIFFS then
  30. $ESPTOOL --port $USB --baud 460800 write_flash -fm dio 0x100000 \
  31. $BIN/0x100000-0x10000.img
  32. # and now you are good to go
  33. ]]
  34. -----------------------------------------------------------------------------------
  35. --
  36. -- It is a good idea to add an _init.lua module to your LFS and do most of the
  37. -- LFS module related initialisaion in this. This example uses standard Lua
  38. -- features to simplify the LFS API.
  39. --
  40. -- The first adds a 'LFS' table to _G and uses the __index metamethod to resolve
  41. -- functions in the LFS, so you can execute the main function of module 'fred'
  42. -- by doing LFS.fred(params)
  43. --
  44. -- The second adds the LFS to the require searchlist so that you can require a
  45. -- Lua module 'jean' in the LFS by simply doing require "jean". However not that
  46. -- this is at the search entry following the FS searcher, so if you also have
  47. -- jean.lc or jean.lua in SPIFFS, then this will get preferentially loaded,
  48. -- albeit into RAM. (Useful, for development).
  49. --
  50. do
  51. local index = node.flashindex
  52. local lfs_t = { __index = function(_, name)
  53. local fn, ba = index(name)
  54. if not ba then return fn end -- or return nil implied
  55. end}
  56. getfenv().LFS = setmetatable(lfs_t,lfs_t)
  57. local function loader_flash(module)
  58. local fn, ba = index(module)
  59. return ba and "Module not in LFS" or fn
  60. end
  61. package.loaders[3] = loader_flash
  62. end
  63. -----------------------------------------------------------------------------------
  64. --
  65. -- File: init.lua
  66. --
  67. -- With the previous example you still need an init.lua to bootstrap the _init
  68. -- module in LFS. Here is an example. It's a good idea either to use a timer
  69. -- delay or a GPIO pin during development, so that you as developer can break into
  70. -- the boot sequence if there is a problem with the _init bootstrap that is causing
  71. -- a panic loop. Here is one example of how you might do this. You have a second to
  72. -- inject tmr.stop(0) into UART0. Extend if your reactions can't meet this.
  73. --
  74. -- You also want to do autoload the LFS, for example by adding the following:
  75. --
  76. if node.flashindex() == nil then
  77. node.flashreload('flash.img')
  78. end
  79. tmr.alarm(0, 1000, tmr.ALARM_SINGLE,
  80. function()
  81. local fi=node.flashindex; return pcall(fi and fi'_init')
  82. end)
  83. -----------------------------------------------------------------------------------
  84. --
  85. -- The debug.getstrings function can be used to get a listing of the RAM (or ROM
  86. -- if LFS is loaded), as per the following example, so you can do this at the
  87. -- interactive prompt or call it as a debug function during a running application.
  88. --
  89. do
  90. local a=debug.getstrings'RAM'
  91. for i =1, #a do a[i] = ('%q'):format(a[i]) end
  92. print ('local preload='..table.concat(a,','))
  93. end
  94. -----------------------------------------------------------------------------------
  95. --
  96. -- File: LFS_dummy_strings.lua
  97. --
  98. -- luac.cross -f will generate a ROM string table that includes all strings
  99. -- referenced in the loaded modules. If you want to preload other string constants
  100. -- hen the trick is to include a dummy module in the LFS. You never need to call
  101. -- this. It's inclusion is enough to add the strings to the ROM table. Once in
  102. -- the ROM table, then you can use them in your application without incuring any
  103. -- RAM or Lua Garbage Collector (LGC) overhead. Here is a useful starting point,
  104. -- but you can add to this for your application.
  105. --
  106. -- The trick is to build the LFS as normal then run the previous example from your
  107. -- running application and append these lines to this file.
  108. --
  109. local preload = "?.lc;?.lua", "@init.lua", "_G", "_LOADED", "_LOADLIB", "__add",
  110. "__call", "__concat", "__div", "__eq", "__gc", "__index", "__le", "__len", "__lt",
  111. "__mod", "__mode", "__mul", "__newindex", "__pow", "__sub", "__tostring", "__unm",
  112. "collectgarbage", "cpath", "debug", "file", "file.obj", "file.vol", "flash",
  113. "getstrings", "index", "ipairs", "list", "loaded", "loader", "loaders", "loadlib",
  114. "module", "net.tcpserver", "net.tcpsocket", "net.udpsocket", "newproxy", "package",
  115. "pairs", "path", "preload", "reload", "require", "seeall", "wdclr"