irq.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. #ifndef MICROPY_INCLUDED_STM32_IRQ_H
  27. #define MICROPY_INCLUDED_STM32_IRQ_H
  28. // Use this macro together with NVIC_SetPriority to indicate that an IRQn is non-negative,
  29. // which helps the compiler optimise the resulting inline function.
  30. #define IRQn_NONNEG(pri) ((pri) & 0x7f)
  31. // these states correspond to values from query_irq, enable_irq and disable_irq
  32. #define IRQ_STATE_DISABLED (0x00000001)
  33. #define IRQ_STATE_ENABLED (0x00000000)
  34. // Enable this to get a count for the number of times each irq handler is called,
  35. // accessible via pyb.irq_stats().
  36. #define IRQ_ENABLE_STATS (0)
  37. #if IRQ_ENABLE_STATS
  38. extern uint32_t irq_stats[FPU_IRQn + 1];
  39. #define IRQ_ENTER(irq) ++irq_stats[irq]
  40. #define IRQ_EXIT(irq)
  41. #else
  42. #define IRQ_ENTER(irq)
  43. #define IRQ_EXIT(irq)
  44. #endif
  45. static inline mp_uint_t query_irq(void) {
  46. return __get_PRIMASK();
  47. }
  48. // enable_irq and disable_irq are defined inline in mpconfigport.h
  49. #if __CORTEX_M >= 0x03
  50. // irqs with a priority value greater or equal to "pri" will be disabled
  51. // "pri" should be between 1 and 15 inclusive
  52. static inline uint32_t raise_irq_pri(uint32_t pri) {
  53. uint32_t basepri = __get_BASEPRI();
  54. // If non-zero, the processor does not process any exception with a
  55. // priority value greater than or equal to BASEPRI.
  56. // When writing to BASEPRI_MAX the write goes to BASEPRI only if either:
  57. // - Rn is non-zero and the current BASEPRI value is 0
  58. // - Rn is non-zero and less than the current BASEPRI value
  59. pri <<= (8 - __NVIC_PRIO_BITS);
  60. __ASM volatile ("msr basepri_max, %0" : : "r" (pri) : "memory");
  61. return basepri;
  62. }
  63. // "basepri" should be the value returned from raise_irq_pri
  64. static inline void restore_irq_pri(uint32_t basepri) {
  65. __set_BASEPRI(basepri);
  66. }
  67. #endif
  68. MP_DECLARE_CONST_FUN_OBJ_0(pyb_wfi_obj);
  69. MP_DECLARE_CONST_FUN_OBJ_0(pyb_disable_irq_obj);
  70. MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_enable_irq_obj);
  71. MP_DECLARE_CONST_FUN_OBJ_0(pyb_irq_stats_obj);
  72. // IRQ priority definitions.
  73. //
  74. // Lower number implies higher interrupt priority.
  75. //
  76. // The default priority grouping is set to NVIC_PRIORITYGROUP_4 in the
  77. // HAL_Init function. This corresponds to 4 bits for the priority field
  78. // and 0 bits for the sub-priority field (which means that for all intensive
  79. // purposes that the sub-priorities below are ignored).
  80. //
  81. // While a given interrupt is being processed, only higher priority (lower number)
  82. // interrupts will preempt a given interrupt. If sub-priorities are active
  83. // then the sub-priority determines the order that pending interrupts of
  84. // a given priority are executed. This is only meaningful if 2 or more
  85. // interrupts of the same priority are pending at the same time.
  86. //
  87. // The priority of the SysTick timer is determined from the TICK_INT_PRIORITY
  88. // value which is normally set to 0 in the stm32f4xx_hal_conf.h file.
  89. //
  90. // The following interrupts are arranged from highest priority to lowest
  91. // priority to make it a bit easier to figure out.
  92. #if __CORTEX_M == 0
  93. //#def IRQ_PRI_SYSTICK 0
  94. #define IRQ_PRI_UART 1
  95. #define IRQ_PRI_FLASH 1
  96. #define IRQ_PRI_SDIO 1
  97. #define IRQ_PRI_DMA 1
  98. #define IRQ_PRI_OTG_FS 2
  99. #define IRQ_PRI_OTG_HS 2
  100. #define IRQ_PRI_TIM5 2
  101. #define IRQ_PRI_CAN 2
  102. #define IRQ_PRI_TIMX 2
  103. #define IRQ_PRI_EXTINT 2
  104. #define IRQ_PRI_PENDSV 3
  105. #define IRQ_PRI_RTC_WKUP 3
  106. #else
  107. //#def IRQ_PRI_SYSTICK NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 0, 0)
  108. // The UARTs have no FIFOs, so if they don't get serviced quickly then characters
  109. // get dropped. The handling for each character only consumes about 0.5 usec
  110. #define IRQ_PRI_UART NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 1, 0)
  111. // Flash IRQ must be higher priority than interrupts of all those components
  112. // that rely on the flash storage.
  113. #define IRQ_PRI_FLASH NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 2, 0)
  114. // SDIO must be higher priority than DMA for SDIO DMA transfers to work.
  115. #define IRQ_PRI_SDIO NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 4, 0)
  116. // DMA should be higher priority than USB, since USB Mass Storage calls
  117. // into the sdcard driver which waits for the DMA to complete.
  118. #define IRQ_PRI_DMA NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 5, 0)
  119. #define IRQ_PRI_OTG_FS NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 6, 0)
  120. #define IRQ_PRI_OTG_HS NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 6, 0)
  121. #define IRQ_PRI_TIM5 NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 6, 0)
  122. #define IRQ_PRI_CAN NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 7, 0)
  123. // Interrupt priority for non-special timers.
  124. #define IRQ_PRI_TIMX NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 13, 0)
  125. #define IRQ_PRI_EXTINT NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 14, 0)
  126. // PENDSV should be at the lowst priority so that other interrupts complete
  127. // before exception is raised.
  128. #define IRQ_PRI_PENDSV NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 15, 0)
  129. #define IRQ_PRI_RTC_WKUP NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 15, 0)
  130. #endif
  131. #endif // MICROPY_INCLUDED_STM32_IRQ_H