Skip to content

Commit e0afa32

Browse files
committed
use RTC_DATA_ATTR; address review comments
1 parent 39124b8 commit e0afa32

File tree

4 files changed

+27
-56
lines changed

4 files changed

+27
-56
lines changed

ports/esp32s2/common-hal/alarm/SleepMemory.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,33 +30,37 @@
3030
#include "py/runtime.h"
3131
#include "common-hal/alarm/SleepMemory.h"
3232

33+
#include "esp_log.h"
3334
#include "esp_sleep.h"
3435

36+
// Data storage for singleton instance of SleepMemory.
37+
// Might be RTC_SLOW_MEM or RTC_FAST_MEM, depending on setting of CONFIG_ESP32S2_RTCDATA_IN_FAST_MEM.
38+
static RTC_DATA_ATTR uint8_t _sleep_mem[SLEEP_MEMORY_LENGTH];
39+
3540
void alarm_sleep_memory_reset(void) {
36-
// Power RTC slow memory during deep sleep
37-
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_ON);
41+
// ESP-IDF build system takes care of doing esp_sleep_pd_config() or the equivalentwith
42+
// the correct settings, depending on which RTC mem we are using.
43+
// https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/api-reference/system/sleep_modes.html#power-down-of-rtc-peripherals-and-memories
3844
}
3945

4046
uint32_t common_hal_alarm_sleep_memory_get_length(alarm_sleep_memory_obj_t *self) {
41-
return SLEEP_MEMORY_LENGTH;
47+
return sizeof(_sleep_mem);
4248
}
4349

