[Explanation of STM32] GPIO Application Design IO External Interrupt EXTI

Table of contents

I. Introduction

Two, EXTI structure

3. The concept of IO external interrupt

1. External interrupt mapping

2. External interrupt register

3. External interrupt API function

4. IO external interrupt configuration application steps

V. Summary


I. Introduction

        The GPIO-related API functions and configuration of STM32 have been introduced in detail before. When GPIO is used as an input pin, calling the relevant read signal pin function interface can poll the input signal in the loop of the program. Detection operation, in addition to polling to access the input pin, you can also use another method called external interrupt to detect the input signal of the pin. This article first introduces the structure of EXTI, and then introduces the concept of external interrupt , I have a preliminary understanding of STM32's IO external interrupt EXTI, and on this basis, focus on the analysis of the use of IO external interrupt EXTI.

Figure 1 External interrupt design


Two, EXTI structure

        EXTI (External interrupt/event controller)—External interrupt/event controller, which manages 20 interrupt/event lines of the controller. Each interrupt/event line corresponds to an edge detector, which can realize the detection of the rising edge and falling edge of the input signal. EXTI can realize the individual configuration of each interrupt/event line, which can be individually configured as interrupt or event, and the attributes of triggering events.

        In Figure 2, you can see that many signal lines are marked with a slash and marked with "20". This means that there are 20 similar signal lines inside the controller, which is consistent with the total of 20 interrupt/event lines in EXTI. . So as long as we understand the principle of one of them, the other 19 circuit principles will also be known.

Figure 2 EXTI structure

        EXTI can be divided into two major functions, one is to generate interrupts, the other is to generate events, these two functions are different from the hardware.

        The difference between interrupts and events:

        Event: A certain signal occurs, such as a rising edge or a falling edge. Does not necessarily trigger an interrupt.

        Interrupt: A certain event occurs and generates an interrupt, and then jumps to the corresponding interrupt service function for corresponding processing .

        First, let's look at the circuit flow indicated by the red dotted line in the figure. It is a line that generates an interrupt, and finally the signal flows into the NVIC controller.

        Number 1 is the input line. The EXTI controller has 19 interrupt/event input lines. These input lines can be set to any GPIO through registers, or they can be some peripheral events. We will explain this part later. The input line is generally a signal with level changes.

        Number 2 is an edge detection circuit, which controls signal triggering according to the settings of the corresponding bits of the rising edge trigger selection register (EXTI_RTSR) and the falling edge trigger selection register (EXTI_FTSR). The edge detection circuit takes the input line as the signal input terminal, and if an edge transition is detected, it outputs a valid signal 1 to the number 3 circuit, otherwise it outputs an invalid signal 0. The two registers EXTI_RTSR and EXTI_FTSR can be used to detect which types of level transitions the controller needs to detect. It can be triggered only by rising edges, only by falling edges, or by both rising and falling edges.

        The No. 3 circuit is actually an OR gate circuit, one of its inputs comes from the No. 2 circuit, and the other comes from the software interrupt event register (EXTI_SWIER). EXTI_SWIER allows us to start the interrupt/event line through program control, which is very useful in some places. We know that the function of the OR gate is 1 if there is 1, so any one of these two inputs has a valid signal 1 and can output 1 to the number 4 and number 6 circuits.

        The number 4 circuit is an AND gate circuit, one of its inputs is the number 3 circuit, and the other input comes from the interrupt mask register (EXTI_IMR). The AND gate circuit requires the input to be 1 before outputting 1. The result is that if EXTI_IMR is set to 0, then no matter whether the output signal of the number 3 circuit is 1 or 0, the output signal of the final number 4 circuit is 0; if EXTI_IMR is set When it is 1, the output signal of the final number 4 circuit is determined by the output signal of the number 3 circuit, so that we can simply control EXTI_IMR to achieve the purpose of generating an interrupt. The signal output by circuit No. 4 will be stored in the pending register (EXTI_PR). If it is determined that the output of circuit No. 4 is 1, the corresponding bit of EXTI_PR will be set to 1.

        Number 5 is to output the contents of the EXTI_PR register to the NVIC, so as to realize the system interrupt event control.

        Next we look at the circuit flow indicated by the green dotted line. It is a line that generates events and eventually outputs a pulse signal. The event generation line is different from the interrupt line after the number 3 circuit, and the previous circuits are all shared.

        The number 6 circuit is an AND gate, one of its inputs comes from the number 3 circuit, and the other input comes from the event mask register (EXTI_EMR). If EXTI_EMR is set to 0, no matter whether the output signal of the number 3 circuit is 1 or 0, the signal output by the final number 6 circuit is 0; if EXTI_EMR is set to 1, the signal output by the final number 6 circuit is determined by the number 3 circuit The output signal is determined, so we can simply control EXTI_EMR to achieve the purpose of whether to generate events.

        No. 7 is a pulse generator circuit. When its input terminal, that is, the output terminal of No. 6 circuit, is a valid signal 1, a pulse will be generated; if the input terminal is an invalid signal, no pulse will be output.

        Number 8 is a pulse signal, which is the final product of the circuit that generates the event. This pulse signal can be used by other peripheral circuits, such as timer TIM, analog-to-digital converter ADC, etc. Such pulse signal is generally used to trigger TIM or ADC starts converting.

        The purpose of generating the interrupt line is to input the input signal to the NVIC, and further run the interrupt service function to realize the function, which is at the software level. The purpose of generating the event line is to transmit a pulse signal to other peripherals, and it is a signal transmission at the circuit level, which belongs to the hardware level.

        In addition, EXTI is on the APB2 bus, you need to pay attention to this when programming.


