machine_pin.c 10 KB

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