44-
bool common_hal_alarm_sleep_memory_set_bytes(alarm_sleep_memory_obj_t *self,
45-
uint32_t start_index, uint8_t* values, uint32_t len) {
50+
bool common_hal_alarm_sleep_memory_set_bytes(alarm_sleep_memory_obj_t *self, uint32_t start_index, const uint8_t* values, uint32_t len) {
4651

47-
if (start_index + len > SLEEP_MEMORY_LENGTH) {
52+
if (start_index + len > sizeof(_sleep_mem)) {
4853
return false;
4954
}
5055

51-
memcpy((uint8_t *) (SLEEP_MEMORY_BASE + start_index), values, len);
56+
memcpy((uint8_t *) (_sleep_mem + start_index), values, len);
5257
return true;
5358
}
5459

55-
void common_hal_alarm_sleep_memory_get_bytes(alarm_sleep_memory_obj_t *self,
56-
uint32_t start_index, uint32_t len, uint8_t* values) {
60+
void common_hal_alarm_sleep_memory_get_bytes(alarm_sleep_memory_obj_t *self, uint32_t start_index, uint8_t* values, uint32_t len) {
5761

58-
if (start_index + len > SLEEP_MEMORY_LENGTH) {
62+
if (start_index + len > sizeof(_sleep_mem)) {
5963
return;
6064
}
61-
memcpy(values, (uint8_t *) (SLEEP_MEMORY_BASE + start_index), len);
65+
memcpy(values, (uint8_t *) (_sleep_mem + start_index), len);
6266
}

ports/esp32s2/common-hal/alarm/SleepMemory.h

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,15 @@
3434
// RTC registers: There are a few 32-bit registers maintained during deep sleep.
3535
// We are already using one for saving sleep information during deep sleep.
3636
//
37-
// RTC Fast Memory: 8kB, also used for deep-sleep power on stub, and for heap
38-
// during normal operation if CONFIG_ESP32S2_ALLOW_RTC_FAST_MEM_AS_HEAP is set.
39-
// Power-on during deep sleep must be enabled.
40-
// I experimented with using RTC Fast Memory. It seemed to work, but occasionally,
41-
// got smashed for unknown reasons.
42-
// Base of RTC Fast memory on the data bus is 0x3FF9E000. The address is different on the instruction bus.
43-
//
37+
// RTC Fast Memory: 8kB, also used for deep-sleep power-on stub.
4438
// RTC Slow Memory: 8kB, also used for the ULP (tiny co-processor available during sleep).
45-
// Less likely to be used by ESP-IDF.
46-
// Since we may want to use the ULP in the future, we will use the upper half
47-
// of Slow Memory and reserve the lower half for ULP.
48-
// From ulp.h:
49-
// #define RTC_SLOW_MEM ((uint32_t*) 0x50000000) /*!< RTC slow memory, 8k size */
39+
//
40+
// The ESP-IDF build system takes care of the power management of these regions.
41+
// RTC_DATA_ATTR will allocate storage in RTC_SLOW_MEM unless CONFIG_ESP32S2_RTCDATA_IN_FAST_MEM
42+
// is set. Any memory not allocated by us can be used by the ESP-IDF for heap or other purposes.
5043

51-
// Upper half of RTC_SLOW_MEM.
44+
// Use half of RTC_SLOW_MEM or RTC_FAST_MEM.
5245
#define SLEEP_MEMORY_LENGTH (4096)
53-
#define SLEEP_MEMORY_BASE (0x50000000 + 4096)
5446

5547
typedef struct {
5648
mp_obj_base_t base;

shared-bindings/alarm/SleepMemory.c

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,26 +54,6 @@
5454
//| ...
5555
//|
5656

57-
//| def __bool__(self) -> bool:
58-
//| """``sleep_memory`` is ``True`` if its length is greater than zero.
59-
//| This is an easy way to check for its existence.
60-
//| """
61-
//| ...
62-
//|
63-
//| def __len__(self) -> int:
64-
//| """Return the length. This is used by (`len`)"""
65-
//| ...
66-
//|
67-
STATIC mp_obj_t alarm_sleep_memory_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
68-
alarm_sleep_memory_obj_t *self = MP_OBJ_TO_PTR(self_in);
69-
uint16_t len = common_hal_alarm_sleep_memory_get_length(self);
70-
switch (op) {
71-
case MP_UNARY_OP_BOOL: return mp_obj_new_bool(len != 0);
72-
case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(len);
73-
default: return MP_OBJ_NULL; // op not supported
74-
}
75-
}
76-
7757
STATIC const mp_rom_map_elem_t alarm_sleep_memory_locals_dict_table[] = {
7858
};
7959

@@ -131,7 +111,7 @@ STATIC mp_obj_t alarm_sleep_memory_subscr(mp_obj_t self_in, mp_obj_t index_in, m
131111
}
132112

133113
if (!common_hal_alarm_sleep_memory_set_bytes(self, slice.start, src_items, src_len)) {
134-
mp_raise_RuntimeError(translate("Unable to write to nvm."));
114+
mp_raise_RuntimeError(translate("Unable to write to sleep_memory."));
135115
}
136116
return mp_const_none;
137117
#else
@@ -141,7 +121,7 @@ STATIC mp_obj_t alarm_sleep_memory_subscr(mp_obj_t self_in, mp_obj_t index_in, m
141121
// Read slice.
142122
size_t len = slice.stop - slice.start;
143123
uint8_t *items = m_new(uint8_t, len);
144-
common_hal_alarm_sleep_memory_get_bytes(self, slice.start, len, items);
124+
common_hal_alarm_sleep_memory_get_bytes(self, slice.start, items, len);
145125
return mp_obj_new_bytearray_by_ref(len, items);
146126
}
147127
#endif
@@ -152,7 +132,7 @@ STATIC mp_obj_t alarm_sleep_memory_subscr(mp_obj_t self_in, mp_obj_t index_in, m
152132
if (value == MP_OBJ_SENTINEL) {
153133
// load
154134
uint8_t value_out;
155-
common_hal_alarm_sleep_memory_get_bytes(self, index, 1, &value_out);
135+
common_hal_alarm_sleep_memory_get_bytes(self, index, &value_out, 1);
156136
return MP_OBJ_NEW_SMALL_INT(value_out);
157137
} else {
158138
// store
@@ -162,7 +142,7 @@ STATIC mp_obj_t alarm_sleep_memory_subscr(mp_obj_t self_in, mp_obj_t index_in, m
162142
}
163143
uint8_t short_value = byte_value;
164144
if (!common_hal_alarm_sleep_memory_set_bytes(self, index, &short_value, 1)) {
165-
mp_raise_RuntimeError(translate("Unable to write to nvm."));
145+
mp_raise_RuntimeError(translate("Unable to write to sleep_memory."));
166146
}
167147
return mp_const_none;
168148
}
@@ -174,7 +154,6 @@ const mp_obj_type_t alarm_sleep_memory_type = {
174154
{ &mp_type_type },
175155
.name = MP_QSTR_SleepMemory,
176156
.subscr = alarm_sleep_memory_subscr,
177-
.unary_op = alarm_sleep_memory_unary_op,
178157
.print = NULL,
179158
.locals_dict = (mp_obj_t)&alarm_sleep_memory_locals_dict,
180159
};

shared-bindings/alarm/SleepMemory.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,7 @@ extern const mp_obj_type_t alarm_sleep_memory_type;
3434

3535
uint32_t common_hal_alarm_sleep_memory_get_length(alarm_sleep_memory_obj_t *self);
3636

37-
bool common_hal_alarm_sleep_memory_set_bytes(alarm_sleep_memory_obj_t *self,
38-
uint32_t start_index, uint8_t* values, uint32_t len);
39-
// len and values are intentionally swapped to signify values is an output and
40-
// also leverage the compiler to validate uses are expected.
41-
void common_hal_alarm_sleep_memory_get_bytes(alarm_sleep_memory_obj_t *self,
42-
uint32_t start_index, uint32_t len, uint8_t* values);
37+
bool common_hal_alarm_sleep_memory_set_bytes(alarm_sleep_memory_obj_t *self, uint32_t start_index, const uint8_t* values, uint32_t len);
38+
void common_hal_alarm_sleep_memory_get_bytes(alarm_sleep_memory_obj_t *self, uint32_t start_index, uint8_t* values, uint32_t len);
4339

4440
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_SLEEPMEMORY_H

0 commit comments

Comments
 (0)