中断概览

什么是中断?

  • 中断是指计算机运行过程中,出现某些意外情况需要主机干预时,机器自动停止正在运行的程序并转入处理新情况的程序,处理完毕后又返回原被暂停的程序继续运行
  • 异常和中断的概念相近,异常可以说是内核活动产生(比如执行指令出错)。中断一般是指,由连接到外部器间(外设)产生(比如外设产生中断。提示数据传输完成)。它们的触发或者说处理方式相同。使用中一般并不严格区分异常和中断。
  • 如果没有特殊说明,后面所叙述的异常,特指系统异常,中断特指外中断,也就是外设中断。

STM32异常一览表

编号 类型 优先级 简介
0 N/A N/A 没有异常在运行
1 复位 -3(最高) 复位
2 NMI -2 不可屏蔽中断(来自外部NMI输入脚)
3 硬(hard) fault -1 所有被除能的fault,都将”上访”成硬件fault,除能的原因包括当前被禁用,或者被PRIMASK或BASPRI掩蔽。
4 MemManage fault 可编程 存储器管理fault,MPU访问犯规一级访问非法位置均可引发此fault
5 总线fault 可编程 从总线系统收到了错误响应,原因可以是预取流产(Abort)或数据流产,或者企图访问协处理器
6 用法(usage) Fault 可编程 由于程序错误导致的异常,通常是使用了一条无效指令,或者是非法的状态转换,例如尝试切换到ARM状态
7-10 保留 N/A N/A
11 SVCall 可编程 执行系统服务调用指令(SVC)引发的异常
12 调试监视器 可编程 调试监视器(断点,数据观察点,或者是外部调试请求)
13 保留 N/A N/A
14 PendSV 可编程 为系统设备而设置的”可悬挂请求”(pendable request)
15 SysTick 可编程 系统滴答定时器

STM32中断一览表

16 IRQ #0 可编程 外中断 #0
17 IRQ #1 可编程 外中断 #1
…. ….
255 IRQ #239 可编程 外中断 #239

