include
#ifndef __KET_IT_H__
#define __KEY_IT_H__
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gic.h"
#include "stm32mp1xx_exti.h"
void key1_it_config();
void key2_it_config();
void key3_it_config();
#endif
test.c
#include "key_it.h"
//key1 PF9 99中断号 中断事件9
//key2 PF7 97中断号 中断事件7
//key3 PF8 98中断号 中断事件8
void key1_it_config()
{
//设置GPIOF时钟
RCC->MP_AHB4ENSETR |= (0x1<<5);
//输入
GPIOF->MODER &= (~(0x3<<18));
//设置PF9产生EXIT9事件
EXTI->EXTICR3 &= (~(0xff<<8));
EXTI->EXTICR3 |= (0x05<<8);
//设置下降沿检测
EXTI->FTSR1 |= (0x1<<9);
//设置中断不屏蔽 可以被转到GIC
EXTI->C1IMR1 |= (0x1<<9);
//将PF9中断保存在0组 GIC接受 使能
GICD->ISENABLER[3] |= (0x1<<3);
//设置中断优先级
GICD->IPRIORITYR[24] &= (0x1f<<27);
GICD->IPRIORITYR[24] |= (0x0<<27);
//设置99号中断可以被CPU0处
GICD->ITARGETSR[24] &= (~(0x3<<24));
GICD->ITARGETSR[24] |= (0x1<<24);
//将99号中断转发GICC层
GICD->CTRL |=0x1;
//设置中断优先级掩码
GICC->PMR |=(0x1f<<3);
//允许中断发送cpu
GICC->CTRL |= 0x1;
}
//key2 PF7 97中断号 中断事件7
void key2_it_config()
{
//输入
GPIOF->MODER &= (~(0x3<<14));
//设置PF7产生EXIT9事件
EXTI->EXTICR2 &= (~(0xff<<24));
EXTI->EXTICR2 |= (0x05<<24);
//设置下降沿检测
EXTI->FTSR1 |= (0x1<<7);
//设置中断不屏蔽 可以被转到GIC
EXTI->C1IMR1 |= (0x1<<7);
//将PF9中断保存在0组 GIC接受 使能
GICD->ISENABLER[3] |= (0x1<<1);
//设置中断优先级
GICD->IPRIORITYR[24] &= (0x1f<<11);
GICD->IPRIORITYR[24] |= (0x0<<11);
//设置99号中断可以被CPU0处
GICD->ITARGETSR[24] &= (~(0x3<<8));
GICD->ITARGETSR[24] |= (0x1<<8);
//将99号中断转发GICC层
GICD->CTRL |=0x1;
//设置中断优先级掩码
GICC->PMR |=(0x1f<<3);
//允许中断发送cpu
GICC->CTRL |= 0x1;
}
//key3 PF8 98中断号 中断事件8
void key3_it_config()
{
//输入
GPIOF->MODER &= (~(0x3<<16));
//设置PF7产生EXIT9事件
EXTI->EXTICR3 &= (~0xff);
EXTI->EXTICR3 |= 0x05;
//设置下降沿检测
EXTI->FTSR1 |= (0x1<<8);
//设置中断不屏蔽 可以被转到GIC
EXTI->C1IMR1 |= (0x1<<8);
//将PF9中断保存在0组 GIC接受 使能
GICD->ISENABLER[3] |= (0x1<<2);
//设置中断优先级
GICD->IPRIORITYR[24] &= (0x1f<<19);
GICD->IPRIORITYR[24] |= (0x0<<19);
//设置99号中断可以被CPU0处
GICD->ITARGETSR[24] &= (~(0x3<<16));
GICD->ITARGETSR[24] |= (0x1<<16);
//将99号中断转发GICC层
GICD->CTRL |=0x1;
//设置中断优先级掩码
GICC->PMR |=(0x1f<<3);
//允许中断发送cpu
GICC->CTRL |= 0x1;
}
do_lrq.c
#include "key_it.h"
#include "myled.h"
extern void printf(const char *fmt, ...);
unsigned int i = 0;
int flag1 = 0, flag2 = 0, flag3 = 0;
void do_irq(void)
{
int i;
i = GICC->IAR & 0x3ff;
switch (i)
{
case 99:
{
if (flag1 == 0)
{
led1_off();
flag1=1;
}
else
{
led1_on();
flag1=0;
}
printf("key_1\n");
EXTI->FPR1 |= (0X1 << 9);
GICD->ICPENDR[3] = (0X1 << 3);
break;
}
case 97:
{
if (flag2 == 0)
{
led2_off();
flag2=1;
}
else
{
led2_on();
flag2=0;
}
printf("key_2\n");
EXTI->FPR1 |= (0X1 << 9);
GICD->ICPENDR[3] = (0X1 << 1);
break;
}
case 98:
{
if (flag3 == 0)
{
led3_off();
flag3=1;
}
else
{
led3_on();
flag3=0;
}
printf("key_3\n");
EXTI->FPR1 |= (0X1 << 9);
GICD->ICPENDR[3] = (0X1 << 2);
break;
}
}
GICC->EOIR = i;
}
main.c
#include "myled.h"
#include "key_it.h"
int main()
{
key1_it_config();
key2_it_config();
key3_it_config();
all_led_init();
led1_on();
led2_on();
led3_on();
while (1)
{
}
return 0;
}