stm32_it.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * Original template from ST Cube library. See below for header.
  5. *
  6. * The MIT License (MIT)
  7. *
  8. * Copyright (c) 2013, 2014 Damien P. George
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this software and associated documentation files (the "Software"), to deal
  12. * in the Software without restriction, including without limitation the rights
  13. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in
  18. * all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. * THE SOFTWARE.
  27. */
  28. /**
  29. ******************************************************************************
  30. * @file Templates/Src/stm32f4xx_it.c
  31. * @author MCD Application Team
  32. * @version V1.0.1
  33. * @date 26-February-2014
  34. * @brief Main Interrupt Service Routines.
  35. * This file provides template for all exceptions handler and
  36. * peripherals interrupt service routine.
  37. ******************************************************************************
  38. * @attention
  39. *
  40. * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
  41. *
  42. * Redistribution and use in source and binary forms, with or without modification,
  43. * are permitted provided that the following conditions are met:
  44. * 1. Redistributions of source code must retain the above copyright notice,
  45. * this list of conditions and the following disclaimer.
  46. * 2. Redistributions in binary form must reproduce the above copyright notice,
  47. * this list of conditions and the following disclaimer in the documentation
  48. * and/or other materials provided with the distribution.
  49. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  50. * may be used to endorse or promote products derived from this software
  51. * without specific prior written permission.
  52. *
  53. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  54. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  55. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  56. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  57. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  58. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  59. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  60. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  61. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  62. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  63. *
  64. ******************************************************************************
  65. */
  66. #include <stdio.h>
  67. #include "py/obj.h"
  68. #include "py/mphal.h"
  69. #include "stm32_it.h"
  70. #include "pendsv.h"
  71. #include "irq.h"
  72. #include "pybthread.h"
  73. #include "gccollect.h"
  74. #include "extint.h"
  75. #include "timer.h"
  76. #include "uart.h"
  77. #include "storage.h"
  78. #include "can.h"
  79. #include "dma.h"
  80. #include "i2c.h"
  81. #include "usb.h"
  82. extern void __fatal_error(const char*);
  83. #if defined(MICROPY_HW_USB_FS)
  84. extern PCD_HandleTypeDef pcd_fs_handle;
  85. #endif
  86. #if defined(MICROPY_HW_USB_HS)
  87. extern PCD_HandleTypeDef pcd_hs_handle;
  88. #endif
  89. /******************************************************************************/
  90. /* Cortex-M4 Processor Exceptions Handlers */
  91. /******************************************************************************/
  92. // Set the following to 1 to get some more information on the Hard Fault
  93. // More information about decoding the fault registers can be found here:
  94. // http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0646a/Cihdjcfc.html
  95. STATIC char *fmt_hex(uint32_t val, char *buf) {
  96. const char *hexDig = "0123456789abcdef";
  97. buf[0] = hexDig[(val >> 28) & 0x0f];
  98. buf[1] = hexDig[(val >> 24) & 0x0f];
  99. buf[2] = hexDig[(val >> 20) & 0x0f];
  100. buf[3] = hexDig[(val >> 16) & 0x0f];
  101. buf[4] = hexDig[(val >> 12) & 0x0f];
  102. buf[5] = hexDig[(val >> 8) & 0x0f];
  103. buf[6] = hexDig[(val >> 4) & 0x0f];
  104. buf[7] = hexDig[(val >> 0) & 0x0f];
  105. buf[8] = '\0';
  106. return buf;
  107. }
  108. STATIC void print_reg(const char *label, uint32_t val) {
  109. char hexStr[9];
  110. mp_hal_stdout_tx_str(label);
  111. mp_hal_stdout_tx_str(fmt_hex(val, hexStr));
  112. mp_hal_stdout_tx_str("\r\n");
  113. }
  114. STATIC void print_hex_hex(const char *label, uint32_t val1, uint32_t val2) {
  115. char hex_str[9];
  116. mp_hal_stdout_tx_str(label);
  117. mp_hal_stdout_tx_str(fmt_hex(val1, hex_str));
  118. mp_hal_stdout_tx_str(" ");
  119. mp_hal_stdout_tx_str(fmt_hex(val2, hex_str));
  120. mp_hal_stdout_tx_str("\r\n");
  121. }
  122. // The ARMv7M Architecture manual (section B.1.5.6) says that upon entry
  123. // to an exception, that the registers will be in the following order on the
  124. // // stack: R0, R1, R2, R3, R12, LR, PC, XPSR
  125. typedef struct {
  126. uint32_t r0, r1, r2, r3, r12, lr, pc, xpsr;
  127. } ExceptionRegisters_t;
  128. int pyb_hard_fault_debug = 0;
  129. void HardFault_C_Handler(ExceptionRegisters_t *regs) {
  130. if (!pyb_hard_fault_debug) {
  131. NVIC_SystemReset();
  132. }
  133. #if MICROPY_HW_ENABLE_USB
  134. // We need to disable the USB so it doesn't try to write data out on
  135. // the VCP and then block indefinitely waiting for the buffer to drain.
  136. pyb_usb_flags = 0;
  137. #endif
  138. mp_hal_stdout_tx_str("HardFault\r\n");
  139. print_reg("R0 ", regs->r0);
  140. print_reg("R1 ", regs->r1);
  141. print_reg("R2 ", regs->r2);
  142. print_reg("R3 ", regs->r3);
  143. print_reg("R12 ", regs->r12);
  144. print_reg("SP ", (uint32_t)regs);
  145. print_reg("LR ", regs->lr);
  146. print_reg("PC ", regs->pc);
  147. print_reg("XPSR ", regs->xpsr);
  148. #if __CORTEX_M >= 3
  149. uint32_t cfsr = SCB->CFSR;
  150. print_reg("HFSR ", SCB->HFSR);
  151. print_reg("CFSR ", cfsr);
  152. if (cfsr & 0x80) {
  153. print_reg("MMFAR ", SCB->MMFAR);
  154. }
  155. if (cfsr & 0x8000) {
  156. print_reg("BFAR ", SCB->BFAR);
  157. }
  158. #endif
  159. if ((void*)&_ram_start <= (void*)regs && (void*)regs < (void*)&_ram_end) {
  160. mp_hal_stdout_tx_str("Stack:\r\n");
  161. uint32_t *stack_top = &_estack;
  162. if ((void*)regs < (void*)&_heap_end) {
  163. // stack not in static stack area so limit the amount we print
  164. stack_top = (uint32_t*)regs + 32;
  165. }
  166. for (uint32_t *sp = (uint32_t*)regs; sp < stack_top; ++sp) {
  167. print_hex_hex(" ", (uint32_t)sp, *sp);
  168. }
  169. }
  170. /* Go to infinite loop when Hard Fault exception occurs */
  171. while (1) {
  172. __fatal_error("HardFault");
  173. }
  174. }
  175. // Naked functions have no compiler generated gunk, so are the best thing to
  176. // use for asm functions.
  177. __attribute__((naked))
  178. void HardFault_Handler(void) {
  179. // From the ARMv7M Architecture Reference Manual, section B.1.5.6
  180. // on entry to the Exception, the LR register contains, amongst other
  181. // things, the value of CONTROL.SPSEL. This can be found in bit 3.
  182. //
  183. // If CONTROL.SPSEL is 0, then the exception was stacked up using the
  184. // main stack pointer (aka MSP). If CONTROL.SPSEL is 1, then the exception
  185. // was stacked up using the process stack pointer (aka PSP).
  186. #if __CORTEX_M == 0
  187. __asm volatile(
  188. " mov r0, lr \n"
  189. " lsr r0, r0, #3 \n" // Shift Bit 3 into carry to see which stack pointer we should use.
  190. " mrs r0, msp \n" // Make R0 point to main stack pointer
  191. " bcc .use_msp \n" // Keep MSP in R0 if SPSEL (carry) is 0
  192. " mrs r0, psp \n" // Make R0 point to process stack pointer
  193. " .use_msp: \n"
  194. " b HardFault_C_Handler \n" // Off to C land
  195. );
  196. #else
  197. __asm volatile(
  198. " tst lr, #4 \n" // Test Bit 3 to see which stack pointer we should use.
  199. " ite eq \n" // Tell the assembler that the nest 2 instructions are if-then-else
  200. " mrseq r0, msp \n" // Make R0 point to main stack pointer
  201. " mrsne r0, psp \n" // Make R0 point to process stack pointer
  202. " b HardFault_C_Handler \n" // Off to C land
  203. );
  204. #endif
  205. }
  206. /**
  207. * @brief This function handles NMI exception.
  208. * @param None
  209. * @retval None
  210. */
  211. void NMI_Handler(void) {
  212. }
  213. /**
  214. * @brief This function handles Memory Manage exception.
  215. * @param None
  216. * @retval None
  217. */
  218. void MemManage_Handler(void) {
  219. /* Go to infinite loop when Memory Manage exception occurs */
  220. while (1) {
  221. __fatal_error("MemManage");
  222. }
  223. }
  224. /**
  225. * @brief This function handles Bus Fault exception.
  226. * @param None
  227. * @retval None
  228. */
  229. void BusFault_Handler(void) {
  230. /* Go to infinite loop when Bus Fault exception occurs */
  231. while (1) {
  232. __fatal_error("BusFault");
  233. }
  234. }
  235. /**
  236. * @brief This function handles Usage Fault exception.
  237. * @param None
  238. * @retval None
  239. */
  240. void UsageFault_Handler(void) {
  241. /* Go to infinite loop when Usage Fault exception occurs */
  242. while (1) {
  243. __fatal_error("UsageFault");
  244. }
  245. }
  246. /**
  247. * @brief This function handles SVCall exception.
  248. * @param None
  249. * @retval None
  250. */
  251. void SVC_Handler(void) {
  252. }
  253. /**
  254. * @brief This function handles Debug Monitor exception.
  255. * @param None
  256. * @retval None
  257. */
  258. void DebugMon_Handler(void) {
  259. }
  260. /**
  261. * @brief This function handles PendSVC exception.
  262. * @param None
  263. * @retval None
  264. */
  265. void PendSV_Handler(void) {
  266. pendsv_isr_handler();
  267. }
  268. /**
  269. * @brief This function handles SysTick Handler.
  270. * @param None
  271. * @retval None
  272. */
  273. void SysTick_Handler(void) {
  274. // Instead of calling HAL_IncTick we do the increment here of the counter.
  275. // This is purely for efficiency, since SysTick is called 1000 times per
  276. // second at the highest interrupt priority.
  277. // Note: we don't need uwTick to be declared volatile here because this is
  278. // the only place where it can be modified, and the code is more efficient
  279. // without the volatile specifier.
  280. extern uint32_t uwTick;
  281. uwTick += 1;
  282. // Read the systick control regster. This has the side effect of clearing
  283. // the COUNTFLAG bit, which makes the logic in mp_hal_ticks_us
  284. // work properly.
  285. SysTick->CTRL;
  286. // Right now we have the storage and DMA controllers to process during
  287. // this interrupt and we use custom dispatch handlers. If this needs to
  288. // be generalised in the future then a dispatch table can be used as
  289. // follows: ((void(*)(void))(systick_dispatch[uwTick & 0xf]))();
  290. #if MICROPY_HW_ENABLE_STORAGE
  291. if (STORAGE_IDLE_TICK(uwTick)) {
  292. NVIC->STIR = FLASH_IRQn;
  293. }
  294. #endif
  295. if (DMA_IDLE_ENABLED() && DMA_IDLE_TICK(uwTick)) {
  296. dma_idle_handler(uwTick);
  297. }
  298. #if MICROPY_PY_THREAD
  299. if (pyb_thread_enabled) {
  300. if (pyb_thread_cur->timeslice == 0) {
  301. if (pyb_thread_cur->run_next != pyb_thread_cur) {
  302. SCB->ICSR = SCB_ICSR_PENDSVSET_Msk;
  303. }
  304. } else {
  305. --pyb_thread_cur->timeslice;
  306. }
  307. }
  308. #endif
  309. }
  310. /******************************************************************************/
  311. /* STM32F4xx Peripherals Interrupt Handlers */
  312. /* Add here the Interrupt Handler for the used peripheral(s) (PPP), for the */
  313. /* available peripheral interrupt handler's name please refer to the startup */
  314. /* file (startup_stm32f4xx.s). */
  315. /******************************************************************************/
  316. /**
  317. * @brief This function handles USB-On-The-Go FS global interrupt request.
  318. * @param None
  319. * @retval None
  320. */
  321. #if MICROPY_HW_USB_FS
  322. void OTG_FS_IRQHandler(void) {
  323. IRQ_ENTER(OTG_FS_IRQn);
  324. HAL_PCD_IRQHandler(&pcd_fs_handle);
  325. IRQ_EXIT(OTG_FS_IRQn);
  326. }
  327. #endif
  328. #if MICROPY_HW_USB_HS
  329. void OTG_HS_IRQHandler(void) {
  330. IRQ_ENTER(OTG_HS_IRQn);
  331. HAL_PCD_IRQHandler(&pcd_hs_handle);
  332. IRQ_EXIT(OTG_HS_IRQn);
  333. }
  334. #endif
  335. #if MICROPY_HW_USB_FS || MICROPY_HW_USB_HS
  336. /**
  337. * @brief This function handles USB OTG Common FS/HS Wakeup functions.
  338. * @param *pcd_handle for FS or HS
  339. * @retval None
  340. */
  341. STATIC void OTG_CMD_WKUP_Handler(PCD_HandleTypeDef *pcd_handle) {
  342. if (pcd_handle->Init.low_power_enable) {
  343. /* Reset SLEEPDEEP bit of Cortex System Control Register */
  344. SCB->SCR &= (uint32_t)~((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
  345. /* Configures system clock after wake-up from STOP: enable HSE, PLL and select
  346. PLL as system clock source (HSE and PLL are disabled in STOP mode) */
  347. __HAL_RCC_HSE_CONFIG(MICROPY_HW_CLK_HSE_STATE);
  348. /* Wait till HSE is ready */
  349. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
  350. {}
  351. /* Enable the main PLL. */
  352. __HAL_RCC_PLL_ENABLE();
  353. /* Wait till PLL is ready */
  354. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
  355. {}
  356. /* Select PLL as SYSCLK */
  357. MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_SYSCLKSOURCE_PLLCLK);
  358. #if defined(STM32H7)
  359. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL1)
  360. {}
  361. #else
  362. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL)
  363. {}
  364. #endif
  365. /* ungate PHY clock */
  366. __HAL_PCD_UNGATE_PHYCLOCK(pcd_handle);
  367. }
  368. }
  369. #endif
  370. #if MICROPY_HW_USB_FS
  371. /**
  372. * @brief This function handles USB OTG FS Wakeup IRQ Handler.
  373. * @param None
  374. * @retval None
  375. */
  376. void OTG_FS_WKUP_IRQHandler(void) {
  377. IRQ_ENTER(OTG_FS_WKUP_IRQn);
  378. OTG_CMD_WKUP_Handler(&pcd_fs_handle);
  379. /* Clear EXTI pending Bit*/
  380. __HAL_USB_FS_EXTI_CLEAR_FLAG();
  381. IRQ_EXIT(OTG_FS_WKUP_IRQn);
  382. }
  383. #endif
  384. #if MICROPY_HW_USB_HS
  385. /**
  386. * @brief This function handles USB OTG HS Wakeup IRQ Handler.
  387. * @param None
  388. * @retval None
  389. */
  390. void OTG_HS_WKUP_IRQHandler(void) {
  391. IRQ_ENTER(OTG_HS_WKUP_IRQn);
  392. OTG_CMD_WKUP_Handler(&pcd_hs_handle);
  393. /* Clear EXTI pending Bit*/
  394. __HAL_USB_HS_EXTI_CLEAR_FLAG();
  395. IRQ_EXIT(OTG_HS_WKUP_IRQn);
  396. }
  397. #endif
  398. /**
  399. * @brief This function handles PPP interrupt request.
  400. * @param None
  401. * @retval None
  402. */
  403. /*void PPP_IRQHandler(void)
  404. {
  405. }*/
  406. // Handle a flash (erase/program) interrupt.
  407. void FLASH_IRQHandler(void) {
  408. IRQ_ENTER(FLASH_IRQn);
  409. // This calls the real flash IRQ handler, if needed
  410. /*
  411. uint32_t flash_cr = FLASH->CR;
  412. if ((flash_cr & FLASH_IT_EOP) || (flash_cr & FLASH_IT_ERR)) {
  413. HAL_FLASH_IRQHandler();
  414. }
  415. */
  416. #if MICROPY_HW_ENABLE_STORAGE
  417. // This call the storage IRQ handler, to check if the flash cache needs flushing
  418. storage_irq_handler();
  419. #endif
  420. IRQ_EXIT(FLASH_IRQn);
  421. }
  422. /**
  423. * @brief These functions handle the EXTI interrupt requests.
  424. * @param None
  425. * @retval None
  426. */
  427. void EXTI0_IRQHandler(void) {
  428. IRQ_ENTER(EXTI0_IRQn);
  429. Handle_EXTI_Irq(0);
  430. IRQ_EXIT(EXTI0_IRQn);
  431. }
  432. void EXTI1_IRQHandler(void) {
  433. IRQ_ENTER(EXTI1_IRQn);
  434. Handle_EXTI_Irq(1);
  435. IRQ_EXIT(EXTI1_IRQn);
  436. }
  437. void EXTI2_IRQHandler(void) {
  438. IRQ_ENTER(EXTI2_IRQn);
  439. Handle_EXTI_Irq(2);
  440. IRQ_EXIT(EXTI2_IRQn);
  441. }
  442. void EXTI3_IRQHandler(void) {
  443. IRQ_ENTER(EXTI3_IRQn);
  444. Handle_EXTI_Irq(3);
  445. IRQ_EXIT(EXTI3_IRQn);
  446. }
  447. void EXTI4_IRQHandler(void) {
  448. IRQ_ENTER(EXTI4_IRQn);
  449. Handle_EXTI_Irq(4);
  450. IRQ_EXIT(EXTI4_IRQn);
  451. }
  452. void EXTI9_5_IRQHandler(void) {
  453. IRQ_ENTER(EXTI9_5_IRQn);
  454. Handle_EXTI_Irq(5);
  455. Handle_EXTI_Irq(6);
  456. Handle_EXTI_Irq(7);
  457. Handle_EXTI_Irq(8);
  458. Handle_EXTI_Irq(9);
  459. IRQ_EXIT(EXTI9_5_IRQn);
  460. }
  461. void EXTI15_10_IRQHandler(void) {
  462. IRQ_ENTER(EXTI15_10_IRQn);
  463. Handle_EXTI_Irq(10);
  464. Handle_EXTI_Irq(11);
  465. Handle_EXTI_Irq(12);
  466. Handle_EXTI_Irq(13);
  467. Handle_EXTI_Irq(14);
  468. Handle_EXTI_Irq(15);
  469. IRQ_EXIT(EXTI15_10_IRQn);
  470. }
  471. void PVD_IRQHandler(void) {
  472. IRQ_ENTER(PVD_IRQn);
  473. Handle_EXTI_Irq(EXTI_PVD_OUTPUT);
  474. IRQ_EXIT(PVD_IRQn);
  475. }
  476. #if defined(STM32L4)
  477. void PVD_PVM_IRQHandler(void) {
  478. IRQ_ENTER(PVD_PVM_IRQn);
  479. Handle_EXTI_Irq(EXTI_PVD_OUTPUT);
  480. IRQ_EXIT(PVD_PVM_IRQn);
  481. }
  482. #endif
  483. void RTC_Alarm_IRQHandler(void) {
  484. IRQ_ENTER(RTC_Alarm_IRQn);
  485. Handle_EXTI_Irq(EXTI_RTC_ALARM);
  486. IRQ_EXIT(RTC_Alarm_IRQn);
  487. }
  488. #if defined(ETH) // The 407 has ETH, the 405 doesn't
  489. void ETH_WKUP_IRQHandler(void) {
  490. IRQ_ENTER(ETH_WKUP_IRQn);
  491. Handle_EXTI_Irq(EXTI_ETH_WAKEUP);
  492. IRQ_EXIT(ETH_WKUP_IRQn);
  493. }
  494. #endif
  495. void TAMP_STAMP_IRQHandler(void) {
  496. IRQ_ENTER(TAMP_STAMP_IRQn);
  497. Handle_EXTI_Irq(EXTI_RTC_TIMESTAMP);
  498. IRQ_EXIT(TAMP_STAMP_IRQn);
  499. }
  500. void RTC_WKUP_IRQHandler(void) {
  501. IRQ_ENTER(RTC_WKUP_IRQn);
  502. RTC->ISR &= ~(1 << 10); // clear wakeup interrupt flag
  503. Handle_EXTI_Irq(EXTI_RTC_WAKEUP); // clear EXTI flag and execute optional callback
  504. IRQ_EXIT(RTC_WKUP_IRQn);
  505. }
  506. #if defined(STM32F0)
  507. void RTC_IRQHandler(void) {
  508. IRQ_ENTER(RTC_IRQn);
  509. RTC->ISR &= ~(1 << 10); // clear wakeup interrupt flag
  510. Handle_EXTI_Irq(EXTI_RTC_WAKEUP); // clear EXTI flag and execute optional callback
  511. IRQ_EXIT(RTC_IRQn);
  512. }
  513. void EXTI0_1_IRQHandler(void) {
  514. IRQ_ENTER(EXTI0_1_IRQn);
  515. Handle_EXTI_Irq(0);
  516. Handle_EXTI_Irq(1);
  517. IRQ_EXIT(EXTI0_1_IRQn);
  518. }
  519. void EXTI2_3_IRQHandler(void) {
  520. IRQ_ENTER(EXTI2_3_IRQn);
  521. Handle_EXTI_Irq(2);
  522. Handle_EXTI_Irq(3);
  523. IRQ_EXIT(EXTI2_3_IRQn);
  524. }
  525. void EXTI4_15_IRQHandler(void) {
  526. IRQ_ENTER(EXTI4_15_IRQn);
  527. for (int i = 4; i <= 15; ++i) {
  528. Handle_EXTI_Irq(i);
  529. }
  530. IRQ_EXIT(EXTI4_15_IRQn);
  531. }
  532. void TIM1_BRK_UP_TRG_COM_IRQHandler(void) {
  533. IRQ_ENTER(TIM1_BRK_UP_TRG_COM_IRQn);
  534. timer_irq_handler(1);
  535. IRQ_EXIT(TIM1_BRK_UP_TRG_COM_IRQn);
  536. }
  537. #endif
  538. void TIM1_BRK_TIM9_IRQHandler(void) {
  539. IRQ_ENTER(TIM1_BRK_TIM9_IRQn);
  540. timer_irq_handler(9);
  541. IRQ_EXIT(TIM1_BRK_TIM9_IRQn);
  542. }
  543. #if defined(STM32L4)
  544. void TIM1_BRK_TIM15_IRQHandler(void) {
  545. IRQ_ENTER(TIM1_BRK_TIM15_IRQn);
  546. timer_irq_handler(15);
  547. IRQ_EXIT(TIM1_BRK_TIM15_IRQn);
  548. }
  549. #endif
  550. void TIM1_UP_TIM10_IRQHandler(void) {
  551. IRQ_ENTER(TIM1_UP_TIM10_IRQn);
  552. timer_irq_handler(1);
  553. timer_irq_handler(10);
  554. IRQ_EXIT(TIM1_UP_TIM10_IRQn);
  555. }
  556. #if defined(STM32L4)
  557. void TIM1_UP_TIM16_IRQHandler(void) {
  558. IRQ_ENTER(TIM1_UP_TIM16_IRQn);
  559. timer_irq_handler(1);
  560. timer_irq_handler(16);
  561. IRQ_EXIT(TIM1_UP_TIM16_IRQn);
  562. }
  563. #endif
  564. void TIM1_TRG_COM_TIM11_IRQHandler(void) {
  565. IRQ_ENTER(TIM1_TRG_COM_TIM11_IRQn);
  566. timer_irq_handler(11);
  567. IRQ_EXIT(TIM1_TRG_COM_TIM11_IRQn);
  568. }
  569. #if defined(STM32L4)
  570. void TIM1_TRG_COM_TIM17_IRQHandler(void) {
  571. IRQ_ENTER(TIM1_TRG_COM_TIM17_IRQn);
  572. timer_irq_handler(17);
  573. IRQ_EXIT(TIM1_TRG_COM_TIM17_IRQn);
  574. }
  575. #endif
  576. void TIM1_CC_IRQHandler(void) {
  577. IRQ_ENTER(TIM1_CC_IRQn);
  578. timer_irq_handler(1);
  579. IRQ_EXIT(TIM1_CC_IRQn);
  580. }
  581. void TIM2_IRQHandler(void) {
  582. IRQ_ENTER(TIM2_IRQn);
  583. timer_irq_handler(2);
  584. IRQ_EXIT(TIM2_IRQn);
  585. }
  586. void TIM3_IRQHandler(void) {
  587. IRQ_ENTER(TIM3_IRQn);
  588. timer_irq_handler(3);
  589. IRQ_EXIT(TIM3_IRQn);
  590. }
  591. void TIM4_IRQHandler(void) {
  592. IRQ_ENTER(TIM4_IRQn);
  593. timer_irq_handler(4);
  594. IRQ_EXIT(TIM4_IRQn);
  595. }
  596. void TIM5_IRQHandler(void) {
  597. IRQ_ENTER(TIM5_IRQn);
  598. timer_irq_handler(5);
  599. HAL_TIM_IRQHandler(&TIM5_Handle);
  600. IRQ_EXIT(TIM5_IRQn);
  601. }
  602. #if defined(TIM6) // STM32F401 doesn't have TIM6
  603. void TIM6_DAC_IRQHandler(void) {
  604. IRQ_ENTER(TIM6_DAC_IRQn);
  605. timer_irq_handler(6);
  606. IRQ_EXIT(TIM6_DAC_IRQn);
  607. }
  608. #endif
  609. #if defined(TIM7) // STM32F401 doesn't have TIM7
  610. void TIM7_IRQHandler(void) {
  611. IRQ_ENTER(TIM7_IRQn);
  612. timer_irq_handler(7);
  613. IRQ_EXIT(TIM7_IRQn);
  614. }
  615. #endif
  616. #if defined(TIM8) // STM32F401 doesn't have TIM8
  617. void TIM8_BRK_TIM12_IRQHandler(void) {
  618. IRQ_ENTER(TIM8_BRK_TIM12_IRQn);
  619. timer_irq_handler(12);
  620. IRQ_EXIT(TIM8_BRK_TIM12_IRQn);
  621. }
  622. void TIM8_UP_TIM13_IRQHandler(void) {
  623. IRQ_ENTER(TIM8_UP_TIM13_IRQn);
  624. timer_irq_handler(8);
  625. timer_irq_handler(13);
  626. IRQ_EXIT(TIM8_UP_TIM13_IRQn);
  627. }
  628. #if defined(STM32L4)
  629. void TIM8_UP_IRQHandler(void) {
  630. IRQ_ENTER(TIM8_UP_IRQn);
  631. timer_irq_handler(8);
  632. IRQ_EXIT(TIM8_UP_IRQn);
  633. }
  634. #endif
  635. void TIM8_CC_IRQHandler(void) {
  636. IRQ_ENTER(TIM8_CC_IRQn);
  637. timer_irq_handler(8);
  638. IRQ_EXIT(TIM8_CC_IRQn);
  639. }
  640. void TIM8_TRG_COM_TIM14_IRQHandler(void) {
  641. IRQ_ENTER(TIM8_TRG_COM_TIM14_IRQn);
  642. timer_irq_handler(14);
  643. IRQ_EXIT(TIM8_TRG_COM_TIM14_IRQn);
  644. }
  645. #endif
  646. // UART/USART IRQ handlers
  647. void USART1_IRQHandler(void) {
  648. IRQ_ENTER(USART1_IRQn);
  649. uart_irq_handler(1);
  650. IRQ_EXIT(USART1_IRQn);
  651. }
  652. void USART2_IRQHandler(void) {
  653. IRQ_ENTER(USART2_IRQn);
  654. uart_irq_handler(2);
  655. IRQ_EXIT(USART2_IRQn);
  656. }
  657. #if defined(STM32F0)
  658. void USART3_8_IRQHandler(void) {
  659. IRQ_ENTER(USART3_8_IRQn);
  660. uart_irq_handler(3);
  661. uart_irq_handler(4);
  662. uart_irq_handler(5);
  663. uart_irq_handler(6);
  664. uart_irq_handler(7);
  665. uart_irq_handler(8);
  666. IRQ_EXIT(USART3_8_IRQn);
  667. }
  668. #else
  669. void USART3_IRQHandler(void) {
  670. IRQ_ENTER(USART3_IRQn);
  671. uart_irq_handler(3);
  672. IRQ_EXIT(USART3_IRQn);
  673. }
  674. void UART4_IRQHandler(void) {
  675. IRQ_ENTER(UART4_IRQn);
  676. uart_irq_handler(4);
  677. IRQ_EXIT(UART4_IRQn);
  678. }
  679. void UART5_IRQHandler(void) {
  680. IRQ_ENTER(UART5_IRQn);
  681. uart_irq_handler(5);
  682. IRQ_EXIT(UART5_IRQn);
  683. }
  684. void USART6_IRQHandler(void) {
  685. IRQ_ENTER(USART6_IRQn);
  686. uart_irq_handler(6);
  687. IRQ_EXIT(USART6_IRQn);
  688. }
  689. #if defined(UART8)
  690. void UART7_IRQHandler(void) {
  691. IRQ_ENTER(UART7_IRQn);
  692. uart_irq_handler(7);
  693. IRQ_EXIT(UART7_IRQn);
  694. }
  695. #endif
  696. #if defined(UART8)
  697. void UART8_IRQHandler(void) {
  698. IRQ_ENTER(UART8_IRQn);
  699. uart_irq_handler(8);
  700. IRQ_EXIT(UART8_IRQn);
  701. }
  702. #endif
  703. #endif
  704. #if defined(MICROPY_HW_CAN1_TX)
  705. void CAN1_RX0_IRQHandler(void) {
  706. IRQ_ENTER(CAN1_RX0_IRQn);
  707. can_rx_irq_handler(PYB_CAN_1, CAN_FIFO0);
  708. IRQ_EXIT(CAN1_RX0_IRQn);
  709. }
  710. void CAN1_RX1_IRQHandler(void) {
  711. IRQ_ENTER(CAN1_RX1_IRQn);
  712. can_rx_irq_handler(PYB_CAN_1, CAN_FIFO1);
  713. IRQ_EXIT(CAN1_RX1_IRQn);
  714. }
  715. void CAN1_SCE_IRQHandler(void) {
  716. IRQ_ENTER(CAN1_SCE_IRQn);
  717. can_sce_irq_handler(PYB_CAN_1);
  718. IRQ_EXIT(CAN1_SCE_IRQn);
  719. }
  720. #endif
  721. #if defined(MICROPY_HW_CAN2_TX)
  722. void CAN2_RX0_IRQHandler(void) {
  723. IRQ_ENTER(CAN2_RX0_IRQn);
  724. can_rx_irq_handler(PYB_CAN_2, CAN_FIFO0);
  725. IRQ_EXIT(CAN2_RX0_IRQn);
  726. }
  727. void CAN2_RX1_IRQHandler(void) {
  728. IRQ_ENTER(CAN2_RX1_IRQn);
  729. can_rx_irq_handler(PYB_CAN_2, CAN_FIFO1);
  730. IRQ_EXIT(CAN2_RX1_IRQn);
  731. }
  732. void CAN2_SCE_IRQHandler(void) {
  733. IRQ_ENTER(CAN2_SCE_IRQn);
  734. can_sce_irq_handler(PYB_CAN_2);
  735. IRQ_EXIT(CAN2_SCE_IRQn);
  736. }
  737. #endif
  738. #if MICROPY_PY_PYB_LEGACY
  739. #if defined(MICROPY_HW_I2C1_SCL)
  740. void I2C1_EV_IRQHandler(void) {
  741. IRQ_ENTER(I2C1_EV_IRQn);
  742. i2c_ev_irq_handler(1);
  743. IRQ_EXIT(I2C1_EV_IRQn);
  744. }
  745. void I2C1_ER_IRQHandler(void) {
  746. IRQ_ENTER(I2C1_ER_IRQn);
  747. i2c_er_irq_handler(1);
  748. IRQ_EXIT(I2C1_ER_IRQn);
  749. }
  750. #endif // defined(MICROPY_HW_I2C1_SCL)
  751. #if defined(MICROPY_HW_I2C2_SCL)
  752. void I2C2_EV_IRQHandler(void) {
  753. IRQ_ENTER(I2C2_EV_IRQn);
  754. i2c_ev_irq_handler(2);
  755. IRQ_EXIT(I2C2_EV_IRQn);
  756. }
  757. void I2C2_ER_IRQHandler(void) {
  758. IRQ_ENTER(I2C2_ER_IRQn);
  759. i2c_er_irq_handler(2);
  760. IRQ_EXIT(I2C2_ER_IRQn);
  761. }
  762. #endif // defined(MICROPY_HW_I2C2_SCL)
  763. #if defined(MICROPY_HW_I2C3_SCL)
  764. void I2C3_EV_IRQHandler(void) {
  765. IRQ_ENTER(I2C3_EV_IRQn);
  766. i2c_ev_irq_handler(3);
  767. IRQ_EXIT(I2C3_EV_IRQn);
  768. }
  769. void I2C3_ER_IRQHandler(void) {
  770. IRQ_ENTER(I2C3_ER_IRQn);
  771. i2c_er_irq_handler(3);
  772. IRQ_EXIT(I2C3_ER_IRQn);
  773. }
  774. #endif // defined(MICROPY_HW_I2C3_SCL)
  775. #if defined(MICROPY_HW_I2C4_SCL)
  776. void I2C4_EV_IRQHandler(void) {
  777. IRQ_ENTER(I2C4_EV_IRQn);
  778. i2c_ev_irq_handler(4);
  779. IRQ_EXIT(I2C4_EV_IRQn);
  780. }
  781. void I2C4_ER_IRQHandler(void) {
  782. IRQ_ENTER(I2C4_ER_IRQn);
  783. i2c_er_irq_handler(4);
  784. IRQ_EXIT(I2C4_ER_IRQn);
  785. }
  786. #endif // defined(MICROPY_HW_I2C4_SCL)
  787. #endif // MICROPY_PY_PYB_LEGACY