cortex-A7核按键中断实验
fun.h
#ifndef __FUN_H__
#define __FUN_H__
#include "stm32mp1xx_uart.h" // 调用UART封装库
#include "stm32mp1xx_gpio.h" // 调用GPIO封装库
#include "stm32mp1xx_rcc.h" // 调用RCC封装库
#include "stm32mp1xx_exti.h" //调用EXTI封装库
#include "stm32mp1xx_gic.h" //调用GIC封装库
// GPIO
//===========================================================================================
//***********GPIO引脚************
#define GPIO_PIN_0 0
#define GPIO_PIN_1 1
#define GPIO_PIN_2 2
#define GPIO_PIN_3 3
#define GPIO_PIN_4 4
#define GPIO_PIN_5 5
#define GPIO_PIN_6 6
#define GPIO_PIN_7 7
#define GPIO_PIN_8 8
#define GPIO_PIN_9 9
#define GPIO_PIN_10 10
//*******中断*******
void hal_exti_pf9_init();
void hal_gicd_pf9_init();
void hal_gicc_pf9_init();
//*******中断*******
#endif
text.c
#include "fun.h"
//*******中断*******
void hal_exti_pf9_init()
{
// RCC章节初始化
RCC->MP_AHB4ENSETR |= (0x1 << 5);
// GPIO章节初始化
GPIOF->MODER &= (~(0x3 << 18));
GPIOF->MODER &= (~(0x3 << 14));
GPIOF->MODER &= (~(0x3 << 16));
// exti章节初始化
// EXTI_EXTICR(3\2) 外部中断选择寄存器 GPIO选择和哪一个EXTI组控制器进行连接
EXTI->EXTICR3 &= (~(0xFF << 8));
EXTI->EXTICR3 |= (0x05 << 8);
EXTI->EXTICR2 &= (~(0xFF << 24));
EXTI->EXTICR2 |= (0x05 << 24);
EXTI->EXTICR3 &= (~(0xFF << 0));
EXTI->EXTICR3 |= (0x05 << 0);
// EXTI_FTSR1 EXTI下降沿触发选择寄存器 设置EXTI编号为下降沿触发
EXTI->FTSR1 |= (0x1 << 9);
EXTI->FTSR1 |= (0x1 << 7);
EXTI->FTSR1 |= (0x1 << 8);
// EXTI_IMR1 EXTI中断屏蔽寄存器 设置EXTI信号是否转发到GIC层
EXTI->C1IMR1 |= (0x1 << 9);
EXTI->C1IMR1 |= (0x1 << 7);
EXTI->C1IMR1 |= (0x1 << 8);
}
void hal_gicd_pf9_init()
{
// GICD层控制寄存器 使能GICD层组0中断:GICD_CTLR[0] = 1
GICD->CTRL |= (0x1 << 0);
// 中断设置使能寄存器 GICD_ISENABLER3[3] = 1 [1] = 1 [2] = 1
GICD->ISENABLER[3] |= (0x1 << 3);
GICD->ISENABLER[3] |= (0x1 << 1);
GICD->ISENABLER[3] |= (0x1 << 2);
// GICD层中断优先级寄存器
GICD->IPRIORITYR[24] &= (~(0x1F << 27));
GICD->IPRIORITYR[24] |= (1 << 27);
GICD->IPRIORITYR[24] &= (~(0x1F << 11));
GICD->IPRIORITYR[24] |= (1 << 11);
GICD->IPRIORITYR[24] &= (~(0x1F << 19));
GICD->IPRIORITYR[24] |= (1 << 19);
// GICD层中断目标分配寄存器 GICD_ITARGETSR24[25:24] [9:8] [17:16]
GICD->ITARGETSR[24] &= (~(0x3 << 24));
GICD->ITARGETSR[24] |= (0x1 << 24);
GICD->ITARGETSR[24] &= (~(0x3 << 8));
GICD->ITARGETSR[24] |= (0x1 << 8);
GICD->ITARGETSR[24] &= (~(0x3 << 16));
GICD->ITARGETSR[24] |= (0x1 << 16);
}
void hal_gicc_pf9_init()
{
// GICC层控制寄存器 GICC_CTLR[0] = 1
GICC->CTRL |= (0x1 << 0);
// GICC层中断优先级屏蔽寄存器
GICC->PMR &= (~(0x1F << 3));
GICC->PMR |= (0x2 << 3);
}
//*******中断*******
do_irq.c
#include "fun.h"
extern void printf(const char *fmt, ...);
unsigned int i = 0;
void do_irq(void)
{
// 获取中断号 IAR
i = GICC->IAR & 0x3ff;
// 判断获取的中断号, 进行打印
switch (i)
{
case 99:
printf("key1\n");
// 清除对应EXTI层挂起的标志位
// EXTI_FPR1 EXTI下降沿触挂起寄存器 清除EXTI层中断挂起标志
EXTI->FPR1 &= (~(0x1 << 9));
EXTI->FPR1 |= (0x1 << 9);
// 清除对应GICD层挂起的标志位
// 中断清除挂起寄存器 GICD_ICPENDR3[3]= 1
GICD->ICPENDR[3] &= (~(0x1 << 3));
GICD->ICPENDR[3] |= (0x1 << 3);
break;
case 98:
printf("key3\n");
// 清除对应EXTI层挂起的标志位
// EXTI_FPR1 EXTI下降沿触挂起寄存器 清除EXTI层中断挂起标志
EXTI->FPR1 &= (~(0x1 << 8));
EXTI->FPR1 |= (0x1 << 8);
// 清除对应GICD层挂起的标志位
// 中断清除挂起寄存器 GICD_ICPENDR3[2]= 1
GICD->ICPENDR[3] &= (~(0x1 << 2));
GICD->ICPENDR[3] |= (0x1 << 2);
break;
case 97:
printf("key2\n");
// 清除对应EXTI层挂起的标志位
// EXTI_FPR1 EXTI下降沿触挂起寄存器 清除EXTI层中断挂起标志
EXTI->FPR1 &= (~(0x1 << 7));
EXTI->FPR1 |= (0x1 << 7);
// 清除对应GICD层挂起的标志位
// 中断清除挂起寄存器 GICD_ICPENDR3[1]= 1
GICD->ICPENDR[3] &= (~(0x1 << 1));
GICD->ICPENDR[3] |= (0x1 << 1);
break;
}
// 清除对应GICC层挂起的标志位
GICC->EOIR |= i;
}
main.c
#include "fun.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_exti_pf9_init();
hal_gicd_pf9_init();
hal_gicc_pf9_init();
while (1)
{
}
return 0;
}