天天看點

uboot關閉看門狗和中斷---嵌入式回歸第八篇

上一節中根據uboot架構分析我們第一步設定系統為SVC模式,這一小節根據架構設計步驟來關閉看門狗和屏蔽中斷!(架構中的第二步和第三步)

相信做嵌入式這行的對看門狗都不陌生!

1. 看門狗的作用:在嵌入式領域,有些系統需要長期運作在無人看守的環境。在運作過程中,難免不出現系統當機的情況,這時就需要系統自身帶有一種自動重新開機的功能。watchdog一般就是一個硬體子產品,其作用就是在系統當機時幫助系統實作自動重新開機。

2. 看門狗的工作方式:watchdog在硬體上實作了計時功能,啟動計時後,使用者軟體必須在計時結束前重新開始計時,俗稱“喂狗”,如果到逾時的時候還沒有重新開始計時,那麼它就認為系統是當機了,就自動重新開機系統。

這裡貼上6410 Watchdog ARM晶片手冊上看門狗部分的英文原版!

34  WATCHDOG TIMER 

This chapter describes the functions and usage of Watchdog Timer in S3C6410X RISC microprocessor.  

34.1 OVERVIEW  

The S3C6410X RISC microprocessor watchdog timer is used to resume the controller operation whenever it is 

disturbed by malfunctions such as noise and system errors. The watchdog timer generates the reset signal.          

It can be used as a normal 16-bit interval timer to request interrupt service.  

Advantage in using WDT instead of PWM timer is that WDT generates the reset signal.  

34.2 FEATURES   

The Watchdog Timer includes the following features:  

• 

Normal interval timer mode with interrupt request. 

• 

Internal reset signal is activated when the timer count value reaches 0 (time-out).  

• 

Level-triggered Interrupt mechanism. 

34.3 FUNCTIONAL DESCRIPTION   

34.3.1 WATCHDOG TIMER OPERATION  

Figure 34-1 shows the functional block diagram of the watchdog timer. The watchdog timer uses only PCLK as its 

source clock. The PCLK frequency is prescaled to generate the corresponding watchdog timer clock, and the 

resulting frequency is divided again.  

下面的一張圖可以分為三個部分:

1. 時鐘源PCLK ,分頻器将時鐘源分頻出來的時鐘就是Watchdog所用的時鐘!

2. 計數邏輯子產品,減1操作,設定一個時間, 會一直減減,是以一定時間要定時重新設定新值

3.  減到0時會産生一個信号 Reset Signal Generator--->RESET

一般uboot程式比較簡單,是以一開始都是把watchdog關閉

uboot關閉看門狗和中斷---嵌入式回歸第八篇

The prescaler value and the frequency division factor are specified in the watchdog timer control (WTCON) 

register. Valid prescaler values range from 0 to 28-1. The frequency division factor can be selected as 16, 32, 64, 

or 128. 

Use the following equation to calculate the watchdog timer clock frequency and the duration of each timer clock 

cycle: 

            t_watchdog  = 1/( PCLK / (Prescaler value + 1) / Division_factor ) 

34.3.2 WTDAT & WTCNT  

Once the watchdog timer is enabled, the value of watchdog timer data (WTDAT) register cannot be automatically 

reloaded into the timer counter (WTCNT). For this reason, an initial value must be written to the watchdog timer 

count (WTCNT) register, before the watchdog timer starts.  

34.3.3 CONSIDERATION OF DEBUGGING ENVIRONMENT  

When the S3C6410 is in debug mode using Embedded ICE, the watchdog timer must not operate.  

The watchdog timer can determine whether or not it is currently in the debug mode from the CPU core signal 

(DBGACK signal). Once the DBGACK signal is asserted, the reset output of the watchdog timer is not activated as 

the watchdog timer is expired. 

34.4 SPECIAL FUNCTION REGISTER 

34.4.1 MEMORY MAP  

uboot關閉看門狗和中斷---嵌入式回歸第八篇

34.5 INDIVIDUAL REGISTER DESCRIPTION 

34.5.1 WATCHDOG TIMER CONTROL (WTCON) REGISTER  

The WTCON register allows the user to enable/disable the watchdog timer, select the clock signal from 4 different 

sources, enable/disable interrupts, and enable/disable the watchdog timer output.  

The Watchdog timer is used to resume the S3C6410 restart on mal-function after its power on. At this time, 

disable the interrupt generation and enable the Watchdog timer output for reset signal.  

If controller restart is not desired and if the user wants to use the normal timer only, which is provided by the 

Watchdog timer, enable the interrupt generation and disable the Watchdog timer output for reset signal.  

從下面的表格可以看出:WTCON可以控制watchdog的開關,看下面的表格設定第0位為0就disable了看門狗

