machine_pin.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014, 2015 Damien P. George
  7. * Copyright (c) 2016 Linaro Limited
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include <stdio.h>
  28. #include <stdint.h>
  29. #include <string.h>
  30. #include <zephyr.h>
  31. #include <gpio.h>
  32. #include "py/runtime.h"
  33. #include "py/gc.h"
  34. #include "py/mphal.h"
  35. #include "modmachine.h"
  36. const mp_obj_base_t machine_pin_obj_template = {&machine_pin_type};
  37. STATIC void machine_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  38. machine_pin_obj_t *self = self_in;
  39. mp_printf(print, "<Pin %p %d>", self->port, self->pin);
  40. }
  41. // pin.init(mode, pull=None, *, value)
  42. 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) {
  43. enum { ARG_mode, ARG_pull, ARG_value };
  44. static const mp_arg_t allowed_args[] = {
  45. { MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_INT },
  46. { MP_QSTR_pull, MP_ARG_OBJ, {.u_obj = mp_const_none}},
  47. { MP_QSTR_value, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}},
  48. };
  49. // parse args
  50. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  51. mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
  52. // get io mode
  53. uint mode = args[ARG_mode].u_int;
  54. // get pull mode
  55. uint pull = GPIO_PUD_NORMAL;
  56. if (args[ARG_pull].u_obj != mp_const_none) {
  57. pull = mp_obj_get_int(args[ARG_pull].u_obj);
  58. }
  59. int ret = gpio_pin_configure(self->port, self->pin, mode | pull);
  60. if (ret) {
  61. mp_raise_ValueError("invalid pin");
  62. }
  63. // get initial value
  64. if (args[ARG_value].u_obj != MP_OBJ_NULL) {
  65. (void)gpio_pin_write(self->port, self->pin, mp_obj_is_true(args[ARG_value].u_obj));
  66. }
  67. return mp_const_none;
  68. }
  69. // constructor(drv_name, pin, ...)
  70. 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) {
  71. mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
  72. // get the wanted port
  73. if (!MP_OBJ_IS_TYPE(args[0], &mp_type_tuple)) {
  74. mp_raise_ValueError("Pin id must be tuple of (\"GPIO_x\", pin#)");
  75. }
  76. mp_obj_t *items;
  77. mp_obj_get_array_fixed_n(args[0], 2, &items);
  78. const char *drv_name = mp_obj_str_get_str(items[0]);
  79. int wanted_pin = mp_obj_get_int(items[1]);
  80. struct device *wanted_port = device_get_binding(drv_name);
  81. if (!wanted_port) {
  82. mp_raise_ValueError("invalid port");
  83. }
  84. machine_pin_obj_t *pin = m_new_obj(machine_pin_obj_t);
  85. pin->base = machine_pin_obj_template;
  86. pin->port = wanted_port;
  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. // fast method for getting/setting pin value
  97. 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) {
  98. mp_arg_check_num(n_args, n_kw, 0, 1, false);
  99. machine_pin_obj_t *self = self_in;
  100. if (n_args == 0) {
  101. u32_t pin_val;
  102. (void)gpio_pin_read(self->port, self->pin, &pin_val);
  103. return MP_OBJ_NEW_SMALL_INT(pin_val);
  104. } else {
  105. (void)gpio_pin_write(self->port, self->pin, mp_obj_is_true(args[0]));
  106. return mp_const_none;
  107. }
  108. }
  109. // pin.init(mode, pull)
  110. STATIC mp_obj_t machine_pin_obj_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
  111. return machine_pin_obj_init_helper(args[0], n_args - 1, args + 1, kw_args);
  112. }
  113. MP_DEFINE_CONST_FUN_OBJ_KW(machine_pin_init_obj, 1, machine_pin_obj_init);
  114. // pin.value([value])
  115. STATIC mp_obj_t machine_pin_value(size_t n_args, const mp_obj_t *args) {
  116. return machine_pin_call(args[0], n_args - 1, 0, args + 1);
  117. }
  118. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_pin_value_obj, 1, 2, machine_pin_value);
  119. STATIC mp_obj_t machine_pin_off(mp_obj_t self_in) {
  120. machine_pin_obj_t *self = self_in;
  121. (void)gpio_pin_write(self->port, self->pin, 0);
  122. return mp_const_none;
  123. }
  124. STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_off_obj, machine_pin_off);
  125. STATIC mp_obj_t machine_pin_on(mp_obj_t self_in) {
  126. machine_pin_obj_t *self = self_in;
  127. (void)gpio_pin_write(self->port, self->pin, 1);
  128. return mp_const_none;
  129. }
  130. STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_on_obj, machine_pin_on);
  131. STATIC mp_uint_t machine_pin_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) {
  132. (void)errcode;
  133. machine_pin_obj_t *self = self_in;
  134. switch (request) {
  135. case MP_PIN_READ: {
  136. u32_t pin_val;
  137. gpio_pin_read(self->port, self->pin, &pin_val);
  138. return pin_val;
  139. }
  140. case MP_PIN_WRITE: {
  141. gpio_pin_write(self->port, self->pin, arg);
  142. return 0;
  143. }
  144. }
  145. return -1;
  146. }
  147. STATIC const mp_rom_map_elem_t machine_pin_locals_dict_table[] = {
  148. // instance methods
  149. { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_pin_init_obj) },
  150. { MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&machine_pin_value_obj) },
  151. { MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(&machine_pin_off_obj) },
  152. { MP_ROM_QSTR(MP_QSTR_on), MP_ROM_PTR(&machine_pin_on_obj) },
  153. // class constants
  154. { MP_ROM_QSTR(MP_QSTR_IN), MP_ROM_INT(GPIO_DIR_IN) },
  155. { MP_ROM_QSTR(MP_QSTR_OUT), MP_ROM_INT(GPIO_DIR_OUT) },
  156. { MP_ROM_QSTR(MP_QSTR_PULL_UP), MP_ROM_INT(GPIO_PUD_PULL_UP) },
  157. { MP_ROM_QSTR(MP_QSTR_PULL_DOWN), MP_ROM_INT(GPIO_PUD_PULL_DOWN) },
  158. };
  159. STATIC MP_DEFINE_CONST_DICT(machine_pin_locals_dict, machine_pin_locals_dict_table);
  160. STATIC const mp_pin_p_t machine_pin_pin_p = {
  161. .ioctl = machine_pin_ioctl,
  162. };
  163. const mp_obj_type_t machine_pin_type = {
  164. { &mp_type_type },
  165. .name = MP_QSTR_Pin,
  166. .print = machine_pin_print,
  167. .make_new = mp_pin_make_new,
  168. .call = machine_pin_call,
  169. .protocol = &machine_pin_pin_p,
  170. .locals_dict = (mp_obj_t)&machine_pin_locals_dict,
  171. };