machine_pin.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2017 Armink (armink.ztl@gmail.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 <stdint.h>
  28. #include <string.h>
  29. #include <rtthread.h>
  30. #include <drivers/pin.h>
  31. #include "py/runtime.h"
  32. #include "py/gc.h"
  33. #include "py/mphal.h"
  34. #include "py/mperrno.h"
  35. #include "py/stream.h"
  36. #include "modmachine.h"
  37. #if MICROPY_PY_PIN
  38. #define GPIO_MODE_IN ((uint32_t)0x00000000) /*!< Input Floating Mode */
  39. #define GPIO_MODE_OUT_PP ((uint32_t)0x00000001) /*!< Output Push Pull Mode */
  40. #define GPIO_MODE_OUT_OD ((uint32_t)0x00000011) /*!< Output Open Drain Mode */
  41. #define GPIO_MODE_AF_PP ((uint32_t)0x00000002) /*!< Alternate Function Push Pull Mode */
  42. #define GPIO_MODE_AF_OD ((uint32_t)0x00000012) /*!< Alternate Function Open Drain Mode */
  43. #define GPIO_MODE_ANALOG ((uint32_t)0x00000003) /*!< Analog Mode */
  44. #define GPIO_NOPULL ((uint32_t)0x00000000) /*!< No Pull-up or Pull-down activation */
  45. #define GPIO_PULLUP ((uint32_t)0x00000001) /*!< Pull-up activation */
  46. #define GPIO_PULLDOWN ((uint32_t)0x00000002) /*!< Pull-down activation */
  47. #define GPIO_MODE_IT_RISING ((uint32_t)0x10110000) /*!< External Interrupt Mode with Rising edge trigger detection */
  48. #define GPIO_MODE_IT_FALLING ((uint32_t)0x10210000) /*!< External Interrupt Mode with Falling edge trigger detection */
  49. #define GPIO_MODE_IT_RISING_FALLING ((uint32_t)0x10310000) /*!< External Interrupt Mode with Rising/Falling edge trigger detection */
  50. const mp_obj_base_t machine_pin_obj_template = {&machine_pin_type};
  51. void mp_pin_od_write(void *machine_pin, int stat) {
  52. if (stat == PIN_LOW) {
  53. rt_pin_mode(((machine_pin_obj_t *)machine_pin)->pin, PIN_MODE_OUTPUT);
  54. rt_pin_write(((machine_pin_obj_t *)machine_pin)->pin, stat);
  55. } else {
  56. rt_pin_mode(((machine_pin_obj_t *)machine_pin)->pin, PIN_MODE_INPUT_PULLUP);
  57. }
  58. }
  59. void mp_hal_pin_open_set(void *machine_pin, int mode) {
  60. rt_pin_mode(((machine_pin_obj_t *)machine_pin)->pin, mode);
  61. }
  62. char* mp_hal_pin_get_name(void *machine_pin) {
  63. return ((machine_pin_obj_t *)machine_pin)->name;
  64. }
  65. STATIC mp_obj_t machine_pin_obj_init_helper(machine_pin_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
  66. STATIC void machine_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  67. machine_pin_obj_t *self = self_in;
  68. mp_printf(print, "<Pin %d>", self->pin);
  69. }
  70. // constructor(drv_name, pin, ...)
  71. 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) {
  72. mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
  73. // get the wanted port
  74. if (!MP_OBJ_IS_TYPE(args[0], &mp_type_tuple)) {
  75. mp_raise_ValueError("Pin id must be tuple of (\"GPIO_x\", pin#)");
  76. }
  77. mp_obj_t *items;
  78. mp_obj_get_array_fixed_n(args[0], 2, &items);
  79. const char *pin_name = mp_obj_str_get_str(items[0]);
  80. int wanted_pin = mp_obj_get_int(items[1]);
  81. machine_pin_obj_t *pin = m_new_obj(machine_pin_obj_t);
  82. if (!pin) {
  83. mp_raise_OSError(ENOMEM);
  84. }
  85. strncpy(pin->name, pin_name, sizeof(pin->name));
  86. pin->base = machine_pin_obj_template;
  87. pin->pin = wanted_pin;
  88. if (n_args > 1 || n_kw > 0) {
  89. // pin mode given, so configure this GPIO
  90. mp_map_t kw_args;
  91. mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
  92. machine_pin_obj_init_helper(pin, n_args - 1, args + 1, &kw_args);
  93. }
  94. return (mp_obj_t)pin;
  95. }
  96. // pin.init(mode, pull=None, *, value)
  97. STATIC mp_obj_t machine_pin_obj_init_helper(machine_pin_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
  98. enum { ARG_mode, ARG_pull, ARG_value };
  99. static const mp_arg_t allowed_args[] = {
  100. { MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_INT },
  101. { MP_QSTR_pull, MP_ARG_OBJ, {.u_obj = mp_const_none}},
  102. { MP_QSTR_value, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}},
  103. };
  104. // parse args
  105. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  106. mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
  107. // get io mode
  108. uint mode = args[ARG_mode].u_int;
  109. // get pull mode
  110. uint pull = GPIO_NOPULL;
  111. if (args[ARG_pull].u_obj != mp_const_none) {
  112. pull = mp_obj_get_int(args[ARG_pull].u_obj);
  113. }
  114. switch(mode) {
  115. case GPIO_MODE_IN: {
  116. if (pull == GPIO_PULLUP) {
  117. mode = PIN_MODE_INPUT_PULLUP;
  118. } else if (pull == GPIO_PULLDOWN) {
  119. mode = PIN_MODE_INPUT_PULLDOWN;
  120. } else {
  121. mode = PIN_MODE_INPUT;
  122. }
  123. break;
  124. }
  125. case GPIO_MODE_OUT_PP : {
  126. mode = PIN_MODE_OUTPUT;
  127. break;
  128. }
  129. case GPIO_MODE_OUT_OD : {
  130. mode = PIN_MODE_OUTPUT_OD;
  131. break;
  132. }
  133. case GPIO_MODE_AF_PP :
  134. case GPIO_MODE_AF_OD :
  135. case GPIO_MODE_ANALOG :
  136. //TODO
  137. mp_raise_NotImplementedError("not implemented pin mode");
  138. }
  139. rt_pin_mode(self->pin, mode);
  140. // get initial value
  141. if (args[ARG_value].u_obj != MP_OBJ_NULL) {
  142. rt_pin_write(self->pin, mp_obj_is_true(args[ARG_value].u_obj));
  143. }
  144. return mp_const_none;
  145. }
  146. // fast method for getting/setting pin value
  147. 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) {
  148. mp_arg_check_num(n_args, n_kw, 0, 1, false);
  149. machine_pin_obj_t *self = self_in;
  150. if (n_args == 0) {
  151. return mp_obj_new_bool(rt_pin_read(self->pin));
  152. } else {
  153. rt_pin_write(self->pin, mp_obj_is_true(args[0]));
  154. return mp_const_none;
  155. }
  156. }
  157. // pin.init(mode, pull)
  158. STATIC mp_obj_t machine_pin_obj_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
  159. return machine_pin_obj_init_helper(args[0], n_args - 1, args + 1, kw_args);
  160. }
  161. MP_DEFINE_CONST_FUN_OBJ_KW(machine_pin_init_obj, 1, machine_pin_obj_init);
  162. // pin.value([value])
  163. STATIC mp_obj_t machine_pin_value(size_t n_args, const mp_obj_t *args) {
  164. return machine_pin_call(args[0], n_args - 1, 0, args + 1);
  165. }
  166. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_pin_value_obj, 1, 2, machine_pin_value);
  167. // pin.name()
  168. STATIC mp_obj_t machine_pin_name(size_t n_args, const mp_obj_t *args) {
  169. machine_pin_obj_t *self = (machine_pin_obj_t *)args[0];
  170. return mp_obj_new_str(self->name, strlen(self->name));
  171. }
  172. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_pin_name_obj, 1, 2, machine_pin_name);
  173. // pin.pin()
  174. STATIC mp_obj_t machine_pin_pin(size_t n_args, const mp_obj_t *args) {
  175. return MP_OBJ_NEW_SMALL_INT(((machine_pin_obj_t *)args[0])->pin);
  176. }
  177. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_pin_pin_obj, 1, 2, machine_pin_pin);
  178. STATIC mp_uint_t machine_pin_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) {
  179. (void)errcode;
  180. machine_pin_obj_t *self = self_in;
  181. switch (request) {
  182. case MP_PIN_READ: {
  183. uint32_t pin_val = rt_pin_read(self->pin);
  184. return pin_val;
  185. }
  186. case MP_PIN_WRITE: {
  187. rt_pin_write(self->pin, arg);
  188. return 0;
  189. }
  190. }
  191. *errcode = MP_EINVAL;
  192. return MP_STREAM_ERROR;
  193. }
  194. STATIC const mp_rom_map_elem_t machine_pin_locals_dict_table[] = {
  195. // instance methods
  196. { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_pin_init_obj) },
  197. { MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&machine_pin_value_obj) },
  198. { MP_ROM_QSTR(MP_QSTR_name), MP_ROM_PTR(&machine_pin_name_obj) },
  199. { MP_ROM_QSTR(MP_QSTR_pin), MP_ROM_PTR(&machine_pin_pin_obj) },
  200. // class constants
  201. { MP_ROM_QSTR(MP_QSTR_ALT_OD), MP_ROM_INT(GPIO_MODE_AF_OD) },
  202. { MP_ROM_QSTR(MP_QSTR_ALT_PP), MP_ROM_INT(GPIO_MODE_AF_PP) },
  203. { MP_ROM_QSTR(MP_QSTR_ANALOG), MP_ROM_INT(GPIO_MODE_ANALOG) },
  204. { MP_ROM_QSTR(MP_QSTR_IN), MP_ROM_INT(GPIO_MODE_IN) },
  205. { MP_ROM_QSTR(MP_QSTR_OUT_PP), MP_ROM_INT(GPIO_MODE_OUT_PP) },
  206. { MP_ROM_QSTR(MP_QSTR_OUT_OD), MP_ROM_INT(GPIO_MODE_OUT_OD) },
  207. { MP_ROM_QSTR(MP_QSTR_PULL_DOWN), MP_ROM_INT(GPIO_PULLDOWN) },
  208. { MP_ROM_QSTR(MP_QSTR_PULL_NONE), MP_ROM_INT(GPIO_NOPULL) },
  209. { MP_ROM_QSTR(MP_QSTR_PULL_UP), MP_ROM_INT(GPIO_PULLUP) },
  210. { MP_ROM_QSTR(MP_QSTR_IRQ_RISING), MP_ROM_INT(GPIO_MODE_IT_RISING) },
  211. { MP_ROM_QSTR(MP_QSTR_IRQ_FALLING), MP_ROM_INT(GPIO_MODE_IT_FALLING) },
  212. { MP_ROM_QSTR(MP_QSTR_IRQ_RISING_FALLING), MP_ROM_INT(GPIO_MODE_IT_RISING_FALLING) },
  213. };
  214. STATIC MP_DEFINE_CONST_DICT(machine_pin_locals_dict, machine_pin_locals_dict_table);
  215. STATIC const mp_pin_p_t machine_pin_pin_p = {
  216. .ioctl = machine_pin_ioctl,
  217. };
  218. const mp_obj_type_t machine_pin_type = {
  219. { &mp_type_type },
  220. .name = MP_QSTR_Pin,
  221. .print = machine_pin_print,
  222. .make_new = mp_pin_make_new,
  223. .call = machine_pin_call,
  224. .protocol = &machine_pin_pin_p,
  225. .locals_dict = (mp_obj_t)&machine_pin_locals_dict,
  226. };
  227. #endif