|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2017 "Eric Poulsen" <eric@zyxod.com> |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <stdio.h> |
| 28 | +#include <string.h> |
| 29 | + |
| 30 | +#include <time.h> |
| 31 | +#include <sys/time.h> |
| 32 | +#include "driver/gpio.h" |
| 33 | + |
| 34 | +#include "py/nlr.h" |
| 35 | +#include "py/obj.h" |
| 36 | +#include "py/runtime.h" |
| 37 | +#include "py/mphal.h" |
| 38 | +#include "timeutils.h" |
| 39 | +#include "modmachine.h" |
| 40 | +#include "machine_rtc.h" |
| 41 | +#include "modesp32.h" |
| 42 | + |
| 43 | +extern machine_rtc_config_t machine_rtc_config; |
| 44 | + |
| 45 | +STATIC mp_obj_t esp32_wake_on_touch(const mp_obj_t wake) { |
| 46 | + |
| 47 | + if (machine_rtc_config.ext0_pin != -1) { |
| 48 | + mp_raise_ValueError("no resources"); |
| 49 | + } |
| 50 | + //nlr_raise(mp_obj_new_exception_msg(&mp_type_RuntimeError, "touchpad wakeup not available for this version of ESP-IDF")); |
| 51 | + |
| 52 | + machine_rtc_config.wake_on_touch = mp_obj_is_true(wake); |
| 53 | + return mp_const_none; |
| 54 | +} |
| 55 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32_wake_on_touch_obj, esp32_wake_on_touch); |
| 56 | + |
| 57 | +STATIC mp_obj_t esp32_wake_on_ext0(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 58 | + |
| 59 | + if (machine_rtc_config.wake_on_touch) { |
| 60 | + mp_raise_ValueError("no resources"); |
| 61 | + } |
| 62 | + enum {ARG_pin, ARG_level}; |
| 63 | + const mp_arg_t allowed_args[] = { |
| 64 | + { MP_QSTR_pin, MP_ARG_OBJ, {.u_obj = mp_obj_new_int(machine_rtc_config.ext0_pin)} }, |
| 65 | + { MP_QSTR_level, MP_ARG_BOOL, {.u_bool = machine_rtc_config.ext0_level} }, |
| 66 | + }; |
| 67 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 68 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 69 | + |
| 70 | + if (args[ARG_pin].u_obj == mp_const_none) { |
| 71 | + machine_rtc_config.ext0_pin = -1; // "None" |
| 72 | + } else { |
| 73 | + gpio_num_t pin_id = machine_pin_get_id(args[ARG_pin].u_obj); |
| 74 | + if (pin_id != machine_rtc_config.ext0_pin) { |
| 75 | + if (!RTC_IS_VALID_EXT_PIN(pin_id)) { |
| 76 | + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid pin")); |
| 77 | + } |
| 78 | + machine_rtc_config.ext0_pin = pin_id; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + machine_rtc_config.ext0_level = args[ARG_level].u_bool; |
| 83 | + machine_rtc_config.ext0_wake_types = MACHINE_WAKE_SLEEP | MACHINE_WAKE_DEEPSLEEP; |
| 84 | + |
| 85 | + return mp_const_none; |
| 86 | +} |
| 87 | +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(esp32_wake_on_ext0_obj, 0, esp32_wake_on_ext0); |
| 88 | + |
| 89 | +STATIC mp_obj_t esp32_wake_on_ext1(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 90 | + enum {ARG_pins, ARG_level}; |
| 91 | + const mp_arg_t allowed_args[] = { |
| 92 | + { MP_QSTR_pins, MP_ARG_OBJ, {.u_obj = mp_const_none} }, |
| 93 | + { MP_QSTR_level, MP_ARG_BOOL, {.u_bool = machine_rtc_config.ext1_level} }, |
| 94 | + }; |
| 95 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 96 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 97 | + uint64_t ext1_pins = machine_rtc_config.ext1_pins; |
| 98 | + |
| 99 | + |
| 100 | + // Check that all pins are allowed |
| 101 | + if (args[ARG_pins].u_obj != mp_const_none) { |
| 102 | + mp_uint_t len = 0; |
| 103 | + mp_obj_t *elem; |
| 104 | + mp_obj_get_array(args[ARG_pins].u_obj, &len, &elem); |
| 105 | + ext1_pins = 0; |
| 106 | + |
| 107 | + for (int i = 0; i < len; i++) { |
| 108 | + |
| 109 | + gpio_num_t pin_id = machine_pin_get_id(elem[i]); |
| 110 | + if (!RTC_IS_VALID_EXT_PIN(pin_id)) { |
| 111 | + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid pin")); |
| 112 | + break; |
| 113 | + } |
| 114 | + ext1_pins |= (1ll << pin_id); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + machine_rtc_config.ext1_level = args[ARG_level].u_bool; |
| 119 | + machine_rtc_config.ext1_pins = ext1_pins; |
| 120 | + |
| 121 | + return mp_const_none; |
| 122 | +} |
| 123 | +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(esp32_wake_on_ext1_obj, 0, esp32_wake_on_ext1); |
| 124 | + |
| 125 | +STATIC const mp_rom_map_elem_t esp32_module_globals_table[] = { |
| 126 | + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_esp32) }, |
| 127 | + |
| 128 | + { MP_OBJ_NEW_QSTR(MP_QSTR_wake_on_touch), (mp_obj_t)&esp32_wake_on_touch_obj }, |
| 129 | + { MP_OBJ_NEW_QSTR(MP_QSTR_wake_on_ext0), (mp_obj_t)&esp32_wake_on_ext0_obj }, |
| 130 | + { MP_OBJ_NEW_QSTR(MP_QSTR_wake_on_ext1), (mp_obj_t)&esp32_wake_on_ext1_obj }, |
| 131 | + { MP_OBJ_NEW_QSTR(MP_QSTR_WAKEUP_ALL_LOW), mp_const_false }, |
| 132 | + { MP_OBJ_NEW_QSTR(MP_QSTR_WAKEUP_ANY_HIGH), mp_const_true }, |
| 133 | +}; |
| 134 | + |
| 135 | +STATIC MP_DEFINE_CONST_DICT(esp32_module_globals, esp32_module_globals_table); |
| 136 | + |
| 137 | +const mp_obj_module_t esp32_module = { |
| 138 | + .base = { &mp_type_module }, |
| 139 | + .globals = (mp_obj_dict_t*)&esp32_module_globals, |
| 140 | +}; |
0 commit comments