key.h
#ifndef __KEY_H__
#define __KEY_H__
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gic.h"
#include "stm32mp1xx_exti.h"
void hal_exti_pf9_init();
void hal_gicd_pf9_init();
void hal_gicc_pf9_init();
void hal_exti_pf7_init();
void hal_gicd_pf7_init();
void hal_gicc_pf7_init();
void hal_exti_pf8_init();
void hal_gicd_pf8_init();
void hal_gicc_pf8_init();
#endif
key.c
#include "key.h"
void hal_exti_pf9_init()
{
//RCC章节初始化
RCC->MP_AHB4ENSETR|=(0x1<<5);
//GPIO章节初始化
GPIOF->MODER &=(~(0x3<<18));
//exti章节初始化
EXTI->EXTICR3 &=(~(0xff<<8));
EXTI->EXTICR3 |=(0x05<<8);
//设置为下降沿模式
EXTI->FTSR1 &=(~(0x1<<9));
EXTI->FTSR1 |=(0x1<<9);
//设置为中断不屏蔽
EXTI->C1IMR1 &=(~(0x1<<9));
EXTI->C1IMR1 |=(0x1<<9);
}
void hal_gicd_pf9_init()
{
//使能对应组中断
GICD->CTRL &=(~(0x1<<0));
GICD->CTRL |=(0x1<<0);
//设置GICD层中断使能寄存器
GICD->ISENABLER[3] &=(~(0x1<<3));
GICD->ISENABLER[3] |=(0x1<<3);
//设置GICD层中断优先级寄存器
GICD->IPRIORITYR[24] &=(~(0x1f<<27));
GICD->IPRIORITYR[24] |=(0x1<<27);
//设置GICD层中断目标分配寄存器
GICD->ITARGETSR[24] &=(~(0x3<<24));
GICD->ITARGETSR[24] |=(0x1<<24);
}
void hal_gicc_pf9_init()
{
//使能
GICC->CTRL &=(~(0x1<<0));
GICC->CTRL |=(0x1<<0);
//设置GICC层中断优先级寄存器
//GICC->PMR &=(~(0x1f<<3));
GICC->PMR |=(0x1f<<3);
}
void hal_exti_pf7_init()
{
//GPIO章节初始化
GPIOF->MODER &=(~(0x3<<14));
//exti章节初始化修改后,前为3
EXTI->EXTICR2 &=(~(0xff<<24));
EXTI->EXTICR2 |=(0x05<<24);
//设置为下降沿模式
EXTI->FTSR1 &=(~(0x1<<7));
EXTI->FTSR1 |=(0x1<<7);
//设置为中断不屏蔽
EXTI->C1IMR1 &=(~(0x1<<7));
EXTI->C1IMR1 |=(0x1<<7);
}
void hal_gicd_pf7_init()
{
//使能对应组中断
GICD->CTRL &=(~(0x1<<0));
GICD->CTRL |=(0x1<<0);
//设置GICD层中断使能寄存器
GICD->ISENABLER[3] |=(0x1<<1);
//设置GICD层中断优先级寄存器
GICD->IPRIORITYR[24] &=(~(0x1f<<11));
GICD->IPRIORITYR[24] |=(0x1<<11);
//设置GICD层中断目标分配寄存器
GICD->ITARGETSR[24] &=(~(0x3<<8));
GICD->ITARGETSR[24] |=(0x1<<8);
}
void hal_gicc_pf7_init()
{
//使能
GICC->CTRL &=(~(0x1<<0));
GICC->CTRL |=(0x1<<0);
//设置GICC层中断优先级寄存器
//GICC->PMR &=(~(0x1f<<3));
GICC->PMR |=(0x1f<<3);
}
void hal_exti_pf8_init()
{
//GPIO章节初始化
GPIOF->MODER &=(~(0x3<<16));
//exti章节初始化
EXTI->EXTICR3 &=(~(0xff<<0));
EXTI->EXTICR3 |=(0x5<<0);
//设置为下降沿模式
EXTI->FTSR1 &=(~(0x1<<8));
EXTI->FTSR1 |=(0x1<<8);
//设置为中断不屏蔽
EXTI->C1IMR1 &=(~(0x1<<8));
EXTI->C1IMR1 |=(0x1<<8);
}
void hal_gicd_pf8_init()
{
//使能对应组中断
GICD->CTRL &=(~(0x1<<0));
GICD->CTRL |=(0x1<<0);
//设置GICD层中断使能寄存器
GICD->ISENABLER[3] |=(0x1<<2);
//设置GICD层中断优先级寄存器
GICD->IPRIORITYR[24] &=(~(0x1f<<19));
GICD->IPRIORITYR[24] |=(0x1<<19);
//设置GICD层中断目标分配寄存器
GICD->ITARGETSR[24] &=(~(0x3<<16));
GICD->ITARGETSR[24] |=(0x1<<16);
}
void hal_gicc_pf8_init()
{
//使能
GICC->CTRL &=(~(0x1<<0));
GICC->CTRL |=(0x1<<0);
//设置GICC层中断优先级寄存器
GICC->PMR &=(~(0x1f<<3));
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_gicd_pf9_init();
hal_gicc_pf9_init();
hal_exti_pf7_init();
hal_gicd_pf7_init();
hal_gicc_pf7_init();
hal_exti_pf8_init();
hal_gicd_pf8_init();
hal_gicc_pf8_init();
while(1)
{
}
return 0;
}
do_irq.c
#include "key.h"
extern void printf(const char *fmt, ...);
unsigned int str=0;
//中断处理函数
void do_irq(void)
{
//获取中断号 IAR
str=(GICC->IAR&0x3ff);
//判断获取的中断号,进行打印
if(99==str)
{
printf("key1\n");
//清除对断挂起标志位
EXTI->FPR1 &=(~(0x1<<9));
EXTI->FPR1 |=(0x1<<9);
//清除对应GICD层中断挂起标志位
GICD->ICPENDR[3] &=(~(0x1<<3));
GICD->ICPENDR[3] |=(0x1<<3);
}
else if(97==str)
{
//清除对断挂起标志位
printf("key2\n");
EXTI->FPR1 &=(~(0x1<<7));
EXTI->FPR1 |=(0x1<<7);
//清除对应GICD层中断挂起标志位
GICD->ICPENDR[3] &=(~(0x1<<1));
GICD->ICPENDR[3] |=(0x1<<1);
}
else if(98==str)
{
//清除对断挂起标志位
printf("key3\n");
EXTI->FPR1 &=(~(0x1<<8));
EXTI->FPR1 |=(0x1<<8);
//清除对应GICD层中断挂起标志位
GICD->ICPENDR[3] &=(~(0x1<<2));
GICD->ICPENDR[3] |=(0x1<<2);
}
//清除对应GIC层中断号???????????
GICC->EOIR|=str;
}