FL2440无操作系统应用程序编写测试009——WATCHDOG TIMER

Subject:FL2440无操作系统应用程序编写测试009——WATCHDOG TIMER

Date:   22-Nov-2011

By:       Calvinlee1984@163.com

 

1.Watchdog Timer简介

嵌入式系统运行时受到外部干扰或者系统错误,程序有时会跑飞,导致整个系统瘫痪。为了防止这一现像的发生,在对系统稳定性要求较高的场合往往要加入看门狗(Watchdog)电路。其基本原理为:设本系统程序完整运行一周期的时间是Tp,看门狗定时周期为Ti,Ti>Tp,在程序正常运行时,定时器不会溢出,若由于干扰等原因使系统不能在Tp时刻之前修改定时器的计数值,定时器将在Ti时刻溢出,引发系统复位,使系统得以重新运行,从而起到监控作用。

 

2.Watchdog Timer控制框图

FL2440无操作系统应用程序编写测试009——WATCHDOG TIMER_第1张图片

 

3.Watchdog Timer寄存器设置

static void Watchdog_Port_Init(void){

       //Watchdog 时钟信号设置

       //t_watchdog = 1/[50Mhz/(199+1)/128] = 0.000512s

      

       rWTCON = (199<<8|0x3<<3);     //设置预分频值,比例因子

      FL2440无操作系统应用程序编写测试009——WATCHDOG TIMER_第2张图片

}

 

4. 代码分析 Watchdog.c

#include "2440addr.h"

#include "2440lib.h"

#include "UART.h"

#include "Option.h"

 

//Watchdog寄存器设置

static void Watchdog_Port_Init(void){

       //t_watchdog = 1/[50Mhz/(199+1)/128] = 0.000512s

       rWTCON = (199<<8|0x3<<3);             

}

 

//Watchdog Timer中断处理程序

static void __irq Watchdog_Timer_ISR(void){

       U32 r;

       EnterCritical(&r);

       if(rSUBSRCPND & BIT_SUB_WDT){

               ClearSubPending(BIT_SUB_WDT);

               ClearPending(BIT_WDT_AC97);

               UART0_Printf("Watchdog Timer Up!\n");

       }    

       ExitCritical(&r);

}

 

//Watchdog timer with reset signal

void Watchdog_Reset_Test(void){

 

       Watchdog_Port_Init();   //Watchdog寄存器设置

 

       //Watchdog 定时数据寄存器,用于设定定时时间

       rWTDAT = 4000;

      

      

       //Watchdog 计数数据寄存器

       //存储当前的计数值,当该值为零时会产生INT_SUB_WDT中断源

       rWTCNT = 4000;

      

 

       //Enable reset function and start watchdog timer

       rWTCON |= (0x1<<5|0x1<<0);

       FL2440无操作系统应用程序编写测试009——WATCHDOG TIMER_第3张图片

       while(1){

               UART0_Printf("Wait for reset signal...\n");

               Delay(500);

       }

}

 

 

void Watchdog_Timer_Test(void){

       Watchdog_Port_Init();   //Watchdog寄存器设置

       rWTDAT = 2000;

       rWTCNT = 2000;

       pISR_WDT_AC97 = (int)Watchdog_Timer_ISR; //安装中断处理程序

        //使能中断

       EnableIrq(BIT_WDT_AC97);

       EnableSubIrq(BIT_SUB_WDT);

       //Enable interrupt and start watchdog timer

       rWTCON |= (0x1<<5|0x1<<2);

       FL2440无操作系统应用程序编写测试009——WATCHDOG TIMER_第4张图片

       while(UART0_GetKey()!=ESC_KEY){

               ;      //wait for WDT interrupt

       }

       //Disable interrupt and stop watchdog timer

       rWTCON &= ~(0x1<<5|0x1<<2);      

       

        //禁止中断

       DisableSubIrq(BIT_SUB_WDT);

       DisableIrq(BIT_WDT_AC97);

}

 

5.测试程序及结果

#include "UART.h"

#include "Watchdog.h"

 

int Main(void){

       UART0_Port_Init(115200);    //UART0端口初始化

       //Watchdog_Reset_Test();

       Watchdog_Timer_Test();

       while(1){

               ;            

       }

       return 0;

}

 

FL2440无操作系统应用程序编写测试009——WATCHDOG TIMER_第5张图片

 

FL2440无操作系统应用程序编写测试009——WATCHDOG TIMER_第6张图片

 

你可能感兴趣的:(timer,function,测试,嵌入式,代码分析,delay)