STM32-外部中斷原理與配置
| 中斷線 | M3 | M4 | M7 |
|---|---|---|---|
| EXTI線0~15:對應外部IO口的輸入中斷。 | √ | √ | √ |
| EXTI線16:連接到PVD輸出。 | √ | √ | √ |
| EXTI線17:連接到RTC鬧鍾事件。 | √ | √ | √ |
| EXTI線18:連接到USB OTG FS喚醒事件。 | √ | √ | √ |
| EXTI線19:連接到以太網喚醒事件。 | √ | √ | |
| EXTI線20:連接到USB OTG HS(在FS中配置)喚醒事件 | √ | √ | |
| EXTI線21:連接到RTC入侵和時間戳事件。 | √ | √ | |
| EXTI線22:連接到RTC喚醒事件。 | √ | √ | |
| EXSTI線23:連接到LPTIM1異步事件 | √ |
STM32的每個IO都可以作為外部中斷輸入。
每個外部中斷線可以獨立的配置觸發方式(上升沿,下降沿或者雙邊沿觸發),觸發/屏蔽,專用的狀態位。
STM32供IO使用的中斷線只有16個,但是STM32F系列的IO口多達上百個,STM32F103ZGT6(112),那么中斷線怎么跟io口對應呢?
GPIOx.0映射到EXTI0
GPIOx.1映射到EXTI1
…
…
GPIOx.14映射到EXTI14
GPIOx.15映射到EXTI15
對於M4/M7,配置寄存器為SYSCFG_EXTIRx
對於M3,配置寄存器為AFIO_EXTICRx
如下圖所示,EXTI0[3:0]有4個位,可以配置16個,所以可以從PA0選擇到PI0。也就是說16個中斷線,最多可以處理16*16個外部引腳的中斷。

可以在手冊中找到SYSCFG 外部中斷配置寄存器:

16個中斷線就分配16個中斷服務函數?
IO口外部中斷在中斷向量表中只分配了7個中斷向量,也就是只能使用7個中斷服務函數。

從表中可以看出,外部中斷線5~ 9分配一個中斷向量,共用一個服務函數外部中斷線10~15分配一個中斷向量,共用一個中斷服務函數。
中斷服務函數列表:
EXTI0_IRQHandler
EXTI1_IRQHandler
EXTI2_IRQHandler
EXTI3_IRQHandler
EXTI4_IRQHandler
EXTI9_5_IRQHandler
EXTI15_10_IRQHandler
外部中斷操作使用到的函數分布文件
stm32fxxx_hal_gpio.h
stm32fxxx_hal_gpio.c
外部中斷配置:
外部中斷的中斷線映射配置和觸發方式都是在GPIO初始化函數中完成:
GPIO_InitTypeDef GPIO_Initure;
GPIO_Initure.Pin=GPIO_PIN_0; //PA0
GPIO_Initure.Mode=GPIO_MODE_IT_RISING; //上升沿觸發
GPIO_Initure.Pull=GPIO_PULLDOWN;
HAL_GPIO_Init(GPIOA,&GPIO_Initure);
void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init) { uint32_t position; uint32_t ioposition = 0x00; uint32_t iocurrent = 0x00; uint32_t temp = 0x00; /* Check the parameters */ assert_param(IS_GPIO_ALL_INSTANCE(GPIOx)); assert_param(IS_GPIO_PIN(GPIO_Init->Pin)); assert_param(IS_GPIO_MODE(GPIO_Init->Mode)); assert_param(IS_GPIO_PULL(GPIO_Init->Pull)); /* Configure the port pins */ for(position = 0; position < GPIO_NUMBER; position++) { /* Get the IO position */ ioposition = ((uint32_t)0x01) << position; /* Get the current IO position */ iocurrent = (uint32_t)(GPIO_Init->Pin) & ioposition; if(iocurrent == ioposition) { /*--------------------- GPIO Mode Configuration ------------------------*/ /* In case of Alternate function mode selection */ if((GPIO_Init->Mode == GPIO_MODE_AF_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_OD)) { /* Check the Alternate function parameter */ assert_param(IS_GPIO_AF(GPIO_Init->Alternate)); /* Configure Alternate function mapped with the current IO */ temp = GPIOx->AFR[position >> 3]; temp &= ~((uint32_t)0xF << ((uint32_t)(position & (uint32_t)0x07) * 4)) ; temp |= ((uint32_t)(GPIO_Init->Alternate) << (((uint32_t)position & (uint32_t)0x07) * 4)); GPIOx->AFR[position >> 3] = temp; } /* Configure IO Direction mode (Input, Output, Alternate or Analog) */ temp = GPIOx->MODER; temp &= ~(GPIO_MODER_MODER0 << (position * 2)); temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2)); GPIOx->MODER = temp; /* In case of Output or Alternate function mode selection */ if((GPIO_Init->Mode == GPIO_MODE_OUTPUT_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_PP) || (GPIO_Init->Mode == GPIO_MODE_OUTPUT_OD) || (GPIO_Init->Mode == GPIO_MODE_AF_OD)) { /* Check the Speed parameter */ assert_param(IS_GPIO_SPEED(GPIO_Init->Speed)); /* Configure the IO Speed */ temp = GPIOx->OSPEEDR; temp &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2