.s文件中找到/* External Interrupts */(外部中断)

  1. /* External Interrupts */
  2. .word WWDG_IRQHandler /* Window WatchDog */
  3. .word PVD_IRQHandler /* PVD through EXTI Line detection */
  4. .word TAMP_STAMP_IRQHandler /* Tamper and TimeStamps through the EXTI line */
  5. .word RTC_WKUP_IRQHandler /* RTC Wakeup through the EXTI line */
  6. .word FLASH_IRQHandler /* FLASH */
  7. .word RCC_IRQHandler /* RCC */
  8. .word EXTI0_IRQHandler /* EXTI Line0 */
  9. .word EXTI1_IRQHandler /* EXTI Line1 */
  10. .word EXTI2_IRQHandler /* EXTI Line2 */
  11. .word EXTI3_IRQHandler /* EXTI Line3 */
  12. .word EXTI4_IRQHandler /* EXTI Line4 */
  13. .word DMA1_Stream0_IRQHandler /* DMA1 Stream 0 */
  14. .word DMA1_Stream1_IRQHandler /* DMA1 Stream 1 */
  15. .word DMA1_Stream2_IRQHandler /* DMA1 Stream 2 */
  16. .word DMA1_Stream3_IRQHandler /* DMA1 Stream 3 */
  17. .word DMA1_Stream4_IRQHandler /* DMA1 Stream 4 */
  18. .word DMA1_Stream5_IRQHandler /* DMA1 Stream 5 */
  19. .word DMA1_Stream6_IRQHandler /* DMA1 Stream 6 */
  20. .word ADC_IRQHandler /* ADC1, ADC2 and ADC3s */
  21. .word CAN1_TX_IRQHandler /* CAN1 TX */
  22. .word CAN1_RX0_IRQHandler /* CAN1 RX0 */
  23. .word CAN1_RX1_IRQHandler /* CAN1 RX1 */
  24. .word CAN1_SCE_IRQHandler /* CAN1 SCE */
  25. .word EXTI9_5_IRQHandler /* External Line[9:5]s */
  26. .word TIM1_BRK_TIM9_IRQHandler /* TIM1 Break and TIM9 */
  27. .word TIM1_UP_TIM10_IRQHandler /* TIM1 Update and TIM10 */
  28. .word TIM1_TRG_COM_TIM11_IRQHandler /* TIM1 Trigger and Commutation and TIM11 */
  29. .word TIM1_CC_IRQHandler /* TIM1 Capture Compare */
  30. .word TIM2_IRQHandler /* TIM2 */
  31. .word TIM3_IRQHandler /* TIM3 */
  32. .word TIM4_IRQHandler /* TIM4 */
  33. .word I2C1_EV_IRQHandler /* I2C1 Event */
  34. .word I2C1_ER_IRQHandler /* I2C1 Error */
  35. .word I2C2_EV_IRQHandler /* I2C2 Event */
  36. .word I2C2_ER_IRQHandler /* I2C2 Error */
  37. .word SPI1_IRQHandler /* SPI1 */
  38. .word SPI2_IRQHandler /* SPI2 */
  39. .word USART1_IRQHandler /* USART1 */
  40. .word USART2_IRQHandler /* USART2 */
  41. .word USART3_IRQHandler /* USART3 */
  42. .word EXTI15_10_IRQHandler /* External Line[15:10]s */
  43. .word RTC_Alarm_IRQHandler /* RTC Alarm (A and B) through EXTI Line */
  44. .word OTG_FS_WKUP_IRQHandler /* USB OTG FS Wakeup through EXTI line */
  45. .word TIM8_BRK_TIM12_IRQHandler /* TIM8 Break and TIM12 */
  46. .word TIM8_UP_TIM13_IRQHandler /* TIM8 Update and TIM13 */
  47. .word TIM8_TRG_COM_TIM14_IRQHandler /* TIM8 Trigger and Commutation and TIM14 */
  48. .word TIM8_CC_IRQHandler /* TIM8 Capture Compare */
  49. .word DMA1_Stream7_IRQHandler /* DMA1 Stream7 */
  50. .word FSMC_IRQHandler /* FSMC */
  51. .word SDIO_IRQHandler /* SDIO */
  52. .word TIM5_IRQHandler /* TIM5 */
  53. .word SPI3_IRQHandler /* SPI3 */
  54. .word UART4_IRQHandler /* UART4 */
  55. .word UART5_IRQHandler /* UART5 */
  56. .word TIM6_DAC_IRQHandler /* TIM6 and DAC1&2 underrun errors */
  57. .word TIM7_IRQHandler /* TIM7 */
  58. .word DMA2_Stream0_IRQHandler /* DMA2 Stream 0 */
  59. .word DMA2_Stream1_IRQHandler /* DMA2 Stream 1 */
  60. .word DMA2_Stream2_IRQHandler /* DMA2 Stream 2 */
  61. .word DMA2_Stream3_IRQHandler /* DMA2 Stream 3 */
  62. .word DMA2_Stream4_IRQHandler /* DMA2 Stream 4 */
  63. .word ETH_IRQHandler /* Ethernet */
  64. .word ETH_WKUP_IRQHandler /* Ethernet Wakeup through EXTI line */
  65. .word CAN2_TX_IRQHandler /* CAN2 TX */
  66. .word CAN2_RX0_IRQHandler /* CAN2 RX0 */
  67. .word CAN2_RX1_IRQHandler /* CAN2 RX1 */
  68. .word CAN2_SCE_IRQHandler /* CAN2 SCE */
  69. .word OTG_FS_IRQHandler /* USB OTG FS */
  70. .word DMA2_Stream5_IRQHandler /* DMA2 Stream 5 */
  71. .word DMA2_Stream6_IRQHandler /* DMA2 Stream 6 */
  72. .word DMA2_Stream7_IRQHandler /* DMA2 Stream 7 */
  73. .word USART6_IRQHandler /* USART6 */
  74. .word I2C3_EV_IRQHandler /* I2C3 event */
  75. .word I2C3_ER_IRQHandler /* I2C3 error */
  76. .word OTG_HS_EP1_OUT_IRQHandler /* USB OTG HS End Point 1 Out */
  77. .word OTG_HS_EP1_IN_IRQHandler /* USB OTG HS End Point 1 In */
  78. .word OTG_HS_WKUP_IRQHandler /* USB OTG HS Wakeup through EXTI */
  79. .word OTG_HS_IRQHandler /* USB OTG HS */
  80. .word DCMI_IRQHandler /* DCMI */
  81. .word 0 /* CRYP crypto */
  82. .word HASH_RNG_IRQHandler /* Hash and Rng */
  83. .word FPU_IRQHandler /* FPU

对于STM32的异常或者中断外设而言:

  • 1、它的中断(/异常)是可以被编号标识的
  • 2、它没有特定的类型
  • 3、优先级

    • 中断优先级分别俩种:
      • 可编程
      • 不可编程
    • 对于STM32的中断优先级决定着内核优先级响应谁的中断请求
    • 小值优先原则,中断优先级数值越小,中断会被优先响应
    • 中断优先级按照优先级分组配置

      优先级分组

  • 通过优先级分组,可以管理中断响应顺序。

  • 只有抢占优先级才有抢占中断权限,发生中断嵌套
    • 例: B中断正在执行,A中断抢占优先级比B中断小(A中断优先级要比B高),A中断则抢过B中断的使用权,响应A的中断服务函数,A中断执行完再交回B
  • 如果抢占优先级相同,不发生抢占行为。
  • 如果多个挂起的中断具有相同的抢占优先级,则子优先级高的先行,如果子优先级相同,则IRQ编号小的先行。

    • A,B,C,D中D中断在运行,A,B,C的优先级无法抢断D中断;等D中端完成后,A,B,C的抢占优先级相同,就要判断子优先级,如果A的子优先级要比B和C的要大,则先运行A中断; 如果B和C的子优先级相同,则由IRQ编号小的先行\
      • 查看: system_stm32f4xx.c -> #include “stm32f4xx.h” -> 找到特定的包(我这里时F407)
        1. #if defined(STM32F405xx)
        2. #include "stm32f405xx.h"
        3. #elif defined(STM32F415xx)
        4. #include "stm32f415xx.h"
        5. #elif defined(STM32F407xx)
        6. #include "stm32f407xx.h"
        7. #elif defined(STM32F417xx)
        8. #include "stm32f417xx.h"
        9. #elif defined(STM32F427xx)
        10. #include "stm32f427xx.h"
        11. #elif defined(STM32F437xx)
        12. #include "stm32f437xx.h"
        13. #elif defined(STM32F429xx)
        14. #include "stm32f429xx.h"
        15. #elif defined(STM32F439xx)
        16. #include "stm32f439xx.h"
        17. #elif defined(STM32F401xC)
        18. #include "stm32f401xc.h"
        19. #elif defined(STM32F401xE)
        20. #include "stm32f401xe.h"
        21. #elif defined(STM32F410Tx)
        22. #include "stm32f410tx.h"
        23. #elif defined(STM32F410Cx)
        24. #include "stm32f410cx.h"
        25. #elif defined(STM32F410Rx)
        26. #include "stm32f410rx.h"
        27. #elif defined(STM32F411xE)
        28. #include "stm32f411xe.h"
        29. #elif defined(STM32F446xx)
        30. #include "stm32f446xx.h"
        31. #elif defined(STM32F469xx)
        32. #include "stm32f469xx.h"
        33. #elif defined(STM32F479xx)
        34. #include "stm32f479xx.h"
        35. #elif defined(STM32F412Cx)
        36. #include "stm32f412cx.h"
        37. #elif defined(STM32F412Zx)
        38. #include "stm32f412zx.h"
        39. #elif defined(STM32F412Rx)
        40. #include "stm32f412rx.h"
        41. #elif defined(STM32F412Vx)
        42. #include "stm32f412vx.h"
        43. #elif defined(STM32F413xx)
        44. #include "stm32f413xx.h"
        45. #elif defined(STM32F423xx)
        46. #include "stm32f423xx.h"
        47. #else
        48. #error "Please select first the target STM32F4xx device used in your application (in stm32f4xx.h file)"
        49. #endif
        亮的图标Ctrl + 点击找到
        1. typedef enum
        2. {
        3. /****** Cortex-M4 Processor Exceptions Numbers ****************************************************************/
        4. NonMaskableInt_IRQn = -14, /*!< 2 Non Maskable Interrupt */
        5. MemoryManagement_IRQn = -12, /*!< 4 Cortex-M4 Memory Management Interrupt */
        6. BusFault_IRQn = -11, /*!< 5 Cortex-M4 Bus Fault Interrupt */
        7. UsageFault_IRQn = -10, /*!< 6 Cortex-M4 Usage Fault Interrupt */
        8. SVCall_IRQn = -5, /*!< 11 Cortex-M4 SV Call Interrupt */
        9. DebugMonitor_IRQn = -4, /*!< 12 Cortex-M4 Debug Monitor Interrupt */
        10. PendSV_IRQn = -2, /*!< 14 Cortex-M4 Pend SV Interrupt */
        11. SysTick_IRQn = -1, /*!< 15 Cortex-M4 System Tick Interrupt */
        12. /****** STM32 specific Interrupt Numbers **********************************************************************/
        13. WWDG_IRQn = 0, /*!< Window WatchDog Interrupt */
        14. PVD_IRQn = 1, /*!< PVD through EXTI Line detection Interrupt */
        15. TAMP_STAMP_IRQn = 2, /*!< Tamper and TimeStamp interrupts through the EXTI line */
        16. RTC_WKUP_IRQn = 3, /*!< RTC Wakeup interrupt through the EXTI line */
        17. FLASH_IRQn = 4, /*!< FLASH global Interrupt */
        18. RCC_IRQn = 5, /*!< RCC global Interrupt */
        19. EXTI0_IRQn = 6, /*!< EXTI Line0 Interrupt */
        20. EXTI1_IRQn = 7, /*!< EXTI Line1 Interrupt */
        21. EXTI2_IRQn = 8, /*!< EXTI Line2 Interrupt */
        22. EXTI3_IRQn = 9, /*!< EXTI Line3 Interrupt */
        23. EXTI4_IRQn = 10, /*!< EXTI Line4 Interrupt */
        24. DMA1_Stream0_IRQn = 11, /*!< DMA1 Stream 0 global Interrupt */
        25. DMA1_Stream1_IRQn = 12, /*!< DMA1 Stream 1 global Interrupt */
        26. DMA1_Stream2_IRQn = 13, /*!< DMA1 Stream 2 global Interrupt */
        27. DMA1_Stream3_IRQn = 14, /*!< DMA1 Stream 3 global Interrupt */
        28. DMA1_Stream4_IRQn = 15, /*!< DMA1 Stream 4 global Interrupt */
        29. DMA1_Stream5_IRQn = 16, /*!< DMA1 Stream 5 global Interrupt */
        30. DMA1_Stream6_IRQn = 17, /*!< DMA1 Stream 6 global Interrupt */
        31. ADC_IRQn = 18, /*!< ADC1, ADC2 and ADC3 global Interrupts */
        32. CAN1_TX_IRQn = 19, /*!< CAN1 TX Interrupt */
        33. CAN1_RX0_IRQn = 20, /*!< CAN1 RX0 Interrupt */
        34. CAN1_RX1_IRQn = 21, /*!< CAN1 RX1 Interrupt */
        35. CAN1_SCE_IRQn = 22, /*!< CAN1 SCE Interrupt */
        36. EXTI9_5_IRQn = 23, /*!< External Line[9:5] Interrupts */
        37. TIM1_BRK_TIM9_IRQn = 24, /*!< TIM1 Break interrupt and TIM9 global interrupt */
        38. TIM1_UP_TIM10_IRQn = 25, /*!< TIM1 Update Interrupt and TIM10 global interrupt */
        39. TIM1_TRG_COM_TIM11_IRQn = 26, /*!< TIM1 Trigger and Commutation Interrupt and TIM11 global interrupt */
        40. TIM1_CC_IRQn = 27, /*!< TIM1 Capture Compare Interrupt */
        41. TIM2_IRQn = 28, /*!< TIM2 global Interrupt */
        42. TIM3_IRQn = 29, /*!< TIM3 global Interrupt */
        43. TIM4_IRQn = 30, /*!< TIM4 global Interrupt */
        44. I2C1_EV_IRQn = 31, /*!< I2C1 Event Interrupt */
        45. I2C1_ER_IRQn = 32, /*!< I2C1 Error Interrupt */
        46. I2C2_EV_IRQn = 33, /*!< I2C2 Event Interrupt */
        47. I2C2_ER_IRQn = 34, /*!< I2C2 Error Interrupt */
        48. SPI1_IRQn = 35, /*!< SPI1 global Interrupt */
        49. SPI2_IRQn = 36, /*!< SPI2 global Interrupt */
        50. USART1_IRQn = 37, /*!< USART1 global Interrupt */
        51. USART2_IRQn = 38, /*!< USART2 global Interrupt */
        52. USART3_IRQn = 39, /*!< USART3 global Interrupt */
        53. EXTI15_10_IRQn = 40, /*!< External Line[15:10] Interrupts */
        54. RTC_Alarm_IRQn = 41, /*!< RTC Alarm (A and B) through EXTI Line Interrupt */
        55. OTG_FS_WKUP_IRQn = 42, /*!< USB OTG FS Wakeup through EXTI line interrupt */
        56. TIM8_BRK_TIM12_IRQn = 43, /*!< TIM8 Break Interrupt and TIM12 global interrupt */
        57. TIM8_UP_TIM13_IRQn = 44, /*!< TIM8 Update Interrupt and TIM13 global interrupt */
        58. TIM8_TRG_COM_TIM14_IRQn = 45, /*!< TIM8 Trigger and Commutation Interrupt and TIM14 global interrupt */
        59. TIM8_CC_IRQn = 46, /*!< TIM8 Capture Compare global interrupt */
        60. DMA1_Stream7_IRQn = 47, /*!< DMA1 Stream7 Interrupt */
        61. FSMC_IRQn = 48, /*!< FSMC global Interrupt */
        62. SDIO_IRQn = 49, /*!< SDIO global Interrupt */
        63. TIM5_IRQn = 50, /*!< TIM5 global Interrupt */
        64. SPI3_IRQn = 51, /*!< SPI3 global Interrupt */
        65. UART4_IRQn = 52, /*!< UART4 global Interrupt */
        66. UART5_IRQn = 53, /*!< UART5 global Interrupt */
        67. TIM6_DAC_IRQn = 54, /*!< TIM6 global and DAC1&2 underrun error interrupts */
        68. TIM7_IRQn = 55, /*!< TIM7 global interrupt */
        69. DMA2_Stream0_IRQn = 56, /*!< DMA2 Stream 0 global Interrupt */
        70. DMA2_Stream1_IRQn = 57, /*!< DMA2 Stream 1 global Interrupt */
        71. DMA2_Stream2_IRQn = 58, /*!< DMA2 Stream 2 global Interrupt */
        72. DMA2_Stream3_IRQn = 59, /*!< DMA2 Stream 3 global Interrupt */
        73. DMA2_Stream4_IRQn = 60, /*!< DMA2 Stream 4 global Interrupt */
        74. ETH_IRQn = 61, /*!< Ethernet global Interrupt */
        75. ETH_WKUP_IRQn = 62, /*!< Ethernet Wakeup through EXTI line Interrupt */
        76. CAN2_TX_IRQn = 63, /*!< CAN2 TX Interrupt */
        77. CAN2_RX0_IRQn = 64, /*!< CAN2 RX0 Interrupt */
        78. CAN2_RX1_IRQn = 65, /*!< CAN2 RX1 Interrupt */
        79. CAN2_SCE_IRQn = 66, /*!< CAN2 SCE Interrupt */
        80. OTG_FS_IRQn = 67, /*!< USB OTG FS global Interrupt */
        81. DMA2_Stream5_IRQn = 68, /*!< DMA2 Stream 5 global interrupt */
        82. DMA2_Stream6_IRQn = 69, /*!< DMA2 Stream 6 global interrupt */
        83. DMA2_Stream7_IRQn = 70, /*!< DMA2 Stream 7 global interrupt */
        84. USART6_IRQn = 71, /*!< USART6 global interrupt */
        85. I2C3_EV_IRQn = 72, /*!< I2C3 event interrupt */
        86. I2C3_ER_IRQn = 73, /*!< I2C3 error interrupt */
        87. OTG_HS_EP1_OUT_IRQn = 74, /*!< USB OTG HS End Point 1 Out global interrupt */
        88. OTG_HS_EP1_IN_IRQn = 75, /*!< USB OTG HS End Point 1 In global interrupt */
        89. OTG_HS_WKUP_IRQn = 76, /*!< USB OTG HS Wakeup through EXTI interrupt */
        90. OTG_HS_IRQn = 77, /*!< USB OTG HS global interrupt */
        91. DCMI_IRQn = 78, /*!< DCMI global interrupt */
        92. RNG_IRQn = 80, /*!< RNG global Interrupt */
        93. FPU_IRQn = 81 /*!< FPU global interrupt */
        94. } IRQn_Type;
        这些都是中断的IRQ编号
  • 可编程的优先级,通过嵌套向量中断控制器(NVIC)实现。

    所以: 抢占优先级 > 子优先级 > IRQ优先级

