Skip to content

Commit 1a01f76

Browse files
committed
alif/modmachine: Implement timeout_ms argument to deepsleep.
This allows `machine.deepsleep(N)` to wake the device after the timout N seconds. Could probably also do the same thing for lightsleep. Note that this uses the LPTIMER0 resource, which would not work if `MICROPY_HW_SYSTEM_TICK_USE_LPTIMER` was ever enabled (it's currently never enabled, so OK for now). Signed-off-by: Damien George <damien@micropython.org>
1 parent 4191cec commit 1a01f76

3 files changed

Lines changed: 43 additions & 4 deletions

File tree

ports/alif/boards/ALIF_ENSEMBLE/board.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ void board_early_init(void) {
8181
.vtor_address = SCB->VTOR,
8282
.vtor_address_ns = SCB->VTOR,
8383
// Configure wake-up sources.
84-
.ewic_cfg = EWIC_VBAT_GPIO | EWIC_RTC_A,
85-
.wakeup_events = WE_LPGPIO7 | WE_LPGPIO6 | WE_LPGPIO5 | WE_LPGPIO4 | WE_LPRTC,
84+
.ewic_cfg = EWIC_VBAT_GPIO | EWIC_VBAT_TIMER | EWIC_RTC_A,
85+
.wakeup_events = WE_LPGPIO7 | WE_LPGPIO6 | WE_LPGPIO5 | WE_LPGPIO4 | WE_LPTIMER0 | WE_LPRTC,
8686
};
8787

8888
if (se_services_set_off_profile(&off_profile)) {

ports/alif/boards/OPENMV_AE3/board.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ void board_early_init(void) {
154154
.vtor_address = SCB->VTOR,
155155
.vtor_address_ns = SCB->VTOR,
156156
// Configure wake-up sources.
157-
.ewic_cfg = EWIC_VBAT_GPIO | EWIC_RTC_A,
158-
.wakeup_events = WE_LPGPIO7 | WE_LPGPIO6 | WE_LPGPIO5 | WE_LPGPIO4 | WE_LPRTC,
157+
.ewic_cfg = EWIC_VBAT_GPIO | EWIC_VBAT_TIMER | EWIC_RTC_A,
158+
.wakeup_events = WE_LPGPIO7 | WE_LPGPIO6 | WE_LPGPIO5 | WE_LPGPIO4 | WE_LPTIMER0 | WE_LPRTC,
159159
};
160160

161161
if (se_services_set_off_profile(&off_profile)) {

ports/alif/modmachine.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
// This file is never compiled standalone, it's included directly from
2828
// extmod/modmachine.c via MICROPY_PY_MACHINE_INCLUDEFILE.
2929

30+
#include "modmachine.h"
3031
#include "se_services.h"
3132
#include "tusb.h"
3233

@@ -116,7 +117,37 @@ static void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) {
116117
#endif
117118
}
118119

120+
#include "lptimer.h"
121+
#include "sys_ctrl_lptimer.h"
122+
123+
#define LPTIMER ((LPTIMER_Type *)LPTIMER_BASE)
124+
#define LPTIMER_CH_A (0)
125+
126+
static void lptimer_set_wakeup(uint64_t timeout_us) {
127+
lptimer_disable_counter(LPTIMER, LPTIMER_CH_A);
128+
129+
ANA_REG->MISC_CTRL |= 1 << 0; // SEL_32K, select LXFO
130+
131+
select_lptimer_clk(LPTIMER_CLK_SOURCE_32K, LPTIMER_CH_A);
132+
133+
// Maximum 131 second timeout, to not overflow 32-bit ticks when
134+
// LPTIMER is clocked at 32768Hz.
135+
uint32_t timeout_ticks = (uint64_t)MIN(timeout_us, 131000000) * 32768 / 1000000;
136+
137+
// Set up the LPTIMER interrupt to fire after the given timeout.
138+
lptimer_set_mode_userdefined(LPTIMER, LPTIMER_CH_A);
139+
lptimer_load_count(LPTIMER, LPTIMER_CH_A, &timeout_ticks);
140+
lptimer_clear_interrupt(LPTIMER, LPTIMER_CH_A);
141+
lptimer_unmask_interrupt(LPTIMER, LPTIMER_CH_A);
142+
lptimer_enable_counter(LPTIMER, LPTIMER_CH_A);
143+
}
144+
119145
MP_NORETURN static void mp_machine_deepsleep(size_t n_args, const mp_obj_t *args) {
146+
mp_int_t sleep_ms = -1;
147+
if (n_args != 0) {
148+
sleep_ms = mp_obj_get_int(args[0]);
149+
}
150+
120151
#if MICROPY_HW_ENABLE_USBDEV
121152
mp_machine_enable_usb(false);
122153
#endif
@@ -127,6 +158,14 @@ MP_NORETURN static void mp_machine_deepsleep(size_t n_args, const mp_obj_t *args
127158

128159
__disable_irq();
129160

161+
if (sleep_ms >= 0) {
162+
if (sleep_ms < 10000) {
163+
lptimer_set_wakeup(sleep_ms * 1000);
164+
} else {
165+
machine_rtc_set_wakeup(sleep_ms / 1000);
166+
}
167+
}
168+
130169
// If power is removed from the subsystem, the function does
131170
// not return, and the CPU will reboot when/if the subsystem
132171
// is next powered up.

0 commit comments

Comments
 (0)