Skip to content

Instantly share code, notes, and snippets.

@msraynsford
Created March 12, 2018 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msraynsford/d1669644bde063c7514ff9e66c14aa9b to your computer and use it in GitHub Desktop.
Save msraynsford/d1669644bde063c7514ff9e66c14aa9b to your computer and use it in GitHub Desktop.
#include <EEPROM.h>
#define FLAGSET 0x55
#define FLAGCLEAR 0xAA
#define FLAGADDRESS 00
#define FLAGTIMEOUT 3000
uint32_t flag = FLAGCLEAR;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200); Serial.println();
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
//Activate the EEPROM before attempting to use it
EEPROM.begin(sizeof(flag));
//Check to see if the flag is still set from the previous boot
if(checkFlag()) {
//Clear the flag now it has been detected
writeFlag(FLAGCLEAR);
//Do the firmware reset here
Serial.println("Flag Detected");
digitalWrite(LED_BUILTIN, LOW);
}
else {
//Set the flag for detection with the next boot
writeFlag(FLAGSET);
}
// After setup, while away the end of the reset period
while (millis() < FLAGTIMEOUT);
writeFlag(FLAGCLEAR);
Serial.println("Flag Checking Stopped");
}
void loop() {
// put your main code here, to run repeatedly:
}
bool checkFlag() {
flag = EEPROM.read(FLAGADDRESS);
return flag == FLAGSET;
}
void writeFlag(uint32_t value) {
flag = value;
EEPROM.write(FLAGADDRESS, flag);
EEPROM.commit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment