From 8f0a674a1b97c3682656322b0b45c85fb1c8e0d6 Mon Sep 17 00:00:00 2001 From: microDev <70126934+microDev1@users.noreply.github.com> Date: Sat, 22 Oct 2022 19:08:49 +0530 Subject: [PATCH] coproc module enhancements - add light and pretend-to-deep sleep support - check coproc running status --- .../common-hal/alarm/coproc/CoprocAlarm.c | 32 +++++++++++++++---- ports/espressif/common-hal/coproc/__init__.c | 18 +++++------ shared-bindings/coproc/__init__.c | 4 +-- shared-bindings/coproc/__init__.h | 1 - 4 files changed, 36 insertions(+), 19 deletions(-) diff --git a/ports/espressif/common-hal/alarm/coproc/CoprocAlarm.c b/ports/espressif/common-hal/alarm/coproc/CoprocAlarm.c index 3e0cff1c4c..aa49e13a2a 100644 --- a/ports/espressif/common-hal/alarm/coproc/CoprocAlarm.c +++ b/ports/espressif/common-hal/alarm/coproc/CoprocAlarm.c @@ -27,9 +27,16 @@ #include "shared-bindings/alarm/coproc/CoprocAlarm.h" #include "shared-bindings/coproc/__init__.h" +#if CIRCUITPY_COPROC + +#include "supervisor/port.h" + +#include "driver/rtc_cntl.h" +#include "soc/rtc_cntl_reg.h" + #include "esp_sleep.h" -#if CIRCUITPY_COPROC +static volatile bool woke_up = false; mp_obj_t alarm_coproc_coprocalarm_find_triggered_alarm(const size_t n_alarms, const mp_obj_t *alarms) { for (size_t i = 0; i < n_alarms; i++) { @@ -47,6 +54,13 @@ mp_obj_t alarm_coproc_coprocalarm_create_wakeup_alarm(void) { return alarm; } +// This is used to wake the main CircuitPython task. +STATIC void coproc_interrupt(void *arg) { + (void)arg; + woke_up = true; + port_wake_main_task_from_isr(); +} + void alarm_coproc_coprocalarm_set_alarm(const bool deep_sleep, const size_t n_alarms, const mp_obj_t *alarms) { bool coproc_alarm_set = false; alarm_coproc_coprocalarm_obj_t *coproc_alarm = MP_OBJ_NULL; @@ -65,13 +79,18 @@ void alarm_coproc_coprocalarm_set_alarm(const bool deep_sleep, const size_t n_al return; } - // load coproc program - common_hal_coproc_load(coproc_alarm->coproc); + // enable coproc interrupt + rtc_isr_register(&coproc_interrupt, NULL, RTC_CNTL_COCPU_INT_ST); + REG_SET_BIT(RTC_CNTL_INT_ENA_REG, RTC_CNTL_COCPU_INT_ENA); + + // start coproc program + common_hal_coproc_run(coproc_alarm->coproc); } void alarm_coproc_coprocalarm_prepare_for_deep_sleep(void) { - // start coproc program - common_hal_coproc_run(NULL); + // disbale coproc interrupt + rtc_isr_deregister(&coproc_interrupt, NULL); + REG_CLR_BIT(RTC_CNTL_INT_ENA_REG, RTC_CNTL_COCPU_INT_ENA); // enable coproc wakeup esp_sleep_enable_ulp_wakeup(); @@ -79,10 +98,11 @@ void alarm_coproc_coprocalarm_prepare_for_deep_sleep(void) { } bool alarm_coproc_coprocalarm_woke_this_cycle(void) { - return false; + return woke_up; } void alarm_coproc_coprocalarm_reset(void) { + woke_up = false; } #else // CIRCUITPY_COPROC diff --git a/ports/espressif/common-hal/coproc/__init__.c b/ports/espressif/common-hal/coproc/__init__.c index 9fce6afcdf..e372bfa235 100644 --- a/ports/espressif/common-hal/coproc/__init__.c +++ b/ports/espressif/common-hal/coproc/__init__.c @@ -40,6 +40,14 @@ #include "soc/rtc_cntl_reg.h" void common_hal_coproc_run(coproc_coproc_obj_t *self) { + if (GET_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN)) { + mp_raise_RuntimeError(translate("Already running")); + } + + ulp_riscv_load_binary(self->buf, self->buf_len); + m_free(self->buf); + self->buf = (uint8_t *)RTC_SLOW_MEM; + ulp_riscv_run(); } @@ -55,18 +63,10 @@ void common_hal_coproc_halt(coproc_coproc_obj_t *self) { CLEAR_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN); // suspends the ulp operation SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_DONE); - // Resets the processor + // resets the processor SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SHUT_RESET_EN); } -void common_hal_coproc_load(coproc_coproc_obj_t *self) { - if (ulp_riscv_load_binary(self->buf, self->buf_len) != ESP_OK) { - mp_raise_RuntimeError(translate("Firmware is too big")); - } - m_free(self->buf); - self->buf = (uint8_t *)RTC_SLOW_MEM; -} - mp_obj_t common_hal_coproc_memory(coproc_coproc_obj_t *self) { return (self->coproc_memory) ? MP_OBJ_FROM_PTR(self->coproc_memory) : mp_const_none; } diff --git a/shared-bindings/coproc/__init__.c b/shared-bindings/coproc/__init__.c index 1709e9b203..06b196d8be 100644 --- a/shared-bindings/coproc/__init__.c +++ b/shared-bindings/coproc/__init__.c @@ -68,9 +68,7 @@ STATIC coproc_coproc_obj_t *get_coproc_obj(mp_obj_t *self_in) { //| ... //| STATIC mp_obj_t coproc_run(mp_obj_t self_in) { - coproc_coproc_obj_t *self = get_coproc_obj(&self_in); - common_hal_coproc_load(self); - common_hal_coproc_run(self); + common_hal_coproc_run(get_coproc_obj(&self_in)); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(coproc_run_obj, coproc_run); diff --git a/shared-bindings/coproc/__init__.h b/shared-bindings/coproc/__init__.h index 0609bf827b..6f2b2a56ae 100644 --- a/shared-bindings/coproc/__init__.h +++ b/shared-bindings/coproc/__init__.h @@ -32,7 +32,6 @@ extern void common_hal_coproc_run(coproc_coproc_obj_t *self); extern void common_hal_coproc_halt(coproc_coproc_obj_t *self); -extern void common_hal_coproc_load(coproc_coproc_obj_t *self); extern mp_obj_t common_hal_coproc_memory(coproc_coproc_obj_t *self); #endif // MICROPY_INCLUDED_SHARED_BINDINGS_COPROC___INIT___H