From dbc8d791c6e0231ff421668dca64efc08733069d Mon Sep 17 00:00:00 2001 From: Johny Mattsson Date: Thu, 2 Mar 2017 22:29:40 +1100 Subject: [PATCH] Added node.chipid() based on esptool's formula. --- components/modules/node.c | 25 +++++++++++++++++++++++-- docs/en/modules/node.md | 5 ++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/components/modules/node.c b/components/modules/node.c index e9d21a84..c01e3088 100644 --- a/components/modules/node.c +++ b/components/modules/node.c @@ -7,14 +7,33 @@ #include "vfs.h" #include "esp_system.h" #include "esp_log.h" +#include "soc/efuse_reg.h" #include "ldebug.h" -// Lua: heap() +// Lua: node.chipid() +static int node_chipid( lua_State *L ) +{ + // This matches the way esptool.py generates a chipid for the ESP32 as of + // esptool commit e9e9179f6fc3f2ecfc568987d3224b5e53a05f06 + // Oddly, this drops the lowest byte what's effectively the MAC address, so + // it would seem plausible to encounter up to 256 chips with the same chipid + uint64_t word16 = REG_READ(EFUSE_BLK0_RDATA1_REG); + uint64_t word17 = REG_READ(EFUSE_BLK0_RDATA2_REG); + const uint64_t MAX_UINT24 = 0xffffff; + uint64_t cid = ((word17 & MAX_UINT24) << 24) | ((word16 >> 8) & MAX_UINT24); + char chipid[17] = { 0 }; + sprintf(chipid, "0x%llx", cid); + lua_pushstring(L, chipid); + return 1; +} + + +// Lua: node.heap() static int node_heap( lua_State* L ) { uint32_t sz = esp_get_free_heap_size(); lua_pushinteger(L, sz); -return 1; + return 1; } static int node_restart (lua_State *L) @@ -147,6 +166,7 @@ static int writer(lua_State* L, const void* p, size_t size, void* u) return 0; } + #define toproto(L,i) (clvalue(L->top+(i))->l.p) // Lua: compile(filename) -- compile lua file into lua bytecode, and save to .lc static int node_compile( lua_State* L ) @@ -280,6 +300,7 @@ static const LUA_REG_TYPE node_task_map[] = { static const LUA_REG_TYPE node_map[] = { + { LSTRKEY( "chipid" ), LFUNCVAL( node_chipid ) }, { LSTRKEY( "compile" ), LFUNCVAL( node_compile ) }, { LSTRKEY( "dsleep" ), LFUNCVAL( node_dsleep ) }, { LSTRKEY( "egc" ), LROVAL( node_egc_map ) }, diff --git a/docs/en/modules/node.md b/docs/en/modules/node.md index d2e8e725..8b0dc0a6 100644 --- a/docs/en/modules/node.md +++ b/docs/en/modules/node.md @@ -56,7 +56,10 @@ Returns the ESP chip ID. none #### Returns -chip ID (number) +chip ID (string) + +Note that due to the chip id being a much larger value on the ESP32, it is +reported as a string now. E.g. `"0x1818fe346a88"`. ## node.compile()