3. The concept of IO external interrupt

        External interrupt is an internal mechanism for the microcontroller to process external events in real time. When an external event occurs, the interrupt system of the single-chip microcomputer will force the CPU to suspend the program being executed and turn to handle the interrupt event; after the interrupt is processed. It returns to the interrupted program and continues to execute.

Figure 3 Concept content of external interrupt

1. External interrupt mapping

        The external interrupt/event controller EXTI contains up to 23 edge detectors for generating event/interrupt requests. Each input line is individually configurable to select the type (interrupt or event) and corresponding trigger event (rising edge, falling edge, or edge). Each input wire can also be individually shielded.

        Taking STM32F407 as an example, it supports up to 23 software events/interrupt requests. These events/interrupt requests are input to the EXTI controller through the EXTI line, and the EXTI lines are connected as follows:

        EXTI_Line0~15: Connect to the input interrupt of the external GPIO port.

        EXTI_Line16: Connect to PVD output

        EXTI_Line17: Connect to RTC alarm event

        EXTI_Line18: Connect to USB OTG FS wakeup event

        EXTI_Line19: connect to ethernet wakeup event

        EXTI_Line20: Connect to USB OTG HS (configured in FS) wakeup event

        EXTI_Line21: Connect to RTC intrusion and timestamp events

        EXTI_Line22: Connect to RTC wakeup event

        What we focus on here is the input interrupt of the GPIO port, so EXTI_Line16~EXTI_Line22 is not the focus of this article. Each GPIO pin of STM32 can be used as an external interrupt input, and there are dozens or even hundreds of GPIO port pins of STM32, so since each GPIO pin can be used as an external interrupt input, and EXTI_Line0~15 only has 16 Therefore, the corresponding relationship between IO pins and external interrupt lines is as follows:

Figure 4 External interrupt/event GPIO mapping

        As can be seen from Figure 4, since each GPIO port of STM32 has 16 pins, EXTI_Line0~15 corresponds to pins pin0~pin15. For example, EXTI_Line0 corresponds to GPIOA0~GPIOI0, so by analogy EXTI_Line1 corresponds to GPIOA1~GPIOI1, so each EXTI_Line can correspond to up to 9 pins, and the specific mapping to that pin needs to be configured accordingly.

2. External interrupt register

(1), interrupt mask register EXTI_IMR

        Figure 5 is the definition of the interrupt mask register. This register is used to open and close the external interrupt request. Bits 0~22 are valid, corresponding to the 23 external interrupt requests mentioned above. When the corresponding bit is written to 0, the external interrupt request is closed; corresponding When the bit is written as 1, the external interrupt request is turned on.

