Xmega Application Note


wdt_example.c

Go to the documentation of this file.
00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
00054 #define F_CPU (2000000UL)
00055 
00056 #include "avr_compiler.h"
00057 #include "wdt_driver.h"
00058 
00059 void wdt_fuse_enable_example( void );
00060 void wdt_sw_enable_example( void );
00061 
00063 #define TO_WD     128
00064 
00066 #define TO_WDW    64
00067 
00069 #define WINDOW_MODE_DELAY   ( (TO_WDW) +  (TO_WD / 2) )
00070 
00072 #define NORMAL_MODE_DELAY   ( TO_WD / 2 )
00073 
00074 
00075 
00081 int main( void )
00082 {
00083         wdt_sw_enable_example();
00084         /*wdt_fuse_enable_example();*/
00085 
00086 }
00087 
00088 
00089 
00095 void wdt_sw_enable_example( void )
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 }
00127 
00128 
00129 
00136 void wdt_fuse_enable_example( void )
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 }
@DOC_TITLE@
Generated on Thu Apr 16 15:20:08 2009 for AVR1310 Using the XMEGA Watchdog by doxygen 1.5.8