效果图:
源码:
#include "key.h"
void hal_key1_rcc_gpio_init()
{
// 使能GPIOF组
RCC->MP_AHB4ENSETR |= (0x1 << 5);
// 设置引脚位输入模式
GPIOF->MODER &= (~(0X3 << 18));
GPIOF->MODER &= (~(0X3 << 16));
GPIOF->MODER &= (~(0X3 << 14));
}
void hal_key1_exti_init()
{
// 使能EXTI
EXTI->EXTICR3 &= (~(0XFF << 8));
EXTI->EXTICR3 |= (0X5 << 8);
// 使能EXTI
EXTI->EXTICR2 &= (~(0XFF << 24));
EXTI->EXTICR2 |= (0X5 << 24);
// 使能EXTI
EXTI->EXTICR3 &= (~(0XFF));
EXTI->EXTICR3 |= (0X5);
// 设置为下降沿触发
EXTI->FTSR1 |= (0X7 << 7);
// 设置不屏蔽
EXTI->C1IMR1 |= (0X7 << 7);
// 清除中断挂起标志位
EXTI->FPR1 |= (0X1 << 9);
}
void hal_key1_gic_init()
{
/*--------GCID---------*/
// gicd组使能
GICD->CTRL |= (0X1);
// 对应中断组使能
GICD->ISENABLER[3] |= (0X1 << 3);
GICD->ISENABLER[3] |= (0X1 << 2);
GICD->ISENABLER[3] |= (0X1 << 1);
// 中断优先级
GICD->IPRIORITYR[24] &= (~(0X1F << 27));
GICD->IPRIORITYR[24] |= (0X0A << 27);
// 中断优先级
GICD->IPRIORITYR[24] &= (~(0X1F << 11));
GICD->IPRIORITYR[24] |= (0X0A << 11);
// 中断优先级
GICD->IPRIORITYR[24] &= (~(0X1F << 19));
GICD->IPRIORITYR[24] |= (0X0A << 19);
// 中断分配给哪个cpu
GICD->ITARGETSR[24] &= (~(0X3 << 24));
GICD->ITARGETSR[24] |= (0X1 << 24);
// 中断分配给哪个cpu
GICD->ITARGETSR[24] &= (~(0X3 << 8));
GICD->ITARGETSR[24] |= (0X1 << 8);
// 中断分配给哪个cpu
GICD->ITARGETSR[24] &= (~(0X3 << 16));
GICD->ITARGETSR[24] |= (0X1 << 16);
// 清除挂起位标志
// GICD->ICPENDR[2] |= (0X1 << 3);
// GICD->ICPENDR[1] |= (0X1 << 1);
// GICD->ICPENDR[2] |= (0X1 << 2);
/*--------GICC---------*/
GICC->CTRL |= (0X1);
GICC->PMR &= (~(0X1F << 3));
GICC->PMR |= (0X0F << 3);
}
#include "key.h"
extern void printf(const char *fmt, ...);
unsigned int i = 0;
void do_irq(void) // 中断处理函数
{
// printf("have someone asshole interrupt!\n");
unsigned int irq_number = GICC->IAR & 0x3FF;
// 根据中断号调用对应的中断处理函数
switch (irq_number)
{
case 99:
printf("key1 down!!!key1 ID = %d\n", irq_number);
EXTI->FPR1 |= (0X1 << 9);
GICD->ICPENDR[3] |= (0X1 << 3);
GICC->EOIR = irq_number;
break;
case 98:
printf("key3 down!!!key3 ID = %d\n", irq_number);
EXTI->FPR1 |= (0X1 << 8);
GICD->ICPENDR[3] |= (0X1 << 2);
GICC->EOIR = irq_number;
break;
case 97:
printf("key2 down!!!key2 ID = %d\n", irq_number);
EXTI->FPR1 |= (0X1 << 7);
GICD->ICPENDR[3] |= (0X1 << 1);
GICC->EOIR = irq_number;
break;
default:
// 处理其他中断或错误情况
break;
}
// 清除中断号
// EXTI->FPR1 |= (0X1 << 9);
//GICD->ICPENDR[3] |= (0X1 << 3);
//GICC->EOIR = irq_number;
}
#include "key.h"
int main()
{
hal_key1_exti_init();
hal_key1_gic_init();
hal_key1_rcc_gpio_init();
while (1)
{
}
return 0;
}
#ifndef __KEY_H__
#define __KEY_H__
#include "stm32mp1xx_gic.h"
#include "stm32mp1xx_exti.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
void hal_key1_rcc_gpio_init();
void hal_key1_exti_init();
void hal_key1_gic_init();
#endif