这个问题困扰了我很久,2440中断到底是怎样一个怎样的机制? 自己花了很大的力气终于弄明白了,在这里和大家交流一下.
中断的实现是由硬件和软件机制结合工作的,把它们抽象出来 :由中断异常作为一个源点,在一定机制下,从表一跳至表二,再跳至表三,
表一:
表二:
^ _ISR_STARTADDRESS ; _ISR_STARTADDRESS=0x33FF_FF00
HandleReset # 4
HandleUndef # 4
HandleSWI # 4
HandlePabort # 4
HandleDabort # 4
HandleReserved # 4
HandleIRQ # 4
HandleFIQ # 4
表三:
;Do not use the label 'IntVectorTable',
;The value of IntVectorTable is different with the address you think it may be.
;IntVectorTable
;@0x33FF_FF20
HandleEINT0 # 4
HandleEINT1 # 4
HandleEINT2 # 4
HandleEINT3 # 4
HandleEINT4_7 # 4
HandleEINT8_23 # 4
HandleCAM # 4 ; Added for 2440.
HandleBATFLT # 4
HandleTICK # 4
HandleWDT # 4
HandleTIMER0 # 4
HandleTIMER1 # 4
HandleTIMER2 # 4
HandleTIMER3 # 4
HandleTIMER4 # 4
HandleUART2 # 4
;@0x33FF_FF60
HandleLCD # 4
HandleDMA0 # 4
HandleDMA1 # 4
HandleDMA2 # 4
HandleDMA3 # 4
HandleMMC # 4
HandleSPI0 # 4
HandleUART1 # 4
HandleNFCON # 4 ; Added for 2440.
HandleUSBD # 4
HandleUSBH # 4
HandleIIC # 4
HandleUART0 # 4
HandleSPI1 # 4
HandleRTC # 4
HandleADC # 4
;@0x33FF_FFA0
跳转过程详解:
1.异常 --> 表一
这是由硬件机制决定的,当发生异常时,pc自动档指向与之相对应的异常地址,该地址是由硬件决定的,不可更改。
2.表一 --> 表二
表一对应的地址中存储了 : b HandlerIRQ ;handler for IRQ interrupt
HandlerLRQ 是一个标号,实际值为一个地址
这里我们先来看一下下面这个宏,和该宏的一个定义:
MACRO
$HandlerLabel HANDLER $HandleLabel
$HandlerLabel
sub sp,sp,#4 ;decrement sp(to store jump address)
stmfd sp!,{r0} ;PUSH the work register to stack(lr does not push because it return to original address)
ldr r0,=$HandleLabel;load the address of HandleXXX to r0
ldr r0,[r0] ;load the contents(service routine start address) of HandleXXX
str r0,[sp,#4] ;store the contents(ISR) of HandleXXX to stack
ldmfd sp!,{r0,pc} ;POP the work register and pc(jump to ISR)
MEND
下面是一个宏定义,
HandlerFIQ HANDLER HandleFIQ
宏展开结果:
HandlerFIQ
sub sp,sp,#4 ;decrement sp(to store jump address)
stmfd sp!,{r0} ;PUSH the work register to stack(lr does not push because it return to original address)
ldr r0,=HandleFIQ;load the address of HandleXXX to r0
ldr r0,[r0] ;load the contents(service routine start address) of HandleXXX
str r0,[sp,#4] ;store the contents(ISR) of HandleXXX to stack
ldmfd sp!,{r0,pc} ;POP the work register and pc(jump to ISR)
在上面宏展开后,我们发现了HandleFIQ,没错就是表二中的第七个地址,这样在这个名为HANDLER宏的作用下我们由表一跳至了表二;
但细心的你会发现: ldr r0,=HandleFIQ
ldr r0,[r0]
这里最终是把HandleFIQ值赋给了r0,不只是pc跳到表二那么简单,哈哈!先别急看看下面;
3. 表二 --> 表三
接上面的分析
; Setup IRQ handler
ldr r0,=HandleIRQ ;This routine is needed
ldr r1,=IsrIRQ ;if there is not 'subs pc,lr,#4' at 0x18, 0x1c
str r1,[r0]
这里把一个标号IsrIRQ的值赋给了表二中的HandleIRQ,上面不是说pc指向了HandleIRQ值,那IsrIRQ是一个什么呢?
IsrIRQ
sub sp,sp,#4 ;reserved for PC
stmfd sp!,{r8-r9}
ldr r9,=INTOFFSET
ldr r9,[r9]
ldr r8,=HandleEINT0
add r8,r8,r9,lsl #2
ldr r8,[r8]
str r8,[sp,#8]
ldmfd sp!,{r8-r9,pc}
LTORG
从这段代码中我们看见了一个很熟悉的INTOFFSET,它是这个寄存器的值,其值表示当前pend的中断号,
这样pc依据INTOFFSET,跳至对应表三的地址,
4.表三 --> 中断服务函数
这是最令人激动的一部,哈哈!终于到c函数了,
pISR_EINT1 = (U32)Key1_ISR;
该等式是把中断服务函数的地址值赋给pISR_EINT1,而pISR_EINT1即为表三中的HandleEINT0 # 4;
static void __irq Key1_ISR(void) //EINT1
{
int led;
rSRCPND = rSRCPND | (0x1<<1);
rINTPND = rINTPND | (0x1<<1);
led = rGPBDAT & (0x1<<5);
if (led ==0)
rGPBDAT = rGPBDAT | (0x1<<5);
else
rGPBDAT = rGPBDAT & ~(0x1<<5);
}