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