练习:
封装exti,cic初始化函数
//EXTI初始化
void hal_key_exti_init(int id,int exticr,int mode){
//获取偏移地址
int address_offset = (id%4)*8;
//获取寄存器编号
int re_ser = (id/4)+1;
//
printf("address_offset=%d,re_ser=%d\n",address_offset,re_ser);
//1.设置外部中断选择寄存器进行连接
if(re_ser==1){
EXTI->EXTICR1 &= ~(0xff<<address_offset);
EXTI->EXTICR1 |= (exticr<<address_offset);
}else if(re_ser==2){
EXTI->EXTICR2 &= ~(0xff<<address_offset);
EXTI->EXTICR2 |= (exticr<<address_offset);
}else if(re_ser==3){
EXTI->EXTICR3 &= ~(0xff<<address_offset);
EXTI->EXTICR3 |= (exticr<<address_offset);
}else if(re_ser==4){
EXTI->EXTICR4 &= ~(0xff<<address_offset);
EXTI->EXTICR4 |= (exticr<<address_offset);
}
//2.设置触发方式
if(mode==1){
EXTI->FTSR1 &= ~(0x1<<id);
EXTI->RTSR1 |= (0x1<<id);
}else if(mode==0){
EXTI->RTSR1 &= ~(0x1<<id);
EXTI->FTSR1 |= (0x1<<id);
}
//3.设置EXTI层中断不屏蔽
EXTI->C1IMR1 |= (0x1<<id);
}
//GIC初始化
void hal_key_gic_init(int id,int interrupt_lv){
//获取中断id对应使能寄存器偏移量
int address_offset = id%32;
//获取中断id对应使能寄存器
int re_enable = id/32;
//
printf("address_offset=%d,re_enable=%d\n",address_offset,re_enable);
//GICD
//1.使能GICD组0
GICD->CTRL |= (0x1<<0);
//2.设置对应中断号使能
GICD->ISENABLER[re_enable] |= (0x1<<address_offset);
//2.设置GICD层中断优先级
GICD->IPRIORITYR[re_enable*8] &= (~(interrupt_lv<<(address_offset*8+3)));
//3.设置GICD层中断分配
GICD->ITARGETSR[re_enable*8] &= (~(0x3<<(address_offset*8)));
GICD->ITARGETSR[re_enable*8] |= (0x1<<(address_offset*8));
//1.设置GICC层组0使能
GICC->CTRL |= (0x1<<0);
//2.设置GICC层中断优先级
GICC->PMR &= (~(0x1f<<3));
GICC->PMR |= (0x1f<<3);
}