From e78471416bc77d5355e2181047e57f1e45d3654c Mon Sep 17 00:00:00 2001 From: "Kwabena W. Agyeman" Date: Thu, 10 Aug 2023 11:18:48 -0700 Subject: [PATCH] mimxrt/modmachine: Add support for machine.deepsleep. Signed-off-by: "Kwabena W. Agyeman" --- ports/mimxrt/Makefile | 1 + ports/mimxrt/machine_rtc.c | 8 +++++++ ports/mimxrt/modmachine.c | 46 ++++++++++++++++++++++++++++++++++++++ ports/mimxrt/modmachine.h | 2 +- 4 files changed, 56 insertions(+), 1 deletion(-) diff --git a/ports/mimxrt/Makefile b/ports/mimxrt/Makefile index f44518a8aa13..3521f922887f 100644 --- a/ports/mimxrt/Makefile +++ b/ports/mimxrt/Makefile @@ -128,6 +128,7 @@ SRC_HAL_IMX_C += \ $(MCU_DIR)/drivers/fsl_edma.c \ $(MCU_DIR)/drivers/fsl_flexram.c \ $(MCU_DIR)/drivers/fsl_flexspi.c \ + $(MCU_DIR)/drivers/fsl_gpc.c \ $(MCU_DIR)/drivers/fsl_gpio.c \ $(MCU_DIR)/drivers/fsl_gpt.c \ $(MCU_DIR)/drivers/fsl_lpi2c.c \ diff --git a/ports/mimxrt/machine_rtc.c b/ports/mimxrt/machine_rtc.c index 2a2800530128..169ec6b910ea 100644 --- a/ports/mimxrt/machine_rtc.c +++ b/ports/mimxrt/machine_rtc.c @@ -50,7 +50,11 @@ void machine_rtc_alarm_set_en() { void machine_rtc_alarm_off(bool clear) { machine_rtc_alarm_clear_en(); + #ifdef MIMXRT117x_SERIES + DisableIRQ(SNVS_HP_NON_TZ_IRQn); + #else DisableIRQ(SNVS_HP_WRAPPER_IRQn); + #endif if (clear) { SNVS->LPTAR = 0; @@ -60,7 +64,11 @@ void machine_rtc_alarm_off(bool clear) { } void machine_rtc_alarm_on() { + #ifdef MIMXRT117x_SERIES + EnableIRQ(SNVS_HP_NON_TZ_IRQn); + #else EnableIRQ(SNVS_HP_WRAPPER_IRQn); + #endif machine_rtc_alarm_set_en(); } diff --git a/ports/mimxrt/modmachine.c b/ports/mimxrt/modmachine.c index 423a67d6095f..16eae22029b5 100644 --- a/ports/mimxrt/modmachine.c +++ b/ports/mimxrt/modmachine.c @@ -37,6 +37,12 @@ #include "led.h" #include "pin.h" #include "modmachine.h" +#include "fsl_gpc.h" +#ifdef MIMXRT117x_SERIES +#include "fsl_soc_src.h" +#else +#include "fsl_src.h" +#endif #include "fsl_wdog.h" #if FSL_FEATURE_BOOT_ROM_HAS_ROMAPI #include "fsl_romapi.h" @@ -74,6 +80,14 @@ STATIC mp_obj_t machine_reset(void) { MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_obj, machine_reset); STATIC mp_obj_t machine_reset_cause(void) { + #ifdef MIMXRT117x_SERIES + uint32_t user_reset_flag = kSRC_M7CoreIppUserResetFlag; + #else + uint32_t user_reset_flag = kSRC_IppUserResetFlag; + #endif + if (SRC->SRSR & user_reset_flag) { + return MP_OBJ_NEW_SMALL_INT(MP_DEEPSLEEP_RESET); + } uint16_t reset_cause = WDOG_GetStatusFlags(WDOG1) & (kWDOG_PowerOnResetFlag | kWDOG_TimeoutResetFlag | kWDOG_SoftwareResetFlag); if (reset_cause == kWDOG_PowerOnResetFlag) { @@ -98,6 +112,37 @@ STATIC mp_obj_t machine_idle(void) { } STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_idle_obj, machine_idle); +STATIC mp_obj_t machine_deepsleep(size_t n_args, const mp_obj_t *args) { + if (n_args != 0) { + mp_int_t seconds = mp_obj_get_int(args[0]) / 1000; + if (seconds > 0) { + machine_rtc_alarm_helper(seconds, false); + #ifdef MIMXRT117x_SERIES + GPC_CM_EnableIrqWakeup(GPC_CPU_MODE_CTRL_0, SNVS_HP_NON_TZ_IRQn, true); + #else + GPC_EnableIRQ(GPC, SNVS_HP_WRAPPER_IRQn); + #endif + } + } + + #ifdef MIMXRT117x_SERIES + machine_pin_config(&pin_WAKEUP_DIG, PIN_MODE_IT_RISING, PIN_PULL_DISABLED, PIN_DRIVE_OFF, 0, PIN_AF_MODE_ALT5); + GPC_CM_EnableIrqWakeup(GPC_CPU_MODE_CTRL_0, GPIO13_Combined_0_31_IRQn, true); + #elif defined IOMUXC_SNVS_WAKEUP_GPIO5_IO00 + machine_pin_config(&pin_WAKEUP, PIN_MODE_IT_RISING, PIN_PULL_DISABLED, PIN_DRIVE_OFF, 0, PIN_AF_MODE_ALT5); + GPC_EnableIRQ(GPC, GPIO5_Combined_0_15_IRQn); + #endif + + SNVS->LPCR |= SNVS_LPCR_TOP_MASK; + + while (true) { + ; + } + + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_deepsleep_obj, 0, 1, machine_deepsleep); + STATIC mp_obj_t machine_disable_irq(void) { uint32_t state = MICROPY_BEGIN_ATOMIC_SECTION(); return mp_obj_new_int(state); @@ -162,6 +207,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&machine_wdt_type) }, { MP_ROM_QSTR(MP_QSTR_idle), MP_ROM_PTR(&machine_idle_obj) }, + { MP_ROM_QSTR(MP_QSTR_deepsleep), MP_ROM_PTR(&machine_deepsleep_obj) }, { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) }, { MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) }, diff --git a/ports/mimxrt/modmachine.h b/ports/mimxrt/modmachine.h index d00d2031c477..478b1c77362a 100644 --- a/ports/mimxrt/modmachine.h +++ b/ports/mimxrt/modmachine.h @@ -51,7 +51,7 @@ void mimxrt_sdram_init(void); void machine_i2s_init0(); void machine_i2s_deinit_all(void); void machine_rtc_start(void); - +void machine_rtc_alarm_helper(int seconds, bool repeat); void machine_uart_set_baudrate(mp_obj_t uart, uint32_t baudrate); #endif // MICROPY_INCLUDED_MIMXRT_MODMACHINE_H