1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-06 05:21:22 +03:00

Update RTC memory example

This commit is contained in:
Ivan Grokhotkov 2016-06-02 14:34:28 +08:00
parent 4b43860276
commit 9db0393b65

View File

@ -1,52 +1,106 @@
// Example: Storing struct data in RTC user memory // Example: Storing struct data in RTC user rtcDataory
// //
// Struct data with the maximum size of 512 bytes can be stored in the RTC user memory using the ESP-specifc APIs. // Struct data with the maximum size of 512 bytes can be stored
// The stored data can be retained between deep sleep cycles. However, the data might be lost after power cycling the ESP8266. // in the RTC user rtcDataory using the ESP-specifc APIs.
// The stored data can be retained between deep sleep cycles.
// However, the data might be lost after power cycling the ESP8266.
//
// This example uses deep sleep mode, so connect GPIO16 and RST
// pins before running it.
// //
// Created Mar 30, 2016 by Macro Yau. // Created Mar 30, 2016 by Macro Yau.
// //
// This example code is in the public domain. // This example code is in the public domain.
typedef struct { // CRC function used to ensure data validity
byte data[512]; uint32_t calculateCRC32(const uint8_t *data, size_t length);
} rtcUserMemory;
rtcUserMemory mem; // helper function to dump memory contents as hex
void printMemory();
void printMemory(bool readFromRtc) { // Structure which will be stored in RTC memory.
char buf[3]; // First field is CRC32, which is calculated based on the
Serial.print(readFromRtc ? "Read: " : "Write: "); // rest of structure contents.
for (int i = 0; i < sizeof(mem); i++) { // Any fields can go after CRC32.
sprintf(buf, "%02X", mem.data[i]); // We use byte array as an example.
Serial.print(buf); struct {
Serial.print(" "); uint32_t crc32;
} byte data[508];
Serial.println(); } rtcData;
}
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
Serial.println(); Serial.println();
delay(1000);
// Read struct from RTC user memory // Read struct from RTC memory
if (ESP.rtcUserMemoryRead((uint32_t*) &mem, sizeof(mem))) { if (ESP.rtcUserMemoryRead(0, (uint32_t*) &rtcData, sizeof(rtcData))) {
printMemory(true); Serial.println("Read: ");
printMemory();
Serial.println();
uint32_t crcOfData = calculateCRC32(((uint8_t*) &rtcData) + 4, sizeof(rtcData) - 4);
Serial.print("CRC32 of data: ");
Serial.println(crcOfData, HEX);
Serial.print("CRC32 read from RTC: ");
Serial.println(rtcData.crc32, HEX);
if (crcOfData != rtcData.crc32) {
Serial.println("CRC32 in RTC memory doesn't match CRC32 of data. Data is probably invalid!");
}
else {
Serial.println("CRC32 check ok, data is probably valid.");
}
} }
// Generate new data set for the struct // Generate new data set for the struct
for (int i = 0; i < sizeof(mem); i++) { for (int i = 0; i < sizeof(rtcData); i++) {
mem.data[i] = random(0, 128); rtcData.data[i] = random(0, 128);
}
// Update CRC32 of data
rtcData.crc32 = calculateCRC32(((uint8_t*) &rtcData) + 4, sizeof(rtcData) - 4);
// Write struct to RTC memory
if (ESP.rtcUserMemoryWrite(0, (uint32_t*) &rtcData, sizeof(rtcData))) {
Serial.println("Write: ");
printMemory();
Serial.println();
} }
// Write struct to RTC user memory Serial.println("Going into deep sleep for 5 seconds");
if (ESP.rtcUserMemoryWrite((uint32_t*) &mem, sizeof(mem))) { ESP.deepSleep(5e6);
printMemory(false);
}
// Enter deep sleep mode for 10 seconds
ESP.deepSleep(10e6);
} }
void loop() { void loop() {
}
uint32_t calculateCRC32(const uint8_t *data, size_t length)
{
uint32_t crc = 0xffffffff;
while (length--) {
uint8_t c = *data++;
for (uint32_t i = 0x80; i > 0; i >>= 1) {
bool bit = crc & 0x80000000;
if (c & i) {
bit = !bit;
}
crc <<= 1;
if (bit) {
crc ^= 0x04c11db7;
}
}
}
return crc;
}
void printMemory() {
char buf[3];
for (int i = 0; i < sizeof(rtcData); i++) {
sprintf(buf, "%02X", rtcData.data[i]);
Serial.print(buf);
if ((i + 1) % 32 == 0) {
Serial.println();
}
else {
Serial.print(" ");
}
}
Serial.println();
} }