ARM中断处理实现
1. 中断概念
为什么要有中断?cpu和外设之间要进行通信,那么cpu怎么知道外设有数据来,有两种方式:
2. 中断工作流程图
cpu处理步骤:
1.保存现场
2.向中断控制器查询是哪个中断
3.执行中断处理程序
4.恢复现场
GPIO控制器:
1.CON:功能配置寄存器
2.INTC:触发方式
3.PEND:中断记录标志位
4.MASK:开关
3. 中断控制器工作流程图
以其中的一组中断控制为例:
每32个中断源为一组,fiq为快速中断,irq为普通中断,vic中有32个寄存器与中断源一一对应,存储的是中断函数的地址,address寄存器保存当前中断的中断函数地址,当enable打开时才会通知cpu有中断来临
4. 外部中断驱动实现
4.1 汇编程序
start.s:
.global _start
_start:
b reset
b undef
b swi
b abt_pre
b abt_dat
b reserved
b irq
b fiq
reset:
@重新映射异常向量表到0x40008000
ldr r0, =0x40008000
mcr p15, 0, r0, c12, c0, 0
@打开中断的总开关
mrs r0, cpsr
bic r0, r0, #(0x1 << 7)
msr cpsr, r0
bl main
undef:
swi:
abt_pre:
abt_dat:
reserved:
irq:
ldr sp, =0x420000000 @初始化栈指针
stmfd sp!, {r0-r12,lr}
bl irq_handler @调到中断函数
ldmfd sp!, {r0-r12,lr}
subs pc, lr, #4
fiq:
4.2 C语言程序
main.c:
#define VICOINTSELECT (*(volatile unsigned int *)0xF200000C)
#define VIC0VECTADDR (*(volatile unsigned int *)0xF2000100)
#define VIC0INTENABLE (*(volatile unsigned int *)0xF2000010)
#define VIC0ADDRESS (*(volatile unsigned int *)0xF2000F00)
#define GPH0CON (*(volatile unsigned int *)0xE0200C00))
#define EXT_INT_0_CON (*(volatile unsigned int *)0xE0200E00)
#define EXT_INT_0_MASK (*(volatile unsigned int *)0xE0200F00)
#define EXT_INT_0_PEND (*(volatile unsigned int *)0xE0200F40)
void key1_handler(void)
{
int (*printf)(char *format,...)=(void *)0x3ff13e54;
printf("hello key1..\n");
//清理GPIO里面的中断记录
EXT_INT_0_PEND = 0x1;
}
int main()
{
//初始化中断控制器
VICOINTSELECT = 0;
VIC0VECTADDR = (unsigned int)key1_handler;
VIC0INTENABLE |= 0x1; //开启0号中断
//初始化按键,GPIO
GPH0CON |=0xf;//设置成中断功能
EXT_INT_0_CON &=~0xf;
EXT_INT_0_CON |=0x3;
EXT_INT_0_MASK &=~0x1;
while(1)
{
;
}
return 0;
}
//中断处理函数
void irq_handler()
{
void(*handler)();
handler=(void *)VIC0ADDRESS ;
handler();
//清理中断控制器
VIC0ADDRESS=0;
}
4.3 编译和运行
PS:注意链接部分语句
上机结果如下:
5. 优化中断程序
5.1 将所有中断的操作封装
irq.c:
#define VICOINTSELECT (*(volatile unsigned int *)0xF200000C)
#define VIC1INTSELECT (*(volatile unsigned int *)0xF210000C)
#define VIC2INTSELECT (*(volatile unsigned int *)0xF220000C)
#define VIC3INTSELECT (*(volatile unsigned int *)0xF230000C)
#define VIC0VECTADDR (volatile unsigned int *)0xF2000100
#define VIC1VECTADDR (volatile unsigned int *)0xF2100100
#define VIC2VECTADDR (volatile unsigned int *)0xF2200100
#define VIC3VECTADDR (volatile unsigned int *)0xF2300100
#define VIC0INTENABLE (*(volatile unsigned int *)0xF2000010)
#define VIC1INTENABLE (*(volatile unsigned int *)0xF2100010)
#define VIC2INTENABLE (*(volatile unsigned int *)0xF2200010)
#define VIC3INTENABLE (*(volatile unsigned int *)0xF2300010)
#define VIC0ADDRESS (*(volatile unsigned int *)0xF2000F00)
#define VIC1ADDRESS (*(volatile unsigned int *)0xF2100F00)
#define VIC2ADDRESS (*(volatile unsigned int *)0xF2200F00)
#define VIC3ADDRESS (*(volatile unsigned int *)0xF2300F00)
//中断控制器初始化函数
int request_irq(int irq, void(*handler)())
{
if(irq>=0 && irq <= 31)
{
VICOINTSELECT = 0;//初始化中断控制器
VIC0VECTADDR[irq] = (unsigned int)handler; //注册中断函数
VIC0INTENABLE |= 0x1<<irq; //开启中断
}
else if(irq>=32 && irq<=63)
{
irq-=32;
VIC1INTSELECT = 0;
VIC1VECTADDR[irq] = (unsigned int)handler;
VIC1INTENABLE |= 0x1<<irq; //开启中断
}
else if(irq>=64 && irq <=95)
{
irq-=64;
VIC2INTSELECT = 0;
VIC2VECTADDR[irq] = (unsigned int)handler;
VIC2INTENABLE |= 0x1<<irq; //开启中断
}
else if(irq>=96 && irq <=128)
{
irq-=96;
VIC3INTSELECT = 0;
VIC3VECTADDR[irq] = (unsigned int)handler;
VIC3INTENABLE |= 0x1<<irq; //开启中断
}
}
//中断处理函数
void irq_handler()
{
void(*handler)();
//判断是那个中断发生
if(VIC0ADDRESS !=0 )
handler=(void *)VIC0ADDRESS;
else if(VIC1ADDRESS !=0)
handler=(void *)VIC1ADDRESS;
else if(VIC2ADDRESS !=0)
handler=(void *)VIC2ADDRESS;
else if(VIC3ADDRESS !=0)
handler=(void *)VIC3ADDRESS;
//执行绑定的中断处理函数
handler();
//清理中断控制器
VIC0ADDRESS=0;
VIC1ADDRESS=0;
VIC2ADDRESS=0;
VIC3ADDRESS=0;
}
5.2 修改main
修改main.c:
#define GPH0CON (*(volatile unsigned int *)0xE0200C00))
#define EXT_INT_0_CON (*(volatile unsigned int *)0xE0200E00)
#define EXT_INT_0_MASK (*(volatile unsigned int *)0xE0200F00)
#define EXT_INT_0_PEND (*(volatile unsigned int *)0xE0200F40)
void key1_handler(void)
{
int (*printf)(char *format,...)=(void *)0x3ff13e54;
printf("hello key1..\n");
//清理GPIO里面的中断记录
EXT_INT_0_PEND = 0x1;
}
int main()
{
//初始化中断控制器
request_irq(0,key1_handler);
//初始化按键,GPIO
GPH0CON |=0xf;//设置成中断功能
EXT_INT_0_CON &=~0xf;
EXT_INT_0_CON |=0x3;
EXT_INT_0_MASK &=~0x1;//使能
while(1)
{
;
}
return 0;
}
5.3 编译和运行
6. WDT中断实现
看门狗定时器(WDT,Watch Dog Timer)是单片机的一个组成部分,它实际上是一个计数器,一般给看门狗一个数字,程序开始运行后看门狗开始计数。如果程序运行正常,过一段时间CPU应发出指令让看门狗置零,重新开始计数。如果看门狗增加到设定值就认为程序没有正常工作,强制整个系统复位。
6.1 WDT驱动实现
wdt.c
#define WTCON (*(volatile unsigned int *)0xE2700000))
#define WTDAT (*(volatile unsigned int *)0xE2700004))
#define WTCNT (*(volatile unsigned int *)0xE2700008))
#define WTCLRINT (*(volatile unsigned int *)0xE270000C))
void key1_handler(void)
{
int (*printf)(char *format,...)=(void *)0x3ff13e54;
printf("hello wdt..\n");
//清理GPIO里面的中断记录
WTCLRINT = 1;//清理中断
}
void wdt_init()
{
WTCON = (65<<8)|(0x1<<5)|(0x1<<3)|(0x1<<2)|0;
//t=(32*(65+1))/ 66M = 32us
WTCNT = 1000*1000/32;//1s
WTDAT = 1000*1000/32; //首次喂狗
}
int main()
{
//初始化看门狗
wdt_init();
//初始化中断控制器
request_irq(27,wdt_handler);//看门狗是27号中断
while(1)
{
;
}
return 0;
}