Skip to content

Instantly share code, notes, and snippets.

@murilopontes
Created May 31, 2016 12:15
Show Gist options
  • Save murilopontes/b7ec396c8876f44cdcc757b08de5b180 to your computer and use it in GitHub Desktop.
Save murilopontes/b7ec396c8876f44cdcc757b08de5b180 to your computer and use it in GitHub Desktop.
ESP rtc user data
void setup(){
#define RTC_MEM ((volatile uint32_t*)0x60001200)
#define RTC_USER_DATA_START_OFFSET 32
#define RTC_USER_DATA_END_OFFSET 126
#define RTC_USER_DATA_CRC_OFFSET 127
//print current values
for(int i=0;i<128;i++){
Serial.printf("offset %d: %04x\r\n",i,RTC_MEM[i]);
}
//calculate SUM CRC (from user data of previous boot)
uint32_t sum_crc=0;
for(int i=RTC_USER_DATA_START_OFFSET;i<=RTC_USER_DATA_END_OFFSET;i++){
sum_crc += RTC_MEM[i];
}
sum_crc = ~sum_crc;
//if USER_DATA is corrupted (SUM CRC diff from stored SUM CRC)-> initialize USER_DATA
if(RTC_MEM[RTC_USER_DATA_CRC_OFFSET] != sum_crc){
for(int i=RTC_USER_DATA_START_OFFSET;i<=RTC_USER_DATA_END_OFFSET;i++) RTC_MEM[i]=0;
}
//update other user data (any offset between RTC_USER_DATA_START_OFFSET and RTC_USER_DATA_END_OFFSET)
for(int i=RTC_USER_DATA_START_OFFSET;i<=RTC_USER_DATA_END_OFFSET;i++) RTC_MEM[i]++;
//update SUM CRC (will be used to verify user data in next boot)
RTC_MEM[RTC_USER_DATA_CRC_OFFSET]=0;
for(int i=RTC_USER_DATA_START_OFFSET;i<=RTC_USER_DATA_END_OFFSET;i++){
RTC_MEM[RTC_USER_DATA_CRC_OFFSET] += RTC_MEM[i];
}
RTC_MEM[RTC_USER_DATA_CRC_OFFSET]= ~RTC_MEM[RTC_USER_DATA_CRC_OFFSET];
//enter into deepsleep
ESP.deepSleep(1 * 1000000);
}
void loop(){
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment