key.h
#ifndef __KEY_H__
#define __KEY_H__
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_gic.h"
#include "stm32mp1xx_exti.h"
void key1_config();
void key2_config();
void key3_config();
#endif
key.c
#include "key.h"
void key1_config()
{
//设置时钟使能
RCC->MP_AHB4ENSETR |=(0x1<<5);
//设置管脚输入模式PF9
GPIOF->MODER &=(~(0x3<<18));
//设置EXTI事件与管脚对应
EXTI->EXTICR3 &=(~(0xff<<8));
EXTI->EXTICR3 |=(0x05<<8);
//设置检测方式为下降沿
EXTI->FTSR1 |=(0x1<<9);
//设置可以产生事件取消屏蔽
EXTI->C1IMR1 |=(0x1<<9);
//在GPID层启用中断
GICD->ISENABLER[3] |=(0x1<<3);
//设置组寄存器的优先级
GICD->IPRIORITYR[24] &=(~(0x1f<<27));
//设置处理CPU
GICD->ITARGETSR[24] |=(0x1<<24);
//设置全局使能组为0
GICD->CTLR |=(0x1);
//设置优先级掩码为最低
GICC->PMR |=(0x1f<<3);
//使能组0的中断
GICC->CTLR |=(0x1);
}
void key2_config()
{
//设置时钟使能
RCC->MP_AHB4ENSETR |=(0x1<<5);
//设置管脚输入模式PF7
GPIOF->MODER &=(~(0x3<<14));
//设置EXTI事件与管脚对应
EXTI->EXTICR2 &=(~(0xff<<24));
EXTI->EXTICR2 |=(0x05<<24);
//设置检测方式为下降沿
EXTI->FTSR1 |=(0x1<<7);
//设置可以产生事件取消屏蔽
EXTI->C1IMR1 |=(0x1<<7);
//在GPID层启用中断
GICD->ISENABLER[3] |=(0x1<<1);
//设置组寄存器的优先级
GICD->IPRIORITYR[24] &=(~(0x1f<<11));
//设置处理CPU
GICD->ITARGETSR[24] |=(0x1<<8);
//设置全局使能组为0
GICD->CTLR |=(0x1);
//设置优先级掩码为最低
GICC->PMR |=(0x1f<<3);
//使能组0的中断
GICC->CTLR |=(0x1);
}
void key3_config()
{
//设置时钟使能
RCC->MP_AHB4ENSETR |=(0x1<<5);
//设置管脚输入模式PF8
GPIOF->MODER &=(~(0x3<<16));
//设置EXTI事件与管脚对应
EXTI->EXTICR3 &=(~(0xff));
EXTI->EXTICR3 |=(0x05);
//设置检测方式为下降沿
EXTI->FTSR1 |=(0x1<<8);
//设置可以产生事件取消屏蔽
EXTI->C1IMR1 |=(0x1<<8);
//在GPID层启用中断
GICD->ISENABLER[3] |=(0x1<<2);
//设置组寄存器的优先级
GICD->IPRIORITYR[24] &=(~(0x1f<<19));
//设置处理CPU
GICD->ITARGETSR[24] |=(0x1<<16);
//设置全局使能组为0
GICD->CTLR |=(0x1);
//设置优先级掩码为最低
GICC->PMR |=(0x1f<<3);
//使能组0的中断
GICC->CTLR |=(0x1);
}
do_irq.c
#include "key.h"
#include "led.h"
extern void printf(const char *fmt, ...);
unsigned int i = 0;
//延时函数
void delay1(int ms)
{
int i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<2000;j++);
}
}
void do_irq(void)
{
int num=(GICC->IAR &(0x3ff));
switch(num)
{
case 99:
led1_on();
//清除排队标志
GICD->ICPENDR[3] |=(0x1<<3);
//清除挂起标志
EXTI->FPR1 |=(0x1<<9);
break;
case 98:
led1_off();
//清除排队标志
GICD->ICPENDR[3] |=(0x1<<2);
//清除挂起标志
EXTI->FPR1 |=(0x1<<8);
break;
case 97:
led2_on();
delay1(1000);
led2_off();
//清除排队标志
GICD->ICPENDR[3] |=(0x1<<1);
//清除挂起标志
EXTI->FPR1 |=(0x1<<7);
break;
}
//清除中断号
GICC->EOIR |= num;
}
main.c
#include "key.h"
#include "led.h"
//延时函数
void delay(int ms)
{
int i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<2000;j++);
}
}
int main()
{
all_led_init();
key1_config();
key2_config();
key3_config();
while(1)
{
printf("mmmm\n");
delay(1000);
}
return 0;
}