include/key.h
#ifndef __KEY_H__
#define __KEY_H__
#include "stm32mp1xx_exti.h"
#include "stm32mp1xx_gic.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
//对RCC/GPIO/EXTI进行初始化
void hal_key1_exit_init();
//对GIC章节进行初始化
void hal_key1_gic_init();
#endif
*/
#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();
//对GIC的初始化
void hal_key1_gic_init();
#endif
src/key.c
/*
#include "key.h"
//对RCC/GPIO/EXTI进行初始化
void hal_key1_exit_init()
{
//RCC_MP_AHB4ENSETR寄存器使能gpio组控制器
//[5] = 1
RCC->MP_AHB4ENSETR |= (0x1 << 5);
//GPIOx_MODER寄存器设置引脚为输入模式
//pf9[19:18]=00
//pf7[15:14]=00
//pf8[17:16]=00
GPIOF->MODER &= (~(0x3 << 18));
//EXTI_EXTICR3寄存器,外部中断选择寄存器
//选择和哪一个EXTI进行连接
//EXTI9[15:8]=0x05
//EXTI7[31:24]=0x05
//EXTI8[7:0]=0x05
EXTI->EXTICR3 &= (~(0xFF) << 8);
EXTI->EXTICR3 |= (0x05 << 8);
//EXTI_FTSR1寄存器,设置中断下降沿触发方式
EXTI->FTSR1 |= (0x1 << 9);
//EXTI_IMR1中断屏蔽选择寄存器
EXTI->C1IMR1 |= (0b01 << 9);
}
//对GIC的初始化
void hal_key1_gic_init()
{
//GICD的初始化
//1.使能GICD层控制器
GICD->CTRL |= 0x1;
//2.设置GICD层的中断使能
GICD->ISENABLER[3] |= (0x1 << 3);
//3.设置GICD层中断优先级[31:27]
GICD->IPRIORITYR[24] &= (~(0x1F << 27));
//4.设置GICD层中断目标分配寄存器[24:25]
GICD->ITARGETSR[24] |= (0x1 << 24);
//GICC章节初始化
//1.使能GICC层控制器
GICC->CTRL |= 0x1;
//2.设置GICC层中断优先级
GICC->PMR |= (0x1F << 3);
}
*/
#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);
}
//对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);
}
main.c
/*
#include "key.h"
//对RCC/GPIO/EXTI进行初始化
void hal_key1_exit_init()
{
//RCC_MP_AHB4ENSETR寄存器使能gpio组控制器
//[5] = 1
RCC->MP_AHB4ENSETR |= (0x1 << 5);
//GPIOx_MODER寄存器设置引脚为输入模式
//pf9[19:18]=00
//pf7[15:14]=00
//pf8[17:16]=00
GPIOF->MODER &= (~(0x3 << 18));
//EXTI_EXTICR3寄存器,外部中断选择寄存器
//选择和哪一个EXTI进行连接
//EXTI9[15:8]=0x05
//EXTI7[31:24]=0x05
//EXTI8[7:0]=0x05
EXTI->EXTICR3 &= (~(0xFF) << 8);
EXTI->EXTICR3 |= (0x05 << 8);
//EXTI_FTSR1寄存器,设置中断下降沿触发方式
EXTI->FTSR1 |= (0x1 << 9);
//EXTI_IMR1中断屏蔽选择寄存器
EXTI->C1IMR1 |= (0b01 << 9);
}
//对GIC的初始化
void hal_key1_gic_init()
{
//GICD的初始化
//1.使能GICD层控制器
GICD->CTRL |= 0x1;
//2.设置GICD层的中断使能
GICD->ISENABLER[3] |= (0x1 << 3);
//3.设置GICD层中断优先级[31:27]
GICD->IPRIORITYR[24] &= (~(0x1F << 27));
//4.设置GICD层中断目标分配寄存器[24:25]
GICD->ITARGETSR[24] |= (0x1 << 24);
//GICC章节初始化
//1.使能GICC层控制器
GICC->CTRL |= 0x1;
//2.设置GICC层中断优先级
GICC->PMR |= (0x1F << 3);
}
*/
#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);
}
//对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);
}
ubuntu@vivi:06-key-c$ ^C
ubuntu@vivi:06-key-c$ cat 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();
while(1)
{
}
return 0;
}
src/do_irq.c
#include "key.h"
extern void printf(const char *fmt, ...);
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 98:
break;
case 97:
break;
}
//3.清除获取到的中断号
GICC->EOIR |= num;
}
result
文章来源地址https://www.toymoban.com/news/detail-534786.html
文章来源:https://www.toymoban.com/news/detail-534786.html
到了这里,关于ARM_key1按键按下,打印key1的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!