pin.c 24 KB

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