#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
//灯引脚编号封装
#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;
//RCC/GPIO章节初始化
void hal_rcc_gpio_init();
void hal_led_init(gpio_t* gpio,int n);
//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);
//函数功能:gpio相关初始化操作
//参数1:GPIO组编号
//参数2:GPIO引脚编号
//参数3:初始化相关内容
//返回值:无
void hal_gpio_init(gpio_t* gpiox,unsigned int pin,gpio_init_t* init);
void hal_led_on(gpio_t* GPIO,unsigned int pin);
#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);
RCC->MP_AHB4ENSETR |= (0x3 << 4);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~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] |= (priority << ((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);
}
void hal_gpio_init(gpio_t* gpiox,unsigned int pin,gpio_init_t* init)
{
//设置模式
gpiox->MODER &= (~(0x3 << (pin * 2)));
gpiox->MODER |= (init->moder << (pin * 2));
//设置输出类型
gpiox->OTYPER &= (~(0x1 << pin));
gpiox->OTYPER |= (init->otyper << pin);
//设置输出速率
gpiox->OSPEEDR &= (~(0x3 << (pin * 2)));
gpiox->OSPEEDR |= (init->ospeedr << (pin * 2));
//设置是否需要上下拉电阻
gpiox->PUPDR &= (~(0x3 << (pin * 2)));
gpiox->PUPDR |= (init->pupdr << (pin * 2));
}
void hal_led_on(gpio_t* GPIO,unsigned int pin)
{
GPIO->ODR ^= (0x1 << pin);
}
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章节初始化
//GPIOE组/GPIOF组使能
//结构体初始化
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);
// 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;
}