ARM->cortex-A7核按键中断实验->day7

作业:

1.需要实现KEY1/KEY2/KEY3三个按键按下,并且上传到CSDN

 需要分析芯片手册

一、代码

汇编中有跳转指令到do_irp.c

ARM->cortex-A7核按键中断实验->day7_第1张图片

1.1 key.h

#ifndef __KEY_H__
#define __KEY_H__

#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_exti.h"
#include "stm32mp1xx_gic.h"


//初始化EXTI层
void PF9_EXTI_INIT();
//初始化GICD
void PF9_GICD_INIT();
//初始化GICC层
void PF9_GICC_INIT();

//初始化EXTI层
void PF7_EXTI_INIT();
//初始化GICD
void PF7_GICD_INIT();
//初始化GICC层
void PF7_GICC_INIT();


//初始化EXTI层
void PF8_EXTI_INIT();
//初始化GICD
void PF8_GICD_INIT();
//初始化GICC层
void PF8_GICC_INIT();   


#endif

1.2 key.c

#include "key.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_exti.h"
#include "stm32mp1xx_gic.h"

//初始化EXTI层
void PF9_EXTI_INIT()
{
	//1.设置GPIOF组使能
	RCC->MP_AHB4ENSETR |= (0x1 << 5);
	//1.设置PF9引脚为输入模式
	GPIOF->MODER &=(~(0x3 << 18));
	//2.设置PF91引脚和EXIT9控制器进行连接
	EXTI->EXTICR3 &= (~(0xff << 8));
	EXTI->EXTICR3 |= (0x05 << 8);
	//2.1 下降沿触发寄存器
	EXTI->FTSR1 |= (0x1 << 9);
	//3.设置PF9引脚中断不屏蔽
	EXTI->C1IMR1 |= (0X1 << 9);
	//4.设置清零标志位
}
//初始化GICD
void PF9_GICD_INIT()
{
	//1.设置GICD层全局中断使能寄存器
	GICD->CTRL |= (0x1 << 0);
	//2.设置GICD层中断使能寄存器
	GICD->ISENABLER[3] |= (0x1 << 3);
	//3.设置GICD层中断优先级寄存器
	GICD->IPRIORITYR[24] &= (~(0x1f << 27));
	GICD->IPRIORITYR[24] |= (0x1 << 27);
	//4.设置GICD层中断目标号寄存器
	GICD->ITARGETSR[24] &= (~(0x3 << 24));
	GICD->ITARGETSR[24] |= (0x1 << 24);
}
//初始化GICC层
void PF9_GICC_INIT()
{
	//1.设置GICC层全局中断使能寄存器
	GICC->CTRL |= (0x1 << 0);
	//2.设置GICC层中断优先级寄存器
	GICC->PMR &= (~(0X1f << 3));
	GICC->PMR |= (0X2 << 3);
}


//初始化EXTI层
void PF7_EXTI_INIT()
{
	//1.设置GPIOF组使能
	RCC->MP_AHB4ENSETR |= (0x1 << 5);
	//1.设置PF7引脚为输入模式
	GPIOF->MODER &=(~(0x3 << 14));
	//2.设置PF7引脚和EXTI7控制器进行连接
	EXTI->EXTICR2 &= (~(0xff << 24));
	EXTI->EXTICR2 |= (0x05 << 24);
	//2.1 下降沿触发寄存器
	EXTI->FTSR1 |= (0x1 << 7);
	//3.设置PF7引脚中断不屏蔽
	EXTI->C1IMR1 |= (0X1 << 7);
	//4.设置清零标志位
}
//初始化GICD
void PF7_GICD_INIT()
{
	//1.设置GICD层全局中断使能寄存器
	GICD->CTRL |= (0x1 << 0);
	//2.设置GICD层中断使能寄存器
	GICD->ISENABLER[3] |= (0x1 << 1);
	//3.设置GICD层中断优先级寄存器
	GICD->IPRIORITYR[24] &= (~(0x1f << 11));
	GICD->IPRIORITYR[24] |= (0x1 << 11);
	//4.设置GICD层中断目标号寄存器
	GICD->ITARGETSR[24] &= (~(0x3 << 8));
	GICD->ITARGETSR[24] |= (0x1 << 8);
}
//初始化GICC层
void PF7_GICC_INIT()
{
	//1.设置GICC层全局中断使能寄存器
	GICC->CTRL |= (0x1 << 0);
	//2.设置GICC层中断优先级寄存器
	GICC->PMR &= (~(0X1f << 3));
	GICC->PMR |= (0X2 << 3);

}


