Fix ESP32-C3 safeboot upload

This commit is contained in:
Theo Arends 2022-05-10 22:21:34 +02:00
parent a7439cf853
commit d6e5c89463
3 changed files with 32 additions and 3 deletions

View File

@ -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);

View File

@ -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

View File

@ -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
}
}