DS_CAP_SYS_RTCMEM - denis-stepanov/esp-ds-system GitHub Wiki

DS_CAP_SYS_RTCMEM — RTC Memory

Description

This capability adds support for reading and writing RTC memory (a small memory region of 512 bytes surviving deep sleep). The following methods are available:

// Read 'num' 4 bytes slots from RTC memory offset 'idx' into 'result'
bool System::getRTCMem(uint32_t* result, const uint8_t idx = 0, const uint8_t num = 1);

// Store 'num' 4 bytes slots into RTC memory offset 'idx' from 'source'
bool System::setRTCMem(const uint32_t* source, const uint8_t idx = 0, const uint8_t num = 1);

The API is inspired by NodeMCU Lua 'rtcmem' module. The memory is divided in "slots" of four bytes each, addressable with the index idx, starting from 0. The number of slots to read or write is given by the num argument. Operations return true on success. The functions are just thin interface to built-in ESP.rtcUserMemoryRead() / ESP.rtcUserMemoryWrite() functions.

Requires

Cooperates With

None.

Conflicts With

None.

Usage

MySystem.h:

#define DS_CAP_SYS_RTCMEM   // Enable RTC memory

#include "System.h"         // System global definitions

sketch.ino:

#include "MySystem.h"

using namespace ds;

void setup() {

    // Send one number (four bytes) into RTC memory
    uint32_t number_to_write = 3;
    auto ret = System::setRTCMem(&number_to_write);

    // Fetch stored number from RTC memory
    uint32_t restored_number;
    ret = System::getRTCMem(&restored_number);

    // Example of storing an array with an offset of 10 slots (i.e., starting from 40th byte of RTC)
    uint32_t data_to_write[3] = {3, 14, 159};
    ret = System::setRTCMem(data_to_write, 10, 3);
    uint32_t restored_data[3];
    ret = System::getRTCMem(restored_data, 10, 3);
}

void loop() {
}

Mandatory Calls

System::begin() Not required
System::update() Not required

Examples

Bugs

  • This API is not very convenient to store strings. This may be added in future (issue #8)

Availability

Version 1.0 or later.

See Also

None.

⚠️ **GitHub.com Fallback** ⚠️