ARM函数封装实现KEY1,KEY2,KEY3按键打印

key.h

#ifndef __KEY_H__
#define __KEY_H__

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

#define EXTI0 0
#define EXTI1 1
#define EXTI2 2
#define EXTI3 3
#define EXTI4 4
#define EXTI5 5
#define EXTI6 6
#define EXTI7 7
#define EXTI8 8
#define EXTI9 9
#define EXTI10 10
#define EXTI11 11
#define EXTI12 12
#define EXTI13 13
#define EXTI14 14
#define EXTI15 15

//RCC/GPIO章节初始化
void hal_rcc_gpio_init();

//EXTI章节初始化--->KEY1
//void hal_exti_key1_init();

//GIC章节初始化---->KEY1 
//void hal_gic_key1_init();

//************************封装函数方式实现按键123*******************************//

//封装EXTI初始化函数 (EXTI编号,GPIOF组号,触发方式)
void hal_exti_init(const unsigned int node,const unsigned int group,const int type);

//封装GIC初始化函数(中断号,中断优先级)
void hal_gic_init(int interrupt_id,int priority);

#endif

key.c

#include "key.h"

//KEY1 ---> PF9
//KEY2 ---> PF7
//KEY3 ---> PF8

//**************************RCC/GPIO章节初始化**********************************//
void hal_rcc_gpio_init()
{
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~RCC章节初始化~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
	//1.使能GPIOF组控制器 MP_AHB4ENSETR[5] = 1
	RCC->MP_AHB4ENSETR |= (0x1 << 5);

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~GPIO章节初始化~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
	//1.设置KEY1-->PF9引脚为输入模式 MODER[19:18] = 00
	GPIOF->MODER &= (~(0x3 << 18));
	//2.设置KEY2-->PF7引脚为输入模式 MODER[15:14] = 00
	GPIOF->MODER &= (~(0x3 << 14));
	//3.设置KEY3-->PF8引脚为输入模式 MODER[17:16] = 00
	GPIOF->MODER &= (~(0x3 << 16));
}


/***********************EXTI章节初始化--->KEY1********************************
void hal_exti_key1_init()
{
 	//1.设置EXTI中断选择寄存器 PF9--->EXTI9 EXTICR3[15:8] = 0x05
	EXTI->EXTICR3 &= (~(0xff << 8));
	EXTI->EXTICR3 |= (0x05 << 8);
	//2.设置PF9引脚,中断触发方式为下降沿触发 FTSR1[9] = 1
	EXTI->FTSR1 |= (0x1 << 9);
	//3.设置EXTI层中断不屏蔽 C1IMR1[9] = 1
	EXTI->C1IMR1 |= (0x1 << 9);
}


************************GIC章节初始化---->KEY1*******************************
void hal_gic_key1_init()
{
	//~~~~~~~~~~~~~~~~~~~~~~~GICD章节初始化~~~~~~~~~~~~~~~~~~~~~~~~~~~//
	//1.设置GICD层使能组0 CTRL[0] = 1
	GICD->CTRL |= (0x1 << 0);
	//2.设置GICD层中断设置使能寄存器 ISENABLER[3] 第3位写1
	GICD->ISENABLER[3] |= (0x1 << 3);
	//3.设置GICD层中断优先级寄存器 IPRIORITYR[24] 第[31:27] = 00000
	GICD->IPRIORITYR[24] &= (~(0x1f << 27));
	//4.设置GICD层中断目标分配寄存器 ITARGETSR[24] 第[25:24] = 01
	GICD->ITARGETSR[24] &= (~(0x3 << 24));
	GICD->ITARGETSR[24] |= (0x1 << 24);
	
	//~~~~~~~~~~~~~~~~~~~~~~~GICC章节初始化~~~~~~~~~~~~~~~~~~~~~~~~~~~//
	//1.设置GICC层使能组0 CTRL[0] = 1
	GICC->CTRL |= (0x1 << 0);
	//2.设置GICC层中断优先级寄存器 PMR[7:3] = 11111
	GICC->PMR &= (~(0x1f << 3));
	GICC->PMR |= (0x1f << 3);
}
*/


//**********************************封装函数方式实现按键123***********************************//

