machine_pin.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * Development of the code in this file was sponsored by Microbric Pty Ltd
  5. *
  6. * The MIT License (MIT)
  7. *
  8. * Copyright (c) 2016 Damien P. George
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this software and associated documentation files (the "Software"), to deal
  12. * in the Software without restriction, including without limitation the rights
  13. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in
  18. * all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. * THE SOFTWARE.
  27. */
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include "driver/gpio.h"
  31. #include "py/runtime.h"
  32. #include "py/mphal.h"
  33. #include "mphalport.h"
  34. #include "modmachine.h"
  35. #include "extmod/virtpin.h"
  36. #include "machine_rtc.h"
  37. #include "modesp32.h"
  38. typedef struct _machine_pin_obj_t {
  39. mp_obj_base_t base;
  40. gpio_num_t id;
  41. } machine_pin_obj_t;
  42. typedef struct _machine_pin_irq_obj_t {
  43. mp_obj_base_t base;
  44. gpio_num_t id;
  45. } machine_pin_irq_obj_t;
  46. STATIC const machine_pin_obj_t machine_pin_obj[] = {
  47. {{&machine_pin_type}, GPIO_NUM_0},
  48. {{&machine_pin_type}, GPIO_NUM_1},
  49. {{&machine_pin_type}, GPIO_NUM_2},
  50. {{&machine_pin_type}, GPIO_NUM_3},
  51. {{&machine_pin_type}, GPIO_NUM_4},
  52. {{&machine_pin_type}, GPIO_NUM_5},
  53. {{&machine_pin_type}, GPIO_NUM_6},
  54. {{&machine_pin_type}, GPIO_NUM_7},
  55. {{&machine_pin_type}, GPIO_NUM_8},
  56. {{&machine_pin_type}, GPIO_NUM_9},
  57. {{&machine_pin_type}, GPIO_NUM_10},
  58. {{&machine_pin_type}, GPIO_NUM_11},
  59. {{&machine_pin_type}, GPIO_NUM_12},
  60. {{&machine_pin_type}, GPIO_NUM_13},
  61. {{&machine_pin_type}, GPIO_NUM_14},
  62. {{&machine_pin_type}, GPIO_NUM_15},
  63. {{&machine_pin_type}, GPIO_NUM_16},
  64. {{&machine_pin_type}, GPIO_NUM_17},
  65. {{&machine_pin_type}, GPIO_NUM_18},
  66. {{&machine_pin_type}, GPIO_NUM_19},
  67. {{NULL}, -1},
  68. {{&machine_pin_type}, GPIO_NUM_21},
  69. {{&machine_pin_type}, GPIO_NUM_22},
  70. {{&machine_pin_type}, GPIO_NUM_23},
  71. {{NULL}, -1},
  72. {{&machine_pin_type}, GPIO_NUM_25},
  73. {{&machine_pin_type}, GPIO_NUM_26},
  74. {{&machine_pin_type}, GPIO_NUM_27},
  75. {{NULL}, -1},
  76. {{NULL}, -1},
  77. {{NULL}, -1},
  78. {{NULL}, -1},
  79. {{&machine_pin_type}, GPIO_NUM_32},
  80. {{&machine_pin_type}, GPIO_NUM_33},
  81. {{&machine_pin_type}, GPIO_NUM_34},
  82. {{&machine_pin_type}, GPIO_NUM_35},
  83. {{&machine_pin_type}, GPIO_NUM_36},
  84. {{&machine_pin_type}, GPIO_NUM_37},
  85. {{&machine_pin_type}, GPIO_NUM_38},
  86. {{&machine_pin_type}, GPIO_NUM_39},
  87. };
  88. // forward declaration
  89. STATIC const machine_pin_irq_obj_t machine_pin_irq_object[];
  90. void machine_pins_init(void) {
  91. static bool did_install = false;
  92. if (!did_install) {
  93. gpio_install_isr_service(0);
  94. did_install = true;
  95. }
  96. memset(&MP_STATE_PORT(machine_pin_irq_handler[0]), 0, sizeof(MP_STATE_PORT(machine_pin_irq_handler)));
  97. }
  98. void machine_pins_deinit(void) {
  99. for (int i = 0; i < MP_ARRAY_SIZE(machine_pin_obj); ++i) {
  100. if (machine_pin_obj[i].id != (gpio_num_t)-1) {
  101. gpio_isr_handler_remove(machine_pin_obj[i].id);
  102. }
  103. }
  104. }
  105. STATIC void IRAM_ATTR machine_pin_isr_handler(void *arg) {
  106. machine_pin_obj_t *self = arg;
  107. mp_obj_t handler = MP_STATE_PORT(machine_pin_irq_handler)[self->id];
  108. mp_sched_schedule(handler, MP_OBJ_FROM_PTR(self));
  109. mp_hal_wake_main_task_from_isr();
  110. }
  111. gpio_num_t machine_pin_get_id(mp_obj_t pin_in) {
  112. if (mp_obj_get_type(pin_in) != &machine_pin_type) {
  113. mp_raise_ValueError("expecting a pin");
  114. }
  115. machine_pin_obj_t *self = pin_in;
  116. return self->id;
  117. }
  118. STATIC void machine_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  119. machine_pin_obj_t *self = self_in;
  120. mp_printf(print, "Pin(%u)", self->id);
  121. }
  122. // pin.init(mode, pull=None, *, value)
  123. STATIC mp_obj_t machine_pin_obj_init_helper(const machine_pin_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
  124. enum { ARG_mode, ARG_pull, ARG_value };
  125. static const mp_arg_t allowed_args[] = {
  126. { MP_QSTR_mode, MP_ARG_OBJ, {.u_obj = mp_const_none}},
  127. { MP_QSTR_pull, MP_ARG_OBJ, {.u_obj = mp_const_none}},
  128. { MP_QSTR_value, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}},
  129. };
  130. // parse args
  131. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  132. mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
  133. // configure the pin for gpio
  134. gpio_pad_select_gpio(self->id);
  135. // set initial value (do this before configuring mode/pull)
  136. if (args[ARG_value].u_obj != MP_OBJ_NULL) {
  137. gpio_set_level(self->id, mp_obj_is_true(args[ARG_value].u_obj));
  138. }
  139. // configure mode
  140. if (args[ARG_mode].u_obj != mp_const_none) {
  141. mp_int_t pin_io_mode = mp_obj_get_int(args[ARG_mode].u_obj);
  142. if (self->id >= 34 && (pin_io_mode & GPIO_MODE_DEF_OUTPUT)) {
  143. mp_raise_ValueError("pin can only be input");
  144. } else {
  145. gpio_set_direction(self->id, pin_io_mode);
  146. }
  147. }
  148. // configure pull
  149. if (args[ARG_pull].u_obj != mp_const_none) {
  150. gpio_set_pull_mode(self->id, mp_obj_get_int(args[ARG_pull].u_obj));
  151. }
  152. return mp_const_none;
  153. }
  154. // constructor(id, ...)
  155. mp_obj_t mp_pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  156. mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
  157. // get the wanted pin object
  158. int wanted_pin = mp_obj_get_int(args[0]);
  159. const machine_pin_obj_t *self = NULL;
  160. if (0 <= wanted_pin && wanted_pin < MP_ARRAY_SIZE(machine_pin_obj)) {
  161. self = (machine_pin_obj_t*)&machine_pin_obj[wanted_pin];
  162. }
  163. if (self == NULL || self->base.type == NULL) {
  164. mp_raise_ValueError("invalid pin");
  165. }
  166. if (n_args > 1 || n_kw > 0) {
  167. // pin mode given, so configure this GPIO
  168. mp_map_t kw_args;
  169. mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
  170. machine_pin_obj_init_helper(self, n_args - 1, args + 1, &kw_args);
  171. }
  172. return MP_OBJ_FROM_PTR(self);
  173. }
  174. // fast method for getting/setting pin value
  175. STATIC mp_obj_t machine_pin_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  176. mp_arg_check_num(n_args, n_kw, 0, 1, false);
  177. machine_pin_obj_t *self = self_in;
  178. if (n_args == 0) {
  179. // get pin
  180. return MP_OBJ_NEW_SMALL_INT(gpio_get_level(self->id));
  181. } else {
  182. // set pin
  183. gpio_set_level(self->id, mp_obj_is_true(args[0]));
  184. return mp_const_none;
  185. }
  186. }
  187. // pin.init(mode, pull)
  188. STATIC mp_obj_t machine_pin_obj_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
  189. return machine_pin_obj_init_helper(args[0], n_args - 1, args + 1, kw_args);
  190. }
  191. MP_DEFINE_CONST_FUN_OBJ_KW(machine_pin_init_obj, 1, machine_pin_obj_init);
  192. // pin.value([value])
  193. STATIC mp_obj_t machine_pin_value(size_t n_args, const mp_obj_t *args) {
  194. return machine_pin_call(args[0], n_args - 1, 0, args + 1);
  195. }
  196. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_pin_value_obj, 1, 2, machine_pin_value);
  197. // pin.irq(handler=None, trigger=IRQ_FALLING|IRQ_RISING)
  198. STATIC mp_obj_t machine_pin_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
  199. enum { ARG_handler, ARG_trigger, ARG_wake };
  200. static const mp_arg_t allowed_args[] = {
  201. { MP_QSTR_handler, MP_ARG_OBJ, {.u_obj = mp_const_none} },
  202. { MP_QSTR_trigger, MP_ARG_INT, {.u_int = GPIO_PIN_INTR_POSEDGE | GPIO_PIN_INTR_NEGEDGE} },
  203. { MP_QSTR_wake, MP_ARG_OBJ, {.u_obj = mp_const_none} },
  204. };
  205. machine_pin_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
  206. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  207. mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
  208. if (n_args > 1 || kw_args->used != 0) {
  209. // configure irq
  210. mp_obj_t handler = args[ARG_handler].u_obj;
  211. uint32_t trigger = args[ARG_trigger].u_int;
  212. mp_obj_t wake_obj = args[ARG_wake].u_obj;
  213. if ((trigger == GPIO_PIN_INTR_LOLEVEL || trigger == GPIO_PIN_INTR_HILEVEL) && wake_obj != mp_const_none) {
  214. mp_int_t wake;
  215. if (mp_obj_get_int_maybe(wake_obj, &wake)) {
  216. if (wake < 2 || wake > 7) {
  217. mp_raise_ValueError("bad wake value");
  218. }
  219. } else {
  220. mp_raise_ValueError("bad wake value");
  221. }
  222. if (machine_rtc_config.wake_on_touch) { // not compatible
  223. mp_raise_ValueError("no resources");
  224. }
  225. if (!RTC_IS_VALID_EXT_PIN(self->id)) {
  226. mp_raise_ValueError("invalid pin for wake");
  227. }
  228. if (machine_rtc_config.ext0_pin == -1) {
  229. machine_rtc_config.ext0_pin = self->id;
  230. } else if (machine_rtc_config.ext0_pin != self->id) {
  231. mp_raise_ValueError("no resources");
  232. }
  233. machine_rtc_config.ext0_level = trigger == GPIO_PIN_INTR_LOLEVEL ? 0 : 1;
  234. machine_rtc_config.ext0_wake_types = wake;
  235. } else {
  236. if (machine_rtc_config.ext0_pin == self->id) {
  237. machine_rtc_config.ext0_pin = -1;
  238. }
  239. if (handler == mp_const_none) {
  240. handler = MP_OBJ_NULL;
  241. trigger = 0;
  242. }
  243. gpio_isr_handler_remove(self->id);
  244. MP_STATE_PORT(machine_pin_irq_handler)[self->id] = handler;
  245. gpio_set_intr_type(self->id, trigger);
  246. gpio_isr_handler_add(self->id, machine_pin_isr_handler, (void*)self);
  247. }
  248. }
  249. // return the irq object
  250. return MP_OBJ_FROM_PTR(&machine_pin_irq_object[self->id]);
  251. }
  252. STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_pin_irq_obj, 1, machine_pin_irq);
  253. STATIC const mp_rom_map_elem_t machine_pin_locals_dict_table[] = {
  254. // instance methods
  255. { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_pin_init_obj) },
  256. { MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&machine_pin_value_obj) },
  257. { MP_ROM_QSTR(MP_QSTR_irq), MP_ROM_PTR(&machine_pin_irq_obj) },
  258. // class constants
  259. { MP_ROM_QSTR(MP_QSTR_IN), MP_ROM_INT(GPIO_MODE_INPUT) },
  260. { MP_ROM_QSTR(MP_QSTR_OUT), MP_ROM_INT(GPIO_MODE_INPUT_OUTPUT) },
  261. { MP_ROM_QSTR(MP_QSTR_OPEN_DRAIN), MP_ROM_INT(GPIO_MODE_INPUT_OUTPUT_OD) },
  262. { MP_ROM_QSTR(MP_QSTR_PULL_UP), MP_ROM_INT(GPIO_PULLUP_ONLY) },
  263. { MP_ROM_QSTR(MP_QSTR_PULL_DOWN), MP_ROM_INT(GPIO_PULLDOWN_ONLY) },
  264. { MP_ROM_QSTR(MP_QSTR_IRQ_RISING), MP_ROM_INT(GPIO_PIN_INTR_POSEDGE) },
  265. { MP_ROM_QSTR(MP_QSTR_IRQ_FALLING), MP_ROM_INT(GPIO_PIN_INTR_NEGEDGE) },
  266. { MP_ROM_QSTR(MP_QSTR_WAKE_LOW), MP_ROM_INT(GPIO_PIN_INTR_LOLEVEL) },
  267. { MP_ROM_QSTR(MP_QSTR_WAKE_HIGH), MP_ROM_INT(GPIO_PIN_INTR_HILEVEL) },
  268. };
  269. STATIC mp_uint_t pin_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) {
  270. (void)errcode;
  271. machine_pin_obj_t *self = self_in;
  272. switch (request) {
  273. case MP_PIN_READ: {
  274. return gpio_get_level(self->id);
  275. }
  276. case MP_PIN_WRITE: {
  277. gpio_set_level(self->id, arg);
  278. return 0;
  279. }
  280. }
  281. return -1;
  282. }
  283. STATIC MP_DEFINE_CONST_DICT(machine_pin_locals_dict, machine_pin_locals_dict_table);
  284. STATIC const mp_pin_p_t pin_pin_p = {
  285. .ioctl = pin_ioctl,
  286. };
  287. const mp_obj_type_t machine_pin_type = {
  288. { &mp_type_type },
  289. .name = MP_QSTR_Pin,
  290. .print = machine_pin_print,
  291. .make_new = mp_pin_make_new,
  292. .call = machine_pin_call,
  293. .protocol = &pin_pin_p,
  294. .locals_dict = (mp_obj_t)&machine_pin_locals_dict,
  295. };
  296. /******************************************************************************/
  297. // Pin IRQ object
  298. STATIC const mp_obj_type_t machine_pin_irq_type;
  299. STATIC const machine_pin_irq_obj_t machine_pin_irq_object[] = {
  300. {{&machine_pin_irq_type}, GPIO_NUM_0},
  301. {{&machine_pin_irq_type}, GPIO_NUM_1},
  302. {{&machine_pin_irq_type}, GPIO_NUM_2},
  303. {{&machine_pin_irq_type}, GPIO_NUM_3},
  304. {{&machine_pin_irq_type}, GPIO_NUM_4},
  305. {{&machine_pin_irq_type}, GPIO_NUM_5},
  306. {{&machine_pin_irq_type}, GPIO_NUM_6},
  307. {{&machine_pin_irq_type}, GPIO_NUM_7},
  308. {{&machine_pin_irq_type}, GPIO_NUM_8},
  309. {{&machine_pin_irq_type}, GPIO_NUM_9},
  310. {{&machine_pin_irq_type}, GPIO_NUM_10},
  311. {{&machine_pin_irq_type}, GPIO_NUM_11},
  312. {{&machine_pin_irq_type}, GPIO_NUM_12},
  313. {{&machine_pin_irq_type}, GPIO_NUM_13},
  314. {{&machine_pin_irq_type}, GPIO_NUM_14},
  315. {{&machine_pin_irq_type}, GPIO_NUM_15},
  316. {{&machine_pin_irq_type}, GPIO_NUM_16},
  317. {{&machine_pin_irq_type}, GPIO_NUM_17},
  318. {{&machine_pin_irq_type}, GPIO_NUM_18},
  319. {{&machine_pin_irq_type}, GPIO_NUM_19},
  320. {{NULL}, -1},
  321. {{&machine_pin_irq_type}, GPIO_NUM_21},
  322. {{&machine_pin_irq_type}, GPIO_NUM_22},
  323. {{&machine_pin_irq_type}, GPIO_NUM_23},
  324. {{NULL}, -1},
  325. {{&machine_pin_irq_type}, GPIO_NUM_25},
  326. {{&machine_pin_irq_type}, GPIO_NUM_26},
  327. {{&machine_pin_irq_type}, GPIO_NUM_27},
  328. {{NULL}, -1},
  329. {{NULL}, -1},
  330. {{NULL}, -1},
  331. {{NULL}, -1},
  332. {{&machine_pin_irq_type}, GPIO_NUM_32},
  333. {{&machine_pin_irq_type}, GPIO_NUM_33},
  334. {{&machine_pin_irq_type}, GPIO_NUM_34},
  335. {{&machine_pin_irq_type}, GPIO_NUM_35},
  336. {{&machine_pin_irq_type}, GPIO_NUM_36},
  337. {{&machine_pin_irq_type}, GPIO_NUM_37},
  338. {{&machine_pin_irq_type}, GPIO_NUM_38},
  339. {{&machine_pin_irq_type}, GPIO_NUM_39},
  340. };
  341. STATIC mp_obj_t machine_pin_irq_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  342. machine_pin_irq_obj_t *self = self_in;
  343. mp_arg_check_num(n_args, n_kw, 0, 0, false);
  344. machine_pin_isr_handler((void*)&machine_pin_obj[self->id]);
  345. return mp_const_none;
  346. }
  347. STATIC mp_obj_t machine_pin_irq_trigger(size_t n_args, const mp_obj_t *args) {
  348. machine_pin_irq_obj_t *self = args[0];
  349. uint32_t orig_trig = GPIO.pin[self->id].int_type;
  350. if (n_args == 2) {
  351. // set trigger
  352. gpio_set_intr_type(self->id, mp_obj_get_int(args[1]));
  353. }
  354. // return original trigger value
  355. return MP_OBJ_NEW_SMALL_INT(orig_trig);
  356. }
  357. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_pin_irq_trigger_obj, 1, 2, machine_pin_irq_trigger);
  358. STATIC const mp_rom_map_elem_t machine_pin_irq_locals_dict_table[] = {
  359. { MP_ROM_QSTR(MP_QSTR_trigger), MP_ROM_PTR(&machine_pin_irq_trigger_obj) },
  360. };
  361. STATIC MP_DEFINE_CONST_DICT(machine_pin_irq_locals_dict, machine_pin_irq_locals_dict_table);
  362. STATIC const mp_obj_type_t machine_pin_irq_type = {
  363. { &mp_type_type },
  364. .name = MP_QSTR_IRQ,
  365. .call = machine_pin_irq_call,
  366. .locals_dict = (mp_obj_dict_t*)&machine_pin_irq_locals_dict,
  367. };