extint.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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 <stddef.h>
  28. #include <string.h>
  29. #include "py/runtime.h"
  30. #include "py/gc.h"
  31. #include "py/mphal.h"
  32. #include "pin.h"
  33. #include "extint.h"
  34. #include "irq.h"
  35. /// \moduleref pyb
  36. /// \class ExtInt - configure I/O pins to interrupt on external events
  37. ///
  38. /// There are a total of 22 interrupt lines. 16 of these can come from GPIO pins
  39. /// and the remaining 6 are from internal sources.
  40. ///
  41. /// For lines 0 thru 15, a given line can map to the corresponding line from an
  42. /// arbitrary port. So line 0 can map to Px0 where x is A, B, C, ... and
  43. /// line 1 can map to Px1 where x is A, B, C, ...
  44. ///
  45. /// def callback(line):
  46. /// print("line =", line)
  47. ///
  48. /// Note: ExtInt will automatically configure the gpio line as an input.
  49. ///
  50. /// extint = pyb.ExtInt(pin, pyb.ExtInt.IRQ_FALLING, pyb.Pin.PULL_UP, callback)
  51. ///
  52. /// Now every time a falling edge is seen on the X1 pin, the callback will be
  53. /// called. Caution: mechanical pushbuttons have "bounce" and pushing or
  54. /// releasing a switch will often generate multiple edges.
  55. /// See: http://www.eng.utah.edu/~cs5780/debouncing.pdf for a detailed
  56. /// explanation, along with various techniques for debouncing.
  57. ///
  58. /// Trying to register 2 callbacks onto the same pin will throw an exception.
  59. ///
  60. /// If pin is passed as an integer, then it is assumed to map to one of the
  61. /// internal interrupt sources, and must be in the range 16 thru 22.
  62. ///
  63. /// All other pin objects go through the pin mapper to come up with one of the
  64. /// gpio pins.
  65. ///
  66. /// extint = pyb.ExtInt(pin, mode, pull, callback)
  67. ///
  68. /// Valid modes are pyb.ExtInt.IRQ_RISING, pyb.ExtInt.IRQ_FALLING,
  69. /// pyb.ExtInt.IRQ_RISING_FALLING, pyb.ExtInt.EVT_RISING,
  70. /// pyb.ExtInt.EVT_FALLING, and pyb.ExtInt.EVT_RISING_FALLING.
  71. ///
  72. /// Only the IRQ_xxx modes have been tested. The EVT_xxx modes have
  73. /// something to do with sleep mode and the WFE instruction.
  74. ///
  75. /// Valid pull values are pyb.Pin.PULL_UP, pyb.Pin.PULL_DOWN, pyb.Pin.PULL_NONE.
  76. ///
  77. /// There is also a C API, so that drivers which require EXTI interrupt lines
  78. /// can also use this code. See extint.h for the available functions and
  79. /// usrsw.h for an example of using this.
  80. // TODO Add python method to change callback object.
  81. #define EXTI_OFFSET (EXTI_BASE - PERIPH_BASE)
  82. // Macro used to set/clear the bit corresponding to the line in the IMR/EMR
  83. // register in an atomic fashion by using bitband addressing.
  84. #define EXTI_MODE_BB(mode, line) (*(__IO uint32_t *)(PERIPH_BB_BASE + ((EXTI_OFFSET + (mode)) * 32) + ((line) * 4)))
  85. #if defined(STM32L4)
  86. // The L4 MCU supports 40 Events/IRQs lines of the type configurable and direct.
  87. // Here we only support configurable line types. Details, see page 330 of RM0351, Rev 1.
  88. // The USB_FS_WAKUP event is a direct type and there is no support for it.
  89. #define EXTI_Mode_Interrupt offsetof(EXTI_TypeDef, IMR1)
  90. #define EXTI_Mode_Event offsetof(EXTI_TypeDef, EMR1)
  91. #define EXTI_RTSR EXTI->RTSR1
  92. #define EXTI_FTSR EXTI->FTSR1
  93. #elif defined(STM32H7)
  94. #define EXTI_Mode_Interrupt offsetof(EXTI_Core_TypeDef, IMR1)
  95. #define EXTI_Mode_Event offsetof(EXTI_Core_TypeDef, EMR1)
  96. #define EXTI_RTSR EXTI->RTSR1
  97. #define EXTI_FTSR EXTI->FTSR1
  98. #else
  99. #define EXTI_Mode_Interrupt offsetof(EXTI_TypeDef, IMR)
  100. #define EXTI_Mode_Event offsetof(EXTI_TypeDef, EMR)
  101. #define EXTI_RTSR EXTI->RTSR
  102. #define EXTI_FTSR EXTI->FTSR
  103. #endif
  104. #define EXTI_SWIER_BB(line) (*(__IO uint32_t *)(PERIPH_BB_BASE + ((EXTI_OFFSET + offsetof(EXTI_TypeDef, SWIER)) * 32) + ((line) * 4)))
  105. typedef struct {
  106. mp_obj_base_t base;
  107. mp_int_t line;
  108. } extint_obj_t;
  109. STATIC uint8_t pyb_extint_mode[EXTI_NUM_VECTORS];
  110. STATIC bool pyb_extint_hard_irq[EXTI_NUM_VECTORS];
  111. // The callback arg is a small-int or a ROM Pin object, so no need to scan by GC
  112. STATIC mp_obj_t pyb_extint_callback_arg[EXTI_NUM_VECTORS];
  113. #if !defined(ETH)
  114. #define ETH_WKUP_IRQn 62 // Some MCUs don't have ETH, but we want a value to put in our table
  115. #endif
  116. #if !defined(OTG_HS_WKUP_IRQn)
  117. #define OTG_HS_WKUP_IRQn 76 // Some MCUs don't have HS, but we want a value to put in our table
  118. #endif
  119. #if !defined(OTG_FS_WKUP_IRQn)
  120. #define OTG_FS_WKUP_IRQn 42 // Some MCUs don't have FS IRQ, but we want a value to put in our table
  121. #endif
  122. STATIC const uint8_t nvic_irq_channel[EXTI_NUM_VECTORS] = {
  123. #if defined(STM32F0)
  124. EXTI0_1_IRQn, EXTI0_1_IRQn, EXTI2_3_IRQn, EXTI2_3_IRQn,
  125. EXTI4_15_IRQn, EXTI4_15_IRQn, EXTI4_15_IRQn, EXTI4_15_IRQn,
  126. EXTI4_15_IRQn, EXTI4_15_IRQn, EXTI4_15_IRQn, EXTI4_15_IRQn,
  127. EXTI4_15_IRQn, EXTI4_15_IRQn, EXTI4_15_IRQn, EXTI4_15_IRQn,
  128. #else
  129. EXTI0_IRQn, EXTI1_IRQn, EXTI2_IRQn, EXTI3_IRQn, EXTI4_IRQn,
  130. EXTI9_5_IRQn, EXTI9_5_IRQn, EXTI9_5_IRQn, EXTI9_5_IRQn, EXTI9_5_IRQn,
  131. EXTI15_10_IRQn, EXTI15_10_IRQn, EXTI15_10_IRQn, EXTI15_10_IRQn, EXTI15_10_IRQn,
  132. EXTI15_10_IRQn,
  133. #if defined(STM32L4)
  134. PVD_PVM_IRQn,
  135. #else
  136. PVD_IRQn,
  137. #endif
  138. RTC_Alarm_IRQn,
  139. OTG_FS_WKUP_IRQn,
  140. ETH_WKUP_IRQn,
  141. OTG_HS_WKUP_IRQn,
  142. TAMP_STAMP_IRQn,
  143. RTC_WKUP_IRQn,
  144. #endif
  145. };
  146. // Set override_callback_obj to true if you want to unconditionally set the
  147. // callback function.
  148. uint extint_register(mp_obj_t pin_obj, uint32_t mode, uint32_t pull, mp_obj_t callback_obj, bool override_callback_obj) {
  149. const pin_obj_t *pin = NULL;
  150. uint v_line;
  151. if (MP_OBJ_IS_INT(pin_obj)) {
  152. // If an integer is passed in, then use it to identify lines 16 thru 22
  153. // We expect lines 0 thru 15 to be passed in as a pin, so that we can
  154. // get both the port number and line number.
  155. v_line = mp_obj_get_int(pin_obj);
  156. if (v_line < 16) {
  157. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "ExtInt vector %d < 16, use a Pin object", v_line));
  158. }
  159. if (v_line >= EXTI_NUM_VECTORS) {
  160. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "ExtInt vector %d >= max of %d", v_line, EXTI_NUM_VECTORS));
  161. }
  162. } else {
  163. pin = pin_find(pin_obj);
  164. v_line = pin->pin;
  165. }
  166. if (mode != GPIO_MODE_IT_RISING &&
  167. mode != GPIO_MODE_IT_FALLING &&
  168. mode != GPIO_MODE_IT_RISING_FALLING &&
  169. mode != GPIO_MODE_EVT_RISING &&
  170. mode != GPIO_MODE_EVT_FALLING &&
  171. mode != GPIO_MODE_EVT_RISING_FALLING) {
  172. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid ExtInt Mode: %d", mode));
  173. }
  174. if (pull != GPIO_NOPULL &&
  175. pull != GPIO_PULLUP &&
  176. pull != GPIO_PULLDOWN) {
  177. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid ExtInt Pull: %d", pull));
  178. }
  179. mp_obj_t *cb = &MP_STATE_PORT(pyb_extint_callback)[v_line];
  180. if (!override_callback_obj && *cb != mp_const_none && callback_obj != mp_const_none) {
  181. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "ExtInt vector %d is already in use", v_line));
  182. }
  183. // We need to update callback atomically, so we disable the line
  184. // before we update anything.
  185. extint_disable(v_line);
  186. *cb = callback_obj;
  187. pyb_extint_mode[v_line] = (mode & 0x00010000) ? // GPIO_MODE_IT == 0x00010000
  188. EXTI_Mode_Interrupt : EXTI_Mode_Event;
  189. if (*cb != mp_const_none) {
  190. pyb_extint_hard_irq[v_line] = true;
  191. pyb_extint_callback_arg[v_line] = MP_OBJ_NEW_SMALL_INT(v_line);
  192. mp_hal_gpio_clock_enable(pin->gpio);
  193. GPIO_InitTypeDef exti;
  194. exti.Pin = pin->pin_mask;
  195. exti.Mode = mode;
  196. exti.Pull = pull;
  197. exti.Speed = GPIO_SPEED_FREQ_HIGH;
  198. HAL_GPIO_Init(pin->gpio, &exti);
  199. // Calling HAL_GPIO_Init does an implicit extint_enable
  200. /* Enable and set NVIC Interrupt to the lowest priority */
  201. NVIC_SetPriority(IRQn_NONNEG(nvic_irq_channel[v_line]), IRQ_PRI_EXTINT);
  202. HAL_NVIC_EnableIRQ(nvic_irq_channel[v_line]);
  203. }
  204. return v_line;
  205. }
  206. // This function is intended to be used by the Pin.irq() method
  207. void extint_register_pin(const pin_obj_t *pin, uint32_t mode, bool hard_irq, mp_obj_t callback_obj) {
  208. uint32_t line = pin->pin;
  209. // Check if the ExtInt line is already in use by another Pin/ExtInt
  210. mp_obj_t *cb = &MP_STATE_PORT(pyb_extint_callback)[line];
  211. if (*cb != mp_const_none && MP_OBJ_FROM_PTR(pin) != pyb_extint_callback_arg[line]) {
  212. if (MP_OBJ_IS_SMALL_INT(pyb_extint_callback_arg[line])) {
  213. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
  214. "ExtInt vector %d is already in use", line));
  215. } else {
  216. const pin_obj_t *other_pin = MP_OBJ_TO_PTR(pyb_extint_callback_arg[line]);
  217. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
  218. "IRQ resource already taken by Pin('%q')", other_pin->name));
  219. }
  220. }
  221. extint_disable(line);
  222. *cb = callback_obj;
  223. pyb_extint_mode[line] = (mode & 0x00010000) ? // GPIO_MODE_IT == 0x00010000
  224. EXTI_Mode_Interrupt : EXTI_Mode_Event;
  225. if (*cb != mp_const_none) {
  226. // Configure and enable the callback
  227. pyb_extint_hard_irq[line] = hard_irq;
  228. pyb_extint_callback_arg[line] = MP_OBJ_FROM_PTR(pin);
  229. // Route the GPIO to EXTI
  230. __HAL_RCC_SYSCFG_CLK_ENABLE();
  231. SYSCFG->EXTICR[line >> 2] =
  232. (SYSCFG->EXTICR[line >> 2] & ~(0x0f << (4 * (line & 0x03))))
  233. | ((uint32_t)(GPIO_GET_INDEX(pin->gpio)) << (4 * (line & 0x03)));
  234. // Enable or disable the rising detector
  235. if ((mode & GPIO_MODE_IT_RISING) == GPIO_MODE_IT_RISING) {
  236. EXTI_RTSR |= 1 << line;
  237. } else {
  238. EXTI_RTSR &= ~(1 << line);
  239. }
  240. // Enable or disable the falling detector
  241. if ((mode & GPIO_MODE_IT_FALLING) == GPIO_MODE_IT_FALLING) {
  242. EXTI_FTSR |= 1 << line;
  243. } else {
  244. EXTI_FTSR &= ~(1 << line);
  245. }
  246. // Configure the NVIC
  247. NVIC_SetPriority(IRQn_NONNEG(nvic_irq_channel[line]), IRQ_PRI_EXTINT);
  248. HAL_NVIC_EnableIRQ(nvic_irq_channel[line]);
  249. // Enable the interrupt
  250. extint_enable(line);
  251. }
  252. }
  253. void extint_enable(uint line) {
  254. if (line >= EXTI_NUM_VECTORS) {
  255. return;
  256. }
  257. #if defined(STM32F0) || defined(STM32F7) || defined(STM32H7)
  258. // The Cortex-M7 doesn't have bitband support.
  259. mp_uint_t irq_state = disable_irq();
  260. if (pyb_extint_mode[line] == EXTI_Mode_Interrupt) {
  261. #if defined(STM32H7)
  262. EXTI_D1->IMR1 |= (1 << line);
  263. #else
  264. EXTI->IMR |= (1 << line);
  265. #endif
  266. } else {
  267. #if defined(STM32H7)
  268. EXTI_D1->EMR1 |= (1 << line);
  269. #else
  270. EXTI->EMR |= (1 << line);
  271. #endif
  272. }
  273. enable_irq(irq_state);
  274. #else
  275. // Since manipulating IMR/EMR is a read-modify-write, and we want this to
  276. // be atomic, we use the bit-band area to just affect the bit we're
  277. // interested in.
  278. EXTI_MODE_BB(pyb_extint_mode[line], line) = 1;
  279. #endif
  280. }
  281. void extint_disable(uint line) {
  282. if (line >= EXTI_NUM_VECTORS) {
  283. return;
  284. }
  285. #if defined(STM32F0) || defined(STM32F7) || defined(STM32H7)
  286. // The Cortex-M7 doesn't have bitband support.
  287. mp_uint_t irq_state = disable_irq();
  288. #if defined(STM32H7)
  289. EXTI_D1->IMR1 &= ~(1 << line);
  290. EXTI_D1->EMR1 &= ~(1 << line);
  291. #else
  292. EXTI->IMR &= ~(1 << line);
  293. EXTI->EMR &= ~(1 << line);
  294. #endif
  295. enable_irq(irq_state);
  296. #else
  297. // Since manipulating IMR/EMR is a read-modify-write, and we want this to
  298. // be atomic, we use the bit-band area to just affect the bit we're
  299. // interested in.
  300. EXTI_MODE_BB(EXTI_Mode_Interrupt, line) = 0;
  301. EXTI_MODE_BB(EXTI_Mode_Event, line) = 0;
  302. #endif
  303. }
  304. void extint_swint(uint line) {
  305. if (line >= EXTI_NUM_VECTORS) {
  306. return;
  307. }
  308. // we need 0 to 1 transition to trigger the interrupt
  309. #if defined(STM32L4) || defined(STM32H7)
  310. EXTI->SWIER1 &= ~(1 << line);
  311. EXTI->SWIER1 |= (1 << line);
  312. #else
  313. EXTI->SWIER &= ~(1 << line);
  314. EXTI->SWIER |= (1 << line);
  315. #endif
  316. }
  317. /// \method line()
  318. /// Return the line number that the pin is mapped to.
  319. STATIC mp_obj_t extint_obj_line(mp_obj_t self_in) {
  320. extint_obj_t *self = MP_OBJ_TO_PTR(self_in);
  321. return MP_OBJ_NEW_SMALL_INT(self->line);
  322. }
  323. STATIC MP_DEFINE_CONST_FUN_OBJ_1(extint_obj_line_obj, extint_obj_line);
  324. /// \method enable()
  325. /// Enable a disabled interrupt.
  326. STATIC mp_obj_t extint_obj_enable(mp_obj_t self_in) {
  327. extint_obj_t *self = MP_OBJ_TO_PTR(self_in);
  328. extint_enable(self->line);
  329. return mp_const_none;
  330. }
  331. STATIC MP_DEFINE_CONST_FUN_OBJ_1(extint_obj_enable_obj, extint_obj_enable);
  332. /// \method disable()
  333. /// Disable the interrupt associated with the ExtInt object.
  334. /// This could be useful for debouncing.
  335. STATIC mp_obj_t extint_obj_disable(mp_obj_t self_in) {
  336. extint_obj_t *self = MP_OBJ_TO_PTR(self_in);
  337. extint_disable(self->line);
  338. return mp_const_none;
  339. }
  340. STATIC MP_DEFINE_CONST_FUN_OBJ_1(extint_obj_disable_obj, extint_obj_disable);
  341. /// \method swint()
  342. /// Trigger the callback from software.
  343. STATIC mp_obj_t extint_obj_swint(mp_obj_t self_in) {
  344. extint_obj_t *self = MP_OBJ_TO_PTR(self_in);
  345. extint_swint(self->line);
  346. return mp_const_none;
  347. }
  348. STATIC MP_DEFINE_CONST_FUN_OBJ_1(extint_obj_swint_obj, extint_obj_swint);
  349. // TODO document as a staticmethod
  350. /// \classmethod regs()
  351. /// Dump the values of the EXTI registers.
  352. STATIC mp_obj_t extint_regs(void) {
  353. #if defined(STM32L4)
  354. printf("EXTI_IMR1 %08x\n", (unsigned int)EXTI->IMR1);
  355. printf("EXTI_IMR2 %08x\n", (unsigned int)EXTI->IMR2);
  356. printf("EXTI_EMR1 %08x\n", (unsigned int)EXTI->EMR1);
  357. printf("EXTI_EMR2 %08x\n", (unsigned int)EXTI->EMR2);
  358. printf("EXTI_RTSR1 %08x\n", (unsigned int)EXTI->RTSR1);
  359. printf("EXTI_RTSR2 %08x\n", (unsigned int)EXTI->RTSR2);
  360. printf("EXTI_FTSR1 %08x\n", (unsigned int)EXTI->FTSR1);
  361. printf("EXTI_FTSR2 %08x\n", (unsigned int)EXTI->FTSR2);
  362. printf("EXTI_SWIER1 %08x\n", (unsigned int)EXTI->SWIER1);
  363. printf("EXTI_SWIER2 %08x\n", (unsigned int)EXTI->SWIER2);
  364. printf("EXTI_PR1 %08x\n", (unsigned int)EXTI->PR1);
  365. printf("EXTI_PR2 %08x\n", (unsigned int)EXTI->PR2);
  366. #elif defined(STM32H7)
  367. printf("EXTI_IMR1 %08x\n", (unsigned int)EXTI_D1->IMR1);
  368. printf("EXTI_IMR2 %08x\n", (unsigned int)EXTI_D1->IMR2);
  369. printf("EXTI_IMR3 %08x\n", (unsigned int)EXTI_D1->IMR3);
  370. printf("EXTI_EMR1 %08x\n", (unsigned int)EXTI_D1->EMR1);
  371. printf("EXTI_EMR2 %08x\n", (unsigned int)EXTI_D1->EMR2);
  372. printf("EXTI_EMR3 %08x\n", (unsigned int)EXTI_D1->EMR3);
  373. printf("EXTI_RTSR1 %08x\n", (unsigned int)EXTI->RTSR1);
  374. printf("EXTI_RTSR2 %08x\n", (unsigned int)EXTI->RTSR2);
  375. printf("EXTI_RTSR3 %08x\n", (unsigned int)EXTI->RTSR3);
  376. printf("EXTI_FTSR1 %08x\n", (unsigned int)EXTI->FTSR1);
  377. printf("EXTI_FTSR2 %08x\n", (unsigned int)EXTI->FTSR2);
  378. printf("EXTI_FTSR3 %08x\n", (unsigned int)EXTI->FTSR3);
  379. printf("EXTI_SWIER1 %08x\n", (unsigned int)EXTI->SWIER1);
  380. printf("EXTI_SWIER2 %08x\n", (unsigned int)EXTI->SWIER2);
  381. printf("EXTI_SWIER3 %08x\n", (unsigned int)EXTI->SWIER3);
  382. printf("EXTI_PR1 %08x\n", (unsigned int)EXTI_D1->PR1);
  383. printf("EXTI_PR2 %08x\n", (unsigned int)EXTI_D1->PR2);
  384. printf("EXTI_PR3 %08x\n", (unsigned int)EXTI_D1->PR3);
  385. #else
  386. printf("EXTI_IMR %08x\n", (unsigned int)EXTI->IMR);
  387. printf("EXTI_EMR %08x\n", (unsigned int)EXTI->EMR);
  388. printf("EXTI_RTSR %08x\n", (unsigned int)EXTI->RTSR);
  389. printf("EXTI_FTSR %08x\n", (unsigned int)EXTI->FTSR);
  390. printf("EXTI_SWIER %08x\n", (unsigned int)EXTI->SWIER);
  391. printf("EXTI_PR %08x\n", (unsigned int)EXTI->PR);
  392. #endif
  393. return mp_const_none;
  394. }
  395. STATIC MP_DEFINE_CONST_FUN_OBJ_0(extint_regs_fun_obj, extint_regs);
  396. STATIC MP_DEFINE_CONST_STATICMETHOD_OBJ(extint_regs_obj, MP_ROM_PTR(&extint_regs_fun_obj));
  397. /// \classmethod \constructor(pin, mode, pull, callback)
  398. /// Create an ExtInt object:
  399. ///
  400. /// - `pin` is the pin on which to enable the interrupt (can be a pin object or any valid pin name).
  401. /// - `mode` can be one of:
  402. /// - `ExtInt.IRQ_RISING` - trigger on a rising edge;
  403. /// - `ExtInt.IRQ_FALLING` - trigger on a falling edge;
  404. /// - `ExtInt.IRQ_RISING_FALLING` - trigger on a rising or falling edge.
  405. /// - `pull` can be one of:
  406. /// - `pyb.Pin.PULL_NONE` - no pull up or down resistors;
  407. /// - `pyb.Pin.PULL_UP` - enable the pull-up resistor;
  408. /// - `pyb.Pin.PULL_DOWN` - enable the pull-down resistor.
  409. /// - `callback` is the function to call when the interrupt triggers. The
  410. /// callback function must accept exactly 1 argument, which is the line that
  411. /// triggered the interrupt.
  412. STATIC const mp_arg_t pyb_extint_make_new_args[] = {
  413. { MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
  414. { MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} },
  415. { MP_QSTR_pull, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} },
  416. { MP_QSTR_callback, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
  417. };
  418. #define PYB_EXTINT_MAKE_NEW_NUM_ARGS MP_ARRAY_SIZE(pyb_extint_make_new_args)
  419. STATIC mp_obj_t extint_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  420. // type_in == extint_obj_type
  421. // parse args
  422. mp_arg_val_t vals[PYB_EXTINT_MAKE_NEW_NUM_ARGS];
  423. mp_arg_parse_all_kw_array(n_args, n_kw, args, PYB_EXTINT_MAKE_NEW_NUM_ARGS, pyb_extint_make_new_args, vals);
  424. extint_obj_t *self = m_new_obj(extint_obj_t);
  425. self->base.type = type;
  426. self->line = extint_register(vals[0].u_obj, vals[1].u_int, vals[2].u_int, vals[3].u_obj, false);
  427. return MP_OBJ_FROM_PTR(self);
  428. }
  429. STATIC void extint_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  430. extint_obj_t *self = MP_OBJ_TO_PTR(self_in);
  431. mp_printf(print, "<ExtInt line=%u>", self->line);
  432. }
  433. STATIC const mp_rom_map_elem_t extint_locals_dict_table[] = {
  434. { MP_ROM_QSTR(MP_QSTR_line), MP_ROM_PTR(&extint_obj_line_obj) },
  435. { MP_ROM_QSTR(MP_QSTR_enable), MP_ROM_PTR(&extint_obj_enable_obj) },
  436. { MP_ROM_QSTR(MP_QSTR_disable), MP_ROM_PTR(&extint_obj_disable_obj) },
  437. { MP_ROM_QSTR(MP_QSTR_swint), MP_ROM_PTR(&extint_obj_swint_obj) },
  438. { MP_ROM_QSTR(MP_QSTR_regs), MP_ROM_PTR(&extint_regs_obj) },
  439. // class constants
  440. /// \constant IRQ_RISING - interrupt on a rising edge
  441. /// \constant IRQ_FALLING - interrupt on a falling edge
  442. /// \constant IRQ_RISING_FALLING - interrupt on a rising or falling edge
  443. { MP_ROM_QSTR(MP_QSTR_IRQ_RISING), MP_ROM_INT(GPIO_MODE_IT_RISING) },
  444. { MP_ROM_QSTR(MP_QSTR_IRQ_FALLING), MP_ROM_INT(GPIO_MODE_IT_FALLING) },
  445. { MP_ROM_QSTR(MP_QSTR_IRQ_RISING_FALLING), MP_ROM_INT(GPIO_MODE_IT_RISING_FALLING) },
  446. { MP_ROM_QSTR(MP_QSTR_EVT_RISING), MP_ROM_INT(GPIO_MODE_EVT_RISING) },
  447. { MP_ROM_QSTR(MP_QSTR_EVT_FALLING), MP_ROM_INT(GPIO_MODE_EVT_FALLING) },
  448. { MP_ROM_QSTR(MP_QSTR_EVT_RISING_FALLING), MP_ROM_INT(GPIO_MODE_EVT_RISING_FALLING) },
  449. };
  450. STATIC MP_DEFINE_CONST_DICT(extint_locals_dict, extint_locals_dict_table);
  451. const mp_obj_type_t extint_type = {
  452. { &mp_type_type },
  453. .name = MP_QSTR_ExtInt,
  454. .print = extint_obj_print,
  455. .make_new = extint_make_new,
  456. .locals_dict = (mp_obj_dict_t*)&extint_locals_dict,
  457. };
  458. void extint_init0(void) {
  459. for (int i = 0; i < PYB_EXTI_NUM_VECTORS; i++) {
  460. MP_STATE_PORT(pyb_extint_callback)[i] = mp_const_none;
  461. pyb_extint_mode[i] = EXTI_Mode_Interrupt;
  462. }
  463. }
  464. // Interrupt handler
  465. void Handle_EXTI_Irq(uint32_t line) {
  466. if (__HAL_GPIO_EXTI_GET_FLAG(1 << line)) {
  467. __HAL_GPIO_EXTI_CLEAR_FLAG(1 << line);
  468. if (line < EXTI_NUM_VECTORS) {
  469. mp_obj_t *cb = &MP_STATE_PORT(pyb_extint_callback)[line];
  470. if (*cb != mp_const_none) {
  471. // If it's a soft IRQ handler then just schedule callback for later
  472. if (!pyb_extint_hard_irq[line]) {
  473. mp_sched_schedule(*cb, pyb_extint_callback_arg[line]);
  474. return;
  475. }
  476. mp_sched_lock();
  477. // When executing code within a handler we must lock the GC to prevent
  478. // any memory allocations. We must also catch any exceptions.
  479. gc_lock();
  480. nlr_buf_t nlr;
  481. if (nlr_push(&nlr) == 0) {
  482. mp_call_function_1(*cb, pyb_extint_callback_arg[line]);
  483. nlr_pop();
  484. } else {
  485. // Uncaught exception; disable the callback so it doesn't run again.
  486. *cb = mp_const_none;
  487. extint_disable(line);
  488. printf("Uncaught exception in ExtInt interrupt handler line %u\n", (unsigned int)line);
  489. mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
  490. }
  491. gc_unlock();
  492. mp_sched_unlock();
  493. }
  494. }
  495. }
  496. }