Skip to content

Commit d4e9eea

Browse files
dhalberttannewt
authored andcommitted
mark alarm.wake_alarm during gc sweep
1 parent 0dcc659 commit d4e9eea

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,10 @@ void gc_collect(void) {
613613

614614
background_callback_gc_collect();
615615

616+
#if CIRCUITPY_ALARM
617+
common_hal_alarm_gc_collect();
618+
#endif
619+
616620
#if CIRCUITPY_DISPLAYIO
617621
displayio_gc_collect();
618622
#endif

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@
2525
* THE SOFTWARE.
2626
*/
2727

28+
#include "py/gc.h"
2829
#include "py/obj.h"
2930
#include "py/objtuple.h"
3031
#include "py/runtime.h"
3132

33+
#include "shared-bindings/alarm/__init__.h"
3234
#include "shared-bindings/alarm/pin/PinAlarm.h"
3335
#include "shared-bindings/alarm/SleepMemory.h"
3436
#include "shared-bindings/alarm/time/TimeAlarm.h"
@@ -38,8 +40,6 @@
3840
#include "supervisor/port.h"
3941
#include "supervisor/shared/workflow.h"
4042

41-
#include "common-hal/alarm/__init__.h"
42-
4343
#include "esp_sleep.h"
4444

4545
#include "components/soc/soc/esp32s2/include/soc/rtc_cntl_reg.h"
@@ -159,3 +159,7 @@ void NORETURN alarm_enter_deep_sleep(void) {
159159
// We don't need to worry about resetting them in the interim.
160160
esp_deep_sleep_start();
161161
}
162+
163+
void common_hal_alarm_gc_collect(void) {
164+
gc_collect_ptr(alarm_get_wake_alarm());
165+
}

shared-bindings/alarm/__init__.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,18 @@ STATIC mp_map_elem_t alarm_module_globals_table[] = {
199199
{ MP_ROM_QSTR(MP_QSTR_SleepMemory), MP_OBJ_FROM_PTR(&alarm_sleep_memory_type) },
200200
{ MP_ROM_QSTR(MP_QSTR_sleep_memory), MP_OBJ_FROM_PTR(&alarm_sleep_memory_obj) },
201201
};
202-
STATIC MP_DEFINE_MUTABLE_DICT(alarm_module_globals, alarm_module_globals_table);
202+
MP_DEFINE_MUTABLE_DICT(alarm_module_globals, alarm_module_globals_table);
203+
204+
// Fetch value from module dict.
205+
mp_obj_t alarm_get_wake_alarm(void) {
206+
mp_map_elem_t *elem =
207+
mp_map_lookup(&alarm_module_globals.map, MP_ROM_QSTR(MP_QSTR_wake_alarm), MP_MAP_LOOKUP);
208+
if (elem) {
209+
return elem->value;
210+
} else {
211+
return NULL;
212+
}
213+
}
203214

204215
STATIC void alarm_set_wake_alarm(mp_obj_t alarm) {
205216
// Equivalent of:

shared-bindings/alarm/__init__.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131

3232
#include "common-hal/alarm/__init__.h"
3333

34+
// Make module dict available elsewhere, so we can fetch
35+
extern mp_obj_dict_t alarm_module_globals;
36+
3437
extern mp_obj_t common_hal_alarm_light_sleep_until_alarms(size_t n_alarms, const mp_obj_t *alarms);
3538

3639
// Deep sleep is a two step process. Alarms are set when the VM is valid but
@@ -43,6 +46,10 @@ extern void common_hal_alarm_set_deep_sleep_alarms(size_t n_alarms, const mp_obj
4346
// Deep sleep is entered outside of the VM so we omit the `common_hal_` prefix.
4447
extern NORETURN void alarm_enter_deep_sleep(void);
4548

49+
// Fetches value from module dict.
50+
extern mp_obj_t alarm_get_wake_alarm(void);
51+
52+
extern void common_hal_alarm_gc_collect(void);
4653
extern mp_obj_t common_hal_alarm_get_wake_alarm(void);
4754

4855
// Used by wake-up code.
@@ -52,4 +59,5 @@ void alarm_save_wakeup_alarm(void);
5259
// True if an alarm is alerting. This is most useful for pretend deep sleep.
5360
extern bool alarm_woken_from_sleep(void);
5461

62+
5563
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_ALARM___INIT___H

0 commit comments

Comments
 (0)