RT1064 study notes - external interrupt

All interrupt numbers can be viewed from line 74 in MIMXRT1064.h

external interruption

1. Initialize the IO port as input

Set the state of the IO port to be used as an external interrupt input, which can be set as a pull-up/pull-down input,

It can also be set as a floating input (PKE=0), but when floating, the external pull-up or pull-down resistor must be provided.

Otherwise it may cause interrupts to trigger continuously.

//设置 B3 的复用功能选择寄存器,设置B3为普通GPIO模式
IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B0_03_GPIO1_IO03,0);
//配置 B3 的功能
//0 1111 0000 1000 0000
//关闭 Hyst,上拉 22K Ohm,选择Pull,使能pull/keeper,关闭开漏,100MHz_SPEED2,关闭输出驱动,低转换速度
IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_B0_03_GPIO1_IO03,0xF080);

2. Enable the interrupt function of the corresponding IO port

When the IO port of RT1052 is set to input mode, if you want to enable the interrupt function of a certain IO port, you need to set the corresponding bit of GPIOx→IMR to 1 to trigger the interrupt.

This step is realized through the GPIO_PinInit function. Also take the KEY button as an example, the KEY_UP button corresponds to GPIO5_IO00, the code to set this IO as an interrupt function is as follows:

gpio_pin_config_t int_config;
key_config.direction=kGPIO_DigitalInput;    //输入
key_config.interruptMode=kGPIO_IntFallingEdge;  //下降沿触发
key_config.outputLogic=1;           //此值设置为输出时才有效
GPIO_PinInit(GPIO1,3,&key_config);//此函数中自动打开时钟

3. Set trigger conditions

typedef enum _gpio_interrupt_mode
{
    
    
    kGPIO_NoIntmode              = 0U, //无中断模式
    kGPIO_IntLowLevel            = 1U, //低电平触发
    kGPIO_IntHighLevel           = 2U, //高电平触发
    kGPIO_IntRisingEdge          = 3U, //上升沿触发
    kGPIO_IntFallingEdge         = 4U, //下降沿触发
    kGPIO_IntRisingOrFallingEdge = 5U, //任意边沿触发
} gpio_interrupt_mode_t;

Configure the variable of interruptMode in this structure in (2) to enable the external interrupt
key_config.interruptMode=kGPIO_IntFallingEdge; //Falling edge trigger

4. Enable and configure interrupt priority

In this step, we configure the interrupt priority and enable it. For the RT1052 interrupt, it can only be executed if the NVIC setting is configured and enabled, otherwise it will not be executed in the interrupt service function.

board_init();
//在board_init();中有下面这条代码,此代码分配了中断优先级的分组
NVIC_SetPriorityGrouping(((uint32_t)0x3));  //设置中断优先级分组

The priority grouping is 3, all of which are preemptive

EnableGlobalIRQ(0);Finally, add to enable total interrupt before while(1) .

5. Write interrupt service function

This is the last step of the interrupt setting. The interrupt service function is essential. If the interrupt is enabled in the code, but the interrupt service function is not written, it may cause a hardware error and cause the program to crash! So after enabling an interrupt, you must remember to write a service function for the interrupt. Write the post-interrupt operation you want to perform in the interrupt service function.

It is absolutely forbidden to use the delay function in the interrupt in the actual project! ! ! Here, for the convenience of demonstrating the routine, the delay function is used in the interrupt to debounce!

RT1052 has 5 groups of IO ports, the lower 16 bits of each group of IO ports occupy one interrupt, and the upper 16 bits occupy one interrupt, so that each group of IO ports can be handled by using 2 interrupt service functions. When each group of IO ports has multiple interrupts, it is necessary to judge the source of the interrupt (ISR register) in the interrupt service function, and then perform related processing.

Examples of Interrupt Service Functions

void GPIO3_Combined_16_31_IRQHandler(void)
{
    
    
delay_ms(10); 
if(GPIO_PinRead(GPIO3,26)==0) //KEY2(GPIO3_25)是否保持按下状态
{
    
    
 LED0_Toggle;
}
 GPIO_PortClearInterruptFlags(GPIO3,1<<26); //清除中断标志位
__DSB(); //数据同步屏蔽指令 
}
void PIT_IRQHandler(void)
{
    
    
    if(PIT_FLAG_GET(PIT_CH0))
    {
    
    
        PIT_FLAG_CLEAR(PIT_CH0);
        
    }
    __DSB();//数据同步屏蔽指令 
}

Basic flow of interrupt function writing

中断函数名(){
    
    
	if(读取中断标志位){
    
    
		执行函数
		轻触中断标志位
	}
}

This article refers to the punctual atom RT1052 development guide.

Guess you like

Origin blog.csdn.net/m0_56116736/article/details/123448401