key.h
#ifndef __KEY_IT_H__
#define __KEY_IT_H__
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_gic.h"
#include "stm32mp1xx_exti.h"
#include "stm32mp1xx_rcc.h"
void key1_it_config();
void key2_it_config();
void key3_it_config();
#endif
key.c
#include "key_it.h"
void key1_it_config()
{
//使能GPIOF时钟
RCC->MP_AHB4ENSETR |= (0x1<<5);
//设置PF9管脚为输入
GPIOF->MODER &= (~(0x3<<18));
//设置PF9作为exit9事件的输入
EXTI->EXTICR3 &= (~(0xFF<<8));
EXTI->EXTICR3 |= (0x05<<8);
//设置下降沿使能检测EXTI9事件
EXTI->FTSR1 |=(0x1<<9);
//设置EXTI9时间不屏蔽
EXTI->C1EMR1 |=(0x1<<9);
//GICD使能EXIT9(99号中断)
GICD->ISENABLER[3] |= (0x1<<3);
//GICD设置99号中断的优先级
GICD->IPRIORITYR[24] &= (~(0X1F<<27));
//选择CPU0处理当前中断
GICD->ITARGETSR[24] |= (0X1<<24);
//全局使能组0中断被转发到GICC
GICD->CTRL |= (0x1);
//设置中断的优先级掩码
GICC->PMR |= (0x1f<<3);
//允许组0中断被CPU处理
GICC->CTRL |= (0x1);
}
void key2_it_config()
{
//使能GPIOF时钟
RCC->MP_AHB4ENSETR |= (0x1<<5);
//设置PF9管脚为输入
GPIOF->MODER &= (~(0x3<<14));
//设置PF9作为exit9事件的输入
EXTI->EXTICR2 &= (~(0xFF<<24));
EXTI->EXTICR2 |= (0x05<<24);
//设置下降沿使能检测EXTI9事件
EXTI->FTSR1 |=(0x1<<7);
//设置EXTI9时间不屏蔽
EXTI->C1EMR1 |=(0x1<<7);
//GICD使能EXIT9(99号中断)
GICD->ISENABLER[3] |= (0x1<<1);
//GICD设置99号中断的优先级
GICD->IPRIORITYR[24] &= (~(0X1F<<11));
//选择CPU0处理当前中断
GICD->ITARGETSR[24] |= (0X1<<8);
//全局使能组0中断被转发到GICC
GICD->CTRL |= (0x1);
//设置中断的优先级掩码
GICC->PMR |= (0x1f<<3);
//允许组0中断被CPU处理
GICC->CTRL |= (0x1);
}
void key3_it_config()
{
//使能GPIOF时钟
RCC->MP_AHB4ENSETR |= (0x1<<5);
//设置PF9管脚为输入
GPIOF->MODER &= (~(0x3<<16));
//设置PF9作为exit9事件的输入
EXTI->EXTICR2 &= (~(0xFF));
EXTI->EXTICR2 |= (0x05);
//设置下降沿使能检测EXTI9事件
EXTI->FTSR1 |=(0x1<<8);
//设置EXTI9时间不屏蔽
EXTI->C1EMR1 |=(0x1<<8);
//GICD使能EXIT9(99号中断)
GICD->ISENABLER[3] |= (0x1<<2);
//GICD设置99号中断的优先级
GICD->IPRIORITYR[24] &= (~(0X1F<<19));
//选择CPU0处理当前中断
GICD->ITARGETSR[24] |= (0X1<<16);
//全局使能组0中断被转发到GICC
GICD->CTRL |= (0x1);
//设置中断的优先级掩码
GICC->PMR |= (0x1f<<3);
//允许组0中断被CPU处理
GICC->CTRL |= (0x1);
}
do_irq.c
#include "key_it.h"
#include"led.h"
extern void printf(const char *fmt, ...);
unsigned int i = 0;
void do_irq(void)
{
int irqno = (GICC->IAR & 0x3ff);
switch (irqno)
{
case 99: //key1
led1_on();
printf("KEY1_INT\n");
//清除GICD的中断排队标志
GICD->ICPENDR[3] |= (0x1<<3);
//清除exti的事件触发标志
EXTI->FPR1 |= (0x1 << 9);
break;
case 97: //key2
led2_on();
printf("KEY2_INT\n");
//清除GICD的中断排队标志
GICD->ICPENDR[3] |= (0x1<<1);
//清除exti的事件触发标志
EXTI->FPR1 |= (0x1 << 7);
break;
case 98: //key3
led3_on();
printf("KEY3_INT\n");
//清除GICD的中断排队标志
GICD->ICPENDR[3] |= (0x1<<2);
//清除exti的事件触发标志
EXTI->FPR1 |= (0x1 << 8);
break;
default:
break;
}
//清除中断号
GICC->EOIR = irqno;
}
main.c
#include"led.h"
#include"key_it.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();
while(1)
{
key1_it_config();
key2_it_config();
key3_it_config();
}
return 0;
}