Figure 5 Interrupt mask register definition

(2), event mask register EXTI_EMR

        Figure 6 is the definition of the event mask register. This register is used to open and close the external event request. 0~22 bits are valid, corresponding to the 23 external event requests mentioned above. When the corresponding bit is written to 0, the external event request is closed; corresponding Writing a 1 to this bit turns on external event requests. The event is just a trigger signal. It is the trigger source of the interrupt. It can trigger the interrupt or not trigger the interrupt. Open the interrupt mask bit corresponding to EXTI_IMR, then the event can trigger the corresponding interrupt. Only after an interrupt is triggered, the program will jump to the corresponding interrupt handler.

Figure 6 Event mask register definition

(3), rising edge trigger selection register EXTI_RTSR

        Figure 7 is the definition of the rising edge trigger selection register. This register is used to set the trigger event of the external interrupt as the rising edge of the signal. Bits 0~22 are valid, corresponding to the 23 external event requests mentioned above. When the corresponding bit is written to 0, Turn off the rising edge request of the external event signal, and the rising edge interrupt of the signal cannot be triggered; when the corresponding bit is written to 1, turn on the rising edge request of the external signal, and the rising edge interrupt of the signal can be triggered.

Figure 7 Rising edge trigger selection register definition

(4), falling edge trigger selection register EXTI_FTSR

        Figure 8 is the definition of the falling edge trigger selection register. This register is used to set the trigger event of the external interrupt as the falling edge of the signal. Bits 0~22 are valid, corresponding to the 23 external event requests mentioned above. When the corresponding bit is written to 0, Turn off the external event signal falling edge request, and the signal falling edge interrupt cannot be triggered; when the corresponding bit is written to 1, turn on the external signal falling edge request, and the signal falling edge interrupt can be triggered.

Figure 8 Falling edge trigger selection register definition

(5), software interrupt event register EXTI_SWIER

        Figure 9 is the definition of the software interrupt event register. This register can trigger the generation of event interrupts in the form of software programs, and is used to simulate the generation of external actual event interrupts. Bits 0~22 are valid, corresponding to the 23 external event requests mentioned earlier. , when the corresponding bit is written as 1, it is used to simulate the generation of external events, when the corresponding bit is written as 0, it is used to reset the event state, and next time you can write 1 to generate an event. Therefore, this register uses software programs to simulate the generation of external actual events to trigger interrupts, of course, the premise is that IMR and EMR are turned on.

Figure 9 Definition of software interrupt event register

(6), pending register EXTI_PR

        Figure 10 is the definition of the pending register register. This register can mark whether an external interrupt event request has been generated. At the same time, the interrupt event can be cleared by writing 1 to the corresponding bit. Bits 0~22 are valid, corresponding to the 23 external event requests mentioned earlier. , when the corresponding bit is read as 1, it means that an external event interrupt has occurred; when the corresponding bit is read as 1, it means that no external event interrupt has occurred. Therefore, once the interrupt condition is triggered, the corresponding bit is set to 1, but it is necessary to write 1 to the corresponding bit in the interrupt service function to clear the interrupt, otherwise it will always enter the interrupt.

Figure 10 Pending register definition

3. External interrupt API function

        The EXTI function interface of STM32 introduced in this section is the function interface of the STM32 standard library. Before introducing the function of each API function interface in detail, we need to analyze the key parameters used in the function interface.

EXTI_InitTypeDef* EXTI_InitStruct

        This parameter is the structure pointer of the function parameters that need to be initialized by the EXTI function port. Let's look at the definition of this structure.

typedef struct
{
  uint32_t EXTI_Line;              //外部中断事件连接线                                                 
  EXTIMode_TypeDef EXTI_Mode;      //外部中断事件模式                                    
  EXTITrigger_TypeDef EXTI_Trigger; //边沿事件触发方式                                      
  FunctionalState EXTI_LineCmd;     //外部中断事件连接线开关
}EXTI_InitTypeDef;

        (a), External interrupt event connection line: the optional range is EXTI_Line0~EXTI_Line15.

        (b) External interrupt event mode: used to select the mode in which EXTI occurs, and the optional modes are as follows.

