From 33ba44ebda93c936cbc9a0b2dd2764567fda63ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Tue, 16 Aug 2022 18:01:38 +0200 Subject: [PATCH] [core] Move running OTA detection to family code --- arduino/beken-72xx/cores/arduino/LibreTuyaAPI.cpp | 5 +++++ arduino/libretuya/core/LibreTuyaClass.cpp | 12 ------------ arduino/libretuya/core/LibreTuyaClass.h | 5 ++++- arduino/realtek-ambz/cores/arduino/LibreTuyaAPI.cpp | 7 +++++++ 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/arduino/beken-72xx/cores/arduino/LibreTuyaAPI.cpp b/arduino/beken-72xx/cores/arduino/LibreTuyaAPI.cpp index 47faf56..64645b5 100644 --- a/arduino/beken-72xx/cores/arduino/LibreTuyaAPI.cpp +++ b/arduino/beken-72xx/cores/arduino/LibreTuyaAPI.cpp @@ -112,6 +112,11 @@ uint32_t LibreTuya::getMaxAllocHeap() { static int8_t otaImage2Valid = -1; +uint8_t LibreTuya::otaGetRunning() { + // Beken has bootloader-based OTA, running app is always index 1 + return 1; +} + uint8_t LibreTuya::otaGetStoredIndex() { return otaHasImage2() ? 2 : 1; } diff --git a/arduino/libretuya/core/LibreTuyaClass.cpp b/arduino/libretuya/core/LibreTuyaClass.cpp index fb739da..a02ed9c 100644 --- a/arduino/libretuya/core/LibreTuyaClass.cpp +++ b/arduino/libretuya/core/LibreTuyaClass.cpp @@ -74,18 +74,6 @@ __attribute__((weak)) uint32_t LibreTuya::getFlashChipSize() { #endif } -static uint8_t otaRunningIndex = 0; - -/** - * @brief Get the currently running firmware OTA index. - */ -uint8_t LibreTuya::otaGetRunning() { - if (otaRunningIndex) - return otaRunningIndex; - // otaRunningIndex will be correct even after switchOta() - return otaRunningIndex = otaGetStoredIndex(); -} - /** * @brief Get the OTA index for updated firmware. * diff --git a/arduino/libretuya/core/LibreTuyaClass.h b/arduino/libretuya/core/LibreTuyaClass.h index 82a2d33..1c16896 100644 --- a/arduino/libretuya/core/LibreTuyaClass.h +++ b/arduino/libretuya/core/LibreTuyaClass.h @@ -33,7 +33,6 @@ class LibreTuya { const char *getDeviceName(); uint32_t getCpuFreqMHz(); uint32_t getFlashChipSize(); - uint8_t otaGetRunning(); uint8_t otaGetTarget(); bool otaRollback(); bool otaCanRollback(); @@ -109,6 +108,10 @@ class LibreTuya { uint32_t getMaxAllocHeap(); public: /* OTA-related */ + /** + * @brief Get the currently running firmware OTA index. + */ + uint8_t otaGetRunning(); /** * @brief Read the currently active OTA index, i.e. the one that will boot upon restart. */ diff --git a/arduino/realtek-ambz/cores/arduino/LibreTuyaAPI.cpp b/arduino/realtek-ambz/cores/arduino/LibreTuyaAPI.cpp index 9d31dcb..b1c4103 100644 --- a/arduino/realtek-ambz/cores/arduino/LibreTuyaAPI.cpp +++ b/arduino/realtek-ambz/cores/arduino/LibreTuyaAPI.cpp @@ -99,6 +99,13 @@ uint32_t LibreTuya::getMaxAllocHeap() { /* OTA-related */ +uint8_t LibreTuya::otaGetRunning() { + // RTL8710B is XIP, so check the code offset in flash + uint32_t addr = (uint32_t)lt_log; + uint32_t offs = addr - SPI_FLASH_BASE; + return offs > FLASH_OTA2_OFFSET ? 2 : 1; +} + uint8_t LibreTuya::otaGetStoredIndex() { uint32_t *otaAddress = (uint32_t *)0x8009000; if (*otaAddress == 0xFFFFFFFF)