machine_pin.c 9.5 KB

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