Add node.bootreason (#3413)

This commit is contained in:
Marcel Stör 2021-04-02 08:03:17 +02:00 committed by GitHub
parent ef5d9ff405
commit 3acb6b7c79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 134 additions and 2 deletions

View File

@ -15,6 +15,8 @@
#ifndef LUA_CROSS_COMPILER
#include "esp_system.h"
#include "vfs.h"
/* defined in esp_system_internal.h */
void esp_reset_reason_set_hint(esp_reset_reason_t hint);
#else
#endif
@ -956,20 +958,52 @@ LUALIB_API void luaL_assertfail(const char *file, int line, const char *message)
#endif
}
#ifndef LUA_CROSS_COMPILER
/*
* upper 28 bit have magic value 0xD1EC0DE0
* if paniclevel is valid (i.e. matches magic value)
* lower 4 bits have paniclevel:
* 0 = no panic occurred
* 1..15 = one..fifteen subsequent panic(s) occurred
*/
static __NOINIT_ATTR uint32_t l_rtc_panic_val;
int panic_get_nvval() {
if ((l_rtc_panic_val & 0xfffffff0) == 0xD1EC0DE0) {
return (l_rtc_panic_val & 0xf);
}
panic_clear_nvval();
return 0;
}
void panic_clear_nvval() {
l_rtc_panic_val = 0xD1EC0DE0;
}
#endif
static int panic (lua_State *L) {
(void)L; /* to avoid warnings */
#ifndef LUA_CROSS_COMPILER
uint8_t paniclevel = panic_get_nvval();
if (paniclevel < 15) paniclevel++;
l_rtc_panic_val = 0xD1EC0DE0 | paniclevel;
#endif
#if defined(LUA_USE_STDIO)
fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n",
lua_tostring(L, -1));
#else
luai_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n",
lua_tostring(L, -1));
#endif
#ifndef LUA_CROSS_COMPILER
/* call abort() directly - we don't want another reset cause to intervene */
esp_reset_reason_set_hint(ESP_RST_PANIC);
abort();
#endif
while (1) {}
return 0;
}
LUALIB_API lua_State *luaL_newstate (void) {
lua_State *L = lua_newstate(l_alloc, NULL);
lua_setallocf(L, l_alloc, L); /* allocator need lua_State. */

View File

@ -184,4 +184,7 @@ LUALIB_API void (luaL_pushresult) (luaL_Buffer *B);
#endif
#ifndef LUA_CROSS_COMPILER
int panic_get_nvval();
void panic_clear_nvval();
#endif

View File

@ -15,6 +15,75 @@
#include "esp_vfs.h"
#include "lnodeaux.h"
#include "lflash.h"
#include "rom/rtc.h"
// Lua: node.bootreason()
static int node_bootreason( lua_State *L)
{
int panicval = panic_get_nvval();
RESET_REASON rr0 = rtc_get_reset_reason(0);
unsigned rawinfo = 3;
// rawinfo can take these values as defined in docs/modules/node.md
//
// 1, power-on
// 2, reset (software?)
// 3, hardware reset via reset pin or unknown reason
// 4, WDT reset (watchdog timeout)
//
// extendedinfo can take these values as definded in docs/modules/node.md
//
// 0, power-on
// 1, hardware watchdog reset
// 2, exception reset
// 3, software watchdog reset
// 4, software restart
// 5, wake from deep sleep
// 6, external reset
// added values from rom/rtc.h with offset 7
// 7: NO_MEAN = 0,
// 8: POWERON_RESET = 1, /**<1, Vbat power on reset*/
// 9:
// 10: SW_RESET = 3, /**<3, Software reset digital core*/
// 11: OWDT_RESET = 4, /**<4, Legacy watch dog reset digital core*/
// 12: DEEPSLEEP_RESET = 5, /**<3, Deep Sleep reset digital core*/
// 13: SDIO_RESET = 6, /**<6, Reset by SLC module, reset digital core*/
// 14: TG0WDT_SYS_RESET = 7, /**<7, Timer Group0 Watch dog reset digital core*/
// 15: TG1WDT_SYS_RESET = 8, /**<8, Timer Group1 Watch dog reset digital core*/
// 16: RTCWDT_SYS_RESET = 9, /**<9, RTC Watch dog Reset digital core*/
// 17: INTRUSION_RESET = 10, /**<10, Instrusion tested to reset CPU*/
// 18: TGWDT_CPU_RESET = 11, /**<11, Time Group reset CPU*/
// 19: SW_CPU_RESET = 12, /**<12, Software reset CPU*/
// 20: RTCWDT_CPU_RESET = 13, /**<13, RTC Watch dog Reset CPU*/
// 21: EXT_CPU_RESET = 14, /**<14, for APP CPU, reseted by PRO CPU*/
// 22: RTCWDT_BROWN_OUT_RESET = 15, /**<15, Reset when the vdd voltage is not stable*/
// 23: RTCWDT_RTC_RESET = 16 /**<16, RTC Watch dog reset digital core and rtc module*/`
switch (rr0) {
case NO_MEAN: rawinfo = 3; break;
case POWERON_RESET: rawinfo = 1; break;
case SW_RESET: rawinfo = 2; break;
case OWDT_RESET: rawinfo = 4; break;
case DEEPSLEEP_RESET:
case SDIO_RESET:
case TG0WDT_SYS_RESET:
case TG1WDT_SYS_RESET:
rawinfo = 3; break;
case RTCWDT_SYS_RESET: rawinfo = 4; break;
case INTRUSION_RESET: rawinfo = 3; break;
case TGWDT_CPU_RESET: rawinfo = 4; break;
case SW_CPU_RESET: rawinfo = 2; break;
case RTCWDT_CPU_RESET: rawinfo = 4; break;
case EXT_CPU_RESET:
case RTCWDT_BROWN_OUT_RESET: rawinfo = 3; break;
case RTCWDT_RTC_RESET: rawinfo = 3; break;
}
lua_pushinteger(L, (lua_Integer)rawinfo);
lua_pushinteger(L, (lua_Integer)rr0+7);
if (rr0 == SW_CPU_RESET) {
lua_pushinteger(L, (lua_Integer)panicval);
return 3;
}
return 2;
}
// Lua: node.chipid()
static int node_chipid( lua_State *L )
@ -44,6 +113,7 @@ static int node_heap( lua_State* L )
static int node_restart (lua_State *L)
{
panic_clear_nvval();
esp_restart ();
return 0;
}
@ -637,6 +707,7 @@ LROT_BEGIN(node_wakeup)
LROT_END(node_wakeup, NULL, 0)
LROT_BEGIN(node)
LROT_FUNCENTRY( bootreason, node_bootreason )
LROT_FUNCENTRY( chipid, node_chipid )
LROT_FUNCENTRY( compile, node_compile )
LROT_FUNCENTRY( dsleep, node_dsleep )

View File

@ -26,10 +26,34 @@ The second value returned is the extended reset cause. Values are:
- 5, wake from deep sleep
- 6, external reset
Or the internal reset code of the system can be returned as extended reset cause:
- 7: NO_MEAN no known reason
- 8: POWERON_RESET power on reset, this includes reset button connected to power-on reset
- 9:
- 10 SW_RESET software reset digital core caused by esp-idk firmware
- 11: OWDT_RESET legacy watch dog reset digital core
- 12: DEEPSLEEP_RESET Deep Sleep reset digital core
- 13: SDIO_RESET Reset by SLC module, reset digital core
- 14: TG0WDT_SYS_RESET Timer Group0 Watch dog reset digital core
- 15: TG1WDT_SYS_RESET Timer Group1 Watch dog reset digital core
- 16: RTCWDT_SYS_RESET RTC Watch dog Reset digital core
- 17: INTRUSION_RESET Instrusion tested to reset CPU
- 18: TGWDT_CPU_RESET Time Group reset CPU
- 19: SW_CPU_RESET Software reset (from node.restart() or lua PANIC)
- 20: RTCWDT_CPU_RESET RTC Watch dog Reset CPU
- 21: EXT_CPU_RESET for APP CPU, reseted by PRO CPU
- 22: RTCWDT_BROWN_OUT_RESET Reset when the vdd voltage is not stable
- 23: RTCWDT_RTC_RESET RTC Watch dog reset digital core and rtc module
In general, the extended reset cause supercedes the raw code. The raw code is kept for backwards compatibility only. For new applications it is highly recommended to use the extended reset cause instead.
In case of extended reset cause 3 (exception reset), additional values are returned containing the crash information. These are, in order, EXCCAUSE, EPC1, EPC2, EPC3, EXCVADDR, and DEPC.
In case of extended reset cause 19 (SW_CPU_RESET), an additional value is returned containing the number of consecutive Lua panics. If the reset was caused by a call to node.restart() this value is 0; after the first panic the value is 1; if a panic reoccurs the value increments upto 15.
#### Syntax
`node.bootreason()`