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

169 lines
5.5 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-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-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"
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 "supervisor/port.h"
#include "supervisor/shared/workflow.h"
2020-12-01 20:01:14 -05:00
#include "common-hal/alarm/__init__.h"
2020-09-24 01:29:04 -04:00
#include "esp_sleep.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,
},
};
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-18 02:24:36 -05:00
alarm_time_timealarm_reset();
alarm_touch_touchalarm_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) {
if (alarm_time_timealarm_woke_us_up()) {
return ESP_SLEEP_WAKEUP_TIMER;
}
2020-12-18 02:24:36 -05:00
if (alarm_touch_touchalarm_woke_us_up()) {
return ESP_SLEEP_WAKEUP_TOUCHPAD;
}
return esp_sleep_get_wakeup_cause();
}
bool alarm_woken_from_sleep(void) {
return _get_wakeup_cause() != ESP_SLEEP_WAKEUP_UNDEFINED;
}
STATIC mp_obj_t _get_wake_alarm(size_t n_alarms, const mp_obj_t *alarms) {
switch (_get_wakeup_cause()) {
2020-11-20 23:33:39 -05:00
case ESP_SLEEP_WAKEUP_TIMER: {
return alarm_time_timealarm_get_wakeup_alarm(n_alarms, alarms);
2020-11-20 23:33:39 -05:00
}
case ESP_SLEEP_WAKEUP_EXT0: {
// TODO: implement pin alarm wake.
break;
2020-11-20 23:33:39 -05:00
}
2020-09-24 14:58:50 -04:00
case ESP_SLEEP_WAKEUP_TOUCHPAD:
2020-12-18 02:24:36 -05:00
return alarm_touch_touchalarm_get_wakeup_alarm(n_alarms, alarms);
2020-09-24 14:58:50 -04:00
break;
2020-11-20 23:33:39 -05: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
mp_obj_t common_hal_alarm_get_wake_alarm(void) {
return _get_wake_alarm(0, NULL);
}
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) {
2020-11-22 19:10:09 -05:00
bool time_alarm_set = false;
alarm_time_time_alarm_obj_t *time_alarm = MP_OBJ_NULL;
2020-11-22 19:10:09 -05:00
for (size_t i = 0; i < n_alarms; i++) {
if (MP_OBJ_IS_TYPE(alarms[i], &alarm_pin_pin_alarm_type)) {
2020-11-27 16:02:17 -05:00
mp_raise_NotImplementedError(translate("PinAlarm not yet implemented"));
} else if (MP_OBJ_IS_TYPE(alarms[i], &alarm_time_time_alarm_type)) {
2020-11-22 19:10:09 -05:00
if (time_alarm_set) {
mp_raise_ValueError(translate("Only one alarm.time alarm can be set."));
}
time_alarm = MP_OBJ_TO_PTR(alarms[i]);
2020-11-22 19:10:09 -05:00
time_alarm_set = true;
}
}
if (time_alarm_set) {
alarm_time_timealarm_set_alarm(time_alarm);
}
2020-12-30 11:41:22 -05:00
alarm_touch_touchalarm_set_alarm(deep_sleep, n_alarms, alarms);
2020-11-22 19:10:09 -05:00
}
STATIC void _idle_until_alarm(void) {
// Poll for alarms.
while (!mp_hal_is_interrupted()) {
RUN_BACKGROUND_TASKS;
// Allow ctrl-C interrupt.
if (alarm_woken_from_sleep()) {
return;
2020-11-27 16:02:17 -05:00
}
port_idle_until_interrupt();
2020-11-27 16:02:17 -05:00
}
}
2020-12-01 20:01:14 -05:00
// Is it safe to do a light sleep? Check whether WiFi is on or there are
// other ongoing tasks that should not be shut down.
STATIC bool _light_sleep_ok(void) {
return !common_hal_wifi_radio_get_enabled(&common_hal_wifi_radio_obj) && !supervisor_workflow_active();
2020-12-01 20:01:14 -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);
2020-11-27 16:02:17 -05:00
// Light sleep can break some functionality so only do it when possible. Otherwise we idle.
if (_light_sleep_ok()) {
2020-12-01 20:01:14 -05:00
esp_light_sleep_start();
} else {
_idle_until_alarm();
2020-12-01 20:01:14 -05:00
}
mp_obj_t wake_alarm = _get_wake_alarm(n_alarms, alarms);
alarm_reset();
return wake_alarm;
}
void common_hal_alarm_set_deep_sleep_alarms(size_t n_alarms, const mp_obj_t *alarms) {
_setup_sleep_alarms(true, n_alarms, alarms);
2020-12-01 20:01:14 -05:00
}
void NORETURN alarm_enter_deep_sleep(void) {
2020-12-19 02:26:34 -05:00
alarm_touch_touchalarm_prepare_for_deep_sleep();
// 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
}