首先是头文件部分
#ifndef __KEY_H__
#define __KEY_H__
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_exti.h"
#include "stm32mp1xx_gic.h"
//#include ""
//gpio端口初始化
void gpio_init();
//exti按键控制器初始化
void exti_init();
//gic中断初始化
void gic_init();
#endif
然后是do_irq.c部分的代码,该部分负责调用
#include"gpio.h"
#include"key.h"
#include "uart4.h"
extern void delay_ms(unsigned int ms);
extern void printf(const char *fmt, ...);
unsigned int i = 0;
void do_irq(void)
{
unsigned int num=0;
//获取中断号
num=GICC->IAR &0x1ff;
//GICC_IAR[9:0];
switch(num)
{
case 97:
{
delay_ms(500);
printf("KEY2----------\n");
EXTI->FPR1 |=(0X1 <<7);
EXTI->FPR1 |=(0X1 <<8);
EXTI->FPR1 |=(0X1 <<9);
GICD->ICPENDR[3] |= (0x1 << 3);
GICD->ICPENDR[3] |= (0x1 << 1);
GICD->ICPENDR[3] |= (0x1 << 2);
}
break;
case 98:
{
delay_ms(500);
printf("KEY3----------\n");
EXTI->FPR1 |=(0X1 << 7);
EXTI->FPR1 |=(0X1 << 8);
EXTI->FPR1 |=(0X1 << 9);
GICD->ICPENDR[3] |= (0x1 << 3);
GICD->ICPENDR[3] |= (0x1 << 1);
GICD->ICPENDR[3] |= (0x1 << 2);
}
break;
case 99:
//调用99号中断处理
//打印函数
{
delay_ms(500);
printf("KEY1-----------\n");
EXTI->FPR1 |=(0X1 << 7);
EXTI->FPR1 |=(0X1 << 8);
EXTI->FPR1 |= (0x1 << 9);
GICD->ICPENDR[3] |= (0x1 << 3);
GICD->ICPENDR[3] |= (0x1 << 1);
GICD->ICPENDR[3] |= (0x1 << 2);
}
break;
}
//清楚中断号
GICC->EOIR &=~(0x1ff);
GICC->EOIR |=num;
}
接着是key.c部分的代码,该部分的代码负责初始化各个寄存器
#include "key.h"
void gpio_init()
{
//RCC使能控制
RCC->MP_AHB4ENSETR |=(0X1 <<5);
//设置PF9为输入
GPIOF->MODER &= ~(0X3 << 18);
GPIOF->MODER &= ~(0X3 << 16);
GPIOF->MODER &= ~(0X3 << 14);
}
//设置按键编号
void exti_init()
{
//先将PF9连接到EXTI9
EXTI->EXTICR3 &=~(0Xff<<8);
EXTI->EXTICR3 |=(0X05 <<8);
EXTI->EXTICR3 &=~(0xff << 0);
EXTI->EXTICR3 |=(0X05 <<0);
EXTI->EXTICR2 &=~(0xff <<24);
EXTI->EXTICR2 |=(0x05 << 24);
//设置为下降沿
EXTI->FTSR1|=(0X1 << 7);
EXTI->FTSR1|=(0X1 << 8);
EXTI->FTSR1|=(0X1 << 9);
//EXTI9使能中断
EXTI->C1IMR1 |=(0X1 <<7);
EXTI->C1IMR1 |=(0X1 <<8);
EXTI->C1IMR1 |=(0X1 <<9);
}
//设置中断号
void gic_init()
{
//GICD中断使能
GICD->CTRL |=(0X1 << 0);
GICD->ISENABLER[3]=(0x1 <<3);
GICD->ISENABLER[3]=(0x1 <<1);
GICD->ISENABLER[3]=(0x1 <<2);
GICD->IPRIORITYR[24] &=~(0x1F <<11); //0-31
GICD->IPRIORITYR[24] |=(0x1 << 11); //0-31
GICD->IPRIORITYR[24] &=~(0x1F <<19); //0-31
GICD->IPRIORITYR[24] |=(0x1 << 19); //0-31
GICD->IPRIORITYR[24] &=~(0x1F <<27); //0-31
GICD->IPRIORITYR[24] |=(0x1 <<27); //0-31
GICD->ITARGETSR[24] &=~(0x3 <<8);
GICD->ITARGETSR[24] |=(0x1 <<8);
GICD->ITARGETSR[24] &=~(0x3 <<16);
GICD->ITARGETSR[24] |=(0x1 <<16);
GICD->ITARGETSR[24] &=~(0x3 << 24);
GICD->ITARGETSR[24] |=(0x1 <<24);
//GICC中断使能
GICC->CTRL |=(0x1 << 0);
GICC->PMR &=~(0X1f << 3);
GICC->PMR |=(0XF << 3);
}
最后是主函数的调用,只需要调用三个初始化函数即可
#include "led.h"
#include "uart4.h"
#include "key.h"
#include "gpio.h"
//#include "command"
extern void printf(const char *fmt, ...);
void delay_ms(unsigned int ms)
{
int i,j;
for(i = 0; i < ms;i++)
for (j = 0; j < 1800; j++);
}
int main()
{
// exti_init();
gpio_init();
exti_init();
gic_init();
while(1)
{
}
return 0;
}