diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index c4951f78f92ab..9702568df543a 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -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 \n" "Language-Team: LANGUAGE \n" @@ -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" @@ -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" @@ -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 "" @@ -3144,6 +3159,7 @@ 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 @@ -3151,6 +3167,7 @@ msgstr "" #: 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 "" @@ -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 "" @@ -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 "" @@ -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 "" @@ -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 "" @@ -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 "" diff --git a/main.c b/main.c index dbd367a5961f6..0aaaa2711bd35 100755 --- a/main.c +++ b/main.c @@ -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" @@ -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")); @@ -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); diff --git a/ports/atmel-samd/common-hal/microcontroller/__init__.c b/ports/atmel-samd/common-hal/microcontroller/__init__.c index 09212a0d125a7..f41494b5bfe05 100644 --- a/ports/atmel-samd/common-hal/microcontroller/__init__.c +++ b/ports/atmel-samd/common-hal/microcontroller/__init__.c @@ -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 = { diff --git a/ports/cxd56/common-hal/microcontroller/__init__.c b/ports/cxd56/common-hal/microcontroller/__init__.c index 7aa3b839d7801..57140dec7041f 100644 --- a/ports/cxd56/common-hal/microcontroller/__init__.c +++ b/ports/cxd56/common-hal/microcontroller/__init__.c @@ -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) }, diff --git a/ports/esp32s2/common-hal/alarm/__init__.c b/ports/esp32s2/common-hal/alarm/__init__.c new file mode 100644 index 0000000000000..552ad4452bf46 --- /dev/null +++ b/ports/esp32s2/common-hal/alarm/__init__.c @@ -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; +} diff --git a/ports/esp32s2/common-hal/alarm_io/__init__.c b/ports/esp32s2/common-hal/alarm_io/__init__.c new file mode 100644 index 0000000000000..b39693c6af657 --- /dev/null +++ b/ports/esp32s2/common-hal/alarm_io/__init__.c @@ -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); +} diff --git a/ports/esp32s2/common-hal/alarm_time/__init__.c b/ports/esp32s2/common-hal/alarm_time/__init__.c new file mode 100644 index 0000000000000..252b6e107cdb5 --- /dev/null +++ b/ports/esp32s2/common-hal/alarm_time/__init__.c @@ -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); +} diff --git a/ports/esp32s2/common-hal/alarm_touch/__init__.c b/ports/esp32s2/common-hal/alarm_touch/__init__.c new file mode 100644 index 0000000000000..df32b269306a3 --- /dev/null +++ b/ports/esp32s2/common-hal/alarm_touch/__init__.c @@ -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); +} diff --git a/ports/esp32s2/common-hal/microcontroller/__init__.c b/ports/esp32s2/common-hal/microcontroller/__init__.c index 6b2e18673db1d..ab0e1bfaa47cf 100644 --- a/ports/esp32s2/common-hal/microcontroller/__init__.c +++ b/ports/esp32s2/common-hal/microcontroller/__init__.c @@ -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" @@ -41,6 +41,8 @@ #include "freertos/FreeRTOS.h" +#include "esp_sleep.h" + void common_hal_mcu_delay_us(uint32_t delay) { } @@ -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 = { diff --git a/ports/esp32s2/mpconfigport.mk b/ports/esp32s2/mpconfigport.mk index bbaabb1c3defc..a6a4522cf1632 100644 --- a/ports/esp32s2/mpconfigport.mk +++ b/ports/esp32s2/mpconfigport.mk @@ -14,6 +14,10 @@ LONGINT_IMPL = MPZ # These modules are implemented in ports//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 @@ -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 diff --git a/ports/esp32s2/supervisor/port.c b/ports/esp32s2/supervisor/port.c index 4290b9bb4a899..be98d94b1f7b2 100644 --- a/ports/esp32s2/supervisor/port.c +++ b/ports/esp32s2/supervisor/port.c @@ -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" diff --git a/ports/litex/common-hal/microcontroller/__init__.c b/ports/litex/common-hal/microcontroller/__init__.c index 3c91661144b81..e6f50ed5a6abe 100644 --- a/ports/litex/common-hal/microcontroller/__init__.c +++ b/ports/litex/common-hal/microcontroller/__init__.c @@ -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 = { diff --git a/ports/mimxrt10xx/common-hal/microcontroller/__init__.c b/ports/mimxrt10xx/common-hal/microcontroller/__init__.c index 6a8537e2da17b..0329ced69b5d9 100644 --- a/ports/mimxrt10xx/common-hal/microcontroller/__init__.c +++ b/ports/mimxrt10xx/common-hal/microcontroller/__init__.c @@ -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 = { diff --git a/ports/nrf/common-hal/microcontroller/__init__.c b/ports/nrf/common-hal/microcontroller/__init__.c index 06aac9409dc1f..9911896bff751 100644 --- a/ports/nrf/common-hal/microcontroller/__init__.c +++ b/ports/nrf/common-hal/microcontroller/__init__.c @@ -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 = { diff --git a/ports/stm/common-hal/microcontroller/__init__.c b/ports/stm/common-hal/microcontroller/__init__.c index a827399ccb05f..bc81b0e4f5e90 100644 --- a/ports/stm/common-hal/microcontroller/__init__.c +++ b/ports/stm/common-hal/microcontroller/__init__.c @@ -81,6 +81,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 = { diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index 76c0a4db6752b..8b32bb5a2464c 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -102,6 +102,18 @@ endif ifeq ($(CIRCUITPY_AESIO),1) SRC_PATTERNS += aesio/% endif +ifeq ($(CIRCUITPY_ALARM),1) +SRC_PATTERNS += alarm/% +endif +ifeq ($(CIRCUITPY_ALARM_IO),1) +SRC_PATTERNS += alarm_io/% +endif +ifeq ($(CIRCUITPY_ALARM_TIME),1) +SRC_PATTERNS += alarm_time/% +endif +ifeq ($(CIRCUITPY_ALARM_TOUCH),1) +SRC_PATTERNS += alarm_touch/% +endif ifeq ($(CIRCUITPY_ANALOGIO),1) SRC_PATTERNS += analogio/% endif @@ -301,6 +313,10 @@ SRC_COMMON_HAL_ALL = \ _bleio/__init__.c \ _pew/PewPew.c \ _pew/__init__.c \ + alarm/__init__.c \ + alarm_io/__init__.c \ + alarm_time/__init__.c \ + alarm_touch/__init__.c \ analogio/AnalogIn.c \ analogio/AnalogOut.c \ analogio/__init__.c \ diff --git a/py/circuitpy_mpconfig.h b/py/circuitpy_mpconfig.h index 0583ae1c97a43..fe1b7a299b7b9 100644 --- a/py/circuitpy_mpconfig.h +++ b/py/circuitpy_mpconfig.h @@ -238,6 +238,34 @@ extern const struct _mp_obj_module_t aesio_module; #define AESIO_MODULE #endif +#if CIRCUITPY_ALARM +extern const struct _mp_obj_module_t alarm_module; +#define ALARM_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_alarm), (mp_obj_t)&alarm_module }, +#else +#define ALARM_MODULE +#endif + +#if CIRCUITPY_ALARM_IO +extern const struct _mp_obj_module_t alarm_io_module; +#define ALARM_IO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_alarm_io), (mp_obj_t)&alarm_io_module }, +#else +#define ALARM_IO_MODULE +#endif + +#if CIRCUITPY_ALARM_TIME +extern const struct _mp_obj_module_t alarm_time_module; +#define ALARM_TIME_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_alarm_time), (mp_obj_t)&alarm_time_module }, +#else +#define ALARM_TIME_MODULE +#endif + +#if CIRCUITPY_ALARM_TOUCH +extern const struct _mp_obj_module_t alarm_touch_module; +#define ALARM_TOUCH_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_alarm_touch), (mp_obj_t)&alarm_touch_module }, +#else +#define ALARM_TOUCH_MODULE +#endif + #if CIRCUITPY_ANALOGIO #define ANALOGIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_analogio), (mp_obj_t)&analogio_module }, extern const struct _mp_obj_module_t analogio_module; @@ -761,6 +789,10 @@ extern const struct _mp_obj_module_t wifi_module; // Some are omitted because they're in MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS above. #define MICROPY_PORT_BUILTIN_MODULES_STRONG_LINKS \ AESIO_MODULE \ + ALARM_MODULE \ + ALARM_IO_MODULE \ + ALARM_TIME_MODULE \ + ALARM_TOUCH_MODULE \ ANALOGIO_MODULE \ AUDIOBUSIO_MODULE \ AUDIOCORE_MODULE \ diff --git a/py/circuitpy_mpconfig.mk b/py/circuitpy_mpconfig.mk index 9b9bd83b4d54f..6d6cac0204f51 100644 --- a/py/circuitpy_mpconfig.mk +++ b/py/circuitpy_mpconfig.mk @@ -36,6 +36,18 @@ CFLAGS += -DCIRCUITPY_FULL_BUILD=$(CIRCUITPY_FULL_BUILD) CIRCUITPY_AESIO ?= 0 CFLAGS += -DCIRCUITPY_AESIO=$(CIRCUITPY_AESIO) +CIRCUITPY_ALARM ?= 0 +CFLAGS += -DCIRCUITPY_ALARM=$(CIRCUITPY_ALARM) + +CIRCUITPY_ALARM_IO ?= 0 +CFLAGS += -DCIRCUITPY_ALARM_IO=$(CIRCUITPY_ALARM_IO) + +CIRCUITPY_ALARM_TIME ?= 0 +CFLAGS += -DCIRCUITPY_ALARM_TIME=$(CIRCUITPY_ALARM_TIME) + +CIRCUITPY_ALARM_TOUCH ?= 0 +CFLAGS += -DCIRCUITPY_ALARM_TOUCH=$(CIRCUITPY_ALARM_TOUCH) + CIRCUITPY_ANALOGIO ?= 1 CFLAGS += -DCIRCUITPY_ANALOGIO=$(CIRCUITPY_ANALOGIO) diff --git a/shared-bindings/alarm/__init__.c b/shared-bindings/alarm/__init__.c new file mode 100644 index 0000000000000..f43c2ea12de95 --- /dev/null +++ b/shared-bindings/alarm/__init__.c @@ -0,0 +1,29 @@ +#include "shared-bindings/alarm/__init__.h" + +//| """alarm module +//| +//| The `alarm` module implements deep sleep.""" + +STATIC mp_obj_t alarm_disable_all(void) { + common_hal_alarm_disable_all(); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_0(alarm_disable_all_obj, alarm_disable_all); + +STATIC mp_obj_t alarm_get_wake_alarm(void) { + return common_hal_alarm_get_wake_alarm(); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_0(alarm_get_wake_alarm_obj, alarm_get_wake_alarm); + +STATIC const mp_rom_map_elem_t alarm_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_alarm) }, + { MP_ROM_QSTR(MP_QSTR_Disable), MP_ROM_PTR(&alarm_disable_all_obj) }, + { MP_ROM_QSTR(MP_QSTR_get_wake_alarm), MP_ROM_PTR(&alarm_get_wake_alarm_obj) }, +}; + +STATIC MP_DEFINE_CONST_DICT(alarm_module_globals, alarm_module_globals_table); + +const mp_obj_module_t alarm_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&alarm_module_globals, +}; diff --git a/shared-bindings/alarm/__init__.h b/shared-bindings/alarm/__init__.h new file mode 100644 index 0000000000000..db3966ac5d28f --- /dev/null +++ b/shared-bindings/alarm/__init__.h @@ -0,0 +1,9 @@ +#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_ALARM___INIT___H +#define MICROPY_INCLUDED_SHARED_BINDINGS_ALARM___INIT___H + +#include "py/obj.h" + +extern void common_hal_alarm_disable_all(void); +extern mp_obj_t common_hal_alarm_get_wake_alarm(void); + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_ALARM___INIT___H diff --git a/shared-bindings/alarm_io/__init__.c b/shared-bindings/alarm_io/__init__.c new file mode 100644 index 0000000000000..4e42f9a2e1606 --- /dev/null +++ b/shared-bindings/alarm_io/__init__.c @@ -0,0 +1,53 @@ +#include "py/obj.h" + +#include "shared-bindings/alarm_io/__init__.h" +#include "shared-bindings/microcontroller/Pin.h" + +//| """alarm_io module +//| +//| The `alarm_io` module implements deep sleep.""" + +STATIC mp_obj_t alarm_io_pin_state(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_level, ARG_pull }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_level, MP_ARG_INT | MP_ARG_KW_ONLY | MP_ARG_REQUIRED }, + { MP_QSTR_pull, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_bool = false} }, + }; + + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + mcu_pin_obj_t *pin = validate_obj_is_pin(pos_args[0]); + alarm_io_obj_t *self = m_new_obj(alarm_io_obj_t); + + self->base.type = &alarm_io_type; + self->gpio = pin->number; + self->level = args[ARG_level].u_int; + self->pull = args[ARG_pull].u_bool; + + return common_hal_alarm_io_pin_state(self); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(alarm_io_pin_state_obj, 1, alarm_io_pin_state); + +STATIC mp_obj_t alarm_io_disable(void) { + common_hal_alarm_io_disable(); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_0(alarm_io_disable_obj, alarm_io_disable); + +STATIC const mp_rom_map_elem_t alarm_io_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_alarm_io) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_PinState), MP_ROM_PTR(&alarm_io_pin_state_obj) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_Disable), MP_ROM_PTR(&alarm_io_disable_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(alarm_io_module_globals, alarm_io_module_globals_table); + +const mp_obj_module_t alarm_io_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&alarm_io_module_globals, +}; + +const mp_obj_type_t alarm_io_type = { + { &mp_type_type }, + .name = MP_QSTR_ioAlarm, +}; diff --git a/shared-bindings/alarm_io/__init__.h b/shared-bindings/alarm_io/__init__.h new file mode 100644 index 0000000000000..0a53497c01f3e --- /dev/null +++ b/shared-bindings/alarm_io/__init__.h @@ -0,0 +1,17 @@ +#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_IO___INIT___H +#define MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_IO___INIT___H + +#include "py/runtime.h" + +typedef struct { + mp_obj_base_t base; + uint8_t gpio, level; + bool pull; +} alarm_io_obj_t; + +extern const mp_obj_type_t alarm_io_type; + +extern mp_obj_t common_hal_alarm_io_pin_state (alarm_io_obj_t *self_in); +extern void common_hal_alarm_io_disable (void); + +#endif //MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_IO___INIT___H diff --git a/shared-bindings/alarm_time/__init__.c b/shared-bindings/alarm_time/__init__.c new file mode 100644 index 0000000000000..1707f7488f342 --- /dev/null +++ b/shared-bindings/alarm_time/__init__.c @@ -0,0 +1,50 @@ +#include "py/obj.h" +#include "shared-bindings/alarm_time/__init__.h" + +//| """alarm_time module +//| +//| The `alarm_time` module implements deep sleep.""" + +STATIC mp_obj_t alarm_time_duration(mp_obj_t seconds_o) { + #if MICROPY_PY_BUILTINS_FLOAT + mp_float_t seconds = mp_obj_get_float(seconds_o); + mp_float_t msecs = 1000.0f * seconds + 0.5f; + #else + mp_int_t seconds = mp_obj_get_int(seconds_o); + mp_int_t msecs = 1000 * seconds; + #endif + + if (seconds < 0) { + mp_raise_ValueError(translate("sleep length must be non-negative")); + } + common_hal_alarm_time_duration(msecs); + + alarm_time_obj_t *self = m_new_obj(alarm_time_obj_t); + self->base.type = &alarm_time_type; + + return self; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(alarm_time_duration_obj, alarm_time_duration); + +STATIC mp_obj_t alarm_time_disable(void) { + common_hal_alarm_time_disable(); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_0(alarm_time_disable_obj, alarm_time_disable); + +STATIC const mp_rom_map_elem_t alarm_time_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_alarm_time) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_Duration), MP_ROM_PTR(&alarm_time_duration_obj) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_Disable), MP_ROM_PTR(&alarm_time_disable_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(alarm_time_module_globals, alarm_time_module_globals_table); + +const mp_obj_module_t alarm_time_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&alarm_time_module_globals, +}; + +const mp_obj_type_t alarm_time_type = { + { &mp_type_type }, + .name = MP_QSTR_timeAlarm, +}; diff --git a/shared-bindings/alarm_time/__init__.h b/shared-bindings/alarm_time/__init__.h new file mode 100644 index 0000000000000..a96383069339b --- /dev/null +++ b/shared-bindings/alarm_time/__init__.h @@ -0,0 +1,15 @@ +#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_TIME___INIT___H +#define MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_TIME___INIT___H + +#include "py/runtime.h" + +typedef struct { + mp_obj_base_t base; +} alarm_time_obj_t; + +extern const mp_obj_type_t alarm_time_type; + +extern void common_hal_alarm_time_duration (uint32_t); +extern void common_hal_alarm_time_disable (void); + +#endif //MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_TIME___INIT___H diff --git a/shared-bindings/alarm_touch/__init__.c b/shared-bindings/alarm_touch/__init__.c new file mode 100644 index 0000000000000..5f5a156d8012c --- /dev/null +++ b/shared-bindings/alarm_touch/__init__.c @@ -0,0 +1,28 @@ +#include "py/obj.h" +#include "shared-bindings/alarm_touch/__init__.h" + +//| """alarm_touch module +//| +//| The `alarm_touch` module implements deep sleep.""" + +STATIC mp_obj_t alarm_touch_disable(void) { + common_hal_alarm_touch_disable(); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_0(alarm_touch_disable_obj, alarm_touch_disable); + +STATIC const mp_rom_map_elem_t alarm_touch_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_alarm_touch) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_Disable), MP_ROM_PTR(&alarm_touch_disable_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(alarm_touch_module_globals, alarm_touch_module_globals_table); + +const mp_obj_module_t alarm_touch_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&alarm_touch_module_globals, +}; + +const mp_obj_type_t alarm_touch_type = { + { &mp_type_type }, + .name = MP_QSTR_touchAlarm, +}; diff --git a/shared-bindings/alarm_touch/__init__.h b/shared-bindings/alarm_touch/__init__.h new file mode 100644 index 0000000000000..600587d247e2c --- /dev/null +++ b/shared-bindings/alarm_touch/__init__.h @@ -0,0 +1,14 @@ +#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_TOUCH___INIT___H +#define MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_TOUCH___INIT___H + +#include "py/runtime.h" + +typedef struct { + mp_obj_base_t base; +} alarm_touch_obj_t; + +extern const mp_obj_type_t alarm_touch_type; + +extern void common_hal_alarm_touch_disable (void); + +#endif //MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_TOUCH___INIT___H diff --git a/shared-bindings/microcontroller/__init__.c b/shared-bindings/microcontroller/__init__.c index 2e58bdcc290f0..bbc1640f76146 100644 --- a/shared-bindings/microcontroller/__init__.c +++ b/shared-bindings/microcontroller/__init__.c @@ -39,7 +39,6 @@ #include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/microcontroller/Processor.h" -#include "py/runtime.h" #include "supervisor/shared/translate.h" //| """Pin references and cpu functionality @@ -136,6 +135,13 @@ STATIC mp_obj_t mcu_reset(void) { } STATIC MP_DEFINE_CONST_FUN_OBJ_0(mcu_reset_obj, mcu_reset); +STATIC mp_obj_t mcu_sleep(void) { + common_hal_mcu_deep_sleep(); + // We won't actually get here because mcu is going into sleep. + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mcu_sleep_obj, mcu_sleep); + //| nvm: Optional[ByteArray] //| """Available non-volatile memory. //| This object is the sole instance of `nvm.ByteArray` when available or ``None`` otherwise. @@ -171,6 +177,8 @@ STATIC const mp_rom_map_elem_t mcu_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_enable_interrupts), MP_ROM_PTR(&mcu_enable_interrupts_obj) }, { MP_ROM_QSTR(MP_QSTR_on_next_reset), MP_ROM_PTR(&mcu_on_next_reset_obj) }, { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&mcu_reset_obj) }, + //ToDo: Remove MP_QSTR_sleep when sleep on code.py exit implemented. + { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mcu_sleep_obj) }, #if CIRCUITPY_INTERNAL_NVM_SIZE > 0 { MP_ROM_QSTR(MP_QSTR_nvm), MP_ROM_PTR(&common_hal_mcu_nvm_obj) }, #else diff --git a/shared-bindings/microcontroller/__init__.h b/shared-bindings/microcontroller/__init__.h index 8abdff763c1ff..f5bcfaa08a5f1 100644 --- a/shared-bindings/microcontroller/__init__.h +++ b/shared-bindings/microcontroller/__init__.h @@ -28,8 +28,8 @@ #ifndef MICROPY_INCLUDED_SHARED_BINDINGS_MICROCONTROLLER___INIT___H #define MICROPY_INCLUDED_SHARED_BINDINGS_MICROCONTROLLER___INIT___H -#include "py/mpconfig.h" #include "py/obj.h" +#include "py/mpconfig.h" #include "common-hal/microcontroller/Processor.h" @@ -43,6 +43,8 @@ extern void common_hal_mcu_enable_interrupts(void); extern void common_hal_mcu_on_next_reset(mcu_runmode_t runmode); extern void common_hal_mcu_reset(void); +extern void common_hal_mcu_deep_sleep(void); + extern const mp_obj_dict_t mcu_pin_globals; extern const mcu_processor_obj_t common_hal_mcu_processor_obj;