|
1 |
| -// Example: Storing struct data in RTC user memory |
| 1 | +// Example: Storing struct data in RTC user rtcDataory |
2 | 2 | //
|
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. |
5 | 10 | //
|
6 | 11 | // Created Mar 30, 2016 by Macro Yau.
|
7 | 12 | //
|
8 | 13 | // This example code is in the public domain.
|
9 | 14 |
|
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); |
13 | 17 |
|
14 |
| -rtcUserMemory mem; |
| 18 | +// helper function to dump memory contents as hex |
| 19 | +void printMemory(); |
15 | 20 |
|
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; |
26 | 30 |
|
27 | 31 | void setup() {
|
28 | 32 | Serial.begin(115200);
|
29 | 33 | Serial.println();
|
| 34 | + delay(1000); |
30 | 35 |
|
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 | + } |
34 | 52 | }
|
35 | 53 |
|
36 | 54 | // 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); |
39 | 57 | }
|
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(); |
44 | 65 | }
|
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); |
48 | 69 | }
|
49 | 70 |
|
50 | 71 | 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(); |
52 | 106 | }
|
0 commit comments