基于ARM的按键中断

该代码实现了一个STM32MP1xx微处理器的GPIO、EXTI中断控制器以及GIC(GenericInterruptController)的初始化,用于处理键输入。在`do_irq.c`中定义了中断服务函数,根据中断号识别并响应按键事件,清除中断标志并调用相应的清除操作。
摘要由CSDN通过智能技术生成

效果:

 

key.h

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


//初始化EXTI控制器
void hal_exit_init();
//初始化GICD控制器
void hal_gicd_init();
//初始化GICC控制器
void hal_gicc_init();




#endif

key.c

#include"key.h"
#include "/home/ubuntu/2022101/ARM/04_key_c/common/include/stm32mp1xx_gpio.h"
#include "/home/ubuntu/2022101/ARM/04_key_c/common/include/stm32mp1xx_gic.h"
#include "/home/ubuntu/2022101/ARM/04_key_c/common/include/stm32mp1xx_exti.h"

//初始化EXIT控制器
void hal_exit_init()
{
	/**********RCC章节初始化*********/
	//gpio-f组控制器初始化
	RCC_MP_AHB4ENSETR|=(0x1<<5);

	/**********GPIO章节初始化*********/
	//设置MODER寄存器为输入模式
	GPIOF->MODER&=(~(0x3<<18));   //设置key1->pf9;
	GPIOF->MODER&=(~(0x3<<14));   //设置key2->pf7;
	GPIOF->MODER&=(~(0x3<<16));   //设置key3->pf8;

	/**********EXIT章节初始化*********/

	/**********EXTI->EXTICR3初始化**************/
	//key1->PF9初始化
	EXTI->EXTICR3&=(~(0xff)<<8);
	EXTI->EXTICR3|=(0x01)<<8;
	EXTI->EXTICR3|=(0x01)<<10;

	//key2->PF7初始化
	EXTI->EXTICR2&=(~(0x0f)<<24);
	EXTI->EXTICR2|=(0x01)<<24;
	EXTI->EXTICR2|=(0x01)<<26;

	//key3->PF8初始化
	EXTI->EXTICR3&=(~(0xff)<<0);
	EXTI->EXTICR3|=(0x01)<<0;
	EXTI->EXTICR3|=(0x01)<<2;


	/**********EXTI->FTSR1初始化**************/
	//key1->pf9初始化
	EXTI->FTSR1|=(0x1<<9);
	//key2->pf7初始化
	EXTI->FTSR1|=(0x1<<7);
	//key3->pf8初始化
	EXTI->FTSR1|=(0x1<<8);


	/**********EXTI->IMR1初始化**************/
	//key1->pf9初始化
	EXTI->C1IMR1|=(0x1<<9);
	//key2->pf7初始化
	EXTI->C1IMR1|=(0x1<<7);
	//key3->pf8初始化
	EXTI->C1IMR1|=(0x1<<8);

}

//初始化GICD控制器
void hal_gicd_init()
{
	//设置CRLR
	GICD->CTRL|=(0x1<<0);

	//设置ISENABLERx寄存器
	GICD->ISENABLER[3]|=(0x1<<3);   //key1
	GICD->ISENABLER[3]|=(0x1<<1);   //key2
	GICD->ISENABLER[3]|=(0x1<<2);   //key3

	//设置GICD_IPRIORITYR
	GICD->IPRIORITYR[24]|=(0x1<<27);   //key1
	GICD->IPRIORITYR[24]|=(0x1<<11);   //key2
	GICD->IPRIORITYR[24]|=(0x1<<19);   //key3

	//设置GICD_ITARGETSR
	GICD->ITARGETSR[24]&=(~(0x3<<24));  //分配给cpu0
	GICD->ITARGETSR[24]|=(0x1<<24);   	//key1
	
	GICD->ITARGETSR[24]&=(~(0x3<<8));  //分配给cpu0
	GICD->ITARGETSR[24]|=(0x1<<8);   	//key2

	GICD->ITARGETSR[24]&=(~(0x3<<16));  //分配给cpu0
	GICD->ITARGETSR[24]|=(0x1<<16);   	//key3
}

//初始化GICC控制器
void hal_gicc_init()
{
	//设置CTRL	
	GICC->CTRL|=(0x1<<0);

	//设置GICC_PMR
	GICC->PMR|=(0x1<<3);
	GICC->PMR|=(0x1<<4);
	GICC->PMR|=(0x1<<5);

}

do_irq.c

#include "/home/ubuntu/2022101/ARM/04_key_c/common/include/stm32mp1xx_gic.h"
#include "/home/ubuntu/2022101/ARM/04_key_c/common/include/stm32mp1xx_exti.h"

extern void printf(const char *fmt, ...);
unsigned int i = 0;
void do_irq(void) 
{
	//获取终端号
	int GICC_num=GICC->IAR;
	//判断终端号
	switch (GICC_num)
	{
	case 99:    //key1
		printf("i'am key1,我要玩亚索\n");
		//清除EXTI
		EXTI->FPR1|=(0x1<<9);   //key1
		EXTI->FPR1|=(0x1<<7);   //key2
		EXTI->FPR1|=(0x1<<8);   //key3

		//清除终端挂起标志位,清除终端号
		//清除GICD
		GICD->ICPENDR[3]|=(0x1<<3);                //key1
		GICD->ICPENDR[1]|=(0x1<<1); 			   //key2
		GICD->ICPENDR[2]|=(0x1<<2); 			   //key3
		//清除GICC
		GICC->EOIR=GICC_num;
		break;

	case 97:    //key2
		printf("i'am key2,我要玩亚索\n");	
		//清除EXTI
		EXTI->FPR1|=(0x1<<9);   //key1
		EXTI->FPR1|=(0x1<<7);   //key2
		EXTI->FPR1|=(0x1<<8);   //key3

		//清除终端挂起标志位,清除终端号
		//清除GICD
		GICD->ICPENDR[3]|=(0x1<<3);                //key1
		GICD->ICPENDR[1]|=(0x1<<1); 			   //key2
		GICD->ICPENDR[2]|=(0x1<<2); 			   //key3
		//清除GICC
		GICC->EOIR=GICC_num;
		break;
	case 98:
		printf("i'am key3,我要玩亚索\n");
		//清除EXTI
		EXTI->FPR1|=(0x1<<9);   //key1
		EXTI->FPR1|=(0x1<<7);   //key2
		EXTI->FPR1|=(0x1<<8);   //key3

		//清除终端挂起标志位,清除终端号
		//清除GICD
		GICD->ICPENDR[3]|=(0x1<<3);                //key1
		GICD->ICPENDR[1]|=(0x1<<1); 			   //key2
		GICD->ICPENDR[2]|=(0x1<<2); 			   //key3
		//清除GICC
		GICC->EOIR=GICC_num;

		break;
	}
}

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_exit_init();
 hal_gicd_init();
 hal_gicc_init();
 while(1)
 {
 }
 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值