node.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. // Module for interfacing with system
  2. #include "module.h"
  3. #include "lauxlib.h"
  4. #include "ldebug.h"
  5. #include "ldo.h"
  6. #include "lfunc.h"
  7. #include "lmem.h"
  8. #include "lobject.h"
  9. #include "lstate.h"
  10. #include "legc.h"
  11. #include "lopcodes.h"
  12. #include "lstring.h"
  13. #include "lundump.h"
  14. #include "platform.h"
  15. #include "lflash.h"
  16. #include <stdint.h>
  17. #include <string.h>
  18. #include "driver/uart.h"
  19. #include "user_interface.h"
  20. #include "flash_api.h"
  21. #include "vfs.h"
  22. #include "user_version.h"
  23. #include "rom.h"
  24. #include "task/task.h"
  25. #define CPU80MHZ 80
  26. #define CPU160MHZ 160
  27. // Lua: restart()
  28. static int node_restart( lua_State* L )
  29. {
  30. system_restart();
  31. return 0;
  32. }
  33. static int dsleepMax( lua_State *L ) {
  34. lua_pushnumber(L, (uint64_t)system_rtc_clock_cali_proc()*(0x80000000-1)/(0x1000));
  35. return 1;
  36. }
  37. // Lua: dsleep( us, option )
  38. static int node_deepsleep( lua_State* L )
  39. {
  40. uint64 us;
  41. uint8 option;
  42. //us = luaL_checkinteger( L, 1 );
  43. // Set deleep option, skip if nil
  44. if ( lua_isnumber(L, 2) )
  45. {
  46. option = lua_tointeger(L, 2);
  47. if ( option < 0 || option > 4)
  48. return luaL_error( L, "wrong arg range" );
  49. else
  50. system_deep_sleep_set_option( option );
  51. }
  52. bool instant = false;
  53. if (lua_isnumber(L, 3))
  54. instant = lua_tointeger(L, 3);
  55. // Set deleep time, skip if nil
  56. if ( lua_isnumber(L, 1) )
  57. {
  58. us = luaL_checknumber(L, 1);
  59. // if ( us <= 0 )
  60. if ( us < 0 )
  61. return luaL_error( L, "wrong arg range" );
  62. else
  63. {
  64. if (instant)
  65. system_deep_sleep_instant(us);
  66. else
  67. system_deep_sleep( us );
  68. }
  69. }
  70. return 0;
  71. }
  72. #ifdef PMSLEEP_ENABLE
  73. #include "pm/pmSleep.h"
  74. int node_sleep_resume_cb_ref= LUA_NOREF;
  75. void node_sleep_resume_cb(void)
  76. {
  77. PMSLEEP_DBG("START");
  78. pmSleep_execute_lua_cb(&node_sleep_resume_cb_ref);
  79. PMSLEEP_DBG("END");
  80. }
  81. // Lua: node.sleep(table)
  82. static int node_sleep( lua_State* L )
  83. {
  84. #ifdef TIMER_SUSPEND_ENABLE
  85. pmSleep_INIT_CFG(cfg);
  86. cfg.sleep_mode=LIGHT_SLEEP_T;
  87. if(lua_istable(L, 1)){
  88. pmSleep_parse_table_lua(L, 1, &cfg, NULL, &node_sleep_resume_cb_ref);
  89. }
  90. else{
  91. return luaL_argerror(L, 1, "must be table");
  92. }
  93. cfg.resume_cb_ptr = &node_sleep_resume_cb;
  94. pmSleep_suspend(&cfg);
  95. #else
  96. dbg_printf("\n The option \"TIMER_SUSPEND_ENABLE\" in \"app/include/user_config.h\" was disabled during FW build!\n");
  97. return luaL_error(L, "node.sleep() is unavailable");
  98. #endif
  99. return 0;
  100. }
  101. #else
  102. static int node_sleep( lua_State* L )
  103. {
  104. dbg_printf("\n The options \"TIMER_SUSPEND_ENABLE\" and \"PMSLEEP_ENABLE\" in \"app/include/user_config.h\" were disabled during FW build!\n");
  105. return luaL_error(L, "node.sleep() is unavailable");
  106. }
  107. #endif //PMSLEEP_ENABLE
  108. static int node_info( lua_State* L )
  109. {
  110. const char* options[] = {"hw", "sw_version", "build_config", "legacy", NULL};
  111. int option = luaL_checkoption (L, 1, options[3], options);
  112. switch (option) {
  113. case 0: { // hw
  114. lua_createtable (L, 0, 5);
  115. int table_index = lua_gettop(L);
  116. lua_pushinteger(L, system_get_chip_id()); // chip id
  117. lua_setfield(L, table_index, "chip_id");
  118. lua_pushinteger(L, spi_flash_get_id()); // flash id
  119. lua_setfield(L, table_index, "flash_id");
  120. lua_pushinteger(L, flash_rom_get_size_byte() / 1024); // flash size in KB
  121. lua_setfield(L, table_index, "flash_size");
  122. lua_pushinteger(L, flash_rom_get_mode());
  123. lua_setfield(L, table_index, "flash_mode");
  124. lua_pushinteger(L, flash_rom_get_speed());
  125. lua_setfield(L, table_index, "flash_speed");
  126. return 1;
  127. }
  128. case 1: { // sw_version
  129. lua_createtable (L, 0, 7);
  130. int table_index = lua_gettop(L);
  131. lua_pushinteger(L, NODE_VERSION_MAJOR);
  132. lua_setfield(L, table_index, "node_version_major");
  133. lua_pushinteger(L, NODE_VERSION_MINOR);
  134. lua_setfield(L, table_index, "node_version_minor");
  135. lua_pushinteger(L, NODE_VERSION_REVISION);
  136. lua_setfield(L, table_index, "node_version_revision");
  137. lua_pushstring(L, BUILDINFO_BRANCH);
  138. lua_setfield(L, table_index, "git_branch");
  139. lua_pushstring(L, BUILDINFO_COMMIT_ID);
  140. lua_setfield(L, table_index, "git_commit_id");
  141. lua_pushstring(L, BUILDINFO_RELEASE);
  142. lua_setfield(L, table_index, "git_release");
  143. lua_pushstring(L, BUILDINFO_RELEASE_DTS);
  144. lua_setfield(L, table_index, "git_commit_dts");
  145. return 1;
  146. }
  147. case 2: { // build_config
  148. lua_createtable (L, 0, 4);
  149. int table_index = lua_gettop(L);
  150. lua_pushboolean(L, BUILDINFO_SSL);
  151. lua_setfield(L, table_index, "ssl");
  152. lua_pushnumber(L, BUILDINFO_LFS);
  153. lua_setfield(L, table_index, "lfs_size");
  154. lua_pushstring(L, BUILDINFO_MODULES);
  155. lua_setfield(L, table_index, "modules");
  156. lua_pushstring(L, BUILDINFO_BUILD_TYPE);
  157. lua_setfield(L, table_index, "number_type");
  158. return 1;
  159. }
  160. default:
  161. {
  162. platform_print_deprecation_note("node.info() without parameter", "in the next version");
  163. lua_pushinteger(L, NODE_VERSION_MAJOR);
  164. lua_pushinteger(L, NODE_VERSION_MINOR);
  165. lua_pushinteger(L, NODE_VERSION_REVISION);
  166. lua_pushinteger(L, system_get_chip_id()); // chip id
  167. lua_pushinteger(L, spi_flash_get_id()); // flash id
  168. lua_pushinteger(L, flash_rom_get_size_byte() / 1024); // flash size in KB
  169. lua_pushinteger(L, flash_rom_get_mode());
  170. lua_pushinteger(L, flash_rom_get_speed());
  171. return 8;
  172. }
  173. }
  174. }
  175. // Lua: chipid()
  176. static int node_chipid( lua_State* L )
  177. {
  178. uint32_t id = system_get_chip_id();
  179. lua_pushinteger(L, id);
  180. return 1;
  181. }
  182. // deprecated, moved to adc module
  183. // Lua: readvdd33()
  184. // static int node_readvdd33( lua_State* L )
  185. // {
  186. // uint32_t vdd33 = readvdd33();
  187. // lua_pushinteger(L, vdd33);
  188. // return 1;
  189. // }
  190. // Lua: flashid()
  191. static int node_flashid( lua_State* L )
  192. {
  193. uint32_t id = spi_flash_get_id();
  194. lua_pushinteger( L, id );
  195. return 1;
  196. }
  197. // Lua: flashsize()
  198. static int node_flashsize( lua_State* L )
  199. {
  200. uint32_t sz = flash_rom_get_size_byte();
  201. lua_pushinteger( L, sz );
  202. return 1;
  203. }
  204. // Lua: heap()
  205. static int node_heap( lua_State* L )
  206. {
  207. uint32_t sz = system_get_free_heap_size();
  208. lua_pushinteger(L, sz);
  209. return 1;
  210. }
  211. // Lua: input("string")
  212. static int node_input( lua_State* L ) {
  213. luaL_checkstring(L, 1);
  214. lua_getfield(L, LUA_REGISTRYINDEX, "stdin");
  215. lua_rawgeti(L, -1, 1); /* get the pipe_write func from stdin[1] */
  216. lua_insert(L, -2); /* and move above the pipe ref */
  217. lua_pushvalue(L, 1);
  218. lua_call(L, 2, 0); /* stdin:write(line) */
  219. return 0;
  220. }
  221. static int serial_debug = 1;
  222. void output_redirect(const char *str) {
  223. lua_State *L = lua_getstate();
  224. int n = lua_gettop(L);
  225. lua_pushliteral(L, "stdout");
  226. lua_rawget(L, LUA_REGISTRYINDEX); /* fetch reg.stdout */
  227. if (lua_istable(L, -1)) { /* reg.stdout is pipe */
  228. if (serial_debug) {
  229. uart0_sendStr(str);
  230. }
  231. lua_rawgeti(L, -1, 1); /* get the pipe_write func from stdout[1] */
  232. lua_insert(L, -2); /* and move above the pipe ref */
  233. lua_pushstring(L, str);
  234. lua_call(L, 2, 0); /* Reg.stdout:write(str) */
  235. } else { /* reg.stdout == nil */
  236. uart0_sendStr(str);
  237. }
  238. lua_settop(L, n); /* Make sure all code paths leave stack unchanged */
  239. }
  240. extern int pipe_create(lua_State *L);
  241. // Lua: output(function(c), debug)
  242. static int node_output( lua_State* L )
  243. {
  244. serial_debug = (lua_isnumber(L, 2) && lua_tointeger(L, 2) == 0) ? 0 : 1;
  245. lua_settop(L, 1);
  246. if (lua_isanyfunction(L, 1)) {
  247. lua_pushlightfunction(L, &pipe_create);
  248. lua_insert(L, 1);
  249. lua_pushinteger(L, LUA_TASK_MEDIUM);
  250. lua_call(L, 2, 1); /* T[1] = pipe.create(dojob, low_priority) */
  251. } else { // remove the stdout pipe
  252. lua_pop(L,1);
  253. lua_pushnil(L); /* T[1] = nil */
  254. serial_debug = 1;
  255. }
  256. lua_pushliteral(L, "stdout");
  257. lua_insert(L, 1);
  258. lua_rawset(L, LUA_REGISTRYINDEX); /* Reg.stdout = nil or pipe */
  259. return 0;
  260. }
  261. static int writer(lua_State* L, const void* p, size_t size, void* u)
  262. {
  263. UNUSED(L);
  264. int file_fd = *( (int *)u );
  265. if (!file_fd)
  266. return 1;
  267. NODE_DBG("get fd:%d,size:%d\n", file_fd, size);
  268. if (size != 0 && (size != vfs_write(file_fd, (const char *)p, size)) )
  269. return 1;
  270. NODE_DBG("write fd:%d,size:%d\n", file_fd, size);
  271. return 0;
  272. }
  273. #define toproto(L,i) (clvalue(L->top+(i))->l.p)
  274. // Lua: compile(filename) -- compile lua file into lua bytecode, and save to .lc
  275. static int node_compile( lua_State* L )
  276. {
  277. Proto* f;
  278. int file_fd = 0;
  279. size_t len;
  280. const char *fname = luaL_checklstring( L, 1, &len );
  281. const char *basename = vfs_basename( fname );
  282. luaL_argcheck(L, strlen(basename) <= FS_OBJ_NAME_LEN && strlen(fname) == len, 1, "filename invalid");
  283. char *output = luaM_malloc( L, len+1 );
  284. strcpy(output, fname);
  285. // check here that filename end with ".lua".
  286. if (len < 4 || (strcmp( output + len - 4, ".lua") != 0) ) {
  287. luaM_free( L, output );
  288. return luaL_error(L, "not a .lua file");
  289. }
  290. output[strlen(output) - 2] = 'c';
  291. output[strlen(output) - 1] = '\0';
  292. NODE_DBG(output);
  293. NODE_DBG("\n");
  294. if (luaL_loadfile(L, fname) != 0) {
  295. luaM_free( L, output );
  296. return luaL_error(L, lua_tostring(L, -1));
  297. }
  298. f = toproto(L, -1);
  299. int stripping = 1; /* strip debug information? */
  300. file_fd = vfs_open(output, "w+");
  301. if (!file_fd)
  302. {
  303. luaM_free( L, output );
  304. return luaL_error(L, "cannot open/write to file");
  305. }
  306. lua_lock(L);
  307. int result = luaU_dump(L, f, writer, &file_fd, stripping);
  308. lua_unlock(L);
  309. if (vfs_flush(file_fd) != VFS_RES_OK) {
  310. // overwrite Lua error, like writer() does in case of a file io error
  311. result = 1;
  312. }
  313. vfs_close(file_fd);
  314. file_fd = 0;
  315. luaM_free( L, output );
  316. if (result == LUA_ERR_CC_INTOVERFLOW) {
  317. return luaL_error(L, "value too big or small for target integer type");
  318. }
  319. if (result == LUA_ERR_CC_NOTINTEGER) {
  320. return luaL_error(L, "target lua_Number is integral but fractional value found");
  321. }
  322. if (result == 1) { // result status generated by writer() or fs_flush() fail
  323. return luaL_error(L, "writing to file failed");
  324. }
  325. return 0;
  326. }
  327. // Lua: node.task.post([priority],task_cb) -- schedule a task for execution next
  328. static int node_task_post( lua_State* L )
  329. {
  330. int n=1;
  331. unsigned priority = TASK_PRIORITY_MEDIUM;
  332. if (lua_type(L, 1) == LUA_TNUMBER) {
  333. priority = (unsigned) luaL_checkint(L, 1);
  334. luaL_argcheck(L, priority <= TASK_PRIORITY_HIGH, 1, "invalid priority");
  335. n++;
  336. }
  337. luaL_checkanyfunction(L, n);
  338. lua_settop(L, n);
  339. (void) luaN_posttask(L, priority);
  340. return 0;
  341. }
  342. // Lua: setcpufreq(mhz)
  343. // mhz is either CPU80MHZ od CPU160MHZ
  344. static int node_setcpufreq(lua_State* L)
  345. {
  346. // http://www.esp8266.com/viewtopic.php?f=21&t=1369
  347. uint32_t new_freq = luaL_checkinteger(L, 1);
  348. if (new_freq == CPU160MHZ){
  349. REG_SET_BIT(0x3ff00014, BIT(0));
  350. ets_update_cpu_frequency(CPU160MHZ);
  351. } else {
  352. REG_CLR_BIT(0x3ff00014, BIT(0));
  353. ets_update_cpu_frequency(CPU80MHZ);
  354. }
  355. new_freq = ets_get_cpu_frequency();
  356. lua_pushinteger(L, new_freq);
  357. return 1;
  358. }
  359. // Lua: freq = node.getcpufreq()
  360. static int node_getcpufreq(lua_State* L)
  361. {
  362. lua_pushinteger(L, system_get_cpu_freq());
  363. return 1;
  364. }
  365. // Lua: code, reason [, exccause, epc1, epc2, epc3, excvaddr, depc ] = bootreason()
  366. static int node_bootreason (lua_State *L)
  367. {
  368. const struct rst_info *ri = system_get_rst_info ();
  369. uint32_t arr[8] = {
  370. rtc_get_reset_reason(),
  371. ri->reason,
  372. ri->exccause, ri->epc1, ri->epc2, ri->epc3, ri->excvaddr, ri->depc
  373. };
  374. int i, n = ((ri->reason != REASON_EXCEPTION_RST) ? 2 : 8);
  375. for (i = 0; i < n; ++i)
  376. lua_pushinteger (L, arr[i]);
  377. return n;
  378. }
  379. // Lua: restore()
  380. static int node_restore (lua_State *L)
  381. {
  382. system_restore();
  383. return 0;
  384. }
  385. #ifdef LUA_OPTIMIZE_DEBUG
  386. /* node.stripdebug([level[, function]]). 
  387. * level: 1 don't discard debug
  388. * 2 discard Local and Upvalue debug info
  389. * 3 discard Local, Upvalue and lineno debug info.
  390. * function: Function to be stripped as per setfenv except 0 not permitted.
  391. * If no arguments then the current default setting is returned.
  392. * If function is omitted, this is the default setting for future compiles
  393. * The function returns an estimated integer count of the bytes stripped.
  394. */
  395. static int node_stripdebug (lua_State *L) {
  396. int level;
  397. if (L->top == L->base) {
  398. lua_pushlightuserdata(L, &luaG_stripdebug );
  399. lua_gettable(L, LUA_REGISTRYINDEX);
  400. if (lua_isnil(L, -1)) {
  401. lua_pop(L, 1);
  402. lua_pushinteger(L, LUA_OPTIMIZE_DEBUG);
  403. }
  404. return 1;
  405. }
  406. level = luaL_checkint(L, 1);
  407. if ((level <= 0) || (level > 3)) luaL_argerror(L, 1, "must in range 1-3");
  408. if (L->top == L->base + 1) {
  409. /* Store the default level in the registry if no function parameter */
  410. lua_pushlightuserdata(L, &luaG_stripdebug);
  411. lua_pushinteger(L, level);
  412. lua_settable(L, LUA_REGISTRYINDEX);
  413. lua_settop(L,0);
  414. return 0;
  415. }
  416. if (level == 1) {
  417. lua_settop(L,0);
  418. lua_pushinteger(L, 0);
  419. return 1;
  420. }
  421. if (!lua_isfunction(L, 2)) {
  422. int scope = luaL_checkint(L, 2);
  423. if (scope > 0) {
  424. /* if the function parameter is a +ve integer then climb to find function */
  425. lua_Debug ar;
  426. lua_pop(L, 1); /* pop level as getinfo will replace it by the function */
  427. if (lua_getstack(L, scope, &ar)) {
  428. lua_getinfo(L, "f", &ar);
  429. }
  430. }
  431. }
  432. if(!lua_isfunction(L, 2) || lua_iscfunction(L, -1)) luaL_argerror(L, 2, "must be a Lua Function");
  433. // lua_lock(L);
  434. Proto *f = clvalue(L->base + 1)->l.p;
  435. // lua_unlock(L);
  436. lua_settop(L,0);
  437. lua_pushinteger(L, luaG_stripdebug(L, f, level, 1));
  438. return 1;
  439. }
  440. #endif
  441. // Lua: node.egc.setmode( mode, [param])
  442. // where the mode is one of the node.egc constants NOT_ACTIVE , ON_ALLOC_FAILURE,
  443. // ON_MEM_LIMIT, ALWAYS. In the case of ON_MEM_LIMIT an integer parameter is reqired
  444. // See legc.h and lecg.c.
  445. static int node_egc_setmode(lua_State* L) {
  446. unsigned mode = luaL_checkinteger(L, 1);
  447. int limit = luaL_optinteger (L, 2, 0);
  448. luaL_argcheck(L, mode <= (EGC_ON_ALLOC_FAILURE | EGC_ON_MEM_LIMIT | EGC_ALWAYS), 1, "invalid mode");
  449. luaL_argcheck(L, !(mode & EGC_ON_MEM_LIMIT) || limit!=0, 1, "limit must be non-zero");
  450. legc_set_mode( L, mode, limit );
  451. return 0;
  452. }
  453. // totalallocated, estimatedused = node.egc.meminfo()
  454. static int node_egc_meminfo(lua_State *L) {
  455. global_State *g = G(L);
  456. lua_pushinteger(L, g->totalbytes);
  457. lua_pushinteger(L, g->estimate);
  458. return 2;
  459. }
  460. //
  461. // Lua: osprint(true/false)
  462. // Allows you to turn on the native Espressif SDK printing
  463. static int node_osprint( lua_State* L )
  464. {
  465. if (lua_toboolean(L, 1)) {
  466. system_set_os_print(1);
  467. } else {
  468. system_set_os_print(0);
  469. }
  470. return 0;
  471. }
  472. int node_random_range(int l, int u) {
  473. // The range is the number of different values to return
  474. unsigned int range = u + 1 - l;
  475. // If this is very large then use simpler code
  476. if (range >= 0x7fffffff) {
  477. unsigned int v;
  478. // This cannot loop more than half the time
  479. while ((v = os_random()) >= range) {
  480. }
  481. // Now v is in the range [0, range)
  482. return v + l;
  483. }
  484. // Easy case, with only one value, we know the result
  485. if (range == 1) {
  486. return l;
  487. }
  488. // Another easy case -- uniform 32-bit
  489. if (range == 0) {
  490. return os_random();
  491. }
  492. // Now we have to figure out what a large multiple of range is
  493. // that just fits into 32 bits.
  494. // The limit will be less than 1 << 32 by some amount (not much)
  495. uint32_t limit = ((0x80000000 / ((range + 1) >> 1)) - 1) * range;
  496. uint32_t v;
  497. while ((v = os_random()) >= limit) {
  498. }
  499. // Now v is uniformly distributed in [0, limit) and limit is a multiple of range
  500. return (v % range) + l;
  501. }
  502. static int node_random (lua_State *L) {
  503. int u;
  504. int l;
  505. switch (lua_gettop(L)) { /* check number of arguments */
  506. case 0: { /* no arguments */
  507. #ifdef LUA_NUMBER_INTEGRAL
  508. lua_pushnumber(L, 0); /* Number between 0 and 1 - always 0 with ints */
  509. #else
  510. lua_pushnumber(L, (lua_Number)os_random() / (lua_Number)(1LL << 32));
  511. #endif
  512. return 1;
  513. }
  514. case 1: { /* only upper limit */
  515. l = 1;
  516. u = luaL_checkint(L, 1);
  517. break;
  518. }
  519. case 2: { /* lower and upper limits */
  520. l = luaL_checkint(L, 1);
  521. u = luaL_checkint(L, 2);
  522. break;
  523. }
  524. default:
  525. return luaL_error(L, "wrong number of arguments");
  526. }
  527. luaL_argcheck(L, l<=u, 2, "interval is empty");
  528. lua_pushnumber(L, node_random_range(l, u)); /* int between `l' and `u' */
  529. return 1;
  530. }
  531. #ifdef DEVELOPMENT_TOOLS
  532. // Lua: rec = node.readrcr(id)
  533. static int node_readrcr (lua_State *L) {
  534. int id = luaL_checkinteger(L, 1);
  535. char *data;
  536. int n = platform_rcr_read(id, (void **)&data);
  537. if (n == ~0) return 0;
  538. lua_pushlstring(L, data, n);
  539. return 1;
  540. }
  541. // Lua: n = node.writercr(id,rec)
  542. static int node_writercr (lua_State *L) {
  543. int id = luaL_checkinteger(L, 1),l;
  544. const char *data = lua_tolstring(L, 2, &l);
  545. int n = platform_rcr_write(id, data, l);
  546. lua_pushinteger(L, n);
  547. return 1;
  548. }
  549. #endif
  550. typedef enum pt_t { lfs_addr=0, lfs_size, spiffs_addr, spiffs_size, max_pt} pt_t;
  551. LROT_BEGIN(pt)
  552. LROT_NUMENTRY( lfs_addr, lfs_addr )
  553. LROT_NUMENTRY( lfs_size, lfs_size )
  554. LROT_NUMENTRY( spiffs_addr, spiffs_addr )
  555. LROT_NUMENTRY( spiffs_size, spiffs_size )
  556. LROT_END( pt, NULL, 0 )
  557. // Lua: ptinfo = node.getpartitiontable()
  558. static int node_getpartitiontable (lua_State *L) {
  559. uint32_t param[max_pt] = {0};
  560. param[lfs_size] = platform_flash_get_partition(NODEMCU_LFS0_PARTITION, param + lfs_addr);
  561. param[spiffs_size] = platform_flash_get_partition(NODEMCU_SPIFFS0_PARTITION, param + spiffs_addr);
  562. lua_settop(L, 0);
  563. lua_createtable (L, 0, max_pt); /* at index 1 */
  564. lua_pushrotable(L, (void*)pt_map); /* at index 2 */
  565. lua_pushnil(L); /* first key at index 3 */
  566. while (lua_next(L, 2) != 0) { /* key at index 3, and v at index 4 */
  567. lua_pushvalue(L, 3); /* dup key to index 5 */
  568. lua_pushinteger(L, param[lua_tointeger(L, 4)]); /* param [v] at index 6 */
  569. lua_rawset(L, 1);
  570. lua_pop(L, 1); /* discard v */
  571. }
  572. lua_pop(L, 1); /* discard pt_map reference */
  573. return 1;
  574. }
  575. static void insert_partition(partition_item_t *p, int n, uint32_t type, uint32_t addr) {
  576. if (n>0)
  577. memmove(p+1, p, n*sizeof(partition_item_t)); /* overlapped so must be move not cpy */
  578. p->type = type;
  579. p->addr = addr;
  580. p->size = 0;
  581. }
  582. static void delete_partition(partition_item_t *p, int n) {
  583. if (n>0)
  584. memmove(p, p+1, n*sizeof(partition_item_t)); /* overlapped so must be move not cpy */
  585. }
  586. #define SKIP (~0)
  587. #define IROM0_PARTITION (SYSTEM_PARTITION_CUSTOMER_BEGIN + NODEMCU_IROM0TEXT_PARTITION)
  588. #define LFS_PARTITION (SYSTEM_PARTITION_CUSTOMER_BEGIN + NODEMCU_LFS0_PARTITION)
  589. #define SPIFFS_PARTITION (SYSTEM_PARTITION_CUSTOMER_BEGIN + NODEMCU_SPIFFS0_PARTITION)
  590. // Lua: node.setpartitiontable(pt_settings)
  591. static int node_setpartitiontable (lua_State *L) {
  592. partition_item_t *rcr_pt = NULL, *pt;
  593. uint32_t flash_size = flash_rom_get_size_byte();
  594. uint32_t i = platform_rcr_read(PLATFORM_RCR_PT, (void **) &rcr_pt);
  595. uint32_t last = 0;
  596. uint32_t n = i / sizeof(partition_item_t);
  597. uint32_t param[max_pt] = {SKIP, SKIP, SKIP, SKIP};
  598. luaL_argcheck(L, lua_istable(L, 1), 1, "must be table");
  599. lua_settop(L, 1);
  600. /* convert input table into 4 option array */
  601. lua_pushrotable(L, (void*)pt_map); /* at index 2 */
  602. lua_pushnil(L); /* first key at index 3 */
  603. while (lua_next(L, 1) != 0) {
  604. /* 'key' (at index 3) and 'value' (at index 4) */
  605. luaL_argcheck(L, lua_isstring(L, 3) && lua_isnumber(L, 4), 1, "invalid partition setting");
  606. lua_pushvalue(L, 3); /* dup key to index 5 */
  607. lua_rawget(L, 2); /* lookup in pt_map */
  608. luaL_argcheck(L, !lua_isnil(L, -1), 1, "invalid partition setting");
  609. param[lua_tointeger(L, 5)] = lua_tointeger(L, 4);
  610. /* removes 'value'; keeps 'key' for next iteration */
  611. lua_pop(L, 2); /* discard value and lookup */
  612. }
  613. /*
  614. * Allocate a scratch Partition Table as userdata on the Lua stack, and copy the
  615. * current Flash PT into this for manipulation
  616. */
  617. lua_newuserdata(L, (n+2)*sizeof(partition_item_t));
  618. pt = lua_touserdata (L, -1);
  619. memcpy(pt, rcr_pt, n*sizeof(partition_item_t));
  620. pt[n].type = 0; pt[n+1].type = 0;
  621. for (i = 0; i < n; i ++) {
  622. partition_item_t *p = pt + i;
  623. if (p->type == IROM0_PARTITION && p[1].type != LFS_PARTITION) {
  624. // if the LFS partition is not following IROM0 then slot a blank one in
  625. insert_partition(p + 1, n-i-1, LFS_PARTITION, p->addr + p->size);
  626. n++;
  627. } else if (p->type == LFS_PARTITION) {
  628. if (p[1].type != SPIFFS_PARTITION) {
  629. // if the SPIFFS partition is not following LFS then slot a blank one in
  630. insert_partition(p + 1, n-i-1, SPIFFS_PARTITION, 0);
  631. n++;
  632. }
  633. // update the LFS options if set
  634. if (param[lfs_addr] != SKIP) {
  635. p->addr = param[lfs_addr];
  636. }
  637. if (param[lfs_size] != SKIP) {
  638. p->size = param[lfs_size];
  639. }
  640. } else if (p->type == SPIFFS_PARTITION) {
  641. // update the SPIFFS options if set
  642. if (param[spiffs_addr] != SKIP) {
  643. p->addr = param[spiffs_addr];
  644. p->size = SKIP;
  645. }
  646. if (param[spiffs_size] != SKIP) {
  647. // BOTCH: - at the moment the firmware doesn't boot if the SPIFFS partition
  648. // is deleted so the minimum SPIFFS size is 64Kb
  649. p->size = param[spiffs_size] > 0x10000 ? param[spiffs_size] : 0x10000;
  650. }
  651. if (p->size == SKIP) {
  652. if (p->addr < 0) {
  653. // This allocate all the remaining flash to SPIFFS
  654. p->addr = last;
  655. p->size = flash_size - last;
  656. } else {
  657. p->size = flash_size - p->addr;
  658. }
  659. } else if (/* size is specified && */ p->addr == 0) {
  660. // if the is addr not specified then start SPIFFS at 1Mb
  661. // boundary if the size will fit otherwise make it consecutive
  662. // to the previous partition.
  663. p->addr = (p->size <= flash_size - 0x100000) ? 0x100000 : last;
  664. }
  665. }
  666. if (p->size == 0) {
  667. // Delete 0-sized partitions as the SDK barfs on these
  668. delete_partition(p, n-i-1);
  669. n--; i--;
  670. } else {
  671. // Do consistency tests on the partition
  672. if (p->addr & (INTERNAL_FLASH_SECTOR_SIZE - 1) ||
  673. p->size & (INTERNAL_FLASH_SECTOR_SIZE - 1) ||
  674. p->addr < last ||
  675. p->addr + p->size > flash_size) {
  676. luaL_error(L, "value out of range");
  677. }
  678. }
  679. }
  680. // for (i = 0; i < n; i ++)
  681. // dbg_printf("Partition %d: %04x %06x %06x\n", i, pt[i].type, pt[i].addr, pt[i].size);
  682. platform_rcr_write(PLATFORM_RCR_PT, pt, n*sizeof(partition_item_t));
  683. while(1); // Trigger WDT; the new PT will be loaded on reboot
  684. return 0;
  685. }
  686. // Module function map
  687. LROT_BEGIN(node_egc)
  688. LROT_FUNCENTRY( meminfo, node_egc_meminfo )
  689. LROT_FUNCENTRY( setmode, node_egc_setmode )
  690. LROT_NUMENTRY( NOT_ACTIVE, EGC_NOT_ACTIVE )
  691. LROT_NUMENTRY( ON_ALLOC_FAILURE, EGC_ON_ALLOC_FAILURE )
  692. LROT_NUMENTRY( ON_MEM_LIMIT, EGC_ON_MEM_LIMIT )
  693. LROT_NUMENTRY( ALWAYS, EGC_ALWAYS )
  694. LROT_END( node_egc, NULL, 0 )
  695. LROT_BEGIN(node_task)
  696. LROT_FUNCENTRY( post, node_task_post )
  697. LROT_NUMENTRY( LOW_PRIORITY, TASK_PRIORITY_LOW )
  698. LROT_NUMENTRY( MEDIUM_PRIORITY, TASK_PRIORITY_MEDIUM )
  699. LROT_NUMENTRY( HIGH_PRIORITY, TASK_PRIORITY_HIGH )
  700. LROT_END( node_task, NULL, 0 )
  701. LROT_BEGIN(node)
  702. LROT_FUNCENTRY( heap, node_heap )
  703. LROT_FUNCENTRY( info, node_info )
  704. LROT_TABENTRY( task, node_task )
  705. LROT_FUNCENTRY( flashreload, luaN_reload_reboot )
  706. LROT_FUNCENTRY( flashindex, luaN_index )
  707. LROT_FUNCENTRY( restart, node_restart )
  708. LROT_FUNCENTRY( dsleep, node_deepsleep )
  709. LROT_FUNCENTRY( dsleepMax, dsleepMax )
  710. LROT_FUNCENTRY( sleep, node_sleep )
  711. #ifdef PMSLEEP_ENABLE
  712. PMSLEEP_INT_MAP
  713. #endif
  714. #ifdef DEVELOPMENT_TOOLS
  715. LROT_FUNCENTRY( readrcr, node_readrcr )
  716. LROT_FUNCENTRY( writercr, node_writercr )
  717. #endif
  718. LROT_FUNCENTRY( chipid, node_chipid )
  719. LROT_FUNCENTRY( flashid, node_flashid )
  720. LROT_FUNCENTRY( flashsize, node_flashsize )
  721. LROT_FUNCENTRY( input, node_input )
  722. LROT_FUNCENTRY( output, node_output )
  723. // Moved to adc module, use adc.readvdd33()
  724. // LROT_FUNCENTRY( readvdd33, node_readvdd33 )
  725. LROT_FUNCENTRY( compile, node_compile )
  726. LROT_NUMENTRY( CPU80MHZ, CPU80MHZ )
  727. LROT_NUMENTRY( CPU160MHZ, CPU160MHZ )
  728. LROT_FUNCENTRY( setcpufreq, node_setcpufreq )
  729. LROT_FUNCENTRY( getcpufreq, node_getcpufreq )
  730. LROT_FUNCENTRY( bootreason, node_bootreason )
  731. LROT_FUNCENTRY( restore, node_restore )
  732. LROT_FUNCENTRY( random, node_random )
  733. #ifdef LUA_OPTIMIZE_DEBUG
  734. LROT_FUNCENTRY( stripdebug, node_stripdebug )
  735. #endif
  736. LROT_TABENTRY( egc, node_egc )
  737. #ifdef DEVELOPMENT_TOOLS
  738. LROT_FUNCENTRY( osprint, node_osprint )
  739. #endif
  740. LROT_FUNCENTRY( getpartitiontable, node_getpartitiontable )
  741. LROT_FUNCENTRY( setpartitiontable, node_setpartitiontable )
  742. // Combined to dsleep(us, option)
  743. // LROT_FUNCENTRY( dsleepsetoption, node_deepsleep_setoption )
  744. LROT_END( node, NULL, 0 )
  745. NODEMCU_MODULE(NODE, "node", node, NULL);