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()
{
//rcc初始化
RCC->MP_AHB4ENSETR |= (0x3 << 4);
//初始化按键GPIOF模式为输入模式
hal_key_gpio_init(GPIOF,GPIO_PIN_9);
hal_key_gpio_init(GPIOF,GPIO_PIN_8);
hal_key_gpio_init(GPIOF,GPIO_PIN_7);
//初始化EXTI设置中断触发为下降沿不屏蔽
hal_exti_init(EXTI,GPIO_PIN_8,DOWN);
hal_exti_init(EXTI,GPIO_PIN_7,DOWN);
hal_exti_init(EXTI,GPIO_PIN_9,DOWN);
//初始化GICC、GICD使能并设置优先级
hal_gic_init(TERMINAL_CODE_7,PIN_7);
hal_gic_init(TERMINAL_CODE_8,PIN_8);
hal_gic_init(TERMINAL_CODE_9,PIN_9);
//LED灯初始化为输出模式,推挽,低速,不上下拉
hal_led_init(GPIOE,GPIO_PIN_10);
hal_led_init(GPIOF,GPIO_PIN_10);
hal_led_init(GPIOE,GPIO_PIN_8);
while(1)
{
}
return 0;
}
key.c
#include "key.h"
//led灯使能初始化
void hal_led_init(gpio_t* gpios,unsigned int pin){
//引脚初始化
gpios->MODER &= (~(0x3 << pin*2));
gpios->MODER |= (0x1 << pin*2);
gpios->OTYPER &= (~(0x1 << pin));
gpios->OSPEEDR &= (~(0x3 << pin*2));
gpios->PUPDR &= (~(0x3 << pin*2));
}
//led开关
void hal_led_set(gpio_t* gpios,unsigned int GPIO_PIN){
//获取灯当前状态
unsigned int status = (gpios->ODR & (0x1 << GPIO_PIN)) >> GPIO_PIN;
//判断后取反
if(status == 1){
gpios->ODR &= (~(0x1 << GPIO_PIN));
}else{
gpios->ODR |= (0x1 << GPIO_PIN);
}
}
void hal_key_gpio_init(gpio_t* gpios,unsigned int pin){
//gpio初始化
gpios->MODER &= (~(0x3 << pin*2));
}
//对rcc gpio exti 进行初始化
void hal_exti_init(exti_t* extis,unsigned int pin,ftsr_t stat){
//exti初始化
//设置exti选择寄存器
if(pin == 9){
extis->EXTICR3 &=(~(0xff << 8));
extis->EXTICR3 |=(0x5 << 8);
}
if(pin == 7){
extis->EXTICR2 &=(~(0xff << 24));
extis->EXTICR2 |=(0x5 << 24);
}
if(pin == 8){
extis->EXTICR3 &=(~(0xff));
extis->EXTICR3 |=(0x5);
}
//设置中断触发为下降沿触发
extis->FTSR1 |= (stat << pin);
//设置中断不屏蔽
extis->C1IMR1 |= (stat << pin);
}
//对gic初始化
void hal_gic_init(unsigned int t_code,unsigned int pin){
//gicd初始化
//设置gicd层组控制器使能
GICD->CTRL |= (0x1);
//设置gicd层中断设置使能寄存器
GICD->ISENABLER[t_code/32] |=(0x1 << t_code%32);
//设置gicd层中断优先级寄存器
GICD->IPRIORITYR[t_code/4] &=(~(0x1f << ((t_code%4)*8+3)));
//设置gicd层中断目标分配寄存器
GICD->ITARGETSR[t_code/4] &=(~(0x3 << (t_code%4)*8));
GICD->ITARGETSR[t_code/4] |=(0x1 << (t_code%4)*8);
//gicc初始化
//设置gicc层组控制器使能
GICC->CTRL |=(0x1);
//设置gicc层中断优先级寄存器
GICC->PMR |= (pin << 3);
}
key.h
#ifndef __KEY_H__
#define __KEY_H__
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_gic.h"
#include "stm32mp1xx_exti.h"
#define GPIO_PIN_10 10
#define GPIO_PIN_9 9
#define GPIO_PIN_8 8
#define GPIO_PIN_7 7
#define PIN_9 9
#define PIN_8 8
#define PIN_7 7
#define TERMINAL_CODE_7 97
#define TERMINAL_CODE_8 98
#define TERMINAL_CODE_9 99
typedef enum{
UP,
DOWN,
}ftsr_t;
//对rcc gpio exti 进行初始化
void hal_exti_init(exti_t* extis,unsigned int pin,ftsr_t stat);
//对gic初始化
void hal_gic_init(unsigned int t_code,unsigned int pin);
//key gpio初始化
void hal_key_gpio_init(gpio_t* gpios,unsigned int pin);
//led灯使能初始化
void hal_led_init(gpio_t* gpios,unsigned int GPIO_PIN);
//led开关
void hal_led_set(gpio_t* gpios,unsigned int GPIO_PIN);
#endif
do_irq.c
#include "key.h"
extern void printf(const char *fmt, ...);
unsigned int i = 0;
void do_irq(void)
{
unsigned int num;
//获取中断号
num = (GICC->IAR & (0x3ff));
//判中断号
//99-----》key1 ----->printf
//98-----》key3 ----->printf
//97-----》key4 ----->printf
//清除对应挂起位
switch(num){
case 97:
//key2开关控制led灯2的亮灭
hal_led_set(GPIOF,GPIO_PIN_10);
printf("key2 press!!!!!!\n");
EXTI->FPR1 |=(0x1 << 7);
GICD->ICPENDR[3] |=(0x1 << 1);
break;
case 98:
//key3开关控制led灯3的亮灭
hal_led_set(GPIOE,GPIO_PIN_8);
printf("key3 press!!!!!!\n");
EXTI->FPR1 |=(0x1 << 8);
GICD->ICPENDR[3] |=(0x1 << 2);
break;
case 99:
//key1开关控制led灯1的亮灭
hal_led_set(GPIOE,GPIO_PIN_10);
printf("key1 press!!!!!!\n");
//清除exti层中断挂起的标志位
EXTI->FPR1 |=(0x1 << 9);
//清除gicd层中断挂起的标志位
GICD->ICPENDR[3] |=(0x1 << 3);
break;
}
//清除所获取到的中断号
GICC->EOIR = num;
}