在WinCE中,有3種方式可以使系統進入休眠:
1、用戶在開始菜單選擇掛起
2、用戶短按電源鍵,請求系統進入休眠(需要電源按鍵驅動的支持,比如設為長按關機,短按休眠)
3、用戶長時間無操作,系統狀態由POWER_STATE_ON轉為POWER_STATE_USERIDLE,POWER_STATE_IDLE狀態,最後進入POWER_STATE_SUSPEND即休眠狀態
休眠之前,對於支持電源管理的驅動,系統會調用此驅動的XXX_PowerDown接口關閉設備電源,之後系統會調用OEMPowerOff接口,進入休眠。
OEMPowerOFF位於SMDK2440A/Src/Common/Power/off.c文件中,不要被它的名字迷惑了,它不僅負責休眠,喚醒後恢復也在它的內部實現,它的執行流程如下:
1、調用BSPPowerOFF,關閉休眠時不必要的設備電源,如USB
2、保存當前GPIO和LCD控制器等信息在內存中
3、設置GPIO為低功耗狀態,關閉LCD等設備
4、調用OALCPUPowerOff,SDRAM進入自刷新模式,關閉CPU,等待中斷喚醒
5、恢復保存在內存中的GPIO和LCD控制器等信息
6、調用BSPPowerOn打開相關設備電源,然後返回
在這個過程中,最容易出問題的部分在OALCPUPowerOff部分,這要從2440的休眠模式說起。
2440需要提供兩路獨立電源,一路電源給CPU及其內部邏輯供電,另一路電源單獨給喚醒邏輯供電。當2440進入休眠時,CPU及其內部邏輯的這一路供電將會停止,僅保留喚醒邏輯的供電。喚醒邏輯支持EINT0-15和RTC中斷,如果休眠期間產生這些中斷,系統將恢復對CPU的供電並喚醒CPU。
由於CPU在休眠期間是掉電的,所以它喚醒後將會從地址0x00000000開始執行指令,也就是說,這時候CPU已經脫離了WinCE的執行空間,運行的是BootLoader!
BootLoader執行後,首先執行一些必要的初始化工作,如設置系統時鐘等,然後檢查GSTATUS2[1]位,如果被設置為1,表明系統是從睡眠中恢復的,BootLoader將跳過通常的啟動流程,恢復對SDRAM的供電,然後從SDRAM恢復睡眠前保存下來的數據,包括喚醒地址,然後轉到喚醒地址執行,返回WinCE的執行空間。
下面通過代碼來詳細這個過程(SMDK2440A/SRC/OAL/OALLIB/startup.s):
- ;; OALCPUPowerOff調用入口
- LEAF_ENTRY OALCPUPowerOff
-
- ; 1. Push SVC state onto our stack
- stmdb sp!, {r4-r12}
- stmdb sp!, {lr}
-
- ; 2. Save MMU & CPU Register to RAM
- ;; 注意:這裡SLEEPDATA_BASE_VIRTUAL使用的是虛擬地址,其映射的物理地址必須和BootLoader中設定的一致
- ldr r3, =SLEEPDATA_BASE_VIRTUAL ; base of Sleep mode storage
-
- ldr r2, =Awake_address ; store Virtual return address
- str r2, [r3], #4
-
- mrc p15, 0, r2, c1, c0, 0 ; load r2 with MMU Control
- ldr r0, =MMU_CTL_MASK ; mask off the undefined bits
- bic r2, r2, r0
- str r2, [r3], #4 ; store MMU Control data
-
- mrc p15, 0, r2, c2, c0, 0 ; load r2 with TTB address.
- ldr r0, =MMU_TTB_MASK ; mask off the undefined bits
- bic r2, r2, r0
- str r2, [r3], #4 ; store TTB address
-
- mrc p15, 0, r2, c3, c0, 0 ; load r2 with domain access control.
- str r2, [r3], #4 ; store domain access control
-
- str sp, [r3], #4 ; store SVC stack pointer
-
- mrs r2, spsr
- str r2, [r3], #4 ; store SVC status register
-
- mov r1, #Mode_FIQ:OR:I_Bit:OR:F_Bit ; Enter FIQ mode, no interrupts
- msr cpsr, r1
- mrs r2, spsr
- stmia r3!, {r2, r8-r12, sp, lr} ; store the FIQ mode registers
-
- mov r1, #Mode_ABT:OR:I_Bit:OR:F_Bit ; Enter ABT mode, no interrupts
- msr cpsr, r1
- mrs r0, spsr
- stmia r3!, {r0, sp, lr} ; store the ABT mode Registers
-
- mov r1, #Mode_IRQ:OR:I_Bit:OR:F_Bit ; Enter IRQ mode, no interrupts
- msr cpsr, r1
- mrs r0, spsr
- stmia r3!, {r0, sp, lr} ; store the IRQ Mode Registers
-
- mov r1, #Mode_UND:OR:I_Bit:OR:F_Bit ; Enter UND mode, no interrupts
- msr cpsr, r1
- mrs r0, spsr
- stmia r3!, {r0, sp, lr} ; store the UND mode Registers
-
- mov r1, #Mode_SYS:OR:I_Bit:OR:F_Bit ; Enter SYS mode, no interrupts
- msr cpsr, r1
- stmia r3!, {sp, lr} ; store the SYS mode Registers
-
- mov r1, #Mode_SVC:OR:I_Bit:OR:F_Bit ; Back to SVC mode, no interrupts
- msr cpsr, r1
-
- ; 3. do Checksum on the Sleepdata
- ldr r3, =SLEEPDATA_BASE_VIRTUAL ; get pointer to SLEEPDATA
- ldr r2, =0x0
- ldr r0, =(SLEEPDATA_SIZE-1) ; get size of data structure (in words)
- 30
- ldr r1, [r3], #4
- and r1, r1, #0x1
- mov r1, r1, ROR #31
- add r2, r2, r1
- subs r0, r0, #1
- bne %b30
-
- ;; 保存數據的校驗和放在GSTATUS3中,BootLoader中需要進行校驗,只有校驗正確才會返回WinCE,否則按冷啟動的流程走
- ldr r0, =vGPIOBASE
- str r2, [r0, #oGSTATUS3] ; Store in Power Manager Scratch pad register
-
-
- ; 4. Interrupt Disable
- ldr r0, =vINTBASE
- mvn r2, #0
- str r2, [r0, #oINTMSK]
- str r2, [r0, #oSRCPND]
- str r2, [r0, #oINTPND]
-
- ;; 5. Cache Flush
- bl OALClearUTLB
- bl OALFlushICache
- ldr r0, = (DCACHE_LINES_PER_SET - 1)
- ldr r1, = (DCACHE_NUM_SETS - 1)
- ldr r2, = DCACHE_SET_INDEX_BIT
- ldr r3, = DCACHE_LINE_SIZE
- bl OALFlushDCache
-
- ; 6. Setting Wakeup External Interrupt(EINT0,1) Mode
- ldr r0, =vGPIOBASE
-
- ldr r1, =0x550a
- str r1, [r0, #oGPFCON]
-
- ; 7. Go to Power-Off Mode
- ldr r0, =vMISCCR ; hit the TLB
- ldr r0, [r0]
- ldr r0, =vCLKCON
- ldr r0, [r0]
-
- ;; 內存進入自刷新模式,CPU掉電後保持內容
- ldr r0, =vREFRESH
- ldr r1, [r0] ; r1=rREFRESH
- orr r1, r1, #(1 << 22)
-
- ldr r2, =vMISCCR
- ldr r3, [r2]
- orr r3, r3, #(7<<17) ; Make sure that SCLK0:SCLK->0, SCLK1:SCLK->0, SCKE=L during boot-up
- bic r3, r3, #(7<<20)
- orr r3, r3, #(6<<20)
-
- ldr r4, =vCLKCON
- ldr r5, =0x1ffff8 ; Power Off Mode
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; Sometimes it is not working in cache mode. So I modify to jump to ROM area.
- ;
- ;;; ldr r6, =0x92000000 ; make address to 0x9200 0020
- ;;; add r6, r6, #0x20 ;
- ;;; mov pc, r6 ; jump to Power off code in ROM
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- b SelfRefreshAndPowerOff
-
- ALIGN 32 ; for I-Cache Line(32Byte, 8 Word)
-
- ;; 關閉CPU
- SelfRefreshAndPowerOff ; run with Instruction Cache's code
- str r1, [r0] ; Enable SDRAM self-refresh
- str r3, [r2] ; MISCCR Setting
- str r5, [r4] ; Power Off !!
- b .
下面是BootLoader中喚醒部分的代碼:
- ;Check if the boot is caused by the wake-up from SLEEP mode.
- ldr r1,=GSTATUS2
- ldr r0,[r1]
- tst r0,#0x2
-
- ;In case of the wake-up from SLEEP mode, go to SLEEP_WAKEUP handler.
- bne WAKEUP_SLEEP
-
-
-
- WAKEUP_SLEEP
- ;Release SCLKn after wake-up from the SLEEP mode.
- ldr r1,=MISCCR
- ldr r0,[r1]
- bic r0,r0,#(7<<17) ;SCLK0:0->SCLK, SCLK1:0->SCLK, SCKE:0->=SCKE.
- str r0,[r1]
-
- ;Set memory control registers
- ldr r0,=SMRDATA
- ldr r1,=BWSCON ;BWSCON Address
- add r2, r0, #52 ;End address of SMRDATA
- 0
- ldr r3, [r0], #4
- str r3, [r1], #4
- cmp r2, r0
- bne %B0
-
- mov r1,#256
- 0 subs r1,r1,#1 ;1) wait until the SelfRefresh is released.
- bne %B0
-
- ;------------------------------------------------------------------------------
- ; Recover Process : Starting Point
- ;
- ; 1. Checksum Calculation saved Data
- ;; 這裡的SLEEPDATA_BASE_PHYSICAL,必須上面的虛擬地址SLEEPDATA_BASE_VIRTUAL映射到相同的物理內存
- ldr r5, =SLEEPDATA_BASE_PHYSICAL ; pointer to physical address of reserved Sleep mode info data structure
- mov r3, r5 ; pointer for checksum calculation
- ldr r2, =0x0
- ldr r0, =(SLEEPDATA_SIZE-1) ; get size of data structure to do checksum on
- 50
- ldr r1, [r3], #4 ; pointer to SLEEPDATA
- and r1, r1, #0x1
- mov r1, r1, ROR #31
- add r2, r2, r1
- subs r0, r0, #1 ; dec the count
- bne %b50 ; loop till done
-
- ;; 檢查校驗和,如果不匹配,說明內存中保存的數據有問題,按冷啟動執行
- ldr r0,=GSTATUS3
- ldr r3, [r0] ; get the Sleep data checksum from the Power Manager Scratch pad register
- cmp r2, r3 ; compare to what we saved before going to sleep
- bne BringUpWinCE ; bad news - do a cold boot
-
- ; 2. MMU Enable
- ldr r10, [r5, #SleepState_MMUDOMAIN] ; load the MMU domain access info
- ldr r9, [r5, #SleepState_MMUTTB] ; load the MMU TTB info
- ldr r8, [r5, #SleepState_MMUCTL] ; load the MMU control info
- ldr r7, [r5, #SleepState_WakeAddr ] ; load the LR address
- nop
- nop
- nop
- nop
- nop
-
- ; if software reset
- mov r1, #0
- teq r1, r7
- bne %f60
- b BringUpWinCE
-
- ; wakeup routine
- 60 mcr p15, 0, r10, c3, c0, 0 ; setup access to domain 0
- mcr p15, 0, r9, c2, c0, 0 ; PT address
- mcr p15, 0, r0, c8, c7, 0 ; flush I+D TLBs
- mcr p15, 0, r8, c1, c0, 0 ; restore MMU control
-
- ; 3. Jump to Kernel Image's fw.s (Awake_address)
- mov pc, r7 ; jump to new VA (back up Power management stack)
- nop
-
- BringUpWinCE
- ; bad news, data lose, bring up wince again
- mov r0, #2
- ldr r1, =GSTATUS2
- str r0, [r1]