key.h
- 1 #ifndef __KEY_H__
- 2 #define __KEY_H__
- 3
- 4 #include "stm32mp1xx_gpio.h"
- 5 #include "stm32mp1xx_gic.h"
- 6 #include "stm32mp1xx_rcc.h"
- 7 #include "stm32mp1xx_exti.h"
- 8
- 9
- 10 //需要初始化GPIOF key1 = pf pin 9
- 11 void hal_gpio_key1_init(void);
- 12
- 13
- 14 //初始化exti
- 15 void hal_exti_key1_init(void);
- 16
- 17 //初始化gicd
- 18 void hal_gicd_key1_init(void);
- 19
- 20 //初始化gicc
- 21 void hal_gicc_key1_init(void);
- 22
- 23 #endif
- ~
- ~
- ~
IRQ中断执行代码:
- 1 #include "key.h"
- 2 //#include "led.h"
- 3
- 4 extern void printf(const char *fmt, ...);
- 5 extern void delay_ms(int ms);
- 6
- 7 unsigned int i = 0;
- 8 void do_irq(void)
- 9 {
- 10 printf("key1 is pressed\n\r");
- 11 //获取中断号
- 12 i = GICC->IAR;
- 13
- 14 printf("i = %d\n\r",i);
- 15 i &= (0x3ff);
- 16 //判断中断号
- 17 if(99 == i)
- 18 {
- 19
- 20
- 21 GICC->EOIR &= ~(0x3ff);
- 22 GICC->EOIR |= ((0x3ff)&i);
- 23 //clear
- 24 //99/32 = 3---3
- 25 GICD->ICPENDR[3] |= (0x1 <<3);
- 26
- 27 EXTI->FPR1 |= (0x1 <<9);
- 28
- 29
- 30 //清除中断号以及中断挂起标志
- 31
- 32 }
- 33
- 34
- 35 return;
- 36 }
key.c
- 1 #include "key.h"
- 2
- 3
- 4
- 5 //需要初始化GPIOF key1 = pf pin 9
- 6 void hal_gpio_key1_init(void)
- 7 {
- 8 //RCC使能GPIOF
- 9 RCC->MP_AHB4ENSETR |= (0X1 <<5);
- 10
- 11 //GPIOF pf9设置为input
- 12 GPIOF->MODER &= ~(0x3 <<(9*2));
- 13
- 14 return;
- 15 }
- 16
- 17
- 18 //初始化exti
- 19 void hal_exti_key1_init(void)
- 20 {
- 21 EXTI->EXTICR3 &= ~(0XFF <<8);
- 22 EXTI->EXTICR3 |= (0x05 <<8);
- 23
- 24 EXTI->FTSR1 |= (0X1 <<9);
- 25
- 26 EXTI->C1IMR1 |= (0X1 <<9);
- 27
- 28 /*
- 29 EXTI->FPR1 |= (0x1 <<9);
- 30 */
- 31 return;
- 32 }
- 33
- 34 //初始化gicd
- 35 void hal_gicd_key1_init(void)
- 36 {
- 37 GICD->CTRL |= (0X1);
- 38
- 39 //BIT 3
- 40 GICD->ISENABLER[3] |= (0X1<<3);
- 41
- 42 //99/4 = 24---3; 3:24~31 -->24+3~31
- 43 GICD->IPRIORITYR[24] &= ~(0x1f <<27);
- 44 GICD->IPRIORITYR[24] |= (0x01 <<27);
- 45
- 46 //99/4 = 24---3; 3*8 = 24; [25~24]
- 47 GICD->ITARGETSR[24] &= ~(0x3 <<24);
- 48 GICD->ITARGETSR[24] |= (0x1 <<24);
- 49
- 50 /*
- 51 //clear
- 52 //99/32 = 3---3
- 53 GICD->ICPENDR[3] |= (0x1 <<3);
- 54 */
- 55 return;
- 56 }
- 57
- 58 //初始化gicc
- 59 void hal_gicc_key1_init(void)
- 60 {
- 61 GICC->CTRL |= (0x1<<0);
- 62
- 63 GICC->PMR &= ~(0x1f <<3);
- 64 GICC->PMR |= (0X11 <<3);
- 65
- 66 /*
- 67
- 68 GICC->EOIR &= ~(0x3ff);
- 69 GICC->EOIR |= ((0x3ff)&GICC->IAR);
- 70
- 71 */
- 72 }
- 73
- 74
- 75
- ~
main.c
- 1 //#include "led.h"
- 2 #include "key.h"
- 3
- 4 extern void printf(const char *fmt, ...);
- 5 void delay_ms(int ms)
- 6 {
- 7 int i,j;
- 8 for(i = 0; i < ms;i++)
- 9 for (j = 0; j < 1800; j++);
- 10 }
- 11
- 12
- 13 int main(void)
- 14 {
- 15 hal_gpio_key1_init();
- 16
- 17 hal_exti_key1_init();
- 18
- 19 hal_gicd_key1_init();
- 20
- 21 hal_gicc_key1_init();
- 22 while(1)
- 23 {
- 24 ;
- 25 }
- 26 return 0;
- 27 }
- 28 /*
- 29 LED1(INIT);//led1_init();
- 30 LED2(INIT);//led2_init();
- 31 LED3(INIT);//led3_init();
- 32 LED1(ON);//led1_on();
- 33 delay_ms(500);
- 34 LED1(OFF);//led1_off();
- 35 delay_ms(500);
- 36
- 37 LED2(ON);//led2_on();
- 38 delay_ms(500);
- 39 LED2(OFF);//led2_off();
- 40 delay_ms(500);
- 41
- 42 LED3(ON);//led3_on();
- 43 delay_ms(500);
- 44 LED3(OFF);//led3_off();
- 45 delay_ms(500);
- 46
- 47 LED2(ON);//led2_on();
- 48 delay_ms(500);
- 49 LED2(OFF);//led2_off();
- 50 delay_ms(500);
- 51
- 52
- 53 */
- ~