嵌套向量中断控制器(NVIC)功能(负荷CMSIS标准的NVIC库函数)

NVIC库函数 描述
void NVIC_EnableIRQ(TRQn_Type IRQn) 使能中断
void NVIC_DisableIRQ(TRQn_Type IRQn) 使能中断
void NVIC_SetPendingIRQ(TRQn_Type IRQn) 设置中断悬起位
void NVIC_ClearPendingIRQ(TRQn_Type IRQn) 清除中断悬起位
uint32_t NVIC_GetPendingIRQ(TRQn_Type IRQn) 获取悬起中断编号
void NVIC_SetPrionty(TRQn_Type IRQn, uint32_t prionty) 设置中断优先级
uint32_t NVIC_GetPrionty(TRQn_Type IRQn) 获取中断优先级
void NVIC_SystemReset(void) 系统复位

找到#include "core_cmx.h"文件(有关NVIC相关内容)(x:是M几),Ctrl+点击
可以找像

  1. #else
  2. #define NVIC_SetPriorityGrouping __NVIC_SetPriorityGrouping
  3. #define NVIC_GetPriorityGrouping __NVIC_GetPriorityGrouping
  4. #define NVIC_EnableIRQ __NVIC_EnableIRQ
  5. #define NVIC_GetEnableIRQ __NVIC_GetEnableIRQ
  6. #define NVIC_DisableIRQ __NVIC_DisableIRQ
  7. #define NVIC_GetPendingIRQ __NVIC_GetPendingIRQ
  8. #define NVIC_SetPendingIRQ __NVIC_SetPendingIRQ
  9. #define NVIC_ClearPendingIRQ __NVIC_ClearPendingIRQ
  10. #define NVIC_GetActive __NVIC_GetActive
  11. #define NVIC_SetPriority __NVIC_SetPriority
  12. #define NVIC_GetPriority __NVIC_GetPriority
  13. #define NVIC_SystemReset __NVIC_SystemReset
  14. #endif /* CMSIS_NVIC_VIRTUAL */
  1. /**
  2. \brief Enable Interrupt
  3. \details Enables a device specific interrupt in the NVIC interrupt controller.
  4. \param [in] IRQn Device specific interrupt number.
  5. \note IRQn must not be negative.
  6. */
  7. __STATIC_INLINE void __NVIC_EnableIRQ(IRQn_Type IRQn)
  8. {
  9. if ((int32_t)(IRQn) >= 0)
  10. {
  11. NVIC->ISER[(((uint32_t)IRQn) >> 5UL)] = (uint32_t)(1UL << (((uint32_t)IRQn) & 0x1FUL));
  12. }
  13. }

