wdt_drv.h

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 #ifndef _WDT_DRV_H_
00017 #define _WDT_DRV_H_
00018 
00019 //_____ I N C L U D E S ____________________________________________________
00020 
00021 #ifdef AVRGCC
00022    #include <avr/io.h>
00023    #include <avr/wdt.h>
00024 #endif
00025 
00026 
00027 #ifdef AVRGCC
00028 //#define Wdt_reset_instruction()   (asm("WDR"))
00029 #define Wdt_reset_instruction()  (wdt_reset())
00030 #else
00031 #define Wdt_reset_instruction()  (__watchdog_reset())
00032 #endif
00033 
00034 
00035 // ======================
00036 // == LOW LEVEL MACROS ==
00037 // ======================
00038 
00039 // ** GENERAL RESET **
00040 #define   Is_ext_reset()                ((MCUSR&(1<<EXTRF)) ? TRUE:FALSE)
00041 #define   Ack_ext_reset()               (MCUSR= ~(1<<EXTRF))
00042 #define   Is_POR_reset()                ((MCUSR&(1<<(MCUSR= ~(1<<PORF)))) ? TRUE:FALSE)
00043 #define   Ack_POR_reset()               (MCUSR= ~(1<<PORF))
00044 #define   Is_BOD_reset()                ((MCUSR&(1<<BORF)) ? TRUE:FALSE)
00045 #define   Ack_BOD_reset()               (MCUSR= ~(1<<BORF))
00046 #define   Is_wdt_reset()                ((MCUSR&(1<<WDRF)) ? TRUE:FALSE)
00047 #define   Ack_wdt_reset()               (MCUSR= ~(1<<WDRF))
00048 #define   Wdt_clear_flag()              Ack_wdt_reset()
00049 
00050 // ** WATCHDOG GENERAL **
00051 #define   Wdt_change_enable()           (WDTCSR = (1<<WDCE) | (1<<WDE))     // to change WDE or prescaler
00052 #define   Wdt_system_reset_enable()     (WDTCSR |= (1<<WDE))
00053 #define   Wdt_system_reset_disable()    (WDTCSR &= ~(1<<WDE))
00054 #define   Wdt_stop()                    (WDTCSR = 0x00, WDTCKD = 0x00)
00055 
00056 // ** INTERRUPTS & FLAGS **
00057 // Warning : both flags are auto cleared once the interrupt routine is branched (flag polling should be only used if interrupts are disabled)
00058 #define   Wdt_enable_early_interrupt()  (WDTCKD |= (1<<WDEWIE))
00059 #define   Wdt_disable_early_interrupt() (WDTCKD &= ~(1<<WDEWIE))
00060 #define   Is_wdt_early_interrupt()      (((WDTCKD&(1<<WDEWIF)) != 0) ? TRUE : FALSE)
00061 #define   Ack_wdt_early_interrupt()     (WDTCKD |= (1<<WDEWIF))
00062 
00063 #define   Wdt_enable_real_interrupt()   (WDTCSR |= (1<<WDIE))
00064 #define   Wdt_disable_real_interrupt()  (WDTCSR &= ~(1<<WDIE))
00065 #define   Is_wdt_real_interrupt()       (((WDTCSR&(1<<WDIF)) != 0) ? TRUE : FALSE)
00066 #define   Ack_wdt_real_interrupt()      (WDTCSR |= (1<<WDIF))
00067 
00068 // ** CLOCK SOURCE DIVIDER **
00069 // Can be changed only if WDE and WDCE are set in a single instruction before
00070 #define   Wdt_set_clock_divider(ck)     (WDTCKD = ck)
00071 #define   WDT_CLOCK_DIV_1               0x00
00072 #define   WDT_CLOCK_DIV_3               0x01
00073 #define   WDT_CLOCK_DIV_5               0x02
00074 #define   WDT_CLOCK_DIV_7               0x03
00075 
00076 // ** PRESCALER **
00077 // Can be changed only if WDE and WDCE are set in a single instruction before
00078 #define   Wdt_set_prescaler(ck)         (WDTCSR = ck)
00079 #define   WDT_PRESC_1                   0x00
00080 #define   WDT_PRESC_2                   0x01
00081 #define   WDT_PRESC_4                   0x02
00082 #define   WDT_PRESC_8                   0x03
00083 #define   WDT_PRESC_16                  0x04
00084 #define   WDT_PRESC_32                  0x05
00085 #define   WDT_PRESC_64                  0x06
00086 #define   WDT_PRESC_128                 0x07
00087 #define   WDT_PRESC_256                 0x20
00088 #define   WDT_PRESC_512                 0x21
00089 
00090 
00091 // =======================
00092 // == HIGH LEVEL MACROS ==
00093 // ===============================================
00094 // = Select time-out delay first (prescaler), then select operating mode
00095 // ===============================================
00096 
00097 // These macros select the operating mode
00098 // WARNING : they don't clear the prescaler except for "Wdt_off()", so may be used after having set the prescaler
00099 #define   Wdt_off()                          (Wdt_reset_instruction(),  \
00100                                               Wdt_clear_flag(),         \
00101                                               Wdt_change_enable(),      \
00102                                               Wdt_stop())
00103 
00104 #define   Wdt_set_mode_interrupt()           (Wdt_reset_instruction(),  \
00105                                               Wdt_clear_flag(),         \
00106                                               WDTCSR |= (1<<WDIE))
00107 
00108 #define   Wdt_set_mode_system_reset()        (Wdt_reset_instruction(),  \
00109                                               Wdt_clear_flag(),         \
00110                                               WDTCSR |= (1<<WDE))
00111 
00112 #define   Wdt_set_mode_interrupt_and_reset() (Wdt_reset_instruction(),  \
00113                                               Wdt_clear_flag(),         \
00114                                               WDTCSR |= ((1<<WDE) | (1<<WDIE)))
00115 
00116 // These macros change the watchdog prescaler settings
00117 // The delay indicated below is the delay of the EARLY interrupt
00118 // The real delay before a wdt REAL interrupt or wdt reset is the double of the delay indicated below
00119 // WARNING : use these macros first, before setting operating mode, because they clear the current mode
00120 #define   Wdt_change_16ms()             (Wdt_reset_instruction(),                 \
00121                                          Wdt_change_enable(),                     \
00122                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_1),  \
00123                                          Wdt_change_enable(),                     \
00124                                          Wdt_set_prescaler(WDT_PRESC_1))
00125 #define   Wdt_change_32ms()             (Wdt_reset_instruction(),                 \
00126                                          Wdt_change_enable(),                     \
00127                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_1),  \
00128                                          Wdt_change_enable(),                     \
00129                                          Wdt_set_prescaler(WDT_PRESC_2))
00130 #define   Wdt_change_48ms()             (Wdt_reset_instruction(),                 \
00131                                          Wdt_change_enable(),                     \
00132                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_3),  \
00133                                          Wdt_change_enable(),                     \
00134                                          Wdt_set_prescaler(WDT_PRESC_1))
00135 #define   Wdt_change_64ms()             (Wdt_reset_instruction(),                 \
00136                                          Wdt_change_enable(),                     \
00137                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_1),  \
00138                                          Wdt_change_enable(),                     \
00139                                          Wdt_set_prescaler(WDT_PRESC_4))
00140 #define   Wdt_change_80ms()             (Wdt_reset_instruction(),                 \
00141                                          Wdt_change_enable(),                     \
00142                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_5),  \
00143                                          Wdt_change_enable(),                     \
00144                                          Wdt_set_prescaler(WDT_PRESC_1))
00145 #define   Wdt_change_96ms()             (Wdt_reset_instruction(),                 \
00146                                          Wdt_change_enable(),                     \
00147                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_3),  \
00148                                          Wdt_change_enable(),                     \
00149                                          Wdt_set_prescaler(WDT_PRESC_2))
00150 #define   Wdt_change_112ms()            (Wdt_reset_instruction(),                 \
00151                                          Wdt_change_enable(),                     \
00152                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_7),  \
00153                                          Wdt_change_enable(),                     \
00154                                          Wdt_set_prescaler(WDT_PRESC_1))
00155 #define   Wdt_change_125ms()            (Wdt_reset_instruction(),                 \
00156                                          Wdt_change_enable(),                     \
00157                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_1),  \
00158                                          Wdt_change_enable(),                     \
00159                                          Wdt_set_prescaler(WDT_PRESC_8))
00160 #define   Wdt_change_160ms()            (Wdt_reset_instruction(),                 \
00161                                          Wdt_change_enable(),                     \
00162                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_5),  \
00163                                          Wdt_change_enable(),                     \
00164                                          Wdt_set_prescaler(WDT_PRESC_2))
00165 #define   Wdt_change_192ms()            (Wdt_reset_instruction(),                 \
00166                                          Wdt_change_enable(),                     \
00167                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_3),  \
00168                                          Wdt_change_enable(),                     \
00169                                          Wdt_set_prescaler(WDT_PRESC_4))
00170 #define   Wdt_change_224ms()            (Wdt_reset_instruction(),                 \
00171                                          Wdt_change_enable(),                     \
00172                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_7),  \
00173                                          Wdt_change_enable(),                     \
00174                                          Wdt_set_prescaler(WDT_PRESC_2))
00175 #define   Wdt_change_250ms()            (Wdt_reset_instruction(),                 \
00176                                          Wdt_change_enable(),                     \
00177                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_1),  \
00178                                          Wdt_change_enable(),                     \
00179                                          Wdt_set_prescaler(WDT_PRESC_16))
00180 #define   Wdt_change_320ms()            (Wdt_reset_instruction(),                 \
00181                                          Wdt_change_enable(),                     \
00182                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_5),  \
00183                                          Wdt_change_enable(),                     \
00184                                          Wdt_set_prescaler(WDT_PRESC_4))
00185 #define   Wdt_change_375ms()            (Wdt_reset_instruction(),                 \
00186                                          Wdt_change_enable(),                     \
00187                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_3),  \
00188                                          Wdt_change_enable(),                     \
00189                                          Wdt_set_prescaler(WDT_PRESC_8))
00190 #define   Wdt_change_448ms()            (Wdt_reset_instruction(),                 \
00191                                          Wdt_change_enable(),                     \
00192                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_7),  \
00193                                          Wdt_change_enable(),                     \
00194                                          Wdt_set_prescaler(WDT_PRESC_4))
00195 #define   Wdt_change_500ms()            (Wdt_reset_instruction(),                 \
00196                                          Wdt_change_enable(),                     \
00197                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_1),  \
00198                                          Wdt_change_enable(),                     \
00199                                          Wdt_set_prescaler(WDT_PRESC_32))
00200 #define   Wdt_change_640ms()            (Wdt_reset_instruction(),                 \
00201                                          Wdt_change_enable(),                     \
00202                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_5),  \
00203                                          Wdt_change_enable(),                     \
00204                                          Wdt_set_prescaler(WDT_PRESC_8))
00205 #define   Wdt_change_750ms()            (Wdt_reset_instruction(),                 \
00206                                          Wdt_change_enable(),                     \
00207                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_3),  \
00208                                          Wdt_change_enable(),                     \
00209                                          Wdt_set_prescaler(WDT_PRESC_16))
00210 #define   Wdt_change_896ms()            (Wdt_reset_instruction(),                 \
00211                                          Wdt_change_enable(),                     \
00212                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_7),  \
00213                                          Wdt_change_enable(),                     \
00214                                          Wdt_set_prescaler(WDT_PRESC_8))
00215 #define   Wdt_change_1s()               (Wdt_reset_instruction(),                 \
00216                                          Wdt_change_enable(),                     \
00217                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_1),  \
00218                                          Wdt_change_enable(),                     \
00219                                          Wdt_set_prescaler(WDT_PRESC_64))
00220 #define   Wdt_change_1s25()             (Wdt_reset_instruction(),                 \
00221                                          Wdt_change_enable(),                     \
00222                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_5),  \
00223                                          Wdt_change_enable(),                     \
00224                                          Wdt_set_prescaler(WDT_PRESC_16))
00225 #define   Wdt_change_1s5()              (Wdt_reset_instruction(),                 \
00226                                          Wdt_change_enable(),                     \
00227                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_3),  \
00228                                          Wdt_change_enable(),                     \
00229                                          Wdt_set_prescaler(WDT_PRESC_32))
00230 #define   Wdt_change_1s75()             (Wdt_reset_instruction(),                 \
00231                                          Wdt_change_enable(),                     \
00232                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_7),  \
00233                                          Wdt_change_enable(),                     \
00234                                          Wdt_set_prescaler(WDT_PRESC_16))
00235 #define   Wdt_change_2s()               (Wdt_reset_instruction(),                 \
00236                                          Wdt_change_enable(),                     \
00237                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_1),  \
00238                                          Wdt_change_enable(),                     \
00239                                          Wdt_set_prescaler(WDT_PRESC_128))
00240 #define   Wdt_change_2s5()              (Wdt_reset_instruction(),                 \
00241                                          Wdt_change_enable(),                     \
00242                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_5),  \
00243                                          Wdt_change_enable(),                     \
00244                                          Wdt_set_prescaler(WDT_PRESC_32))
00245 #define   Wdt_change_3s()               (Wdt_reset_instruction(),                 \
00246                                          Wdt_change_enable(),                     \
00247                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_3),  \
00248                                          Wdt_change_enable(),                     \
00249                                          Wdt_set_prescaler(WDT_PRESC_64))
00250 #define   Wdt_change_3s5()              (Wdt_reset_instruction(),                 \
00251                                          Wdt_change_enable(),                     \
00252                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_7),  \
00253                                          Wdt_change_enable(),                     \
00254                                          Wdt_set_prescaler(WDT_PRESC_32))
00255 #define   Wdt_change_4s()               (Wdt_reset_instruction(),                 \
00256                                          Wdt_change_enable(),                     \
00257                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_1),  \
00258                                          Wdt_change_enable(),                     \
00259                                          Wdt_set_prescaler(WDT_PRESC_256))
00260 #define   Wdt_change_5s()               (Wdt_reset_instruction(),                 \
00261                                          Wdt_change_enable(),                     \
00262                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_5),  \
00263                                          Wdt_change_enable(),                     \
00264                                          Wdt_set_prescaler(WDT_PRESC_64))
00265 #define   Wdt_change_6s()               (Wdt_reset_instruction(),                 \
00266                                          Wdt_change_enable(),                     \
00267                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_3),  \
00268                                          Wdt_change_enable(),                     \
00269                                          Wdt_set_prescaler(WDT_PRESC_128))
00270 #define   Wdt_change_7s()               (Wdt_reset_instruction(),                 \
00271                                          Wdt_change_enable(),                     \
00272                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_7),  \
00273                                          Wdt_change_enable(),                     \
00274                                          Wdt_set_prescaler(WDT_PRESC_64))
00275 #define   Wdt_change_8s()               (Wdt_reset_instruction(),                 \
00276                                          Wdt_change_enable(),                     \
00277                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_1),  \
00278                                          Wdt_change_enable(),                     \
00279                                          Wdt_set_prescaler(WDT_PRESC_512))
00280 #define   Wdt_change_10s()              (Wdt_reset_instruction(),                 \
00281                                          Wdt_change_enable(),                     \
00282                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_5),  \
00283                                          Wdt_change_enable(),                     \
00284                                          Wdt_set_prescaler(WDT_PRESC_128))
00285 #define   Wdt_change_12s()              (Wdt_reset_instruction(),                 \
00286                                          Wdt_change_enable(),                     \
00287                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_3),  \
00288                                          Wdt_change_enable(),                     \
00289                                          Wdt_set_prescaler(WDT_PRESC_256))
00290 #define   Wdt_change_14s()              (Wdt_reset_instruction(),                 \
00291                                          Wdt_change_enable(),                     \
00292                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_7),  \
00293                                          Wdt_change_enable(),                     \
00294                                          Wdt_set_prescaler(WDT_PRESC_128))
00295 #define   Wdt_change_20s()              (Wdt_reset_instruction(),                 \
00296                                          Wdt_change_enable(),                     \
00297                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_5),  \
00298                                          Wdt_change_enable(),                     \
00299                                          Wdt_set_prescaler(WDT_PRESC_256))
00300 #define   Wdt_change_24s()              (Wdt_reset_instruction(),                 \
00301                                          Wdt_change_enable(),                     \
00302                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_3),  \
00303                                          Wdt_change_enable(),                     \
00304                                          Wdt_set_prescaler(WDT_PRESC_512))
00305 #define   Wdt_change_28s()              (Wdt_reset_instruction(),                 \
00306                                          Wdt_change_enable(),                     \
00307                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_7),  \
00308                                          Wdt_change_enable(),                     \
00309                                          Wdt_set_prescaler(WDT_PRESC_256))
00310 #define   Wdt_change_40s()              (Wdt_reset_instruction(),                 \
00311                                          Wdt_change_enable(),                     \
00312                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_5),  \
00313                                          Wdt_change_enable(),                     \
00314                                          Wdt_set_prescaler(WDT_PRESC_512))
00315 #define   Wdt_change_56s()              (Wdt_reset_instruction(),                 \
00316                                          Wdt_change_enable(),                     \
00317                                          Wdt_set_clock_divider(WDT_CLOCK_DIV_7),  \
00318                                          Wdt_change_enable(),                     \
00319                                          Wdt_set_prescaler(WDT_PRESC_512))
00320 
00321 #endif  // _WDT_DRV_H_
00322 
00323 
00324 

Generated on Fri Jun 15 14:07:33 2007 for Atmel by  doxygen 1.5.1-p1