程序代码:
mykey.h:
cpp
#ifndef __MYKEY_H__
#define __MYKEY_H__
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_exti.h"
#include "stm32mp1xx_gic.h"
extern void printf(const char *fmt, ...);
void key1_int_config();
void key2_int_config();
void key3_int_config();
#endif
main.c:
cpp
#include "mykey.h"
void delay_ms(int ms)
{
int i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<2000;j++)
{
}
}
}
int main()
{
//按键中断初始化
key1_int_config();
key2_int_config();
key3_int_config();
while(1)
{
printf("main func\n");
delay_ms(1000);
}
}
mykey.c:
cpp
#include "mykey.h"
void key1_int_config()
{
//使能GPIOF外设时钟
RCC->MP_AHB4ENSETR |= (0x1<<5);
//设置PF9为输入
GPIOF->MODER &= (~(0X3<<18));
//设置PF9产生EXTI9事件
EXTI->EXTICR3 &= (~(0Xff<<8));
EXTI->EXTICR3 |= (0X05<<8);
//设置下降沿检测EXTI9事件
EXTI->FTSR1 |= (0x1<<9);
//取消EXTI9事件的屏蔽
EXTI->C1IMR1 |= (0x1<<9);
//全局使能99号中断
GICD->ISENABLER[3] |= (0x1<<3);
//设置99号中断优先级
GICD->IPRIORITYR[24] &= (~(0X1f<<27));//最高优先级
//设置99号中断的目标处理器
GICD->ITARGETSR[24] |= (0x1<<24);
//允许组0中断被转发到GICC层
GICD->CTRL |= 0X1;
//设置中断优先级掩码
GICC->PMR |=(0x1f<<3);
//允许组0中断被转发给处理器
GICC->CTRL |= 0X1;
}
void key2_int_config()
{
//使能GPIOF外设时钟
RCC->MP_AHB4ENSETR |= (0x1<<5);
//设置PF7为输入
GPIOF->MODER &= (~(0X3<<14));
//设置PF7产生EXTI7事件
EXTI->EXTICR2 &= (~(0Xff<<24));
EXTI->EXTICR2 |= (0X05<<24);
//设置下降沿检测EXTI7事件
EXTI->FTSR1 |= (0x1<<7);
//取消EXTI7事件的屏蔽
EXTI->C1IMR1 |= (0x1<<7);
//全局使能97号中断
GICD->ISENABLER[3] |= (0x1<<1);
//设置97号中断优先级
GICD->IPRIORITYR[24] &= (~(0X1f<<11));
//设置97号中断的目标处理器
GICD->ITARGETSR[24] |= (0x1<<8);
//允许组0中断被转发到GICC层
GICD->CTRL |= 0X1;
//设置中断优先级掩码
GICC->PMR |=(0x1f<<3);//最低优先级
//允许组0中断被转发给处理器
GICC->CTRL |= 0X1;
}
void key3_int_config()
{
//使能GPIOF外设时钟
RCC->MP_AHB4ENSETR |= (0x1<<5);
//设置PF8为输入
GPIOF->MODER &= (~(0X3<<16));
//设置PF8产生EXTI8事件
EXTI->EXTICR3 &= (~0Xff);
EXTI->EXTICR3 |= 0X05;
//设置下降沿检测EXTI8事件
EXTI->FTSR1 |= (0x1<<8);
//取消EXTI8事件的屏蔽
EXTI->C1IMR1 |= (0x1<<8);
//全局使能98号中断
GICD->ISENABLER[3] |= (0x1<<2);
//设置98号中断优先级
GICD->IPRIORITYR[24] &= (~(0X1f<<19));
//设置98号中断的目标处理器
GICD->ITARGETSR[24] |= (0x1<<16);
//允许组0中断被转发到GICC层
GICD->CTRL |= 0X1;
//设置中断优先级掩码
GICC->PMR |=(0x1f<<3);
//允许组0中断被转发给处理器
GICC->CTRL |= 0X1;
}
do_irq.c:
cpp
#include "mykey.h"
extern void printf(const char *fmt, ...);
unsigned int i = 0;
void do_irq(void)
{
//获取中断号
unsigned int irqno=(GICC->IAR&0X3ff);
switch (irqno)
{
case 99:
//中断处理逻辑
printf("KEY1_INTC\n");
//清除GICD中断标志
GICD->ICPENDR[3] |= (0x1<<3);
//清除EXTI中断触发标志
EXTI->FPR1 |= (0X1<<9);
break;
case 97:
//中断处理逻辑
printf("KEY2_INTC\n");
//清除GICD中断标志
GICD->ICPENDR[3] |= (0x1<<3);
//清除EXTI中断触发标志
EXTI->FPR1 |= (0X1<<9);
break;
case 98:
//中断处理逻辑
printf("KEY3_INTC\n");
//清除GICD中断标志
GICD->ICPENDR[3] |= (0x1<<3);
//清除EXTI中断触发标志
EXTI->FPR1 |= (0X1<<9);
break;
}
//清除IAR寄存器中保存的中断号
GICC->EOIR=irqno;
}
运行结果: