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