Skip to content

Commit ded134c

Browse files
committed
store wake_alarm in a static object
1 parent 0221cc8 commit ded134c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+195
-210
lines changed

main.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -747,8 +747,7 @@ STATIC void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
747747

748748
supervisor_allocation *heap = allocate_remaining_memory();
749749

750-
// true means this is the first set of VM's after a hard reset.
751-
start_mp(heap, true);
750+
start_mp(heap);
752751

753752
#if CIRCUITPY_USB
754753
// Set up default USB values after boot.py VM starts but before running boot.py.
@@ -964,6 +963,9 @@ int __attribute__((used)) main(void) {
964963
// Record which alarm woke us up, if any.
965964
// common_hal_alarm_record_wake_alarm() should return a static, non-heap object
966965
shared_alarm_save_wake_alarm(common_hal_alarm_record_wake_alarm());
966+
// Then reset the alarm system. It's not reset in reset_port(), because that's also called
967+
// on VM teardown, which would clear any alarm setup.
968+
alarm_reset();
967969
#endif
968970

969971
// Reset everything and prep MicroPython to run boot.py.
@@ -1010,6 +1012,9 @@ int __attribute__((used)) main(void) {
10101012
serial_write_compressed(translate("soft reboot\n"));
10111013
}
10121014
if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) {
1015+
// If code.py did a fake deep sleep, pretend that we
1016+
// are running code.py for the first time after a hard
1017+
// reset. This will preserve any alarm information.
10131018
skip_repl = run_code_py(safe_mode, &simulate_reset);
10141019
} else {
10151020
skip_repl = false;
@@ -1018,14 +1023,10 @@ int __attribute__((used)) main(void) {
10181023
break;
10191024
}
10201025

1021-
// Either the REPL or code.py has run and finished.
1022-
// If code.py did a fake deep sleep, pretend that we are running code.py for
1023-
// the first time after a hard reset. This will preserve any alarm information.
1024-
if (!simulate_reset) {
1025-
#if CIRCUITPY_ALARM
1026-
shared_alarm_save_wake_alarm(mp_const_none);
1027-
#endif
1028-
}
1026+
#if CIRCUITPY_ALARM
1027+
shared_alarm_save_wake_alarm(simulate_reset ? common_hal_alarm_record_wake_alarm() : mp_const_none);
1028+
alarm_reset();
1029+
#endif
10291030
}
10301031
mp_deinit();
10311032
return 0;

ports/atmel-samd/common-hal/alarm/SleepMemory.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM_SLEEPMEMORY_H
28-
#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM_SLEEPMEMORY_H
27+
#pragma once
2928

3029
#include "py/obj.h"
3130

@@ -34,5 +33,3 @@ typedef struct {
3433
} alarm_sleep_memory_obj_t;
3534

3635
extern void alarm_sleep_memory_reset(void);
37-
38-
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM_SLEEPMEMORY_H

ports/atmel-samd/common-hal/alarm/__init__.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,9 @@ const alarm_sleep_memory_obj_t alarm_sleep_memory_obj = {
4848
},
4949
};
5050

51-
// Static alarm object recording alarm (if any) that woke up CircuitPython after light or deep sleep.
51+
// Non-heap alarm object recording alarm (if any) that woke up CircuitPython after light or deep sleep.
5252
// This object lives across VM instantiations, so none of these objects can contain references to the heap.
53-
static union {
54-
alarm_pin_pinalarm_obj_t pin_alarm;
55-
alarm_time_timealarm_obj_t time_alarm;
56-
} wake_alarm;
53+
alarm_wake_alarm_union_t alarm_wake_alarm;
5754

