实现三个按键的中断,现象和代码
include/key_inc.h:
#ifndef __KEY_INC_H__
#define __KEY_INC_H__
#include "stm32mp1xx_gic.h"
#include "stm32mp1xx_exti.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
void key1_it_config();
void key2_it_config();
void key3_it_config();
#endif
src/key_inc.c:
#include"key_inc.h"
void key1_it_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);
//设置EXTI9中断全局使能
GICD->ISENABLER[3] |= (0x1<<3);
//设置中断优先级
GICD->IPRIORITYR[24] &= (~(0x1f<<27));
//设置CPU0处理当前中断
GICD->ITARGETSR[24] |= (0x1<<24);
//设置组0中断可以被转发到GICC层
GICD->CTRL |=0X1;
//设置中断优先级掩码
GICC->PMR |= (0x1f<<3);
//设置组0中断交给指定处理器处理
GICC->CTRL |= 0x1;
}
void key2_it_config()
{
//使能GPIOF外设时钟
RCC->MP_AHB4ENSETR |= (0x1<<5);
//将PF7设置为输入模式
GPIOF->MODER &= (~(0x3<<14));
//设置由PF9管脚产生EXTI9事件
//设置由PF7管脚产生EXTI7事件
EXTI->EXTICR2 &= (~(0XFF<<24));
EXTI->EXTICR2|= (0X05<<24);
//通过下降沿检测EXTI7事件是否产生
EXTI->FTSR1 |= (0x1<<7);
//取消EXTI7中断屏蔽
EXTI->C1IMR1 |= (0x1<<7);
//设置EXTI7中断全局使能
GICD->ISENABLER[3] |= (0x1<<1);
//设置中断优先级
GICD->IPRIORITYR[24] &= (~(0x1f<<11));
//设置CPU0处理当前中断
GICD->ITARGETSR[24] &=( ~(0x3<<8));
GICD->ITARGETSR[24] |= (0x1<<8);
//设置组0中断可以被转发到GICC层
GICD->CTRL |=0X1;
//设置中断优先级掩码
//GICC->PMR |= (0x1f<<3);
//设置组0中断交给指定处理器处理
//GICC->CTRL |= 0x1;
}
void key3_it_config()
{
//使能GPIOF外设时钟
RCC->MP_AHB4ENSETR |= (0x1<<5);
//将PF7设置为输入模式
GPIOF->MODER &= (~(0x3<<16));
//设置由PF9管脚产生EXTI9事件
//设置由PF8管脚产生EXTI8事件
EXTI->EXTICR3 &= (~(0XFF<<0));
EXTI->EXTICR3|= (0X05<<0);
//通过下降沿检测EXTI8事件是否产生
EXTI->FTSR1 |= (0x1<<8);
//取消EXTI8中断屏蔽
EXTI->C1IMR1 |= (0x1<<8);
//设置EXTI8中断全局使能
GICD->ISENABLER[3] |= (0x1<<2);
//设置中断优先级
GICD->IPRIORITYR[24] &= (~(0x1f<<19));
//设置CPU0处理当前中断
GICD->ITARGETSR[24] &=( ~(0x3<<16));
GICD->ITARGETSR[24] |= (0x1<<16);
//设置组0中断可以被转发到GICC层
GICD->CTRL |=0X1;
}
src/do_irq.c:
#include "stm32mp1xx_gic.h"
#include "stm32mp1xx_exti.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:
printf("key1_intc\n");
//清除GICD中断排队标志
GICD->ICPENDR[3] |= (0X1<<3);
//清除EXTI中断触发标志
EXTI->FPR1 |= (0X1<<9);
break;
case 97:
printf("key2_initc\n");
//清除GICD中断排队标志
GICD->ICPENDR[3] |= (0X1<<1);
//清除EXTI中断触发标志
EXTI->FPR1 |= (0X1<<7);
break;
case 98:
printf("key3_initc\n");
//清除GICD中断排队标志
GICD->ICPENDR[3] |= (0X1<<2);
//清除EXTI中断触发标志
EXTI->FPR1 |= (0X1<<8);
break;
default:
break;
}
//清除IAR寄存器中的中断号
GICC->EOIR |= irqno;
}
main.c:
#include"uart.h"
#include"uart4.h"
#include"led.h"
#include"key_inc.h"
//封装比较函数
int strings_bijiao(const char* str1, const char* str2)
{
while(*str1 && *str2)
{
if *str1 != *str2)
{
//字符不相等
return 0;
}
str1++;
str2++;
}
if(*str1 == '\0' && *str2 == '\0')
{
//字符串相等
return 1;
}
else
{
//字符串长度不同,不相等
return 0;
}
}
//封装延时函数
void delay(int ms)
{
int i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<2000;j++);
}
}
int main()
{
//按键中断的初始化
key1_it_config();
key2_it_config();
key3_it_config();
while(1)
{
printf("in main pro\n");
delay(1000);
}
return 0;
}
现象: