Nut/OS  4.10.3
API Reference
wdt_at91.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006 by egnite Software GmbH. All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holders nor the names of
00014  *    contributors may be used to endorse or promote products derived
00015  *    from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS
00018  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE
00021  * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00024  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00025  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00027  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.ethernut.de/
00031  *
00032  */
00033 
00034 /*
00035  * $Log$
00036  * Revision 1.4  2009/01/17 11:26:37  haraldkipp
00037  * Getting rid of two remaining BSD types in favor of stdint.
00038  * Replaced 'u_int' by 'unsinged int' and 'uptr_t' by 'uintptr_t'.
00039  *
00040  * Revision 1.3  2008/08/11 06:59:13  haraldkipp
00041  * BSD types replaced by stdint types (feature request #1282721).
00042  *
00043  * Revision 1.2  2006/05/25 09:13:22  haraldkipp
00044  * Platform independent watchdog API added.
00045  *
00046  * Revision 1.1  2006/02/23 15:36:35  haraldkipp
00047  * Added support for AT91 watchdog timer.
00048  *
00049  */
00050 
00051 #include <sys/timer.h>
00052 #include <dev/watchdog.h>
00053 
00058 
00059 #if defined(MCU_AT91R40008)
00060 static ureg_t nested;
00061 #endif
00062 
00069 uint32_t At91WatchDogStart(uint32_t ms, uint32_t xmode)
00070 {
00071     unsigned int cmval;
00072 
00073 #if defined(MCU_AT91R40008)
00074     At91WatchDogDisable();
00075 
00076     /*
00077      * The watchdog counts down a 16 bit value, of which the upper
00078      * 4 bits are configurable and the lower 12 bits are set to 1.
00079      * Calculate the number of cycles required to count down the
00080      * upper 4 bits.
00081      */
00082     cmval = ((NutGetCpuClock() / 1000) * ms) >> 13;
00083     
00084     /* Check if MCK/8 is slow enough. */
00085     if (cmval < WD_HPCV) {
00086         cmval = (cmval & WD_HPCV) | WD_WDCLKS_MCK8;
00087     }
00088     /* Check if MCK/32 is slow enough. */
00089     else if ((cmval >>= 2) < WD_HPCV) {
00090         cmval = (cmval & WD_HPCV) | WD_WDCLKS_MCK32;
00091     }
00092     /* Check if MCK/128 is slow enough. */
00093     else if ((cmval >>= 2) < WD_HPCV) {
00094         cmval = (cmval & WD_HPCV) | WD_WDCLKS_MCK128;
00095     }
00096     /* Check if MCK/1024 is slow enough. */
00097     else if ((cmval >>= 3) < WD_HPCV) {
00098         cmval = (cmval & WD_HPCV) | WD_WDCLKS_MCK1024;
00099     }
00100     /* Use maximum. */
00101     else {
00102         cmval = WD_HPCV | WD_WDCLKS_MCK1024;
00103     }
00104     outr(WD_CMR, WD_CKEY | cmval);
00105     if (xmode == 0) {
00106         xmode |= WD_RSTEN;
00107     }
00108     At91WatchDogRestart();
00109     outr(WD_OMR, WD_OKEY | WD_WDEN | xmode);
00110     nested = 1;
00111 #elif defined(MCU_AT91SAM7X) || defined(MCU_AT91SAM7S) || defined(MCU_AT91SAM7SE)
00112         /* Compute 12-bit timer value for 32Khz(approx) slow clock divided by 128 */
00113         cmval = (ms * (32000/128)) / 1000;
00114     if(cmval>4095) cmval=4095;          
00115 
00116         /* if mode=0, Enable watchdog and disable it when debugging (default value) */
00117     if (xmode == 0) {
00118         xmode |= WDT_WDRSTEN|WDT_WDDBGHLT|WDT_WDIDLEHLT;
00119     }
00120 
00121         /* Write watchdog, you can do it only one time. Delta fixed to timer value */
00122         outr(WDT_MR, cmval | (cmval<<16) | xmode);
00123         At91WatchDogRestart();
00124 #endif
00125 
00126     return ms;
00127 }
00128 
00135 void At91WatchDogRestart(void)
00136 {
00137 #if defined(MCU_AT91R40008)
00138     outr(WD_CR, WD_RSTKEY);
00139 #elif defined(MCU_AT91SAM7X) || defined(MCU_AT91SAM7S) || defined(MCU_AT91SAM7SE)
00140         outr(WDT_CR, WDT_KEY|WDT_WDRSTT);
00141 #endif
00142 }
00143 
00150 void At91WatchDogDisable(void)
00151 {
00152 #if defined(MCU_AT91R40008)
00153     if (nested) {
00154         nested++;
00155     }
00156     outr(WD_OMR, WD_OKEY | (inr(WD_OMR) & ~WD_WDEN));
00157 #elif defined(MCU_AT91SAM7X) || defined(MCU_AT91SAM7S) || defined(MCU_AT91SAM7SE)
00158     outr(WDT_MR, WDT_WDDIS);
00159 #endif
00160 }
00161 
00168 void At91WatchDogEnable(void)
00169 {
00170 #if defined(MCU_AT91R40008)
00171     if (nested > 1 && --nested == 1) {
00172         At91WatchDogRestart();
00173         outr(WD_OMR, WD_OKEY | inr(WD_OMR) | WD_WDEN);
00174     }
00175 #endif
00176 }
00177