coproc module enhancements

- add light and pretend-to-deep sleep support
- check coproc running status
This commit is contained in:
microDev 2022-10-22 19:08:49 +05:30
parent 83b54d003d
commit 8f0a674a1b
No known key found for this signature in database
GPG Key ID: 2C0867BE60967730
4 changed files with 36 additions and 19 deletions

View File

@ -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

View File

@ -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;
}

View File

@ -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);

View File

@ -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