From d6e5c8946352c4a9eca1144aebd1ead1a560aa09 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Tue, 10 May 2022 22:21:34 +0200 Subject: [PATCH] Fix ESP32-C3 safeboot upload --- tasmota/support_esp.ino | 11 +++++++++++ tasmota/support_tasmota.ino | 15 +++++++++++++++ tasmota/xdrv_52_3_berry_flash.ino | 9 ++++++--- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/tasmota/support_esp.ino b/tasmota/support_esp.ino index 00538b0db..65a9b01c9 100644 --- a/tasmota/support_esp.ino +++ b/tasmota/support_esp.ino @@ -269,6 +269,17 @@ void QPCWrite(const void *pSettings, unsigned nSettingsLen) { NvmSave("qpc", "pcreg", pSettings, nSettingsLen); } +bool OtaFactoryRead(void) { + uint32_t pOtaLoader; + NvmLoad("otal", "otal", &pOtaLoader, sizeof(pOtaLoader)); + return pOtaLoader; +} + +void OtaFactoryWrite(bool value) { + uint32_t pOtaLoader = value; + NvmSave("otal", "otal", &pOtaLoader, sizeof(pOtaLoader)); +} + void NvsInfo(void) { nvs_stats_t nvs_stats; nvs_get_stats(NULL, &nvs_stats); diff --git a/tasmota/support_tasmota.ino b/tasmota/support_tasmota.ino index 343481e1d..0ea18563f 100644 --- a/tasmota/support_tasmota.ino +++ b/tasmota/support_tasmota.ino @@ -1175,6 +1175,9 @@ void Every250mSeconds(void) if (TasmotaGlobal.ota_state_flag && CommandsReady()) { TasmotaGlobal.ota_state_flag--; if (2 == TasmotaGlobal.ota_state_flag) { +#ifdef CONFIG_IDF_TARGET_ESP32C3 + OtaFactoryWrite(false); +#endif RtcSettings.ota_loader = 0; // Try requested image first ota_retry_counter = OTA_ATTEMPTS; SettingsSave(1); // Free flash for OTA update @@ -1246,6 +1249,9 @@ void Every250mSeconds(void) #ifdef ESP32 #ifndef FIRMWARE_MINIMAL if (EspSingleOtaPartition()) { +#ifdef CONFIG_IDF_TARGET_ESP32C3 + OtaFactoryWrite(true); +#endif RtcSettings.ota_loader = 1; // Try safeboot image next SettingsSaveAll(); AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_APPLICATION D_RESTARTING)); @@ -1413,10 +1419,19 @@ void Every250mSeconds(void) case 3: // Every x.75 second if (!TasmotaGlobal.global_state.network_down) { #ifdef FIRMWARE_MINIMAL + +#ifdef CONFIG_IDF_TARGET_ESP32C3 + if (OtaFactoryRead()) { + OtaFactoryWrite(false); + TasmotaGlobal.ota_state_flag = 3; + } +#else if (1 == RtcSettings.ota_loader) { RtcSettings.ota_loader = 0; TasmotaGlobal.ota_state_flag = 3; } +#endif + #endif // FIRMWARE_MINIMAL #ifdef USE_DISCOVERY diff --git a/tasmota/xdrv_52_3_berry_flash.ino b/tasmota/xdrv_52_3_berry_flash.ino index 39e0e44b1..555eb4e93 100644 --- a/tasmota/xdrv_52_3_berry_flash.ino +++ b/tasmota/xdrv_52_3_berry_flash.ino @@ -63,9 +63,9 @@ size_t FlashWriteSubSector(uint32_t address_start, const uint8_t *data, size_t s /*********************************************************************************************\ * Native functions mapped to Berry functions - * + * * import flash - * + * \*********************************************************************************************/ extern "C" { // Berry: `flash.read(address:int[, length:int]) -> bytes()` @@ -74,7 +74,7 @@ extern "C" { int32_t p_flash_read(struct bvm *vm); int32_t p_flash_read(struct bvm *vm) { int32_t argc = be_top(vm); // Get the number of arguments - if (argc >= 1 && be_isint(vm, 1) && + if (argc >= 1 && be_isint(vm, 1) && (argc < 2 || be_isint(vm, 2)) ) { // optional second argument must be int uint32_t address = be_toint(vm, 1); uint32_t length = 0x1000; @@ -146,6 +146,9 @@ extern "C" { esp_partition_erase_range(otadata_partition, 0, SPI_FLASH_SEC_SIZE * 2); } if (force_ota) { +#ifdef CONFIG_IDF_TARGET_ESP32C3 + OtaFactoryWrite(true); +#endif RtcSettings.ota_loader = 1; // force OTA at next reboot } }