1. 探索嵌入式 ARM 平台與 SoC
Part II – 定址與組合語言瀏覽 . 硬體啟動程序 .
中斷與例外處理
Jim Huang (jserv)
from 0xlab
June 20, 2010
1
0xlab – connect your device to application – http://0xlab.org/
3. Part II 涵蓋範圍 3
0xlab – connect your device to application – http://0xlab.org/
4. Agenda
PXA255 SoC 與 CuRT 的硬體啟動程序
ARM Interrupt, ISR, Exception 的處理
ARM 定址與組合語言概況
4
0xlab – connect your device to application – http://0xlab.org/
5. PXA255 SoC 與 CuRT 的硬體啟動程序
ARM Interrupt, ISR, Exception 的處理
ARM 定址與組合語言概況
5
0xlab – connect your device to application – http://0xlab.org/
6. PXA255 Function Block
Color or Grayscale
LCD Controller
RTC
Memory
OS Timer Controller
PWM(2) 0x4400_0000
PCMCIA
& CF XCVR
Interrupt Control
Controller
Socket 0, 1
Clock & System Bus
Power Man.
Dynamic SDRAM/
I2S Memory SMROM
Control 4 banks
I2C
Megacell
AC97 Write Read
Core Buffer Buffer
FF_UART
BT_UART Variable
Load/Store Data
s u B l ar e h p r e P
Latency
Slow lrDA I/O ASIC
Dcac he Addr
O / I es o pr u P l ar e ne G
Control
i
Fast lrDA DMMU
(32 Kbytes)
e g d r B d na r el l ort n o C A MD
SSP Minicache XScale CS #3,4,5
Core
NSSP Icache PC
IMMU ROM/
(32 Kbytes) Static
USB Memory Flash
Client Instructions Control SRAM
4 banks
MMC 3.6864 MHz 32.768 KHz
Osc Osc CS #0,1,2
i
6
0xlab – connect your device to application – http://0xlab.org/
7. PXA255 系統架構
IRQ FIQ
CP 14
Performance
Monitoring Branch Target Buffer
Interrupt
Request Trace
Buffer
CP 15
Config
Registers Core Memory Bus
Instruction Instruction
Cache
32KBytes MMU
Execution
Coprocessor Core
Mini I-Cache
Interface 2 KBytes
Data
Address
CP0 Data Cache
Multiplier / Data 32 KBytes
MMU Write
Accumulator Buffer
Mini D-Cache
2 KBytes
JTAG Debug System
Management
7
0xlab – connect your device to application – http://0xlab.org/
10. PXA255 的執行模式
Power on, nRESET asserted
Hardware Reset
nRESET asserted
nRESET
nRESET asserted negated nRESET
asserted
RUN
Wait for interrupt
instruction Force sleep bit set, or VDD
or battery fault pins asserted
System or GPIO or RTC
peripheral unit alarm interrupt
interrupt
IDLE
VDD or battery fault pins asserted SLEEP
CPU clock held low, all other
resources active, wait for interrupt Wait for wake-up event
10
0xlab – connect your device to application – http://0xlab.org/
17. GPIO (General Purpose I/O)
Modem control signals for UART (CTS, RTS, CD, etc) implemented via
GPIO signals
GPIO[58:73] = dual panel color or 16 bit parallel input on LCD
GPIO[23:27] = SPI if both synchronous serial protocols
are required in a single system
Memory
Controller
RTC
O S T ime r
PWM(2) 0x4400_0000 PCMCIA
Inter rupt
& CF XCVR
Contro ller Control
Clo ck & Socket 0, 1
Power Ma n. System Bus
Dynamic SDRAM/
I2S Memory SMROM
I2C Control 4 banks
AC97 Megacell Write Read
FF_U AR T Core B uffe r Buffer
BT_ U ART Variable
Load/Store Data Latency
S low lrD A I/O ASIC
s u B l ar e h p r e P
Dcache Addr
Fast lrD A DMMU (32 Kbytes)
Control
XScale
/ I es o pr u P l ar e n e G
Minicache
i
S SP CS
Core #3,4,5
na r el l ort n o C A MD
NSS P Icache PC
IMMU (32 Kbytes) Static ROM/
U SB Memory Flash
Client Instructions Control SRAM
4 banks
MMC 3.6864 MHz 32.768 KHz
Osc Osc CS #0,1,2
17
0xlab – connect your device to application – http://0xlab.org/
18. GPIO
GPIO Pin Direction Register (GPDR)
GPIO Alternate Function Register (GAFR)
GPIO Pin Set Register (GPSR)
GPIO Pin Clear Register (GPCR)
GPIO Falling Edge Detect Enable Register (GFER)
GPIO Rising Edge Detect Enable Register (GRER)
GPIO Edge Detect Status Register (GEDR)
GPIO Pin Level Register (GPLR)
18
0xlab – connect your device to application – http://0xlab.org/
19. GPIO Block Diagram
Pin Direction
0x40E0_000C/10/14
Register(GPDR)
2
Alternate Function 0x40E0_0054/58/5C
Register(GAFR)
GPDR 0x40E0_0060/64/68
Pin Set
0 Registers(GPSR) 0x40E0_0060/64/68
1 Pin Clear
2 Registers(GPCR) 0x40E0_0060/64/68
Alternate Function
3
(Output)
3
Alternate Function Base Address 0x40E0_0000
2 (Input)
1
0
Edge Detect
Edge Status Register(GEDR) 0x40E0_0048/4C/50
Detect
Rising Edge Detect
Enable Register(GRER) 0x40E0_0030/34/38
Power Manager
Falling Edge Detect
Sleep Wake-up Enable Register(GFER) 0x40E0_003C/40/44
logic
Pin-Level
Register(GPLR) 0x40E0_0000/04/08
19
0xlab – connect your device to application – http://0xlab.org/
20. CuRT_v1/arch/arm/mach-pxa/start.S
CuRT_v1/arch/arm/mach-pxa/start.S
init_gpio:
init_gpio:
// FFUART
// FFUART
ldr r12, =FFUART_BASE
ldr r12, =FFUART_BASE
ldr
ldr r0,
r0, =0x00000000
=0x00000000
str
str r0,
r0, [r12, #FFLCR]
[r12, #FFLCR]
…
…
//
// First set the output values to a safe/disabled state
First set the output values to a safe/disabled state
//
// before we change any GPIO's outputs start by settings
before we change any GPIO's outputs start by settings
//
// all of them high which is the safest for most signals
all of them high which is the safest for most signals
ldr r12, =GPIO_BASE
ldr r12, =GPIO_BASE
ldr
ldr r0,
r0, =0xffffffff
=0xffffffff
str
str r0,
r0, [r12, #GPIO_GPSR0]
[r12, #GPIO_GPSR0]
str
str r0,
r0, [r12, #GPIO_GPSR1]
[r12, #GPIO_GPSR1]
str
str r0,
r0, [r12, #GPIO_GPSR2]
[r12, #GPIO_GPSR2]
CuRT_v1/includes/arch/arm/mach-pxa/pxa255.h
CuRT_v1/includes/arch/arm/mach-pxa/pxa255.h
/** General Programmable I/O */
/** General Programmable I/O */
#define GPIO_BASE0x40E00000
#define GPIO_BASE0x40E00000
#define GPIO_REG(_x_) *(volatile unsigned long *)(GPIO_BASE + _x_)
#define GPIO_REG(_x_) *(volatile unsigned long *)(GPIO_BASE + _x_)
#define GPIO_GPLR0
#define GPIO_GPLR0 0x00 /* GPIO<31: 0>
0x00 /* GPIO<31: 0> status register */
status register */ 20
#define GPIO_GPLR1
#define GPIO_GPLR1 0x04 /* GPIO<63:32>
0x04 /* GPIO<63:32> status register */
status register */
0xlab – connect your device to application – http://0xlab.org/
#define GPIO_GPLR2
#define GPIO_GPLR2 0x08 /* GPIO<80:64>
0x08 /* GPIO<80:64> status register */
status register */
21. Register Register
GPIO[15:0] GPIO[31:16] GPIO[47:32] GPIO[63:48] GPIO[79:64] GPIO[80]
Type Function
GPLR Monitor Pin State GPLR0 GPLR1 GPLR2
GPSR GPSR0 GPSR1 GPSR2
Control Output
Pin State
GPCR GPCR0 GPCR1 GPCR2
GPDR Set Pin Direction GPDR0 GPDR1 GPDR2
GRER GRER0 GRER1 GRER2
Detect Rising/
Falling Edge
GFER GFER0 GFER1 GFER2
GEDR Detect Edge Type GEDR0 GEDR1 GEDR2
Set Alternate
GAFR GAFR0_L GAFR0_U GAFR1_L GAFR1_U GAFR2_L GAFR2_U
Functions
21
0xlab – connect your device to application – http://0xlab.org/
22. PXA255 SoC 與 CuRT 的硬體啟動程序
ARM Interrupt, ISR, Exception 的處理
ARM 定址與組合語言概況
22
0xlab – connect your device to application – http://0xlab.org/
23. 如果沒有 interrupt ,該會如何?
Main_Loop UART Tx UART Rx ADC LCD
UART_SendData(ch)
GetAD-Status
ADC Status
LCD_print("Hello");
LCD_print done
UART_GetFlagStatus()
Nachricht1
The main() function executes all peripheral calls in a fixed sequence
23
0xlab – connect your device to application – http://0xlab.org/
24. interrupt 的定義與特性
Interrupts are asynchronous events that may happen any time
Interrupts stop the execution of the current task
The processor jumps into the interrupt service routine (ISR)
The short ISR is executed
Control is given back to the previously executing task
Interrupts may have priorities.
Concurrent interrupts (interrupts that happen at the same time)
are serviced according to their priority
Interrupts may be enabled or disabled
Library functions that may be executed by an ISR must be
thread-safe (they have to adhere to some specific rules)
An ISR should if possible not trigger another interrupt
24
0xlab – connect your device to application – http://0xlab.org/
25. ARM Interrupt Controller
All interrupts routed to FIQ or IRQ
Two level interrupt structure
What module caused interrupt
Serial channel, DMA, Power Management, etc
Why did an interrupt occur there?
RX, TX, overrun, underrun, Data Done, Battery Fault, etc
Template for servicing interrupts provided with firmware
Peripheral/PCMCIA interrupt mask in each module
GPIO masks determined per pin or group of pins
25
0xlab – connect your device to application – http://0xlab.org/
26. ARM Interrupt
Vector table
Reserved area of 32 bytes at the end of the memory map
One word of space for each exception type
Contains a Branch or Load PC instruction for the exception
handler
Exception modes and registers
Handling exceptions changes program from user to nonuser
mode
Each exception handler has access to its own set of registers
26
0xlab – connect your device to application – http://0xlab.org/
27. CuRT_v1/arch/arm/mach-pxa/start.S
CuRT_v1/arch/arm/mach-pxa/start.S
/* exception handler vector table */
/* exception handler vector table */
_start:
_start:
b reset_handler
b reset_handler
b und_handler
b und_handler
b swi_handler
b swi_handler
b abt_pref_handler
b abt_pref_handler
b abt_data_handler
b abt_data_handler
b not_used
b not_used
b irq_handler
b irq_handler
b fiq_handler
b fiq_handler
27
0xlab – connect your device to application – http://0xlab.org/
28. CuRT_v1/arch/arm/mach-pxa/start.S
CuRT_v1/arch/arm/mach-pxa/start.S
/* exception handler vector table */
/* exception handler vector table */
_start:
_start:
b reset_handler
b reset_handler
b und_handler
b und_handler
b swi_handler
b swi_handler
b abt_pref_handler
b abt_pref_handler
b abt_data_handler
b abt_data_handler 如果 Exceptions 同時發生,
b not_used
b not_used
b irq_handler
b irq_handler
會如何?
b fiq_handler
b fiq_handler
28
0xlab – connect your device to application – http://0xlab.org/
30. IRQ 與 FIQ
• Program Status Register
31 30 29 28 27 … 8 7 6 5 4 3 2 1 0
N Z C V I F M4 M3 M2 M1 M0
– 若要抑制 interrupts ,將 "F” 或“ I” bit 設定為 1
• 一旦 interrupt 觸發,處理器將變更至 FIQ32_mode registers 或
IRQ32_mode registers
• Switch register banks
• Copies CPSR to SPSR_mode (saves mode, interrupt flags, etc.)
• Changes the CPSR mode bits (M[4:0])
• Disables interrupts
• Copies PC to R14_mode (to provide return address)
• Sets the PC to the vector address of the exception handler
30
0xlab – connect your device to application – http://0xlab.org/
31. CuRT_v1/arch/arm/mach-pxa/arm_port.S
CuRT_v1/arch/arm/mach-pxa/arm_port.S
irq_service_routine:
irq_service_routine:
msr CPSR_c, #(NO_INT | IRQ32_MODE)
msr CPSR_c, #(NO_INT | IRQ32_MODE)
stmfd sp!, {r1-r3}
stmfd sp!, {r1-r3} // push working registers onto IRQ stack
// push working registers onto IRQ stack
mov r1, sp
mov r1, sp // save IRQ stack pointer
// save IRQ stack pointer
add sp, sp, #12
add sp, sp, #12 // adjust IRQ stack pointer
// adjust IRQ stack pointer
sub r2, lr, #4
sub r2, lr, #4 // adjust pc for return
// adjust pc for return
mrs
mrs r3, SPSR
r3, SPSR // copy SPSR (interrupted thread's CPSR)
// copy SPSR (interrupted thread's CPSR)
msr
msr CPSR_c, #(NO_INT | SVC32_MODE)
CPSR_c, #(NO_INT | SVC32_MODE) // change to SVC mode
// change to SVC mode
// save thread's context onto thread's stack
// save thread's context onto thread's stack
stmfd
stmfd sp!,
sp!, {r2}
{r2} // push thread's return pc
// push thread's return pc
stmfd
stmfd sp!,
sp!, {lr}
{lr} // push thread's LR
// push thread's LR
stmfd
stmfd sp!,
sp!, {r4-r12}
{r4-r12} // push thread's r12-r4
// push thread's r12-r4
ldmfd r1!, {r4-r6}
ldmfd r1!, {r4-r6} // move thread's r1-r3 from IRQ stack to
// move thread's r1-r3 from IRQ stack to
// SVC stack
// SVC stack
stmfd sp!, {r4-r6}
stmfd sp!, {r4-r6}
stmfd sp!, {r0}
stmfd sp!, {r0} // push thread's r0 onto thread's stack
// push thread's r0 onto thread's stack
stmfd sp!, {r3}
stmfd sp!, {r3} // push thread's CPSR(IRQ's SPSR)
// push thread's CPSR(IRQ's SPSR)
bl enter_interrupt • 一旦 interrupt 觸發,處理器將變更至 FIQ32_mode registers 或 IRQ32_mode registers
bl enter_interrupt
... • Switch register banks
...
• Copies CPSR to SPSR_mode (saves mode, interrupt flags, etc.)
• Changes the CPSR mode bits (M[4:0])
• Disables interrupts
• Copies PC to R14_mode (to provide return address)
• Sets the PC to the vector address of the exception handler
31
0xlab – connect your device to application – http://0xlab.org/
32. Interrupt Handlers
• 當 interrupt 發生時,硬體會跳躍到 interrupt handler
time
user program user program
Task
IRQ Interrupt handler
IRQ
FIQ
• On interrupt, the processor will set the
corresponding interrupt bit in the CPSR to disable
Interrupt subsequent interrupts of the same type from
occurring.
• However, interrupts of a higher priority can still
occur.
32
0xlab – connect your device to application – http://0xlab.org/
33. Nested/Reentrant Interrupts
• 但是, interrupts 也可能在執行 interrupt handlers
時被觸發,此為 nested( 巢狀 ) interrupt time
user program user program
Task
IRQ Interrupt handler
IRQ
FIQ Interrupt handler
FIQ
• On interrupt, the processor will set the
Interrupt corresponding interrupt bit in the CPSR to disable
subsequent interrupts of the same type from
occurring.
Second • However, interrupts of a higher priority can still
Interrupt occur.
33
0xlab – connect your device to application – http://0xlab.org/
34. Interrupts 的時序
• 在 interrupt handler 實際運作前,必須保存目前程式
(context) 的 register ( 若觸及這些 register)
• 這也是何以 FIQ 需要額外 register 的緣故,為了降
低 CPU 保存 context 的成本開銷
time
user program user program
cpu context saved
Task
IRQ
“servicing” interrupt
FIQ cpu context restored
Interrupt latency
Interrupt
Interrupt response
34
0xlab – connect your device to application – http://0xlab.org/
35. Exception Handling
When an exception occurs, the ARM:
Copies CPSR into SPSR_<mode>
Sets appropriate CPSR bits
Change to ARM state
0x1C FIQ
Change to exception mode 0x18 IRQ
Disable interrupts (if appropriate) 0x14 (Reserved)
Stores the return address in LR_<mode> 0x10 Data Abort
0x0C Prefetch Abort
Sets PC to vector address 0x08 Software Interrupt
To return, exception handler needs to: 0x04 Undefined Instruction
0x00 Reset
Restore CPSR from SPSR_<mode>
Vector Table
Restore PC from LR_<mode>
Vector table can be at
0xFFFF0000 on ARM720T
This can only be done in ARM state. and on ARM9/10 family devices
35
0xlab – connect your device to application – http://0xlab.org/
36. 案例:從 user mode 切到 FIQ mode
Registers in use Registers in use
User Mode FIQ Mode
r0 r0
r1 r1
r2 r2
r3 r3
r4 r4
r5 r5
r6 r6
r7 r7
r8
r9
r8_fiq
r9_fiq
EXCEPTION r8
r9
r8_fiq
r9_fiq
r10 r10_fiq r10 r10_fiq
r11 r11_fiq r11 r11_fiq
r12 r12_fiq r12 r12_fiq
r13 (sp) r13_fiq r13 (sp) r13_fiq
r14 (lr) r14_fiq r14 (lr) r14_fiq
r15 (pc) r15 (pc)
Return address calculated from User mode
PC value and stored in FIQ mode LR
cpsr cpsr
spsr_fiq spsr_fiq
User mode CPSR copied to FIQ mode SPSR
36
0xlab – connect your device to application – http://0xlab.org/
38. PXA255 Function Block
RTC
Memory
OS Timer Controller
PWM(2) 0x4400_0000
Interrupt
PXA255 Interrupt Controller PCMCIA
& CF XCVR
Control
Controller
Socket 0, 1
Clock & System Bus
Power Man.
Dynamic SDRAM/
I2S Memory SMROM
Control 4 banks
I2C
Megacell
AC97 Write Read
Core Buffer Buffer
FF_UART
BT_UART Variable
Load/Store Data
s u B l ar e h p r e P
Latency
Slow lrDA I/O ASIC
Dcac he Addr
O / I es o pr u P l ar e ne G
Control
i
Fast lrDA DMMU
(32 Kbytes)
e g d r B d na r el l ort n o C A MD
SSP Minicache XScale CS #3,4,5
Core
NSSP Icache PC
IMMU ROM/
(32 Kbytes) Static
USB Memory Flash
Client Instructions Control SRAM
4 banks
MMC 3.6864 MHz 32.768 KHz
Osc Osc CS #0,1,2
Color or Grayscale
LCD Controller
i
38
0xlab – connect your device to application – http://0xlab.org/
43. PXA255 SoC 與 CuRT 的硬體啟動程序
ARM Interrupt, ISR, Exception 的處理
ARM 定址與組合語言概況
43
0xlab – connect your device to application – http://0xlab.org/
44. ARM 組合語言
指令語法
<opcode>{<cond>}{S} <Rd>, <Rn>, <shifteroperand>
44
0xlab – connect your device to application – http://0xlab.org/
45. ARM 組合語言強大的語法
類 C 程式碼
if (z==1) R1=R2+(R3*4)
可編譯為以下的 ARM 組合語言指令
EQADDS R1, R2, R3, LSL #2
→ 只要一道指令
45
0xlab – connect your device to application – http://0xlab.org/
46. ARM 的指令集概述
ARM 指令集採用 Load / Store 架構
也即指令集僅能處理暫存器中的資料,且處理結果都要再放回暫
存器中
堆疊定址 暫存器 - 記憶體定址 (register
Push A memory)
Push B Load R1, A
Add Add R3, R1, B
» Pop the top2 values of Store R3, C
the stack (A, B) and push
the result value into the
stack
Pop C
累加器定址 暫存器定址 (loadstore)
Load A Load R1, A
Add B
Load R2, B
» Add AC (A) with B and
store the result into AC Add R3, R1, R2
Store C Store R3, C
46
0xlab – connect your device to application – http://0xlab.org/
47. ARM 指令集概述
ARM 指令集分為
跳躍指令
資料處理指令
程式狀態暫存器 (PSR) 處理指令
載入 / 存回指令
協同處理器指令
例外事件產生指令
僅探討 CuRT 所用的指令 47
0xlab – connect your device to application – http://0xlab.org/
48. ARM 指令的定址模式
定址方式指 CPU 根據指令中所給予的「位址」訊
息來尋找出「實體位址」的方式
ARM 指令的定址模式
立即定址
暫存器定址
暫存器間接定址
基底定址
相對定址
多暫存器定址
堆疊定址
48
0xlab – connect your device to application – http://0xlab.org/
59. 程式狀態暫存器
ARM 包含一個目前程式狀態暫存器 (CPSR) 和 5 個儲存程
式狀態暫存器 (SPSR) 。 SPSR 用來進行例外事件處理,
其功能包括 :
保存 ALU 中目前操作資訊
控制允許和禁止中斷
設定處理器的執行模式
程式狀態暫存器 (CPSR) 的每位元意義如下:
條件碼旗標位元 保留
控制位元
31 30 29 28 27 26 25 24 8 7 6 5 4 3 2 1 0
N Z C V . . . ….. I F T M4 M3 M2 M1 M0
N: 負值 / 小於
Z: 0
I: 除能 模式位元
C: 進位 / 借位 / 擴展 F: 除能
V: 溢位 T: 狀態位元 59
0xlab – connect your device to application – http://0xlab.org/
60. 條件碼旗標欄位 (1/2)
條件碼旗標欄位 (Condition Code Flags)
N 、 Z 、 C 與 V
均為條件碼旗標位元。它們的內
容可被算術或邏輯運算的結果而有所改變,並且
可以決定某條指令是否被執行
在 ARM 狀態下,絕大多數的指令都是有條件執行
在 Thumb 狀態下,僅有跳躍指令都是有條件執行
60
0xlab – connect your device to application – http://0xlab.org/