Skip to content

Commit

Permalink
Use empty sector before the FS partition for 8266 power-on-counter (E…
Browse files Browse the repository at this point in the history
  • Loading branch information
pkendall64 authored Jun 12, 2024
1 parent 528e312 commit 8b606e1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
47 changes: 47 additions & 0 deletions src/lib/CONFIG/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,10 @@ TxConfig::SetModelId(uint8_t modelId)

#if defined(TARGET_RX)

#if defined(PLATFORM_ESP8266)
#include "flash_hal.h"
#endif

RxConfig::RxConfig()
{
}
Expand Down Expand Up @@ -918,9 +922,38 @@ bool RxConfig::GetIsBound() const
return !m_config.volatileBind && UID_IS_BOUND(m_config.uid);
}

#if defined(PLATFORM_ESP8266)
#define EMPTY_SECTOR ((FS_start - 0x1000 - 0x40200000) / SPI_FLASH_SEC_SIZE) // empty sector before FS area start
static bool erase_power_on_count = false;
static int realPowerOnCounter = -1;
uint8_t
RxConfig::GetPowerOnCounter() const
{
if (realPowerOnCounter == -1) {
byte zeros[16];
ESP.flashRead(EMPTY_SECTOR * SPI_FLASH_SEC_SIZE, zeros, sizeof(zeros));
realPowerOnCounter = sizeof(zeros);
for (int i=0 ; i<sizeof(zeros) ; i++) {
if (zeros[i] != 0) {
realPowerOnCounter = i;
break;
}
}
}
return realPowerOnCounter;
}
#endif

void
RxConfig::Commit()
{
#if defined(PLATFORM_ESP8266)
if (erase_power_on_count)
{
ESP.flashEraseSector(EMPTY_SECTOR);
erase_power_on_count = false;
}
#endif
if (!m_modified)
{
// No changes
Expand Down Expand Up @@ -948,11 +981,25 @@ RxConfig::SetUID(uint8_t* uid)
void
RxConfig::SetPowerOnCounter(uint8_t powerOnCounter)
{
#if defined(PLATFORM_ESP8266)
realPowerOnCounter = powerOnCounter;
if (powerOnCounter == 0)
{
erase_power_on_count = true;
m_modified = true;
}
else
{
byte zeros[16] = {0};
ESP.flashWrite(EMPTY_SECTOR * SPI_FLASH_SEC_SIZE, zeros, std::min((size_t)powerOnCounter, sizeof(zeros)));
}
#else
if (m_config.powerOnCounter != powerOnCounter)
{
m_config.powerOnCounter = powerOnCounter;
m_modified = true;
}
#endif
}

void
Expand Down
4 changes: 4 additions & 0 deletions src/lib/CONFIG/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,11 @@ class RxConfig
// Getters
bool GetIsBound() const;
const uint8_t* GetUID() const { return m_config.uid; }
#if defined(PLATFORM_ESP8266)
uint8_t GetPowerOnCounter() const;
#else
uint8_t GetPowerOnCounter() const { return m_config.powerOnCounter; }
#endif
uint8_t GetModelId() const { return m_config.modelId; }
uint8_t GetPower() const { return m_config.power; }
uint8_t GetAntennaMode() const { return m_config.antennaMode; }
Expand Down
2 changes: 1 addition & 1 deletion src/src/rx_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1588,7 +1588,7 @@ static void updateBindingMode()
}

// If the power on counter is >=3, enter binding, the counter will be reset after 2s
else if (config.GetPowerOnCounter() >= 3)
else if (!InBindingMode && config.GetPowerOnCounter() >= 3)
{
#if defined(PLATFORM_ESP32) || defined(PLATFORM_ESP8266)
// Never enter wifi if forced to binding mode
Expand Down

0 comments on commit 8b606e1

Please sign in to comment.