Issues with Storing Template Structures in ESP32 RTC Slow Memory
Posted: Mon Feb 10, 2025 6:20 am
Dear Espressif Community,
I hope this message finds you well, and I hope this is not a duplicate, but I do feel I have a unique problem as searches along these lines do not return appropriate listings.
I am currently working on a project involving the ESP32 microcontroller, and I have encountered a persistent issue that I hope to get some advice on.
**Project Context:**
I am attempting to implement a circular buffer that persists across deep sleep cycles using the ESP32's RTC slow memory. The buffer is intended to accumulate data over time, even as the device enters and exits deep sleep.
**Approaches Tried:**
1. **Non-Template Structure:**
- I initially implemented the circular buffer using a standard integer array and a non-template structure. This approach successfully preserved the buffer contents across deep sleep cycles, and the buffer accumulated data as expected.
2. **Template Structure:**
- To make the buffer more flexible, I refactored the code to use a template structure, allowing the buffer to store different data types (e.g., `uint16_t`). However, I encountered an issue where the buffer contents did not persist across deep sleep cycles, and the buffer did not accumulate data as expected.
**Code Snippets:**
Here is a simplified version of the template structure and the initialization code:
```cpp
template<typename T, size_t Size>
struct circ_bbuf_t {
T buffer[Size];
int head;
int tail;
const int maxlen;
uint32_t items;
bool isFull;
circ_bbuf_t() : head(0), tail(0), maxlen(Size), items(0), isFull(false) {}
};
RTC_DATA_ATTR circ_bbuf_t<uint16_t, 10> circularBuffer;
RTC_DATA_ATTR bool bufferInitialized = false;
void setup() {
Serial.begin(115200);
esp_sleep_enable_timer_wakeup(5 * 1000000);
if (!bufferInitialized) {
for (size_t i = 0; i < 10; ++i) {
circularBuffer.buffer = 0;
}
circularBuffer.head = 0;
circularBuffer.tail = 0;
circularBuffer.items = 0;
circularBuffer.isFull = false;
bufferInitialized = true;
}
uint16_t value = millis() % 1000;
if (circ_bbuf_push(&circularBuffer, value) == 0) {
Serial.print("Added value to buffer: ");
Serial.println(value);
} else {
Serial.println("Buffer is full, cannot add value.");
}
Serial.println("Circular Buffer Contents:");
circ_bbuf_iterate(&circularBuffer, [](uint16_t val) {
Serial.print(val);
Serial.print(" ");
});
Serial.println();
Serial.print("Number of items in buffer: ");
Serial.println(circularBuffer.items);
Serial.println("Going to sleep...");
delay(1000);
esp_deep_sleep_start();
}
```
**Theory:**
I suspect that the ESP32's RTC slow memory, or the compiler/runtime, might have restrictions or issues with storing template structures. This could be causing the buffer contents to not persist across deep sleep cycles when using templates.
**Request for Advice:**
I would greatly appreciate any insights or advice on the following:
1. Are there known issues or restrictions with storing template structures in the ESP32's RTC slow memory?
2. Are there any alternative approaches or solutions that we have not tried?
3. Any specific compiler or runtime settings that might help resolve this issue?
Thank you in advance for your time and assistance. I look forward to any suggestions or guidance you can provide.
regards,
Jonas
wompusi@gmail.com
I hope this message finds you well, and I hope this is not a duplicate, but I do feel I have a unique problem as searches along these lines do not return appropriate listings.
I am currently working on a project involving the ESP32 microcontroller, and I have encountered a persistent issue that I hope to get some advice on.
**Project Context:**
I am attempting to implement a circular buffer that persists across deep sleep cycles using the ESP32's RTC slow memory. The buffer is intended to accumulate data over time, even as the device enters and exits deep sleep.
**Approaches Tried:**
1. **Non-Template Structure:**
- I initially implemented the circular buffer using a standard integer array and a non-template structure. This approach successfully preserved the buffer contents across deep sleep cycles, and the buffer accumulated data as expected.
2. **Template Structure:**
- To make the buffer more flexible, I refactored the code to use a template structure, allowing the buffer to store different data types (e.g., `uint16_t`). However, I encountered an issue where the buffer contents did not persist across deep sleep cycles, and the buffer did not accumulate data as expected.
**Code Snippets:**
Here is a simplified version of the template structure and the initialization code:
```cpp
template<typename T, size_t Size>
struct circ_bbuf_t {
T buffer[Size];
int head;
int tail;
const int maxlen;
uint32_t items;
bool isFull;
circ_bbuf_t() : head(0), tail(0), maxlen(Size), items(0), isFull(false) {}
};
RTC_DATA_ATTR circ_bbuf_t<uint16_t, 10> circularBuffer;
RTC_DATA_ATTR bool bufferInitialized = false;
void setup() {
Serial.begin(115200);
esp_sleep_enable_timer_wakeup(5 * 1000000);
if (!bufferInitialized) {
for (size_t i = 0; i < 10; ++i) {
circularBuffer.buffer = 0;
}
circularBuffer.head = 0;
circularBuffer.tail = 0;
circularBuffer.items = 0;
circularBuffer.isFull = false;
bufferInitialized = true;
}
uint16_t value = millis() % 1000;
if (circ_bbuf_push(&circularBuffer, value) == 0) {
Serial.print("Added value to buffer: ");
Serial.println(value);
} else {
Serial.println("Buffer is full, cannot add value.");
}
Serial.println("Circular Buffer Contents:");
circ_bbuf_iterate(&circularBuffer, [](uint16_t val) {
Serial.print(val);
Serial.print(" ");
});
Serial.println();
Serial.print("Number of items in buffer: ");
Serial.println(circularBuffer.items);
Serial.println("Going to sleep...");
delay(1000);
esp_deep_sleep_start();
}
```
**Theory:**
I suspect that the ESP32's RTC slow memory, or the compiler/runtime, might have restrictions or issues with storing template structures. This could be causing the buffer contents to not persist across deep sleep cycles when using templates.
**Request for Advice:**
I would greatly appreciate any insights or advice on the following:
1. Are there known issues or restrictions with storing template structures in the ESP32's RTC slow memory?
2. Are there any alternative approaches or solutions that we have not tried?
3. Any specific compiler or runtime settings that might help resolve this issue?
Thank you in advance for your time and assistance. I look forward to any suggestions or guidance you can provide.
regards,
Jonas
wompusi@gmail.com