do_irq_c
cs
#include"key_it.h"
extern void printf(const char *fmt, ...);
unsigned int i = 0;
void do_irq(void)
{
//先获取中断号
unsigned int irqno=(GICC->IAR &0x3ff);
switch(irqno)
{
case 99://按键1中断
//进行中断处理
printf("do_key1_int\n");
//清除GICD中的中断排队标志
GICD->ICPENDR[3] |= (0x1<<3);
//清除EXTI中的事件挂起标志
EXTI->FPR1 |= (0x1<<9);
break;
case 98://按键2中断
//进行中断处理
printf("do_key2_int\n");
//清除GICD中的中断排队标志
GICD->ICPENDR[3] |= (0x1<<2);
//清除EXTI中的事件挂起标志
EXTI->FPR1 |= (0x1<<8);
break;
case 97://按键3中断
//进行中断处理
printf("do_key3_int\n");
//清除GICD中的中断排队标志
GICD->ICPENDR[3] |= (0x1);
//清除EXTI中的事件挂起标志
EXTI->FPR1 |= (0x1<<7);
break;
}
//清除IAR保存的中断号
GICC->EOIR = irqno;
}
key_it.c
cs
#include"key_it.h"
#include"gpio.h"
//按键1中的中断配置
void key1_config()
{
//使能GPIOF外设时钟
RCC->MP_AHB4ENSETR |= (0x1<<5);
//将PF9设置为输入
GPIOF->MODER &= (~(0X3<<18));
//设置PF9生成EXTI9事件
EXTI->EXTICR3 &= (~(0xff<<8));
EXTI->EXTICR3 |= (0X05<<8);
//设置下降沿检测EXTI9事件
EXTI->FTSR1 |= (0X1<<9);
//取消EXTI9的屏蔽
EXTI->C1IMR1 |= (0X1<<9);
//使能99号中断
GICD->ISENABLER[3] |= (0x1<<3);
//设置使能99号中断的优先级
GICD->IPRIORITYR[24] &= (~(0X1f<<27));
//设置99号中断目标处理器
GICD->ITARGETSR[24] |= (0X1<<24);
//全局使能组0中断被转发到GICC层
GICD->CTRL |= 0X1;
//设置中断优先级掩码
GICC->PMR |= (0X1f<<3);
//全局使能组0中断转发给处理器
GICC->CTRL |= (0X1);
}
//按键2中的中断配置
void key2_config()
{
//使能GPIOF外设时钟
RCC->MP_AHB4ENSETR |= (0x1<<5);
//将PF8设置为输入
GPIOF->MODER &= (~(0X3<<16));
//设置PF8生成EXTI8事件
EXTI->EXTICR3 &= (~(0xff));
EXTI->EXTICR3 |= (0X05);
//设置下降沿检测EXTI8事件
EXTI->FTSR1 |= (0X1<<8);
//取消EXTI8的屏蔽
EXTI->C1IMR1 |= (0X1<<8);
//使能98号中断
GICD->ISENABLER[3] |= (0x1<<2);
//设置使能98号中断的优先级
GICD->IPRIORITYR[24] &= (~(0X1f<<19));
//设置98号中断目标处理器
GICD->ITARGETSR[24] |= (0X1<<16);
//全局使能组0中断被转发到GICC层
GICD->CTRL |= (0X1<<0);
//设置中断优先级掩码
GICC->PMR &= (~(0X1f<<3));
GICC->PMR |= (0X6<<3);
//全局使能组0中断转发给处理器
GICC->CTRL |= (0X1);
}
//按键3中的中断配置
void key3_config()
{
//使能GPIOF外设时钟
RCC->MP_AHB4ENSETR |= (0x1<<5);
//将PF7设置为输入
GPIOF->MODER &= (~(0X3<<14));
//设置PF7生成EXTI7事件
EXTI->EXTICR2 &= (~(0xff<<24));
EXTI->EXTICR2 |= (0X5<<24);
//设置下降沿检测EXTI7事件
EXTI->FTSR1 |= (0X1<<7);
//取消EXTI7的屏蔽
EXTI->C1IMR1 |= (0X1<<7);
//使能97号中断
GICD->ISENABLER[3] |= (0x1<<1);
//设置使能97号中断的优先级
GICD->IPRIORITYR[24] &= (~(0X1f<<11));
//设置97号中断目标处理器
GICD->ITARGETSR[24] |= (0X1<<8);
//全局使能组0中断被转发到GICC层
GICD->CTRL |= (0X1<<0);
//设置中断优先级掩码
GICC->PMR &= (~(0x1f<<3));
GICC->PMR |= (0X5<<3);
//全局使能组0中断转发给处理器
GICC->CTRL |= (0X1<<0);
}
key_it.h
cs
#ifndef _KEY_IT_H_
#define _KEY_IT_H_
#include "gpio.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_exti.h"
#include "stm32mp1xx_gic.h"
void key1_config();
void key2_config();
void key3_config();
#endif
main.c
cs
#include"key_it.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<2000;j++)
{
}
}
}
void LED_init()
{
RCC_AHB4_ENSETR |= (0x3<<4);
gpio_init_t init= {OUTPUT,PP,LOW,NO_PUPD};
//LED初始化
hal_gpio_init(GPIOE,&init,GPIO_PIN_10); //led1
hal_gpio_init(GPIOF,&init,GPIO_PIN_10);//led2
hal_gpio_init(GPIOE,&init,GPIO_PIN_8);//led3
}
int main()
{
LED_init();
key1_config();
key2_config();
key3_config();
while(1)
{
printf("do_main\n");
delay_ms(1000);
}
return 0 ;
}