summaryrefslogtreecommitdiff
path: root/ports
diff options
context:
space:
mode:
authormicroDev <70126934+microDev1@users.noreply.github.com>2020-09-24 10:59:04 +0530
committerScott Shawcroft <scott@tannewt.org>2020-10-27 16:15:09 -0700
commit4d8ffdca8dc570f63c34b8f02856feae2d880688 (patch)
treeebfd20b8e28a556f9d908e09cf8710a753cc3431 /ports
parente5ff55b15c8cc67c2ece3b8857b5805109b858b3 (diff)
restructure alarm modules
Diffstat (limited to 'ports')
-rw-r--r--ports/esp32s2/common-hal/alarm/__init__.c84
-rw-r--r--ports/esp32s2/common-hal/alarm/__init__.h6
-rw-r--r--ports/esp32s2/common-hal/microcontroller/__init__.c48
-rw-r--r--ports/esp32s2/mpconfigport.mk6
-rw-r--r--ports/esp32s2/supervisor/port.c7
5 files changed, 100 insertions, 51 deletions
diff --git a/ports/esp32s2/common-hal/alarm/__init__.c b/ports/esp32s2/common-hal/alarm/__init__.c
new file mode 100644
index 000000000..90e8e9ecc
--- /dev/null
+++ b/ports/esp32s2/common-hal/alarm/__init__.c
@@ -0,0 +1,84 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
+ * Copyright (c) 2019 Lucian Copeland for Adafruit Industries
+ *
+ * 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.
+ */
+
+#include <sys/time.h>
+
+#include "common-hal/alarm/__init__.h"
+#include "shared-bindings/alarm/__init__.h"
+
+#include "esp_sleep.h"
+#include "soc/rtc_periph.h"
+#include "driver/rtc_io.h"
+
+static RTC_DATA_ATTR struct timeval sleep_enter_time;
+static RTC_DATA_ATTR struct timeval sleep_exit_time;
+static RTC_DATA_ATTR uint8_t wake_io;
+
+int common_hal_alarm_get_sleep_time(void) {
+ return (sleep_exit_time.tv_sec - sleep_enter_time.tv_sec) * 1000 + (sleep_exit_time.tv_usec - sleep_enter_time.tv_usec) / 1000;
+}
+
+void RTC_IRAM_ATTR esp_wake_deep_sleep(void) {
+ esp_default_wake_deep_sleep();
+ wake_io = rtc_gpio_get_level(6);
+ gettimeofday(&sleep_exit_time, NULL);
+}
+
+mp_obj_t common_hal_alarm_get_wake_alarm(void) {
+ switch (esp_sleep_get_wakeup_cause()) {
+ case ESP_SLEEP_WAKEUP_TIMER: ;
+ //Wake up from timer.
+ alarm_time_obj_t *timer = m_new_obj(alarm_time_obj_t);
+ timer->base.type = &alarm_time_type;
+ return timer;
+ case ESP_SLEEP_WAKEUP_EXT0: ;
+ //Wake up from GPIO
+ /*alarm_io_obj_t *ext0 = m_new_obj(alarm_io_obj_t);
+ ext0->base.type = &alarm_io_type;
+ return ext0;*/
+ return mp_obj_new_int(wake_io);
+ case ESP_SLEEP_WAKEUP_EXT1:
+ //Wake up from GPIO, returns -> esp_sleep_get_ext1_wakeup_status()
+ /*uint64_t wakeup_pin_mask = esp_sleep_get_ext1_wakeup_status();
+ if (wakeup_pin_mask != 0) {
+ int pin = __builtin_ffsll(wakeup_pin_mask) - 1;
+ printf("Wake up from GPIO %d\n", pin);
+ } else {
+ printf("Wake up from GPIO\n");
+ }*/
+ break;
+ case ESP_SLEEP_WAKEUP_TOUCHPAD:
+ //TODO: implement TouchIO
+ //Wake up from touch on pad, returns -> esp_sleep_get_touchpad_wakeup_status()
+ break;
+ case ESP_SLEEP_WAKEUP_UNDEFINED:
+ default:
+ //Not a deep sleep reset
+ break;
+ }
+ return mp_const_none;
+}
diff --git a/ports/esp32s2/common-hal/alarm/__init__.h b/ports/esp32s2/common-hal/alarm/__init__.h
new file mode 100644
index 000000000..8ba5e2b04
--- /dev/null
+++ b/ports/esp32s2/common-hal/alarm/__init__.h
@@ -0,0 +1,6 @@
+#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_MICROCONTROLLER_ALARM_H
+#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_MICROCONTROLLER_ALARM_H
+
+extern void esp_wake_deep_sleep(void);
+
+#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_MICROCONTROLLER_ALARM_H \ No newline at end of file
diff --git a/ports/esp32s2/common-hal/microcontroller/__init__.c b/ports/esp32s2/common-hal/microcontroller/__init__.c
index e79c60202..ba24e1c48 100644
--- a/ports/esp32s2/common-hal/microcontroller/__init__.c
+++ b/ports/esp32s2/common-hal/microcontroller/__init__.c
@@ -25,10 +25,8 @@
* THE SOFTWARE.
*/
-#include <sys/time.h>
-
-#include "py/mphal.h"
#include "py/obj.h"
+#include "py/mphal.h"
#include "py/runtime.h"
#include "common-hal/microcontroller/Pin.h"
@@ -44,9 +42,6 @@
#include "freertos/FreeRTOS.h"
#include "esp_sleep.h"
-#include "soc/rtc_periph.h"
-
-static RTC_DATA_ATTR struct timeval sleep_enter_time;
void common_hal_mcu_delay_us(uint32_t delay) {
@@ -85,50 +80,9 @@ void common_hal_mcu_reset(void) {
}
void common_hal_mcu_sleep(void) {
- gettimeofday(&sleep_enter_time, NULL);
esp_deep_sleep_start();
}
-int common_hal_mcu_get_sleep_time(void) {
- struct timeval now;
- gettimeofday(&now, NULL);
- return (now.tv_sec - sleep_enter_time.tv_sec) * 1000 + (now.tv_usec - sleep_enter_time.tv_usec) / 1000;
-}
-
-mp_obj_t common_hal_mcu_get_wake_alarm(void) {
- switch (esp_sleep_get_wakeup_cause()) {
- case ESP_SLEEP_WAKEUP_TIMER: ;
- //Wake up from timer.
- alarm_time_obj_t *timer = m_new_obj(alarm_time_obj_t);
- timer->base.type = &alarm_time_type;
- return timer;
- case ESP_SLEEP_WAKEUP_EXT0: ;
- //Wake up from GPIO
- alarm_io_obj_t *ext0 = m_new_obj(alarm_io_obj_t);
- ext0->base.type = &alarm_io_type;
- return ext0;
- case ESP_SLEEP_WAKEUP_EXT1:
- //Wake up from GPIO, returns -> esp_sleep_get_ext1_wakeup_status()
- /*uint64_t wakeup_pin_mask = esp_sleep_get_ext1_wakeup_status();
- if (wakeup_pin_mask != 0) {
- int pin = __builtin_ffsll(wakeup_pin_mask) - 1;
- printf("Wake up from GPIO %d\n", pin);
- } else {
- printf("Wake up from GPIO\n");
- }*/
- break;
- case ESP_SLEEP_WAKEUP_TOUCHPAD:
- //TODO: implement TouchIO
- //Wake up from touch on pad, returns -> esp_sleep_get_touchpad_wakeup_status()
- break;
- case ESP_SLEEP_WAKEUP_UNDEFINED:
- default:
- //Not a deep sleep reset
- break;
- }
- return mp_const_none;
-}
-
// The singleton microcontroller.Processor object, bound to microcontroller.cpu
// It currently only has properties, and no state.
const mcu_processor_obj_t common_hal_mcu_processor_obj = {
diff --git a/ports/esp32s2/mpconfigport.mk b/ports/esp32s2/mpconfigport.mk
index 4aaf1d00c..1a78821d3 100644
--- a/ports/esp32s2/mpconfigport.mk
+++ b/ports/esp32s2/mpconfigport.mk
@@ -14,6 +14,9 @@ LONGINT_IMPL = MPZ
# These modules are implemented in ports/<port>/common-hal:
CIRCUITPY_FULL_BUILD = 1
+CIRCUITPY_ALARM = 1
+CIRCUITPY_ALARM_IO = 1
+CIRCUITPY_ALARM_TIME = 1
CIRCUITPY_AUDIOBUSIO = 0
CIRCUITPY_AUDIOIO = 0
CIRCUITPY_CANIO = 1
@@ -22,8 +25,7 @@ CIRCUITPY_FREQUENCYIO = 0
CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_ROTARYIO = 0
CIRCUITPY_NVM = 0
-CIRCUITPY_ALARM_TIME = 1
-CIRCUITPY_ALARM_IO = 1
+
# We don't have enough endpoints to include MIDI.
CIRCUITPY_USB_MIDI = 0
CIRCUITPY_WIFI = 1
diff --git a/ports/esp32s2/supervisor/port.c b/ports/esp32s2/supervisor/port.c
index 0b9c03f74..98c064a8b 100644
--- a/ports/esp32s2/supervisor/port.c
+++ b/ports/esp32s2/supervisor/port.c
@@ -34,13 +34,14 @@
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
-#include "common-hal/microcontroller/Pin.h"
+#include "common-hal/alarm/__init__.h"
#include "common-hal/analogio/AnalogOut.h"
#include "common-hal/busio/I2C.h"
#include "common-hal/busio/SPI.h"
#include "common-hal/busio/UART.h"
-#include "common-hal/pulseio/PulseIn.h"
#include "common-hal/pwmio/PWMOut.h"
+#include "common-hal/pulseio/PulseIn.h"
+#include "common-hal/microcontroller/Pin.h"
#include "common-hal/wifi/__init__.h"
#include "supervisor/memory.h"
#include "supervisor/shared/tick.h"
@@ -87,6 +88,8 @@ safe_mode_t port_init(void) {
return NO_HEAP;
}
+ esp_wake_deep_sleep();
+
return NO_SAFE_MODE;
}