/*
实现三个按键的中断控制LED灯:
按键1按下,LED1亮,按键1抬起,LED1灭
按键2按下,LED2亮,按键2抬起,LED2灭
按键3按下,LED3亮,按键3抬起,LED3灭
key1 PF9
key2 PF7
key3 PF8
*/
key.h
#ifndef __KEY_H__
#define __KEY_H__
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_exti.h"
#include "stm32mp1xx_gic.h"
void key_init();
#endif
key.c
#include "key.h"
/*
实现三个按键的中断控制LED灯:
按键1按下,LED1亮,按键1抬起,LED1灭
按键2按下,LED2亮,按键2抬起,LED2灭
按键3按下,LED3亮,按键3抬起,LED3灭
key1 PF9
key2 PF7
key3 PF8
*/
void key_gpio_init(){
RCC->MP_AHB4ENSETR |= (0x1<<5);
GPIOF->MODER &= (~(0x3F<<0xE));
}
void key_exti_init(){
/*=====================================================1: falling edge trigger
function : set falling edge trigger for PF7,PF8,PF9
register : EXTI_FTSR1
bits name : FT
bits fieled : [7] [8] [9]
value : 0: Falling trigger disabled (for event and Interrupt) for input line
1: Falling trigger enabled (for event and Interrupt) for input line.
*/
EXTI->FTSR1 |= (0x7<<0x7);
/*=====================================================2: GPIO port select
function : identify signal from which GPIO port
register : EXTI_EXTICR2
bits name : EXTI7
bits fieled : [31:24]
value : 0x00: PA[7] pin
0x01: PB[7] pin
0x02: PC[7] pin
0x03: PD[7] pin
0x04: PE[7] pin
0x05: PF[7] pin
0x06: PG[7] pin
0x07: PH[7] pin
0x08: PI[7] pin
0x09: PJ[7] pin
0x0A: PK[7] pin
0x0B: PZ[7] pin
*/
EXTI->EXTICR2 &= (~(0xFF<<0x18));
EXTI->EXTICR2 |= (0x05<<0x18);
/*
function : identify signal from which GPIO port
register : EXTI_EXTICR3
bits name : EXTI8
bits fieled : [7:0]
value : 0x00: PA[8] pin
0x01: PB[8] pin
0x02: PC[8] pin
0x03: PD[8] pin
0x04: PE[8] pin
0x05: PF[8] pin
0x06: PG[8] pin
0x07: PH[8] pin
0x08: PI[8] pin
0x09: PJ[8] pin
0x0A: PK[8] pin
0x0B: PZ[8] pin
*/
EXTI->EXTICR3 &= (~(0xFF));
EXTI->EXTICR3 |= (0x05);
/*
function : identify signal from which GPIO port
register : EXTI_EXTICR3
bits name : EXTI9
bits fieled : [15:8]
value : 0x00: PA[9] pin
0x01: PB[9] pin
0x02: PC[9] pin
0x03: PD[9] pin
0x04: PE[9] pin
0x05: PF[9] pin
0x06: PG[9] pin
0x07: PH[9] pin
0x08: PI[9] pin
0x09: PJ[9] pin
0x0A: PK[9] pin
0x0B: PZ[9] pin
*/
EXTI->EXTICR3 &= (~(0xFF<<0x8));
EXTI->EXTICR3 |= (0x05<<0x8);
/*=====================================================3: mask register set
function : set wakeup with interrupt mask register
register : EXTI_IMR1
bits name : IM
bits fieled : [9][8][7]
value : 0: wakeup with interrupt request from Line x is masked
1: wakeup with interrupt request from Line x is unmasked
*/
EXTI->C1IMR1 |= (~(0x7<<0x7));
}
void key_gic_init(){
/*
The GICD provides a programming interface for:
• globally enabling the forwarding of interrupts to the CPU interfaces
• enabling or disabling each interrupt
• setting the priority level of each interrupt
• setting the target processor list of each interrupt
• setting each peripheral interrupt to be level-sensitive or edge-triggered
• setting each interrupt as either group 0 or group 1
• sending an SGI to one or more target processors
• visibility of the state of each interrupt
• a mechanism for software to set or clear the pending state of a peripheral interrupt.
*/
/*=====================================================GICD 1: control register
function : set the group0 to forward to CPU
register : GICD_CTLR (GICD control register)
bits name : ENABLEGRP0/ENABLEGRP1
bits fieled : [1] [0]
value : Bit 1 ENABLEGRP1: enable group 1 interrupts
Global enable for forwarding pending group 1 interrupts from the
GICD to the CPU interfaces
Bit 0 ENABLEGRP0: enable group 0 interrupts
Global enable for forwarding pending group 0 interrupts from the
GICD to the CPU interfaces
*/
GICD->CTLR |= 1;
/*=====================================================GICD 2: set-enable register
EXTI7 interupt ID=97
EXTI8 interupt ID=98
EXTI9 interupt ID=99
function : set-enable EXTI7 EXTI8 EXTI9
register : GICD_ISENABLERx (GICD interrupt set-enable register ) (x = 0 to 8)
bits name :
bits fieled : 97/32=3...1 98/32=3...2 99/32=3...3
value : 1: interrupt set-enable
*/
GICD->ISENABLER[3] |= (0x7<<0x1);
/*=====================================================GICD 3: priority register
EXTI7 interupt ID=97
EXTI8 interupt ID=98
EXTI9 interupt ID=99
function : set priority for EXTI7 EXTI8 EXTI9
register : GICD_IPRIORITYRx (GICD interrupt priority register x) (x = 0 to 71)
bits name :
bits fieled : 97/4=24...1 98/4=24...2 99/4=24...3
value : 00000
*/
GICD->IPRIORITYR[24] &= (~(0x1F<<0xB));
GICD->IPRIORITYR[24] &= (~(0x1F<<0x13));
GICD->IPRIORITYR[24] &= (~(0x1F<<0x1B));
/*=====================================================GICD 4: set target processor
EXTI7 interupt ID=97
EXTI8 interupt ID=98
EXTI9 interupt ID=99
function : set target processor for EXTI7 EXTI8 EXTI9
register : GICD_ITARGETSRx(GICD interrupt processor target register x)(x=0to71)
bits name :
bits fieled : 97/4=24...1 98/4=24...2 99/4=24...3
value : x1: CPU0
1x: CPU1
*/
GICD->IPRIORITYR[24] |= (0x1<<0x8);
GICD->IPRIORITYR[24] |= (0x1<<0x10);
GICD->IPRIORITYR[24] |= (0x1<<0x18);
/*=====================================================GICC 1: control register
function : set the group0 to forward to CPU
register : GICC_CTLR (GICC control register)
bits name : ENABLEGRP0/ENABLEGRP1
bits fieled : [0]
value : Bit 1 ENABLEGRP1: Enable group 1 interrupts
It enables for the signaling of group 1 interrupts by the CPU
interface to the connected processor.
Bit 0 ENABLEGRP0: Enable group 0 interrupts
It enables for the signaling of group 0interrupts by the CPU
interface to the connected processor.
*/
GICC->CTLR |= 1;
/*=====================================================GICC 2: priority mask
function : set the priority mask
register : GICC_PMR (GICC input priority mask register)
bits name : PRIORITY[4:0]:
bits fieled : [7:3 ]
value : priority mask level for the CPU interface
If the priority of an interrupt is higher than the value indicated
by this field, the interface signals the interrupt to the processor.
*/
GICC->PRIORITY |= (0x1f<<0x3);
}
void key_init(){
key_gpio_init();
key_exti_init();
key_gic_init();
}
main.c
#include"led.h"
#include "uart4.h"
//封装延时函数
void delay(int ms){
int i,j;
for(i=0;i<ms;i++){
for(j=0;j<2000;j++){}
}
}
int main()
{
all_led_init();
uart4_init();
key_init();
while(1){
printf("in main function\n\r");
delay(1000);
}
return 0;
}