modesp32.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #include <stdio.h>
  27. #include <string.h>
  28. #include <time.h>
  29. #include <sys/time.h>
  30. #include "soc/rtc_cntl_reg.h"
  31. #include "soc/sens_reg.h"
  32. #include "driver/gpio.h"
  33. #include "py/nlr.h"
  34. #include "py/obj.h"
  35. #include "py/runtime.h"
  36. #include "py/mphal.h"
  37. #include "timeutils.h"
  38. #include "modmachine.h"
  39. #include "machine_rtc.h"
  40. #include "modesp32.h"
  41. STATIC mp_obj_t esp32_wake_on_touch(const mp_obj_t wake) {
  42. if (machine_rtc_config.ext0_pin != -1) {
  43. mp_raise_ValueError("no resources");
  44. }
  45. //nlr_raise(mp_obj_new_exception_msg(&mp_type_RuntimeError, "touchpad wakeup not available for this version of ESP-IDF"));
  46. machine_rtc_config.wake_on_touch = mp_obj_is_true(wake);
  47. return mp_const_none;
  48. }
  49. STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32_wake_on_touch_obj, esp32_wake_on_touch);
  50. STATIC mp_obj_t esp32_wake_on_ext0(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
  51. if (machine_rtc_config.wake_on_touch) {
  52. mp_raise_ValueError("no resources");
  53. }
  54. enum {ARG_pin, ARG_level};
  55. const mp_arg_t allowed_args[] = {
  56. { MP_QSTR_pin, MP_ARG_OBJ, {.u_obj = mp_obj_new_int(machine_rtc_config.ext0_pin)} },
  57. { MP_QSTR_level, MP_ARG_BOOL, {.u_bool = machine_rtc_config.ext0_level} },
  58. };
  59. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  60. mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
  61. if (args[ARG_pin].u_obj == mp_const_none) {
  62. machine_rtc_config.ext0_pin = -1; // "None"
  63. } else {
  64. gpio_num_t pin_id = machine_pin_get_id(args[ARG_pin].u_obj);
  65. if (pin_id != machine_rtc_config.ext0_pin) {
  66. if (!RTC_IS_VALID_EXT_PIN(pin_id)) {
  67. nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid pin"));
  68. }
  69. machine_rtc_config.ext0_pin = pin_id;
  70. }
  71. }
  72. machine_rtc_config.ext0_level = args[ARG_level].u_bool;
  73. machine_rtc_config.ext0_wake_types = MACHINE_WAKE_SLEEP | MACHINE_WAKE_DEEPSLEEP;
  74. return mp_const_none;
  75. }
  76. STATIC MP_DEFINE_CONST_FUN_OBJ_KW(esp32_wake_on_ext0_obj, 0, esp32_wake_on_ext0);
  77. STATIC mp_obj_t esp32_wake_on_ext1(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
  78. enum {ARG_pins, ARG_level};
  79. const mp_arg_t allowed_args[] = {
  80. { MP_QSTR_pins, MP_ARG_OBJ, {.u_obj = mp_const_none} },
  81. { MP_QSTR_level, MP_ARG_BOOL, {.u_bool = machine_rtc_config.ext1_level} },
  82. };
  83. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  84. mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
  85. uint64_t ext1_pins = machine_rtc_config.ext1_pins;
  86. // Check that all pins are allowed
  87. if (args[ARG_pins].u_obj != mp_const_none) {
  88. mp_uint_t len = 0;
  89. mp_obj_t *elem;
  90. mp_obj_get_array(args[ARG_pins].u_obj, &len, &elem);
  91. ext1_pins = 0;
  92. for (int i = 0; i < len; i++) {
  93. gpio_num_t pin_id = machine_pin_get_id(elem[i]);
  94. if (!RTC_IS_VALID_EXT_PIN(pin_id)) {
  95. nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid pin"));
  96. break;
  97. }
  98. ext1_pins |= (1ll << pin_id);
  99. }
  100. }
  101. machine_rtc_config.ext1_level = args[ARG_level].u_bool;
  102. machine_rtc_config.ext1_pins = ext1_pins;
  103. return mp_const_none;
  104. }
  105. STATIC MP_DEFINE_CONST_FUN_OBJ_KW(esp32_wake_on_ext1_obj, 0, esp32_wake_on_ext1);
  106. STATIC mp_obj_t esp32_raw_temperature(void) {
  107. SET_PERI_REG_BITS(SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_SAR, 3, SENS_FORCE_XPD_SAR_S);
  108. SET_PERI_REG_BITS(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_CLK_DIV, 10, SENS_TSENS_CLK_DIV_S);
  109. CLEAR_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_POWER_UP);
  110. CLEAR_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_DUMP_OUT);
  111. SET_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_POWER_UP_FORCE);
  112. SET_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_POWER_UP);
  113. ets_delay_us(100);
  114. SET_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_DUMP_OUT);
  115. ets_delay_us(5);
  116. int res = GET_PERI_REG_BITS2(SENS_SAR_SLAVE_ADDR3_REG, SENS_TSENS_OUT, SENS_TSENS_OUT_S);
  117. return mp_obj_new_int(res);
  118. }
  119. STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32_raw_temperature_obj, esp32_raw_temperature);
  120. STATIC const mp_rom_map_elem_t esp32_module_globals_table[] = {
  121. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_esp32) },
  122. { MP_ROM_QSTR(MP_QSTR_wake_on_touch), MP_ROM_PTR(&esp32_wake_on_touch_obj) },
  123. { MP_ROM_QSTR(MP_QSTR_wake_on_ext0), MP_ROM_PTR(&esp32_wake_on_ext0_obj) },
  124. { MP_ROM_QSTR(MP_QSTR_wake_on_ext1), MP_ROM_PTR(&esp32_wake_on_ext1_obj) },
  125. { MP_ROM_QSTR(MP_QSTR_raw_temperature), MP_ROM_PTR(&esp32_raw_temperature_obj) },
  126. { MP_ROM_QSTR(MP_QSTR_ULP), MP_ROM_PTR(&esp32_ulp_type) },
  127. { MP_ROM_QSTR(MP_QSTR_WAKEUP_ALL_LOW), MP_ROM_PTR(&mp_const_false_obj) },
  128. { MP_ROM_QSTR(MP_QSTR_WAKEUP_ANY_HIGH), MP_ROM_PTR(&mp_const_true_obj) },
  129. };
  130. STATIC MP_DEFINE_CONST_DICT(esp32_module_globals, esp32_module_globals_table);
  131. const mp_obj_module_t esp32_module = {
  132. .base = { &mp_type_module },
  133. .globals = (mp_obj_dict_t*)&esp32_module_globals,
  134. };