1 
2 /******************************************************************************
3 *
4 * @brief Provide common watchdog module routines.
5 *
6 * @history:
7 * 	Jun. 25, 2013	modified the watch dog unlock sequence and disable sequence
8 ******************************************************************************/
9 #include "common.h"
10 #include "wdog.h"
11 
12 /******************************************************************************
13 * Global variables
14 ******************************************************************************/
15 
16 /******************************************************************************
17 * Constants and macros
18 ******************************************************************************/
19 
20 /******************************************************************************
21 * Local types
22 ******************************************************************************/
23 
24 /******************************************************************************
25 * Local function prototypes
26 ******************************************************************************/
27 
28 /******************************************************************************
29 * Local variables
30 ******************************************************************************/
31 
32 /******************************************************************************
33 * Local functions
34 ******************************************************************************/
35 
36 /******************************************************************************
37 * Global functions
38 ******************************************************************************/
39 
40 /******************************************************************************
41 * define watchdog API list
42 *
43 *//*! @addtogroup wdog_api_list
44 * @{
45 *******************************************************************************/
46 
47 /*****************************************************************************//*!
48 *
49 * @brief Watchdog ETMer disable routine.
50 *
51 * @param  none
52 *
53 * @return none
54 *
55 * @ Pass/ Fail criteria: none
56 * @see WDOG_Enable
57 *****************************************************************************/
58 
WDOG_Disable(void)59 void WDOG_Disable(void)
60 {
61     uint8_t u8Cs1 =  WDOG->CS1;
62     uint8_t u8Cs2 =  WDOG->CS2;
63     uint16_t u16TOVAL =  WDOG->TOVAL;
64     uint16_t u16WIN =  WDOG->WIN;
65 
66     u8Cs1       &= ~WDOG_CS1_EN_MASK;
67 
68 	/* First unlock the watchdog so that we can write to registers */
69     WDOG_Unlock();
70     WDOG->CS2    =  u8Cs2;
71     WDOG->TOVAL  =  u16TOVAL;
72     WDOG->WIN    =  u16WIN;
73     WDOG->CS1    =  u8Cs1;
74 }
75 
76 
77 /*****************************************************************************//*!
78 *
79 * @brief Watchdog ETMer disable routine with update enabled.
80 *
81 *   Disable watchdog but the watchdog can be enabled and updated later.
82 *
83 * @param  none
84 *
85 * @return none
86 *
87 * @ Pass/ Fail criteria: none
88 * @see WDOG_Enable
89 *****************************************************************************/
90 
WDOG_DisableWDOGEnableUpdate(void)91 void WDOG_DisableWDOGEnableUpdate(void)
92 {
93     uint8_t u8Cs1 =  WDOG->CS1;
94     uint8_t u8Cs2 =  WDOG->CS2;
95     uint16_t u16TOVAL =  WDOG->TOVAL;
96     uint16_t u16WIN =  WDOG->WIN;
97 
98     u8Cs1       &= ~WDOG_CS1_EN_MASK;
99     u8Cs1       |= WDOG_CS1_UPDATE_MASK;
100 
101 	/* First unlock the watchdog so that we can write to registers */
102     //WDOG_Unlock();
103     WDOG->CS2    =  u8Cs2;
104     WDOG->TOVAL  =  u16TOVAL;
105     WDOG->WIN    =  u16WIN;
106     WDOG->CS1    =  u8Cs1;
107 }
108 
109 /*****************************************************************************//*!
110 *
111 * @brief Watchdog ETMer enable routine.
112 *
113 * @param  none
114 *
115 * @return none
116 *
117 * @ Pass/ Fail criteria: none
118 * @see  WDOG_Disable
119 *****************************************************************************/
120 
WDOG_Enable(void)121 void WDOG_Enable(void)
122 {
123     uint8_t u8Cs1 =  WDOG->CS1;
124 
125     u8Cs1       |= WDOG_CS1_EN_MASK;
126 
127 	/* First unlock the watchdog so that we can write to registers */
128     WDOG_Unlock();
129     WDOG->CS1    = u8Cs1;
130 }
131 
132 
133 /*****************************************************************************//*!
134 *
135 * @brief initialize watchdog.
136 *
137 * @param[in]   pConfig  poiner to watchdog configuration strcture.
138 *
139 * @return none
140 *
141 * @ Pass/ Fail criteria: none
142 *
143 * @warning make sure that WDOG is not initialized after reset or WDOG update is enabled
144 * after reset by calling WDOG_EnableUpdate / WDOG_DisableWDOGEnableUpdate.
145 *
146 * @see WDOG_EnableUpdate, WDOG_DisableWDOGEnableUpdate
147 *
148 *****************************************************************************/
149 
WDOG_Init(WDOG_ConfigPtr pConfig)150 void WDOG_Init(WDOG_ConfigPtr pConfig)
151 {
152     uint8_t     u8Cs1;
153     uint8_t     u8Cs2;
154     uint16_t    u16Toval;
155     uint16_t    u16Win;
156 
157     u8Cs1       = 0x80;                                   /* default CS1 register value */
158     u8Cs2       = 0;
159     u16Toval    = pConfig->u16ETMeOut;
160     u16Win      = pConfig->u16WinETMe;
161 
162     if(pConfig->sBits.bDisable)
163     {
164         u8Cs1 &= ~WDOG_CS1_EN_MASK;
165     }
166     if(pConfig->sBits.bIntEnable)
167     {
168         u8Cs1 |= WDOG_CS1_INT_MASK;
169     }
170     if(pConfig->sBits.bStopEnable)
171     {
172         u8Cs1 |= WDOG_CS1_STOP_MASK;
173     }
174     if(pConfig->sBits.bDbgEnable)
175     {
176         u8Cs1 |= WDOG_CS1_DBG_MASK;
177     }
178     if(pConfig->sBits.bWaitEnable)
179     {
180         u8Cs1 |= WDOG_CS1_WAIT_MASK;
181     }
182     if(pConfig->sBits.bUpdateEnable)
183     {
184         u8Cs1 |= WDOG_CS1_UPDATE_MASK;
185     }
186     if(pConfig->sBits.bWinEnable)
187     {
188         u8Cs2 |= WDOG_CS2_WIN_MASK;
189     }
190     if(pConfig->sBits.bPrescaler)
191     {
192         u8Cs2 |= WDOG_CS2_PRES_MASK;
193     }
194     u8Cs2   |= (pConfig->sBits.bClkSrc & 0x03);
195 
196     /* write regisers */
197     WDOG_Unlock();              /* unlock watchdog first */
198     WDOG->CS2   = u8Cs2;
199 
200     WDOG->TOVAL8B.TOVALL  = u16Toval;
201     WDOG->TOVAL8B.TOVALH  = u16Toval >> 8;
202 
203     WDOG->WIN8B.WINL  = u16Win;
204     WDOG->WIN8B.WINH  = u16Win >> 8;
205 
206     WDOG->CS1   = u8Cs1;
207 }
208 
209 
210 /*****************************************************************************//*!
211 *
212 * @brief initialize watchdog to the default state.
213 *
214 * @param   none
215 *
216 * @return none
217 *
218 * @ Pass/ Fail criteria: none
219 * @warning make sure that WDOG update is enabled after reset by calling WDOG_EnableUpdate.
220 * or by calling WDOG_DisableWDOGEnableUpdate.
221 *
222 * @see WDOG_DisableWDOGEnableUpdate, WDOG_EnableUpdate
223 *
224 *****************************************************************************/
225 
WDOG_DeInit(void)226 void WDOG_DeInit(void)
227 {
228     WDOG_Unlock();
229 
230     WDOG->CS2    =  WDOG_CS2_DEFAULT_VALUE;
231     WDOG->TOVAL  =  WDOG_TOVAL_DEFAULT_VALUE;
232     WDOG->WIN    =  WDOG_WIN_DEFAULT_VALUE;
233     WDOG->CS1    =  WDOG_CS1_DEFAULT_VALUE;
234 }
235 
236 /*****************************************************************************//*!
237 *
238 * @brief feed/refresh watchdog.
239 *
240 * @param   none
241 *
242 * @return none
243 *
244 * @ Pass/ Fail criteria: none
245 *****************************************************************************/
246 
WDOG_Feed(void)247 void WDOG_Feed(void)
248 {
249     DisableInterrupts;
250     WDOG->CNT = 0x02A6;
251     WDOG->CNT = 0x80B4;
252     EnableInterrupts;
253 }
254 
255 
256 
257 /*****************************************************************************//*!
258 *
259 * @brief enable update of WDOG.
260 *
261 * @param  none
262 *
263 * @return none
264 *
265 * @ Pass/ Fail criteria: none
266 * @warning  this must be the last step of writing control bits sequence.
267 *****************************************************************************/
268 
WDOG_EnableUpdate(void)269 void WDOG_EnableUpdate(void)
270 {
271     uint8_t u8Cs1 =  WDOG->CS1;
272     uint8_t u8Cs2 =  WDOG->CS2;
273     uint16_t u16TOVAL =  WDOG->TOVAL;
274     uint16_t u16WIN =  WDOG->WIN;
275 
276     u8Cs1 |= WDOG_CS1_UPDATE_MASK;
277 
278 	/* First unlock the watchdog so that we can write to registers */
279     WDOG_Unlock();
280     WDOG->CS2    =  u8Cs2;
281     WDOG->TOVAL  =  u16TOVAL;
282     WDOG->WIN    =  u16WIN;
283     WDOG->CS1    =  u8Cs1;
284 }
285 
286 
287 /*****************************************************************************//*!
288 *
289 * @brief disable update of WDOG.
290 *
291 * @param  none
292 *
293 * @return none
294 *
295 * @ Pass/ Fail criteria: none
296 * @warning  this must be the last step of writing control bits sequence.
297 *****************************************************************************/
298 
WDOG_DisableUpdate(void)299 void WDOG_DisableUpdate(void)
300 {
301     uint8_t u8Cs1 =  WDOG->CS1;
302     uint8_t u8Cs2 =  WDOG->CS2;
303     uint16_t u16TOVAL =  WDOG->TOVAL;
304     uint16_t u16WIN =  WDOG->WIN;
305 
306     u8Cs1 &= ~WDOG_CS1_UPDATE_MASK;
307 
308 	/* First unlock the watchdog so that we can write to registers */
309     WDOG_Unlock();
310     WDOG->CS2    =  u8Cs2;
311     WDOG->TOVAL  =  u16TOVAL;
312     WDOG->WIN    =  u16WIN;
313     WDOG->CS1    =  u8Cs1;
314 
315 }
316 
317 
318 /********************************************************************/
319 
320 /*! @} End of wdog_api_list                                                    					*/
321 
322 
323 
324 
325