5855
void alarm_reset(void) {
5956
// Reset the alarm flag
@@ -83,13 +80,13 @@ mp_obj_t common_hal_alarm_record_wake_alarm(void) {
8380
if (alarm_pin_pinalarm_woke_this_cycle()) {
8481
TAMPID = RTC->MODE0.TAMPID.reg;
8582
RTC->MODE0.TAMPID.reg = TAMPID; // clear register
86-
return alarm_pin_pinalarm_record_wakeup_alarm(&wake_alarm.pin_alarm, TAMPID);
83+
return alarm_pin_pinalarm_record_wake_alarm(TAMPID);
8784
}
8885
if (alarm_time_timealarm_woke_this_cycle() || (true_deep && TAMPID == 0)) {
89-
return alarm_time_timealarm_record_wakeup_alarm(&wake_alarm.time_alarm);
86+
return alarm_time_timealarm_record_wake_alarm();
9087
}
9188
if (true_deep) {
92-
return alarm_pin_pinalarm_record_wakeup_alarm(&wake_alarm.pin_alarm, TAMPID);
89+
return alarm_pin_pinalarm_record_wake_alarm(TAMPID);
9390
}
9491
return mp_const_none;
9592
}

ports/atmel-samd/common-hal/alarm/__init__.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM__INIT__H
28-
#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM__INIT__H
27+
#pragma once
2928

3029
#include "common-hal/alarm/SleepMemory.h"
30+
#include "common-hal/alarm/pin/PinAlarm.h"
31+
#include "common-hal/alarm/time/TimeAlarm.h"
3132

3233
extern const alarm_sleep_memory_obj_t alarm_sleep_memory_obj;
3334

@@ -53,8 +54,13 @@ typedef enum {
5354
SAMD_WAKEUP_RTC
5455
} samd_sleep_source_t;
5556

57+
typedef union {
58+
alarm_pin_pinalarm_obj_t pin_alarm;
59+
alarm_time_timealarm_obj_t time_alarm;
60+
} alarm_wake_alarm_union_t;
61+
62+
extern alarm_wake_alarm_union_t alarm_wake_alarm;
63+
5664
extern void alarm_set_wakeup_reason(samd_sleep_source_t reason);
57-
void alarm_get_wakeup_cause(void);
65+
extern void alarm_get_wakeup_cause(void);
5866
extern void alarm_reset(void);
59-
60-
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM__INIT__H

ports/atmel-samd/common-hal/alarm/pin/PinAlarm.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
#include "hal/include/hal_gpio.h"
3232
// #include <stdio.h>
3333

34+
#include "shared-bindings/alarm/__init__.h"
3435
#include "shared-bindings/alarm/pin/PinAlarm.h"
3536
#include "shared-bindings/microcontroller/__init__.h"
36-
#include "shared-bindings/microcontroller/Pin.h"
3737
#include "common-hal/alarm/__init__.h"
3838

3939
// This variable stores whether a PinAlarm woke in light sleep or fake deep sleep
@@ -128,8 +128,8 @@ mp_obj_t alarm_pin_pinalarm_find_triggered_alarm(size_t n_alarms, const mp_obj_t
128128
return mp_const_none;
129129
}
130130

131-
mp_obj_t alarm_pin_pinalarm_record_wakeup_alarm(alarm_pin_pinalarm_obj_t *alarm, uint32_t TAMPID) {
132-
// Create tamper alarm that caused wakeup from deep sleep
131+
mp_obj_t alarm_pin_pinalarm_record_wake_alarm(uint32_t TAMPID) {
132+
alarm_pin_pinalarm_obj_t *const alarm = &alarm_wake_alarm.pin_alarm;
133133

134134
for (samd_tamper_pin_t *t = TAMPER_PINS; t->n >= 0; t++) {
135135
if (TAMPID & (1 << t->n)) {

ports/atmel-samd/common-hal/alarm/pin/PinAlarm.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM_PINALARM_H
28-
#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM_PINALARM_H
27+
#pragma once
2928

3029
#include "py/obj.h"
3130
#include "py/objtuple.h"
3231

32+
#include "shared-bindings/microcontroller/Pin.h"
33+
3334
typedef struct {
3435
mp_obj_base_t base;
3536
const mcu_pin_obj_t *pin;
@@ -39,13 +40,11 @@ typedef struct {
3940
} alarm_pin_pinalarm_obj_t;
4041

4142
mp_obj_t alarm_pin_pinalarm_find_triggered_alarm(size_t n_alarms, const mp_obj_t *alarms);
42-
mp_obj_t alarm_pin_pinalarm_record_wakeup_alarm(alarm_pin_pinalarm_obj_t *alarm, uint32_t TAMPID);
43+
mp_obj_t alarm_pin_pinalarm_record_wake_alarm(uint32_t TAMPID);
4344

4445
void pin_alarm_callback(uint8_t num);
4546
void alarm_pin_pinalarm_reset(void);
4647
void alarm_pin_pinalarm_deinit_alarms(size_t n_alarms, const mp_obj_t *alarms);
4748
void alarm_pin_pinalarm_set_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t *alarms);
4849
void alarm_pin_pinalarm_prepare_for_deep_sleep(void);
4950
bool alarm_pin_pinalarm_woke_this_cycle(void);
50-
51-
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM_PINALARM_H

ports/atmel-samd/common-hal/alarm/time/TimeAlarm.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
#include "py/runtime.h"
2828
#include "hpl/pm/hpl_pm_base.h"
2929

30+
#include "shared-bindings/alarm/__init__.h"
3031
#include "shared-bindings/alarm/time/TimeAlarm.h"
3132
#include "shared-bindings/time/__init__.h"
32-
#include "common-hal/alarm/__init__.h"
3333
#include "supervisor/port.h"
3434

3535
STATIC volatile bool woke_up = false;
@@ -58,7 +58,9 @@ mp_obj_t alarm_time_timealarm_find_triggered_alarm(size_t n_alarms, const mp_obj
5858
return mp_const_none;
5959
}
6060

61-
mp_obj_t alarm_time_timealarm_record_wakeup_alarm(alarm_time_timealarm_obj_t *alarm) {
61+
mp_obj_t alarm_time_timealarm_record_wake_alarm(void) {
62+
alarm_time_timealarm_obj_t *const alarm = &alarm_wake_alarm.time_alarm;
63+
6264
alarm->base.type = &alarm_time_timealarm_type;
6365
// TODO: Set monotonic_time based on the RTC state.
6466
// Or don't, most of the other ports don't have this either.

ports/atmel-samd/common-hal/alarm/time/TimeAlarm.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM_TIMEALARM_H
28-
#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM_TIMEALARM_H
27+
#pragma once
2928

3029
#include "py/obj.h"
3130

@@ -35,12 +34,10 @@ typedef struct {
3534
} alarm_time_timealarm_obj_t;
3635

3736
mp_obj_t alarm_time_timealarm_find_triggered_alarm(size_t n_alarms, const mp_obj_t *alarms);
38-
mp_obj_t alarm_time_timealarm_record_wakeup_alarm(alarm_time_timealarm_obj_t *alarm);
37+
mp_obj_t alarm_time_timealarm_record_wake_alarm(void);
3938
void time_alarm_callback(void);
4039
bool alarm_time_timealarm_woke_this_cycle(void);
4140
void alarm_time_timealarm_set_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t *alarms);
4241
void alarm_time_timealarm_reset(void);
4342

4443
void alarm_time_timealarm_prepare_for_deep_sleep(void);
45-
46-
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM_TIMEALARM_H

ports/atmel-samd/common-hal/alarm/touch/TouchAlarm.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,9 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM_TOUCHALARM_H
28-
#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM_TOUCHALARM_H
27+
#pragma once
2928

3029
typedef struct {
3130
mp_obj_base_t base;
3231
const mcu_pin_obj_t *pin;
3332
} alarm_touch_touchalarm_obj_t;
34-
35-
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM_TOUCHALARM_H

ports/atmel-samd/supervisor/port.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,10 +385,6 @@ safe_mode_t port_init(void) {
385385
}
386386

387387
void reset_port(void) {
388-
#if CIRCUITPY_ALARM
389-
alarm_reset();
390-
#endif
391-
392388
#if CIRCUITPY_BUSIO
393389
reset_sercoms();
394390
#endif

0 commit comments

Comments
 (0)