在主函数中输入HAL_NVIC_SetPendingIRQ()Ctrl+点击

  1. void HAL_NVIC_SetPendingIRQ(IRQn_Type IRQn)
  2. {
  3. /* Check the parameters */
  4. assert_param(IS_NVIC_DEVICE_IRQ(IRQn));
  5. /* Set interrupt pending */
  6. NVIC_SetPendingIRQ(IRQn);
  7. }

EXTI-扩展中断和事件控制器

事件的概念

  • STM32上许许多多的外设,是通过内部信号来协助工作的。这个信号,可以理解为事件,比如一个定时器(TIM),当我们使用定时器来计数的时候,怎么知道定时器已经计数完毕?这个时候就往往通过一个”计数完成事件”来告知(系统/用户)计数器已经完成
  • 有些事件是可见的,有些事件是不可见的。事件的可不可见,具体体现在寄存器里可不可以查到这些事件的标志。那如果我们想使用这些事件的时候,该怎么做?查询事件标志吗?
  • 在STM32中,绝大部分数事件是不可见的,但是事件几乎斗鱼中断功能绑定在一起/所以通常我们回开启中断的功能,来使用相应的事件。
  • 事件 —-> 中断 ; 事件 ——-> 事件

EXTI-扩展中断和事件控制器

  • EXTI扩展中断和事件控制器,是STM32上的一个外设。它可以捕获外部输入线电平的变化等等的一些事件。EXTI捕获到了事件后,还可以生成相应的EXTI中断及等等的一些中断。
    • 捕获外部输入等事件。
    • 生成EXTI中断等中断请求

