2014-12-22 12:35:05 +01:00
|
|
|
|
// Module for interfacing with system
|
2015-12-16 06:04:58 +01:00
|
|
|
|
#include "module.h"
|
2014-12-22 12:35:05 +01:00
|
|
|
|
#include "lauxlib.h"
|
2020-04-27 02:13:38 +02:00
|
|
|
|
#include "lmem.h"
|
2015-11-09 00:46:08 +01:00
|
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
|
#include "platform.h"
|
2019-07-23 06:22:38 +02:00
|
|
|
|
#include <stdint.h>
|
2019-07-21 23:58:21 +02:00
|
|
|
|
#include <string.h>
|
Combined dsleep_set_options(option) to dsleep( us, option )
* dsleep( us, option )
Hardware has to support deep-sleep wake up (XPD_DCDC connects to EXT_RSTB with 0R). system_deep_sleep(0) ,set no wake up timer,connect a GPIO to pin RST, the chip will wake up by a falling-edge on pin RST.
** us: Integer
time to sleep.
if us = 0, it will sleep forever.
** option: Integer
option=0, init data byte 108 is valuable;
option>0, init data byte 108 is valueless.
More details as follows:
0, RF_CAL or not after deep-sleep wake up, depends on init data byte 108.
1, RF_CAL after deep-sleep wake up, there will belarge current.
2, no RF_CAL after deep-sleep wake up, there will only be small current.
4, disable RF after deep-sleep wake up, just like modem sleep, there will be the smallest current.
2015-01-29 07:21:38 +01:00
|
|
|
|
#include "user_interface.h"
|
2014-12-31 01:08:31 +01:00
|
|
|
|
#include "flash_api.h"
|
2016-09-05 20:17:13 +02:00
|
|
|
|
#include "vfs.h"
|
2015-03-06 04:59:04 +01:00
|
|
|
|
#include "user_version.h"
|
2016-01-20 09:37:03 +01:00
|
|
|
|
#include "rom.h"
|
2016-03-02 00:37:41 +01:00
|
|
|
|
#include "task/task.h"
|
2014-12-22 12:35:05 +01:00
|
|
|
|
|
2015-03-17 10:23:45 +01:00
|
|
|
|
#define CPU80MHZ 80
|
|
|
|
|
#define CPU160MHZ 160
|
|
|
|
|
|
2020-10-18 20:46:47 +02:00
|
|
|
|
#ifdef PLATFORM_STARTUP_COUNT
|
|
|
|
|
platform_startup_counts_t platform_startup_counts;
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-06-16 09:19:55 +02:00
|
|
|
|
#define DELAY2SEC 2000
|
|
|
|
|
|
2020-08-29 18:48:24 +02:00
|
|
|
|
#ifndef LUA_MAXINTEGER
|
|
|
|
|
#define LUA_MAXINTEGER INT_MAX
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-06-16 09:19:55 +02:00
|
|
|
|
static void restart_callback(void *arg) {
|
|
|
|
|
UNUSED(arg);
|
|
|
|
|
system_restart();
|
|
|
|
|
}
|
|
|
|
|
static int default_onerror(lua_State *L) {
|
|
|
|
|
static os_timer_t restart_timer = {0};
|
|
|
|
|
/* Use Lua print to print the ToS */
|
|
|
|
|
lua_settop(L, 1);
|
|
|
|
|
lua_getglobal(L, "print");
|
|
|
|
|
lua_insert(L, 1);
|
|
|
|
|
lua_pcall(L, 1, 0, 0);
|
|
|
|
|
/* One first time through set automatic restart after 2s delay */
|
|
|
|
|
if (!restart_timer.timer_func) {
|
|
|
|
|
os_timer_setfn(&restart_timer, restart_callback, NULL);
|
|
|
|
|
os_timer_arm(&restart_timer, DELAY2SEC, 0);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Lua: setonerror([function])
|
|
|
|
|
static int node_setonerror( lua_State* L ) {
|
|
|
|
|
lua_settop(L, 1);
|
|
|
|
|
if (!lua_isfunction(L, 1)) {
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
lua_pushcfunction(L, default_onerror);
|
|
|
|
|
}
|
|
|
|
|
lua_setfield(L, LUA_REGISTRYINDEX, "onerror");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
|
// Lua: startupcommand(string)
|
2020-10-18 20:46:47 +02:00
|
|
|
|
// The lua.startup({command="string"}) should be used instead
|
2020-04-27 02:13:38 +02:00
|
|
|
|
static int node_startupcommand( lua_State* L ) {
|
|
|
|
|
size_t l, lrcr;
|
|
|
|
|
const char *cmd = luaL_checklstring(L, 1, &l);
|
|
|
|
|
lrcr = platform_rcr_write(PLATFORM_RCR_INITSTR, cmd, l+1);
|
|
|
|
|
lua_pushboolean(L, lrcr == ~0 ? 0 : 1);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-18 20:46:47 +02:00
|
|
|
|
// Lua: startup([table])
|
|
|
|
|
static int node_startup( lua_State* L ) {
|
|
|
|
|
uint32_t option, *option_p;
|
|
|
|
|
|
|
|
|
|
option = 0;
|
|
|
|
|
|
|
|
|
|
if (platform_rcr_read(PLATFORM_RCR_STARTUP_OPTION, (void **) &option_p) == sizeof(option)) {
|
|
|
|
|
option = *option_p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (lua_gettop(L) > 0) {
|
|
|
|
|
// Lets hope it is a table
|
|
|
|
|
luaL_checktype(L, 1, LUA_TTABLE);
|
|
|
|
|
|
|
|
|
|
int has_entries = 0;
|
|
|
|
|
lua_pushnil(L);
|
|
|
|
|
while (lua_next(L, 1) != 0) {
|
|
|
|
|
if (lua_isstring(L, -2)) {
|
|
|
|
|
const char *key = lua_tostring(L, -2);
|
|
|
|
|
has_entries++;
|
|
|
|
|
|
|
|
|
|
if (strcmp(key, "command") == 0) {
|
|
|
|
|
size_t l, lrcr;
|
|
|
|
|
const char *cmd = luaL_checklstring(L, -1, &l);
|
|
|
|
|
lrcr = platform_rcr_write(PLATFORM_RCR_INITSTR, cmd, l+1);
|
|
|
|
|
if (lrcr == ~0) {
|
|
|
|
|
return luaL_error( L, "failed to set command" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (strcmp(key, "banner") == 0) {
|
|
|
|
|
int enable = lua_toboolean(L, -1);
|
|
|
|
|
option = (option & ~STARTUP_OPTION_NO_BANNER) | (enable ? 0 : STARTUP_OPTION_NO_BANNER);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (strcmp(key, "frequency") == 0) {
|
|
|
|
|
int frequency = lua_tointeger(L, -1);
|
|
|
|
|
option = (option & ~STARTUP_OPTION_CPU_FREQ_MAX) | (frequency == CPU160MHZ ? STARTUP_OPTION_CPU_FREQ_MAX : 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (strcmp(key, "delay_mount") == 0) {
|
|
|
|
|
int enable = lua_toboolean(L, -1);
|
|
|
|
|
option = (option & ~STARTUP_OPTION_DELAY_MOUNT) | (enable ? STARTUP_OPTION_DELAY_MOUNT : 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (has_entries) {
|
|
|
|
|
platform_rcr_write(PLATFORM_RCR_STARTUP_OPTION, &option, sizeof(option));
|
|
|
|
|
} else {
|
|
|
|
|
// This is a special reset everything case
|
|
|
|
|
platform_rcr_delete(PLATFORM_RCR_STARTUP_OPTION);
|
|
|
|
|
platform_rcr_delete(PLATFORM_RCR_INITSTR);
|
|
|
|
|
option = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Now we construct the return table
|
|
|
|
|
lua_createtable(L, 0, 4);
|
|
|
|
|
|
|
|
|
|
const char *init_string;
|
|
|
|
|
size_t l;
|
|
|
|
|
|
|
|
|
|
l = platform_rcr_read(PLATFORM_RCR_INITSTR, (void **) &init_string);
|
|
|
|
|
if (l != ~0) {
|
|
|
|
|
// when reading it back it can be padded with nulls
|
|
|
|
|
while (l > 0 && init_string[l - 1] == 0) {
|
|
|
|
|
l--;
|
|
|
|
|
}
|
|
|
|
|
lua_pushlstring(L, init_string, l);
|
|
|
|
|
lua_setfield(L, -2, "command");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lua_pushboolean(L, !(option & STARTUP_OPTION_NO_BANNER));
|
|
|
|
|
lua_setfield(L, -2, "banner");
|
|
|
|
|
|
|
|
|
|
lua_pushboolean(L, (option & STARTUP_OPTION_DELAY_MOUNT));
|
|
|
|
|
lua_setfield(L, -2, "delay_mount");
|
|
|
|
|
|
|
|
|
|
lua_pushinteger(L, (option & STARTUP_OPTION_CPU_FREQ_MAX) ? CPU160MHZ : CPU80MHZ);
|
|
|
|
|
lua_setfield(L, -2, "frequency");
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2020-04-27 02:13:38 +02:00
|
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
|
// Lua: restart()
|
2015-01-05 03:09:51 +01:00
|
|
|
|
static int node_restart( lua_State* L )
|
2014-12-22 12:35:05 +01:00
|
|
|
|
{
|
|
|
|
|
system_restart();
|
2015-03-15 22:40:43 +01:00
|
|
|
|
return 0;
|
2014-12-22 12:35:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-26 22:45:24 +02:00
|
|
|
|
static int dsleepMax( lua_State *L ) {
|
2020-08-29 18:48:24 +02:00
|
|
|
|
uint64_t dsm = (((uint64_t)system_rtc_clock_cali_proc())*(0x80000000-1))/0x1000;
|
|
|
|
|
lua_pushnumber(L, (lua_Float) dsm);
|
2018-04-26 22:45:24 +02:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
Combined dsleep_set_options(option) to dsleep( us, option )
* dsleep( us, option )
Hardware has to support deep-sleep wake up (XPD_DCDC connects to EXT_RSTB with 0R). system_deep_sleep(0) ,set no wake up timer,connect a GPIO to pin RST, the chip will wake up by a falling-edge on pin RST.
** us: Integer
time to sleep.
if us = 0, it will sleep forever.
** option: Integer
option=0, init data byte 108 is valuable;
option>0, init data byte 108 is valueless.
More details as follows:
0, RF_CAL or not after deep-sleep wake up, depends on init data byte 108.
1, RF_CAL after deep-sleep wake up, there will belarge current.
2, no RF_CAL after deep-sleep wake up, there will only be small current.
4, disable RF after deep-sleep wake up, just like modem sleep, there will be the smallest current.
2015-01-29 07:21:38 +01:00
|
|
|
|
// Lua: dsleep( us, option )
|
2015-01-05 03:09:51 +01:00
|
|
|
|
static int node_deepsleep( lua_State* L )
|
2014-12-22 12:35:05 +01:00
|
|
|
|
{
|
2018-04-26 22:45:24 +02:00
|
|
|
|
uint64 us;
|
2016-01-02 12:10:27 +01:00
|
|
|
|
uint8 option;
|
Combined dsleep_set_options(option) to dsleep( us, option )
* dsleep( us, option )
Hardware has to support deep-sleep wake up (XPD_DCDC connects to EXT_RSTB with 0R). system_deep_sleep(0) ,set no wake up timer,connect a GPIO to pin RST, the chip will wake up by a falling-edge on pin RST.
** us: Integer
time to sleep.
if us = 0, it will sleep forever.
** option: Integer
option=0, init data byte 108 is valuable;
option>0, init data byte 108 is valueless.
More details as follows:
0, RF_CAL or not after deep-sleep wake up, depends on init data byte 108.
1, RF_CAL after deep-sleep wake up, there will belarge current.
2, no RF_CAL after deep-sleep wake up, there will only be small current.
4, disable RF after deep-sleep wake up, just like modem sleep, there will be the smallest current.
2015-01-29 07:21:38 +01:00
|
|
|
|
// Set deleep option, skip if nil
|
|
|
|
|
if ( lua_isnumber(L, 2) )
|
|
|
|
|
{
|
2020-08-29 18:48:24 +02:00
|
|
|
|
option = lua_tounsigned(L, 2);
|
|
|
|
|
luaL_argcheck(L, 2, option <= 4, "wrong option value" );
|
|
|
|
|
system_deep_sleep_set_option( option );
|
Combined dsleep_set_options(option) to dsleep( us, option )
* dsleep( us, option )
Hardware has to support deep-sleep wake up (XPD_DCDC connects to EXT_RSTB with 0R). system_deep_sleep(0) ,set no wake up timer,connect a GPIO to pin RST, the chip will wake up by a falling-edge on pin RST.
** us: Integer
time to sleep.
if us = 0, it will sleep forever.
** option: Integer
option=0, init data byte 108 is valuable;
option>0, init data byte 108 is valueless.
More details as follows:
0, RF_CAL or not after deep-sleep wake up, depends on init data byte 108.
1, RF_CAL after deep-sleep wake up, there will belarge current.
2, no RF_CAL after deep-sleep wake up, there will only be small current.
4, disable RF after deep-sleep wake up, just like modem sleep, there will be the smallest current.
2015-01-29 07:21:38 +01:00
|
|
|
|
}
|
2020-08-29 18:48:24 +02:00
|
|
|
|
bool instant = (lua_isnumber(L, 3) && luaL_checkinteger(L, 3)) ? true: false;
|
|
|
|
|
|
Combined dsleep_set_options(option) to dsleep( us, option )
* dsleep( us, option )
Hardware has to support deep-sleep wake up (XPD_DCDC connects to EXT_RSTB with 0R). system_deep_sleep(0) ,set no wake up timer,connect a GPIO to pin RST, the chip will wake up by a falling-edge on pin RST.
** us: Integer
time to sleep.
if us = 0, it will sleep forever.
** option: Integer
option=0, init data byte 108 is valuable;
option>0, init data byte 108 is valueless.
More details as follows:
0, RF_CAL or not after deep-sleep wake up, depends on init data byte 108.
1, RF_CAL after deep-sleep wake up, there will belarge current.
2, no RF_CAL after deep-sleep wake up, there will only be small current.
4, disable RF after deep-sleep wake up, just like modem sleep, there will be the smallest current.
2015-01-29 07:21:38 +01:00
|
|
|
|
if ( lua_isnumber(L, 1) )
|
|
|
|
|
{
|
2020-08-29 18:48:24 +02:00
|
|
|
|
#if LUA_VERSION_NUM == 501
|
2016-01-02 12:10:27 +01:00
|
|
|
|
us = luaL_checknumber(L, 1);
|
2020-08-29 18:48:24 +02:00
|
|
|
|
#else /* 503 */
|
|
|
|
|
us = lua_isinteger(L, 1) ? lua_tounsigned(L, 1) : (uint64) lua_tonumber(L, 1);
|
|
|
|
|
#endif
|
|
|
|
|
luaL_argcheck(L, 1, us < 36000000000ull, "invalid time value" );
|
|
|
|
|
if (instant)
|
|
|
|
|
system_deep_sleep_instant(us);
|
Combined dsleep_set_options(option) to dsleep( us, option )
* dsleep( us, option )
Hardware has to support deep-sleep wake up (XPD_DCDC connects to EXT_RSTB with 0R). system_deep_sleep(0) ,set no wake up timer,connect a GPIO to pin RST, the chip will wake up by a falling-edge on pin RST.
** us: Integer
time to sleep.
if us = 0, it will sleep forever.
** option: Integer
option=0, init data byte 108 is valuable;
option>0, init data byte 108 is valueless.
More details as follows:
0, RF_CAL or not after deep-sleep wake up, depends on init data byte 108.
1, RF_CAL after deep-sleep wake up, there will belarge current.
2, no RF_CAL after deep-sleep wake up, there will only be small current.
4, disable RF after deep-sleep wake up, just like modem sleep, there will be the smallest current.
2015-01-29 07:21:38 +01:00
|
|
|
|
else
|
2020-08-29 18:48:24 +02:00
|
|
|
|
system_deep_sleep(us);
|
|
|
|
|
}
|
2015-03-15 22:40:43 +01:00
|
|
|
|
return 0;
|
2014-12-22 12:35:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-04 21:31:06 +02:00
|
|
|
|
|
|
|
|
|
#ifdef PMSLEEP_ENABLE
|
2018-04-13 21:41:14 +02:00
|
|
|
|
#include "pm/pmSleep.h"
|
2017-04-04 21:31:06 +02:00
|
|
|
|
|
|
|
|
|
int node_sleep_resume_cb_ref= LUA_NOREF;
|
|
|
|
|
void node_sleep_resume_cb(void)
|
|
|
|
|
{
|
|
|
|
|
PMSLEEP_DBG("START");
|
|
|
|
|
pmSleep_execute_lua_cb(&node_sleep_resume_cb_ref);
|
|
|
|
|
PMSLEEP_DBG("END");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Lua: node.sleep(table)
|
|
|
|
|
static int node_sleep( lua_State* L )
|
|
|
|
|
{
|
2018-04-13 21:41:14 +02:00
|
|
|
|
#ifdef TIMER_SUSPEND_ENABLE
|
2017-04-04 21:31:06 +02:00
|
|
|
|
pmSleep_INIT_CFG(cfg);
|
|
|
|
|
cfg.sleep_mode=LIGHT_SLEEP_T;
|
|
|
|
|
|
|
|
|
|
if(lua_istable(L, 1)){
|
|
|
|
|
pmSleep_parse_table_lua(L, 1, &cfg, NULL, &node_sleep_resume_cb_ref);
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
return luaL_argerror(L, 1, "must be table");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cfg.resume_cb_ptr = &node_sleep_resume_cb;
|
|
|
|
|
pmSleep_suspend(&cfg);
|
2018-04-13 21:41:14 +02:00
|
|
|
|
#else
|
2018-05-08 22:43:12 +02:00
|
|
|
|
dbg_printf("\n The option \"TIMER_SUSPEND_ENABLE\" in \"app/include/user_config.h\" was disabled during FW build!\n");
|
|
|
|
|
return luaL_error(L, "node.sleep() is unavailable");
|
2018-04-13 21:41:14 +02:00
|
|
|
|
#endif
|
2017-04-04 21:31:06 +02:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
2018-05-08 22:43:12 +02:00
|
|
|
|
#else
|
|
|
|
|
static int node_sleep( lua_State* L )
|
|
|
|
|
{
|
|
|
|
|
dbg_printf("\n The options \"TIMER_SUSPEND_ENABLE\" and \"PMSLEEP_ENABLE\" in \"app/include/user_config.h\" were disabled during FW build!\n");
|
|
|
|
|
return luaL_error(L, "node.sleep() is unavailable");
|
|
|
|
|
}
|
2017-04-04 21:31:06 +02:00
|
|
|
|
#endif //PMSLEEP_ENABLE
|
2020-04-27 02:13:38 +02:00
|
|
|
|
|
|
|
|
|
static void add_int_field( lua_State* L, lua_Integer i, const char *name){
|
|
|
|
|
lua_pushinteger(L, i);
|
|
|
|
|
lua_setfield(L, -2, name);
|
|
|
|
|
}
|
|
|
|
|
static void add_string_field( lua_State* L, const char *s, const char *name) {
|
|
|
|
|
lua_pushstring(L, s);
|
|
|
|
|
lua_setfield(L, -2, name);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-22 18:41:02 +02:00
|
|
|
|
static void get_lfs_config ( lua_State* L ){
|
|
|
|
|
int config[5];
|
|
|
|
|
lua_getlfsconfig(L, config);
|
|
|
|
|
lua_createtable(L, 0, 4);
|
|
|
|
|
add_int_field(L, config[0], "lfs_mapped");
|
|
|
|
|
add_int_field(L, config[1], "lfs_base");
|
|
|
|
|
add_int_field(L, config[2], "lfs_size");
|
|
|
|
|
add_int_field(L, config[3], "lfs_used");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int node_info( lua_State* L ){
|
|
|
|
|
const char* options[] = {"lfs", "hw", "sw_version", "build_config", "legacy", NULL};
|
|
|
|
|
int option = luaL_checkoption (L, 1, options[4], options);
|
2019-07-19 21:24:22 +02:00
|
|
|
|
|
|
|
|
|
switch (option) {
|
2020-08-22 18:41:02 +02:00
|
|
|
|
case 0: { // lfs
|
|
|
|
|
get_lfs_config(L);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
case 1: { // hw
|
|
|
|
|
lua_createtable(L, 0, 5);
|
2020-04-27 02:13:38 +02:00
|
|
|
|
add_int_field(L, system_get_chip_id(), "chip_id");
|
|
|
|
|
add_int_field(L, spi_flash_get_id(), "flash_id");
|
|
|
|
|
add_int_field(L, flash_rom_get_size_byte() / 1024, "flash_size");
|
|
|
|
|
add_int_field(L, flash_rom_get_mode(), "flash_mode");
|
|
|
|
|
add_int_field(L, flash_rom_get_speed(), "flash_speed");
|
2019-07-19 21:24:22 +02:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
2020-08-22 18:41:02 +02:00
|
|
|
|
case 2: { // sw_version
|
|
|
|
|
lua_createtable(L, 0, 7);
|
2020-04-27 02:13:38 +02:00
|
|
|
|
add_int_field(L, NODE_VERSION_MAJOR, "node_version_major");
|
|
|
|
|
add_int_field(L, NODE_VERSION_MINOR, "node_version_minor");
|
|
|
|
|
add_int_field(L, NODE_VERSION_REVISION, "node_version_revision");
|
|
|
|
|
add_string_field(L, BUILDINFO_BRANCH, "git_branch");
|
|
|
|
|
add_string_field(L, BUILDINFO_COMMIT_ID, "git_commit_id");
|
|
|
|
|
add_string_field(L, BUILDINFO_RELEASE, "git_release");
|
|
|
|
|
add_string_field(L, BUILDINFO_RELEASE_DTS, "git_commit_dts");
|
2019-07-19 21:24:22 +02:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
2020-08-22 18:41:02 +02:00
|
|
|
|
case 3: { // build_config
|
|
|
|
|
lua_createtable(L, 0, 4);
|
2019-07-19 21:24:22 +02:00
|
|
|
|
lua_pushboolean(L, BUILDINFO_SSL);
|
2020-04-27 02:13:38 +02:00
|
|
|
|
lua_setfield(L, -2, "ssl");
|
2020-08-29 18:48:24 +02:00
|
|
|
|
lua_pushinteger(L, BUILDINFO_LFS_SIZE);
|
2020-04-27 02:13:38 +02:00
|
|
|
|
lua_setfield(L, -2, "lfs_size");
|
|
|
|
|
add_string_field(L, BUILDINFO_MODULES, "modules");
|
|
|
|
|
add_string_field(L, BUILDINFO_BUILD_TYPE, "number_type");
|
2019-07-19 21:24:22 +02:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
2020-08-22 18:41:02 +02:00
|
|
|
|
default: { // legacy
|
2019-07-19 21:24:22 +02:00
|
|
|
|
platform_print_deprecation_note("node.info() without parameter", "in the next version");
|
|
|
|
|
lua_pushinteger(L, NODE_VERSION_MAJOR);
|
|
|
|
|
lua_pushinteger(L, NODE_VERSION_MINOR);
|
|
|
|
|
lua_pushinteger(L, NODE_VERSION_REVISION);
|
|
|
|
|
lua_pushinteger(L, system_get_chip_id()); // chip id
|
|
|
|
|
lua_pushinteger(L, spi_flash_get_id()); // flash id
|
|
|
|
|
lua_pushinteger(L, flash_rom_get_size_byte() / 1024); // flash size in KB
|
|
|
|
|
lua_pushinteger(L, flash_rom_get_mode());
|
|
|
|
|
lua_pushinteger(L, flash_rom_get_speed());
|
|
|
|
|
return 8;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-12-31 07:26:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
|
// Lua: chipid()
|
2015-01-05 03:09:51 +01:00
|
|
|
|
static int node_chipid( lua_State* L )
|
2014-12-22 12:35:05 +01:00
|
|
|
|
{
|
|
|
|
|
uint32_t id = system_get_chip_id();
|
|
|
|
|
lua_pushinteger(L, id);
|
2015-03-15 22:40:43 +01:00
|
|
|
|
return 1;
|
2014-12-22 12:35:05 +01:00
|
|
|
|
}
|
2015-03-26 17:52:55 +01:00
|
|
|
|
|
|
|
|
|
// deprecated, moved to adc module
|
2015-01-26 11:17:15 +01:00
|
|
|
|
// Lua: readvdd33()
|
2015-03-26 17:52:55 +01:00
|
|
|
|
// static int node_readvdd33( lua_State* L )
|
|
|
|
|
// {
|
|
|
|
|
// uint32_t vdd33 = readvdd33();
|
|
|
|
|
// lua_pushinteger(L, vdd33);
|
|
|
|
|
// return 1;
|
|
|
|
|
// }
|
2014-12-31 01:08:31 +01:00
|
|
|
|
// Lua: flashid()
|
2015-01-05 03:09:51 +01:00
|
|
|
|
static int node_flashid( lua_State* L )
|
2014-12-31 01:08:31 +01:00
|
|
|
|
{
|
|
|
|
|
uint32_t id = spi_flash_get_id();
|
|
|
|
|
lua_pushinteger( L, id );
|
2015-03-15 22:40:43 +01:00
|
|
|
|
return 1;
|
2014-12-31 01:08:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Lua: flashsize()
|
2015-01-05 03:09:51 +01:00
|
|
|
|
static int node_flashsize( lua_State* L )
|
2014-12-31 01:08:31 +01:00
|
|
|
|
{
|
2015-03-15 17:51:47 +01:00
|
|
|
|
uint32_t sz = flash_rom_get_size_byte();
|
2014-12-31 01:08:31 +01:00
|
|
|
|
lua_pushinteger( L, sz );
|
2015-03-15 22:40:43 +01:00
|
|
|
|
return 1;
|
2014-12-31 01:08:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
|
// Lua: heap()
|
2015-01-05 03:09:51 +01:00
|
|
|
|
static int node_heap( lua_State* L )
|
2014-12-22 12:35:05 +01:00
|
|
|
|
{
|
|
|
|
|
uint32_t sz = system_get_free_heap_size();
|
|
|
|
|
lua_pushinteger(L, sz);
|
2015-03-15 22:40:43 +01:00
|
|
|
|
return 1;
|
2014-12-22 12:35:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Lua: input("string")
|
2018-06-22 23:29:16 +02:00
|
|
|
|
static int node_input( lua_State* L ) {
|
2019-07-18 18:02:02 +02:00
|
|
|
|
luaL_checkstring(L, 1);
|
|
|
|
|
lua_getfield(L, LUA_REGISTRYINDEX, "stdin");
|
|
|
|
|
lua_rawgeti(L, -1, 1); /* get the pipe_write func from stdin[1] */
|
2020-04-27 02:13:38 +02:00
|
|
|
|
lua_insert(L, -2); /* and move above the pipe ref */
|
2019-07-18 18:02:02 +02:00
|
|
|
|
lua_pushvalue(L, 1);
|
2020-06-16 09:19:55 +02:00
|
|
|
|
lua_call(L, 2, 0); /* stdin:write(line); errors are thrown to caller */
|
2014-12-22 12:35:05 +01:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int serial_debug = 1;
|
2019-07-18 18:02:02 +02:00
|
|
|
|
|
2020-06-16 09:19:55 +02:00
|
|
|
|
/*
|
|
|
|
|
** Output redirector. Note that panics in the output callback cannot be processed
|
|
|
|
|
** using luaL_pcallx() as this would create an infinite error loop, so they are
|
|
|
|
|
** reported direct to the UART.
|
|
|
|
|
*/
|
2020-04-27 02:13:38 +02:00
|
|
|
|
void output_redirect(const char *str, size_t l) {
|
Add New Tasking I/F and rework GPIO, UART, etc to support it
As with the last commit this rolls up the follwowing, but include the various
review comments on the PR.
- **Documentation changes**. I've added the taks FAQ as a stub new Extension
developer FAQ, and split the old FAQ into a Lua Developer FAQ and a Hardware
FAQ.
- **Tasking I/F**. New `app/task/Makefile`, `app/task/task.c`,
`app/include/task/task.h` and `app/Makefile` as per previous commit. Cascade
changes to `app/driver/uart.c`, `app/include/driver/uart.h`,
`app/user/user_main.c` and `app/modules/node.c`
- **GPIO Rework** to `app/modules/gpio.c` and `pin_map.[hc]`, `platform.[hc]`
in `app/platform`
- **Other Optimisations** Move the `platform_*_exists()` from
`app/platform/common.c` to static inline declarations in `platform.h` as
this generates faster, smaller code. Move lgc.a routines out of iram0.
2016-02-17 18:13:17 +01:00
|
|
|
|
lua_State *L = lua_getstate();
|
2019-07-18 18:02:02 +02:00
|
|
|
|
int n = lua_gettop(L);
|
|
|
|
|
lua_pushliteral(L, "stdout");
|
|
|
|
|
lua_rawget(L, LUA_REGISTRYINDEX); /* fetch reg.stdout */
|
|
|
|
|
if (lua_istable(L, -1)) { /* reg.stdout is pipe */
|
|
|
|
|
if (serial_debug) {
|
2020-04-27 02:13:38 +02:00
|
|
|
|
uart0_sendStrn(str, l);
|
2019-07-18 18:02:02 +02:00
|
|
|
|
}
|
|
|
|
|
lua_rawgeti(L, -1, 1); /* get the pipe_write func from stdout[1] */
|
2020-04-27 02:13:38 +02:00
|
|
|
|
lua_insert(L, -2); /* and move above the pipe ref */
|
|
|
|
|
lua_pushlstring(L, str, l);
|
2020-06-16 09:19:55 +02:00
|
|
|
|
if (lua_pcall(L, 2, 0, 0) != LUA_OK) { /* Reg.stdout:write(str) */
|
|
|
|
|
lua_writestringerror("error calling stdout:write(%s)\n", lua_tostring(L, -1));
|
|
|
|
|
system_restart();
|
|
|
|
|
}
|
2019-07-18 18:02:02 +02:00
|
|
|
|
} else { /* reg.stdout == nil */
|
2020-04-27 02:13:38 +02:00
|
|
|
|
uart0_sendStrn(str, l);
|
2014-12-22 12:35:05 +01:00
|
|
|
|
}
|
2019-07-18 18:02:02 +02:00
|
|
|
|
lua_settop(L, n); /* Make sure all code paths leave stack unchanged */
|
2014-12-22 12:35:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-18 18:02:02 +02:00
|
|
|
|
extern int pipe_create(lua_State *L);
|
|
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
|
// Lua: output(function(c), debug)
|
2015-01-05 03:09:51 +01:00
|
|
|
|
static int node_output( lua_State* L )
|
2014-12-22 12:35:05 +01:00
|
|
|
|
{
|
2019-07-18 18:02:02 +02:00
|
|
|
|
serial_debug = (lua_isnumber(L, 2) && lua_tointeger(L, 2) == 0) ? 0 : 1;
|
|
|
|
|
lua_settop(L, 1);
|
2020-04-27 02:13:38 +02:00
|
|
|
|
if (lua_isfunction(L, 1)) {
|
|
|
|
|
lua_pushcfunction(L, pipe_create);
|
|
|
|
|
lua_insert(L, 1);
|
2019-07-18 18:02:02 +02:00
|
|
|
|
lua_pushinteger(L, LUA_TASK_MEDIUM);
|
2020-06-16 09:19:55 +02:00
|
|
|
|
lua_call(L, 2, 1); /* Any pipe.create() errors thrown back to caller */
|
2019-07-18 18:02:02 +02:00
|
|
|
|
} else { // remove the stdout pipe
|
|
|
|
|
lua_pop(L,1);
|
|
|
|
|
lua_pushnil(L); /* T[1] = nil */
|
2014-12-22 12:35:05 +01:00
|
|
|
|
serial_debug = 1;
|
|
|
|
|
}
|
2019-07-18 18:02:02 +02:00
|
|
|
|
lua_pushliteral(L, "stdout");
|
2020-04-27 02:13:38 +02:00
|
|
|
|
lua_insert(L, 1);
|
2019-07-18 18:02:02 +02:00
|
|
|
|
lua_rawset(L, LUA_REGISTRYINDEX); /* Reg.stdout = nil or pipe */
|
2015-03-15 22:40:43 +01:00
|
|
|
|
return 0;
|
2014-12-22 12:35:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-13 08:11:59 +01:00
|
|
|
|
static int writer(lua_State* L, const void* p, size_t size, void* u)
|
|
|
|
|
{
|
|
|
|
|
UNUSED(L);
|
|
|
|
|
int file_fd = *( (int *)u );
|
2016-09-05 20:17:13 +02:00
|
|
|
|
if (!file_fd)
|
2015-02-13 08:11:59 +01:00
|
|
|
|
return 1;
|
2015-03-15 22:40:43 +01:00
|
|
|
|
NODE_DBG("get fd:%d,size:%d\n", file_fd, size);
|
|
|
|
|
|
2016-09-05 20:17:13 +02:00
|
|
|
|
if (size != 0 && (size != vfs_write(file_fd, (const char *)p, size)) )
|
2015-02-13 08:11:59 +01:00
|
|
|
|
return 1;
|
2015-03-15 22:40:43 +01:00
|
|
|
|
NODE_DBG("write fd:%d,size:%d\n", file_fd, size);
|
2015-02-13 08:11:59 +01:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
|
#if LUA_VERSION_NUM == 501
|
|
|
|
|
#define getproto(o) (clvalue(o)->l.p)
|
|
|
|
|
#endif
|
|
|
|
|
|
2015-02-13 08:11:59 +01:00
|
|
|
|
// Lua: compile(filename) -- compile lua file into lua bytecode, and save to .lc
|
|
|
|
|
static int node_compile( lua_State* L )
|
|
|
|
|
{
|
|
|
|
|
Proto* f;
|
2016-09-05 20:17:13 +02:00
|
|
|
|
int file_fd = 0;
|
2015-02-13 08:11:59 +01:00
|
|
|
|
size_t len;
|
|
|
|
|
const char *fname = luaL_checklstring( L, 1, &len );
|
2016-09-05 20:17:13 +02:00
|
|
|
|
const char *basename = vfs_basename( fname );
|
2019-07-21 23:58:21 +02:00
|
|
|
|
luaL_argcheck(L, strlen(basename) <= FS_OBJ_NAME_LEN && strlen(fname) == len, 1, "filename invalid");
|
2015-02-13 08:11:59 +01:00
|
|
|
|
|
2016-09-05 20:17:13 +02:00
|
|
|
|
char *output = luaM_malloc( L, len+1 );
|
2019-07-21 23:58:21 +02:00
|
|
|
|
strcpy(output, fname);
|
2015-02-13 08:11:59 +01:00
|
|
|
|
// check here that filename end with ".lua".
|
2019-07-21 23:58:21 +02:00
|
|
|
|
if (len < 4 || (strcmp( output + len - 4, ".lua") != 0) ) {
|
2016-09-05 20:17:13 +02:00
|
|
|
|
luaM_free( L, output );
|
2015-02-13 08:11:59 +01:00
|
|
|
|
return luaL_error(L, "not a .lua file");
|
2016-09-05 20:17:13 +02:00
|
|
|
|
}
|
2015-02-13 08:11:59 +01:00
|
|
|
|
|
2019-07-21 23:58:21 +02:00
|
|
|
|
output[strlen(output) - 2] = 'c';
|
|
|
|
|
output[strlen(output) - 1] = '\0';
|
2015-02-13 08:11:59 +01:00
|
|
|
|
NODE_DBG(output);
|
|
|
|
|
NODE_DBG("\n");
|
2019-07-18 18:02:02 +02:00
|
|
|
|
if (luaL_loadfile(L, fname) != 0) {
|
2016-09-05 20:17:13 +02:00
|
|
|
|
luaM_free( L, output );
|
2015-03-15 22:40:43 +01:00
|
|
|
|
return luaL_error(L, lua_tostring(L, -1));
|
2015-02-13 08:11:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int stripping = 1; /* strip debug information? */
|
|
|
|
|
|
2016-09-05 20:17:13 +02:00
|
|
|
|
file_fd = vfs_open(output, "w+");
|
|
|
|
|
if (!file_fd)
|
2015-02-13 08:11:59 +01:00
|
|
|
|
{
|
2016-09-05 20:17:13 +02:00
|
|
|
|
luaM_free( L, output );
|
2015-02-13 08:11:59 +01:00
|
|
|
|
return luaL_error(L, "cannot open/write to file");
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
|
int result = lua_dump(L, writer, &file_fd, stripping);
|
2015-02-13 08:11:59 +01:00
|
|
|
|
|
2016-09-05 20:17:13 +02:00
|
|
|
|
if (vfs_flush(file_fd) != VFS_RES_OK) {
|
2015-08-11 00:08:49 +02:00
|
|
|
|
// overwrite Lua error, like writer() does in case of a file io error
|
|
|
|
|
result = 1;
|
|
|
|
|
}
|
2016-09-05 20:17:13 +02:00
|
|
|
|
vfs_close(file_fd);
|
|
|
|
|
file_fd = 0;
|
|
|
|
|
luaM_free( L, output );
|
2015-02-13 08:11:59 +01:00
|
|
|
|
|
2015-03-15 22:40:43 +01:00
|
|
|
|
if (result == LUA_ERR_CC_INTOVERFLOW) {
|
2015-02-13 08:11:59 +01:00
|
|
|
|
return luaL_error(L, "value too big or small for target integer type");
|
|
|
|
|
}
|
2015-03-15 22:40:43 +01:00
|
|
|
|
if (result == LUA_ERR_CC_NOTINTEGER) {
|
2015-02-13 08:11:59 +01:00
|
|
|
|
return luaL_error(L, "target lua_Number is integral but fractional value found");
|
|
|
|
|
}
|
2015-08-11 00:08:49 +02:00
|
|
|
|
if (result == 1) { // result status generated by writer() or fs_flush() fail
|
|
|
|
|
return luaL_error(L, "writing to file failed");
|
|
|
|
|
}
|
2015-02-13 08:11:59 +01:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-03 16:28:34 +01:00
|
|
|
|
// Lua: node.task.post([priority],task_cb) -- schedule a task for execution next
|
2016-03-02 00:37:41 +01:00
|
|
|
|
static int node_task_post( lua_State* L )
|
|
|
|
|
{
|
2019-07-18 18:02:02 +02:00
|
|
|
|
int n=1;
|
2016-03-02 00:37:41 +01:00
|
|
|
|
unsigned priority = TASK_PRIORITY_MEDIUM;
|
2019-07-18 18:02:02 +02:00
|
|
|
|
if (lua_type(L, 1) == LUA_TNUMBER) {
|
2016-03-02 00:37:41 +01:00
|
|
|
|
priority = (unsigned) luaL_checkint(L, 1);
|
|
|
|
|
luaL_argcheck(L, priority <= TASK_PRIORITY_HIGH, 1, "invalid priority");
|
2019-07-18 18:02:02 +02:00
|
|
|
|
n++;
|
2016-03-03 16:28:34 +01:00
|
|
|
|
}
|
2020-04-27 02:13:38 +02:00
|
|
|
|
luaL_checktype(L, n, LUA_TFUNCTION);
|
2019-07-18 18:02:02 +02:00
|
|
|
|
lua_settop(L, n);
|
2020-04-27 02:13:38 +02:00
|
|
|
|
(void) luaL_posttask(L, priority);
|
2016-03-03 16:28:34 +01:00
|
|
|
|
return 0;
|
2016-03-02 00:37:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-17 10:23:45 +01:00
|
|
|
|
// Lua: setcpufreq(mhz)
|
|
|
|
|
// mhz is either CPU80MHZ od CPU160MHZ
|
|
|
|
|
static int node_setcpufreq(lua_State* L)
|
|
|
|
|
{
|
|
|
|
|
// http://www.esp8266.com/viewtopic.php?f=21&t=1369
|
|
|
|
|
uint32_t new_freq = luaL_checkinteger(L, 1);
|
|
|
|
|
if (new_freq == CPU160MHZ){
|
|
|
|
|
REG_SET_BIT(0x3ff00014, BIT(0));
|
2015-10-01 07:20:53 +02:00
|
|
|
|
ets_update_cpu_frequency(CPU160MHZ);
|
2015-03-17 10:23:45 +01:00
|
|
|
|
} else {
|
|
|
|
|
REG_CLR_BIT(0x3ff00014, BIT(0));
|
2015-10-01 07:20:53 +02:00
|
|
|
|
ets_update_cpu_frequency(CPU80MHZ);
|
2015-03-17 10:23:45 +01:00
|
|
|
|
}
|
|
|
|
|
new_freq = ets_get_cpu_frequency();
|
|
|
|
|
lua_pushinteger(L, new_freq);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-20 09:38:33 +02:00
|
|
|
|
// Lua: freq = node.getcpufreq()
|
|
|
|
|
static int node_getcpufreq(lua_State* L)
|
|
|
|
|
{
|
|
|
|
|
lua_pushinteger(L, system_get_cpu_freq());
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-15 01:42:20 +01:00
|
|
|
|
// Lua: code, reason [, exccause, epc1, epc2, epc3, excvaddr, depc ] = bootreason()
|
2015-06-27 04:34:03 +02:00
|
|
|
|
static int node_bootreason (lua_State *L)
|
|
|
|
|
{
|
2016-01-15 01:42:20 +01:00
|
|
|
|
const struct rst_info *ri = system_get_rst_info ();
|
|
|
|
|
uint32_t arr[8] = {
|
|
|
|
|
rtc_get_reset_reason(),
|
|
|
|
|
ri->reason,
|
|
|
|
|
ri->exccause, ri->epc1, ri->epc2, ri->epc3, ri->excvaddr, ri->depc
|
|
|
|
|
};
|
|
|
|
|
int i, n = ((ri->reason != REASON_EXCEPTION_RST) ? 2 : 8);
|
|
|
|
|
for (i = 0; i < n; ++i)
|
|
|
|
|
lua_pushinteger (L, arr[i]);
|
|
|
|
|
return n;
|
2015-06-27 04:34:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-03 21:54:22 +02:00
|
|
|
|
// Lua: restore()
|
|
|
|
|
static int node_restore (lua_State *L)
|
|
|
|
|
{
|
|
|
|
|
system_restore();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-09 00:46:08 +01:00
|
|
|
|
/* node.stripdebug([level[, function]]).
|
|
|
|
|
* level: 1 don't discard debug
|
|
|
|
|
* 2 discard Local and Upvalue debug info
|
|
|
|
|
* 3 discard Local, Upvalue and lineno debug info.
|
|
|
|
|
* function: Function to be stripped as per setfenv except 0 not permitted.
|
|
|
|
|
* If no arguments then the current default setting is returned.
|
|
|
|
|
* If function is omitted, this is the default setting for future compiles
|
|
|
|
|
* The function returns an estimated integer count of the bytes stripped.
|
|
|
|
|
*/
|
|
|
|
|
static int node_stripdebug (lua_State *L) {
|
|
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
|
int n = lua_gettop(L);
|
2020-08-22 18:41:02 +02:00
|
|
|
|
int strip = 0;
|
2015-11-09 00:46:08 +01:00
|
|
|
|
|
2020-08-22 18:41:02 +02:00
|
|
|
|
lua_settop(L, 2);
|
|
|
|
|
if (!lua_isnil(L, 1)) {
|
|
|
|
|
strip = lua_tointeger(L, 1);
|
|
|
|
|
luaL_argcheck(L, strip > 0 && strip < 4, 1, "Invalid strip level");
|
|
|
|
|
}
|
2015-11-09 00:46:08 +01:00
|
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
|
if (lua_isnumber(L, 2)) {
|
2020-08-22 18:41:02 +02:00
|
|
|
|
/* Use debug interface to replace stack level by corresponding function */
|
|
|
|
|
int scope = luaL_checkinteger(L, 2);
|
|
|
|
|
if (scope > 0) {
|
2015-11-09 00:46:08 +01:00
|
|
|
|
lua_Debug ar;
|
2020-08-22 18:41:02 +02:00
|
|
|
|
lua_pop(L, 1);
|
2015-11-09 00:46:08 +01:00
|
|
|
|
if (lua_getstack(L, scope, &ar)) {
|
2020-08-22 18:41:02 +02:00
|
|
|
|
lua_getinfo(L, "f", &ar); /* put function at [2] (ToS) */
|
2015-11-09 00:46:08 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-22 18:41:02 +02:00
|
|
|
|
|
|
|
|
|
int isfunc = lua_isfunction(L, 2);
|
|
|
|
|
luaL_argcheck(L, n < 2 || isfunc, 2, "not a valid function");
|
2015-11-09 00:46:08 +01:00
|
|
|
|
|
2020-08-22 18:41:02 +02:00
|
|
|
|
/* return result of lua_stripdebug, adding 1 if this is get/set level) */
|
|
|
|
|
lua_pushinteger(L, lua_stripdebug(L, strip - 1) + (isfunc ? 0 : 1));
|
2015-11-09 00:46:08 +01:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
|
|
|
|
|
|
#if LUA_VERSION_NUM == 501
|
2016-02-20 19:54:09 +01:00
|
|
|
|
// Lua: node.egc.setmode( mode, [param])
|
2016-03-02 00:37:41 +01:00
|
|
|
|
// where the mode is one of the node.egc constants NOT_ACTIVE , ON_ALLOC_FAILURE,
|
2016-02-20 19:54:09 +01:00
|
|
|
|
// ON_MEM_LIMIT, ALWAYS. In the case of ON_MEM_LIMIT an integer parameter is reqired
|
|
|
|
|
// See legc.h and lecg.c.
|
|
|
|
|
static int node_egc_setmode(lua_State* L) {
|
|
|
|
|
unsigned mode = luaL_checkinteger(L, 1);
|
2018-04-06 14:52:03 +02:00
|
|
|
|
int limit = luaL_optinteger (L, 2, 0);
|
2016-03-02 00:37:41 +01:00
|
|
|
|
|
2016-02-20 19:54:09 +01:00
|
|
|
|
luaL_argcheck(L, mode <= (EGC_ON_ALLOC_FAILURE | EGC_ON_MEM_LIMIT | EGC_ALWAYS), 1, "invalid mode");
|
2018-04-06 14:52:03 +02:00
|
|
|
|
luaL_argcheck(L, !(mode & EGC_ON_MEM_LIMIT) || limit!=0, 1, "limit must be non-zero");
|
2016-03-02 00:37:41 +01:00
|
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
|
lua_setegcmode( L, mode, limit );
|
2016-02-20 19:54:09 +01:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
2018-04-06 14:52:03 +02:00
|
|
|
|
// totalallocated, estimatedused = node.egc.meminfo()
|
|
|
|
|
static int node_egc_meminfo(lua_State *L) {
|
2020-08-22 18:41:02 +02:00
|
|
|
|
int totals[2];
|
|
|
|
|
lua_getegcinfo(L, totals);
|
|
|
|
|
lua_pushinteger(L, totals[0]);
|
|
|
|
|
lua_pushinteger(L, totals[1]);
|
2018-04-06 14:52:03 +02:00
|
|
|
|
return 2;
|
|
|
|
|
}
|
2020-04-27 02:13:38 +02:00
|
|
|
|
#endif
|
2016-03-20 17:54:16 +01:00
|
|
|
|
//
|
|
|
|
|
// Lua: osprint(true/false)
|
|
|
|
|
// Allows you to turn on the native Espressif SDK printing
|
|
|
|
|
static int node_osprint( lua_State* L )
|
|
|
|
|
{
|
|
|
|
|
if (lua_toboolean(L, 1)) {
|
|
|
|
|
system_set_os_print(1);
|
|
|
|
|
} else {
|
|
|
|
|
system_set_os_print(0);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-17 19:26:29 +01:00
|
|
|
|
return 0;
|
2016-03-20 17:54:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-29 18:48:24 +02:00
|
|
|
|
static lua_Unsigned random_value() {
|
|
|
|
|
// Hopefully the compiler is smart enought to spot the constant IF check
|
|
|
|
|
if (sizeof(lua_Unsigned) == 4) {
|
|
|
|
|
return os_random();
|
|
|
|
|
} else {
|
|
|
|
|
return (((uint64_t) os_random()) << 32) + (uint32_t) os_random();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lua_Integer node_random_range(lua_Integer l, lua_Integer u) {
|
2016-12-26 14:14:49 +01:00
|
|
|
|
// The range is the number of different values to return
|
2020-08-29 18:48:24 +02:00
|
|
|
|
lua_Unsigned range = u + 1 - l;
|
2016-12-26 14:14:49 +01:00
|
|
|
|
|
|
|
|
|
// If this is very large then use simpler code
|
2020-08-29 18:48:24 +02:00
|
|
|
|
if (range >= LUA_MAXINTEGER) {
|
|
|
|
|
uint64_t v;
|
2016-12-26 14:14:49 +01:00
|
|
|
|
|
|
|
|
|
// This cannot loop more than half the time
|
2020-08-29 18:48:24 +02:00
|
|
|
|
while ((v = random_value()) >= range) {
|
2016-12-26 14:14:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Now v is in the range [0, range)
|
|
|
|
|
return v + l;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Easy case, with only one value, we know the result
|
|
|
|
|
if (range == 1) {
|
|
|
|
|
return l;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-29 18:48:24 +02:00
|
|
|
|
// Another easy case -- uniform 32/64-bit
|
2016-12-26 14:14:49 +01:00
|
|
|
|
if (range == 0) {
|
2020-08-29 18:48:24 +02:00
|
|
|
|
return random_value();
|
2016-12-26 14:14:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Now we have to figure out what a large multiple of range is
|
2020-08-29 18:48:24 +02:00
|
|
|
|
// that just fits into 32/64 bits.
|
2016-12-26 14:14:49 +01:00
|
|
|
|
// The limit will be less than 1 << 32 by some amount (not much)
|
2020-08-29 18:48:24 +02:00
|
|
|
|
lua_Unsigned limit = (((1 + (lua_Unsigned) LUA_MAXINTEGER) / ((range + 1) >> 1)) - 1) * range;
|
2016-12-26 14:14:49 +01:00
|
|
|
|
|
2020-08-29 18:48:24 +02:00
|
|
|
|
lua_Unsigned v;
|
2016-12-26 14:14:49 +01:00
|
|
|
|
|
2020-08-29 18:48:24 +02:00
|
|
|
|
while ((v = random_value()) >= limit) {
|
2016-12-26 14:14:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Now v is uniformly distributed in [0, limit) and limit is a multiple of range
|
|
|
|
|
|
|
|
|
|
return (v % range) + l;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int node_random (lua_State *L) {
|
2020-08-29 18:48:24 +02:00
|
|
|
|
lua_Integer u;
|
|
|
|
|
lua_Integer l;
|
2016-12-26 14:14:49 +01:00
|
|
|
|
|
|
|
|
|
switch (lua_gettop(L)) { /* check number of arguments */
|
|
|
|
|
case 0: { /* no arguments */
|
|
|
|
|
#ifdef LUA_NUMBER_INTEGRAL
|
2020-08-29 18:48:24 +02:00
|
|
|
|
lua_pushinteger(L, 0); /* Number between 0 and 1 - always 0 with ints */
|
2016-12-26 14:14:49 +01:00
|
|
|
|
#else
|
2020-08-29 18:48:24 +02:00
|
|
|
|
lua_pushnumber(L, ((double)random_value() / 16 / (1LL << (8 * sizeof(lua_Unsigned) - 4))));
|
2016-12-26 14:14:49 +01:00
|
|
|
|
#endif
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
case 1: { /* only upper limit */
|
|
|
|
|
l = 1;
|
2020-08-29 18:48:24 +02:00
|
|
|
|
u = luaL_checkinteger(L, 1);
|
2016-12-26 14:14:49 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 2: { /* lower and upper limits */
|
2020-08-29 18:48:24 +02:00
|
|
|
|
l = luaL_checkinteger(L, 1);
|
|
|
|
|
u = luaL_checkinteger(L, 2);
|
2016-12-26 14:14:49 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
2019-02-17 19:26:29 +01:00
|
|
|
|
default:
|
2016-12-26 14:14:49 +01:00
|
|
|
|
return luaL_error(L, "wrong number of arguments");
|
|
|
|
|
}
|
|
|
|
|
luaL_argcheck(L, l<=u, 2, "interval is empty");
|
2020-08-29 18:48:24 +02:00
|
|
|
|
lua_pushinteger(L, node_random_range(l, u)); /* int between `l' and `u' */
|
2016-12-26 14:14:49 +01:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-18 20:46:47 +02:00
|
|
|
|
// Just return the startup as an array of tables
|
|
|
|
|
static int node_startup_counts(lua_State *L) {
|
|
|
|
|
// If the first argument is a number, then add an entry for that line
|
|
|
|
|
if (lua_isnumber(L, 1)) {
|
|
|
|
|
int lineno = lua_tointeger(L, 1);
|
|
|
|
|
STARTUP_ENTRY(lineno);
|
|
|
|
|
}
|
|
|
|
|
lua_createtable(L, platform_startup_counts.used, 0);
|
|
|
|
|
for (int i = 0; i < platform_startup_counts.used; i++) {
|
|
|
|
|
const platform_count_entry_t *p = &platform_startup_counts.entries[i];
|
|
|
|
|
|
|
|
|
|
lua_createtable(L, 0, 3);
|
|
|
|
|
lua_pushstring(L, p->name);
|
|
|
|
|
lua_setfield(L, -2, "name");
|
|
|
|
|
lua_pushinteger(L, p->line);
|
|
|
|
|
lua_setfield(L, -2, "line");
|
|
|
|
|
lua_pushinteger(L, p->ccount);
|
|
|
|
|
lua_setfield(L, -2, "ccount");
|
|
|
|
|
|
|
|
|
|
lua_rawseti(L, -2, i + 1);
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-01 19:29:11 +02:00
|
|
|
|
#ifdef DEVELOPMENT_TOOLS
|
|
|
|
|
// Lua: rec = node.readrcr(id)
|
|
|
|
|
static int node_readrcr (lua_State *L) {
|
|
|
|
|
int id = luaL_checkinteger(L, 1);
|
|
|
|
|
char *data;
|
|
|
|
|
int n = platform_rcr_read(id, (void **)&data);
|
|
|
|
|
if (n == ~0) return 0;
|
|
|
|
|
lua_pushlstring(L, data, n);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
// Lua: n = node.writercr(id,rec)
|
|
|
|
|
static int node_writercr (lua_State *L) {
|
|
|
|
|
int id = luaL_checkinteger(L, 1),l;
|
|
|
|
|
const char *data = lua_tolstring(L, 2, &l);
|
|
|
|
|
int n = platform_rcr_write(id, data, l);
|
|
|
|
|
lua_pushinteger(L, n);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2020-09-20 16:57:09 +02:00
|
|
|
|
|
2020-09-26 01:18:01 +02:00
|
|
|
|
#if LUA_VERSION_NUM > 501
|
2020-09-20 16:57:09 +02:00
|
|
|
|
static int node_int2float(lua_State *L) {
|
|
|
|
|
union {
|
|
|
|
|
lua_Integer i;
|
|
|
|
|
lua_Float f;
|
|
|
|
|
} u;
|
|
|
|
|
|
|
|
|
|
u.i = luaL_checkinteger(L, 1);
|
|
|
|
|
lua_pushnumber(L, u.f);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int node_float2int(lua_State *L) {
|
|
|
|
|
union {
|
|
|
|
|
lua_Integer i;
|
|
|
|
|
lua_Float f;
|
|
|
|
|
} u;
|
|
|
|
|
|
|
|
|
|
u.f = luaL_checknumber(L, 1);
|
|
|
|
|
lua_pushinteger(L, u.i);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2019-05-01 19:29:11 +02:00
|
|
|
|
#endif
|
2020-09-26 01:18:01 +02:00
|
|
|
|
#endif
|
2019-05-01 19:29:11 +02:00
|
|
|
|
|
2020-08-22 18:41:02 +02:00
|
|
|
|
// Lua: n = node.LFS.reload(lfsimage)
|
|
|
|
|
static int node_lfsreload (lua_State *L) {
|
|
|
|
|
lua_settop(L, 1);
|
|
|
|
|
luaL_lfsreload(L);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 12:17:12 +02:00
|
|
|
|
// Lua: n = node.flashreload(lfsimage)
|
|
|
|
|
static int lua_lfsreload_deprecated (lua_State *L) {
|
|
|
|
|
platform_print_deprecation_note("node.flashreload", "soon. Use node.LFS interface instead");
|
|
|
|
|
return node_lfsreload (L);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-22 18:41:02 +02:00
|
|
|
|
// Lua: n = node.flashindex(module)
|
|
|
|
|
// Lua: n = node.LFS.get(module)
|
|
|
|
|
static int node_lfsindex (lua_State *L) {
|
|
|
|
|
lua_settop(L, 1);
|
|
|
|
|
luaL_pushlfsmodule(L);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Lua: n = node.LFS.list([option])
|
|
|
|
|
// Note that option is ignored in this release
|
|
|
|
|
static int node_lfslist (lua_State *L) {
|
|
|
|
|
lua_settop(L, 1);
|
|
|
|
|
luaL_pushlfsmodules(L);
|
|
|
|
|
if (lua_istable(L, -1) && lua_getglobal(L, "table") == LUA_TTABLE) {
|
|
|
|
|
lua_getfield(L, -1, "sort");
|
|
|
|
|
lua_remove(L, -2); /* remove table table */
|
|
|
|
|
lua_pushvalue(L, -2); /* dup array of modules ref to ToS */
|
|
|
|
|
lua_call(L, 1, 0);
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//== node.LFS Table emulator ==============================================//
|
|
|
|
|
|
|
|
|
|
static int node_lfs_func (lua_State* L) { /*T[1] = LFS, T[2] = fieldname */
|
|
|
|
|
lua_remove(L, 1);
|
|
|
|
|
lua_settop(L, 1);
|
|
|
|
|
const char *name = lua_tostring(L, 1);
|
|
|
|
|
if (!name) {
|
|
|
|
|
lua_pushnil(L);
|
|
|
|
|
} else if (!strcmp(name, "config")) {
|
|
|
|
|
get_lfs_config(L);
|
|
|
|
|
} else if (!strcmp(name, "time")) {
|
|
|
|
|
luaL_pushlfsdts(L);
|
|
|
|
|
} else {
|
|
|
|
|
luaL_pushlfsmodule(L);
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LROT_BEGIN(node_lfs_meta, NULL, LROT_MASK_INDEX)
|
|
|
|
|
LROT_FUNCENTRY( __index, node_lfs_func)
|
|
|
|
|
LROT_END(node_lfs_meta, NULL, LROT_MASK_INDEX)
|
|
|
|
|
|
|
|
|
|
LROT_BEGIN(node_lfs, LROT_TABLEREF(node_lfs_meta), 0)
|
|
|
|
|
LROT_FUNCENTRY( list, node_lfslist)
|
|
|
|
|
LROT_FUNCENTRY( get, node_lfsindex)
|
|
|
|
|
LROT_FUNCENTRY( reload, node_lfsreload )
|
|
|
|
|
LROT_END(node_lfs, LROT_TABLEREF(node_lfs_meta), 0)
|
|
|
|
|
|
|
|
|
|
|
2019-05-01 19:29:11 +02:00
|
|
|
|
typedef enum pt_t { lfs_addr=0, lfs_size, spiffs_addr, spiffs_size, max_pt} pt_t;
|
|
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
|
LROT_BEGIN(pt_map, NULL, 0)
|
2019-05-08 13:08:20 +02:00
|
|
|
|
LROT_NUMENTRY( lfs_addr, lfs_addr )
|
|
|
|
|
LROT_NUMENTRY( lfs_size, lfs_size )
|
|
|
|
|
LROT_NUMENTRY( spiffs_addr, spiffs_addr )
|
|
|
|
|
LROT_NUMENTRY( spiffs_size, spiffs_size )
|
2020-04-27 02:13:38 +02:00
|
|
|
|
LROT_END(pt_map, NULL, 0)
|
2019-05-08 13:08:20 +02:00
|
|
|
|
|
2019-05-01 19:29:11 +02:00
|
|
|
|
|
|
|
|
|
// Lua: ptinfo = node.getpartitiontable()
|
|
|
|
|
static int node_getpartitiontable (lua_State *L) {
|
|
|
|
|
uint32_t param[max_pt] = {0};
|
|
|
|
|
param[lfs_size] = platform_flash_get_partition(NODEMCU_LFS0_PARTITION, param + lfs_addr);
|
|
|
|
|
param[spiffs_size] = platform_flash_get_partition(NODEMCU_SPIFFS0_PARTITION, param + spiffs_addr);
|
|
|
|
|
|
|
|
|
|
lua_settop(L, 0);
|
|
|
|
|
lua_createtable (L, 0, max_pt); /* at index 1 */
|
2020-04-27 02:13:38 +02:00
|
|
|
|
lua_pushrotable(L, LROT_TABLEREF(pt_map)); /* at index 2 */
|
2019-05-01 19:29:11 +02:00
|
|
|
|
lua_pushnil(L); /* first key at index 3 */
|
|
|
|
|
while (lua_next(L, 2) != 0) { /* key at index 3, and v at index 4 */
|
|
|
|
|
lua_pushvalue(L, 3); /* dup key to index 5 */
|
|
|
|
|
lua_pushinteger(L, param[lua_tointeger(L, 4)]); /* param [v] at index 6 */
|
|
|
|
|
lua_rawset(L, 1);
|
|
|
|
|
lua_pop(L, 1); /* discard v */
|
|
|
|
|
}
|
|
|
|
|
lua_pop(L, 1); /* discard pt_map reference */
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void insert_partition(partition_item_t *p, int n, uint32_t type, uint32_t addr) {
|
|
|
|
|
if (n>0)
|
2019-07-21 23:58:21 +02:00
|
|
|
|
memmove(p+1, p, n*sizeof(partition_item_t)); /* overlapped so must be move not cpy */
|
2019-05-01 19:29:11 +02:00
|
|
|
|
p->type = type;
|
|
|
|
|
p->addr = addr;
|
|
|
|
|
p->size = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void delete_partition(partition_item_t *p, int n) {
|
|
|
|
|
if (n>0)
|
2019-07-21 23:58:21 +02:00
|
|
|
|
memmove(p, p+1, n*sizeof(partition_item_t)); /* overlapped so must be move not cpy */
|
2019-05-01 19:29:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define SKIP (~0)
|
|
|
|
|
#define IROM0_PARTITION (SYSTEM_PARTITION_CUSTOMER_BEGIN + NODEMCU_IROM0TEXT_PARTITION)
|
|
|
|
|
#define LFS_PARTITION (SYSTEM_PARTITION_CUSTOMER_BEGIN + NODEMCU_LFS0_PARTITION)
|
|
|
|
|
#define SPIFFS_PARTITION (SYSTEM_PARTITION_CUSTOMER_BEGIN + NODEMCU_SPIFFS0_PARTITION)
|
2020-09-01 23:40:57 +02:00
|
|
|
|
#define SYSTEM_PARAMETER_SIZE 0x3000
|
2019-05-01 19:29:11 +02:00
|
|
|
|
|
2020-09-01 23:40:57 +02:00
|
|
|
|
// Lua: node.setpartitiontable(ptvals)
|
2019-05-01 19:29:11 +02:00
|
|
|
|
static int node_setpartitiontable (lua_State *L) {
|
|
|
|
|
partition_item_t *rcr_pt = NULL, *pt;
|
|
|
|
|
uint32_t flash_size = flash_rom_get_size_byte();
|
|
|
|
|
uint32_t i = platform_rcr_read(PLATFORM_RCR_PT, (void **) &rcr_pt);
|
|
|
|
|
uint32_t last = 0;
|
|
|
|
|
uint32_t n = i / sizeof(partition_item_t);
|
|
|
|
|
uint32_t param[max_pt] = {SKIP, SKIP, SKIP, SKIP};
|
|
|
|
|
|
2020-09-01 23:40:57 +02:00
|
|
|
|
/* stack 1=ptvals, 2=pt_map, 3=key, 4=ptval[key], 5=pt_map[key] */
|
2019-05-01 19:29:11 +02:00
|
|
|
|
luaL_argcheck(L, lua_istable(L, 1), 1, "must be table");
|
|
|
|
|
lua_settop(L, 1);
|
|
|
|
|
/* convert input table into 4 option array */
|
2020-04-27 02:13:38 +02:00
|
|
|
|
lua_pushrotable(L, LROT_TABLEREF(pt_map)); /* at index 2 */
|
|
|
|
|
lua_pushnil(L); /* first key at index 3 */
|
2019-05-01 19:29:11 +02:00
|
|
|
|
while (lua_next(L, 1) != 0) {
|
|
|
|
|
/* 'key' (at index 3) and 'value' (at index 4) */
|
|
|
|
|
luaL_argcheck(L, lua_isstring(L, 3) && lua_isnumber(L, 4), 1, "invalid partition setting");
|
|
|
|
|
lua_pushvalue(L, 3); /* dup key to index 5 */
|
|
|
|
|
lua_rawget(L, 2); /* lookup in pt_map */
|
|
|
|
|
luaL_argcheck(L, !lua_isnil(L, -1), 1, "invalid partition setting");
|
2020-09-01 23:40:57 +02:00
|
|
|
|
param[lua_tointeger(L, 5)] = lua_tounsigned(L, 4);
|
|
|
|
|
lua_pop(L, 2); /* discard value and lookup; keeps 'key' for next iteration */
|
2019-05-01 19:29:11 +02:00
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* Allocate a scratch Partition Table as userdata on the Lua stack, and copy the
|
|
|
|
|
* current Flash PT into this for manipulation
|
|
|
|
|
*/
|
|
|
|
|
lua_newuserdata(L, (n+2)*sizeof(partition_item_t));
|
|
|
|
|
pt = lua_touserdata (L, -1);
|
2019-07-21 23:58:21 +02:00
|
|
|
|
memcpy(pt, rcr_pt, n*sizeof(partition_item_t));
|
2019-05-01 19:29:11 +02:00
|
|
|
|
pt[n].type = 0; pt[n+1].type = 0;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < n; i ++) {
|
|
|
|
|
partition_item_t *p = pt + i;
|
|
|
|
|
|
|
|
|
|
if (p->type == IROM0_PARTITION && p[1].type != LFS_PARTITION) {
|
|
|
|
|
// if the LFS partition is not following IROM0 then slot a blank one in
|
|
|
|
|
insert_partition(p + 1, n-i-1, LFS_PARTITION, p->addr + p->size);
|
|
|
|
|
n++;
|
|
|
|
|
|
|
|
|
|
} else if (p->type == LFS_PARTITION) {
|
2020-09-01 23:40:57 +02:00
|
|
|
|
// update the LFS options if set
|
|
|
|
|
if (param[lfs_addr] != SKIP)
|
|
|
|
|
p->addr = param[lfs_addr];
|
|
|
|
|
if (param[lfs_size] != SKIP)
|
|
|
|
|
p->size = param[lfs_size];
|
2019-05-01 19:29:11 +02:00
|
|
|
|
if (p[1].type != SPIFFS_PARTITION) {
|
|
|
|
|
// if the SPIFFS partition is not following LFS then slot a blank one in
|
|
|
|
|
insert_partition(p + 1, n-i-1, SPIFFS_PARTITION, 0);
|
|
|
|
|
n++;
|
|
|
|
|
}
|
2020-09-01 23:40:57 +02:00
|
|
|
|
|
2019-05-01 19:29:11 +02:00
|
|
|
|
} else if (p->type == SPIFFS_PARTITION) {
|
|
|
|
|
// update the SPIFFS options if set
|
2020-09-01 23:40:57 +02:00
|
|
|
|
if (param[spiffs_size] != SKIP) {
|
|
|
|
|
p->size = param[spiffs_size];
|
|
|
|
|
p->addr = (param[spiffs_addr] != SKIP) ? param[spiffs_addr] :
|
|
|
|
|
((p->size <= flash_size - SYSTEM_PARAMETER_SIZE - 0x100000)
|
|
|
|
|
? 0x100000 : last);
|
|
|
|
|
} else if (param[spiffs_addr] != SKIP) {
|
2019-05-01 19:29:11 +02:00
|
|
|
|
p->addr = param[spiffs_addr];
|
|
|
|
|
}
|
2020-09-01 23:40:57 +02:00
|
|
|
|
#if 0
|
2019-05-01 19:29:11 +02:00
|
|
|
|
if (param[spiffs_size] != SKIP) {
|
|
|
|
|
// BOTCH: - at the moment the firmware doesn't boot if the SPIFFS partition
|
|
|
|
|
// is deleted so the minimum SPIFFS size is 64Kb
|
|
|
|
|
p->size = param[spiffs_size] > 0x10000 ? param[spiffs_size] : 0x10000;
|
|
|
|
|
}
|
2020-09-01 23:40:57 +02:00
|
|
|
|
#endif
|
2019-05-01 19:29:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (p->size == 0) {
|
|
|
|
|
// Delete 0-sized partitions as the SDK barfs on these
|
|
|
|
|
delete_partition(p, n-i-1);
|
|
|
|
|
n--; i--;
|
|
|
|
|
} else {
|
|
|
|
|
// Do consistency tests on the partition
|
|
|
|
|
if (p->addr & (INTERNAL_FLASH_SECTOR_SIZE - 1) ||
|
|
|
|
|
p->size & (INTERNAL_FLASH_SECTOR_SIZE - 1) ||
|
|
|
|
|
p->addr < last ||
|
|
|
|
|
p->addr + p->size > flash_size) {
|
2020-09-01 23:40:57 +02:00
|
|
|
|
luaL_error(L, "Partition value out of range");
|
2019-05-01 19:29:11 +02:00
|
|
|
|
}
|
2020-09-01 23:40:57 +02:00
|
|
|
|
last = p->addr + p->size;
|
2019-05-01 19:29:11 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-01 23:40:57 +02:00
|
|
|
|
for (i = 0; i < n; i ++)
|
|
|
|
|
dbg_printf("Partition %d: %04x %06x %06x\n", i, pt[i].type, pt[i].addr, pt[i].size);
|
|
|
|
|
|
|
|
|
|
platform_rcr_write(PLATFORM_RCR_PT, pt, n*sizeof(partition_item_t));
|
|
|
|
|
while(1); // Trigger WDT; the new PT will be loaded on reboot
|
2019-05-01 19:29:11 +02:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-26 14:14:49 +01:00
|
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
|
// Module function map
|
2020-04-27 02:13:38 +02:00
|
|
|
|
#if LUA_VERSION_NUM == 501
|
|
|
|
|
LROT_BEGIN(node_egc, NULL, 0)
|
2019-05-08 13:08:20 +02:00
|
|
|
|
LROT_FUNCENTRY( meminfo, node_egc_meminfo )
|
|
|
|
|
LROT_FUNCENTRY( setmode, node_egc_setmode )
|
|
|
|
|
LROT_NUMENTRY( NOT_ACTIVE, EGC_NOT_ACTIVE )
|
|
|
|
|
LROT_NUMENTRY( ON_ALLOC_FAILURE, EGC_ON_ALLOC_FAILURE )
|
|
|
|
|
LROT_NUMENTRY( ON_MEM_LIMIT, EGC_ON_MEM_LIMIT )
|
|
|
|
|
LROT_NUMENTRY( ALWAYS, EGC_ALWAYS )
|
2020-04-27 02:13:38 +02:00
|
|
|
|
LROT_END(node_egc, NULL, 0)
|
|
|
|
|
#endif
|
2019-05-08 13:08:20 +02:00
|
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
|
LROT_BEGIN(node_task, NULL, 0)
|
2019-05-08 13:08:20 +02:00
|
|
|
|
LROT_FUNCENTRY( post, node_task_post )
|
|
|
|
|
LROT_NUMENTRY( LOW_PRIORITY, TASK_PRIORITY_LOW )
|
|
|
|
|
LROT_NUMENTRY( MEDIUM_PRIORITY, TASK_PRIORITY_MEDIUM )
|
|
|
|
|
LROT_NUMENTRY( HIGH_PRIORITY, TASK_PRIORITY_HIGH )
|
2020-04-27 02:13:38 +02:00
|
|
|
|
LROT_END(node_task, NULL, 0)
|
2019-05-08 13:08:20 +02:00
|
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
|
LROT_BEGIN(node, NULL, 0)
|
2019-05-08 13:08:20 +02:00
|
|
|
|
LROT_FUNCENTRY( heap, node_heap )
|
|
|
|
|
LROT_FUNCENTRY( info, node_info )
|
|
|
|
|
LROT_TABENTRY( task, node_task )
|
2020-09-08 12:17:12 +02:00
|
|
|
|
LROT_FUNCENTRY( flashreload, lua_lfsreload_deprecated )
|
2020-08-22 18:41:02 +02:00
|
|
|
|
LROT_FUNCENTRY( flashindex, node_lfsindex )
|
|
|
|
|
LROT_TABENTRY( LFS, node_lfs )
|
2020-06-16 09:19:55 +02:00
|
|
|
|
LROT_FUNCENTRY( setonerror, node_setonerror )
|
2020-04-27 02:13:38 +02:00
|
|
|
|
LROT_FUNCENTRY( startupcommand, node_startupcommand )
|
2020-10-18 20:46:47 +02:00
|
|
|
|
LROT_FUNCENTRY( startup, node_startup )
|
2019-05-08 13:08:20 +02:00
|
|
|
|
LROT_FUNCENTRY( restart, node_restart )
|
|
|
|
|
LROT_FUNCENTRY( dsleep, node_deepsleep )
|
|
|
|
|
LROT_FUNCENTRY( dsleepMax, dsleepMax )
|
|
|
|
|
LROT_FUNCENTRY( sleep, node_sleep )
|
2018-05-08 22:43:12 +02:00
|
|
|
|
#ifdef PMSLEEP_ENABLE
|
2019-05-17 14:04:19 +02:00
|
|
|
|
PMSLEEP_INT_MAP
|
2019-05-01 19:29:11 +02:00
|
|
|
|
#endif
|
|
|
|
|
#ifdef DEVELOPMENT_TOOLS
|
2019-05-08 13:08:20 +02:00
|
|
|
|
LROT_FUNCENTRY( readrcr, node_readrcr )
|
|
|
|
|
LROT_FUNCENTRY( writercr, node_writercr )
|
2020-10-18 20:46:47 +02:00
|
|
|
|
#endif
|
|
|
|
|
#ifdef PLATFORM_STARTUP_COUNT
|
|
|
|
|
LROT_FUNCENTRY( startupcounts, node_startup_counts )
|
2017-04-04 21:31:06 +02:00
|
|
|
|
#endif
|
2019-05-08 13:08:20 +02:00
|
|
|
|
LROT_FUNCENTRY( chipid, node_chipid )
|
|
|
|
|
LROT_FUNCENTRY( flashid, node_flashid )
|
|
|
|
|
LROT_FUNCENTRY( flashsize, node_flashsize )
|
|
|
|
|
LROT_FUNCENTRY( input, node_input )
|
|
|
|
|
LROT_FUNCENTRY( output, node_output )
|
2015-11-09 00:46:08 +01:00
|
|
|
|
// Moved to adc module, use adc.readvdd33()
|
2019-05-08 13:08:20 +02:00
|
|
|
|
// LROT_FUNCENTRY( readvdd33, node_readvdd33 )
|
|
|
|
|
LROT_FUNCENTRY( compile, node_compile )
|
|
|
|
|
LROT_NUMENTRY( CPU80MHZ, CPU80MHZ )
|
|
|
|
|
LROT_NUMENTRY( CPU160MHZ, CPU160MHZ )
|
|
|
|
|
LROT_FUNCENTRY( setcpufreq, node_setcpufreq )
|
|
|
|
|
LROT_FUNCENTRY( getcpufreq, node_getcpufreq )
|
|
|
|
|
LROT_FUNCENTRY( bootreason, node_bootreason )
|
|
|
|
|
LROT_FUNCENTRY( restore, node_restore )
|
|
|
|
|
LROT_FUNCENTRY( random, node_random )
|
|
|
|
|
LROT_FUNCENTRY( stripdebug, node_stripdebug )
|
2020-04-27 02:13:38 +02:00
|
|
|
|
#if LUA_VERSION_NUM == 501
|
2019-05-08 13:08:20 +02:00
|
|
|
|
LROT_TABENTRY( egc, node_egc )
|
2020-04-27 02:13:38 +02:00
|
|
|
|
#endif
|
2016-03-20 17:54:16 +01:00
|
|
|
|
#ifdef DEVELOPMENT_TOOLS
|
2019-05-08 13:08:20 +02:00
|
|
|
|
LROT_FUNCENTRY( osprint, node_osprint )
|
2020-09-26 01:18:01 +02:00
|
|
|
|
#if LUA_VERSION_NUM > 501
|
2020-09-20 16:57:09 +02:00
|
|
|
|
LROT_FUNCENTRY( int2float, node_int2float )
|
|
|
|
|
LROT_FUNCENTRY( float2int, node_float2int )
|
2020-09-26 01:18:01 +02:00
|
|
|
|
#endif
|
2016-03-20 17:54:16 +01:00
|
|
|
|
#endif
|
2019-05-08 13:08:20 +02:00
|
|
|
|
LROT_FUNCENTRY( getpartitiontable, node_getpartitiontable )
|
|
|
|
|
LROT_FUNCENTRY( setpartitiontable, node_setpartitiontable )
|
2015-11-09 00:46:08 +01:00
|
|
|
|
|
2015-03-15 22:40:43 +01:00
|
|
|
|
// Combined to dsleep(us, option)
|
2019-05-08 13:08:20 +02:00
|
|
|
|
// LROT_FUNCENTRY( dsleepsetoption, node_deepsleep_setoption )
|
2020-04-27 02:13:38 +02:00
|
|
|
|
LROT_END(node, NULL, 0)
|
2019-05-08 13:08:20 +02:00
|
|
|
|
|
2020-06-16 09:19:55 +02:00
|
|
|
|
int luaopen_node( lua_State *L ) {
|
|
|
|
|
lua_settop(L, 0);
|
|
|
|
|
return node_setonerror(L); /* set default onerror action */
|
|
|
|
|
}
|
2014-12-22 12:35:05 +01:00
|
|
|
|
|
2020-06-16 09:19:55 +02:00
|
|
|
|
NODEMCU_MODULE(NODE, "node", node, luaopen_node);
|