效果:
key.h
#ifndef __KEY_H__
#define __KEY_H__
#define RCC_MP_AHB4ENSETR (*(volatile unsigned int *)0x50000A28)
//初始化EXTI控制器
void hal_exit_init();
//初始化GICD控制器
void hal_gicd_init();
//初始化GICC控制器
void hal_gicc_init();
#endif
key.c
#include"key.h"
#include "/home/ubuntu/2022101/ARM/04_key_c/common/include/stm32mp1xx_gpio.h"
#include "/home/ubuntu/2022101/ARM/04_key_c/common/include/stm32mp1xx_gic.h"
#include "/home/ubuntu/2022101/ARM/04_key_c/common/include/stm32mp1xx_exti.h"
//初始化EXIT控制器
void hal_exit_init()
{
/**********RCC章节初始化*********/
//gpio-f组控制器初始化
RCC_MP_AHB4ENSETR|=(0x1<<5);
/**********GPIO章节初始化*********/
//设置MODER寄存器为输入模式
GPIOF->MODER&=(~(0x3<<18)); //设置key1->pf9;
GPIOF->MODER&=(~(0x3<<14)); //设置key2->pf7;
GPIOF->MODER&=(~(0x3<<16)); //设置key3->pf8;
/**********EXIT章节初始化*********/
/**********EXTI->EXTICR3初始化**************/
//key1->PF9初始化
EXTI->EXTICR3&=(~(0xff)<<8);
EXTI->EXTICR3|=(0x01)<<8;
EXTI->EXTICR3|=(0x01)<<10;
//key2->PF7初始化
EXTI->EXTICR2&=(~(0x0f)<<24);
EXTI->EXTICR2|=(0x01)<<24;
EXTI->EXTICR2|=(0x01)<<26;
//key3->PF8初始化
EXTI->EXTICR3&=(~(0xff)<<0);
EXTI->EXTICR3|=(0x01)<<0;
EXTI->EXTICR3|=(0x01)<<2;
/**********EXTI->FTSR1初始化**************/
//key1->pf9初始化
EXTI->FTSR1|=(0x1<<9);
//key2->pf7初始化
EXTI->FTSR1|=(0x1<<7);
//key3->pf8初始化
EXTI->FTSR1|=(0x1<<8);
/**********EXTI->IMR1初始化**************/
//key1->pf9初始化
EXTI->C1IMR1|=(0x1<<9);
//key2->pf7初始化
EXTI->C1IMR1|=(0x1<<7);
//key3->pf8初始化
EXTI->C1IMR1|=(0x1<<8);
}
//初始化GICD控制器
void hal_gicd_init()
{
//设置CRLR
GICD->CTRL|=(0x1<<0);
//设置ISENABLERx寄存器
GICD->ISENABLER[3]|=(0x1<<3); //key1
GICD->ISENABLER[3]|=(0x1<<1); //key2
GICD->ISENABLER[3]|=(0x1<<2); //key3
//设置GICD_IPRIORITYR
GICD->IPRIORITYR[24]|=(0x1<<27); //key1
GICD->IPRIORITYR[24]|=(0x1<<11); //key2
GICD->IPRIORITYR[24]|=(0x1<<19); //key3
//设置GICD_ITARGETSR
GICD->ITARGETSR[24]&=(~(0x3<<24)); //分配给cpu0
GICD->ITARGETSR[24]|=(0x1<<24); //key1
GICD->ITARGETSR[24]&=(~(0x3<<8)); //分配给cpu0
GICD->ITARGETSR[24]|=(0x1<<8); //key2
GICD->ITARGETSR[24]&=(~(0x3<<16)); //分配给cpu0
GICD->ITARGETSR[24]|=(0x1<<16); //key3
}
//初始化GICC控制器
void hal_gicc_init()
{
//设置CTRL
GICC->CTRL|=(0x1<<0);
//设置GICC_PMR
GICC->PMR|=(0x1<<3);
GICC->PMR|=(0x1<<4);
GICC->PMR|=(0x1<<5);
}
do_irq.c
#include "/home/ubuntu/2022101/ARM/04_key_c/common/include/stm32mp1xx_gic.h"
#include "/home/ubuntu/2022101/ARM/04_key_c/common/include/stm32mp1xx_exti.h"
extern void printf(const char *fmt, ...);
unsigned int i = 0;
void do_irq(void)
{
//获取终端号
int GICC_num=GICC->IAR;
//判断终端号
switch (GICC_num)
{
case 99: //key1
printf("i'am key1,我要玩亚索\n");
//清除EXTI
EXTI->FPR1|=(0x1<<9); //key1
EXTI->FPR1|=(0x1<<7); //key2
EXTI->FPR1|=(0x1<<8); //key3
//清除终端挂起标志位,清除终端号
//清除GICD
GICD->ICPENDR[3]|=(0x1<<3); //key1
GICD->ICPENDR[1]|=(0x1<<1); //key2
GICD->ICPENDR[2]|=(0x1<<2); //key3
//清除GICC
GICC->EOIR=GICC_num;
break;
case 97: //key2
printf("i'am key2,我要玩亚索\n");
//清除EXTI
EXTI->FPR1|=(0x1<<9); //key1
EXTI->FPR1|=(0x1<<7); //key2
EXTI->FPR1|=(0x1<<8); //key3
//清除终端挂起标志位,清除终端号
//清除GICD
GICD->ICPENDR[3]|=(0x1<<3); //key1
GICD->ICPENDR[1]|=(0x1<<1); //key2
GICD->ICPENDR[2]|=(0x1<<2); //key3
//清除GICC
GICC->EOIR=GICC_num;
break;
case 98:
printf("i'am key3,我要玩亚索\n");
//清除EXTI
EXTI->FPR1|=(0x1<<9); //key1
EXTI->FPR1|=(0x1<<7); //key2
EXTI->FPR1|=(0x1<<8); //key3
//清除终端挂起标志位,清除终端号
//清除GICD
GICD->ICPENDR[3]|=(0x1<<3); //key1
GICD->ICPENDR[1]|=(0x1<<1); //key2
GICD->ICPENDR[2]|=(0x1<<2); //key3
//清除GICC
GICC->EOIR=GICC_num;
break;
}
}
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_exit_init();
hal_gicd_init();
hal_gicc_init();
while(1)
{
}
return 0;
}