Skip to content

Commit

Permalink
mimxrt/modmachine: Add support for machine.deepsleep.
Browse files Browse the repository at this point in the history
Signed-off-by: "Kwabena W. Agyeman" <[email protected]>
  • Loading branch information
kwagyeman authored and dpgeorge committed Oct 17, 2023
1 parent 4331580 commit e784714
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions ports/mimxrt/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
8 changes: 8 additions & 0 deletions ports/mimxrt/machine_rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}

Expand Down
46 changes: 46 additions & 0 deletions ports/mimxrt/modmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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) },
Expand Down
2 changes: 1 addition & 1 deletion ports/mimxrt/modmachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit e784714

Please sign in to comment.