开关中断控制
main.c
#include "key_it.h"
#include "uart.h"
void delay(int ms)
{int i,j;for(i=0;i<ms;i++){for(j=0;j<2000;j++);}
}int main()
{key2_it_config();key1_it_config();key3_it_config();all_led_init();char buf[128];gets(buf); while (1){puts(buf);delay(1000);}
}// 最终现象:键盘输入a,串口工具显示b
key_it.c
#include "key_it.h"
void key1_it_config()
{//使能GPIOF时钟RCC->MP_AHB4ENSETR |= (0x1<<5);//设置PF9管脚为输入GPIOF->MODER &= (~(0x3<<18));//设置PF9作为EXTI9事件的输入EXTI->EXTICR3 &= (~(0x1f<<8));EXTI->EXTICR3 |= (0x05<<8);//设置下降沿使能检测EXTI9事件EXTI->FTSR1 |= (0x1<<9);//设置EXTI9事件不屏蔽EXTI->C1IMR1 |= (0x1<<9);//GICD使能EXTI9(99号)中断GICD->ISENABLER[3] |= (0x1<<3);//GICD 设置99号中断的优先级GICD->IPRIORITYR[24] &= (~(0x1f<<27));//选择CPU0处理当前中断GICD->ITARGETSR[24] |= (0x1<<24);//全局使能组0中断被转发到GICCGICD->CTRL |= (0X1);//设置中断的优先级掩码GICC->PMR |= (0x1f<<3);//允许组0中断被CPU处理GICC->CTRL |= (0x1);
}
void key3_it_config()
{//使能GPIOF时钟RCC->MP_AHB4ENSETR |= (0x1<<5);//设置PF8管脚为输入GPIOF->MODER &= (~(0x3<<16));//设置PF8作为EXTI8事件的输入EXTI->EXTICR3 &= (~(0xff<<0));EXTI->EXTICR3 |= (0x05<<0);//设置下降沿使能检测EXTI8事件EXTI->FTSR1 |= (0x1<<8);//设置EXTI8事件不屏蔽EXTI->C1IMR1 |= (0x1<<8);//-------------------------------------------------//GICD使能EXTI8(98号)中断GICD->ISENABLER[3] |= (0x1<<2);//GICD 设置98号中断的优先级GICD->IPRIORITYR[24] &= (~(0x1f<<19));//选择CPU0处理当前中断GICD->ITARGETSR[24] |= (0x1<<16);//全局使能组0中断被转发到GICCGICD->CTRL |= (0X1);//设置中断的优先级掩码GICC->PMR |= (0x1f<<3);//允许组0中断被CPU处理GICC->CTRL |= (0x1);
}
void key2_it_config()
{//使能GPIOF时钟RCC->MP_AHB4ENSETR |= (0x1<<5);//设置PF7输入模式
GPIOF->MODER &=(~(0x3<<14));//设置PF7作为EXTI7事件输入
EXTI->EXTICR2 &=(~(0xff<<24));
EXTI->EXTICR2 |=(0x5<<24);
//设置下降沿检测
EXTI->FTSR1 |=(0x1<<7);
//设置EXTI7事件不屏蔽
EXTI->C1IMR1 |=(0x1<<7);//使能GICD中断
GICD->ISENABLER[3] |=(0x1<<1);
//设置GICD中断优先级
GICD->IPRIORITYR[24] &=(~(0x1f<<11));
//设置cpu0处理当前中断
GICD->ITARGETSR[24] |=(0x1<<8);
//全局使能使组0中断可以被转发到GICC
GICD->CTRL |=(0x1);//设置优先级掩码
GICC->PMR |=(0x1f<<3);
//设置允许组0中断被cpu处理
GICC->CTRL |=(0x1);
}
do_it.c
#include "key_it.h"
#include "uart.h"
extern void printf(const char *fmt, ...);
unsigned int i = 0;
void do_irq(void)
{int irqno = (GICC->IAR & 0x3ff);switch (irqno){case 99: // key1puts("KEY1_INT\n");// 清除GICD的中断排队标志GICD->ICPENDR[3] |= (0x1<<3);// 清除exti的事件触发标志EXTI->FPR1 |= (0x1<<9);break;case 97: // KEY2puts("KEY2_INT\n");//清除GICD的中断排队标志GICD->ICPENDR[3] |=(0x1<<1);//清除EXTI的事件触发标志EXTI->FPR1 |=(0x1<<7);break;case 98: // key3led3_on();// 清除GICD的中断排队标志
GICD->ICPENDR[3] |=(0x1<<2);//清除EXTI的事件触发标志EXTI->FPR1 |=(0x1<<8);break;}// 清除中断号GICC->EOIR = irqno;
}
key_it.h
#ifndef __KEY_IT_H__
#define __KEY_IT_H__
#include"stm32mp1xx_gpio.h"
#include"stm32mp1xx_gic.h"
#include"stm32mp1xx_exti.h"
#include"stm32mp1xx_rcc.h"
void key1_it_config();
void key2_it_config();
void key3_it_config();
#endif