|
| 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" <[email protected]> |
| 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 "soc/rtc_cntl_reg.h" |
| 33 | +#include "driver/gpio.h" |
| 34 | +#include "driver/adc.h" |
| 35 | +#include "esp_heap_caps.h" |
| 36 | +#include "multi_heap.h" |
| 37 | +#include "esp_app_desc.h" |
| 38 | + |
| 39 | +#include "py/nlr.h" |
| 40 | +#include "py/obj.h" |
| 41 | +#include "py/runtime.h" |
| 42 | +#include "py/mphal.h" |
| 43 | +#include "shared/timeutils/timeutils.h" |
| 44 | +#include "modmachine.h" |
| 45 | +#include "machine_rtc.h" |
| 46 | +#include "modesp32.h" |
| 47 | + |
| 48 | +// These private includes are needed for idf_heap_info. |
| 49 | +#define MULTI_HEAP_FREERTOS |
| 50 | +#include "../multi_heap_platform.h" |
| 51 | +#include "../heap_private.h" |
| 52 | + |
| 53 | +STATIC mp_obj_t esp32_wake_on_touch(const mp_obj_t wake) { |
| 54 | + |
| 55 | + if (machine_rtc_config.ext0_pin != -1) { |
| 56 | + mp_raise_ValueError(MP_ERROR_TEXT("no resources")); |
| 57 | + } |
| 58 | + // mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("touchpad wakeup not available for this version of ESP-IDF")); |
| 59 | + |
| 60 | + machine_rtc_config.wake_on_touch = mp_obj_is_true(wake); |
| 61 | + return mp_const_none; |
| 62 | +} |
| 63 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32_wake_on_touch_obj, esp32_wake_on_touch); |
| 64 | + |
| 65 | +STATIC mp_obj_t esp32_wake_on_ext0(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 66 | + |
| 67 | + if (machine_rtc_config.wake_on_touch) { |
| 68 | + mp_raise_ValueError(MP_ERROR_TEXT("no resources")); |
| 69 | + } |
| 70 | + enum {ARG_pin, ARG_level}; |
| 71 | + const mp_arg_t allowed_args[] = { |
| 72 | + { MP_QSTR_pin, MP_ARG_OBJ, {.u_obj = mp_obj_new_int(machine_rtc_config.ext0_pin)} }, |
| 73 | + { MP_QSTR_level, MP_ARG_BOOL, {.u_bool = machine_rtc_config.ext0_level} }, |
| 74 | + }; |
| 75 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 76 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 77 | + |
| 78 | + if (args[ARG_pin].u_obj == mp_const_none) { |
| 79 | + machine_rtc_config.ext0_pin = -1; // "None" |
| 80 | + } else { |
| 81 | + gpio_num_t pin_id = machine_pin_get_id(args[ARG_pin].u_obj); |
| 82 | + if (pin_id != machine_rtc_config.ext0_pin) { |
| 83 | + if (!RTC_IS_VALID_EXT_PIN(pin_id)) { |
| 84 | + mp_raise_ValueError(MP_ERROR_TEXT("invalid pin")); |
| 85 | + } |
| 86 | + machine_rtc_config.ext0_pin = pin_id; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + machine_rtc_config.ext0_level = args[ARG_level].u_bool; |
| 91 | + machine_rtc_config.ext0_wake_types = MACHINE_WAKE_SLEEP | MACHINE_WAKE_DEEPSLEEP; |
| 92 | + |
| 93 | + return mp_const_none; |
| 94 | +} |
| 95 | +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(esp32_wake_on_ext0_obj, 0, esp32_wake_on_ext0); |
| 96 | + |
| 97 | +STATIC mp_obj_t esp32_wake_on_ext1(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 98 | + enum {ARG_pins, ARG_level}; |
| 99 | + const mp_arg_t allowed_args[] = { |
| 100 | + { MP_QSTR_pins, MP_ARG_OBJ, {.u_obj = mp_const_none} }, |
| 101 | + { MP_QSTR_level, MP_ARG_BOOL, {.u_bool = machine_rtc_config.ext1_level} }, |
| 102 | + }; |
| 103 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 104 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 105 | + uint64_t ext1_pins = machine_rtc_config.ext1_pins; |
| 106 | + |
| 107 | + |
| 108 | + // Check that all pins are allowed |
| 109 | + if (args[ARG_pins].u_obj != mp_const_none) { |
| 110 | + size_t len = 0; |
| 111 | + mp_obj_t *elem; |
| 112 | + mp_obj_get_array(args[ARG_pins].u_obj, &len, &elem); |
| 113 | + ext1_pins = 0; |
| 114 | + |
| 115 | + for (int i = 0; i < len; i++) { |
| 116 | + |
| 117 | + gpio_num_t pin_id = machine_pin_get_id(elem[i]); |
| 118 | + if (!RTC_IS_VALID_EXT_PIN(pin_id)) { |
| 119 | + mp_raise_ValueError(MP_ERROR_TEXT("invalid pin")); |
| 120 | + break; |
| 121 | + } |
| 122 | + ext1_pins |= (1ll << pin_id); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + machine_rtc_config.ext1_level = args[ARG_level].u_bool; |
| 127 | + machine_rtc_config.ext1_pins = ext1_pins; |
| 128 | + |
| 129 | + return mp_const_none; |
| 130 | +} |
| 131 | +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(esp32_wake_on_ext1_obj, 0, esp32_wake_on_ext1); |
| 132 | + |
| 133 | +STATIC mp_obj_t esp32_wake_on_ulp(const mp_obj_t wake) { |
| 134 | + if (machine_rtc_config.ext0_pin != -1) { |
| 135 | + mp_raise_ValueError(MP_ERROR_TEXT("no resources")); |
| 136 | + } |
| 137 | + machine_rtc_config.wake_on_ulp = mp_obj_is_true(wake); |
| 138 | + return mp_const_none; |
| 139 | +} |
| 140 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32_wake_on_ulp_obj, esp32_wake_on_ulp); |
| 141 | + |
| 142 | +STATIC mp_obj_t esp32_gpio_deep_sleep_hold(const mp_obj_t enable) { |
| 143 | + if (mp_obj_is_true(enable)) { |
| 144 | + gpio_deep_sleep_hold_en(); |
| 145 | + } else { |
| 146 | + gpio_deep_sleep_hold_dis(); |
| 147 | + } |
| 148 | + return mp_const_none; |
| 149 | +} |
| 150 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32_gpio_deep_sleep_hold_obj, esp32_gpio_deep_sleep_hold); |
| 151 | + |
| 152 | +#if CONFIG_IDF_TARGET_ESP32 |
| 153 | + |
| 154 | +#include "soc/sens_reg.h" |
| 155 | + |
| 156 | +STATIC mp_obj_t esp32_raw_temperature(void) { |
| 157 | + SET_PERI_REG_BITS(SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_SAR, 3, SENS_FORCE_XPD_SAR_S); |
| 158 | + SET_PERI_REG_BITS(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_CLK_DIV, 10, SENS_TSENS_CLK_DIV_S); |
| 159 | + CLEAR_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_POWER_UP); |
| 160 | + CLEAR_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_DUMP_OUT); |
| 161 | + SET_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_POWER_UP_FORCE); |
| 162 | + SET_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_POWER_UP); |
| 163 | + esp_rom_delay_us(100); |
| 164 | + SET_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_DUMP_OUT); |
| 165 | + esp_rom_delay_us(5); |
| 166 | + int res = GET_PERI_REG_BITS2(SENS_SAR_SLAVE_ADDR3_REG, SENS_TSENS_OUT, SENS_TSENS_OUT_S); |
| 167 | + |
| 168 | + return mp_obj_new_int(res); |
| 169 | +} |
| 170 | +STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32_raw_temperature_obj, esp32_raw_temperature); |
| 171 | + |
| 172 | +#endif |
| 173 | + |
| 174 | +STATIC mp_obj_t esp32_idf_heap_info(const mp_obj_t cap_in) { |
| 175 | + mp_int_t cap = mp_obj_get_int(cap_in); |
| 176 | + multi_heap_info_t info; |
| 177 | + heap_t *heap; |
| 178 | + mp_obj_t heap_list = mp_obj_new_list(0, 0); |
| 179 | + SLIST_FOREACH(heap, ®istered_heaps, next) { |
| 180 | + if (heap_caps_match(heap, cap)) { |
| 181 | + multi_heap_get_info(heap->heap, &info); |
| 182 | + mp_obj_t data[] = { |
| 183 | + MP_OBJ_NEW_SMALL_INT(heap->end - heap->start), // total heap size |
| 184 | + MP_OBJ_NEW_SMALL_INT(info.total_free_bytes), // total free bytes |
| 185 | + MP_OBJ_NEW_SMALL_INT(info.largest_free_block), // largest free contiguous |
| 186 | + MP_OBJ_NEW_SMALL_INT(info.minimum_free_bytes), // minimum free seen |
| 187 | + }; |
| 188 | + mp_obj_t this_heap = mp_obj_new_tuple(4, data); |
| 189 | + mp_obj_list_append(heap_list, this_heap); |
| 190 | + } |
| 191 | + } |
| 192 | + return heap_list; |
| 193 | +} |
| 194 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32_idf_heap_info_obj, esp32_idf_heap_info); |
| 195 | + |
| 196 | +STATIC mp_obj_t esp32_firmware_info(void) { |
| 197 | + |
| 198 | + const esp_app_desc_t *app_desc = esp_app_get_description(); |
| 199 | + |
| 200 | + mp_obj_t data[10]; |
| 201 | + data[0] = mp_obj_new_int(app_desc->magic_word); |
| 202 | + data[1] = mp_obj_new_int(app_desc->secure_version); |
| 203 | + // data[2] = mp_obj_new_int(app_desc->reserv1); |
| 204 | + data[2] = mp_const_none; |
| 205 | + data[3] = mp_obj_new_str(app_desc->version, strlen(app_desc->version)); |
| 206 | + data[4] = mp_obj_new_str(app_desc->project_name, strlen(app_desc->project_name)); |
| 207 | + data[5] = mp_obj_new_str(app_desc->time, strlen(app_desc->time)); |
| 208 | + data[6] = mp_obj_new_str(app_desc->date, strlen(app_desc->date)); |
| 209 | + data[7] = mp_obj_new_str(app_desc->idf_ver, strlen(app_desc->idf_ver)); |
| 210 | + char sha256[65] = { 0 }; |
| 211 | + for (int i = 0; i < 32; i++) { |
| 212 | + sprintf(sha256 + i * 2, "%02x", app_desc->app_elf_sha256[i]); |
| 213 | + } |
| 214 | + data[8] = mp_obj_new_str(sha256, strlen(sha256)); |
| 215 | + // data[9] = mp_obj_new_int(app_desc->reserv2); |
| 216 | + data[9] = mp_const_none; |
| 217 | + |
| 218 | + mp_obj_t desc_list = mp_obj_new_tuple(10, data); |
| 219 | + |
| 220 | + return desc_list; |
| 221 | +} |
| 222 | +STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32_firmware_info_obj, esp32_firmware_info); |
| 223 | + |
| 224 | +STATIC const mp_rom_map_elem_t esp32_module_globals_table[] = { |
| 225 | + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_esp32) }, |
| 226 | + |
| 227 | + { MP_ROM_QSTR(MP_QSTR_wake_on_touch), MP_ROM_PTR(&esp32_wake_on_touch_obj) }, |
| 228 | + { MP_ROM_QSTR(MP_QSTR_wake_on_ext0), MP_ROM_PTR(&esp32_wake_on_ext0_obj) }, |
| 229 | + { MP_ROM_QSTR(MP_QSTR_wake_on_ext1), MP_ROM_PTR(&esp32_wake_on_ext1_obj) }, |
| 230 | + { MP_ROM_QSTR(MP_QSTR_wake_on_ulp), MP_ROM_PTR(&esp32_wake_on_ulp_obj) }, |
| 231 | + { MP_ROM_QSTR(MP_QSTR_gpio_deep_sleep_hold), MP_ROM_PTR(&esp32_gpio_deep_sleep_hold_obj) }, |
| 232 | + #if CONFIG_IDF_TARGET_ESP32 |
| 233 | + { MP_ROM_QSTR(MP_QSTR_raw_temperature), MP_ROM_PTR(&esp32_raw_temperature_obj) }, |
| 234 | + #endif |
| 235 | + { MP_ROM_QSTR(MP_QSTR_idf_heap_info), MP_ROM_PTR(&esp32_idf_heap_info_obj) }, |
| 236 | + { MP_ROM_QSTR(MP_QSTR_firmware_info), MP_ROM_PTR(&esp32_firmware_info_obj) }, |
| 237 | + |
| 238 | + { MP_ROM_QSTR(MP_QSTR_NVS), MP_ROM_PTR(&esp32_nvs_type) }, |
| 239 | + { MP_ROM_QSTR(MP_QSTR_Partition), MP_ROM_PTR(&esp32_partition_type) }, |
| 240 | + { MP_ROM_QSTR(MP_QSTR_RMT), MP_ROM_PTR(&esp32_rmt_type) }, |
| 241 | + #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 |
| 242 | + { MP_ROM_QSTR(MP_QSTR_ULP), MP_ROM_PTR(&esp32_ulp_type) }, |
| 243 | + #endif |
| 244 | + |
| 245 | + { MP_ROM_QSTR(MP_QSTR_WAKEUP_ALL_LOW), MP_ROM_FALSE }, |
| 246 | + { MP_ROM_QSTR(MP_QSTR_WAKEUP_ANY_HIGH), MP_ROM_TRUE }, |
| 247 | + |
| 248 | + { MP_ROM_QSTR(MP_QSTR_HEAP_DATA), MP_ROM_INT(MALLOC_CAP_8BIT) }, |
| 249 | + { MP_ROM_QSTR(MP_QSTR_HEAP_EXEC), MP_ROM_INT(MALLOC_CAP_EXEC) }, |
| 250 | +}; |
| 251 | + |
| 252 | +STATIC MP_DEFINE_CONST_DICT(esp32_module_globals, esp32_module_globals_table); |
| 253 | + |
| 254 | +const mp_obj_module_t esp32_module = { |
| 255 | + .base = { &mp_type_module }, |
| 256 | + .globals = (mp_obj_dict_t *)&esp32_module_globals, |
| 257 | +}; |
| 258 | + |
| 259 | +MP_REGISTER_MODULE(MP_QSTR_esp32, esp32_module); |
0 commit comments