实验工程讲解

按照STM32F103ZET6的芯片(正点原子的精英板)来配置
image.png

  • Serial Wire: 串行线

时钟配置
image.png
image.png

选择PB5的GPIO_EXTI5
image.png

image.png

  • GPIO模式

    • Extemal Interrupt Mode: 外部中断模式
    • Rising edge trigger detection : 上升沿触发
    • Falling edge trigger detection : 下降沿触发
    • 外部中断模式
      • Extemal Interrupt Mode with Rising edge trigger detection
      • Extemal Interrupt Mode with Falling edge trigger detection
      • Extemal Interrupt Mode with Rising/Falling edge trigger detection
    • 事件模式
      • Extemal Event Mode Rising edge trigger detection
      • Extemal Event Mode Falling edge trigger detection
      • Extemal Event Mode Rising/Falling edge trigger detection

        配置优先级

        image.png
  • Priorinty Group : 优先级分组

  • Preemption Priorinty : 抢占优先级
  • Sub Priorinty : 子优先级

Project Manager进行项目的配置

vscode 中的STM32F103ZET6

找到void MX_GPIO_Init(void) (gpio.c中)

  1. void MX_GPIO_Init(void)
  2. {
  3. GPIO_InitTypeDef GPIO_InitStruct = {0};
  4. /* GPIO Ports Clock Enable */
  5. __HAL_RCC_GPIOE_CLK_ENABLE();
  6. __HAL_RCC_GPIOC_CLK_ENABLE();
  7. __HAL_RCC_GPIOA_CLK_ENABLE();
  8. __HAL_RCC_GPIOB_CLK_ENABLE();
  9. /*Configure GPIO pin Output Level */
  10. HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);
  11. /*Configure GPIO pin : PtPin */
  12. GPIO_InitStruct.Pin = LED1_Pin;
  13. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  14. GPIO_InitStruct.Pull = GPIO_NOPULL;
  15. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  16. HAL_GPIO_Init(LED1_GPIO_Port, &GPIO_InitStruct);
  17. /*Configure GPIO pin : PtPin */
  18. GPIO_InitStruct.Pin = LED0_Pin;
  19. GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING; //使用了上升沿下降沿中断
  20. GPIO_InitStruct.Pull = GPIO_NOPULL;
  21. HAL_GPIO_Init(LED0_GPIO_Port, &GPIO_InitStruct);
  22. /* EXTI interrupt init*/
  23. HAL_NVIC_SetPriority(EXTI9_5_IRQn, 5, 0); //使用HAL库的设置优先级的函数来使能EXTI9_5_IRQn中断编号的中断使能
  24. HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);//使用HAL库的初始化的函数来使能EXTI9_5_IRQn中断编号的中断使能
  25. }

