arm 写一段按键中断代码
main.c
#include "uart.h"
#include "key_it.h"
int main()
{char c;char *s;uart4_init();//串口初始化//中断初始化key_it_config();while(1){//保证主程序不结束}return 0;
}
src/key_it.c
#include"key_it.h"void key_it_config()
{//RCC使能GPIOF时钟RCC->MP_AHB4ENSETR |= (0x1<<5);//设置PF9 PF7 PF8GPIO输入//PF9GPIOF->MODER &= (~(0x3<<18));//PF8GPIOF->MODER &= (~(0x3<<16));//pf7GPIOF->MODER &= (~(0x3<<14));//设置PF9 PF7 PF8产生EXTI事件 EXTI_EXTICRx//pf7EXTI->EXTICR2 &=(~(0xFF)<<24);EXTI->EXTICR2 |= (0x5<<24);//pf8EXTI->EXTICR3 &=(~(0xFF)<<0);EXTI->EXTICR3 |= (0x5<<0);//pf9EXTI->EXTICR3 &=(~(0xFF)<<8);EXTI->EXTICR3 |= (0x5<<8);//设置事件触发方式为下降沿触发EXTI_FTSR1//pf7EXTI->FTSR1 |= (0x1<<7);//pf8EXTI->FTSR1 |= (0x1<<8);//pf9EXTI->FTSR1 |= (0x1<<9);//设置EXTI事件触发不屏蔽EXTI_IMR1//PF7EXTI->C1IMR1 |= (0x1<<7);//PF8EXTI->C1IMR1 |= (0x1<<8);//PF9EXTI->C1IMR1 |= (0x1<<9);//使能中断能转发到特定的CPU接口层GICD_ISENABLERx//PF7 97号中断GICD->ISENABLER[3] |= (0X1<<1); //PF78 98号中断GICD->ISENABLER[3] |= (0X1<<2); //PF9 99号中断GICD->ISENABLER[3] |= (0X1<<3); //GICD_ISENABLERx设置中断优先级//PF7GICD->IPRIORITYR[24] &= (~(0X1F<<11)); //PF8GICD->IPRIORITYR[24] &= (~(0X1F<<19)); //PF9GICD->IPRIORITYR[24] &= (~(0X1F<<27)); //设置当前中断被转发到哪一个CPU处理GICD_ITARGETSRx//pf7 97中断GICD->ITARGETSR[24] &= (~(0X3<<8));GICD->ITARGETSR[24] |= (0X1<<8);//pf8 98中断GICD->ITARGETSR[24] &= (~(0X3<<16));GICD->ITARGETSR[24] |= (0X1<<16);//pf9 99中断GICD->ITARGETSR[24] &= (~(0X3<<24));GICD->ITARGETSR[24] |= (0X1<<24);//使能组0转发中断GICD_CTLRGICD->CTRL |= 0X1;//设置中断优先级掩码GICC_PMRGICC->PMR |= (0x1F<<3);//使能CPU可以处理组0转发的中断GICC_CTLRGICC->CTRL |= 0X1;}
include/key_it.h
#ifndef __KEY_IT_H__
#define __KEY_IT_H__
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_exti.h"
#include "stm32mp1xx_gic.h"
void key_it_config();#endif
src/do_irq.c
#include"key_it.h"
//按键3的配置
void key3_it_config()
{//RCC使能GPIOF时钟RCC->MP_AHB4ENSETR |= (0x1<<5);GPIOF->MODER &= (~(0x3<<16));EXTI->EXTICR3 &=(~(0xFF<<0));EXTI->EXTICR3 |= (0x5<<0);EXTI->FTSR1 |= (0x1<<8);EXTI->C1IMR1 |= (0x1<<8);GICD->ISENABLER[3] |= (0X1<<2); GICD->IPRIORITYR[24] &= (~(0X1F<<19)); GICD->ITARGETSR[24] &= (~(0X3<<16));GICD->ITARGETSR[24] |= (0X1<<16);
}
//按键1 按键2以及GICC层的配置
void key_it_config()
{//RCC使能GPIOF时钟RCC->MP_AHB4ENSETR |= (0x1<<5);//设置PF9 PF7 PF8GPIO输入//PF9GPIOF->MODER &= (~(0x3<<18));//PF8// GPIOF->MODER &= (~(0x3<<16));//pf7GPIOF->MODER &= (~(0x3<<14));//设置PF9 PF7 PF8产生EXTI事件 EXTI_EXTICRx//pf7EXTI->EXTICR2 &=(~(0xFF<<24));EXTI->EXTICR2 |= (0x5<<24);//pf8// EXTI->EXTICR3 &=(~(0xFF<<0));// EXTI->EXTICR3 |= (0x5<<0);//pf9EXTI->EXTICR3 &=(~(0xFF<<8));EXTI->EXTICR3 |= (0x5<<8);//设置事件触发方式为下降沿触发EXTI_FTSR1//pf7EXTI->FTSR1 |= (0x1<<7);//pf8// EXTI->FTSR1 |= (0x1<<8);//pf9EXTI->FTSR1 |= (0x1<<9);//设置EXTI事件触发不屏蔽EXTI_IMR1//PF7EXTI->C1IMR1 |= (0x1<<7);//PF8// EXTI->C1IMR1 |= (0x1<<8);//PF9EXTI->C1IMR1 |= (0x1<<9);//使能中断能转发到特定的CPU接口层GICD_ISENABLERx//PF7 97号中断GICD->ISENABLER[3] |= (0X1<<1); //PF78 98号中断// GICD->ISENABLER[3] |= (0X1<<2); //PF9 99号中断GICD->ISENABLER[3] |= (0X1<<3); // GICD_ISENABLERx设置中断优先级//PF7GICD->IPRIORITYR[24] &= (~(0X1F<<11)); //PF8// GICD->IPRIORITYR[24] &= (~(0X1F<<19)); //PF9GICD->IPRIORITYR[24] &= (~(0X1F<<27)); //设置当前中断被转发到哪一个CPU处理GICD_ITARGETSRx//pf7 97中断GICD->ITARGETSR[24] &= (~(0X3<<8));GICD->ITARGETSR[24] |= (0X1<<8);//pf8 98中断// GICD->ITARGETSR[24] &= (~(0X3<<16));// GICD->ITARGETSR[24] |= (0X1<<16);//pf9 99中断GICD->ITARGETSR[24] &= (~(0X3<<24));GICD->ITARGETSR[24] |= (0X1<<24);//使能组0转发中断GICD_CTLRGICD->CTRL |= 0X1;//设置中断优先级掩码GICC_PMRGICC->PMR |= (0x1F<<3);//使能CPU可以处理组0转发的中断GICC_CTLRGICC->CTRL |= 0X1;}