Post topics, source code that relate to the Arduino Platform

User avatar
By zoidicus
#19974 I want to use deep-sleep mode, but I need to retain state through that event. Since the module resets after deep sleep, only the RTC memory remains untouched (or so I read). As I understand it, the RTC User-Memory begins at address 60001200h. I have not found any examples (yet) that demonstrate preserving state through the deep-sleep-wake cycle.

I quickly hacked out the following code, which appears to work. It successfully saves a value prior to a deep sleep and retrieves it after waking up. However, I noticed that the memory location contains a consistent non-zero value following a power-up, leading me to believe it is being initialized by something. I did not expect that and I'm wondering if I'm stomping on a piece of memory used by the Esp8266-Arduino libraries, or the RTC itself.

Am I doing this right? And am I playing in a safe bit of the RTC RAM?

Using Esp8266/Arduino 1.6.4:
Code: Select all#include <ESP8266WiFi.h>
const unsigned long SLEEP_INTERVAL = 20 * 1000 * 1000; // 20 sec

void setup() {

  Serial.println("RTC Memory Test");
  long *rtcMemory = (long *)0x60001200;
  Serial.print("current value = ");

  Serial.print("new value = ");


void loop() {
   // should never get here
User avatar
By torntrousers
#20009 There are some system calls for doing this - system_rtc_mem_read and system_rtc_mem_write. They're described in the SDK guide and that shows you the memory layout. There is also an example of using the calls in the back of the guide in section 8.2. "RTC APIs Example.", its not an Arduino example but it gives you the general idea.

I don't know what the "official" place to find these programming guides is but if you google "esp8266 sdk programming guide" you find pages like this and from that a direct link to the 1.0.1 sdk guide is here.

A minimal example of using them in an Arduino sketch is:
Code: Select allextern "C" {
  #include "user_interface.h"
void setup() {
  byte rtcStore[2];
  system_rtc_mem_read(64, rtcStore, 2);
  system_rtc_mem_write(64, rtcStore, 2);
void loop() {}
User avatar
By RexBrown
#55392 Thanks very much to torntrousers! I found that this method of accessing the RTC memory works well in the version of the ESP8266 WIFI I am using (Huzzah Feather). Also the SDK manual you reference is very useful for identifying functions that are available.

I notice that there is an api for accessing SPI Flash. I am wondering if that requires an add on board to provide the flash via SPI or if there is actually an SPI flash provided in the Huzzah Feather module. Thanks for any insight into this.
User avatar
By scargill
#57381 SPI Flash memory (normally used for programs) can indeed be accessed for storing info - you need ot make sure you steer clear of program space, space used for OTA (the other copy of the program) and any system or user non-volatile storage - so on an ESP-12 - the start of the last 1-meg block would be good - I can say this only from the perspective of a C programmer as goodness knows where the likes of LUA stores things.