pybpin.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  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) 2015 Daniel Campora
  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/runtime.h"
  31. #include "py/gc.h"
  32. #include "inc/hw_types.h"
  33. #include "inc/hw_gpio.h"
  34. #include "inc/hw_ints.h"
  35. #include "inc/hw_memmap.h"
  36. #include "rom_map.h"
  37. #include "pin.h"
  38. #include "prcm.h"
  39. #include "gpio.h"
  40. #include "interrupt.h"
  41. #include "pybpin.h"
  42. #include "mpirq.h"
  43. #include "pins.h"
  44. #include "pybsleep.h"
  45. #include "mpexception.h"
  46. #include "mperror.h"
  47. /// \moduleref pyb
  48. /// \class Pin - control I/O pins
  49. ///
  50. /******************************************************************************
  51. DECLARE PRIVATE FUNCTIONS
  52. ******************************************************************************/
  53. STATIC pin_obj_t *pin_find_named_pin(const mp_obj_dict_t *named_pins, mp_obj_t name);
  54. STATIC pin_obj_t *pin_find_pin_by_port_bit (const mp_obj_dict_t *named_pins, uint port, uint bit);
  55. STATIC int8_t pin_obj_find_af (const pin_obj_t* pin, uint8_t fn, uint8_t unit, uint8_t type);
  56. STATIC void pin_free_af_from_pins (uint8_t fn, uint8_t unit, uint8_t type);
  57. STATIC void pin_deassign (pin_obj_t* pin);
  58. STATIC void pin_obj_configure (const pin_obj_t *self);
  59. STATIC void pin_get_hibernate_pin_and_idx (const pin_obj_t *self, uint *wake_pin, uint *idx);
  60. STATIC void pin_irq_enable (mp_obj_t self_in);
  61. STATIC void pin_irq_disable (mp_obj_t self_in);
  62. STATIC void pin_extint_register(pin_obj_t *self, uint32_t intmode, uint32_t priority);
  63. STATIC void pin_validate_mode (uint mode);
  64. STATIC void pin_validate_pull (uint pull);
  65. STATIC void pin_validate_drive (uint strength);
  66. STATIC void pin_validate_af(const pin_obj_t* pin, int8_t idx, uint8_t *fn, uint8_t *unit, uint8_t *type);
  67. STATIC uint8_t pin_get_value(const pin_obj_t* self);
  68. STATIC void GPIOA0IntHandler (void);
  69. STATIC void GPIOA1IntHandler (void);
  70. STATIC void GPIOA2IntHandler (void);
  71. STATIC void GPIOA3IntHandler (void);
  72. STATIC void EXTI_Handler(uint port);
  73. /******************************************************************************
  74. DEFINE CONSTANTS
  75. ******************************************************************************/
  76. #define PYBPIN_NUM_WAKE_PINS (6)
  77. #define PYBPIN_WAKES_NOT (-1)
  78. #define GPIO_DIR_MODE_ALT 0x00000002 // Pin is NOT controlled by the PGIO module
  79. #define GPIO_DIR_MODE_ALT_OD 0x00000003 // Pin is NOT controlled by the PGIO module and is in open drain mode
  80. #define PYB_PIN_FALLING_EDGE 0x01
  81. #define PYB_PIN_RISING_EDGE 0x02
  82. #define PYB_PIN_LOW_LEVEL 0x04
  83. #define PYB_PIN_HIGH_LEVEL 0x08
  84. /******************************************************************************
  85. DEFINE TYPES
  86. ******************************************************************************/
  87. typedef struct {
  88. bool active;
  89. int8_t lpds;
  90. int8_t hib;
  91. } pybpin_wake_pin_t;
  92. /******************************************************************************
  93. DECLARE PRIVATE DATA
  94. ******************************************************************************/
  95. STATIC const mp_irq_methods_t pin_irq_methods;
  96. STATIC pybpin_wake_pin_t pybpin_wake_pin[PYBPIN_NUM_WAKE_PINS] =
  97. { {.active = false, .lpds = PYBPIN_WAKES_NOT, .hib = PYBPIN_WAKES_NOT},
  98. {.active = false, .lpds = PYBPIN_WAKES_NOT, .hib = PYBPIN_WAKES_NOT},
  99. {.active = false, .lpds = PYBPIN_WAKES_NOT, .hib = PYBPIN_WAKES_NOT},
  100. {.active = false, .lpds = PYBPIN_WAKES_NOT, .hib = PYBPIN_WAKES_NOT},
  101. {.active = false, .lpds = PYBPIN_WAKES_NOT, .hib = PYBPIN_WAKES_NOT},
  102. {.active = false, .lpds = PYBPIN_WAKES_NOT, .hib = PYBPIN_WAKES_NOT} } ;
  103. /******************************************************************************
  104. DEFINE PUBLIC FUNCTIONS
  105. ******************************************************************************/
  106. void pin_init0(void) {
  107. // this initalization also reconfigures the JTAG/SWD pins
  108. #ifndef DEBUG
  109. // assign all pins to the GPIO module so that peripherals can be connected to any
  110. // pins without conflicts after a soft reset
  111. const mp_map_t *named_map = &pin_board_pins_locals_dict.map;
  112. for (uint i = 0; i < named_map->used - 1; i++) {
  113. pin_obj_t * pin = (pin_obj_t *)named_map->table[i].value;
  114. pin_deassign (pin);
  115. }
  116. #endif
  117. }
  118. // C API used to convert a user-supplied pin name into an ordinal pin number.
  119. pin_obj_t *pin_find(mp_obj_t user_obj) {
  120. pin_obj_t *pin_obj;
  121. // if a pin was provided, use it
  122. if (MP_OBJ_IS_TYPE(user_obj, &pin_type)) {
  123. pin_obj = user_obj;
  124. return pin_obj;
  125. }
  126. // otherwise see if the pin name matches a cpu pin
  127. pin_obj = pin_find_named_pin(&pin_board_pins_locals_dict, user_obj);
  128. if (pin_obj) {
  129. return pin_obj;
  130. }
  131. mp_raise_ValueError(mpexception_value_invalid_arguments);
  132. }
  133. void pin_config (pin_obj_t *self, int af, uint mode, uint pull, int value, uint strength) {
  134. self->mode = mode, self->pull = pull, self->strength = strength;
  135. // if af is -1, then we want to keep it as it is
  136. if (af != -1) {
  137. self->af = af;
  138. }
  139. // if value is -1, then we want to keep it as it is
  140. if (value != -1) {
  141. self->value = value;
  142. }
  143. // mark the pin as used
  144. self->used = true;
  145. pin_obj_configure ((const pin_obj_t *)self);
  146. // register it with the sleep module
  147. pyb_sleep_add ((const mp_obj_t)self, (WakeUpCB_t)pin_obj_configure);
  148. }
  149. void pin_assign_pins_af (mp_obj_t *pins, uint32_t n_pins, uint32_t pull, uint32_t fn, uint32_t unit) {
  150. for (int i = 0; i < n_pins; i++) {
  151. pin_free_af_from_pins(fn, unit, i);
  152. if (pins[i] != mp_const_none) {
  153. pin_obj_t *pin = pin_find(pins[i]);
  154. pin_config (pin, pin_find_af_index(pin, fn, unit, i), 0, pull, -1, PIN_STRENGTH_2MA);
  155. }
  156. }
  157. }
  158. uint8_t pin_find_peripheral_unit (const mp_obj_t pin, uint8_t fn, uint8_t type) {
  159. pin_obj_t *pin_o = pin_find(pin);
  160. for (int i = 0; i < pin_o->num_afs; i++) {
  161. if (pin_o->af_list[i].fn == fn && pin_o->af_list[i].type == type) {
  162. return pin_o->af_list[i].unit;
  163. }
  164. }
  165. mp_raise_ValueError(mpexception_value_invalid_arguments);
  166. }
  167. uint8_t pin_find_peripheral_type (const mp_obj_t pin, uint8_t fn, uint8_t unit) {
  168. pin_obj_t *pin_o = pin_find(pin);
  169. for (int i = 0; i < pin_o->num_afs; i++) {
  170. if (pin_o->af_list[i].fn == fn && pin_o->af_list[i].unit == unit) {
  171. return pin_o->af_list[i].type;
  172. }
  173. }
  174. mp_raise_ValueError(mpexception_value_invalid_arguments);
  175. }
  176. int8_t pin_find_af_index (const pin_obj_t* pin, uint8_t fn, uint8_t unit, uint8_t type) {
  177. int8_t af = pin_obj_find_af(pin, fn, unit, type);
  178. if (af < 0) {
  179. mp_raise_ValueError(mpexception_value_invalid_arguments);
  180. }
  181. return af;
  182. }
  183. /******************************************************************************
  184. DEFINE PRIVATE FUNCTIONS
  185. ******************************************************************************/
  186. STATIC pin_obj_t *pin_find_named_pin(const mp_obj_dict_t *named_pins, mp_obj_t name) {
  187. const mp_map_t *named_map = &named_pins->map;
  188. mp_map_elem_t *named_elem = mp_map_lookup((mp_map_t*)named_map, name, MP_MAP_LOOKUP);
  189. if (named_elem != NULL && named_elem->value != NULL) {
  190. return named_elem->value;
  191. }
  192. return NULL;
  193. }
  194. STATIC pin_obj_t *pin_find_pin_by_port_bit (const mp_obj_dict_t *named_pins, uint port, uint bit) {
  195. const mp_map_t *named_map = &named_pins->map;
  196. for (uint i = 0; i < named_map->used; i++) {
  197. if ((((pin_obj_t *)named_map->table[i].value)->port == port) &&
  198. (((pin_obj_t *)named_map->table[i].value)->bit == bit)) {
  199. return named_map->table[i].value;
  200. }
  201. }
  202. return NULL;
  203. }
  204. STATIC int8_t pin_obj_find_af (const pin_obj_t* pin, uint8_t fn, uint8_t unit, uint8_t type) {
  205. for (int i = 0; i < pin->num_afs; i++) {
  206. if (pin->af_list[i].fn == fn && pin->af_list[i].unit == unit && pin->af_list[i].type == type) {
  207. return pin->af_list[i].idx;
  208. }
  209. }
  210. return -1;
  211. }
  212. STATIC void pin_free_af_from_pins (uint8_t fn, uint8_t unit, uint8_t type) {
  213. const mp_map_t *named_map = &pin_board_pins_locals_dict.map;
  214. for (uint i = 0; i < named_map->used - 1; i++) {
  215. pin_obj_t * pin = (pin_obj_t *)named_map->table[i].value;
  216. // af is different than GPIO
  217. if (pin->af > PIN_MODE_0) {
  218. // check if the pin supports the target af
  219. int af = pin_obj_find_af(pin, fn, unit, type);
  220. if (af > 0 && af == pin->af) {
  221. // the pin supports the target af, de-assign it
  222. pin_deassign (pin);
  223. }
  224. }
  225. }
  226. }
  227. STATIC void pin_deassign (pin_obj_t* pin) {
  228. pin_config (pin, PIN_MODE_0, GPIO_DIR_MODE_IN, PIN_TYPE_STD, -1, PIN_STRENGTH_4MA);
  229. pin->used = false;
  230. }
  231. STATIC void pin_obj_configure (const pin_obj_t *self) {
  232. uint32_t type;
  233. if (self->mode == PIN_TYPE_ANALOG) {
  234. type = PIN_TYPE_ANALOG;
  235. } else {
  236. type = self->pull;
  237. uint32_t direction = self->mode;
  238. if (direction == PIN_TYPE_OD || direction == GPIO_DIR_MODE_ALT_OD) {
  239. direction = GPIO_DIR_MODE_OUT;
  240. type |= PIN_TYPE_OD;
  241. }
  242. if (self->mode != GPIO_DIR_MODE_ALT && self->mode != GPIO_DIR_MODE_ALT_OD) {
  243. // enable the peripheral clock for the GPIO port of this pin
  244. switch (self->port) {
  245. case PORT_A0:
  246. MAP_PRCMPeripheralClkEnable(PRCM_GPIOA0, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
  247. break;
  248. case PORT_A1:
  249. MAP_PRCMPeripheralClkEnable(PRCM_GPIOA1, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
  250. break;
  251. case PORT_A2:
  252. MAP_PRCMPeripheralClkEnable(PRCM_GPIOA2, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
  253. break;
  254. case PORT_A3:
  255. MAP_PRCMPeripheralClkEnable(PRCM_GPIOA3, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
  256. break;
  257. default:
  258. break;
  259. }
  260. // configure the direction
  261. MAP_GPIODirModeSet(self->port, self->bit, direction);
  262. // set the pin value
  263. if (self->value) {
  264. MAP_GPIOPinWrite(self->port, self->bit, self->bit);
  265. } else {
  266. MAP_GPIOPinWrite(self->port, self->bit, 0);
  267. }
  268. }
  269. // now set the alternate function
  270. MAP_PinModeSet (self->pin_num, self->af);
  271. }
  272. MAP_PinConfigSet(self->pin_num, self->strength, type);
  273. }
  274. STATIC void pin_get_hibernate_pin_and_idx (const pin_obj_t *self, uint *hib_pin, uint *idx) {
  275. // pin_num is actually : (package_pin - 1)
  276. switch (self->pin_num) {
  277. case 56: // GP2
  278. *hib_pin = PRCM_HIB_GPIO2;
  279. *idx = 0;
  280. break;
  281. case 58: // GP4
  282. *hib_pin = PRCM_HIB_GPIO4;
  283. *idx = 1;
  284. break;
  285. case 3: // GP13
  286. *hib_pin = PRCM_HIB_GPIO13;
  287. *idx = 2;
  288. break;
  289. case 7: // GP17
  290. *hib_pin = PRCM_HIB_GPIO17;
  291. *idx = 3;
  292. break;
  293. case 1: // GP11
  294. *hib_pin = PRCM_HIB_GPIO11;
  295. *idx = 4;
  296. break;
  297. case 16: // GP24
  298. *hib_pin = PRCM_HIB_GPIO24;
  299. *idx = 5;
  300. break;
  301. default:
  302. *idx = 0xFF;
  303. break;
  304. }
  305. }
  306. STATIC void pin_irq_enable (mp_obj_t self_in) {
  307. const pin_obj_t *self = self_in;
  308. uint hib_pin, idx;
  309. pin_get_hibernate_pin_and_idx (self, &hib_pin, &idx);
  310. if (idx < PYBPIN_NUM_WAKE_PINS) {
  311. if (pybpin_wake_pin[idx].lpds != PYBPIN_WAKES_NOT) {
  312. // enable GPIO as a wake source during LPDS
  313. MAP_PRCMLPDSWakeUpGPIOSelect(idx, pybpin_wake_pin[idx].lpds);
  314. MAP_PRCMLPDSWakeupSourceEnable(PRCM_LPDS_GPIO);
  315. }
  316. if (pybpin_wake_pin[idx].hib != PYBPIN_WAKES_NOT) {
  317. // enable GPIO as a wake source during hibernate
  318. MAP_PRCMHibernateWakeUpGPIOSelect(hib_pin, pybpin_wake_pin[idx].hib);
  319. MAP_PRCMHibernateWakeupSourceEnable(hib_pin);
  320. }
  321. else {
  322. MAP_PRCMHibernateWakeupSourceDisable(hib_pin);
  323. }
  324. }
  325. // if idx is invalid, the pin supports active interrupts for sure
  326. if (idx >= PYBPIN_NUM_WAKE_PINS || pybpin_wake_pin[idx].active) {
  327. MAP_GPIOIntClear(self->port, self->bit);
  328. MAP_GPIOIntEnable(self->port, self->bit);
  329. }
  330. // in case it was enabled before
  331. else if (idx < PYBPIN_NUM_WAKE_PINS && !pybpin_wake_pin[idx].active) {
  332. MAP_GPIOIntDisable(self->port, self->bit);
  333. }
  334. }
  335. STATIC void pin_irq_disable (mp_obj_t self_in) {
  336. const pin_obj_t *self = self_in;
  337. uint hib_pin, idx;
  338. pin_get_hibernate_pin_and_idx (self, &hib_pin, &idx);
  339. if (idx < PYBPIN_NUM_WAKE_PINS) {
  340. if (pybpin_wake_pin[idx].lpds != PYBPIN_WAKES_NOT) {
  341. // disable GPIO as a wake source during LPDS
  342. MAP_PRCMLPDSWakeupSourceDisable(PRCM_LPDS_GPIO);
  343. }
  344. if (pybpin_wake_pin[idx].hib != PYBPIN_WAKES_NOT) {
  345. // disable GPIO as a wake source during hibernate
  346. MAP_PRCMHibernateWakeupSourceDisable(hib_pin);
  347. }
  348. }
  349. // not need to check for the active flag, it's safe to disable it anyway
  350. MAP_GPIOIntDisable(self->port, self->bit);
  351. }
  352. STATIC int pin_irq_flags (mp_obj_t self_in) {
  353. const pin_obj_t *self = self_in;
  354. return self->irq_flags;
  355. }
  356. STATIC void pin_extint_register(pin_obj_t *self, uint32_t intmode, uint32_t priority) {
  357. void *handler;
  358. uint32_t intnum;
  359. // configure the interrupt type
  360. MAP_GPIOIntTypeSet(self->port, self->bit, intmode);
  361. switch (self->port) {
  362. case GPIOA0_BASE:
  363. handler = GPIOA0IntHandler;
  364. intnum = INT_GPIOA0;
  365. break;
  366. case GPIOA1_BASE:
  367. handler = GPIOA1IntHandler;
  368. intnum = INT_GPIOA1;
  369. break;
  370. case GPIOA2_BASE:
  371. handler = GPIOA2IntHandler;
  372. intnum = INT_GPIOA2;
  373. break;
  374. case GPIOA3_BASE:
  375. default:
  376. handler = GPIOA3IntHandler;
  377. intnum = INT_GPIOA3;
  378. break;
  379. }
  380. MAP_GPIOIntRegister(self->port, handler);
  381. // set the interrupt to the lowest priority, to make sure that
  382. // no other ISRs will be preemted by this one
  383. MAP_IntPrioritySet(intnum, priority);
  384. }
  385. STATIC void pin_validate_mode (uint mode) {
  386. if (mode != GPIO_DIR_MODE_IN && mode != GPIO_DIR_MODE_OUT && mode != PIN_TYPE_OD &&
  387. mode != GPIO_DIR_MODE_ALT && mode != GPIO_DIR_MODE_ALT_OD) {
  388. mp_raise_ValueError(mpexception_value_invalid_arguments);
  389. }
  390. }
  391. STATIC void pin_validate_pull (uint pull) {
  392. if (pull != PIN_TYPE_STD && pull != PIN_TYPE_STD_PU && pull != PIN_TYPE_STD_PD) {
  393. mp_raise_ValueError(mpexception_value_invalid_arguments);
  394. }
  395. }
  396. STATIC void pin_validate_drive(uint strength) {
  397. if (strength != PIN_STRENGTH_2MA && strength != PIN_STRENGTH_4MA && strength != PIN_STRENGTH_6MA) {
  398. mp_raise_ValueError(mpexception_value_invalid_arguments);
  399. }
  400. }
  401. STATIC void pin_validate_af(const pin_obj_t* pin, int8_t idx, uint8_t *fn, uint8_t *unit, uint8_t *type) {
  402. for (int i = 0; i < pin->num_afs; i++) {
  403. if (pin->af_list[i].idx == idx) {
  404. *fn = pin->af_list[i].fn;
  405. *unit = pin->af_list[i].unit;
  406. *type = pin->af_list[i].type;
  407. return;
  408. }
  409. }
  410. mp_raise_ValueError(mpexception_value_invalid_arguments);
  411. }
  412. STATIC uint8_t pin_get_value (const pin_obj_t* self) {
  413. uint32_t value;
  414. bool setdir = false;
  415. if (self->mode == PIN_TYPE_OD || self->mode == GPIO_DIR_MODE_ALT_OD) {
  416. setdir = true;
  417. // configure the direction to IN for a moment in order to read the pin value
  418. MAP_GPIODirModeSet(self->port, self->bit, GPIO_DIR_MODE_IN);
  419. }
  420. // now get the value
  421. value = MAP_GPIOPinRead(self->port, self->bit);
  422. if (setdir) {
  423. // set the direction back to output
  424. MAP_GPIODirModeSet(self->port, self->bit, GPIO_DIR_MODE_OUT);
  425. if (self->value) {
  426. MAP_GPIOPinWrite(self->port, self->bit, self->bit);
  427. } else {
  428. MAP_GPIOPinWrite(self->port, self->bit, 0);
  429. }
  430. }
  431. // return it
  432. return value ? 1 : 0;
  433. }
  434. STATIC void GPIOA0IntHandler (void) {
  435. EXTI_Handler(GPIOA0_BASE);
  436. }
  437. STATIC void GPIOA1IntHandler (void) {
  438. EXTI_Handler(GPIOA1_BASE);
  439. }
  440. STATIC void GPIOA2IntHandler (void) {
  441. EXTI_Handler(GPIOA2_BASE);
  442. }
  443. STATIC void GPIOA3IntHandler (void) {
  444. EXTI_Handler(GPIOA3_BASE);
  445. }
  446. // common interrupt handler
  447. STATIC void EXTI_Handler(uint port) {
  448. uint32_t bits = MAP_GPIOIntStatus(port, true);
  449. MAP_GPIOIntClear(port, bits);
  450. // might be that we have more than one pin interrupt pending
  451. // therefore we must loop through all of the 8 possible bits
  452. for (int i = 0; i < 8; i++) {
  453. uint32_t bit = (1 << i);
  454. if (bit & bits) {
  455. pin_obj_t *self = (pin_obj_t *)pin_find_pin_by_port_bit(&pin_board_pins_locals_dict, port, bit);
  456. if (self->irq_trigger == (PYB_PIN_FALLING_EDGE | PYB_PIN_RISING_EDGE)) {
  457. // read the pin value (hoping that the pin level has remained stable)
  458. self->irq_flags = MAP_GPIOPinRead(self->port, self->bit) ? PYB_PIN_RISING_EDGE : PYB_PIN_FALLING_EDGE;
  459. } else {
  460. // same as the triggers
  461. self->irq_flags = self->irq_trigger;
  462. }
  463. mp_irq_handler(mp_irq_find(self));
  464. // always clear the flags after leaving the user handler
  465. self->irq_flags = 0;
  466. }
  467. }
  468. }
  469. /******************************************************************************/
  470. // MicroPython bindings
  471. STATIC const mp_arg_t pin_init_args[] = {
  472. { MP_QSTR_mode, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
  473. { MP_QSTR_pull, MP_ARG_OBJ, {.u_obj = mp_const_none} },
  474. { MP_QSTR_value, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
  475. { MP_QSTR_drive, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = PIN_STRENGTH_4MA} },
  476. { MP_QSTR_alt, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
  477. };
  478. #define pin_INIT_NUM_ARGS MP_ARRAY_SIZE(pin_init_args)
  479. STATIC mp_obj_t pin_obj_init_helper(pin_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
  480. // parse args
  481. mp_arg_val_t args[pin_INIT_NUM_ARGS];
  482. mp_arg_parse_all(n_args, pos_args, kw_args, pin_INIT_NUM_ARGS, pin_init_args, args);
  483. // get the io mode
  484. uint mode;
  485. // default is input
  486. if (args[0].u_obj == MP_OBJ_NULL) {
  487. mode = GPIO_DIR_MODE_IN;
  488. } else {
  489. mode = mp_obj_get_int(args[0].u_obj);
  490. pin_validate_mode (mode);
  491. }
  492. // get the pull type
  493. uint pull;
  494. if (args[1].u_obj == mp_const_none) {
  495. pull = PIN_TYPE_STD;
  496. } else {
  497. pull = mp_obj_get_int(args[1].u_obj);
  498. pin_validate_pull (pull);
  499. }
  500. // get the value
  501. int value = -1;
  502. if (args[2].u_obj != MP_OBJ_NULL) {
  503. if (mp_obj_is_true(args[2].u_obj)) {
  504. value = 1;
  505. } else {
  506. value = 0;
  507. }
  508. }
  509. // get the strenght
  510. uint strength = args[3].u_int;
  511. pin_validate_drive(strength);
  512. // get the alternate function
  513. int af = args[4].u_int;
  514. if (mode != GPIO_DIR_MODE_ALT && mode != GPIO_DIR_MODE_ALT_OD) {
  515. if (af == -1) {
  516. af = 0;
  517. } else {
  518. goto invalid_args;
  519. }
  520. } else if (af < -1 || af > 15) {
  521. goto invalid_args;
  522. }
  523. // check for a valid af and then free it from any other pins
  524. if (af > PIN_MODE_0) {
  525. uint8_t fn, unit, type;
  526. pin_validate_af (self, af, &fn, &unit, &type);
  527. pin_free_af_from_pins(fn, unit, type);
  528. }
  529. pin_config (self, af, mode, pull, value, strength);
  530. return mp_const_none;
  531. invalid_args:
  532. mp_raise_ValueError(mpexception_value_invalid_arguments);
  533. }
  534. STATIC void pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  535. pin_obj_t *self = self_in;
  536. uint32_t pull = self->pull;
  537. uint32_t drive = self->strength;
  538. // pin name
  539. mp_printf(print, "Pin('%q'", self->name);
  540. // pin mode
  541. qstr mode_qst;
  542. uint32_t mode = self->mode;
  543. if (mode == GPIO_DIR_MODE_IN) {
  544. mode_qst = MP_QSTR_IN;
  545. } else if (mode == GPIO_DIR_MODE_OUT) {
  546. mode_qst = MP_QSTR_OUT;
  547. } else if (mode == GPIO_DIR_MODE_ALT) {
  548. mode_qst = MP_QSTR_ALT;
  549. } else if (mode == GPIO_DIR_MODE_ALT_OD) {
  550. mode_qst = MP_QSTR_ALT_OPEN_DRAIN;
  551. } else {
  552. mode_qst = MP_QSTR_OPEN_DRAIN;
  553. }
  554. mp_printf(print, ", mode=Pin.%q", mode_qst);
  555. // pin pull
  556. qstr pull_qst;
  557. if (pull == PIN_TYPE_STD) {
  558. mp_printf(print, ", pull=%q", MP_QSTR_None);
  559. } else {
  560. if (pull == PIN_TYPE_STD_PU) {
  561. pull_qst = MP_QSTR_PULL_UP;
  562. } else {
  563. pull_qst = MP_QSTR_PULL_DOWN;
  564. }
  565. mp_printf(print, ", pull=Pin.%q", pull_qst);
  566. }
  567. // pin drive
  568. qstr drv_qst;
  569. if (drive == PIN_STRENGTH_2MA) {
  570. drv_qst = MP_QSTR_LOW_POWER;
  571. } else if (drive == PIN_STRENGTH_4MA) {
  572. drv_qst = MP_QSTR_MED_POWER;
  573. } else {
  574. drv_qst = MP_QSTR_HIGH_POWER;
  575. }
  576. mp_printf(print, ", drive=Pin.%q", drv_qst);
  577. // pin af
  578. int alt = (self->af == 0) ? -1 : self->af;
  579. mp_printf(print, ", alt=%d)", alt);
  580. }
  581. STATIC mp_obj_t pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  582. mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
  583. // Run an argument through the mapper and return the result.
  584. pin_obj_t *pin = (pin_obj_t *)pin_find(args[0]);
  585. mp_map_t kw_args;
  586. mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
  587. pin_obj_init_helper(pin, n_args - 1, args + 1, &kw_args);
  588. return (mp_obj_t)pin;
  589. }
  590. STATIC mp_obj_t pin_obj_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
  591. return pin_obj_init_helper(args[0], n_args - 1, args + 1, kw_args);
  592. }
  593. MP_DEFINE_CONST_FUN_OBJ_KW(pin_init_obj, 1, pin_obj_init);
  594. STATIC mp_obj_t pin_value(size_t n_args, const mp_obj_t *args) {
  595. pin_obj_t *self = args[0];
  596. if (n_args == 1) {
  597. // get the value
  598. return MP_OBJ_NEW_SMALL_INT(pin_get_value(self));
  599. } else {
  600. // set the pin value
  601. if (mp_obj_is_true(args[1])) {
  602. self->value = 1;
  603. MAP_GPIOPinWrite(self->port, self->bit, self->bit);
  604. } else {
  605. self->value = 0;
  606. MAP_GPIOPinWrite(self->port, self->bit, 0);
  607. }
  608. return mp_const_none;
  609. }
  610. }
  611. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pin_value_obj, 1, 2, pin_value);
  612. STATIC mp_obj_t pin_id(mp_obj_t self_in) {
  613. pin_obj_t *self = self_in;
  614. return MP_OBJ_NEW_QSTR(self->name);
  615. }
  616. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_id_obj, pin_id);
  617. STATIC mp_obj_t pin_mode(size_t n_args, const mp_obj_t *args) {
  618. pin_obj_t *self = args[0];
  619. if (n_args == 1) {
  620. return mp_obj_new_int(self->mode);
  621. } else {
  622. uint32_t mode = mp_obj_get_int(args[1]);
  623. pin_validate_mode (mode);
  624. self->mode = mode;
  625. pin_obj_configure(self);
  626. return mp_const_none;
  627. }
  628. }
  629. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pin_mode_obj, 1, 2, pin_mode);
  630. STATIC mp_obj_t pin_pull(size_t n_args, const mp_obj_t *args) {
  631. pin_obj_t *self = args[0];
  632. if (n_args == 1) {
  633. if (self->pull == PIN_TYPE_STD) {
  634. return mp_const_none;
  635. }
  636. return mp_obj_new_int(self->pull);
  637. } else {
  638. uint32_t pull;
  639. if (args[1] == mp_const_none) {
  640. pull = PIN_TYPE_STD;
  641. } else {
  642. pull = mp_obj_get_int(args[1]);
  643. pin_validate_pull (pull);
  644. }
  645. self->pull = pull;
  646. pin_obj_configure(self);
  647. return mp_const_none;
  648. }
  649. }
  650. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pin_pull_obj, 1, 2, pin_pull);
  651. STATIC mp_obj_t pin_drive(size_t n_args, const mp_obj_t *args) {
  652. pin_obj_t *self = args[0];
  653. if (n_args == 1) {
  654. return mp_obj_new_int(self->strength);
  655. } else {
  656. uint32_t strength = mp_obj_get_int(args[1]);
  657. pin_validate_drive (strength);
  658. self->strength = strength;
  659. pin_obj_configure(self);
  660. return mp_const_none;
  661. }
  662. }
  663. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pin_drive_obj, 1, 2, pin_drive);
  664. STATIC mp_obj_t pin_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  665. mp_arg_check_num(n_args, n_kw, 0, 1, false);
  666. mp_obj_t _args[2] = {self_in, *args};
  667. return pin_value (n_args + 1, _args);
  668. }
  669. STATIC mp_obj_t pin_alt_list(mp_obj_t self_in) {
  670. pin_obj_t *self = self_in;
  671. mp_obj_t af[2];
  672. mp_obj_t afs = mp_obj_new_list(0, NULL);
  673. for (int i = 0; i < self->num_afs; i++) {
  674. af[0] = MP_OBJ_NEW_QSTR(self->af_list[i].name);
  675. af[1] = mp_obj_new_int(self->af_list[i].idx);
  676. mp_obj_list_append(afs, mp_obj_new_tuple(MP_ARRAY_SIZE(af), af));
  677. }
  678. return afs;
  679. }
  680. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_alt_list_obj, pin_alt_list);
  681. /// \method irq(trigger, priority, handler, wake)
  682. STATIC mp_obj_t pin_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
  683. mp_arg_val_t args[mp_irq_INIT_NUM_ARGS];
  684. mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, mp_irq_INIT_NUM_ARGS, mp_irq_init_args, args);
  685. pin_obj_t *self = pos_args[0];
  686. // convert the priority to the correct value
  687. uint priority = mp_irq_translate_priority (args[1].u_int);
  688. // verify and translate the interrupt mode
  689. uint mp_trigger = mp_obj_get_int(args[0].u_obj);
  690. uint trigger;
  691. if (mp_trigger == (PYB_PIN_FALLING_EDGE | PYB_PIN_RISING_EDGE)) {
  692. trigger = GPIO_BOTH_EDGES;
  693. } else {
  694. switch (mp_trigger) {
  695. case PYB_PIN_FALLING_EDGE:
  696. trigger = GPIO_FALLING_EDGE;
  697. break;
  698. case PYB_PIN_RISING_EDGE:
  699. trigger = GPIO_RISING_EDGE;
  700. break;
  701. case PYB_PIN_LOW_LEVEL:
  702. trigger = GPIO_LOW_LEVEL;
  703. break;
  704. case PYB_PIN_HIGH_LEVEL:
  705. trigger = GPIO_HIGH_LEVEL;
  706. break;
  707. default:
  708. goto invalid_args;
  709. }
  710. }
  711. uint8_t pwrmode = (args[3].u_obj == mp_const_none) ? PYB_PWR_MODE_ACTIVE : mp_obj_get_int(args[3].u_obj);
  712. if (pwrmode > (PYB_PWR_MODE_ACTIVE | PYB_PWR_MODE_LPDS | PYB_PWR_MODE_HIBERNATE)) {
  713. goto invalid_args;
  714. }
  715. // get the wake info from this pin
  716. uint hib_pin, idx;
  717. pin_get_hibernate_pin_and_idx ((const pin_obj_t *)self, &hib_pin, &idx);
  718. if (pwrmode & PYB_PWR_MODE_LPDS) {
  719. if (idx >= PYBPIN_NUM_WAKE_PINS) {
  720. goto invalid_args;
  721. }
  722. // wake modes are different in LDPS
  723. uint wake_mode;
  724. switch (trigger) {
  725. case GPIO_FALLING_EDGE:
  726. wake_mode = PRCM_LPDS_FALL_EDGE;
  727. break;
  728. case GPIO_RISING_EDGE:
  729. wake_mode = PRCM_LPDS_RISE_EDGE;
  730. break;
  731. case GPIO_LOW_LEVEL:
  732. wake_mode = PRCM_LPDS_LOW_LEVEL;
  733. break;
  734. case GPIO_HIGH_LEVEL:
  735. wake_mode = PRCM_LPDS_HIGH_LEVEL;
  736. break;
  737. default:
  738. goto invalid_args;
  739. break;
  740. }
  741. // first clear the lpds value from all wake-able pins
  742. for (uint i = 0; i < PYBPIN_NUM_WAKE_PINS; i++) {
  743. pybpin_wake_pin[i].lpds = PYBPIN_WAKES_NOT;
  744. }
  745. // enable this pin as a wake-up source during LPDS
  746. pybpin_wake_pin[idx].lpds = wake_mode;
  747. } else if (idx < PYBPIN_NUM_WAKE_PINS) {
  748. // this pin was the previous LPDS wake source, so disable it completely
  749. if (pybpin_wake_pin[idx].lpds != PYBPIN_WAKES_NOT) {
  750. MAP_PRCMLPDSWakeupSourceDisable(PRCM_LPDS_GPIO);
  751. }
  752. pybpin_wake_pin[idx].lpds = PYBPIN_WAKES_NOT;
  753. }
  754. if (pwrmode & PYB_PWR_MODE_HIBERNATE) {
  755. if (idx >= PYBPIN_NUM_WAKE_PINS) {
  756. goto invalid_args;
  757. }
  758. // wake modes are different in hibernate
  759. uint wake_mode;
  760. switch (trigger) {
  761. case GPIO_FALLING_EDGE:
  762. wake_mode = PRCM_HIB_FALL_EDGE;
  763. break;
  764. case GPIO_RISING_EDGE:
  765. wake_mode = PRCM_HIB_RISE_EDGE;
  766. break;
  767. case GPIO_LOW_LEVEL:
  768. wake_mode = PRCM_HIB_LOW_LEVEL;
  769. break;
  770. case GPIO_HIGH_LEVEL:
  771. wake_mode = PRCM_HIB_HIGH_LEVEL;
  772. break;
  773. default:
  774. goto invalid_args;
  775. break;
  776. }
  777. // enable this pin as wake-up source during hibernate
  778. pybpin_wake_pin[idx].hib = wake_mode;
  779. } else if (idx < PYBPIN_NUM_WAKE_PINS) {
  780. pybpin_wake_pin[idx].hib = PYBPIN_WAKES_NOT;
  781. }
  782. // we need to update the callback atomically, so we disable the
  783. // interrupt before we update anything.
  784. pin_irq_disable(self);
  785. if (pwrmode & PYB_PWR_MODE_ACTIVE) {
  786. // register the interrupt
  787. pin_extint_register((pin_obj_t *)self, trigger, priority);
  788. if (idx < PYBPIN_NUM_WAKE_PINS) {
  789. pybpin_wake_pin[idx].active = true;
  790. }
  791. } else if (idx < PYBPIN_NUM_WAKE_PINS) {
  792. pybpin_wake_pin[idx].active = false;
  793. }
  794. // all checks have passed, we can create the irq object
  795. mp_obj_t _irq = mp_irq_new (self, args[2].u_obj, &pin_irq_methods);
  796. if (pwrmode & PYB_PWR_MODE_LPDS) {
  797. pyb_sleep_set_gpio_lpds_callback (_irq);
  798. }
  799. // save the mp_trigge for later
  800. self->irq_trigger = mp_trigger;
  801. // enable the interrupt just before leaving
  802. pin_irq_enable(self);
  803. return _irq;
  804. invalid_args:
  805. mp_raise_ValueError(mpexception_value_invalid_arguments);
  806. }
  807. STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pin_irq_obj, 1, pin_irq);
  808. STATIC const mp_rom_map_elem_t pin_locals_dict_table[] = {
  809. // instance methods
  810. { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&pin_init_obj) },
  811. { MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&pin_value_obj) },
  812. { MP_ROM_QSTR(MP_QSTR_id), MP_ROM_PTR(&pin_id_obj) },
  813. { MP_ROM_QSTR(MP_QSTR_mode), MP_ROM_PTR(&pin_mode_obj) },
  814. { MP_ROM_QSTR(MP_QSTR_pull), MP_ROM_PTR(&pin_pull_obj) },
  815. { MP_ROM_QSTR(MP_QSTR_drive), MP_ROM_PTR(&pin_drive_obj) },
  816. { MP_ROM_QSTR(MP_QSTR_alt_list), MP_ROM_PTR(&pin_alt_list_obj) },
  817. { MP_ROM_QSTR(MP_QSTR_irq), MP_ROM_PTR(&pin_irq_obj) },
  818. // class attributes
  819. { MP_ROM_QSTR(MP_QSTR_board), MP_ROM_PTR(&pin_board_pins_obj_type) },
  820. // class constants
  821. { MP_ROM_QSTR(MP_QSTR_IN), MP_ROM_INT(GPIO_DIR_MODE_IN) },
  822. { MP_ROM_QSTR(MP_QSTR_OUT), MP_ROM_INT(GPIO_DIR_MODE_OUT) },
  823. { MP_ROM_QSTR(MP_QSTR_OPEN_DRAIN), MP_ROM_INT(PIN_TYPE_OD) },
  824. { MP_ROM_QSTR(MP_QSTR_ALT), MP_ROM_INT(GPIO_DIR_MODE_ALT) },
  825. { MP_ROM_QSTR(MP_QSTR_ALT_OPEN_DRAIN), MP_ROM_INT(GPIO_DIR_MODE_ALT_OD) },
  826. { MP_ROM_QSTR(MP_QSTR_PULL_UP), MP_ROM_INT(PIN_TYPE_STD_PU) },
  827. { MP_ROM_QSTR(MP_QSTR_PULL_DOWN), MP_ROM_INT(PIN_TYPE_STD_PD) },
  828. { MP_ROM_QSTR(MP_QSTR_LOW_POWER), MP_ROM_INT(PIN_STRENGTH_2MA) },
  829. { MP_ROM_QSTR(MP_QSTR_MED_POWER), MP_ROM_INT(PIN_STRENGTH_4MA) },
  830. { MP_ROM_QSTR(MP_QSTR_HIGH_POWER), MP_ROM_INT(PIN_STRENGTH_6MA) },
  831. { MP_ROM_QSTR(MP_QSTR_IRQ_FALLING), MP_ROM_INT(PYB_PIN_FALLING_EDGE) },
  832. { MP_ROM_QSTR(MP_QSTR_IRQ_RISING), MP_ROM_INT(PYB_PIN_RISING_EDGE) },
  833. { MP_ROM_QSTR(MP_QSTR_IRQ_LOW_LEVEL), MP_ROM_INT(PYB_PIN_LOW_LEVEL) },
  834. { MP_ROM_QSTR(MP_QSTR_IRQ_HIGH_LEVEL), MP_ROM_INT(PYB_PIN_HIGH_LEVEL) },
  835. };
  836. STATIC MP_DEFINE_CONST_DICT(pin_locals_dict, pin_locals_dict_table);
  837. const mp_obj_type_t pin_type = {
  838. { &mp_type_type },
  839. .name = MP_QSTR_Pin,
  840. .print = pin_print,
  841. .make_new = pin_make_new,
  842. .call = pin_call,
  843. .locals_dict = (mp_obj_t)&pin_locals_dict,
  844. };
  845. STATIC const mp_irq_methods_t pin_irq_methods = {
  846. .init = pin_irq,
  847. .enable = pin_irq_enable,
  848. .disable = pin_irq_disable,
  849. .flags = pin_irq_flags,
  850. };
  851. STATIC void pin_named_pins_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  852. pin_named_pins_obj_t *self = self_in;
  853. mp_printf(print, "<Pin.%q>", self->name);
  854. }
  855. const mp_obj_type_t pin_board_pins_obj_type = {
  856. { &mp_type_type },
  857. .name = MP_QSTR_board,
  858. .print = pin_named_pins_obj_print,
  859. .locals_dict = (mp_obj_t)&pin_board_pins_locals_dict,
  860. };