1. 設定那個寄存器, 寄存器位址

2. 設定寄存器哪一位可以關閉看門狗

3. 然後用代碼實作

uboot關閉看門狗和中斷---嵌入式回歸第八篇

Note: Initial state of ‘Reset enable/disable’ is 1(reset enable). If user do not disable this bit, S3C6410 will be 

rebooted in about 5.63sec (In the case of PCLK is 12MHz). So at boot loader, this bit should be disabled before 

under control of Operating System, or Firmware. 

這後面的可以先了解一下,這裡暫時用不到!

34.5.2 WATCHDOG TIMER DATA (WTDAT) REGISTER  

The WTDAT register is used to specify the time-out duration. The content of WTDAT cannot be automatically 

loaded into the timer counter at initial watchdog timer operation. However, using 0x8000 (initial value of WTCNT) 

will drive the first time-out. Then, the value of WTDAT will be automatically reloaded into WTCNT.  

uboot關閉看門狗和中斷---嵌入式回歸第八篇

34.5.3 WATCHDOG TIMER COUNT (WTCNT) REGISTER   

The WTCNT register contains the current count values for the watchdog timer during normal operation.  

NOTE:  

The content of the WTDAT register cannot be automatically loaded into the timer count register when the 

watchdog timer is enabled initially, so the WTCNT register must be set to an initial value before enabling 

it. 

uboot關閉看門狗和中斷---嵌入式回歸第八篇

接着上一課, 這裡貼上關閉Watchdog的彙編代碼(OK6410)

<span style="font-size:18px;">reset:
		bl set_svc
		bl disable_watchdog
		
set_svc:
		mrs r0, cpsr
		bic r0, r0, #0x1f   @後5位清零
		orr r0, r0, #0xd3   @相應位置1 10011
		msr cpsr, r0 @寫入GPSR寄存器
		mov pc, lr 

#define pWTCON 0x7e004000 @watchdog CON寄存器位址
disable_watchdog:
		ldr r0, =pWTCON @将位址裝載到通用寄存器
		mov r1, #0x0 @将0傳到r1寄存器中
		str r1, [r0] @将值傳到到寄存器位址所在的記憶體中
		mov pc, lr</span>
           

下面來關閉中斷部分:(還是看datasheet)

6410采用的是中斷向量的方式,采用硬體自動跳轉!

關閉中斷分為兩個環節:

1.  CPSR寄存器I 位控制中斷, 還有F位控制快速中斷, 這裡其實在設定SVC模式時就附帶做了!這裡可以在前面的博文中找到CPSR寄存器手冊截圖!(看看可以前後連接配接起來)

2. 還需要設定中斷屏蔽寄存器

同上面一樣,先找到那個寄存器,然後寄存器要設定什麼值,從下面手冊上的截圖表格最後一行可以看到Interupt Enable Register 就是我們要找的

uboot關閉看門狗和中斷---嵌入式回歸第八篇

從datasheet中找到這個寄存器:

uboot關閉看門狗和中斷---嵌入式回歸第八篇

這裡可以看到6410的中斷分為兩組,VIC0和VIC1, 下面來看看這兩個寄存器的描述!中斷使能寄存器

uboot關閉看門狗和中斷---嵌入式回歸第八篇

從上面的表格看到,這裡這個寄存器隻能使能中斷,要屏蔽中斷要使用VICINTENCLEAR寄存器

下面再來看看VICINTENCLEAR寄存器:

uboot關閉看門狗和中斷---嵌入式回歸第八篇

寫0是no effect, 看上面的截圖可以看出要向兩個寄存器中寫入全1

這裡貼上代碼,還是在之前的博文上start.S基礎上加上這部分的代碼!

reset:
		bl set_svc
		bl disable_watchdog
		bl disable_interrupt
		
set_svc:
		mrs r0, cpsr
		bic r0, r0, #0x1f   @後5位清零
		orr r0, r0, #0xd3   @相應位置1 10011
		msr cpsr, r0 @寫入GPSR寄存器
		mov pc, lr 

#define pWTCON 0x7e004000 @watchdog CON寄存器位址
disable_watchdog:
		ldr r0, =pWTCON @将位址裝載到通用寄存器
		mov r1, #0x0 @将0傳到r1寄存器中
		str r1, [r0] @将值傳到到寄存器位址所在的記憶體中
		mov pc, lr
		
disable_interrupt:
	mvn r1,#0x0 @寫入全1 取反然後填入到r1寄存器中
	ldr r0,=0x71200014 @用r0儲存位址
	str r1,[r0] @然後将r1中的值寫入到ro位址中去

	ldr r0,=0x71300014
	str r1,[r0]
	mov pc, lr
           

繼續閱讀