ARM_key1按键按下打印key1press key2按键按下打印key2press key3按键按下打印key3press

include/key.h 

#ifndef __KEY_H__
#define __KEY_H__

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


//对RCC/GPIO/EXTI章节的初始化
void hal_key1_exit_init();
void hal_key2_exit_init();
void hal_key3_exit_init();



//对GIC的初始化
void hal_key1_gic_init();
void hal_key2_gic_init();
void hal_key3_gic_init();





#endif

src/key.c

#include "key.h"


//对RCC/GPIO/EXTI章节的初始化
void hal_key1_exit_init()
{
	//RCC
	RCC->MP_AHB4ENSETR |= (0x1 << 5);
	//GPIO设置输入模式
	GPIOF->MODER &= (~(0x3 << 18));
	//EXTI
	//1.外部中断选择寄存器,选择与哪个EXTI(16)链接
	EXTI->EXTICR3 &= (~(0xff << 8));
	EXTI->EXTICR3 |= (0x5 << 8);
	//2.设置中断下降沿触发方式
	EXTI->FTSR1 |= (0x1 << 9);
	//3.中断屏蔽选择寄存器
	EXTI->C1IMR1 |= (0b01 << 9);
}


void hal_key2_exit_init()
{
	//RCC
	RCC->MP_AHB4ENSETR |= (0x1 << 5);
	//GPIO设置输入模式
	GPIOF->MODER &= (~(0x3 << 14));
	//EXTI
	//1.外部中断选择寄存器,选择与哪个EXTI(16)链接
	EXTI->EXTICR2 &= (~(0xff <<24));
	EXTI->EXTICR2 |= (0x5 << 24);
	//2.设置中断下降沿触发方式
	EXTI->FTSR1 |= (0x1 << 7);
	//3.中断屏蔽选择寄存器
	EXTI->C1IMR1 |= (0b01 << 7);
}

void hal_key3_exit_init()
{
	//RCC
	RCC->MP_AHB4ENSETR |= (0x1 << 5);
	//GPIO设置输入模式
	GPIOF->MODER &= (~(0x3 << 16));
	//EXTI
	//1.外部中断选择寄存器,选择与哪个EXTI(16)链接
	EXTI->EXTICR3 &= (~(0xff));
	EXTI->EXTICR3 |= (0x5);
	//2.设置中断下降沿触发方式
	EXTI->FTSR1 |= (0x1 << 8);
	//3.中断屏蔽选择寄存器
	EXTI->C1IMR1 |= (0b01 << 8);
}


//对GIC的初始化
void hal_key1_gic_init()
{
	//GICD的初始化
	//1.使能GICD层控制器
	GICD->CTRL |= 0x1;
	//2.设置GICD层的中断使能
	GICD->ISENABLER[3] |= (0x1 << 3);
	//3.设置GICD层中断优先级
	GICD->IPRIORITYR[24] &= (~(0x1F << 27));
	//4.设置GICD层中断目标分配寄存器
	GICD->ITARGETSR[24] |= (0x1 << 24);

	//GICC章节初始化
	//1.使能GICC层控制器
	GICC->CTRL |= 0x1;
	//2.设置GICC层中断优先级
	GICC->PMR |= (0x1 << 3);
}

void hal_key2_gic_init()
{
	//GICD的初始化
	//1.使能GICD层控制器
	GICD->CTRL |= 0x1;
	//2.设置GICD层的中断使能
	GICD->ISENABLER[3] |= (0x1 << 1);
	//3.设置GICD层中断优先级
	GICD->IPRIORITYR[24] &= (~(0x1F << 11));
	//4.设置GICD层中断目标分配寄存器
	GICD->ITARGETSR[24] |= (0x1 << 8);

	//GICC章节初始化
	//1.使能GICC层控制器
	GICC->CTRL |= 0x1;
	//2.设置GICC层中断优先级
	GICC->PMR |= (0x1 << 3);
}

void hal_key3_gic_init()
{
	//GICD的初始化
	//1.使能GICD层控制器
	GICD->CTRL |= 0x1;
	//2.设置GICD层的中断使能
	GICD->ISENABLER[3] |= (0x1 << 2);
	//3.设置GICD层中断优先级
	GICD->IPRIORITYR[24] &= (~(0x1F << 19));
	//4.设置GICD层中断目标分配寄存器
	GICD->ITARGETSR[24] |= (0x1 << 16);

	//GICC章节初始化
	//1.使能GICC层控制器
	GICC->CTRL |= 0x1;
	//2.设置GICC层中断优先级
	GICC->PMR |= (0x1 << 3);
}

 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()
{

	hal_key1_exit_init();
	hal_key1_gic_init();

	hal_key2_exit_init();
	hal_key2_gic_init();
	
	hal_key3_exit_init();
	hal_key3_gic_init();

	while(1)
	{
	}
	return 0;
}

src/ do_irq.c

#include "key.h"
extern void printf(const char *fmt, ...);
//key1----->PF9----->EXTI9---->EXTI_FPR1[9]
//key2----->PF7----->EXTI7---->EXTI_FPR1[7]
//key3----->PF8----->EXTI8---->EXTI_FPR1[8]
void do_irq(void) 
{
	unsigned int num;
	num = GICC->IAR & 0x3ff;
	switch (num)
	{
	case 99:
		printf("key1 press#####\n");
		//1.消除EXTI层中断挂起标志位
		EXTI->FPR1 |= (0x1 << 9);
		//2.消除GICD层的中断挂起标志位
		GICD->ICPENDR[3] |= (0x1 << 3);
		break;
	case 97:
		printf("key2 press#####\n");
		//1.消除EXTI层中断挂起标志位
		EXTI->FPR1 |= (0x1 << 7);
		//2.消除GICD层的中断挂起标志位
		GICD->ICPENDR[3] |= (0x1);
		break;
	case 98:
		printf("key3 press#####\n");
		//1.消除EXTI层中断挂起标志位
		EXTI->FPR1 |= (0x1 << 8);
		//2.消除GICD层的中断挂起标志位
		GICD->ICPENDR[3] |= (0x1 << 2);

		break;
	}
	//3.清除获取到的中断号
	GICC->EOIR |= num;
}

你可能感兴趣的:(arm开发,java,linux)