//初始化EXTI层
void PF8_EXTI_INIT()
{
	//1.设置GPIOF组使能
	RCC->MP_AHB4ENSETR |= (0x1 << 5);
	//1.设置PF8引脚为输入模式
	GPIOF->MODER &=(~(0x3 << 16));
	//2.设置PF8引脚和EXIT8控制器进行连接
	EXTI->EXTICR3 &= (~(0xff << 0));
	EXTI->EXTICR3 |= (0x05 << 0);
	//2.1 下降沿触发寄存器
	EXTI->FTSR1 |= (0x1 << 8);
	//3.设置PF8引脚中断不屏蔽
	EXTI->C1IMR1 |= (0X1 << 8);
	//4.设置清零标志位
}
//初始化GICD
void PF8_GICD_INIT()
{
	//1.设置GICD层全局中断使能寄存器
	GICD->CTRL |= (0x1 << 0);
	//2.设置GICD层中断使能寄存器
	GICD->ISENABLER[3] |= (0x1 << 2);
	//3.设置GICD层中断优先级寄存器
	GICD->IPRIORITYR[24] &= (~(0x1f << 19));
	GICD->IPRIORITYR[24] |= (0x1 << 19);
	//4.设置GICD层中断目标号寄存器
	GICD->ITARGETSR[24] &= (~(0x3 << 16));
	GICD->ITARGETSR[24] |= (0x1 << 16);
}
//初始化GICC层
void PF8_GICC_INIT()
{
	//1.设置GICC层全局中断使能寄存器
	GICC->CTRL |= (0x1 << 0);
	//2.设置GICC层中断优先级寄存器
	GICC->PMR &= (~(0X1f << 3));
	GICC->PMR |= (0X2 << 3);
}

1.3 do_irp.c 

#include "key.h"
extern void printf(const char *fmt, ...); 
extern void delay_ms(int ms);
unsigned int i = 0; 
void do_irq(void)  
{ 
	unsigned int num=0;
	num=GICC->IAR & 0X3ff;
	switch(num)
	{
		case 97:
			delay_ms(500);
			printf("key2!!!!!!!\n");
			EXTI->FPR1 |= (0X1 << 7);
			GICD->ICPENDR[3] |= (0X1 << 1);
			break;
		case 98:
			delay_ms(500);
			printf("key3!!!!!!!\n");
			EXTI->FPR1 |= (0X1 << 8);
			GICD->ICPENDR[3] |= (0X1 << 2);
			break;
		case 99:
			delay_ms(500);
			printf("key1!!!!!!!\n");
			EXTI->FPR1 |= (0X1 << 9);
			GICD->ICPENDR[3] |= (0X1 << 3);
			break;
	}
	GICC->EOIR &= (~(0X3ff << 0));
	GICC->EOIR |=num;

}

1.4 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()
{
	PF9_EXTI_INIT();
	PF9_GICD_INIT();
	PF9_GICC_INIT();

	PF7_EXTI_INIT();
	PF7_GICD_INIT();
	PF7_GICC_INIT();

	PF8_EXTI_INIT();
	PF8_GICD_INIT();
	PF8_GICC_INIT();

	while(1)
	{
	}
	return 0;
}

二、执行结果

ARM->cortex-A7核按键中断实验->day7_第2张图片

你可能感兴趣的:(arm,arm)