Skip to content

Commit 78b3fe5

Browse files
twenrichdpgeorge
authored andcommitted
esp32/machine_rtc: Preserve RTC user memory over most reset causes.
The user memory area - accessible by machine.RTC.memory() -- will now survive most reboot causes. A power-on reset (also caused by the EN pin on some boards) will clean the memory. When this happens, the magic number not found in the user memory will cause initialization. After other resets (triggered by watchdogs, machine.reset(), ...), the user is responsible to check and validate the contents of the user area. This new behaviour can be changed by enabling MICROPY_HW_RTC_MEM_INIT_ALWAYS: in that case the RTC memory is always cleared on boot. Signed-off-by: Thomas Wenrich <[email protected]>
1 parent d3595fe commit 78b3fe5

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

ports/esp32/machine_rtc.c

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,24 @@ typedef struct _machine_rtc_obj_t {
6060
#define MICROPY_HW_RTC_USER_MEM_MAX 2048
6161
#endif
6262

63+
// A board can enable MICROPY_HW_RTC_MEM_INIT_ALWAYS to always clear out RTC memory on boot.
64+
// Defaults to RTC_NOINIT_ATTR so the user memory survives WDT resets and the like.
65+
#if MICROPY_HW_RTC_MEM_INIT_ALWAYS
66+
#define _USER_MEM_ATTR RTC_DATA_ATTR
67+
#else
68+
#define _USER_MEM_ATTR RTC_NOINIT_ATTR
69+
#endif
70+
6371
// Optionally compile user memory functionality if the size of memory is greater than 0
6472
#if MICROPY_HW_RTC_USER_MEM_MAX > 0
6573
#define MEM_MAGIC 0x75507921
66-
RTC_DATA_ATTR uint32_t rtc_user_mem_magic;
67-
RTC_DATA_ATTR uint16_t rtc_user_mem_len;
68-
RTC_DATA_ATTR uint8_t rtc_user_mem_data[MICROPY_HW_RTC_USER_MEM_MAX];
74+
_USER_MEM_ATTR uint32_t rtc_user_mem_magic;
75+
_USER_MEM_ATTR uint16_t rtc_user_mem_len;
76+
_USER_MEM_ATTR uint8_t rtc_user_mem_data[MICROPY_HW_RTC_USER_MEM_MAX];
6977
#endif
7078

79+
#undef _USER_MEM_ATTR
80+
7181
// singleton RTC object
7282
STATIC const machine_rtc_obj_t machine_rtc_obj = {{&machine_rtc_type}};
7383

@@ -80,6 +90,13 @@ STATIC mp_obj_t machine_rtc_make_new(const mp_obj_type_t *type, size_t n_args, s
8090
// check arguments
8191
mp_arg_check_num(n_args, n_kw, 0, 0, false);
8292

93+
#if MICROPY_HW_RTC_USER_MEM_MAX > 0
94+
if (rtc_user_mem_magic != MEM_MAGIC) {
95+
rtc_user_mem_magic = MEM_MAGIC;
96+
rtc_user_mem_len = 0;
97+
}
98+
#endif
99+
83100
// return constant object
84101
return (mp_obj_t)&machine_rtc_obj;
85102
}
@@ -130,13 +147,6 @@ STATIC mp_obj_t machine_rtc_init(mp_obj_t self_in, mp_obj_t date) {
130147
mp_obj_t args[2] = {self_in, date};
131148
machine_rtc_datetime_helper(2, args);
132149

133-
#if MICROPY_HW_RTC_USER_MEM_MAX > 0
134-
if (rtc_user_mem_magic != MEM_MAGIC) {
135-
rtc_user_mem_magic = MEM_MAGIC;
136-
rtc_user_mem_len = 0;
137-
}
138-
#endif
139-
140150
return mp_const_none;
141151
}
142152
STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_rtc_init_obj, machine_rtc_init);

0 commit comments

Comments
 (0)