|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2016 Scott Shawcroft for Adafruit Industries |
| 7 | + * Copyright (c) 2019 Lucian Copeland for Adafruit Industries |
| 8 | + * |
| 9 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | + * of this software and associated documentation files (the "Software"), to deal |
| 11 | + * in the Software without restriction, including without limitation the rights |
| 12 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | + * copies of the Software, and to permit persons to whom the Software is |
| 14 | + * furnished to do so, subject to the following conditions: |
| 15 | + * |
| 16 | + * The above copyright notice and this permission notice shall be included in |
| 17 | + * all copies or substantial portions of the Software. |
| 18 | + * |
| 19 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | + * THE SOFTWARE. |
| 26 | + */ |
| 27 | + |
| 28 | +#include <sys/time.h> |
| 29 | + |
| 30 | +#include "common-hal/alarm/__init__.h" |
| 31 | +#include "shared-bindings/alarm/__init__.h" |
| 32 | + |
| 33 | +#include "esp_sleep.h" |
| 34 | +#include "soc/rtc_periph.h" |
| 35 | +#include "driver/rtc_io.h" |
| 36 | + |
| 37 | +static RTC_DATA_ATTR struct timeval sleep_enter_time; |
| 38 | +static RTC_DATA_ATTR struct timeval sleep_exit_time; |
| 39 | +static RTC_DATA_ATTR uint8_t wake_io; |
| 40 | + |
| 41 | +int common_hal_alarm_get_sleep_time(void) { |
| 42 | + return (sleep_exit_time.tv_sec - sleep_enter_time.tv_sec) * 1000 + (sleep_exit_time.tv_usec - sleep_enter_time.tv_usec) / 1000; |
| 43 | +} |
| 44 | + |
| 45 | +void RTC_IRAM_ATTR esp_wake_deep_sleep(void) { |
| 46 | + esp_default_wake_deep_sleep(); |
| 47 | + wake_io = rtc_gpio_get_level(6); |
| 48 | + gettimeofday(&sleep_exit_time, NULL); |
| 49 | +} |
| 50 | + |
| 51 | +mp_obj_t common_hal_alarm_get_wake_alarm(void) { |
| 52 | + switch (esp_sleep_get_wakeup_cause()) { |
| 53 | + case ESP_SLEEP_WAKEUP_TIMER: ; |
| 54 | + //Wake up from timer. |
| 55 | + alarm_time_obj_t *timer = m_new_obj(alarm_time_obj_t); |
| 56 | + timer->base.type = &alarm_time_type; |
| 57 | + return timer; |
| 58 | + case ESP_SLEEP_WAKEUP_EXT0: ; |
| 59 | + //Wake up from GPIO |
| 60 | + /*alarm_io_obj_t *ext0 = m_new_obj(alarm_io_obj_t); |
| 61 | + ext0->base.type = &alarm_io_type; |
| 62 | + return ext0;*/ |
| 63 | + return mp_obj_new_int(wake_io); |
| 64 | + case ESP_SLEEP_WAKEUP_EXT1: |
| 65 | + //Wake up from GPIO, returns -> esp_sleep_get_ext1_wakeup_status() |
| 66 | + /*uint64_t wakeup_pin_mask = esp_sleep_get_ext1_wakeup_status(); |
| 67 | + if (wakeup_pin_mask != 0) { |
| 68 | + int pin = __builtin_ffsll(wakeup_pin_mask) - 1; |
| 69 | + printf("Wake up from GPIO %d\n", pin); |
| 70 | + } else { |
| 71 | + printf("Wake up from GPIO\n"); |
| 72 | + }*/ |
| 73 | + break; |
| 74 | + case ESP_SLEEP_WAKEUP_TOUCHPAD: |
| 75 | + //TODO: implement TouchIO |
| 76 | + //Wake up from touch on pad, returns -> esp_sleep_get_touchpad_wakeup_status() |
| 77 | + break; |
| 78 | + case ESP_SLEEP_WAKEUP_UNDEFINED: |
| 79 | + default: |
| 80 | + //Not a deep sleep reset |
| 81 | + break; |
| 82 | + } |
| 83 | + return mp_const_none; |
| 84 | +} |
0 commit comments