typedef enum
{
  EXTI_Mode_Interrupt = 0x00,  //中断模式
  EXTI_Mode_Event = 0x04       //事件模式
}EXTIMode_TypeDef;

(c), Edge event trigger mode: used to select the mode of signal edge trigger event when external IO input.

typedef enum
{
  EXTI_Trigger_Rising = 0x08,        //信号上升沿触发
  EXTI_Trigger_Falling = 0x0C,       //信号下降沿触发
  EXTI_Trigger_Rising_Falling = 0x10  //信号双边沿触发
}EXTITrigger_TypeDef;

(d), External interrupt event connection line switch: used to open and close the external interrupt event connection line.

typedef enum 
{
  DISABLE = 0,         //关闭外部中断事件连接线
  ENABLE = !DISABLE   //打开外部中断事件连接线
} FunctionalState;

        The following will introduce the specific function interface one by one. Since the STM32 standard library is used, EXTI-related functions and configuration definitions and callable interfaces are placed in the officially provided standard library file stm32fxx_exti.c and header file stm32fxx_exti.h.

(1)、void EXTI_DeInit(void);

        Function: Restore the value of each register of EXTI to the reset value, and the reset value of each register is as follows.

EXTI->IMR = 0x00000000;
EXTI->EMR = 0x00000000;
EXTI->RTSR = 0x00000000;
EXTI->FTSR = 0x00000000;
EXTI->PR = 0x007FFFFF;

(2)、void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct);

        Function: Initialize the interrupt line of the external interrupt.

        Example:

EXTI_InitStructure.EXTI_Line = EXTI_Line2; //外部中断事件连接线为EXTI2
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//EXTI模式为外部中断模式
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //外部IO输入信号为下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;//打开外部中断事件连接线
EXTI_Init(&EXTI_InitStructure);

(3)、void EXTI_StructInit(EXTI_InitTypeDef* EXTI_InitStruct);

        Function: Get a default state of EXTI, which can be applied to an external interrupt event. The internal default state of this function is as follows.

EXTI_InitStruct->EXTI_Line = EXTI_LINENONE; //外部中断事件连接线为无
EXTI_InitStruct->EXTI_Mode = EXTI_Mode_Interrupt;//EXTI模式为外部中断模式
EXTI_InitStruct->EXTI_Trigger = EXTI_Trigger_Falling;//外部IO输入信号为下降沿触发
EXTI_InitStruct->EXTI_LineCmd = DISABLE;//关闭外部中断事件连接线

        Example: EXTI_StructInit(&exti_InitStruct), use exti_InitStruct to quickly obtain the default state value of the external interrupt event.

(4)、void EXTI_GenerateSWInterrupt(uint32_t EXTI_Line);

        Function: Use software to simulate an external interrupt, provided that EXTI_IMR and EXTI_EMR are enabled.

        Example: EXTI_GenerateSWInterrupt(EXTI_Line2), an interrupt is generated on EXTI_Line2 by software.

(5)、FlagStatus EXTI_GetFlagStatus(uint32_t EXTI_Line);

        Function: Detect the external event flag bit, and judge whether an external event occurs. FlagStatus=RESET, the event is not generated; FlagStatus=SET, the event is generated.

        Example: status = EXTI_GetFlagStatus(EXTI_Line2), check the external event flag on EXTI_Line2.

(6)、void EXTI_ClearFlag(uint32_t EXTI_Line);

        Function: Clear the external event flag bit.

        Example: EXTI_ClearFlag(EXTI_Line2), clear the EXTI_Line2 external event flag.

(7)、ITStatus EXTI_GetITStatus(uint32_t EXTI_Line);

        Function: Detect the external interrupt flag bit, and judge whether the external interrupt is generated. ITStatus = RESET, the interrupt is not generated; ITStatus = SET, the interrupt is generated.

        For example: status = EXTI_GetITStatus(EXTI_Line2), detect the external interrupt flag on EXTI_Line2.

(8)、void EXTI_ClearITPendingBit(uint32_t EXTI_Line);

        Function: Clear the external interrupt flag bit.

        Example: EXTI_ClearITPendingBit(EXTI_Line2), clear EXTI_Line2 external interrupt flag bit.


