Xmega Application Note


wdt_example.c File Reference

XMEGA Watchdog driver example source. More...

#include "avr_compiler.h"
#include "wdt_driver.h"

Include dependency graph for wdt_example.c:

Go to the source code of this file.

Defines

#define F_CPU   (2000000UL)
 System clock used when running the code example.
#define NORMAL_MODE_DELAY   ( TO_WD / 2 )
 Delay (in ms) between each WD reset in normal mode.
#define TO_WD   128
 Duration of the WD timeout period (in ms).
#define TO_WDW   64
 Duration of the WD window timeout period (in ms).
#define WINDOW_MODE_DELAY   ( (TO_WDW) + (TO_WD / 2) )
 Delay (in ms) between each WD reset in window mode.

Functions

int main (void)
 Main function to call wanted example.
void wdt_fuse_enable_example (void)
 Example showing WD normal operation when WD is enabled through fuses.
void wdt_sw_enable_example (void)
 Example showing WD windowed operation when WD is enabled by the application.


Detailed Description

XMEGA Watchdog driver example source.

This file contains two example applications that demonstrate the Watchdog driver with the Watchdog being enabled by either fuses or the application. Selecting which example to run is done by commenting/uncommenting the wanted example function call in the main function. The examples illustrate configuration and operation of the Watchdog in both normal and window mode.

Application note:
AVR1310: Using the XMEGA Watchdog
Documentation
For comprehensive code documentation, supported compilers, compiler settings and supported devices see readme.html
Author:
Atmel Corporation: http://www.atmel.com
Support email: avr@atmel.com
Revision
2303
Date
2009-04-16 14:47:58 +0200 (to, 16 apr 2009)

Copyright (c) 2009, Atmel Corporation All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. The name of ATMEL may not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Definition in file wdt_example.c.


Define Documentation

#define F_CPU   (2000000UL)

System clock used when running the code example.

Definition at line 54 of file wdt_example.c.

#define NORMAL_MODE_DELAY   ( TO_WD / 2 )

Delay (in ms) between each WD reset in normal mode.

Definition at line 72 of file wdt_example.c.

Referenced by wdt_fuse_enable_example().

#define TO_WD   128

Duration of the WD timeout period (in ms).

Definition at line 63 of file wdt_example.c.

#define TO_WDW   64

Duration of the WD window timeout period (in ms).

Definition at line 66 of file wdt_example.c.

#define WINDOW_MODE_DELAY   ( (TO_WDW) + (TO_WD / 2) )

Delay (in ms) between each WD reset in window mode.

Definition at line 69 of file wdt_example.c.

Referenced by wdt_sw_enable_example().


Function Documentation

int main ( void   ) 

Main function to call wanted example.

The WD example is split into two examples, showing WD setup through fuses and software. Correct WDR execution is also shown for window and normal mode.

Definition at line 81 of file wdt_example.c.

References wdt_sw_enable_example().

00082 {
00083         wdt_sw_enable_example();
00084         /*wdt_fuse_enable_example();*/
00085 
00086 }

Here is the call graph for this function:

void wdt_fuse_enable_example ( void   ) 

Example showing WD normal operation when WD is enabled through fuses.

Note:
This example requires the WDLOCK fuse to be set. Also, the WD timeout period should be set by fuse to 128 cycles and the WD Window period to 64 cycles.

Definition at line 136 of file wdt_example.c.

References NORMAL_MODE_DELAY, WDT_DisableWindowMode(), WDT_EnableWindowMode(), WDT_IsWindowModeEnabled(), and WDT_Reset.

00137 {
00138         /* When the WD is enabled through fuses, always start by resetting the WDT. */
00139         WDT_Reset();
00140 
00141         if (true == WDT_IsWindowModeEnabled()){
00142                 while(1){
00143                         /* If window mode is already enabled, an error or a
00144                          * software reset has occured. It is therfore recommended
00145                          * to wait until the watchdog resets the entire system.
00146                          */
00147                 }
00148         }
00149 
00150         /* Since window mode cannot be enabled through fuses, this must be done by
00151          * the application. The window mode period is defined by the fuses and
00152          * cannot be changed since WDLOCK is set. */
00153         WDT_EnableWindowMode();
00154 
00155 
00156         /* WD is now configured and enabled. */
00157 
00158         /* If it is required to reconfigure the WD, it is recommended
00159          * to reset the WDT first, to avoid timer overflow during synchronization.
00160          * Since window mode is enabled, it is nescessary to wait until the timer is
00161          * in the open window before issuing the WDR. Be sure to wait for at least
00162          * (TO_WDW * 1.3) and no more than ((TO_WDW + TO_WD)*0.7) between resets to
00163          * take clock variation into account. In this example, 90 ms is chosen,
00164          * which satisfies this constraint.*/
00165 
00166         delay_us( 90000 );
00167         WDT_Reset();
00168 
00169         WDT_DisableWindowMode();
00170 
00171         /* Enter a timed loop to show timely execution of WD reset. */
00172         while(true)
00173         {
00174                 uint16_t repeatCounter;
00175                 /* Make sure that the WDT is not reset too early - insert delay */
00176                 for (repeatCounter = NORMAL_MODE_DELAY; repeatCounter > 0; --repeatCounter ) {
00177                         delay_us( 1000 ); /* 1ms delay @ 2MHz */
00178                 }
00179                 WDT_Reset();
00180         }
00181 }

Here is the call graph for this function:

void wdt_sw_enable_example ( void   ) 

Example showing WD windowed operation when WD is enabled by the application.

Note:
This example requires the WDLOCK fuse to NOT be set.

Definition at line 95 of file wdt_example.c.

References WDT_Disable(), WDT_EnableAndSetTimeout(), WDT_EnableWindowModeAndSetTimeout(), WDT_Reset, and WINDOW_MODE_DELAY.

Referenced by main().

00096 {
00097         /* To run the WD in normal mode, set the timer period and enable WD. */
00098         WDT_EnableAndSetTimeout( WDT_PER_32CLK_gc );
00099 
00100         /* The WD is now configured and enabled. */
00101 
00102 
00103         /* If it is required to configure or reconfigure the WD, it is recommended
00104          * to either reset or disable the WDT first. */
00105         WDT_Disable();
00106 
00107         /* When reconfiguring the WD to window mode, start with enabling normal
00108          * mode with the new settings, before configuring window mode. The reason
00109          * for this is that the window mode control register can only be written
00110          * when normal mode is enabled. */
00111         WDT_EnableAndSetTimeout( WDT_PER_128CLK_gc );
00112 
00113         /* Configure and enable window mode. */
00114         WDT_EnableWindowModeAndSetTimeout( WDT_WPER_64CLK_gc );
00115 
00116         /* Enter a timed loop to show timely execution of WD reset. */
00117         while(true)
00118         {
00119                 uint16_t repeatCounter;
00120                 /* Make sure that the WDT is not reset too early - insert delay */
00121                 for (repeatCounter = WINDOW_MODE_DELAY; repeatCounter > 0; --repeatCounter ) {
00122                         delay_us( 1000 ); /* 1ms delay @ 2MHz */
00123                 }
00124                 WDT_Reset();
00125         }
00126 }

Here is the call graph for this function:

@DOC_TITLE@
Generated on Thu Apr 16 15:20:10 2009 for AVR1310 Using the XMEGA Watchdog by doxygen 1.5.8