设置板级支持包(就是功能配置)

配置Makefile

  1. ######################################
  2. # source
  3. ######################################
  4. # C sources
  5. C_SOURCES = \
  6. Core/Src/main.c \
  7. Core/Src/gpio.c \
  8. Core/Src/stm32f1xx_it.c \
  9. Core/Src/stm32f1xx_hal_msp.c \
  10. Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio_ex.c \
  11. Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim.c \
  12. Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim_ex.c \
  13. Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal.c \
  14. Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc.c \
  15. Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc_ex.c \
  16. Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c \
  17. Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c \
  18. Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_cortex.c \
  19. Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_pwr.c \
  20. Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash.c \
  21. Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash_ex.c \
  22. Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_exti.c \
  23. Core/Src/system_stm32f1xx.c \
  24. User/bsp_exti.c #添加这段,是你的位于项目根目录下存放.c路径
  25. ...
  26. # C includes
  27. C_INCLUDES = \
  28. -ICore/Inc \
  29. -IDrivers/STM32F1xx_HAL_Driver/Inc \
  30. -IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy \
  31. -IDrivers/CMSIS/Device/ST/STM32F1xx/Include \
  32. -IDrivers/CMSIS/Include \
  33. -IUser #添加这段,前面'-I'是固定格式,添加'.h'的目录

编写的板级支持包与hal库生成的相同功能,可以不写,只是了解

  1. #include "bsp_exti.h"
  2. void EXTI_Key_Config(void){
  3. GPIO_InitTypeDef GPIO_InitStructure;
  4. /*开启按键GPIO口的时钟*/
  5. __HAL_RCC_GPIOE_CLK_ENABLE();
  6. /*设置按键1的引脚*/
  7. GPIO_InitStructure.Pin = KEY1_INT_GPIO_PIN; //#define KEY1_INT_GPIO_PIN GPIO_PIN_3
  8. /*设置引脚的输入模式*/
  9. GPIO_InitStructure.Mode = GPIO_MODE_IT_RISING; //上升沿
  10. /*设置引脚不上拉,不下拉*/
  11. GPIO_InitStructure.Pull = GPIO_NOPULL;
  12. /*初始化*/
  13. HAL_GPIO_Init(KEY1_GPIO_Port,&GPIO_InitStructure);//#define KEY1_GPIO_Port GPIOE
  14. /*配置EXTI中断源到Key1引脚、配置中断优先级*/
  15. HAL_NVIC_SetPriority(KEY1_INT_EXTI_IRQ,0,0);//#define KEY1_INT_EXTI_IRQ EXTI3_IRQn
  16. /*使能中断*/
  17. HAL_NVIC_EnableIRQ(KEY1_INT_EXTI_IRQ);
  18. /*设置按键0的引脚*/
  19. GPIO_InitStructure.Pin = KEY0_INT_GPIO_PIN; //#define KEY0_INT_GPIO_PIN GPIO_PIN_4
  20. /*设置引脚的输入模式*/
  21. GPIO_InitStructure.Mode = GPIO_MODE_IT_RISING; //上升沿
  22. /*设置引脚不上拉,不下拉*/
  23. GPIO_InitStructure.Pull = GPIO_NOPULL;
  24. /*初始化*/
  25. HAL_GPIO_Init(KEY0_GPIO_Port,&GPIO_InitStructure);//#define KEY0_GPIO_Port GPIOE
  26. /*配置EXTI中断源到Key1引脚、配置中断优先级*/
  27. HAL_NVIC_SetPriority(KEY0_INT_EXTI_IRQ,0,0);//#define KEY0_INT_EXTI_IRQ EXTI3_IRQn
  28. /*使能中断*/
  29. HAL_NVIC_EnableIRQ(KEY0_INT_EXTI_IRQ);
  30. }
  1. #ifndef __BSP_EXTI_H_
  2. #define __BSP_EXTI_H_
  3. #ifdef __cplusplus
  4. extern "C"{
  5. #endif
  6. #define KEY1_INT_GPIO_PIN GPIO_PIN_3
  7. #define KEY1_INT_EXTI_IRQ EXTI3_IRQn
  8. #define KEY1_GPIO_Port GPIOE
  9. #define KEY0_INT_GPIO_PIN GPIO_PIN_4
  10. #define KEY0_INT_EXTI_IRQ EXTI3_IRQn
  11. #define KEY0_GPIO_Port GPIOE
  12. //Include .........
  13. #include "stm32f1xx_hal.h"
  14. void EXTI_Key_Config(void);
  15. #ifdef __cplusplus
  16. }
  17. #endif
  18. #endif

