Skip to content

Commit 039df0c

Browse files
iabdalkaderdpgeorge
authored andcommitted
alif/modmachine: Implement proper low-power modes.
Lightsleep current is around 23mA. Deepsleep current is sub 50uA. Signed-off-by: iabdalkader <[email protected]>
1 parent ff6ed73 commit 039df0c

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

ports/alif/alif.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ ALIF_SRC_C += $(addprefix $(ALIF_DFP_REL_TOP)/,\
182182
Device/common/source/system_utils.c \
183183
Device/common/source/tcm_partition.c \
184184
Device/common/source/tgu_M55.c \
185+
Device/common/source/pm.c \
185186
Device/core/$(MCU_CORE)/source/startup_$(MCU_CORE).c \
186187
drivers/source/adc.c \
187188
drivers/source/mhu_driver.c \

ports/alif/modmachine.c

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
// extmod/modmachine.c via MICROPY_PY_MACHINE_INCLUDEFILE.
2929

3030
#include "se_services.h"
31+
#include "tusb.h"
32+
33+
extern void dcd_uninit(void);
3134

3235
#define MICROPY_PY_MACHINE_EXTRA_GLOBALS \
3336
{ MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, \
@@ -70,15 +73,56 @@ static void mp_machine_set_freq(size_t n_args, const mp_obj_t *args) {
7073
mp_raise_NotImplementedError(NULL);
7174
}
7275

73-
static void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) {
74-
mp_int_t delay = -1;
75-
if (n_args == 1) {
76-
delay = mp_obj_get_int(args[0]);
76+
#if MICROPY_HW_ENABLE_USBDEV
77+
static void mp_machine_enable_usb(bool enable) {
78+
if (enable) {
79+
// Initialize TinyUSB and DCD.
80+
tusb_init();
81+
} else {
82+
// Disconnect USB device.
83+
tud_disconnect();
84+
// Deinitialize TinyUSB.
85+
tud_deinit(TUD_OPT_RHPORT);
86+
// Deinitialize DCD (disables IRQs).
87+
dcd_uninit();
7788
}
78-
mp_hal_delay_ms(delay);
89+
}
90+
#endif
91+
92+
static void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) {
93+
#if MICROPY_HW_ENABLE_USBDEV
94+
mp_machine_enable_usb(false);
95+
#endif
96+
97+
#ifdef MICROPY_BOARD_ENTER_STANDBY
98+
MICROPY_BOARD_ENTER_STANDBY();
99+
#endif
100+
101+
// This enters the deepest possible CPU sleep state, without
102+
// losing CPU state. CPU and subsystem power will remain on.
103+
pm_core_enter_deep_sleep();
104+
105+
#ifdef MICROPY_BOARD_EXIT_STANDBY
106+
MICROPY_BOARD_EXIT_STANDBY();
107+
#endif
108+
109+
#if MICROPY_HW_ENABLE_USBDEV
110+
mp_machine_enable_usb(true);
111+
#endif
79112
}
80113

81114
NORETURN static void mp_machine_deepsleep(size_t n_args, const mp_obj_t *args) {
82-
mp_machine_lightsleep(n_args, args);
115+
#if MICROPY_HW_ENABLE_USBDEV
116+
mp_machine_enable_usb(false);
117+
#endif
118+
119+
#ifdef MICROPY_BOARD_ENTER_STOP
120+
MICROPY_BOARD_ENTER_STOP();
121+
#endif
122+
123+
// If power is removed from the subsystem, the function does
124+
// not return, and the CPU will reboot when/if the subsystem
125+
// is next powered up.
126+
pm_core_enter_deep_sleep_request_subsys_off();
83127
mp_machine_reset();
84128
}

0 commit comments

Comments
 (0)