Skip to content

Deep Sleep support on CPY #3467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions locale/circuitpython.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-09-29 20:14-0500\n"
"POT-Creation-Date: 2020-10-07 09:55+0530\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand All @@ -17,6 +17,13 @@ msgstr ""
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"

#: main.c
msgid ""
"\n"
"\n"
"------ soft reboot ------\n"
msgstr ""

#: main.c
msgid ""
"\n"
Expand Down Expand Up @@ -948,6 +955,10 @@ msgstr ""
msgid "I2SOut not available"
msgstr ""

#: ports/esp32s2/common-hal/alarm_io/__init__.c
msgid "IOs 0, 2 & 4 do not support internal pullup in sleep"
msgstr ""

#: shared-bindings/aesio/aes.c
#, c-format
msgid "IV must be %d bytes long"
Expand Down Expand Up @@ -2743,6 +2754,10 @@ msgstr ""
msgid "invalid syntax for number"
msgstr ""

#: ports/esp32s2/common-hal/alarm_io/__init__.c
msgid "io must be rtc io"
msgstr ""

#: py/objtype.c
msgid "issubclass() arg 1 must be a class"
msgstr ""
Expand Down Expand Up @@ -3144,13 +3159,15 @@ msgstr ""
msgid "pow() with 3 arguments requires integers"
msgstr ""

#: ports/esp32s2/boards/adafruit_metro_esp32s2/mpconfigboard.h
#: ports/esp32s2/boards/electroniccats_bastwifi/mpconfigboard.h
#: ports/esp32s2/boards/espressif_kaluga_1/mpconfigboard.h
#: ports/esp32s2/boards/espressif_saola_1_wroom/mpconfigboard.h
#: ports/esp32s2/boards/espressif_saola_1_wrover/mpconfigboard.h
#: ports/esp32s2/boards/microdev_micro_s2/mpconfigboard.h
#: ports/esp32s2/boards/muselab_nanoesp32_s2/mpconfigboard.h
#: ports/esp32s2/boards/unexpectedmaker_feathers2/mpconfigboard.h
#: ports/esp32s2/boards/unexpectedmaker_feathers2_prerelease/mpconfigboard.h
msgid "pressing boot button at start up.\n"
msgstr ""

Expand Down Expand Up @@ -3247,7 +3264,7 @@ msgstr ""
msgid "size is defined for ndarrays only"
msgstr ""

#: shared-bindings/time/__init__.c
#: shared-bindings/alarm_time/__init__.c shared-bindings/time/__init__.c
msgid "sleep length must be non-negative"
msgstr ""

Expand All @@ -3263,10 +3280,6 @@ msgstr ""
msgid "small int overflow"
msgstr ""

#: main.c
msgid "soft reboot\n"
msgstr ""

#: extmod/ulab/code/numerical/numerical.c
msgid "sort argument must be an ndarray"
msgstr ""
Expand Down Expand Up @@ -3347,6 +3360,10 @@ msgstr ""
msgid "threshold must be in the range 0-65536"
msgstr ""

#: ports/esp32s2/common-hal/alarm_time/__init__.c
msgid "time out of range"
msgstr ""

#: shared-bindings/time/__init__.c
msgid "time.struct_time() takes a 9-sequence"
msgstr ""
Expand Down Expand Up @@ -3392,6 +3409,10 @@ msgstr ""
msgid "trapz is defined for 1D arrays of equal length"
msgstr ""

#: ports/esp32s2/common-hal/alarm_io/__init__.c
msgid "trigger level must be 0 or 1"
msgstr ""

#: extmod/ulab/code/linalg/linalg.c
msgid "tuple index out of range"
msgstr ""
Expand Down Expand Up @@ -3520,6 +3541,10 @@ msgstr ""
msgid "vectors must have same lengths"
msgstr ""

#: ports/esp32s2/common-hal/alarm_io/__init__.c
msgid "wakeup conflict"
msgstr ""

#: shared-bindings/watchdog/WatchDogTimer.c
msgid "watchdog timeout must be greater than 0"
msgstr ""
Expand Down
18 changes: 17 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
#include "supervisor/shared/status_leds.h"
#include "supervisor/shared/stack.h"
#include "supervisor/serial.h"
#include "supervisor/usb.h"

#include "shared-bindings/microcontroller/__init__.h"

#include "boards/board.h"

Expand Down Expand Up @@ -298,6 +301,19 @@ bool run_code_py(safe_mode_t safe_mode) {
}
}

for (uint8_t i = 0; i<=100; i++) {
if (!usb_msc_ejected()) {
//Go into light sleep
break;
}
mp_hal_delay_ms(10);
}

if (usb_msc_ejected()) {
//Go into deep sleep
common_hal_mcu_deep_sleep();
}

// Display a different completion message if the user has no USB attached (cannot save files)
if (!serial_connected_at_start) {
serial_write_compressed(translate("\nCode done running. Waiting for reload.\n"));
Expand Down Expand Up @@ -513,7 +529,7 @@ int __attribute__((used)) main(void) {
}
if (exit_code == PYEXEC_FORCED_EXIT) {
if (!first_run) {
serial_write_compressed(translate("soft reboot\n"));
serial_write_compressed(translate("\n\n------ soft reboot ------\n"));
}
first_run = false;
skip_repl = run_code_py(safe_mode);
Expand Down
4 changes: 4 additions & 0 deletions ports/atmel-samd/common-hal/microcontroller/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ void common_hal_mcu_reset(void) {
reset();
}

void common_hal_mcu_deep_sleep(void) {
//deep sleep call here
}

// 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 = {
Expand Down
4 changes: 4 additions & 0 deletions ports/cxd56/common-hal/microcontroller/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ void common_hal_mcu_reset(void) {
boardctl(BOARDIOC_RESET, 0);
}

void common_hal_mcu_deep_sleep(void) {
//deep sleep call here
}

STATIC const mp_rom_map_elem_t mcu_pin_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_UART2_RXD), MP_ROM_PTR(&pin_UART2_RXD) },
{ MP_ROM_QSTR(MP_QSTR_UART2_TXD), MP_ROM_PTR(&pin_UART2_TXD) },
Expand Down
60 changes: 60 additions & 0 deletions ports/esp32s2/common-hal/alarm/__init__.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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 "shared-bindings/alarm/__init__.h"
#include "shared-bindings/alarm_io/__init__.h"
#include "shared-bindings/alarm_time/__init__.h"

