要求:
main.c
#include "uart4.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_key_init();
hal_exti_init();
hal_gicd_init();
hal_gicc_init();
while(1)
{
}
return 0;
}
key_it.c
#include "../include/key_it.h"
void hal_key_init()
{
RCC->MP_AHB4ENSETR |= (0x1 << 5);
GPIOF->MODER &= (~(0x3 << 18));
GPIOF->MODER &= (~(0x3 << 14));
GPIOF->MODER &= (~(0x3 << 16));
}
void hal_exti_init()
{
EXTI->EXTICR3 &= (~(0xff << 8));
EXTI->EXTICR3 |= (0x05 << 8);
EXTI->EXTICR3 &= (~(0xff << 24));
EXTI->EXTICR3 |= (0x05 << 24);
EXTI->EXTICR3 &= (~(0xff << 0));
EXTI->EXTICR3 |= (0x05 << 0);
EXTI->FTSR1 |=(0x1 << 9);
EXTI->FTSR1 |=(0x1 << 7);
EXTI->FTSR1 |=(0x1 << 8);
EXTI->C1IMR1 |= (0x1 << 9);
EXTI->C1IMR1 |= (0x1 << 7);
EXTI->C1IMR1 |= (0x1 << 8);
}
void hal_gicd_init()
{
GICD->CTRL |= 0x1;
GICD->ISENABLER[3] |= (0x1 << 3);
GICD->ISENABLER[3] |= (0x1 << 1);
GICD->ISENABLER[3] |= (0x1 << 2);
GICD->IPRIORITYR[24] &= (~(0x1f << 27));
GICD->IPRIORITYR[24] |= (0x1 << 27);
GICD->IPRIORITYR[24] &= (~(0x1f << 11));
GICD->IPRIORITYR[24] |= (0x1 << 11);
GICD->IPRIORITYR[24] &= (~(0x1f << 19));
GICD->IPRIORITYR[24] |= (0x1 << 19);
GICD->ITARGETSR[24] &= (~(0x3 << 24));
GICD->ITARGETSR[24] |= (0x1 << 24);
GICD->ITARGETSR[24] &= (~(0x3 << 8));
GICD->ITARGETSR[24] |= (0x1 << 8);
GICD->ITARGETSR[24] &= (~(0x3 << 16));
GICD->ITARGETSR[24] |= (0x1 << 16);
}
void hal_gicc_init()
{
GICC->CTRL |= 0x1;
GICC->PMR &= (~(0x1f << 3));
GICC->PMR |= (0x5 << 3);
}
do_irq.c
#include "../include/key_it.h"
extern void printf(const char *fmt, ...);
extern void delay_ms(int ms);
unsigned int i = 0;
void do_irq(void)
{
i=GICC->IAR & 0x3ff;
switch(i)
{
case 99:
delay_ms(500);
printf("key1按下\n");
EXTI->FPR1 |= (0x1 << 9);
GICD->ICPENDR[3] |= (0X1 << 3);
break;
case 97:
delay_ms(500);
printf("key2按下\n");
EXTI->FPR1 |= (0x1 << 7);
GICD->ICPENDR[3] |= (0X1 << 1);
break;
case 98:
delay_ms(500);
printf("key3按下\n");
EXTI->FPR1 |= (0x1 << 8);
GICD->ICPENDR[3] |= (0X1 << 2);
break;
default:
break;
}
GICC->EOIR &= (~(0x3ff));
GICC->EOIR |= i;
}
key_it.h
#ifndef __KEY_IT_H__
#define __KEY_IT_H__
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_exti.h"
#include "stm32mp1xx_gic.h"
void hal_key_init();
void hal_exti_init();
void hal_gicd_init();
void hal_gicc_init();
#endif