⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wdt_sample.c

📁 oki67500系列arm工程例程源代码
💻 C
字号:
/**********************************************************************************/
/*                                                                                */
/*    Copyright (C) 2003 Oki Electric Industry Co., LTD.                          */
/*                                                                                */
/*    System Name    :  ML675001 series                                           */
/*    Module Name    :  WDT Sample Program                                        */
/*    File   Name    :  wdt_sample.c                                              */
/*    Revision       :  01.00                                                     */
/*    Date           :  2003/03/09 initial version                                */
/*                                                                                */
/**********************************************************************************/
#include "ML675001.h"
#include "common.h"
#include "irq.h"
#include "cache.h"

/* constants */
#define MHz     (1000000L)
#define TMRCYC  (10)      /* interval of TIMER interrupt (ms) */
#define CCLK    (60*MHz)    /* CCLK (Hz) */
#define VALUE_OF_TMRLR      /* reload value of timer */\
                ((0x10000L*(16*1000)-((TMRCYC)*(CCLK)))/(16*1000))
#define LED_ERROR_END_PATTERN   0x0079

#if ((VALUE_OF_TMRLR) < 0 || 0x10000 <= (VALUE_OF_TMRLR))
#error Invalid value : VALUE_OF_TMRLR
#endif

/* functions */
int main(void);                     /* main routine */
static void reg_irq_handler(void);  /* registration of IRQ handler */
static void set_timer(void);        /* setup TIMER */
static void set_wdt(void);          /* setup WDT */
static void init_var(void);         /* initialize variables */
static void wdt_handler(void);      /* WDT handler */
static void timer_handler(void);    /* TIMER handler */

/* global variables */
static volatile int count;    /* counter of timer interrupt */
static volatile int end_flag;   /* end flag */

/****************************************************************************/
/*  Entry point                                                             */
/*  Function : main                                                         */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   0                                                   */
/****************************************************************************/
int main(void)
{

    init_cache();  /* Initialize CACHE memory */
    cache_on(CACHE_BANK0);  /* Bank0 : Cache enable */

    init_irq(); /* initialize IRQ */

    reg_irq_handler();  /* registration of IRQ handler */

    set_timer();    /* setup TIMER */

    set_wdt();  /* setup WDT */

    init_var(); /* initialize variables */
    
    irq_en();   /* enable IRQ */

    /* light LED */
    init_led();    /* output mode */
    led_on(LED_START_PATTERN);
    
    /* timer start */
    put_wvalue(TMEN, 0x01); /* enable timer (write '1' in TMEN[0]) */

    put_value(WDTCON, 0x3C);    /* wdt start */

    /* loop */
    while(end_flag != 1)
        ;

    /* wdt stop */
    put_value(WDTBCON, WDTBCON_WE);
    put_value(WDTBCON, WDTBCON_WDHLT);

    put_wvalue(TMEN, 0);    /* timer stop */

    irq_dis();  /* disable IRQ */
    
    /* output exit status */
    if(count >= 300)
        led_on(LED_NORMAL_END_PATTERN);   /* normal end */
    else
        led_on(LED_ERROR_END_PATTERN);   /* error end */

    return 0;
}

/****************************************************************************/
/*  Registration of IRQ Handler                                             */
/*  Function : reg_irq_handler                                              */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   Nothing                                             */
/*  Note : Initialization of IRQ needs to be performed before this process. */
/****************************************************************************/
void reg_irq_handler(void)
{
    /* register IRQ handlers into IRQ handler table */
    IRQ_HANDLER_TABLE[INT_SYSTEM_TIMER] = timer_handler;
    IRQ_HANDLER_TABLE[INT_WDT] = wdt_handler;

    /* setup interrupt level */
    set_wbit(ILC0, ILC0_ILR0 & ILC0_INT_LV1);   /* system timer(nIRQ[0]) -> level1 */
    set_wbit(ILC0, ILC0_ILR1 & ILC0_INT_LV2);   /* wdt(nIR[1]) -> level2 */

    return;
}

/****************************************************************************/
/*  Setup of timer                                                          */
/*  Function : set_timer                                                    */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   Nothing                                             */
/****************************************************************************/
void set_timer(void)
{
    put_wvalue(TMEN, 0x0);      /* disable timer (write '0' in TMEN[0])*/
    put_wvalue(TMOVF, 0x01);    /* clear overflow register (write '1' in TMOVF[0])*/
    put_wvalue(TMRLR, VALUE_OF_TMRLR);  /* set TMRLR */

    return;
}

/****************************************************************************/
/*  Setup of WDT                                                            */
/*  Function : set_wdt                                                      */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   Nothing                                             */
/****************************************************************************/
void set_wdt(void)
{
    put_value(WDTBCON, WDTBCON_WE);		/* enable writing to WDTBCON register */
    put_value(WDTBCON, WDTBCON_CLK256	/* CCLK/256 */
			          |WDTBCON_WDTM		/* WATCH DOG TIMER mode */
			          |WDTBCON_INT);	/* when WDT overflowed, interrupt occurs */

    return;
}

/****************************************************************************/
/*  initialize variables                                                    */
/*  Function : init_var                                                     */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   Nothing                                             */
/****************************************************************************/
void init_var(void)
{
    count = 0;
    end_flag = 0;

    return;
}

/****************************************************************************/
/*  WDT handler                                                             */
/*  Function : wdt_handler                                                  */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   Nothing                                             */
/****************************************************************************/
void wdt_handler(void)
{
    put_value(WDSTAT, WDSTAT_WDTIST);   /* clear WDT interrupt */
    end_flag = 1;

    /* it takes up to 20 clocks to clear WDTIST bit,
       so interrupt controller has to be cleared more than 20 clocks
       after WDTIST bit was cleared.
    */
    __asm{
        NOP
        NOP
        NOP
        NOP
        NOP
        NOP
        NOP
        NOP
        NOP
        NOP
    }
}

/****************************************************************************/
/*  System Timer handler                                                    */
/*  Function : timer_handler                                                */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   Nothing                                             */
/****************************************************************************/
void timer_handler(void)
{
    static UBYTE clr_value = WDTCON_0xC3;
    
    put_wvalue(TMOVF, 0x01);    /* clear TMOVF register (write '1' in TMOVF[0])
                                   if TMOVF register isn't cleared,
                                   timer interrupt never occur. */

    /* clear WDT */
    put_value(WDTCON, clr_value);
    clr_value = ~clr_value;

    count++;

    if(count >= 300)
        put_wvalue(TMEN, 0x00); /* stop timer */
    
    return;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -