key.h:
#ifndef __KEY_H_
#define __KEY_H_
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_exti.h"
#include "stm32mp1xx_gic.h"
//KEY1-->PF9
//RCC/GPIO/EXTI章节初始化
void hal_exti_pf9_init();
//GIC章节初始化
void hal_gic_pf9_init();
#endif
key.c:
#include "key.h"
//RCC/GPIO/EXTI章节初始化
void hal_exti_pf9_init()
{
//RCC章节初始化
//1.使能GPIOF组控制器 MP_AHB4ENSETR[5] = 1
RCC->MP_AHB4ENSETR |= (0x1 << 5);
//GPIO章节初始化
//1.设置KEY1按键为输入模式 MODER[19:18] = 00
GPIOF->MODER &= (~(0x3 << 18));
//EXTI章节初始化
//1.设置PF9引脚与EXTI9进行连接EXTICR3[15:8] = 0x05
EXTI->EXTICR3 &= (~(0xff << 8));
EXTI->EXTICR3 |= (0x05 << 8);
//2.设置KEY1按键为下降沿触发方式 FTSR1[9] = 1
EXTI->FTSR1 |= (0x1 << 9);
//3.设置EXTI9中断不屏蔽 C1IMR1[9] = 1
EXTI->C1IMR1 |= (0x1 << 9);
}
//GIC章节初始化
void hal_gic_pf9_init()
{
//GICD章节初始化
//1.设置GICD层中断设置使能寄存器
//CTRL[0] = 1 ISENABLER[3]第3位写1
GICD->CTRL |= (0x1 << 0);
GICD->ISENABLER[3] |= (0x1 << 3);
//2.设置GICD层中断优先级寄存器 IPRIORITYR[24]第[31:27] = 0b00000
GICD->IPRIORITYR[24] &= (~(0x1f << 27));
//3.设置中断目标分配寄存器 ITARGETSR[24]第[25:24] = 0b01
GICD->ITARGETSR[24] &= (~(0x3 << 24));
GICD->ITARGETSR[24] |= (0x1 << 24);
//GICC章节初始化
//1.设置GICC层中断设置使能寄存器 CTRL[0] = 1
GICC->CTRL |= (0x1 << 0);
//2.设置GICC层中断优先级寄存器 PMR[7:3] = 0b11111
GICC->PMR |= (0x1f << 3);
}
main.c:
#include "key.h"
extern void printf(const char *fmt, ...);
void delay_ms(int ms)
{
int i,j;
for(i = 0; i < ms;i++)
for (j = 0; j < 1800; j++);
}
int main()
{
hal_exti_pf9_init();
hal_gic_pf9_init();
while(1)
{
}
return 0;
}
do_irq:
#include "stm32mp1xx_gic.h"
#include "stm32mp1xx_exti.h"
extern void printf(const char *fmt, ...);
//中断处理函数
void do_irq(void)
{
unsigned int num;
//1.获取中断号 IAR[9:0]
num = GICC->IAR & (0x3ff);
//2.判断中断号,并且打印一句话
switch(num)
{
case 97:
break;
case 98:
break;
case 99:
printf("key1 down!!!!\n");
//3.清除EXTI层中断挂起标志位 FPR1[9] = 1
EXTI->FPR1 |= (0x1 << 9);
//4.清除GICD层中断挂起标志位 ICPENDR[3]第三位
GICD->ICPENDR[3] |= (0x1 << 3);
break;
}
//5.清除获取到中断号 EOIR
GICC->EOIR = num;
}