-->
Page 1 of 1

RTC memory access

PostPosted: Tue Jun 09, 2015 4:40 pm
by zoidicus
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.begin(115200);
  Serial.println();

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


  (*rtcMemory)++;
  Serial.print("new value = ");
  Serial.println(*rtcMemory);
 
  //sleep modes:  WAKE_RF_DEFAULT, WAKE_RFCAL, WAKE_NO_RFCAL, WAKE_RF_DISABLED
  ESP.deepSleep(SLEEP_INTERVAL - micros(), WAKE_RF_DISABLED);

  delay(1000);
}

void loop() {
   // should never get here
}

Re: RTC memory access

PostPosted: Wed Jun 10, 2015 2:35 am
by torntrousers
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() {}

Re: RTC memory access

PostPosted: Mon Sep 19, 2016 10:35 am
by RexBrown
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.

Re: RTC memory access

PostPosted: Sun Oct 30, 2016 7:18 am
by scargill
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.