@vtucode - in BCS402 Module 4 PDF 2022 Scheme
@vtucode - in BCS402 Module 4 PDF 2022 Scheme
MODULE-4
1
MICROCONTROLLER(BCS402)
Whenever an exception occurs, the core enter a specific mode. The ARM
processor modes can be entered manually by changing the cpsr.
When an exception occurs the ARM processor always switches to ARM
state. Figure 4.1 shows an exceptions and associated modes.
2
MICROCONTROLLER(BCS402)
3
MICROCONTROLLER(BCS402)
4
MICROCONTROLLER(BCS402)
5
MICROCONTROLLER(BCS402)
Example(1)
This show the method of returning from an IRQ and FIQ handler is to use
a SUBS instruction:
Since there is an S at the end of the SUB instruction and the pc is the
destination register, the cpsr is automatically restored from the spsr
register.
Example (2)
This example shows another method that subtracts the offset from the
link register r14 at the beginning of the handler.
Example (3)
The example uses the interrupt stack to store the link register. This
method first subtracts an offset from the link register and then stores it
onto the interrupt stack.
To return to normal execution, the LDM instruction is used to load the pc.
The ˆ symbol in the instruction forces the cpsr to be restored from the spsr.
4.2 Interrupts
There are two types of interrupts available on the ARM processor. The
first type of interrupt causes an exception raised by an external
peripheral—namely, IRQ and FIQ.
6
MICROCONTROLLER(BCS402)
Prioritization
We can program the interrupt controller to ignore interrupts of the same
or lower priority than the interrupt we are handling presently, so only
a higher-priority task can interrupt our handler. We then re-enable the
interrupts. The processor spends time in the lower-priority interrupts until
a higher-priority interupt occurs. Therefore higher-priority interrupts have
a lower average interrupt latency than the lower-priority interrupts.
It reduces latency by speeding up the completion time on the critical
time-sensitive interrupts.
IRQ and FIQ exceptions only occur when a specific interrupt mask is
cleared in the cpsr.
The ARM processor will continue executing the current instruction in the
execution stage of the pipeline before handling the interrupt.
An IRQ or FIQ exception causes the processor hardware to go through a
standard procedure listed below,
1) The processor changes to a specific interrupt request mode, which
being raised.
2) The previous mode’s cpsr is saved into the spsr of the new interrupt
request mode.
3) The pc is saved in the lr of the new interrupt request mode.
4) Interrupt/s are disabled—either the IRQ or both IRQ and FIQ
exceptions are disabled in the cpsr. This immediately stops another
interrupt request of the same type being raised.
8
MICROCONTROLLER(BCS402)
Example 4.5: what happens when an IRQ exception is raised when the
processor is in user mode?
Example 4.6 what happens when an FIQ exception is raised when the
processor is in user mode?
Figure 4.5 shows an example of an FIQ exception.
9
MICROCONTROLLER(BCS402)
i) The processor starts in state 1.In this mode both the IRQ and FIQ exception
bits in the cpsr are enabled.
ii) When an FIQ occurs the processor moves into state 2.->
a) This transition automatically sets the IRQ bit and FIQ to one, disabling
both IRQ and FIQ exceptions,
b) The cpsr processor mode changes to FIQ mode,
c) The user mode cpsr is automatically copied into spsr_fiq,
d) Register r14_fiq is assigned the value of the pc when the interrupt was
raised,
e) The pc is then set to the FIQ entry +0x1c in the vector table.
iii ) In state 3 the software handler takes over and calls the appropriate interrupt
service routine to service the source of the interrupt. After completion, the
processor mode reverts back to the original user mode code in state 1.
iv) When processor changes from user mode to FIQ mode, there is no
requirement to save registers r8 to r12 since these registers are banked in FIQ
mode. These registers can be used to hold temporary data, such as buffer
pointers or counters. This makes FIQ ideal for servicing a single-source,high-
priority, low-latency interrupt.
The ARM processor core has a simple procedure to manually enable and
disable interrupts by modifying the cpsr when the processor is in a
privileged mode.
The procedure uses three ARM instructions.
1)The instruction MRS copies the contents of the cpsr into register r1.
2)The instruction BIC clears the IRQ or FIQ mask bit.
3) The instruction MSR then copies the updated contents in register r1
back into the cpsr, to enable the interrupt request.
Table 4.5 shows how IRQ and FIQ interrupts are enabled.
The postfix _c identifies that the bit field being updated is the control
field bit [7:0] of the cpsr.
10
MICROCONTROLLER(BCS402)
The IRQ mode stack has to be set up during the initialization code for the
system.The stack size is reserved in the initial stages of boot-up .
Figure 4.6 shows two memory layouts in a linear address space.
For each processor mode a stack has to be set up. This is carried out every
time the processor is reset. Figure 9.7 shows an implementation of stack
using layout A.
12
MICROCONTROLLER(BCS402)
There is an advantage using separate stacks for each mode rather than
using a single stack . Errant tasks can be debugged and isolated from the
rest of the system.
Each mode stack must be set up. Here is an example to set up three
different stacks when the processor core comes out of reset.
Initialization code starts by setting up the stack registers for each
processor mode. The stack register r13 is one of the registers that is
always banked when a mode change occurs.
A set of defines are declared that map the memory region names with an
absolute address.
Example, the User stack is given the label USR_Stack and is set to
address 0x20000. The Supervisor stack is set to an address that is 128
bytes below the IRQ stack.
USR_Stack EQU 0x20000
IRQ_Stack EQU 0x8000
SVC_Stack EQU IRQ_Stack-128
A set of defines that map each processor mode with a particular mode bit
pattern. These labels can then be used to set the cpsr to a new mode.
13
MICROCONTROLLER(BCS402)
To set up the IRQ stack, the processor mode has to change to IRQ
mode. This is achieved by storing a cpsr bit pattern into register r2.
Register r2 is then copied into the cpsr, placing the processor into IRQ
mode.
14