三星6410裸机程序开发4:eclipse中实现6410中断功能

2023-05-05  

在调试三星6410裸机程序时,遇到的一个很棘手的问题:在eclipse中怎么实现中断?这个问题的实质是:GCC中怎么声明ARM的中断处理函数。


这个问题折腾了很久,有点影响了项目的进度。后面改为RVDS开发环境才得以避开这个问题。现在回过头来再分析分析这个问题。刚好看到一篇博文,结合自己的理解,我想应该可以很好地阐释和解决这个问题。


吐槽下

三星6410的中断带有中断处理协处理器VIC。使用VIC来管理中断,相比采用像51单片机那样的固定中断向量入口的方式来使用中断,不仅效率更高,而且更加容易使用,也更为灵活。


需要吐槽的是,友善之臂Tiny6410板光盘提供的中断示例都是像51单片机那样的固定中断向量入口的方式来使用中断。总而言之,友善之臂提供的裸机程序只能用做参考或者入门,实际意义并不大。


__irq关键字

刚开始时,我并没注意__irq关键字,编写的中断服务程序(ISR)跟其它函数一样。直到自己编写的中断程序只能运行一下,然后整个程序就不动了,才注意到它的存在。

* 在C语言中,关键字”__irq”的作用:当ISR定义时有此关键字,则ISR结束后CPU自动从栈中恢复中断前
* 模式的LR,并把它赋值给PC,完成ISR的正常返回。如果无此关键字,则CPU只能返回到二级ISR前的中
* 断状态,此时仍为IRQ工作模式。当然也能够继续执行用户程序,只是工作模式不对,此模式下再不能响
* 应其它IRQ中断。
*
* 事实上,CPU响应中断并执行ISR相当于一个程序调用过程。用户程序不必干预CPU的模式切换、现场保
* 护、程序返回。
*
* 如果在执行中断服务函数之前没有对中断现场进行保护,那么中断服务函数必须要使用“__irq”关键字进
* 行声明。例如,在0x0000 0018处执行指令“LDR PC, [PC, #-0xff0]”,此时对应的中断服务函数必
* 须要使用“__irq”关键字进行声明;如果在执行中断服务函数之前已经对中断现场进行了保护,那么中断
* 服务函数不能使用“__irq”关键字进行声明。

关于__irq的含义,引述下ADS手册中的描述:

5.5.1 Simple interrupt handlers in C

You can write simple C interrupt handlers by using the __irq function declaration keyword. You can use the __irq keyword both for simple one-level interrupt handlers, and interrupt handlers that call subroutines. However, you cannot use the __irq keyword for reentrant interrupt handlers, because it does not cause the SPSR to be saved or restored. In this context, reentrant means that the handler re-enables interrupts, and may itself be interrupted. See Reentrant interrupt handlers on page 5-26 for more information.

The __irq keyword:

• preserves all ATPCS corruptible registers

• preserves all other registers (excluding the floating-point registers) used by the function

• exits the function by setting the program counter to (lr – 4) and restoring the CPSR to its original value.

If the function calls a subroutine, __irq preserves the link register for the interrupt mode in addition to preserving the other corruptible registers. See Calling subroutines from interrupt handlers for more information.

Note

C interrupt handlers cannot be produced in this way using tcc. The __irq keyword is faulted by tcc because tcc can only produce Thumb code, and the processor is always switched to ARM state when an interrupt, or any other exception, occurs.

However, the subroutine called by an __irq function can be compiled for Thumb, with interworking enabled. See Chapter 3 Interworking ARM and Thumb for more information on interworking.

Calling subroutines from interrupt handlers

If you call subroutines from your top-level interrupt handler, the __irq keyword also restores the value of lr_IRQ from the stack so that it can be used by a SUBS instruction to return to the correct address after the interrupt has been handled.

Example 5-13 on page 5-25 shows how this works. The top level interrupt handler reads the value of a memory-mapped interrupt controller base address at 0x80000000. If the value of the address is 1, the top-level handler branches to a handler written in C.

Handling Processor Exceptions ARM DUI 0056D Copyright © 1999-2001 ARM Limited. All rights reserved. 5-25

01 Example 5-13
02
03 __irq void IRQHandler (void)
04
05 {
06
07     volatile unsigned int *base = (unsigned int *) 0x80000000;
08
09     if (*base == 1) // which interrupt was it?
10
11     {
12
13         C_int_handler(); // process the interrupt
14
15     }
16
17     *(base+1) = 0; // clear the interrupt
18
19 }
20
21 Compiled with armcc, Example 5-13 produces the following code:
22
23 IRQHandler PROC
24
25 STMFD sp!,{r0-r4,r12,lr}
26
27 MOV r4,#0x80000000
28
29 LDR r0,[r4,#0]
文章来源于:电子工程世界    原文链接
本站所有转载文章系出于传递更多信息之目的,且明确注明来源,不希望被转载的媒体或个人可与我们联系,我们将立即进行删除处理。