Skip to content

Commit 9db0393

Browse files
committed
Update RTC memory example
1 parent 4b43860 commit 9db0393

File tree

1 file changed

+84
-30
lines changed

1 file changed

+84
-30
lines changed
Lines changed: 84 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,106 @@
1-
// Example: Storing struct data in RTC user memory
1+
// Example: Storing struct data in RTC user rtcDataory
22
//
3-
// Struct data with the maximum size of 512 bytes can be stored in the RTC user memory using the ESP-specifc APIs.
4-
// The stored data can be retained between deep sleep cycles. However, the data might be lost after power cycling the ESP8266.
3+
// Struct data with the maximum size of 512 bytes can be stored
4+
// in the RTC user rtcDataory using the ESP-specifc APIs.
5+
// The stored data can be retained between deep sleep cycles.
6+
// However, the data might be lost after power cycling the ESP8266.
7+
//
8+
// This example uses deep sleep mode, so connect GPIO16 and RST
9+
// pins before running it.
510
//
611
// Created Mar 30, 2016 by Macro Yau.
712
//
813
// This example code is in the public domain.
914

10-
typedef struct {
11-
byte data[512];
12-
} rtcUserMemory;
15+
// CRC function used to ensure data validity
16+
uint32_t calculateCRC32(const uint8_t *data, size_t length);
1317

14-
rtcUserMemory mem;
18+
// helper function to dump memory contents as hex
19+
void printMemory();
1520

16-
void printMemory(bool readFromRtc) {
17-
char buf[3];
18-
Serial.print(readFromRtc ? "Read: " : "Write: ");
19-
for (int i = 0; i < sizeof(mem); i++) {
20-
sprintf(buf, "%02X", mem.data[i]);
21-
Serial.print(buf);
22-
Serial.print(" ");
23-
}
24-
Serial.println();
25-
}
21+
// Structure which will be stored in RTC memory.
22+
// First field is CRC32, which is calculated based on the
23+
// rest of structure contents.
24+
// Any fields can go after CRC32.
25+
// We use byte array as an example.
26+
struct {
27+
uint32_t crc32;
28+
byte data[508];
29+
} rtcData;
2630

2731
void setup() {
2832
Serial.begin(115200);
2933
Serial.println();
34+
delay(1000);
3035

31-
// Read struct from RTC user memory
32-
if (ESP.rtcUserMemoryRead((uint32_t*) &mem, sizeof(mem))) {
33-
printMemory(true);
36+
// Read struct from RTC memory
37+
if (ESP.rtcUserMemoryRead(0, (uint32_t*) &rtcData, sizeof(rtcData))) {
38+
Serial.println("Read: ");
39+
printMemory();
40+
Serial.println();
41+
uint32_t crcOfData = calculateCRC32(((uint8_t*) &rtcData) + 4, sizeof(rtcData) - 4);
42+
Serial.print("CRC32 of data: ");
43+
Serial.println(crcOfData, HEX);
44+
Serial.print("CRC32 read from RTC: ");
45+
Serial.println(rtcData.crc32, HEX);
46+
if (crcOfData != rtcData.crc32) {
47+
Serial.println("CRC32 in RTC memory doesn't match CRC32 of data. Data is probably invalid!");
48+
}
49+
else {
50+
Serial.println("CRC32 check ok, data is probably valid.");
51+
}
3452
}
3553

3654
// Generate new data set for the struct
37-
for (int i = 0; i < sizeof(mem); i++) {
38-
mem.data[i] = random(0, 128);
55+
for (int i = 0; i < sizeof(rtcData); i++) {
56+
rtcData.data[i] = random(0, 128);
3957
}
40-
41-
// Write struct to RTC user memory
42-
if (ESP.rtcUserMemoryWrite((uint32_t*) &mem, sizeof(mem))) {
43-
printMemory(false);
58+
// Update CRC32 of data
59+
rtcData.crc32 = calculateCRC32(((uint8_t*) &rtcData) + 4, sizeof(rtcData) - 4);
60+
// Write struct to RTC memory
61+
if (ESP.rtcUserMemoryWrite(0, (uint32_t*) &rtcData, sizeof(rtcData))) {
62+
Serial.println("Write: ");
63+
printMemory();
64+
Serial.println();
4465
}
45-
46-
// Enter deep sleep mode for 10 seconds
47-
ESP.deepSleep(10e6);
66+
67+
Serial.println("Going into deep sleep for 5 seconds");
68+
ESP.deepSleep(5e6);
4869
}
4970

5071
void loop() {
51-
72+
}
73+
74+
uint32_t calculateCRC32(const uint8_t *data, size_t length)
75+
{
76+
uint32_t crc = 0xffffffff;
77+
while (length--) {
78+
uint8_t c = *data++;
79+
for (uint32_t i = 0x80; i > 0; i >>= 1) {
80+
bool bit = crc & 0x80000000;
81+
if (c & i) {
82+
bit = !bit;
83+
}
84+
crc <<= 1;
85+
if (bit) {
86+
crc ^= 0x04c11db7;
87+
}
88+
}
89+
}
90+
return crc;
91+
}
92+
93+
void printMemory() {
94+
char buf[3];
95+
for (int i = 0; i < sizeof(rtcData); i++) {
96+
sprintf(buf, "%02X", rtcData.data[i]);
97+
Serial.print(buf);
98+
if ((i + 1) % 32 == 0) {
99+
Serial.println();
100+
}
101+
else {
102+
Serial.print(" ");
103+
}
104+
}
105+
Serial.println();
52106
}

0 commit comments

Comments
 (0)