完成Cortex-A7核按键中断实验
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()
{
gpio_init();
key1_exti_init();
key1_gic_init();
key2_exti_init();
key2_gic_init();
key3_exti_init();
key3_gic_init();
led1_init();
led2_init();
led3_init();
while(1);
return 0;
}
key.h:
#ifndef __KEY_H__
#define __KEY_H__
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_exti.h"
#include "stm32mp1xx_gic.h"
//GPIO章节初始化
void gpio_init();
//EXTI章节初始化
void key1_exti_init();
void key2_exti_init();
void key3_exti_init();
//GIC层章节初始化
void key1_gic_init();
void key2_gic_init();
void key3_gic_init();
//LED1初始化
void led1_init();
//LED1点亮
void led1_on();
//LED1熄灭
void led1_off();
//LED2初始化
void led2_init();
//LED2点亮
void led2_on();
//LED2熄灭
void led2_off();
//LED3初始化
void led3_init();
//LED3点亮
void led3_on();
//LED3熄灭
void led3_off();
#endif
key.c:
#include "key.h"
void gpio_init()
{
//RCC章节使能GPIOF MP_AHB4ENSETR[5]:1
RCC->MP_AHB4ENSETR |=(0x1<<5);
//GPIOF_MODER设置按键为输入模式 [19:18] [15:14] [17:16] 00
GPIOF->MODER &=(~(0x3<<18));
GPIOF->MODER &=(~(0x3<<14));
GPIOF->MODER &=(~(0x3<<16));
}
void key1_exti_init()
{
//EXTI_EXTICR3[15:8]:0x05
EXTI->EXTICR3 &=(~(0xff<<8));
EXTI->EXTICR3 |=(0x05<<8);
//EXTI_FTSR1[9] = 1
EXTI->FTSR1 |=(0x1<<9);
//EXTI_IMR1[9] = 1
EXTI->C1IMR1 |=(0x1<<9);
}
void key2_exti_init()
{
//EXTI_EXTICR2[31:24] = 0x05
EXTI->EXTICR2 &=(~(0xff<<24));
EXTI->EXTICR2 |=(0x05<<24);
//EXTI_FTSR1[7] = 1
EXTI->FTSR1 |=(0x1<<7);
//EXTI_IMR1[7] = 1
EXTI->C1IMR1 |=(0x1<<7);
}
void key3_exti_init()
{
//EXTI_EXTICR3[7:0] = 0x05
EXTI->EXTICR3 &=(~0xff);
EXTI->EXTICR3 |=(0x05);
//EXTI_FTSR1[8] = 1
EXTI->FTSR1 |=(0x1<<8);
//EXTI_IMR1[8] = 1
EXTI->C1IMR1 |=(0x1<<8);
}
void key1_gic_init()
{
//GICD
//使能GICD层GICD_CTLR[0] = 1
GICD->CTRL |=(0x1);
//GICD_ISENABLER3[3] = 1
GICD->ISENABLER[3] |=(0x1<<3);
//GICD_IPRIORITYR24[31:27]
//GICD层设置的中断优先级,需要比GICC层中断优先级高,中断的值设置的越小,优先级越高
GICD->IPRIORITYR[24] |=(0x1<<27);
//中断目标分配寄存器,GICD_ITARGETSR24[25:24] 0x01,分配给cpu0
GICD->ITARGETSR[24] &=(~(0x3<<24));
GICD->ITARGETSR[24] |=(0x1<<24);
//GICC
//CTLR[0] = 1 -----> GICC层使能
GICC->CTRL |=(0x1);
//GICC_PMR[7:3]设置GICC中断优先级屏蔽寄存器
GICC->PMR |=(0x1f<<3);
}
void key2_gic_init()
{
//GICD
//使能GICD层GICD_CTLR[0] = 1
GICD->CTRL |=(0x1);
//GICD_ISENABLER3[1] = 1
GICD->ISENABLER[3] |=(0x1<<1);
//GICD_IPRIORITYR24[15:11]
//GICD层设置的中断优先级,需要比GICC层中断优先级高,中断的值设置的越小,优先级越高
GICD->IPRIORITYR[24] |=(0x1<<11);
//中断目标分配寄存器,GICD_ITARGETSR24[9:8] 0x01,分配给cpu0
GICD->ITARGETSR[24] &=(~(0x3<<8));
GICD->ITARGETSR[24] |=(0x1<<8);
//GICC
//CTLR[0] = 1 -----> GICC层使能
GICC->CTRL |=(0x1);
//GICC_PMR[7:3]设置GICC中断优先级屏蔽寄存器
GICC->PMR |=(0x1f<<3);
}
void key3_gic_init()
{
//GICD
//使能GICD层GICD_CTLR[0] = 1
GICD->CTRL |=(0x1);
//GICD_ISENABLER3[2] = 1
GICD->ISENABLER[3] |=(0x1<<2);
//GICD_IPRIORITYR24[23:19]
//GICD层设置的中断优先级,需要比GICC层中断优先级高,中断的值设置的越小,优先级越高
GICD->IPRIORITYR[24] |=(0x1<<19);
//中断目标分配寄存器,GICD_ITARGETSR24[17:16] 0x01,分配给cpu0
GICD->ITARGETSR[24] &=(~(0x3<<16));
GICD->ITARGETSR[24] |=(0x1<<16);
//GICC
//CTLR[0] = 1 -----> GICC层使能
GICC->CTRL |=(0x1);
//GICC_PMR[7:3]设置GICC中断优先级屏蔽寄存器
GICC->PMR |=(0x1f<<3);
}
void led1_init()
{
//gcc章节
RCC->MC_AHB4ENSETR |=(0x1<<4);
//GPIO章节
//MODER (21:20) 01
GPIOE->MODER &=(~(0x1<<21));
GPIOE->MODER |=(0x1<<20);
//OTYPER (10) 0
GPIOE->OTYPER &=(~(0x1<<10));
//OSPEEDR (21:20) 00
GPIOE->OSPEEDR &=(~(0x1<<20));
GPIOE->OSPEEDR &=(~(0x1<<21));
//PUPDR (21:20) 00
GPIOE->PUPDR &=(~(0x1<<20));
GPIOE->PUPDR &=(~(0x1<<21));
}
void led2_init()
{
//gcc章节
RCC->MC_AHB4ENSETR |=(0x1<<5);
//GPIO章节
//MODER (21:20) 01
GPIOF->MODER &=(~(0x1<<21));
GPIOF->MODER |=(0x1<<20);
//OTYPER (10) 0
GPIOF->OTYPER &=(~(0x1<<10));
//OSPEEDR (21:20) 00
GPIOF->OSPEEDR &=(~(0x1<<20));
GPIOF->OSPEEDR &=(~(0x1<<21));
//PUPDR (21:20) 00
GPIOF->PUPDR &=(~(0x1<<20));
GPIOF->PUPDR &=(~(0x1<<21));
}
void led3_init()
{
//gcc章节
//GPIO章节
//MODER (17:16) 01
GPIOE->MODER &=(~(0x1<<17));
GPIOE->MODER |=(0x1<<16);
//OTYPER (8) 0
GPIOE->OTYPER &=(~(0x1<<8));
//OSPEEDR (17:16) 00
GPIOE->OSPEEDR &=(~(0x1<<16));
GPIOE->OSPEEDR &=(~(0x1<<17));
//PUPDR (17:16) 00
GPIOE->PUPDR &=(~(0x1<<16));
GPIOE->PUPDR &=(~(0x1<<17));
}
void led1_on()
{
GPIOE->ODR |=(0x1<<10);
}
void led1_off()
{
GPIOE->ODR &=(~(0x1<<10));
}
void led2_on()
{
GPIOF->ODR |=(0x1<<10);
}
void led2_off()
{
GPIOF->ODR &=(~(0x1<<10));
}
void led3_on()
{
GPIOE->ODR |=(0x1<<8);
}
void led3_off()
{
GPIOE->ODR &=(~(0x1<<8));
}
do_irq.c:
#include "key.h"
extern void printf(const char *fmt, ...);
extern void delay_ms();
unsigned int i = 0;
void do_irq(void)
{
int num;
num=GICC->IAR;
switch(num)
{
case 99:
printf("key1!!!!!!!!!!!!!!!!!\n");
led1_on();
delay_ms(500);
led1_off();
//写1清除中断挂起标志位 EXTI_FPR1[9] = 1
EXTI->FPR1 |=(0x1<<9);
//清除GICD层挂起标志位 GICD_ISENABLER3[3] = 1
GICD->ISENABLER[3] |=(0x1<<3);
break;
case 97:
printf("key2!!!!!!!!!!!!!!!!!\n");
led2_on();
delay_ms(500);
led2_off();
//写1清除中断挂起标志位 EXTI_FPR1[7] = 1
EXTI->FPR1 |=(0x1<<7);
//清除GICD层挂起标志位 GICD_ISENABLER3[1] = 1
GICD->ISENABLER[3] |=(0x1<<1);
break;
case 98:
printf("key3!!!!!!!!!!!!!!!!!\n");
led3_on();
delay_ms(500);
led3_off();
//写1清除中断挂起标志位 EXTI_FPR1[8] = 1
EXTI->FPR1 |=(0x1<<8);
//清除GICD层挂起标志位 GICD_ISENABLER3[1] = 1
GICD->ISENABLER[3] |=(0x1<<2);
}
//GICC_EOIR[9:0]:清除中断号
GICC->EOIR |=num;
}
效果图: