C语言 cortex-A7核 按键中断 实验【重点】

一、KEY1

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章节初始化
void hal_rcc_gpio_init();

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

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

#endif

src/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.使能PF9引脚为输入模式 MODER[19:18] = 00
	GPIOF->MODER &= (~(0X3 << 18));
	//2.使能PF7引脚为输入模式 MODER[15:14] = 00
	GPIOF->MODER &= (~(0x3 << 14));	
	//3.使能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层中断不屏蔽 IMR1[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层中断目标分配寄存器  给CPU0 ITARGETSR[24] 第[25:24] = 01
	GICD->ITARGETSR[24] &= (~(0x1 << 25));
	GICD->ITARGETSR[24] |= (0x1 << 24);

	//GICC章节初始化
	//1.使能GICC组0 CTLR[0] = 1
	GICC->CTRL |= (0x1 << 0);
	//2.设置KEY1 GICC层优先级 PMR[7:3] = 11111
	GICC->PMR |= (0x1f << 3);
}

src/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;
	//获取中断号
	interrupt_id = GICC->IAR & (0x3ff);
	switch(interrupt_id)
	{
	case 97:
		break;
	case 98:
		break;
	case 99:
		printf("key1 down!!!");
		printf("key1 ID = %d\n",interrupt_id);
		//清除EXTI层中断挂起标志位
		EXTI->FPR1 |= (0x1 << 9);
		//清除GICD层中断挂起标志位 ICPENDR[3] 第3位 = 1
		GICD->ICPENDR[3] |= (0x1 << 3);
		break;
	}
	//清除GICC中对应断号
	GICC->EOIR = interrupt_id; //将IAR寄存器中的中断号获取值,赋值给EOIR
}

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_gic_key1_init(); //GIC章节初始化
	while(1)

	{

	}
	return 0;
}

二、封装函数

include/key.h

#ifndef __KEY_H__
#define __KEY_H__

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

typedef enum{
	Falling, //下降沿
	Rising,  //上升沿
}trigger_t;

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

//void hal_exti_init(EXTI编号,GPIOF组号,触发方式);
void hal_exti_init(unsigned int EXTI_INPUT,unsigned int GPIOxGroup,trigger_t event_trigger);

//void hal_gic_init(中断号,中断优先级);
void hal_gic_init(unsigned int interrupt_id,unsigned int priority);
#endif

src/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));
	//1.设置KEY3-->PF8引脚为输入模式 MODER[17:16] = 00
	GPIOF->MODER &= (~(0x3 << 16));
}

//void hal_exti_init(EXTI编号,GPIOF组号,触发方式);
void hal_exti_init(unsigned int EXTI_INPUT,unsigned int GPIOxGroup,trigger_t event_trigger)
{
	//1.设置EXTI中断选择寄存器 PF9--->EXTI9 EXTICR3[15:8] = 0x05
	switch(EXTI_INPUT / 4)
	{
		case 0:
			break;
		case 1:
			EXTI->EXTICR2 &= (~(0xff << (EXTI_INPUT % 4 * 8)));
			EXTI->EXTICR2 |= (GPIOxGroup << (EXTI_INPUT % 4 * 8));
			break;
		case 2:
			EXTI->EXTICR3 &= (~(0xff << (EXTI_INPUT % 4 * 8)));
			EXTI->EXTICR3 |= (GPIOxGroup << (EXTI_INPUT % 4 * 8));
			break;
		case 3:
			break;
	}
	//2.设置中断触发方式
	if(event_trigger == Falling)
	{
		EXTI->FTSR1 |= (0x1 << EXTI_INPUT);
	}else
	{
		EXTI->RTSR1 |= (0x1 << EXTI_INPUT);
	}
	//3.设置EXTI层中断不屏蔽
	EXTI->C1IMR1 |= (0x1 << EXTI_INPUT);
}

//void hal_gic_init(中断号,中断优先级);
void hal_gic_init(unsigned int interrupt_id,unsigned int priority)
{
	//GICD章节初始化 
	//1.设置GICD层使能组0 CTRL[0] = 1
	GICD->CTRL |= (0x1 << 0);
	//2.设置GICD层中断设置使能寄存器 
	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] |= (priority << (interrupt_id % 4 * 8 + 3));
	//4.设置GICD层中断目标分配寄存器 ITARGETSR[24] 第[25:24] = 01
	GICD->ITARGETSR[interrupt_id/4] &= (~(0x3 << (interrupt_id % 4 * 8)));
	GICD->ITARGETSR[interrupt_id/4] |= (0x1 << (interrupt_id % 4 * 8));

	//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);
}

src/do_irq.c

#include "stm32mp1xx_exti.h"
#include "stm32mp1xx_gic.h"
#include "led.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: //KEY2 ---> LED2 ---->PF10
			printf("key2 down!!!");
			printf("key2 ID = %d\n",interrupt_id);
			GPIOF->ODR ^= (0x1 << 10);
			//清除EXTI层中断挂起标志位 FPR1[7] = 1
			EXTI->FPR1 |= (0x1 << 7);
			//清除GICD层中断挂起标志位 ICPENDR[3]第一位写1
			GICD->ICPENDR[3] |= (0x1 << 1);
			break;

		case 98: //KEY3 ---> LED1 ---->PE10
			printf("key3 down!!!");
			printf("key3 ID = %d\n",interrupt_id);
			GPIOE->ODR ^= (0x1 << 10);
			//清除EXTI层中断挂起标志位 FPR1[8] = 1
			EXTI->FPR1 |= (0x1 << 8);
			//清除GICD层中断挂起标志位 ICPENDR[3]第2位写1
			GICD->ICPENDR[3] |= (0x1 << 2);
			break;
		case 99: //KEY1 ---> LED3 ----> PE8
			printf("key1 down!!!");
			printf("key1 ID = %d\n",interrupt_id);
			GPIOE->ODR ^= (0x1 << 8);
			//清除EXTI层中断挂起标志位 FPR1[9] = 1
			EXTI->FPR1 |= (0x1 << 9);
			//清除GICD层中断挂起标志位 ICPENDR[3]第三位写1
			GICD->ICPENDR[3] |= (0x1 << 3);
			break;
	}
	//清除GICC层中断号 EOIR
	GICC->EOIR = interrupt_id;
}

main.c

#include "led.h"

#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++);

}



void led_init()

{

	//GPIOE组/GPIOF组使能

	RCC_MP_AHB4ENSETR |= (0x3 << 4);

	//结构体初始化

	gpio_init_t init = {OUTPUT,PP,LOW,NO_PU_PD};

	hal_gpio_init(GPIOE,GPIO_PIN_10,&init);

	hal_gpio_init(GPIOF,GPIO_PIN_10,&init);

	hal_gpio_init(GPIOE,GPIO_PIN_8,&init);

}



int main()

{

 	led_init();// LED1灯初始化

	//key1 ----> PF9 

	hal_rcc_gpio_init();

	hal_exti_init(9,0x05,Falling);

	hal_gic_init(99,9);

	//key2 ----> PF7 

	hal_exti_init(7,0x05,Falling);

	hal_gic_init(97,7);

	//key3 ----> PF8

	hal_exti_init(8,0x05,Falling);

	hal_gic_init(98,8);

	while(1)

	{



	}

	return 0;

}

调用:include/led.h

#ifndef __LED_H__
#define __LED_H__

//LED1 ---> PE10
//LED2 ---> PF10
//LED3 ---> PE8

//结构体封装
typedef struct{
	volatile unsigned int MODER;  // 0x00
	volatile unsigned int OTYPER; // 0x04
	volatile unsigned int OSPEEDR; //0x08
	volatile unsigned int PUPDR; //0x0c
	volatile unsigned int IDR; //0x10
	volatile unsigned int ODR; //0x14
}gpio_t;

//GPIOE组基地址 0x50006000
#define GPIOE ((gpio_t*)0x50006000)

//GPIOF组基地址 0x50007000
#define GPIOF ((gpio_t*)0x50007000)

#define RCC_MP_AHB4ENSETR (*(volatile unsigned int*)0x50000A28)

//引脚编号封装
#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
#define GPIO_PIN_11 11
#define GPIO_PIN_12 12
#define GPIO_PIN_13 13
#define GPIO_PIN_14 14
#define GPIO_PIN_15 15

//模式寄存器封装
typedef enum{
	INPUT, //输入模式
	OUTPUT, //输出模式
	ALT, //复用功能模式
	ANALOG, //模拟模式
}gpio_moder_t;

//输出类型寄存器封装
typedef enum{
	PP,//推挽
	OD,//开漏
}gpio_otyper_t;

//输出速率寄存器封装
typedef enum{
	LOW,//低速
	MED,//中速
	HIGH,//高速
	VERY_HIGH,//快速
}gpio_ospeedr_t;

//是否需要上下拉电阻进行封装
typedef enum{
	NO_PU_PD,//禁止上下拉电阻
	PU,//上拉
	PD,//下拉
}gpio_pupdr_t;

//封装初始化结构体
typedef struct{
	gpio_moder_t moder; //模式相关寄存器
	gpio_otyper_t otyper;  //输出类型寄存器 
	gpio_ospeedr_t ospeedr; //输出速率寄存器 
	gpio_pupdr_t pupdr;   //是否需要上下拉电阻寄存器
}gpio_init_t;

//输出高低电平
typedef enum{
	GPIO_RESET_T,//低电平
	GPIO_SET_T,//高电平
}gpio_status_t;

//函数功能:gpio相关初始化操作
//参数1:GPIO组编号
//参数2:GPIO引脚编号
//参数3:初始化相关内容
//返回值:无
void hal_gpio_init(gpio_t* gpiox,unsigned int pin,gpio_init_t* init);


//函数功能:gpio写相关操作
//参数1:GPIO组编号
//参数2:GPIO引脚编号
//参数3:写值 写1高电平  写0低电平
//返回值:无
void hal_gpio_write(gpio_t* gpiox,unsigned int pin,gpio_status_t status);

#endif

你可能感兴趣的:(ARM,c语言,linux,服务器)