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**********/
//初始化EXTI层
void pf9_exti_init();
//初始化GICD层
void pf9_gicd_init();
//初始化GICC层
void pf9_gicc_init();
//初始化EXTI层
void pf8_exti_init();
//初始化GICD层
void pf8_gicd_init();
//初始化GICC层
void pf8_gicc_init();
//初始化EXTI层
void pf7_exti_init();
//初始化GICD层
void pf7_gicd_init();
//初始化GICC层
void pf7_gicc_init();
#endif
key.c
#include "key.h"
/*****KEY1---->PF9**********/
//初始化EXTI层
void pf9_exti_init()
{
/*****RCC章节初始化******/
//1.设置GPIOF组时钟使能 MP_AHB4ENSETR[5] = 1
RCC->MP_AHB4ENSETR |= 0x1<<5;
/*****GPIO章节初始化******/
//1.设置PF9引脚为输入模式 MODER[19:18] = 00
GPIOF->MODER &=(~(0x3<<18));
/*****EXTI章节初始化******/
//1.设置PF9引脚和EXIT9控制器进行连接 EXTICR3[15:8] = 0x05
EXTI->EXTICR3 &=(~(0xff<<8));
EXTI->EXTICR3 |= (0x05<<8);
//2.设置PF9引脚检测方式下降沿 FTSR1[9] = 1
EXTI->FTSR1 |= 0x1<<9;
//3.设置PF9引脚中断不屏蔽 C1IMR1[9] = 1
EXTI->C1IMR1 |= 0x1<<9;
}
void pf8_exti_init()
{
RCC->MP_AHB4ENSETR |= (0x1 << 5);
GPIOF->MODER &= (~(0x3 << 16));
EXTI->EXTICR3 |= (0x05);
EXTI->FTSR1 |= (0x1 << 8);
EXTI->C1IMR1 |= (0x1 << 8);
}
void pf7_exti_init()
{
RCC->MP_AHB4ENSETR |= (0x1 << 5);
GPIOF->MODER &= (~(0x3 << 14));
EXTI->EXTICR2 &= (~(0xff << 24));
EXTI->EXTICR2 |= (0x05 << 24);
EXTI->FTSR1 |= (0x1 << 7);
EXTI->C1IMR1 |= (0x1 << 7);
}
//初始化GICD层
void pf9_gicd_init()
{
//1.设置GICD层全局中断使能寄存器 CTRL[0] = 1
GICD->CTRL |=0x1<<0;
//2.设置GICD层中断使能寄存器 ISENABLER[3] 第三位 1
GICD->ISENABLER[3] |= 0x1<<3;
//3.设置GICD层中断优先级寄存器IPRIORITYR[24] [31:27]
GICD->IPRIORITYR[24] &=(~(0x1f<<27));
//4.设置GICD层中断目标分配寄存器 ITARGETSR[24] [25:24] = 01
GICD->ITARGETSR[24] &=(~(0x3<<24));
GICD->ITARGETSR[24] |=0x1<<24;
}
void pf8_gicd_init()
{
GICD->CTRL |= 0x1;
GICD->ISENABLER[3] |= (0x1 << 2);
GICD->IPRIORITYR[24] |= (0x01 << 19);
GICD->ITARGETSR[24] |= (0x1 << 16);
}
void pf7_gicd_init()
{
GICD->CTRL |= 0x1;
GICD->ISENABLER[3] |= (0x1 << 1);
GICD->IPRIORITYR[24] |= (0x01 << 11);
GICD->ITARGETSR[24] |= (0x1 << 8);
}
//初始化GICC层
void pf9_gicc_init()
{
//1.设置GICC层全局中断使能寄存器 CTRL[0] = 1
GICC->CTRL |=0x1<<0;
//2.设置GICC层中断优先级寄存器 PMR[7:3]
GICC->PMR &=(~(0x1f<<3));
GICC->PMR |=0x1f<<3;
}
void pf8_gicc_init()
{
GICC->CTRL |= 0x1;
GICC->PMR |= (0x02 << 3);
}
void pf7_gicc_init()
{
GICC->CTRL |= 0x1;
GICC->PMR |= (0x02 << 3);
}
do_irq.c
#include "key.h"
extern void printf(const char *fmt, ...);
extern void delay_ms(int ms);
unsigned int i = 0;
void do_irq(void)
{
unsigned int num = 0;
//获取中断信号
num = GICC->IAR;
switch(num)
{
case 97:
delay_ms(500); //防按键抖动
printf("key2######\n");
//清除EXTI中断挂起标志位
EXTI->FPR1 |= (0x1 << 7);
//清除GICD层中断挂起标志位
//key1-->PF9-->EXTI9-->事件9-->中断号99
//97 / 32 = 3...1
GICD->ICPENDR[3] |= (0x1 << 1);
break;
case 98:
delay_ms(500); //防按键抖动
printf("key3######\n");
//清除EXTI中断挂起标志位
EXTI->FPR1 |= (0x1 << 8);
//清除GICD层中断挂起标志位
//key1-->PF9-->EXTI9-->事件9-->中断号99
//98 / 32 = 3...2
GICD->ICPENDR[3] |= (0x1 << 2);
break;
case 99:
delay_ms(500); //防按键抖动
printf("key1######\n");
//清除EXTI中断挂起标志位
EXTI->FPR1 |= (0x1 << 9);
//清除GICD层中断挂起标志位
//key1-->PF9-->EXTI9-->事件9-->中断号99
//99 / 32 = 3...3
GICD->ICPENDR[3] |= (0x1 << 3);
break;
}
//清除中断信号
GICC->EOIR &= (~(0x3FF << 0));
GICC->EOIR |= num;
}