circuitpython/ports/espressif/common-hal/alarm/__init__.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

208 lines
7.6 KiB
C
Raw Normal View History

2020-12-10 13:03:40 -05:00
/*
2020-09-24 01:29:04 -04:00
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
2020-12-10 13:03:40 -05:00
* Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
* Copyright (c) 2020 Dan Halbert for Adafruit Industries
2020-09-24 01:29:04 -04:00
*
* 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.
*/
2020-12-22 10:06:43 -05:00
#include "py/gc.h"
2020-11-25 15:07:57 -05:00
#include "py/obj.h"
2020-11-22 19:10:09 -05:00
#include "py/objtuple.h"
#include "py/runtime.h"
2020-11-22 19:10:09 -05:00
2020-12-22 10:06:43 -05:00
#include "shared-bindings/alarm/__init__.h"
2020-12-10 13:03:40 -05:00
#include "shared-bindings/alarm/SleepMemory.h"
2020-12-18 02:24:36 -05:00
#include "shared-bindings/alarm/pin/PinAlarm.h"
2020-11-25 17:52:06 -05:00
#include "shared-bindings/alarm/time/TimeAlarm.h"
2020-12-18 02:24:36 -05:00
#include "shared-bindings/alarm/touch/TouchAlarm.h"
2022-10-20 05:26:10 -04:00
#include "shared-bindings/alarm/coproc/CoprocAlarm.h"
2020-12-18 02:24:36 -05:00
2020-12-01 20:01:14 -05:00
#include "shared-bindings/wifi/__init__.h"
2020-12-18 02:24:36 -05:00
#include "shared-bindings/microcontroller/__init__.h"
2020-12-01 20:01:14 -05:00
#include "common-hal/digitalio/DigitalInOut.h"
#include "supervisor/port.h"
#include "supervisor/shared/workflow.h"
2020-09-24 01:29:04 -04:00
#include "esp_sleep.h"
2021-09-25 14:30:00 -04:00
#include "soc/rtc_cntl_reg.h"
#include "components/driver/include/driver/gpio.h"
#include "components/driver/include/driver/uart.h"
2020-12-10 13:03:40 -05:00
// Singleton instance of SleepMemory.
const alarm_sleep_memory_obj_t alarm_sleep_memory_obj = {
.base = {
.type = &alarm_sleep_memory_type,
},
};
2022-10-27 22:42:04 -04:00
// Static alarm object recording alarm (if any) that woke up CircuitPython after light or deep sleep.
// This object lives across VM instantiations, so none of these objects can contain references to the heap.
static union {
alarm_pin_pinalarm_obj_t pin_alarm;
alarm_time_timealarm_obj_t time_alarm;
alarm_touch_touchalarm_obj_t touch_alarm;
alarm_coproc_coprocalarm_obj_t coproc_alarm;
} wake_alarm;
2020-11-22 19:10:09 -05:00
void alarm_reset(void) {
2020-12-10 13:03:40 -05:00
alarm_sleep_memory_reset();
2020-12-30 12:14:22 -05:00
alarm_pin_pinalarm_reset();
2020-12-18 02:24:36 -05:00
alarm_time_timealarm_reset();
alarm_touch_touchalarm_reset();
2022-10-20 05:26:10 -04:00
alarm_coproc_coprocalarm_reset();
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL);
2020-11-22 19:10:09 -05:00
}
STATIC esp_sleep_wakeup_cause_t _get_wakeup_cause(void) {
// First check if the modules remember what last woke up
if (alarm_pin_pinalarm_woke_this_cycle()) {
2020-12-30 12:14:22 -05:00
return ESP_SLEEP_WAKEUP_GPIO;
}
if (alarm_time_timealarm_woke_this_cycle()) {
return ESP_SLEEP_WAKEUP_TIMER;
}
if (alarm_touch_touchalarm_woke_this_cycle()) {
2020-12-18 02:24:36 -05:00
return ESP_SLEEP_WAKEUP_TOUCHPAD;
}
2022-10-20 05:26:10 -04:00
if (alarm_coproc_coprocalarm_woke_this_cycle()) {
return ESP_SLEEP_WAKEUP_ULP;
}
// If waking from true deep sleep, modules will have lost their state,
// so check the deep wakeup cause manually
2021-12-21 21:48:36 -05:00
return esp_sleep_get_wakeup_cause();
}
2021-03-27 14:08:44 -04:00
bool common_hal_alarm_woken_from_sleep(void) {
return _get_wakeup_cause() != ESP_SLEEP_WAKEUP_UNDEFINED;
}
2022-10-27 22:42:04 -04:00
mp_obj_t common_hal_alarm_record_wake_alarm(void) {
// If woken from deep sleep, create a copy alarm similar to what would have
// been passed in originally. Otherwise, just return none
esp_sleep_wakeup_cause_t cause = _get_wakeup_cause();
switch (cause) {
2020-11-20 23:33:39 -05:00
case ESP_SLEEP_WAKEUP_TIMER: {
2022-10-27 22:42:04 -04:00
return alarm_time_timealarm_record_wakeup_alarm(&wake_alarm.time_alarm);
2020-11-20 23:33:39 -05:00
}
case ESP_SLEEP_WAKEUP_GPIO:
case ESP_SLEEP_WAKEUP_EXT0:
case ESP_SLEEP_WAKEUP_EXT1: {
2022-10-27 22:42:04 -04:00
return alarm_pin_pinalarm_record_wakeup_alarm(&wake_alarm.pin_alarm);
2020-11-20 23:33:39 -05:00
}
case ESP_SLEEP_WAKEUP_TOUCHPAD: {
2022-10-27 22:42:04 -04:00
return alarm_touch_touchalarm_record_wakeup_alarm(&wake_alarm.touch_alarm);
}
2020-11-20 23:33:39 -05:00
2022-10-20 05:26:10 -04:00
case ESP_SLEEP_WAKEUP_ULP: {
2022-10-27 22:42:04 -04:00
return alarm_coproc_coprocalarm_record_wakeup_alarm(&wake_alarm.coproc_alarm);
2022-10-20 05:26:10 -04:00
}
2020-09-24 01:29:04 -04:00
case ESP_SLEEP_WAKEUP_UNDEFINED:
default:
2020-11-20 23:33:39 -05:00
// Not a deep sleep reset.
2020-09-24 01:29:04 -04:00
break;
}
return mp_const_none;
}
2020-11-22 19:10:09 -05:00
2020-11-27 16:02:17 -05:00
// Set up light sleep or deep sleep alarms.
STATIC void _setup_sleep_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t *alarms) {
alarm_pin_pinalarm_set_alarms(deep_sleep, n_alarms, alarms);
alarm_time_timealarm_set_alarms(deep_sleep, n_alarms, alarms);
2020-12-30 11:41:22 -05:00
alarm_touch_touchalarm_set_alarm(deep_sleep, n_alarms, alarms);
2022-10-20 05:26:10 -04:00
alarm_coproc_coprocalarm_set_alarm(deep_sleep, n_alarms, alarms);
2020-11-22 19:10:09 -05:00
}
mp_obj_t common_hal_alarm_light_sleep_until_alarms(size_t n_alarms, const mp_obj_t *alarms) {
_setup_sleep_alarms(false, n_alarms, alarms);
mp_obj_t wake_alarm = mp_const_none;
// We cannot esp_light_sleep_start() here because it shuts down all non-RTC peripherals.
while (!mp_hal_is_interrupted()) {
RUN_BACKGROUND_TASKS;
// Detect if interrupt was alarm or ctrl-C interrupt.
2021-03-27 14:08:44 -04:00
if (common_hal_alarm_woken_from_sleep()) {
esp_sleep_wakeup_cause_t cause = _get_wakeup_cause();
switch (cause) {
case ESP_SLEEP_WAKEUP_TIMER: {
wake_alarm = alarm_time_timealarm_find_triggered_alarm(n_alarms,alarms);
break;
}
case ESP_SLEEP_WAKEUP_GPIO: {
wake_alarm = alarm_pin_pinalarm_find_triggered_alarm(n_alarms,alarms);
break;
}
case ESP_SLEEP_WAKEUP_TOUCHPAD: {
wake_alarm = alarm_touch_touchalarm_find_triggered_alarm(n_alarms,alarms);
break;
}
2022-10-20 05:26:10 -04:00
case ESP_SLEEP_WAKEUP_ULP: {
wake_alarm = alarm_coproc_coprocalarm_find_triggered_alarm(n_alarms,alarms);
break;
}
default:
// Should not reach this, if all light sleep types are covered correctly
break;
}
shared_alarm_save_wake_alarm(wake_alarm);
break;
2020-11-27 16:02:17 -05:00
}
port_idle_until_interrupt();
2020-11-27 16:02:17 -05:00
}
if (mp_hal_is_interrupted()) {
return mp_const_none; // Shouldn't be given to python code because exception handling should kick in.
2020-12-01 20:01:14 -05:00
}
alarm_reset();
return wake_alarm;
}
void common_hal_alarm_set_deep_sleep_alarms(size_t n_alarms, const mp_obj_t *alarms, size_t n_dios, digitalio_digitalinout_obj_t *preserve_dios[]) {
digitalio_digitalinout_preserve_for_deep_sleep(n_dios, preserve_dios);
_setup_sleep_alarms(true, n_alarms, alarms);
2020-12-01 20:01:14 -05:00
}
2021-03-27 14:08:44 -04:00
void NORETURN common_hal_alarm_enter_deep_sleep(void) {
alarm_pin_pinalarm_prepare_for_deep_sleep();
2020-12-19 02:26:34 -05:00
alarm_touch_touchalarm_prepare_for_deep_sleep();
2022-10-20 05:26:10 -04:00
alarm_coproc_coprocalarm_prepare_for_deep_sleep();
2021-02-08 12:40:20 -05:00
// We no longer need to remember the pin preservations, since any pin resets are all done.
clear_pin_preservations();
// The ESP-IDF caches the deep sleep settings and applies them before sleep.
// We don't need to worry about resetting them in the interim.
esp_deep_sleep_start();
2020-11-22 19:10:09 -05:00
}
2020-12-22 10:06:43 -05:00
void common_hal_alarm_gc_collect(void) {
2021-03-27 14:08:44 -04:00
gc_collect_ptr(shared_alarm_get_wake_alarm());
2020-12-22 10:06:43 -05:00
}