blob: f8a493b5296c7a5d693e6e6ce186701d600f368b [file] [log] [blame]
Christina Quast8be71e42014-12-02 13:06:01 +01001/* ----------------------------------------------------------------------------
2 * ATMEL Microcontroller Software Support
3 * ----------------------------------------------------------------------------
4 * Copyright (c) 2009, Atmel Corporation
5 *
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the disclaimer below.
13 *
14 * Atmel's name may not be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
20 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * ----------------------------------------------------------------------------
28 */
29
30/**
31 * \file
32 *
33 * Implementation of Watchdog Timer (WDT) controller.
34 *
35 */
36
37/** \addtogroup wdt_module Working with WDT
38 * The WDT driver provides the interface to configure and use the WDT
39 * peripheral.
40 *
41 * The WDT can be used to prevent system lock-up if the software becomes
42 * trapped in a deadlock. It can generate a general reset or a processor
43 * reset only. It is clocked by slow clock divided by 128.
44 *
45 * The WDT is running at reset with 16 seconds watchdog period (slow clock at 32.768 kHz)
46 * and external reset generation enabled. The user must either disable it or
47 * reprogram it to meet the application requires.
48 *
49 * To use the WDT, the user could follow these few steps:
50 * <ul>
51 * <li>Enable watchdog with given mode using \ref WDT_Enable().
52 * <li>Restart the watchdog using \ref WDT_Restart() within the watchdog period.
53 * </ul>
54 *
55 * For more accurate information, please look at the WDT section of the
56 * Datasheet.
57 *
58 * \note
59 * The Watchdog Mode Register (WDT_MR) can be written only once.\n
60 *
61 * Related files :\n
62 * \ref wdt.c\n
63 * \ref wdt.h.\n
64 */
65/*@{*/
66/*@}*/
67
68/*---------------------------------------------------------------------------
69 * Headers
70 *---------------------------------------------------------------------------*/
71
72#include "chip.h"
73
74#include <stdint.h>
75
76/*----------------------------------------------------------------------------
77 * Exported functions
78 *----------------------------------------------------------------------------*/
79
80/**
81 * \brief Enable watchdog with given mode.
82 *
83 * \note The Watchdog Mode Register (WDT_MR) can be written only once.
84 * Only a processor reset resets it.
85 *
86 * \param dwMode WDT mode to be set
87 */
88extern void WDT_Enable( Wdt* pWDT, uint32_t dwMode )
89{
90 pWDT->WDT_MR = dwMode ;
91}
92
93/**
94 * \brief Disable watchdog.
95 *
96 * \note The Watchdog Mode Register (WDT_MR) can be written only once.
97 * Only a processor reset resets it.
98 */
99extern void WDT_Disable( Wdt* pWDT )
100{
101 pWDT->WDT_MR = WDT_MR_WDDIS;
102}
103
104/**
105 * \brief Watchdog restart.
106 */
107extern void WDT_Restart( Wdt* pWDT )
108{
109 pWDT->WDT_CR = 0xA5000001;
110}
111
112/**
113 * \brief Watchdog get status.
114 */
115extern uint32_t WDT_GetStatus( Wdt* pWDT )
116{
117 return (pWDT->WDT_SR & 0x3) ;
118}
119
120/**
121 * \brief Watchdog get period.
122 *
123 * \param dwMs desired watchdog period in millisecond.
124 */
125extern uint32_t WDT_GetPeriod( uint32_t dwMs )
126{
127 if ( (dwMs < 4) || (dwMs > 16000) )
128 {
129 return 0 ;
130 }
131 return ((dwMs << 8) / 1000) ;
132}