Topic05 Dealing with Interrupts on the Freescale MC9S12X
-
Upload
brett-wildermoth -
Category
Documents
-
view
34 -
download
0
description
Transcript of Topic05 Dealing with Interrupts on the Freescale MC9S12X
Interrupts: Not just one of Life’s little annoyances.
Topic Video 055
1Tuesday, 7 July 2009
What is an interrupt?
2Tuesday, 7 July 2009
What is an interrupt?
2Tuesday, 7 July 2009
What is an interrupt?Pete... Take
the rubbish out Please...
2Tuesday, 7 July 2009
What is an interrupt?Pete... Take
the rubbish out Please...I’ll do it
later...
2Tuesday, 7 July 2009
What is an interrupt?Pete... Take
the rubbish out Please...I’ll do it
later...
You will do it now or no computer for a
week...
2Tuesday, 7 July 2009
Why?
• All tasks are not created equal.
• Not all tasks are predictable.
3Tuesday, 7 July 2009
What is a Hardware Interrupt...
X
Y
CCR
PC
StackCPUS12
D
SP
Running
X
Y
CCR
PC
D
SP
4Tuesday, 7 July 2009
What is a Hardware Interrupt...
X
Y
CCR
PC
StackCPUS12
D
SP
Running
X
Y
CCR
PC
D
SP
4Tuesday, 7 July 2009
What is a Hardware Interrupt...
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
Y
CCR
PC
D
SP
4Tuesday, 7 July 2009
What is a Hardware Interrupt...
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
Y
CCR
PC
D
SP
4Tuesday, 7 July 2009
What is a Hardware Interrupt...
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
YCCR
PC
D
SP
4Tuesday, 7 July 2009
What is a Hardware Interrupt...
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
YCCR
PC
D
SP
4Tuesday, 7 July 2009
What is a Hardware Interrupt...
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
YCCR
PC
D
SP
4Tuesday, 7 July 2009
What is a Hardware Interrupt...
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
Y
CCR
PC
D
SP
4Tuesday, 7 July 2009
What is a Hardware Interrupt...
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
Y
CCR
PC
D
SP
Interrupts Disabled
4Tuesday, 7 July 2009
What is a Hardware Interrupt...
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
Y
CCR
PC
D
SP
Interrupts Disabled
5Tuesday, 7 July 2009
What is a Hardware Interrupt...
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
Y
CCR
PC
D
SP
Interrupts Disabled
5Tuesday, 7 July 2009
What is a Hardware Interrupt...
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
Y
CCR
PC
D
SP
PC
Interrupts Disabled
5Tuesday, 7 July 2009
What is a Hardware Interrupt...
X
Y
CCR
PC
StackCPUS12
D
SP
Running
X
Y
CCR
PC
D
SP
PC
CCR
X
Y
Interrupts Disabled
D
SP
5Tuesday, 7 July 2009
What is a Hardware Interrupt...
X
Y
CCR
PC
StackCPUS12
D
SP
Running
X
Y
CCR
PC
D
SP
PC
CCR
X
Y
Interrupts Disabled
D
5Tuesday, 7 July 2009
What is a Hardware Interrupt...
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
Y
CCR
PC
D
SP
PC
CCR
X
Y
Interrupts Disabled
D
5Tuesday, 7 July 2009
What is a Hardware Interrupt...
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
Y
CCR
PC
D
SP
Interrupts Disabled
6Tuesday, 7 July 2009
What is a Hardware Interrupt...
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
Y
CCR
PC
D
SP
Interrupts Disabled
6Tuesday, 7 July 2009
What is a Hardware Interrupt...
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
YCCR
PC
D
SP
6Tuesday, 7 July 2009
What is a Hardware Interrupt...
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
YCCR
PC
D
SP
6Tuesday, 7 July 2009
What is a Hardware Interrupt...
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
YCCR
PC
D
SP
6Tuesday, 7 July 2009
What is a Hardware Interrupt...
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
Y
CCR
PC
D
SP
6Tuesday, 7 July 2009
What is a Hardware Interrupt...
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
Y
CCR
PC
D
SP
6Tuesday, 7 July 2009
What is a Hardware Interrupt...
X
Y
CCR
PC
StackCPUS12
D
SP
Running
X
Y
CCR
PC
D
SP
6Tuesday, 7 July 2009
SCPU12 Interrupts
• The MC9S12XDP512 contains vectored interrupts with hardware priority resolution that can be customized with software.
• The MC9S12XDP512 contains two external dedicated interrupt lines IRQ and XIRQ located on port E.
• The IRQ • The XIRQ
• The MC9S12XDP512 contains numerous internal interrupt sources that are associated with the various internal subsystems.
7Tuesday, 7 July 2009
Questions that need to be answered...
• Do we need an interrupt?
• Who will trigger the interrupt and under what conditions?
• Internal or External source?
• What must be done when it occurs?
• Run some code?
8Tuesday, 7 July 2009
Interrupt Vector Table• What is an Interrupt Vector Table?
• A table of pointers in memory that point to subroutines that need to be executed once the interrupt it triggered.
• Each interrupt source has its own special location in this table.
Vector Address Interrupt Source CCR Mask Local Enable
$FFFE System reset None None
$FFFC Clock Monitor Reset None PLLCTL
$FFFA COP Watchdog Reset None COP Rate select
Vector Base + $F8 Unimplemented OpCode None None
Vector Base + $F6 SWI None None
Vector Base + $F4 XIRQ X-bit None
Vector Base + $F2 IRQ I-bit IRQCR
9Tuesday, 7 July 2009
Setting the Vector Base
• Apart from the vectors for the three reset sources (Reset, Clock Reset and Watchdog Reset) the vector table can be located anywhere inside system memory.
• The vector base address must be set using the IVBR register.
VB7 VB6 VB5 VB4 VB3 VB2 VB1 VB0
IVBR ($0121)
Holds the MSB of the vector base address.
10Tuesday, 7 July 2009
What else?
• The vector table makes the connection between a interrupt source and the corresponding code that must be executed.
• Code that is executed as a result of an interrupt is referred to as an interrupt service routine (ISR).
• By putting the address of the ISR into a particular position of the vector table, will ensure that the ISR will be executed if that interrupt source is triggered.
• So as long as the interrupt source is enabled, the ISR will run.
11Tuesday, 7 July 2009
Controlling Interrupts
• The X bit in the CCR is used to control the XIRQ interrupt source.
• The I bit in the CCR is used to control the remaining interrupt sources.
• The interrupts will only work if they have been properly enabled.
12Tuesday, 7 July 2009
Controlling InterruptsThe Interrupt Process
• Using the two bits in the CCR namely the X and I bits, it is possible to control the entire interrupt subsystem.
• Each maskable interrupt can be individually disabled or enabled by setting the appropriate bit in the device’s control registers.
• During an interrupt, the X bit remains unchanged, this means a non maskable interrupt can occur within an interrupt service routine.
• The use of nested interrupts should be avoided, however it is possible to have nested interrupts if the I bit is cleared during an interrupt service routine (recommended only for experts).
• Interrupts are disabled on reset.
13Tuesday, 7 July 2009
Things you need to do...Initialising Interrupt Vectors
• Before using interrupts, the interrupt vectors must be initialised to point to the appropriate interrupt service routines.
• If a subsystem uses the interrupt facility then the interrupt vector must be set and a interrupt service routine provided.
• The Hardware Reset interrupt vector must be set to point to the beginning of the code.
• When the processor is originally powered up or reset, the routine pointed to by the hardware interrupt service will be executed.
14Tuesday, 7 July 2009
Reset Vector ExampleEntry:
LDS #RAMEnd+1 ; initialize the stack pointer
CLI ; enable interrupts
mainLoop:
BRA *
;**************************************************************
;* Interrupt Vectors *
;**************************************************************
ORG $FFFE
DC.W Entry ; Reset Vector
15Tuesday, 7 July 2009
Interrupt Sources
• Inside the MC9S12XDP512 there are approximately 128 possible subsystem interrupt sources that can be enabled or disabled by bits locally contained within the control registers of the particular I/O subsystem.
• Each of these sources has its own interrupt vector associated with it and therefore has space allocated inside the vector table.
• When an interrupt request is generated the value located in the interrupt vector for the particular source is fetched and loaded into the program counter.
16Tuesday, 7 July 2009
Interrupt SourcesExternal Interrupt Sources (IRQ)
• An interrupt request on the IRQ pin is generated by external logic.• The IRQ pin is an active low signal and you may set it to a level active
or a negative edge response.• The Interrupt Control Register located at $001E contains bits to
select which response is needed and to enable or disable the IRQ pin.
00000DLYIRQENIRQE
Interrupt Control Register (IRQCR): ($001E)
IRQE: IRQ Select Edge Sensitivity (0=level triggered, 1=falling edge triggered).IRQEN: Enable IRQ pin (1=enabled, 0=GPIO)
17Tuesday, 7 July 2009
Other Interrupt SourcesExceptions
• In a system, there are always events that are so important that they should never be ignored.
• These events are usually referred to as exceptions.
RESETThis the hardware reset, this usually take place when the system is reset or firstly powered up. This interrupt has the highest priority of all the interrupts.
18Tuesday, 7 July 2009
CLOCK MONITOR FAILURE
If the CPU clock signals slow down or fails and the Clock monitor is enabled. The clock monitor will detect a problem and issue a CME reset signal. However it is impossible for the CPU to execute the interrupt until the clock signal reappears.
Other Interrupt SourcesExceptions
19Tuesday, 7 July 2009
COMPUTER OPERATING NORMALLYAlso referred to as the watchdog timer. If your program crashes or hangs then this exception is triggered.
Other Interrupt SourcesExceptions
20Tuesday, 7 July 2009
UNIMPLEMENTED OPCODE TRAPIf for some reason the program gets lost and begins executing data, it is highly likely that it will try to execute an unimplemented op-code. If this does occur the CPU will trigger the an interrupt and the unimplemented opcode trap vector will be fetched and executed.
Other Interrupt SourcesExceptions
21Tuesday, 7 July 2009
SOFTWARE INTERRUPTThe software interrupt is effectively a branch to the subroutine whose address is located in the software interrupt vector. It is a software triggered interrupt.
Other Interrupt Sources
22Tuesday, 7 July 2009
NON-MASKABLE INTERRUPT REQUESTXIRQ is an external non-maskable interrupt input. It is controlled using only the X bit in the CCR. Once the X bit has been cleared it is impossible to set this bit again once the program has commenced.
Other Interrupt Sources
23Tuesday, 7 July 2009
InterruptsInterrupt Flags / Internal Interrupt Sources
• There exists many internal interrupt sources form the various built-in subsystems.
• In order to use the interrupt capability of these subsystems, global interrupts must be enabled and the enable bit set in the appropriate control register for the I/O device.
• Some subsystems set a flag bit in a status register when an interrupt occurs and if this flag isn’t reset then an interrupt will trigger again once ISR is complete.
24Tuesday, 7 July 2009
Interrupt Subsystem
25Tuesday, 7 July 2009
Waiting for an Interrupt...
What to do while waiting for an interrupt:
Spin LoopsThe simplest way to make the CPU wait is to implement a spin loop. A spin loop consists of a branch instruction back to itself. For example:
LOOP: BRA LOOP
When an interrupt occurs the loop will be interrupted and will continue the loop when the ISR completes.
26Tuesday, 7 July 2009
WAI: Wait for interruptThe WAI performs two functions:
• Firstly, it pushes the CPU contents onto the stack in preparation for the upcoming interrupt, this reduces the delay involved in executing the ISR.
• Secondly, it places the CPU into a wait state, reducing the overall power consumption. When the interrupt is triggered the ISR is executed and upon returning from the interrupt, the interrupted program will continue from the next instruction following the WAI instruction.
Waiting for an Interrupt...
27Tuesday, 7 July 2009
Stop: Stop clocks instructionThe final method involves using the STOP instruction, this will stop the clocks, and dramatically reduce the power consumption of the CPU. Only the interrupts IRQ, XIRQ and RESET can remove the CPU from this state. You should also ensure that the watchdog timer is disabled otherwise this instruction will trigger a watchdog interrupt.
Waiting for an Interrupt...
28Tuesday, 7 July 2009
What’s so special about an ISR?
• An interrupt service routine (ISR) is simply a subroutine terminated by an RTI instead of RTS.
• The interrupt service routine is only executed once the interrupt vector has been initialised properly, interrupts have been enabled and unmasked, an interrupt has occurred, the CPU registers have been pushed onto the stack, and the vector has been fetched.
29Tuesday, 7 July 2009
What’s so special about an ISR?
• Great care must be taken when using the stack inside an ISR.• All PSH commands must have corresponding PUL commands of
equal size.• All JSR must have corresponding RTS at the end of the
subroutine.• Failure to restore the stack to its original state will result in the
register set being reloaded incorrectly at the end of the ISR.• Hints to writing successful Interrupt Services Routines are:
• Re-enable Interrupts in the ISR only if you need them, if there are higher level interrupts that need servicing then you can re-enable the interrupts by clearing the I bit.
• Do not use nested interrupts, unless you really have to.• Reset any interrupt generating flags in the I/O devices prior to exiting the
ISR. If you do not reset the flag the device will immediately generate another interrupt upon exiting the ISR.
30Tuesday, 7 July 2009
• Do not worry about using registers in the ISR.• Do not assume any register contents.• Keep ISRs simple to begin with. • Keep ISR short.
What’s so special about an ISR?
31Tuesday, 7 July 2009
Another exampleVirgin System
TCO: EQU $FFEE ; Address of vector for Timer channel 0 ORG ROMSTART
Entry: LDS #RAMEnd+1 ; initialize the stack pointer
MOVB #$FF,IVBR
CLI ; enable interrupts
MainLoop: BRA *TCOISR: NOP RTI ORG TCO ;Location of TC0 interrupt vector DC.W TCOISR ; The label of the address is the TCO ISR.
32Tuesday, 7 July 2009
Another exampleuBug12 System
TCO: EQU $FFEE ; Address of vector for Timer channel 0 ORG ROMSTART
Entry: LDS #RAMEnd+1 ; initialize the stack pointer
MOVB #$F7,IVBR
CLI ; enable interrupts
MainLoop: BRA *TCOISR: NOP RTI ORG TCO ;Location of TC0 interrupt vector DC.W TCOISR ; The label of the address is the TCO ISR.
33Tuesday, 7 July 2009
ReCap: The interrupt process...
X
Y
CCR
PC
StackCPUS12
D
SP
Running
X
Y
CCR
PC
D
SP
34Tuesday, 7 July 2009
ReCap: The interrupt process...
X
Y
CCR
PC
StackCPUS12
D
SP
Running
X
Y
CCR
PC
D
SP
34Tuesday, 7 July 2009
ReCap: The interrupt process...
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
Y
CCR
PC
D
SP
34Tuesday, 7 July 2009
ReCap: The interrupt process...
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
Y
CCR
PC
D
SP
34Tuesday, 7 July 2009
ReCap: The interrupt process...
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
YCCR
PC
D
SP
34Tuesday, 7 July 2009
ReCap: The interrupt process...
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
YCCR
PC
D
SP
34Tuesday, 7 July 2009
ReCap: The interrupt process...
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
YCCR
PC
D
SP
34Tuesday, 7 July 2009
ReCap: The interrupt process...
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
Y
CCR
PC
D
SP
34Tuesday, 7 July 2009
ReCap: The interrupt process...
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
Y
CCR
PC
D
SP
Interrupts Disabled
34Tuesday, 7 July 2009
...IRQ...
Reset
X
Y
CCR
PC
Interrupt Vector TableCPUS12
D
SPSP
Interrupts DisabledIRQ
ReCap: The interrupt process...
35Tuesday, 7 July 2009
...IRQ...
Reset
X
Y
CCR
PC
Interrupt Vector TableCPUS12
D
SPSP
Interrupts Disabled
PC
ReCap: The interrupt process...
35Tuesday, 7 July 2009
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
Y
CCR
PC
D
SP
PC
Interrupts Disabled
ReCap: The interrupt process...
36Tuesday, 7 July 2009
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
Y
CCR
PC
D
SP
PC
Interrupts Disabled
ReCap: The interrupt process...
36Tuesday, 7 July 2009
X
Y
CCR
PC
StackCPUS12
D
SP
Running
X
Y
CCR
PC
D
SP
PC
CCR
X
Y
Interrupts Disabled
D
SP
ReCap: The interrupt process...
36Tuesday, 7 July 2009
X
Y
CCR
PC
StackCPUS12
D
SP
Running
X
Y
CCR
PC
D
SP
PC
CCR
X
Y
Interrupts Disabled
D
ReCap: The interrupt process...
36Tuesday, 7 July 2009
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
Y
CCR
PC
D
SP
PC
CCR
X
Y
Interrupts Disabled
D
ReCap: The interrupt process...
36Tuesday, 7 July 2009
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
Y
CCR
PC
D
SP
Interrupts Disabled
ReCap: The interrupt process...
37Tuesday, 7 July 2009
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
Y
CCR
PC
D
SP
Interrupts Disabled
ReCap: The interrupt process...
37Tuesday, 7 July 2009
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
YCCR
PC
D
SP
ReCap: The interrupt process...
37Tuesday, 7 July 2009
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
YCCR
PC
D
SP
ReCap: The interrupt process...
37Tuesday, 7 July 2009
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
YCCR
PC
D
SP
ReCap: The interrupt process...
37Tuesday, 7 July 2009
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
Y
CCR
PC
D
SP
ReCap: The interrupt process...
37Tuesday, 7 July 2009
X
Y
CCR
PC
StackCPUS12
D
SP
Halted
X
Y
CCR
PC
D
SP
ReCap: The interrupt process...
37Tuesday, 7 July 2009
X
Y
CCR
PC
StackCPUS12
D
SP
Running
X
Y
CCR
PC
D
SP
ReCap: The interrupt process...
37Tuesday, 7 July 2009
Need Further Assistance?
• Ask your Demonstrator,
• Post a question on the Forum,
• Email the Convener, or
• Make an appointment.
38Tuesday, 7 July 2009