machine_pin.c 9.5 KB

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