#include "esp_sleep.h"

void common_hal_alarm_disable_all(void) {
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL);
}

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;
case ESP_SLEEP_WAKEUP_TOUCHPAD:
//TODO: implement TouchIO
//Wake up from touch on pad, esp_sleep_get_touchpad_wakeup_status()
break;
case ESP_SLEEP_WAKEUP_UNDEFINED:
default:
//Not a deep sleep reset
break;
}
return mp_const_none;
}
35 changes: 35 additions & 0 deletions ports/esp32s2/common-hal/alarm_io/__init__.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "shared-bindings/alarm_io/__init__.h"

#include "esp_sleep.h"
#include "driver/rtc_io.h"

mp_obj_t common_hal_alarm_io_pin_state (alarm_io_obj_t *self_in) {
if (!rtc_gpio_is_valid_gpio(self_in->gpio)) {
mp_raise_ValueError(translate("io must be rtc io"));
}

if (self_in->pull && !self_in->level) {
for (uint8_t i = 0; i<=4; i+=2) {
if (self_in->gpio == i) {
mp_raise_ValueError(translate("IOs 0, 2 & 4 do not support internal pullup in sleep"));
}
}
}

switch(esp_sleep_enable_ext0_wakeup(self_in->gpio, self_in->level)) {
case ESP_ERR_INVALID_ARG:
mp_raise_ValueError(translate("trigger level must be 0 or 1"));
case ESP_ERR_INVALID_STATE:
mp_raise_RuntimeError(translate("wakeup conflict"));
default:
break;
}

if (self_in->pull) { (self_in->level) ? rtc_gpio_pulldown_en(self_in->gpio) : rtc_gpio_pullup_en(self_in->gpio); }

return self_in;
}

void common_hal_alarm_io_disable (void) {
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_EXT0 | ESP_SLEEP_WAKEUP_EXT1);
}
13 changes: 13 additions & 0 deletions ports/esp32s2/common-hal/alarm_time/__init__.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "esp_sleep.h"

#include "shared-bindings/alarm_time/__init__.h"

void common_hal_alarm_time_duration (uint32_t ms) {
if (esp_sleep_enable_timer_wakeup((ms) * 1000) == ESP_ERR_INVALID_ARG) {
mp_raise_ValueError(translate("time out of range"));
}
}

void common_hal_alarm_time_disable (void) {
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_TIMER);
}
7 changes: 7 additions & 0 deletions ports/esp32s2/common-hal/alarm_touch/__init__.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "esp_sleep.h"

#include "shared-bindings/alarm_touch/__init__.h"

void common_hal_alarm_touch_disable (void) {
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_TOUCHPAD);
}
8 changes: 7 additions & 1 deletion ports/esp32s2/common-hal/microcontroller/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
* THE SOFTWARE.
*/

#include "py/mphal.h"
#include "py/obj.h"
#include "py/mphal.h"
#include "py/runtime.h"

#include "common-hal/microcontroller/Pin.h"
Expand All @@ -41,6 +41,8 @@

#include "freertos/FreeRTOS.h"

#include "esp_sleep.h"

void common_hal_mcu_delay_us(uint32_t delay) {

}
Expand Down Expand Up @@ -77,6 +79,10 @@ void common_hal_mcu_reset(void) {
while(1);
}

void common_hal_mcu_deep_sleep(void) {
esp_deep_sleep_start();
}

// 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 = {
Expand Down
5 changes: 5 additions & 0 deletions ports/esp32s2/mpconfigport.mk
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ 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_ALARM_TOUCH = 1
CIRCUITPY_ANALOGIO = 0
CIRCUITPY_AUDIOBUSIO = 0
CIRCUITPY_AUDIOIO = 0
Expand All @@ -22,6 +26,7 @@ CIRCUITPY_FREQUENCYIO = 0
CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_ROTARYIO = 0
CIRCUITPY_NVM = 0

# We don't have enough endpoints to include MIDI.
CIRCUITPY_USB_MIDI = 0
CIRCUITPY_WIFI = 1
Expand Down
4 changes: 2 additions & 2 deletions ports/esp32s2/supervisor/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#include "common-hal/microcontroller/Pin.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"
Expand Down
4 changes: 4 additions & 0 deletions ports/litex/common-hal/microcontroller/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ void common_hal_mcu_reset(void) {
while(1);
}

void common_hal_mcu_deep_sleep(void) {
//deep sleep call here
}

// 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 = {
Expand Down
4 changes: 4 additions & 0 deletions ports/mimxrt10xx/common-hal/microcontroller/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ void common_hal_mcu_reset(void) {
NVIC_SystemReset();
}

void common_hal_mcu_deep_sleep(void) {
//deep sleep call here
}

// 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 = {
Expand Down
4 changes: 4 additions & 0 deletions ports/nrf/common-hal/microcontroller/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ void common_hal_mcu_reset(void) {
reset_cpu();
}

void common_hal_mcu_deep_sleep(void) {
//deep sleep call here
}

// 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 = {
Expand Down
Loading