2022-10-20 05:26:10 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2022 microDev
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2022-10-29 16:26:36 -04:00
|
|
|
#include "shared-bindings/alarm/__init__.h"
|
2022-10-20 05:26:10 -04:00
|
|
|
#include "shared-bindings/alarm/coproc/CoprocAlarm.h"
|
|
|
|
#include "shared-bindings/coproc/__init__.h"
|
|
|
|
|
2022-10-22 09:38:49 -04:00
|
|
|
#if CIRCUITPY_COPROC
|
|
|
|
|
|
|
|
#include "supervisor/port.h"
|
|
|
|
|
|
|
|
#include "driver/rtc_cntl.h"
|
|
|
|
#include "soc/rtc_cntl_reg.h"
|
|
|
|
|
2022-10-20 05:26:10 -04:00
|
|
|
#include "esp_sleep.h"
|
|
|
|
|
2022-10-22 09:38:49 -04:00
|
|
|
static volatile bool woke_up = false;
|
2022-10-20 05:26:10 -04:00
|
|
|
|
|
|
|
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++) {
|
|
|
|
if (mp_obj_is_type(alarms[i], &alarm_coproc_coprocalarm_type)) {
|
|
|
|
return alarms[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2022-10-29 16:26:36 -04:00
|
|
|
mp_obj_t alarm_coproc_coprocalarm_record_wake_alarm(void) {
|
|
|
|
alarm_coproc_coprocalarm_obj_t *const alarm = &alarm_wake_alarm.coproc_alarm;
|
|
|
|
|
2022-10-20 05:26:10 -04:00
|
|
|
alarm->base.type = &alarm_coproc_coprocalarm_type;
|
|
|
|
return alarm;
|
|
|
|
}
|
|
|
|
|
2022-10-22 09:38:49 -04:00
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
2022-10-20 05:26:10 -04:00
|
|
|
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;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < n_alarms; i++) {
|
|
|
|
if (mp_obj_is_type(alarms[i], &alarm_coproc_coprocalarm_type)) {
|
|
|
|
if (deep_sleep && coproc_alarm_set) {
|
|
|
|
mp_raise_ValueError_varg(translate("Only one %q can be set in deep sleep."), MP_QSTR_CoprocAlarm);
|
|
|
|
}
|
|
|
|
coproc_alarm = MP_OBJ_TO_PTR(alarms[i]);
|
|
|
|
coproc_alarm_set = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!coproc_alarm_set) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-22 09:38:49 -04:00
|
|
|
// 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);
|
2022-10-20 05:26:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void alarm_coproc_coprocalarm_prepare_for_deep_sleep(void) {
|
2022-10-22 09:38:49 -04:00
|
|
|
// disbale coproc interrupt
|
|
|
|
rtc_isr_deregister(&coproc_interrupt, NULL);
|
|
|
|
REG_CLR_BIT(RTC_CNTL_INT_ENA_REG, RTC_CNTL_COCPU_INT_ENA);
|
2022-10-20 05:26:10 -04:00
|
|
|
|
|
|
|
// enable coproc wakeup
|
|
|
|
esp_sleep_enable_ulp_wakeup();
|
|
|
|
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_ON);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool alarm_coproc_coprocalarm_woke_this_cycle(void) {
|
2022-10-22 09:38:49 -04:00
|
|
|
return woke_up;
|
2022-10-20 05:26:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void alarm_coproc_coprocalarm_reset(void) {
|
2022-10-22 09:38:49 -04:00
|
|
|
woke_up = false;
|
2022-10-20 05:26:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#else // CIRCUITPY_COPROC
|
|
|
|
|
|
|
|
mp_obj_t alarm_coproc_coprocalarm_find_triggered_alarm(const size_t n_alarms, const mp_obj_t *alarms) {
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2022-10-29 16:26:36 -04:00
|
|
|
mp_obj_t alarm_coproc_coprocalarm_record_wake_alarm(void) {
|
2022-10-20 05:26:10 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
void alarm_coproc_coprocalarm_set_alarm(const bool deep_sleep, const size_t n_alarms, const mp_obj_t *alarms) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void alarm_coproc_coprocalarm_prepare_for_deep_sleep(void) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool alarm_coproc_coprocalarm_woke_this_cycle(void) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void alarm_coproc_coprocalarm_reset(void) {
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // CIRCUITPY_COPROC
|