查看GPIO_MODE_IT_RISING(中断上升沿模式)

  1. #define GPIO_MODE_IT_RISING 0x10110000u /*!< External Interrupt Mode with Rising edge trigger detection */
  2. #define GPIO_MODE_IT_FALLING 0x10210000u /*!< External Interrupt Mode with Falling edge trigger detection */
  3. #define GPIO_MODE_IT_RISING_FALLING 0x10310000u /*!< External Interrupt Mode with Rising/Falling edge trigger detection */
  1. /*--------------------- EXTI Mode Configuration ------------------------*/ //EXTI模式的配置
  2. /* Configure the External Interrupt or event for the current IO */
  3. if ((GPIO_Init->Mode & EXTI_MODE) == EXTI_MODE)
  4. {
  5. /* Enable AFIO Clock */ //使能IO配置
  6. __HAL_RCC_AFIO_CLK_ENABLE();
  7. temp = AFIO->EXTICR[position >> 2u];
  8. CLEAR_BIT(temp, (0x0Fu) << (4u * (position & 0x03u)));
  9. SET_BIT(temp, (GPIO_GET_INDEX(GPIOx)) << (4u * (position & 0x03u)));
  10. AFIO->EXTICR[position >> 2u] = temp;
  11. /* Configure the interrupt mask */
  12. if ((GPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT)
  13. {
  14. SET_BIT(EXTI->IMR, iocurrent);//置位
  15. }
  16. else
  17. {
  18. CLEAR_BIT(EXTI->IMR, iocurrent);//复位
  19. }
  20. /* Configure the event mask */
  21. if ((GPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT)
  22. {
  23. SET_BIT(EXTI->EMR, iocurrent);
  24. }
  25. else
  26. {
  27. CLEAR_BIT(EXTI->EMR, iocurrent);
  28. }
  29. /* Enable or disable the rising trigger */
  30. if ((GPIO_Init->Mode & RISING_EDGE) == RISING_EDGE)
  31. {
  32. SET_BIT(EXTI->RTSR, iocurrent);
  33. }
  34. else
  35. {
  36. CLEAR_BIT(EXTI->RTSR, iocurrent);
  37. }
  38. /* Enable or disable the falling trigger */
  39. if ((GPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE)
  40. {
  41. SET_BIT(EXTI->FTSR, iocurrent);
  42. }
  43. else
  44. {
  45. CLEAR_BIT(EXTI->FTSR, iocurrent);
  46. }
  47. }
  • EXTI中断 -> 中断向量 -> 偏移:入口地址 -> 中断服务函数

    1. ...
    2. .word WWDG_IRQHandler
    3. .word PVD_IRQHandler
    4. .word TAMPER_IRQHandler
    5. .word RTC_IRQHandler
    6. .word FLASH_IRQHandler
    7. .word RCC_IRQHandler
    8. .word EXTI0_IRQHandler
    9. .word EXTI1_IRQHandler
    10. .word EXTI2_IRQHandler
    11. .word EXTI3_IRQHandler
    12. .word EXTI4_IRQHandler
    13. ...

    大量的Handler,这些函数就是中断的服务函数也就是说中断向量表中存放的都是这些函数

  • 不同的输入线对应着不同的一个中断服务函数,当我们触发中断的时候CPU就会到中断服务向量表中根据中断的编号进行偏移,偏移之后就会到中断向量表中对应位置去取出函数的入口地址

  • 可以在stm32f1xx_it.c句柄

    • NMI_Handler() : 不可屏蔽中断的句柄
    • HardFault_Handler() : 硬件错误的句柄
    • 这些中断句柄都是弱定义来的(也就是说你中断服务函数放在哪个位置都可以)
      1. void KEY0_IRQHandler(void){ //板机支持包中的宏定义: #define KEY0_IRQHandler EXTI4_IRQHandler
      2. //确保是否产生了EXTI Line中断
      3. if(__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_4) != RESET){
      4. HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_5);
      5. //清除标志位
      6. __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_4);
      7. }
      8. }
  • 通过EXTI线,获取EXTI线事件,并且去生成中断,并清除EXTI中断标志(中断标志要清除,不然就会一直产生中断)

没写板级支持包也可以:

  1. /**
  2. * @brief This function handles EXTI line4 interrupt.
  3. */
  4. void EXTI4_IRQHandler(void)
  5. {
  6. /* USER CODE BEGIN EXTI4_IRQn 0 */
  7. if(__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_4) != RESET){
  8. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_5,GPIO_PIN_SET);
  9. __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_4);
  10. }
  11. /* USER CODE END EXTI4_IRQn 0 */
  12. HAL_GPIO_EXTI_IRQHandler(KEY0_Pin);
  13. /* USER CODE BEGIN EXTI4_IRQn 1 */
  14. /* USER CODE END EXTI4_IRQn 1 */
  15. }