pin.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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 Damien P. George
  7. * Copyright (c) 2016, 2018 Glenn Ruben Bakke
  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 "py/nlr.h"
  31. #include "py/runtime.h"
  32. #include "py/mphal.h"
  33. #include "pin.h"
  34. #include "nrf_gpio.h"
  35. #include "nrfx_gpiote.h"
  36. extern const pin_obj_t machine_pin_obj[];
  37. extern const uint8_t machine_pin_num_of_pins;
  38. /// \moduleref machine
  39. /// \class Pin - control I/O pins
  40. ///
  41. /// A pin is the basic object to control I/O pins. It has methods to set
  42. /// the mode of the pin (input, output, etc) and methods to get and set the
  43. /// digital logic level. For analog control of a pin, see the ADC class.
  44. ///
  45. /// Usage Model:
  46. ///
  47. /// All Board Pins are predefined as machine.Pin.board.Name
  48. ///
  49. /// x1_pin = machine.Pin.board.X1
  50. ///
  51. /// g = machine.Pin(machine.Pin.board.X1, machine.Pin.IN)
  52. ///
  53. /// CPU pins which correspond to the board pins are available
  54. /// as `machine.cpu.Name`. For the CPU pins, the names are the port letter
  55. /// followed by the pin number. On the PYBv1.0, `machine.Pin.board.X1` and
  56. /// `machine.Pin.cpu.B6` are the same pin.
  57. ///
  58. /// You can also use strings:
  59. ///
  60. /// g = machine.Pin('X1', machine.Pin.OUT)
  61. ///
  62. /// Users can add their own names:
  63. ///
  64. /// MyMapperDict = { 'LeftMotorDir' : machine.Pin.cpu.C12 }
  65. /// machine.Pin.dict(MyMapperDict)
  66. /// g = machine.Pin("LeftMotorDir", machine.Pin.OUT)
  67. ///
  68. /// and can query mappings
  69. ///
  70. /// pin = machine.Pin("LeftMotorDir")
  71. ///
  72. /// Users can also add their own mapping function:
  73. ///
  74. /// def MyMapper(pin_name):
  75. /// if pin_name == "LeftMotorDir":
  76. /// return machine.Pin.cpu.A0
  77. ///
  78. /// machine.Pin.mapper(MyMapper)
  79. ///
  80. /// So, if you were to call: `machine.Pin("LeftMotorDir", machine.Pin.OUT)`
  81. /// then `"LeftMotorDir"` is passed directly to the mapper function.
  82. ///
  83. /// To summarise, the following order determines how things get mapped into
  84. /// an ordinal pin number:
  85. ///
  86. /// 1. Directly specify a pin object
  87. /// 2. User supplied mapping function
  88. /// 3. User supplied mapping (object must be usable as a dictionary key)
  89. /// 4. Supply a string which matches a board pin
  90. /// 5. Supply a string which matches a CPU port/pin
  91. ///
  92. /// You can set `machine.Pin.debug(True)` to get some debug information about
  93. /// how a particular object gets mapped to a pin.
  94. #define PIN_DEBUG (0)
  95. // Pin class variables
  96. #if PIN_DEBUG
  97. STATIC bool pin_class_debug;
  98. #else
  99. #define pin_class_debug (0)
  100. #endif
  101. void pin_init0(void) {
  102. MP_STATE_PORT(pin_class_mapper) = mp_const_none;
  103. MP_STATE_PORT(pin_class_map_dict) = mp_const_none;
  104. for (int i = 0; i < NUM_OF_PINS; i++) {
  105. MP_STATE_PORT(pin_irq_handlers)[i] = mp_const_none;
  106. }
  107. // Initialize GPIOTE if not done yet.
  108. if (!nrfx_gpiote_is_init()) {
  109. nrfx_gpiote_init();
  110. }
  111. #if PIN_DEBUG
  112. pin_class_debug = false;
  113. #endif
  114. }
  115. // C API used to convert a user-supplied pin name into an ordinal pin number.
  116. const pin_obj_t *pin_find(mp_obj_t user_obj) {
  117. const pin_obj_t *pin_obj;
  118. // If pin is SMALL_INT
  119. if (MP_OBJ_IS_SMALL_INT(user_obj)) {
  120. uint8_t value = MP_OBJ_SMALL_INT_VALUE(user_obj);
  121. for (uint8_t i = 0; i < machine_pin_num_of_pins; i++) {
  122. if (machine_pin_obj[i].pin == value) {
  123. return &machine_pin_obj[i];
  124. }
  125. }
  126. }
  127. // If a pin was provided, then use it
  128. if (MP_OBJ_IS_TYPE(user_obj, &pin_type)) {
  129. pin_obj = user_obj;
  130. if (pin_class_debug) {
  131. printf("Pin map passed pin ");
  132. mp_obj_print((mp_obj_t)pin_obj, PRINT_STR);
  133. printf("\n");
  134. }
  135. return pin_obj;
  136. }
  137. if (MP_STATE_PORT(pin_class_mapper) != mp_const_none) {
  138. pin_obj = mp_call_function_1(MP_STATE_PORT(pin_class_mapper), user_obj);
  139. if (pin_obj != mp_const_none) {
  140. if (!MP_OBJ_IS_TYPE(pin_obj, &pin_type)) {
  141. mp_raise_ValueError("Pin.mapper didn't return a Pin object");
  142. }
  143. if (pin_class_debug) {
  144. printf("Pin.mapper maps ");
  145. mp_obj_print(user_obj, PRINT_REPR);
  146. printf(" to ");
  147. mp_obj_print((mp_obj_t)pin_obj, PRINT_STR);
  148. printf("\n");
  149. }
  150. return pin_obj;
  151. }
  152. // The pin mapping function returned mp_const_none, fall through to
  153. // other lookup methods.
  154. }
  155. if (MP_STATE_PORT(pin_class_map_dict) != mp_const_none) {
  156. mp_map_t *pin_map_map = mp_obj_dict_get_map(MP_STATE_PORT(pin_class_map_dict));
  157. mp_map_elem_t *elem = mp_map_lookup(pin_map_map, user_obj, MP_MAP_LOOKUP);
  158. if (elem != NULL && elem->value != NULL) {
  159. pin_obj = elem->value;
  160. if (pin_class_debug) {
  161. printf("Pin.map_dict maps ");
  162. mp_obj_print(user_obj, PRINT_REPR);
  163. printf(" to ");
  164. mp_obj_print((mp_obj_t)pin_obj, PRINT_STR);
  165. printf("\n");
  166. }
  167. return pin_obj;
  168. }
  169. }
  170. // See if the pin name matches a board pin
  171. pin_obj = pin_find_named_pin(&pin_board_pins_locals_dict, user_obj);
  172. if (pin_obj) {
  173. if (pin_class_debug) {
  174. printf("Pin.board maps ");
  175. mp_obj_print(user_obj, PRINT_REPR);
  176. printf(" to ");
  177. mp_obj_print((mp_obj_t)pin_obj, PRINT_STR);
  178. printf("\n");
  179. }
  180. return pin_obj;
  181. }
  182. // See if the pin name matches a cpu pin
  183. pin_obj = pin_find_named_pin(&pin_cpu_pins_locals_dict, user_obj);
  184. if (pin_obj) {
  185. if (pin_class_debug) {
  186. printf("Pin.cpu maps ");
  187. mp_obj_print(user_obj, PRINT_REPR);
  188. printf(" to ");
  189. mp_obj_print((mp_obj_t)pin_obj, PRINT_STR);
  190. printf("\n");
  191. }
  192. return pin_obj;
  193. }
  194. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "pin '%s' not a valid pin identifier", mp_obj_str_get_str(user_obj)));
  195. }
  196. /// \method __str__()
  197. /// Return a string describing the pin object.
  198. STATIC void pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  199. pin_obj_t *self = self_in;
  200. // pin name
  201. mp_printf(print, "Pin(Pin.cpu.%q, mode=Pin.", self->name);
  202. mp_printf(print, "port=0x%x, ", self->pin / 32);
  203. mp_printf(print, "pin=0x%x, ", self->pin);
  204. /*
  205. uint32_t mode = pin_get_mode(self);
  206. if (mode == GPIO_MODE_ANALOG) {
  207. // analog
  208. mp_print_str(print, "ANALOG)");
  209. } else {
  210. // IO mode
  211. bool af = false;
  212. qstr mode_qst;
  213. if (mode == GPIO_MODE_INPUT) {
  214. mode_qst = MP_QSTR_IN;
  215. } else if (mode == GPIO_MODE_OUTPUT_PP) {
  216. mode_qst = MP_QSTR_OUT;
  217. } else if (mode == GPIO_MODE_OUTPUT_OD) {
  218. mode_qst = MP_QSTR_OPEN_DRAIN;
  219. } else {
  220. af = true;
  221. if (mode == GPIO_MODE_AF_PP) {
  222. mode_qst = MP_QSTR_ALT;
  223. } else {
  224. mode_qst = MP_QSTR_ALT_OPEN_DRAIN;
  225. }
  226. }
  227. mp_print_str(print, qstr_str(mode_qst));
  228. // pull mode
  229. qstr pull_qst = MP_QSTR_NULL;
  230. uint32_t pull = pin_get_pull(self);
  231. if (pull == GPIO_PULLUP) {
  232. pull_qst = MP_QSTR_PULL_UP;
  233. } else if (pull == GPIO_PULLDOWN) {
  234. pull_qst = MP_QSTR_PULL_DOWN;
  235. }
  236. if (pull_qst != MP_QSTR_NULL) {
  237. mp_printf(print, ", pull=Pin.%q", pull_qst);
  238. }
  239. // AF mode
  240. if (af) {
  241. mp_uint_t af_idx = pin_get_af(self);
  242. const pin_af_obj_t *af_obj = pin_find_af_by_index(self, af_idx);
  243. if (af_obj == NULL) {
  244. mp_printf(print, ", af=%d)", af_idx);
  245. } else {
  246. mp_printf(print, ", af=Pin.%q)", af_obj->name);
  247. }
  248. } else {
  249. */
  250. mp_print_str(print, ")");
  251. /* }
  252. }*/
  253. }
  254. STATIC mp_obj_t pin_obj_init_helper(const pin_obj_t *pin, mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args);
  255. /// \classmethod \constructor(id, ...)
  256. /// Create a new Pin object associated with the id. If additional arguments are given,
  257. /// they are used to initialise the pin. See `init`.
  258. STATIC mp_obj_t pin_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
  259. mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
  260. // Run an argument through the mapper and return the result.
  261. const pin_obj_t *pin = pin_find(args[0]);
  262. if (n_args > 1 || n_kw > 0) {
  263. // pin mode given, so configure this GPIO
  264. mp_map_t kw_args;
  265. mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
  266. pin_obj_init_helper(pin, n_args - 1, args + 1, &kw_args);
  267. }
  268. return (mp_obj_t)pin;
  269. }
  270. // fast method for getting/setting pin value
  271. STATIC mp_obj_t pin_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
  272. mp_arg_check_num(n_args, n_kw, 0, 1, false);
  273. pin_obj_t *self = self_in;
  274. if (n_args == 0) {
  275. // get pin
  276. return MP_OBJ_NEW_SMALL_INT(mp_hal_pin_read(self));
  277. } else {
  278. // set pin
  279. mp_hal_pin_write(self, mp_obj_is_true(args[0]));
  280. return mp_const_none;
  281. }
  282. }
  283. STATIC mp_obj_t pin_off(mp_obj_t self_in) {
  284. pin_obj_t *self = self_in;
  285. mp_hal_pin_low(self);
  286. return mp_const_none;
  287. }
  288. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_off_obj, pin_off);
  289. STATIC mp_obj_t pin_on(mp_obj_t self_in) {
  290. pin_obj_t *self = self_in;
  291. mp_hal_pin_high(self);
  292. return mp_const_none;
  293. }
  294. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_on_obj, pin_on);
  295. /// \classmethod mapper([fun])
  296. /// Get or set the pin mapper function.
  297. STATIC mp_obj_t pin_mapper(mp_uint_t n_args, const mp_obj_t *args) {
  298. if (n_args > 1) {
  299. MP_STATE_PORT(pin_class_mapper) = args[1];
  300. return mp_const_none;
  301. }
  302. return MP_STATE_PORT(pin_class_mapper);
  303. }
  304. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pin_mapper_fun_obj, 1, 2, pin_mapper);
  305. STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(pin_mapper_obj, (mp_obj_t)&pin_mapper_fun_obj);
  306. /// \classmethod dict([dict])
  307. /// Get or set the pin mapper dictionary.
  308. STATIC mp_obj_t pin_map_dict(mp_uint_t n_args, const mp_obj_t *args) {
  309. if (n_args > 1) {
  310. MP_STATE_PORT(pin_class_map_dict) = args[1];
  311. return mp_const_none;
  312. }
  313. return MP_STATE_PORT(pin_class_map_dict);
  314. }
  315. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pin_map_dict_fun_obj, 1, 2, pin_map_dict);
  316. STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(pin_map_dict_obj, (mp_obj_t)&pin_map_dict_fun_obj);
  317. /// \classmethod af_list()
  318. /// Returns an array of alternate functions available for this pin.
  319. STATIC mp_obj_t pin_af_list(mp_obj_t self_in) {
  320. pin_obj_t *self = self_in;
  321. mp_obj_t result = mp_obj_new_list(0, NULL);
  322. const pin_af_obj_t *af = self->af;
  323. for (mp_uint_t i = 0; i < self->num_af; i++, af++) {
  324. mp_obj_list_append(result, (mp_obj_t)af);
  325. }
  326. return result;
  327. }
  328. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_af_list_obj, pin_af_list);
  329. #if PIN_DEBUG
  330. /// \classmethod debug([state])
  331. /// Get or set the debugging state (`True` or `False` for on or off).
  332. STATIC mp_obj_t pin_debug(mp_uint_t n_args, const mp_obj_t *args) {
  333. if (n_args > 1) {
  334. pin_class_debug = mp_obj_is_true(args[1]);
  335. return mp_const_none;
  336. }
  337. return mp_obj_new_bool(pin_class_debug);
  338. }
  339. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pin_debug_fun_obj, 1, 2, pin_debug);
  340. STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(pin_debug_obj, (mp_obj_t)&pin_debug_fun_obj);
  341. #endif
  342. // init(mode, pull=None, af=-1, *, value, alt)
  343. STATIC mp_obj_t pin_obj_init_helper(const pin_obj_t *self, mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
  344. static const mp_arg_t allowed_args[] = {
  345. { MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_INT },
  346. { MP_QSTR_pull, MP_ARG_OBJ, {.u_obj = mp_const_none}},
  347. { MP_QSTR_af, MP_ARG_INT, {.u_int = -1}}, // legacy
  348. { MP_QSTR_value, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}},
  349. { MP_QSTR_alt, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1}},
  350. };
  351. // parse args
  352. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  353. mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
  354. // get pull mode
  355. nrf_gpio_pin_pull_t pull = NRF_GPIO_PIN_NOPULL;
  356. if (args[1].u_obj != mp_const_none) {
  357. pull = (nrf_gpio_pin_pull_t)mp_obj_get_int(args[1].u_obj);
  358. }
  359. // if given, set the pin value before initialising to prevent glitches
  360. if (args[3].u_obj != MP_OBJ_NULL) {
  361. mp_hal_pin_write(self, mp_obj_is_true(args[3].u_obj));
  362. }
  363. // get io mode
  364. nrf_gpio_pin_dir_t mode = (nrf_gpio_pin_dir_t)args[0].u_int;
  365. // Connect input or not
  366. nrf_gpio_pin_input_t input = (mode == NRF_GPIO_PIN_DIR_INPUT) ? NRF_GPIO_PIN_INPUT_CONNECT
  367. : NRF_GPIO_PIN_INPUT_DISCONNECT;
  368. if (mode == NRF_GPIO_PIN_DIR_OUTPUT || mode == NRF_GPIO_PIN_DIR_INPUT) {
  369. nrf_gpio_cfg(self->pin,
  370. mode,
  371. input,
  372. pull,
  373. NRF_GPIO_PIN_S0S1,
  374. NRF_GPIO_PIN_NOSENSE);
  375. } else {
  376. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid pin mode: %d", mode));
  377. }
  378. return mp_const_none;
  379. }
  380. STATIC mp_obj_t pin_obj_init(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
  381. return pin_obj_init_helper(args[0], n_args - 1, args + 1, kw_args);
  382. }
  383. MP_DEFINE_CONST_FUN_OBJ_KW(pin_init_obj, 1, pin_obj_init);
  384. /// \method value([value])
  385. /// Get or set the digital logic level of the pin:
  386. ///
  387. /// - With no argument, return 0 or 1 depending on the logic level of the pin.
  388. /// - With `value` given, set the logic level of the pin. `value` can be
  389. /// anything that converts to a boolean. If it converts to `True`, the pin
  390. /// is set high, otherwise it is set low.
  391. STATIC mp_obj_t pin_value(mp_uint_t n_args, const mp_obj_t *args) {
  392. return pin_call(args[0], n_args - 1, 0, args + 1);
  393. }
  394. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pin_value_obj, 1, 2, pin_value);
  395. /// \method low()
  396. /// Set the pin to a low logic level.
  397. STATIC mp_obj_t pin_low(mp_obj_t self_in) {
  398. pin_obj_t *self = self_in;
  399. mp_hal_pin_low(self);
  400. return mp_const_none;
  401. }
  402. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_low_obj, pin_low);
  403. /// \method high()
  404. /// Set the pin to a high logic level.
  405. STATIC mp_obj_t pin_high(mp_obj_t self_in) {
  406. pin_obj_t *self = self_in;
  407. mp_hal_pin_high(self);
  408. return mp_const_none;
  409. }
  410. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_high_obj, pin_high);
  411. /// \method name()
  412. /// Get the pin name.
  413. STATIC mp_obj_t pin_name(mp_obj_t self_in) {
  414. pin_obj_t *self = self_in;
  415. return MP_OBJ_NEW_QSTR(self->name);
  416. }
  417. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_name_obj, pin_name);
  418. /// \method names()
  419. /// Returns the cpu and board names for this pin.
  420. STATIC mp_obj_t pin_names(mp_obj_t self_in) {
  421. pin_obj_t *self = self_in;
  422. mp_obj_t result = mp_obj_new_list(0, NULL);
  423. mp_obj_list_append(result, MP_OBJ_NEW_QSTR(self->name));
  424. mp_map_t *map = mp_obj_dict_get_map((mp_obj_t)&pin_board_pins_locals_dict);
  425. mp_map_elem_t *elem = map->table;
  426. for (mp_uint_t i = 0; i < map->used; i++, elem++) {
  427. if (elem->value == self) {
  428. mp_obj_list_append(result, elem->key);
  429. }
  430. }
  431. return result;
  432. }
  433. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_names_obj, pin_names);
  434. /// \method port()
  435. /// Get the pin port.
  436. STATIC mp_obj_t pin_port(mp_obj_t self_in) {
  437. pin_obj_t *self = self_in;
  438. return MP_OBJ_NEW_SMALL_INT(self->pin / 32);
  439. }
  440. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_port_obj, pin_port);
  441. /// \method pin()
  442. /// Get the pin number.
  443. STATIC mp_obj_t pin_pin(mp_obj_t self_in) {
  444. pin_obj_t *self = self_in;
  445. return MP_OBJ_NEW_SMALL_INT(self->pin);
  446. }
  447. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_pin_obj, pin_pin);
  448. /// \method mode()
  449. /// Returns the currently configured mode of the pin. The integer returned
  450. /// will match one of the allowed constants for the mode argument to the init
  451. /// function.
  452. STATIC mp_obj_t pin_mode(mp_obj_t self_in) {
  453. return mp_const_none; // TODO: MP_OBJ_NEW_SMALL_INT(pin_get_mode(self_in));
  454. }
  455. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_mode_obj, pin_mode);
  456. /// \method pull()
  457. /// Returns the currently configured pull of the pin. The integer returned
  458. /// will match one of the allowed constants for the pull argument to the init
  459. /// function.
  460. STATIC mp_obj_t pin_pull(mp_obj_t self_in) {
  461. return mp_const_none; // TODO: MP_OBJ_NEW_SMALL_INT(pin_get_pull(self_in));
  462. }
  463. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_pull_obj, pin_pull);
  464. /// \method af()
  465. /// Returns the currently configured alternate-function of the pin. The
  466. /// integer returned will match one of the allowed constants for the af
  467. /// argument to the init function.
  468. STATIC mp_obj_t pin_af(mp_obj_t self_in) {
  469. return mp_const_none; // TODO: MP_OBJ_NEW_SMALL_INT(pin_get_af(self_in));
  470. }
  471. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_af_obj, pin_af);
  472. STATIC void pin_common_irq_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) {
  473. mp_obj_t pin_handler = MP_STATE_PORT(pin_irq_handlers)[pin];
  474. mp_obj_t pin_number = MP_OBJ_NEW_SMALL_INT(pin);
  475. const pin_obj_t *pin_obj = pin_find(pin_number);
  476. mp_call_function_1(pin_handler, (mp_obj_t)pin_obj);
  477. }
  478. STATIC mp_obj_t pin_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
  479. enum {ARG_handler, ARG_trigger, ARG_wake};
  480. static const mp_arg_t allowed_args[] = {
  481. { MP_QSTR_handler, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = mp_const_none} },
  482. { MP_QSTR_trigger, MP_ARG_INT, {.u_int = NRF_GPIOTE_POLARITY_LOTOHI | NRF_GPIOTE_POLARITY_HITOLO} },
  483. { MP_QSTR_wake, MP_ARG_BOOL, {.u_bool = false} },
  484. };
  485. pin_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
  486. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  487. mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
  488. nrfx_gpiote_pin_t pin = self->pin;
  489. nrfx_gpiote_in_config_t config = NRFX_GPIOTE_CONFIG_IN_SENSE_TOGGLE(true);
  490. if (args[ARG_trigger].u_int == NRF_GPIOTE_POLARITY_LOTOHI) {
  491. config.sense = NRF_GPIOTE_POLARITY_LOTOHI;
  492. } else if (args[ARG_trigger].u_int == NRF_GPIOTE_POLARITY_HITOLO) {
  493. config.sense = NRF_GPIOTE_POLARITY_HITOLO;
  494. }
  495. config.pull = NRF_GPIO_PIN_PULLUP;
  496. nrfx_err_t err_code = nrfx_gpiote_in_init(pin, &config, pin_common_irq_handler);
  497. if (err_code == NRFX_ERROR_INVALID_STATE) {
  498. // Re-init if already configured.
  499. nrfx_gpiote_in_uninit(pin);
  500. nrfx_gpiote_in_init(pin, &config, pin_common_irq_handler);
  501. }
  502. MP_STATE_PORT(pin_irq_handlers)[pin] = args[ARG_handler].u_obj;
  503. nrfx_gpiote_in_event_enable(pin, true);
  504. // return the irq object
  505. return mp_const_none;
  506. }
  507. STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pin_irq_obj, 1, pin_irq);
  508. STATIC const mp_rom_map_elem_t pin_locals_dict_table[] = {
  509. // instance methods
  510. { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&pin_init_obj) },
  511. { MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&pin_value_obj) },
  512. { MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(&pin_off_obj) },
  513. { MP_ROM_QSTR(MP_QSTR_on), MP_ROM_PTR(&pin_on_obj) },
  514. { MP_ROM_QSTR(MP_QSTR_low), MP_ROM_PTR(&pin_low_obj) },
  515. { MP_ROM_QSTR(MP_QSTR_high), MP_ROM_PTR(&pin_high_obj) },
  516. { MP_ROM_QSTR(MP_QSTR_name), MP_ROM_PTR(&pin_name_obj) },
  517. { MP_ROM_QSTR(MP_QSTR_names), MP_ROM_PTR(&pin_names_obj) },
  518. { MP_ROM_QSTR(MP_QSTR_af_list), MP_ROM_PTR(&pin_af_list_obj) },
  519. { MP_ROM_QSTR(MP_QSTR_port), MP_ROM_PTR(&pin_port_obj) },
  520. { MP_ROM_QSTR(MP_QSTR_pin), MP_ROM_PTR(&pin_pin_obj) },
  521. { MP_ROM_QSTR(MP_QSTR_mode), MP_ROM_PTR(&pin_mode_obj) },
  522. { MP_ROM_QSTR(MP_QSTR_pull), MP_ROM_PTR(&pin_pull_obj) },
  523. { MP_ROM_QSTR(MP_QSTR_af), MP_ROM_PTR(&pin_af_obj) },
  524. { MP_ROM_QSTR(MP_QSTR_irq), MP_ROM_PTR(&pin_irq_obj) },
  525. // class methods
  526. { MP_ROM_QSTR(MP_QSTR_mapper), MP_ROM_PTR(&pin_mapper_obj) },
  527. { MP_ROM_QSTR(MP_QSTR_dict), MP_ROM_PTR(&pin_map_dict_obj) },
  528. #if PIN_DEBUG
  529. { MP_ROM_QSTR(MP_QSTR_debug), MP_ROM_PTR(&pin_debug_obj) },
  530. #endif
  531. // class attributes
  532. { MP_ROM_QSTR(MP_QSTR_board), MP_ROM_PTR(&pin_board_pins_obj_type) },
  533. { MP_ROM_QSTR(MP_QSTR_cpu), MP_ROM_PTR(&pin_cpu_pins_obj_type) },
  534. // class constants
  535. { MP_ROM_QSTR(MP_QSTR_IN), MP_ROM_INT(NRF_GPIO_PIN_DIR_INPUT) },
  536. { MP_ROM_QSTR(MP_QSTR_OUT), MP_ROM_INT(NRF_GPIO_PIN_DIR_OUTPUT) },
  537. /*
  538. { MP_ROM_QSTR(MP_QSTR_OPEN_DRAIN), MP_ROM_INT(GPIO_MODE_OUTPUT_OD) },
  539. { MP_ROM_QSTR(MP_QSTR_ALT), MP_ROM_INT(GPIO_MODE_AF_PP) },
  540. { MP_ROM_QSTR(MP_QSTR_ALT_OPEN_DRAIN), MP_ROM_INT(GPIO_MODE_AF_OD) },
  541. { MP_ROM_QSTR(MP_QSTR_ANALOG), MP_ROM_INT(GPIO_MODE_ANALOG) },
  542. */
  543. { MP_ROM_QSTR(MP_QSTR_PULL_DISABLED), MP_ROM_INT(NRF_GPIO_PIN_NOPULL) },
  544. { MP_ROM_QSTR(MP_QSTR_PULL_UP), MP_ROM_INT(NRF_GPIO_PIN_PULLUP) },
  545. { MP_ROM_QSTR(MP_QSTR_PULL_DOWN), MP_ROM_INT(NRF_GPIO_PIN_PULLDOWN) },
  546. // IRQ triggers, can be or'd together
  547. { MP_ROM_QSTR(MP_QSTR_IRQ_RISING), MP_ROM_INT(NRF_GPIOTE_POLARITY_LOTOHI) },
  548. { MP_ROM_QSTR(MP_QSTR_IRQ_FALLING), MP_ROM_INT(NRF_GPIOTE_POLARITY_HITOLO) },
  549. /*
  550. // legacy class constants
  551. { MP_ROM_QSTR(MP_QSTR_OUT_PP), MP_ROM_INT(GPIO_MODE_OUTPUT_PP) },
  552. { MP_ROM_QSTR(MP_QSTR_OUT_OD), MP_ROM_INT(GPIO_MODE_OUTPUT_OD) },
  553. { MP_ROM_QSTR(MP_QSTR_AF_PP), MP_ROM_INT(GPIO_MODE_AF_PP) },
  554. { MP_ROM_QSTR(MP_QSTR_AF_OD), MP_ROM_INT(GPIO_MODE_AF_OD) },
  555. { MP_ROM_QSTR(MP_QSTR_PULL_NONE), MP_ROM_INT(GPIO_NOPULL) },
  556. */
  557. #include "genhdr/pins_af_const.h"
  558. };
  559. STATIC MP_DEFINE_CONST_DICT(pin_locals_dict, pin_locals_dict_table);
  560. const mp_obj_type_t pin_type = {
  561. { &mp_type_type },
  562. .name = MP_QSTR_Pin,
  563. .print = pin_print,
  564. .make_new = pin_make_new,
  565. .call = pin_call,
  566. .locals_dict = (mp_obj_dict_t*)&pin_locals_dict,
  567. };
  568. /// \moduleref machine
  569. /// \class PinAF - Pin Alternate Functions
  570. ///
  571. /// A Pin represents a physical pin on the microcprocessor. Each pin
  572. /// can have a variety of functions (GPIO, I2C SDA, etc). Each PinAF
  573. /// object represents a particular function for a pin.
  574. ///
  575. /// Usage Model:
  576. ///
  577. /// x3 = machine.Pin.board.X3
  578. /// x3_af = x3.af_list()
  579. ///
  580. /// x3_af will now contain an array of PinAF objects which are availble on
  581. /// pin X3.
  582. ///
  583. /// For the pyboard, x3_af would contain:
  584. /// [Pin.AF1_TIM2, Pin.AF2_TIM5, Pin.AF3_TIM9, Pin.AF7_USART2]
  585. ///
  586. /// Normally, each peripheral would configure the af automatically, but sometimes
  587. /// the same function is available on multiple pins, and having more control
  588. /// is desired.
  589. ///
  590. /// To configure X3 to expose TIM2_CH3, you could use:
  591. /// pin = machine.Pin(machine.Pin.board.X3, mode=machine.Pin.AF_PP, af=machine.Pin.AF1_TIM2)
  592. /// or:
  593. /// pin = machine.Pin(machine.Pin.board.X3, mode=machine.Pin.AF_PP, af=1)
  594. /// \method __str__()
  595. /// Return a string describing the alternate function.
  596. STATIC void pin_af_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  597. pin_af_obj_t *self = self_in;
  598. mp_printf(print, "Pin.%q", self->name);
  599. }
  600. /// \method index()
  601. /// Return the alternate function index.
  602. STATIC mp_obj_t pin_af_index(mp_obj_t self_in) {
  603. pin_af_obj_t *af = self_in;
  604. return MP_OBJ_NEW_SMALL_INT(af->idx);
  605. }
  606. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_af_index_obj, pin_af_index);
  607. /// \method name()
  608. /// Return the name of the alternate function.
  609. STATIC mp_obj_t pin_af_name(mp_obj_t self_in) {
  610. pin_af_obj_t *af = self_in;
  611. return MP_OBJ_NEW_QSTR(af->name);
  612. }
  613. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_af_name_obj, pin_af_name);
  614. /// \method reg()
  615. /// Return the base register associated with the peripheral assigned to this
  616. /// alternate function.
  617. STATIC mp_obj_t pin_af_reg(mp_obj_t self_in) {
  618. pin_af_obj_t *af = self_in;
  619. return MP_OBJ_NEW_SMALL_INT((mp_uint_t)af->reg);
  620. }
  621. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_af_reg_obj, pin_af_reg);
  622. STATIC const mp_rom_map_elem_t pin_af_locals_dict_table[] = {
  623. { MP_ROM_QSTR(MP_QSTR_index), MP_ROM_PTR(&pin_af_index_obj) },
  624. { MP_ROM_QSTR(MP_QSTR_name), MP_ROM_PTR(&pin_af_name_obj) },
  625. { MP_ROM_QSTR(MP_QSTR_reg), MP_ROM_PTR(&pin_af_reg_obj) },
  626. };
  627. STATIC MP_DEFINE_CONST_DICT(pin_af_locals_dict, pin_af_locals_dict_table);
  628. const mp_obj_type_t pin_af_type = {
  629. { &mp_type_type },
  630. .name = MP_QSTR_PinAF,
  631. .print = pin_af_obj_print,
  632. .locals_dict = (mp_obj_dict_t*)&pin_af_locals_dict,
  633. };