From c4091c575bcb904577fb613b76af5594f8b04252 Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Tue, 16 Nov 2021 21:46:42 +0100 Subject: [PATCH 1/3] Berry simplified preinit and autoexec --- lib/libesp32/Berry/default/berry_conf.h | 6 +++ .../Berry/default/embedded/autoconf.be | 2 + lib/libesp32/Berry/src/be_vm.c | 4 +- tasmota/xdrv_52_9_berry.ino | 37 ++----------------- 4 files changed, 13 insertions(+), 36 deletions(-) diff --git a/lib/libesp32/Berry/default/berry_conf.h b/lib/libesp32/Berry/default/berry_conf.h index 3360b9082..e11d6c3cb 100644 --- a/lib/libesp32/Berry/default/berry_conf.h +++ b/lib/libesp32/Berry/default/berry_conf.h @@ -100,6 +100,12 @@ **/ #define BE_STACK_FREE_MIN 20 +/* Macro: BE_STACK_START + * Set the starting size of the stack at VM creation. + * Default: 50 + **/ +#define BE_STACK_START 100 + /* Macro: BE_CONST_SEARCH_SIZE * Constants in function are limited to 255. However the compiler * will look for a maximum of pre-existing constants to avoid diff --git a/lib/libesp32/Berry/default/embedded/autoconf.be b/lib/libesp32/Berry/default/embedded/autoconf.be index 3f9586e04..a30ed2044 100644 --- a/lib/libesp32/Berry/default/embedded/autoconf.be +++ b/lib/libesp32/Berry/default/embedded/autoconf.be @@ -293,6 +293,7 @@ autoconf_module.init = def (m) self._error = nil end + # called by the synthetic event `preinit` def preinit() if self._archive == nil return end # try to launch `preinit.be` @@ -327,6 +328,7 @@ autoconf_module.init = def (m) end end + # called by the synthetic event `autoexec` def autoexec() if self._archive == nil return end # try to launch `preinit.be` diff --git a/lib/libesp32/Berry/src/be_vm.c b/lib/libesp32/Berry/src/be_vm.c index ac2ede585..48834910e 100644 --- a/lib/libesp32/Berry/src/be_vm.c +++ b/lib/libesp32/Berry/src/be_vm.c @@ -453,8 +453,8 @@ BERRY_API bvm* be_vm_new(void) be_stack_init(vm, &vm->refstack, sizeof(binstance*)); be_stack_init(vm, &vm->exceptstack, sizeof(struct bexecptframe)); be_stack_init(vm, &vm->tracestack, sizeof(bcallsnapshot)); - vm->stack = be_malloc(vm, sizeof(bvalue) * BE_STACK_FREE_MIN); - vm->stacktop = vm->stack + BE_STACK_FREE_MIN; + vm->stack = be_malloc(vm, sizeof(bvalue) * BE_STACK_START); + vm->stacktop = vm->stack + BE_STACK_START; vm->reg = vm->stack; vm->top = vm->reg; be_globalvar_init(vm); diff --git a/tasmota/xdrv_52_9_berry.ino b/tasmota/xdrv_52_9_berry.ino index 46cc5692f..84a7b962a 100644 --- a/tasmota/xdrv_52_9_berry.ino +++ b/tasmota/xdrv_52_9_berry.ino @@ -241,34 +241,6 @@ int32_t callBerryEventDispatcher(const char *type, const char *cmd, int32_t idx, return ret; } -// call a method in autoconf -int32_t callBerryAutoconf(const char * method) { - int32_t ret = 0; - bvm *vm = berry.vm; - - if (nullptr == vm) { return ret; } - checkBeTop(); - be_getglobal(vm, "autoconf"); - if (!be_isnil(vm, -1)) { - be_getmethod(vm, -1, method); - if (!be_isnil(vm, -1)) { - be_pushvalue(vm, -2); - BrTimeoutStart(); - ret = be_pcall(vm, 1); // 1 arg - BrTimeoutReset(); - if (ret != 0) { - BerryDumpErrorAndClear(vm, false); // log in Tasmota console only - return ret; - } - be_pop(vm, 1); // remove instance - } - be_pop(vm, 1); // remove method - } - be_pop(vm, 1); // remove instance object - checkBeTop(); - return ret; -} - /*********************************************************************************************\ * VM Observability \*********************************************************************************************/ @@ -364,9 +336,9 @@ void BerryInit(void) { AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_BERRY "Berry initialized, RAM used=%u"), callBerryGC()); berry_init_ok = true; - // Run 'autoconf.preinit()' - callBerryAutoconf("preinit"); - + // we generate a synthetic event `autoexec` + callBerryEventDispatcher(PSTR("preinit"), nullptr, 0, nullptr); + // Run pre-init BrLoad("preinit.be"); // run 'preinit.be' if present } while (0); @@ -790,9 +762,6 @@ bool Xdrv52(uint8_t function) // break; case FUNC_LOOP: if (!berry.autoexec_done) { - // Run 'autoconf.preinit()' - callBerryAutoconf("autoexec"); - // we generate a synthetic event `autoexec` callBerryEventDispatcher(PSTR("autoexec"), nullptr, 0, nullptr); From aca4223ad6b285b8734a1238726fd972af8e602c Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Tue, 16 Nov 2021 22:21:44 +0100 Subject: [PATCH 2/3] ESP32 fix leftover GPIO configuration after restart --- CHANGELOG.md | 1 + tasmota/support_tasmota.ino | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf3b47477..3c68385d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. ## [10.0.0.3] ### Added - Autoconfiguration for ESP32 and variants +- ESP32 fix leftover GPIO configuration after restart ### Changed - ESP8266 Gratuitous ARP enabled and set to 60 seconds (#13623) diff --git a/tasmota/support_tasmota.ino b/tasmota/support_tasmota.ino index 10bbcc495..bfe894446 100644 --- a/tasmota/support_tasmota.ino +++ b/tasmota/support_tasmota.ino @@ -1651,6 +1651,22 @@ void ResetPwm(void) /********************************************************************************************/ +#ifdef ESP32 +// Since ESP-IDF 4.4, GPIO matrix or I/O is not reset during a restart +// and GPIO configuration can get stuck because of leftovers +// +// This patched version of pinMode forces a full GPIO reset before setting new mode +// +extern "C" void ARDUINO_ISR_ATTR __pinMode(uint8_t pin, uint8_t mode); + +extern "C" void ARDUINO_ISR_ATTR pinMode(uint8_t pin, uint8_t mode) { + gpio_reset_pin((gpio_num_t)pin); + __pinMode(pin, mode); +} +#endif + +/********************************************************************************************/ + void GpioInit(void) { if (!ValidModule(Settings->module)) { From 7f8ca14770d7466efcbbe5c75ac07abbb3e84f5c Mon Sep 17 00:00:00 2001 From: Adrian Scillato Date: Tue, 16 Nov 2021 19:10:25 -0300 Subject: [PATCH 3/3] Fix NTC Temp support for ESP32 --- tasmota/xsns_02_analog.ino | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tasmota/xsns_02_analog.ino b/tasmota/xsns_02_analog.ino index 1e69da349..c6fce4051 100644 --- a/tasmota/xsns_02_analog.ino +++ b/tasmota/xsns_02_analog.ino @@ -43,7 +43,7 @@ // Parameters for equation #define ANALOG_V33 3.3 // ESP8266 / ESP32 Analog voltage -#define ANALOG_T0 TO_KELVIN(25.0) // 25 degrees Celcius in Kelvin (= 298.15) +#define ANALOG_T0 TO_KELVIN(25.0) // 25 degrees Celsius in Kelvin (= 298.15) // Shelly 2.5 NTC Thermistor // 3V3 --- ANALOG_NTC_BRIDGE_RESISTANCE ---v--- NTC --- Gnd @@ -409,8 +409,15 @@ void AdcEverySecond(void) { for (uint32_t idx = 0; idx < Adcs.present; idx++) { if (ADC_TEMP == Adc[idx].type) { int adc = AdcRead(Adc[idx].pin, 2); - // Steinhart-Hart equation for thermistor as temperature sensor + // Steinhart-Hart equation for thermistor as temperature sensor: + // double Rt = (adc * Adc[idx].param1 * MAX_ADC_V) / (ANALOG_RANGE * ANALOG_V33 - (double)adc * MAX_ADC_V); + // MAX_ADC_V in ESP8266 is 1 + // MAX_ADC_V in ESP32 is 3.3 +#ifdef ESP8266 double Rt = (adc * Adc[idx].param1) / (ANALOG_RANGE * ANALOG_V33 - (double)adc); // Shelly param1 = 32000 (ANALOG_NTC_BRIDGE_RESISTANCE) +#else + double Rt = (adc * Adc[idx].param1) / (ANALOG_RANGE - (double)adc); +#endif double BC = (double)Adc[idx].param3 / 10000; // Shelly param3 = 3350 (ANALOG_NTC_B_COEFFICIENT) double T = BC / (BC / ANALOG_T0 + TaylorLog(Rt / (double)Adc[idx].param2)); // Shelly param2 = 10000 (ANALOG_NTC_RESISTANCE) Adc[idx].temperature = ConvertTemp(TO_CELSIUS(T));