4. IO external interrupt configuration application steps

Figure 11 IO external interrupt configuration content

(1), initialize the corresponding GPIO pin

        It is necessary to initialize the pin according to the ordinary IO input of GPIO, and at the same time enable the peripheral clock for GPIO.

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//使能GPIOA的外设时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;//设置使用引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//普通IO输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//根据实际应用配置输出速度
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//根据实际应用配置上拉或下拉电阻
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化PA1引脚

(2), the initial system configuration controller SYSCFG

        The system configuration controller SYSCFG can be used to manage GPIO external interrupt line connections. The SYSCFG clock needs to be turned on, and the relationship between the external interrupt event line EXTI_Line and the pin pin of the GPIO needs to be mapped.

RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);//使能 SYSCFG 时钟
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource1);//PA1连接到中断线1

        Map the interrupt line 1 to GPIOA, then it is obvious that the pin1 of GPIOA is connected to the EXTI_Line1 interrupt line.

(3), initialize the external interrupt event line

        That is, call the EXTI_Init interface to configure the interrupt line, and set the parameters of EXTI_Line.

EXTI_InitStructure.EXTI_Line = EXTI_Line1; //外部中断事件连接线为EXTI1,根据实际情况设置
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//EXTI模式为外部中断模式
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //外部IO输入信号为下降沿触发根据实际情况设置
EXTI_InitStructure.EXTI_LineCmd = ENABLE;//打开外部中断事件连接线
EXTI_Init(&EXTI_InitStructure);

(4), initialize NVIC

        NVIC is a nested vector interrupt controller, which belongs to the core peripherals and manages interrupt-related functions including the core and all peripherals on the chip. For the knowledge of NVIC, you can review the contents of the STM32 interrupt system for detailed understanding.

NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn; //使能外部中断EXTI1,根据实际情况设置
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //抢占优先级2,根据实际情况设置
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02; //响应优先级2,根据实际情况设置 
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道 
NVIC_Init(&NVIC_InitStructure); //中断优先级分组初始化

(5), interrupt service function writing

        Although EXTI's external interrupt event lines have 16 EXTI_Line0~EXTI_Line15, there are only 7 GPIO external interrupt service functions specified by STM32:

EXTI0_IRQHandler
EXTI1_IRQHandler
EXTI2_IRQHandler
EXTI3_IRQHandler
EXTI4_IRQHandler
EXTI9_5_IRQHandler
EXTI15_10_IRQHandler

   It can be seen that each interrupt line of EXTI_Line0~EXTI_Line4 corresponds to an interrupt function, the interrupt lines EXTI_Line5~EXTI_Line9 share the interrupt function EXTI9_5_IRQHandler, and EXTI_Line10~EXTI_Line15 share the interrupt function EXTI15_10_IRQHandler.

        A standard GPIO external interrupt service function template is as follows:

void EXTI1_IRQHandle(void)
{
  if(EXTI_GetITStatus(EXTI_Line1)!=RESET)//判断某个EXTI_Line上的中断是否发生
  {
    ................ //此处用户自行定义中断处理逻辑
    EXTI_ClearITPendingBit(EXTI_Line3); //清除EXTI_Line上的中断标志位
  } 
}

        It should be noted that the two interrupts EXTI9_5_IRQHandler and EXTI15_10_IRQHandler are shared by multiple interrupt lines, so multiple EXTI_Line processing logic can be placed in the interrupt service function.

V. Summary

        Before the basic API of GPIO and the configuration and use process, this article introduces the structure and function of EXTI, and the way that ordinary IO input is used as an external interrupt is introduced in detail. Introduce and analyze the concept of external interrupts and the configuration and use of external interrupts. By analyzing the APIs and registers related to external interrupts, we can understand the mapping relationship between external interrupts and GPIO pins, functional characteristics, etc., so that we can better apply the interface of external interrupts. Complete the configuration and use of a series of external interrupts.


↓↓↓ For more technical content and book information acquisition, please pay attention to "Mingjie Embedded" for technical exchanges in the group ↓↓↓ 

Guess you like

Origin blog.csdn.net/helloqusheng/article/details/130069374