Академический Документы
Профессиональный Документы
Культура Документы
Overview
Definitions Hardware interrupts and error conditions Exception modes Vector table Exception handlers SWI Interrupts IRQ and FIQ Vector interrupt controller A complete structure for handlers
Definitions
Exception is an event that requires the CPU to perform an action breaking the normal flow of a program.
Benign events like someone moving a mouse or pushing a button Catastrophic faults such as bus error
Exceptions should be anticipated to help find the cause of the problem during application development or to plan for graceful shutdown. Exceptions can be categorized into two large classes: interrupts and error conditions Interrupts are exceptions raised asynchronously by I/O devices so that they can be served by the CPU. Software Interrupt is an exception that can be raised within the application using SWI instruction. It is a user defined synchronous exception.
3
Hardware Interrupts
ERROR CONDITIONS
Error exceptions occur quite often in an embedded system. So often that software needs to be sufficiently robust to handle them. Undefined instruction
Floating-point instructions and emulate them in software.
Data aborts
occurs when the processor attempts to grab data in memory that does not physically exist occurs when the processor attempts to write data in read only memory region
Prefetch abort
occurs when the processor attempts to grab an instruction from a memory and something goes wrong
6
Prefetch Abort: Occurs when the processor attempts to execute an instruction that was not fetched, because the address was illegal.
Data Abort: Occurs when a data transfer instruction attempts to load or store data at an illegal address. IRQ: Occurs when the processor external Interrupt ReQuest pin is asserted FIQ: Occurs when the processor external Fast Interrupt reQuest pin is asserted
Privileged Modes
CPSR
8
F I NZCV
FIQ Fast interrupts Disable bit IRQ Normal interrupt Disable bit Condition Flags
11
Registers in use
FIQ Mode r0 r1 r2 r3 r4 r5 r6 r7 r8_fiq r9_fiq r10_fiq r11_fiq r12_fiq r13_fiq r14_fiq r15 (pc) cpsr
spsr_fiq
EXCEPTION r8 r8_fiq r9 r9_fiq r10_fiq r10 r11 r11_fiq r12_fiq r12 r13_fiq r13 (sp) r14_fiq r14 (lr) Return address calculated from User mode PC value and stored in FIQ mode LR
spsr_fiq
Note that the processor is responsible for the above actions- no code needs to be written
14
The above actions can only be done in ARM state, and fortunately, the software can usually do these two operations with a single instruction at the end of the handler. MOVS PC, lr or SUBS PC, lr, #4
15
16
In the vector address, ARM uses actual instructions, often a change-of flow instruction types.
0x04 0x00
17
Literal pool containing address of undef handler FIQ handler follows vector table
0x18
0x14 0x10 0x0C 0x08 0x04 18 0x00
B IRQ_handler
(Reserved)
Data Abort
Prefetch Abort MOV PC,#0x30000000 LDR PC, [PC,#0xFF0] Reset
20
Exception Handlers
When an exception occurs, the processor responds
by saving CPSR into SPSR_<mode> by modifying the appropriate bits of the CPSR ( mode change) by saving the return address to LR_<mode> by loading the PC with the appropriate vector address in memory
21
All the above tasks are done by the processor, the focus for the programmer is to write the appropriate exception handler. Once the mode of the processor has been changed, the exception handler will have to access its own stack pointer, link register, and SPSR. Although there are some general purpose registers that can be used only by the handler, it is important to save off other registers that are common to previous mode to a stack before using them. STMFD SP!, {r0-r12, lr}
Exception Handlers
This process of storing registers on the stack can introduce unacceptable delay depending on the type of exception. To reduce such delay, FIQ mode has five additional general registers that the FIQ handler can access to. At the end of all handlers, the programmer is responsible for storing the state of the machine and returning back to the original instruction stream before the exception. This is done through an atomic operation, where the contents of SPSR are stored back into the CPSR while moving the Link register into the program counter. The instructions to do the atomic operation exist only in the ARM instruction set, that is why the processor had to switch from THUMB to ARM if it was executing THUMB code.
22
Exception Priorities
When multiple exceptions occur at the same time, the processor should know which one to attend first.
Suppose A/D converter has asserted IRQ line and at the same time the processor tries to access a memory location that is undefined while another high-priority interrupt tries to tell the processor that we are about to lose power in two minutes
Exception priorities
Priority Exception comment
Highest
Handler usually branches straight to main routine Can sometimes be helped with MMU Current instruction completes, then the interrupt is acknowledged Current instruction completes, then interrupt is acknowledged. Can sometimes be helped with hardware (MMU) Execution of the instruction causes the exception
SWI and Undef are actually exclusive, so they have the same priority
Lowest
Undefined instruction
23
Exception Priorities
In the above scenario, the data abort will be handled first, followed by the FIQ interrupt alerting the system to a power failure, and then the A/D converter will have its turn. Placing the Data Abort exception above the FIQ exception in the priority list ensures that the Data Abort is actually registered before the FIQ is handled. The Data Abort handler is entered first but control is then passed immediately to the FIQ handler. Once the FIQ has been handled, control returns to the Data Abort Handler. This means that the data transfer error does not escape detection as it would if the FIQ were handled first.
24
Exception Priorities
What if an exception occurs while the processor is handling an exception?
For example, data abort occurs while the processor is executing FIQ handler. In this case, the processor will attend to data abort first suspending FIQ handler. Once it has handled the data abort, the processor will resume FIQ handler from where it left off. Before jumping to the data abort handler, the processor will save the CPSR onto SPSR_<data abort>, modify the CPSR bits, save the return address on LR_<data abort> For example, another FIQ occurs while the processor is executing FIQ handler. In this case, the processor will block the new FIQ as FIQ interrupts are automatically disabled by the processor upon entry to FIQ mode.
25
Reset
Reset occurs when power is first connected to the CPU or when a reset switch is operated. Reset causes the program counter to be set to zero, receives its first instruction, which is usually a branch, interrupts are disabled and the CPU is in supervisor mode; CPSR bits F and I are set to 1, T is set to 0 indicating ARM mode. The branch instruction takes the reset to the first instruction of the reset handler, where initialization of the processor or micorcontroller is started.
Initialize the memory system. Initialize all required processor mode stacks and registers. Initialize any critical I/O devices Initialize any peripheral registers, control registers, or clocks such as PLL Enable interrupts Change processor mode and/or state
26
The instruction will raise an exception and forces the processor to go into a supervisor mode. The 24-bit immediate value is not used by the CPU; however, the programmer may use the 24-bit value to devise a system of a multiple software interrupts each leading to a different set of actions.
27
SWI_WriteC EQU 0x0 MOV r0, # A ; move ASCII code for character A into ro SWI SWI_WriteC ; output character in r0
The following call returns control from a user program back to the monitor program SWI_Exit EQU 0x11 SWI SWI_Exit ; finish
30
System
I/O Bus
31
causes the 8-bit value stored in the memory location stored in r1 to be sent to the host and the corresponding character is shown in the display window
causes the ARM program to wait until the semihosting window is active and a key is pressed on the host keyboard. The corresponding character to the key is returned in r0. The program uses the code to indicate that it has finished running to the host computer.
33
cond
0 0 0 1 0 R 0 0 1 1 1 1 Rd
000000000000
CPSR
34
cond
35
cond
0 0 0 1 0 R 0 0 fields 1111 0 0 0 0 0 0 0 0 Rm
36
Modifying CPSR
Unused reserved bits, may be used in future, therefore: they must be preserved when altering PSR the value they return must not be relied upon when testing other bits. Thus read-modify-write strategy must be followed when modifying any PSR: Transfer PSR to register using MRS Modify relevant bits Transfer updated value back to PSR using MSR Example: Change mode to supervisor MRS a1, CPSR BIC a1, a1,#0x1F ORR a1, a1, #0x13 MSR CPSR, a1 Note: In User Mode, all bits can be read but only the flag bits can be written to.
37
Interrupts
Hardware and software interrupts are handled by OS (Operating System)
Interrupts
Functions provided by OS (Operating System)
OS guarantees that users program accesses only the portions of I/O device to which user has rights (e.g., file access) OS provides abstractions for accessing devices by supplying routines that handle low-level device operations OS handles the interrupts generated by I/O devices (and other exceptions generated by a program) OS tries to provide equitable access to the shared I/O resources, as well as schedule accesses in order to enhance system performance
39
Interrupts
How to turn off interrupts during Exception routine? Bits in CPSR determines whether or not interrupts disabled: Interrupt Request bit (I) (1 disabled, 0 enabled) Once an exception occurs (I) bit sets to 1 automatically Return from exception restores the original value Fast Interrupt Request bit (F) only gets disabled if a fast Interrupt occurs How to prevent user program from turning off interrupts (forever)? Interrupt Request bit (I) and Fast Interrupt Request bit (F) bits can only be changed from the privileged modes
40
Interrupts
Preempting exceptions by interrupts? Suppose were dealing with a computer running a nuclear facility. What of were handling a floating-point divide by 0 exception and a nuclear meltdown imminent interrupt comes in? We need to categorize and allow some interrupts preempt other exceptions so we can handle them in order of urgency: emergency vs luxury
ARM Architecture Support to simplify software: An Exception Process sets IRQ bits (disables it). No further IRQ interrupt is possible However, No exception can disable FIQ interrupt
41
Interrupts
If an FIQ interrupt comes while servicing an exception Take FIQ immediately Return to interrupted exception code as soon as no more FIQ Interrupt State information for the preempted exception are saved on banked registers and as well as some internal registers All exceptions set IRQ disable interrupt bit (I) FIQ can interrupt IRQ and other exceptions How allow IRQ interrupts to interrupt other exceptions (other than IRQ and FIQ exceptions)? Interrupt service Routine needs to save: all registers that it is going to use on IRQ Mode Stack Reset the IRQ disable bit
42
ARM cores have two interrupt lines one for a fast interrupt (FIQ) and one for a low-priority interrupt (IRQ) Two interrupt request signals FIQ and IRQ never enough for all of the I/O devices Need a mechanism to attach multiple devices to the same IRQ pin. Two ways to do this:
to wire AND-OR the interrupts coming from the peripherals or external devices together. requires polling by the processor to determine the source of the interrupt ( not effective) To use an external interrupt controller, which is a specialized pieces of hardware that takes in all of the interrupt lines, assigns priorities to them and often provides additional information to help the processor, such as a register that can be read for to quickly determine who requested the interrupt.
Interrupts
43
VIC address
0xFFFFFF00 VIC FIQ
29 30 31
Processor
IRQ
0x18 : LDR PC, [PC, #-0x12]
When LDR instruction is executed, PC will contain 0x20 0x20 0x12 = 0xFFFFFF00
UART0 is connected to slot 6 of VIC Slot 1 is reserved for software interrupt Hardware interrupts of Timer0 and Timer1 are connected to slot 4 and 5 of VIC, respectively
46
First four bits of these registers define the IRQ priority level for each interrupt slot. Lower number indicates higher priority
To configure the VIC so that UART0 is interrupt enabled as IRQ interrupt with priority level of 4
VICIntEnable EQU VICIntSelect EQU VICVectPriority6 EQU 0xFFFFF010 0xFFFFF00C 0xFFFFF218
LDR r4, =VICIntEnable LDR r5, [r4] ORR r5, r5, #0x20 STR r5, [r4] ; Enable UART0 interrupt LDR LDR BIC STR r4, =VICIntSelect r5, [r4] r5, r5,#0x20 r5, [r4] ; UART0 interrupt is FIQ
LDR r4, =VICVectPriority6 LDR r5, #0x4 STR r5, [r4] ; UART0 with priority level of 4
48
Adjust (0x8008)
D F
E D F
50
In addition to configuring the VIC to accept interrupt from a particular peripheral , the peripheral device should also be configured to raise interrupt along with control and pin settings.
The interrupt register of a peripheral device is used to configure the device for raising interrupt flag(s).
51
U0START IER
LDR r4, =U0START LDRB r5, [r4,#IER] BIC r5,r5,#0x3F ORR r5,r5,#0xC2 ; enable THRE interrupt STRB r5, [r4,#IER]
52
56