cleaned up RTC initialisation. fixed some fuzzy issues after flashing/factory reset. improved flash-counter messages and added powercycle tips

This commit is contained in:
Edwin Eefting
2017-06-01 01:38:19 +02:00
parent 1a9eedc6d6
commit fb83badfce
4 changed files with 62 additions and 37 deletions
+1 -6
View File
@@ -86,12 +86,7 @@ void ExecuteCommand(byte source, const char *Line)
if (strcasecmp_P(Command, PSTR("clearRTCRAM")) == 0)
{
success = true;
RTC.factoryResetCounter = 0;
RTC.deepSleepState = 0;
RTC.rebootCounter = 0;
RTC.flashDayCounter = 0;
RTC.flashCounter = 0;
saveToRTC();
initRTC();
}
if (strcasecmp_P(Command, PSTR("notify")) == 0)
+19 -10
View File
@@ -552,18 +552,26 @@ struct pinStatesStruct
uint16_t value;
} pinStates[PINSTATE_TABLE_MAX];
// this offsets are in blocks, bytes = blocks * 4
#define RTC_BASE_STRUCT 64
#define RTC_BASE_USERVAR 74
//max 40 bytes: ( 74 - 64 ) * 4
struct RTCStruct
{
byte ID1;
byte ID2;
boolean valid; // not used?
boolean unused1;
byte factoryResetCounter;
byte deepSleepState;
byte rebootCounter; //not used yet?
byte unused2;
byte flashDayCounter;
unsigned long flashCounter;
unsigned long bootCounter;
} RTC;
int deviceCount = -1;
int protocolCount = -1;
int notificationCount = -1;
@@ -640,6 +648,7 @@ byte lowestRAMid=0;
\*********************************************************************************************/
void setup()
{
lowestRAM = FreeMem();
Serial.begin(115200);
@@ -743,24 +752,24 @@ void setup()
//warm boot
if (readFromRTC())
{
RTC.bootCounter++;
readUserVarFromRTC();
if (RTC.deepSleepState == 1)
{
log = F("INIT : Rebooted from deepsleep");
log = F("INIT : Rebooted from deepsleep #");
lastBootCause=BOOT_CAUSE_DEEP_SLEEP;
}
else
log = F("INIT : Normal boot");
log = F("INIT : Warm boot #");
log += RTC.bootCounter;
}
//cold boot (RTC memory empty)
else
{
RTC.factoryResetCounter=0;
RTC.deepSleepState=0;
RTC.rebootCounter=0;
RTC.flashDayCounter=0;
RTC.flashCounter=0;
saveToRTC();
initRTC();
// cold boot situation
if (lastBootCause == BOOT_CAUSE_MANUAL_REBOOT) // only set this if not set earlier during boot stage.
+38 -20
View File
@@ -522,10 +522,18 @@ void BuildFixes()
\*********************************************************************************************/
void fileSystemCheck()
{
addLog(LOG_LEVEL_INFO, F("FS : Mounting..."));
if (SPIFFS.begin())
{
String log = F("FS : Mount successful");
fs::FSInfo fs_info;
SPIFFS.info(fs_info);
String log = F("FS : Mount successful, used ");
log=log+fs_info.usedBytes;
log=log+F(" bytes of ");
log=log+fs_info.totalBytes;
addLog(LOG_LEVEL_INFO, log);
fs::File f = SPIFFS.open("config.dat", "r");
if (!f)
{
@@ -537,6 +545,7 @@ void fileSystemCheck()
String log = F("FS : Mount failed");
Serial.println(log);
addLog(LOG_LEVEL_ERROR, log);
ResetFactory();
}
}
@@ -811,7 +820,7 @@ boolean SaveToFile(char* fname, int index, byte* memAddress, int datasize)
if (RTC.flashDayCounter > MAX_FLASHWRITES_PER_DAY)
{
String log = F("FS : Daily flash write rate exceeded!");
String log = F("FS : Daily flash write rate exceeded! (powercycle to reset this)");
addLog(LOG_LEVEL_ERROR, log);
return false;
}
@@ -874,33 +883,35 @@ void ResetFactory(void)
{
// Direct Serial is allowed here, since this is only an emergency task.
Serial.println(F("Resetting factory defaults..."));
Serial.println(F("RESET: Resetting factory defaults..."));
delay(1000);
if (readFromRTC())
{
Serial.print(F("RESET: Reboot count: "));
Serial.print(F("RESET: Warm boot, reset count: "));
Serial.println(RTC.factoryResetCounter);
if (RTC.factoryResetCounter > 3)
{
Serial.println(F("RESET: To many reset attempts"));
Serial.println(F("RESET: Too many resets, protecting your flash memory (powercycle to solve this)"));
return;
}
}
else
{
Serial.println(F("RESET: Cold boot"));
initRTC();
}
RTC.factoryResetCounter++;
RTC.flashDayCounter = 0;
saveToRTC();
//always format on factory reset, in case of corrupt SPIFFS
SPIFFS.end();
Serial.println(F("FS : formatting..."));
Serial.println(F("RESET: formatting..."));
SPIFFS.format();
Serial.println(F("FS : formatting done..."));
Serial.println(F("RESET: formatting done..."));
if (!SPIFFS.begin())
{
Serial.println(F("FS : FORMATTING SPIFFS FAILED!"));
Serial.println(F("RESET: FORMAT SPIFFS FAILED!"));
return;
}
@@ -984,7 +995,8 @@ void ResetFactory(void)
Settings.Build = BUILD;
Settings.UseSerial = true;
SaveSettings();
Serial.println("Factory reset succesful, rebooting...");
Serial.println("RESET: Succesful, rebooting. (you might need to press the reset button if you've justed flashed the firmware)");
//NOTE: this is a known ESP8266 bug, not our fault. :)
delay(1000);
WiFi.persistent(true); // use SDK storage of SSID/WPA parameters
WiFi.disconnect(); // this will store empty ssid/wpa into sdk storage
@@ -1128,17 +1140,26 @@ void delayedReboot(int rebootDelay)
/********************************************************************************************\
Save RTC struct to RTC memory
\*********************************************************************************************/
//user-area of the esp starts at block 64 (bytes = block * 4)
#define RTC_BASE_STRUCT 64
boolean saveToRTC()
{
RTC.ID1 = 0xAA;
RTC.ID2 = 0x55;
RTC.valid = true;
return(system_rtc_mem_write(RTC_BASE_STRUCT, (byte*)&RTC, sizeof(RTC)));
}
/********************************************************************************************\
Initialize RTC memory
\*********************************************************************************************/
void initRTC()
{
memset(&RTC, 0, sizeof(RTC));
RTC.ID1 = 0xAA;
RTC.ID2 = 0x55;
saveToRTC();
memset(&UserVar, 0, sizeof(UserVar));
saveUserVarToRTC();
}
/********************************************************************************************\
Read RTC struct from RTC memory
\*********************************************************************************************/
@@ -1148,18 +1169,15 @@ boolean readFromRTC()
return(false);
if (RTC.ID1 == 0xAA && RTC.ID2 == 0x55)
{
RTC.valid = false;
return true;
}
return false;
else
return false;
}
/********************************************************************************************\
Save values to RTC memory
\*********************************************************************************************/
#define RTC_BASE_USERVAR 74
boolean saveUserVarToRTC()
{
//addLog(LOG_LEVEL_DEBUG, F("RTCMEM: saveUserVarToRTC"));
+4 -1
View File
@@ -3377,7 +3377,7 @@ void handle_rules() {
if (RTC.flashDayCounter > MAX_FLASHWRITES_PER_DAY)
{
String log = F("FS : Daily flash write rate exceeded!");
String log = F("FS : Daily flash write rate exceeded! (powercyle to reset this)");
addLog(LOG_LEVEL_ERROR, log);
reply += F("<span style=\"color:red\">Error saving to flash!</span>");
}
@@ -3603,6 +3603,9 @@ void handle_sysinfo() {
break;
}
reply += F("<TR><TD>Warm boot count:<TD>");
reply += RTC.bootCounter;
reply += F("<TR><TD>STA MAC:<TD>");
uint8_t mac[] = {0, 0, 0, 0, 0, 0};
uint8_t* macread = WiFi.macAddress(mac);