- #ifndef __KEY__H__
- #define __KEY__H__
- #include"stm32mp1xx_gpio.h"
- #include"stm32mp1xx_rcc.h"
- #include"stm32mp1xx_gic.h"
- #include"stm32mp1xx_exti.h"
-
-
- void key_config();
- void all_led_init();
- void fan_init();
- void sp_init();
- #endif
- #include"key.h"
-
- void key_config()
- {
- //RCC使能GPIOF时钟
- RCC->MP_AHB4ENSETR |= (0X1<<5);
- //设置PF9 PF7 PF8GPIO输入
- GPIOF->MODER&=(~(0x3<<18)); //设置key1->PF9;
- GPIOF->MODER&=(~(0x3<<14)); //设置key1->PF7;
- GPIOF->MODER&=(~(0x3<<16)); //设置key1->PF8;
- //设置PF9 PF7 PF8产生EXTI事件 EXTI EXTICRX
- //key1->PF9初始化
- EXTI->EXTICR3&=(~(0xff<<8));
- EXTI->EXTICR3|=(0x05)<<8;
-
- //key2->PF7初始化
- EXTI->EXTICR2&=(~(0xFF<<24));
- EXTI->EXTICR2|=(0x5)<<24;
-
- //key3->PF8初始化
- EXTI->EXTICR3&=(~(0xff)<<0);
- EXTI->EXTICR3|=(0x5)<<0;
- //设置事件触发方式为下降沿触发EXTI_FTSR1
- EXTI->FTSR1|=(0x1<<9);
- //key2->pf7初始化
- EXTI->FTSR1|=(0x1<<7);
- //key3->pf8初始化
- EXTI->FTSR1|=(0x1<<8);
- //设置EXTI事件触发不屏蔽EXTI_IMR1
- //key1->pf9初始化
- EXTI->C1IMR1|=(0x1<<9);
- //key2->pf7初始化
- EXTI->C1IMR1|=(0x1<<7);
- //key3->pf8初始化
- EXTI->C1IMR1|=(0x1<<8);
-
- //使能中断能转发到特定的CPU接口层GICD ISENABLERX
- GICD->ISENABLER[3]|=(0x1<<3); //key1
- GICD->ISENABLER[3]|=(0x1<<1); //key2
- GICD->ISENABLER[3]|=(0x1<<2); //key3
- //GICD ISENABLERx设置中断优先级
- GICD->IPRIORITYR[24]&=(~(0x1f<<27)); //key1
- GICD->IPRIORITYR[24]&=(~(0x1f<<11)); //key2
- GICD->IPRIORITYR[24]&=(~(0x1f<<19)); //key3
- //设置当前中断被转发到哪一个CPu处理GICD_ITARGETSRX
- 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
- //使能cPu可以处理组0转发的中断GICC_CTLR
- GICC->CTRL|=(0x1<<0);
- //设置中断优先级掩码GICC_CTLR
- GICC->PMR|=(0x1f<<3);
- //使能组0转发中断GICD_CTLR
- GICD->CTRL|=(0x1<<0);
- }
- //GPIO初始化
- void all_led_init()
- {
- //RCC使能GPIOF时钟
- RCC->MP_AHB4ENSETR |= (0X3<<4);
- //设置PE10 PF10 PE8为输出
- GPIOE->MODER &= (~(0X3<<20));
- GPIOE->MODER |= (0X1<<20);
- GPIOF->MODER &= (~(0X3<<20));
- GPIOF->MODER |= (0X1<<20);
- GPIOE->MODER &= (~(0X3<<16));
- GPIOE->MODER |= (0X1<<16);
- //设置推挽输出
- GPIOE->OTYPER &= (~(0X1<<10));
- GPIOF->OTYPER &= (~(0X1<<10));
- GPIOE->OTYPER &= (~(0X1<<8));
- //设置三个管脚低速输出
- GPIOE->OSPEEDR &= (~(0X3<<20));
- GPIOF->OSPEEDR &= (~(0X3<<20));
- GPIOE->OSPEEDR &= (~(0X3<<16));
- //设置三个管脚输出时无上拉电阻和下拉电阻
- GPIOE->PUPDR &= (~(0X3<<20));
- GPIOF->PUPDR &= (~(0X3<<20));
- GPIOE->PUPDR &= (~(0X3<<16));
-
- // GPIOE->ODR &= (~(0X1<<10));
- // GPIOF->ODR &= (~(0X1<<10));
- // GPIOE->ODR &= (~(0X1<<8));
-
- }
- //PE9风扇
- void fan_init()
- {
- //RCC使能GPIOE时钟
- RCC->MP_AHB4ENSETR |= (0X1<<4);
- //设置PE9为输出
- GPIOE->MODER &= (~(0X3<<18));
- GPIOE->MODER |= (0X1<<18);
- //设置推挽输出
- GPIOE->OTYPER &= (~(0X1<<9));
- //设置三个管脚低速输出
- GPIOE->OSPEEDR &= (~(0X3<<18));
- //设置三个管脚输出时无上拉电阻和下拉电阻
- GPIOE->PUPDR &= (~(0X3<<18));
-
- // GPIOE->ODR &= (~(0X1<<9));
- }
-
- //PB6蜂鸣器
- void sp_init()
- {
- //RCC使能
- RCC->MP_AHB4ENSETR |= (0x1<<1);
- //设置PE9为输出
- GPIOB->MODER &= (~(0X3<<12));
- GPIOB->MODER |= (0X1<<12);
- //设置推挽输出
- GPIOB->OTYPER &= (~(0X1<<6));
- //设置三个管脚低速输出
- GPIOB->OSPEEDR &= (~(0X3<<12));
- //设置三个管脚输出时无上拉电阻和下拉电阻
- GPIOB->PUPDR &= (~(0X3<<12));
-
- // GPIOB->ODR &= (~(0X1<<6));
- }
- #include"key.h"
- extern void printf(const char *fmt, ...);
- unsigned int i = 0;
- void do_irq(void)
- {
- //获取终端号
- int GICC_num=GICC->IAR&0x3ff;
- //判断终端号
- switch (GICC_num)
- {
- case 99: //key1
- printf("key1\n");
- GPIOE->ODR ^= (0X1<<10);
- GPIOF->ODR ^= (0X1<<10);
- GPIOE->ODR ^= (0X1<<8);
- //清除EXTI
- EXTI->FPR1|=(0x1<<9); //key1
- //清除终端挂起标志位,清除终端号
- GICD->ICPENDR[3]|=(0x1<<3); //key1
-
- break;
-
- case 97: //key2
- printf("key2\n");
- GPIOB->ODR ^= (0X1<<6);
- //清除EXTI
- EXTI->FPR1|=(0x1<<7); //key2
- //清除终端挂起标志位,清除终端号
- GICD->ICPENDR[1]|=(0x1<<1); //key2
-
- break;
- case 98:
- printf("key3\n");
- GPIOE->ODR ^= (0X1<<9);
- //清除EXTI
- EXTI->FPR1|=(0x1<<8); //key3
- //清除终端挂起标志位,清除终端号
- GICD->ICPENDR[2]|=(0x1<<2); //key3
-
- break;
- }
- //清除GICC
- GICC->EOIR=GICC_num;
- }
main.c
- #include "uart.h"
- #include "key.h"
- int main()
- {
-
- key_config();
- all_led_init();
- fan_init();
- sp_init();
-
- while(1)
- {
-
- }
-
- return 0;
- }