Skip to content

Commit 2a4081b

Browse files
MacroYauigrr
authored andcommitted
Added support for RTC user memory in ESP-specific APIs. (#1836)
1 parent 974b9ae commit 2a4081b

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

cores/esp8266/Esp.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,24 @@ void EspClass::deepSleep(uint32_t time_us, WakeMode mode)
112112
esp_yield();
113113
}
114114

115+
bool EspClass::rtcUserMemoryRead(uint32_t *data, size_t size)
116+
{
117+
if (size > 512) {
118+
return false;
119+
} else {
120+
return system_rtc_mem_read(64, data, size);
121+
}
122+
}
123+
124+
bool EspClass::rtcUserMemoryWrite(uint32_t *data, size_t size)
125+
{
126+
if (size > 512) {
127+
return false;
128+
} else {
129+
return system_rtc_mem_write(64, data, size);
130+
}
131+
}
132+
115133
extern "C" void __real_system_restart_local();
116134
void EspClass::reset(void)
117135
{

cores/esp8266/Esp.h

+3
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ class EspClass {
9494

9595
void deepSleep(uint32_t time_us, RFMode mode = RF_DEFAULT);
9696

97+
bool rtcUserMemoryRead(uint32_t *data, size_t size);
98+
bool rtcUserMemoryWrite(uint32_t *data, size_t size);
99+
97100
void reset();
98101
void restart();
99102

doc/libraries.md

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ APIs related to deep sleep and watchdog timer are available in the `ESP` object,
8383

8484
`ESP.deepSleep(microseconds, mode)` will put the chip into deep sleep. `mode` is one of `WAKE_RF_DEFAULT`, `WAKE_RFCAL`, `WAKE_NO_RFCAL`, `WAKE_RF_DISABLED`. (GPIO16 needs to be tied to RST to wake from deepSleep.)
8585

86+
`ESP.rtcUserMemoryWrite(&data, sizeof(data))` and `ESP.rtcUserMemoryRead(&data, sizeof(data))` allow struct data with the maximum size of 512 bytes to be stored and retrieved from the RTC user memory of the chip respectively. The stored data can be retained between deep sleep cycles. However, the data might be lost after power cycling the chip.
87+
8688
`ESP.restart()` restarts the CPU.
8789

8890
`ESP.getResetReason()` returns String containing the last reset resaon in human readable format.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Example: Storing struct data in RTC user memory
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.
5+
//
6+
// Created Mar 30, 2016 by Macro Yau.
7+
//
8+
// This example code is in the public domain.
9+
10+
typedef struct {
11+
byte data[512];
12+
} rtcUserMemory;
13+
14+
rtcUserMemory mem;
15+
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+
}
26+
27+
void setup() {
28+
Serial.begin(115200);
29+
Serial.println();
30+
31+
// Read struct from RTC user memory
32+
if (ESP.rtcUserMemoryRead((uint32_t*) &mem, sizeof(mem))) {
33+
printMemory(true);
34+
}
35+
36+
// Generate new data set for the struct
37+
for (int i = 0; i < sizeof(mem); i++) {
38+
mem.data[i] = random(0, 128);
39+
}
40+
41+
// Write struct to RTC user memory
42+
if (ESP.rtcUserMemoryWrite((uint32_t*) &mem, sizeof(mem))) {
43+
printMemory(false);
44+
}
45+
46+
// Enter deep sleep mode for 10 seconds
47+
ESP.deepSleep(10e6);
48+
}
49+
50+
void loop() {
51+
52+
}

0 commit comments

Comments
 (0)