//封装EXTI初始化函数 (EXTI编号,GPIOF组号,触发方式)
void hal_exti_init(const unsigned int node,const unsigned int group,const int type)
{
	
	//1.设置EXTI中断选择寄存器,因为8位管理一个EXTI编号,一个寄存器32位,所以最多管理4个EXTI
	//要管理16个EXTI,需要16/4=4个寄存器
	if((node/4) == 0)
	{
		EXTI->EXTICR1 &= (~(0xff << ((node%4)*8)));
		EXTI->EXTICR1 |= (group << ((node%4)*8));
	}
	else if((node/4) == 1)
	{
		EXTI->EXTICR2 &= (~(0xff << ((node%4)*8)));
		EXTI->EXTICR2 |= (group << ((node%4)*8));
	}
	else if((node/4) == 2)
	{
		EXTI->EXTICR3 &= (~(0xff << ((node%4)*8)));
		EXTI->EXTICR3 |= (group << ((node%4)*8));
	}
	else if((node/4) == 3)
	{
		EXTI->EXTICR4 &= (~(0xff << ((node%4)*8)));
		EXTI->EXTICR4 |= (group << ((node%4)*8));
	}

	//2.设置“按键”引脚,中断触发为下降沿触发  =1
	EXTI->FTSR1 |= (type << node);

	//3.设置EXTI层中断不屏蔽 =1
	EXTI->C1IMR1 |= (0x01 << node);

}

//封装GIC初始化函数(中断号,中断优先级)
void hal_gic_init(int interrupt_id,int priority)
{
	//*********************GICD章节初始化***************************//
	//1.GICD组控制寄存器使0
	GICD->CTRL |= (0X1 << 0);
	
	//2.设置GICD层中断使能,共288个中断号,一个寄存器32位,一个寄存器最多管理32个中断号
	//288/32=9   (0~8个寄存器)
	GICD->ISENABLER[interrupt_id/32] |= (0x1 << (interrupt_id%32));

	//3.设置GICD层中断优先级寄存器
	GICD->IPRIORITYR[interrupt_id/4] &= (~(0x1f << ((interrupt_id%4)*8+3)));
	GICD->IPRIORITYR[interrupt_id/4] |= (interrupt_id << ((interrupt_id%4)*8+3));

	//4.设置GICD层中断目标分配给CPU0
	GICD->ITARGETSR[interrupt_id/4] &= (~(0x3 << ((interrupt_id%4)*8)));
	GICD->ITARGETSR[interrupt_id/4] |= (0x01 << ((interrupt_id%4)*8));


	//*********************GICC章节初始化******************************//
	//1.使能GICC组0
	GICC->CTRL |= (0x1 << 0);

	//2.设置GICC层中断优先级寄存器
	GICC->PMR |= (0x1f << 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_rcc_gpio_init();//RCC/GPIO章节初始化





//	hal_exti_key1_init();//EXTI章节初始化

	hal_exti_init(EXTI9,0x05,1);//EXTI章节初始化

	hal_exti_init(EXTI7,0x05,1);//EXTI章节初始化

	hal_exti_init(EXTI8,0x05,1);//EXTI章节初始化



	

//	hal_gic_key1_init();//GIC章节初始化

	hal_gic_init(99,9);

	hal_gic_init(98,8);

	hal_gic_init(97,7);

	while(1)

	{



	}

	return 0;

}

do_irq.c

#include "stm32mp1xx_exti.h"
#include "stm32mp1xx_gic.h"
extern void printf(const char *fmt, ...);

void do_irq(void) 
{
	unsigned int interrupt_id;
	//获取中断号 ID
	interrupt_id = GICC->IAR & (0x3ff);
	switch(interrupt_id)
	{
		case 97:
			printf("key2 down!!!");
			printf("key2 ID = %d\n",interrupt_id);
			//清除EXTI层中断挂起标志位 FPR1[9] = 1
			EXTI->FPR1 |= (0x1 << 9);
			//清除GICD层中断挂起标志位 ICPENDR[3]第三位写1
			GICD->ICPENDR[3] |= (0x1 << 3);
			break;
		case 98:
			printf("key1 down!!!");
			printf("key1 ID = %d\n",interrupt_id);
			//清除EXTI层中断挂起标志位 FPR1[9] = 1
			EXTI->FPR1 |= (0x1 << 9);
			//清除GICD层中断挂起标志位 ICPENDR[3]第三位写1
			GICD->ICPENDR[3] |= (0x1 << 3);
			break;
		case 99:
			printf("key3 down!!!");
			printf("key3 ID = %d\n",interrupt_id);
			//清除EXTI层中断挂起标志位 FPR1[9] = 1
			EXTI->FPR1 |= (0x1 << 9);
			//清除GICD层中断挂起标志位 ICPENDR[3]第三位写1
			GICD->ICPENDR[3] |= (0x1 << 3);
			break;
	}
	//清除GICC层中断号 EOIR
	GICC->EOIR = interrupt_id;
}

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