summaryrefslogtreecommitdiff
path: root/bsp/lpc408x/Libraries/Drivers/source/lpc_wwdt.c
blob: b4ca9465fca4e2b77bb73643b06c1cd24854a240 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/**********************************************************************
* $Id$      lpc_wwdt.c          2011-06-02
*//**
* @file     lpc_wwdt.c
* @brief    Contains all functions support for Wachtdog Timer
*           firmware library on LPC
* @version  1.0
* @date     02. June. 2011
* @author   NXP MCU SW Application Team
* 
* Copyright(C) 2011, NXP Semiconductor
* All rights reserved.
*
***********************************************************************
* Software that is described herein is for illustrative purposes only
* which provides customers with programming information regarding the
* products. This software is supplied "AS IS" without any warranties.
* NXP Semiconductors assumes no responsibility or liability for the
* use of the software, conveys no license or title under any patent,
* copyright, or mask work right to the product. NXP Semiconductors
* reserves the right to make changes in the software without
* notification. NXP Semiconductors also make no representation or
* warranty that such application will be suitable for the specified
* use without further testing or modification.
* Permission to use, copy, modify, and distribute this software and its
* documentation is hereby granted, under NXP Semiconductors'
* relevant copyright in the software, without fee, provided that it
* is used in conjunction with NXP Semiconductors microcontrollers.  This
* copyright, permission, and disclaimer notice must appear in all copies of
* this code.
**********************************************************************/

/* Peripheral group ----------------------------------------------------------- */
/** @addtogroup WWDT
 * @{
 */
#ifdef __BUILD_WITH_EXAMPLE__
#include "lpc_libcfg.h"
#else
#include "lpc_libcfg_default.h"
#endif /* __BUILD_WITH_EXAMPLE__ */
#ifdef _WDT

/* Includes ------------------------------------------------------------------- */
#include "lpc_wwdt.h"
#include "lpc_clkpwr.h"
#include "lpc_pinsel.h"


 /* Public Functions ----------------------------------------------------------- */
 /** @addtogroup WDT_Public_Functions
  * @{
  */

 /********************************************************************//**
 * @brief       Set timeout value to Timer Constant register 
 *              (if enable) to generate a WatchDog event if match
 *
 * @param[in]   timeoutVal  The value (counter) will write directly to 
 *              Register w/o pre-calc
 *
 * @return      None
 *********************************************************************/
int8_t WWDT_SetTimeOutRaw(uint32_t timeoutVal)
{
    int8_t retval = 0;

    if(timeoutVal < WWDT_TIMEOUT_MIN)
    {
        timeoutVal = WWDT_TIMEOUT_MIN;
        retval = WWDT_FUNC_BAD_PARAM;
    }
    else if (timeoutVal > WWDT_TIMEOUT_MAX)
    {
        timeoutVal = WWDT_TIMEOUT_MAX;
        retval = WWDT_FUNC_BAD_PARAM;
    }

    LPC_WDT->TC = timeoutVal;

    return retval;
}


/********************************************************************//**
 * @brief       Set WDT timeout (cal by usec) to TC register
 *
 * @param[in]   timeout The time (usec) to generate watchdog event if 
 *              the watchdog counter reach this value
 *
 * @return      WWDT_FUNC_OK if success
 *********************************************************************/
int8_t WWDT_SetTimeOut(uint32_t timeout)
{
    return WWDT_SetTimeOutRaw(WDT_GET_FROM_USEC(timeout));
}


/*********************************************************************//**
* @brief        Initial for Watchdog function by setting timeout
*
* @param[in]    TimeOut time out value, should be in range:
*               2048 .. 134217728
*
* @return      WWDT_FUNC_OK if success
 **********************************************************************/
int8_t WWDT_Init(uint32_t TimeOut)
{
    return WWDT_SetTimeOut(TimeOut);
}

/*********************************************************************//**
 * @brief       Configure the WatchDog by initialization all the timing
 *              value for register (Warning value, Window value,...)
 *
 * @param       wdtCfg a the st_Wdt_Config type value
 *
 * @return      None
 **********************************************************************/
void WWDT_Configure(st_Wdt_Config wdtCfg)
{
    WWDT_SetTimeOut(wdtCfg.wdtTmrConst);

    if(wdtCfg.wdtEnable)
    {
        LPC_WDT->MOD |= WWDT_WDMOD_WDEN;
    }
    else
    {
        LPC_WDT->MOD &= ~WWDT_WDMOD_WDEN;
    }

    if(wdtCfg.wdtReset)
    {
        LPC_WDT->MOD |= WWDT_WDMOD_WDRESET;
    }
    else
    {
        LPC_WDT->MOD &= ~WWDT_WDMOD_WDRESET;
    }

    if(wdtCfg.wdtProtect)
    {
        LPC_WDT->MOD |= WWDT_WDMOD_WDPROTECT;
    }
    else
    {
        LPC_WDT->MOD &= ~WWDT_WDMOD_WDPROTECT;
    }

    WWDT_SetWarning(wdtCfg.wdtWarningVal);

    WWDT_SetWindow(wdtCfg.wdtWindowVal);
}

/*********************************************************************//**
 * @brief       Start WatchDog with specific Timeout
 *
 * @param       TimeOut specific Timeout for WatchDog event
 *
 * @return      WWDT_FUNC_OK if success
 **********************************************************************/
int8_t WWDT_Start(uint32_t TimeOut)
{
    int8_t retval = WWDT_FUNC_OK;

    retval = WWDT_SetTimeOut(TimeOut);

    WWDT_Cmd(ENABLE);

    return retval;
}


/********************************************************************//**
 * @brief       Update WDT timeout value and feed
 *
 * @param[in]   WarnTime    time to generate watchdog warning interrupt(us)
 *              should be in range: 2048 .. 134217728
 *
 * @return      None
 *********************************************************************/
void WWDT_SetTimerConstant(uint32_t constVal)
{
    LPC_WDT->TC = constVal;
}


/*********************************************************************//**
 * @brief       Enable/Disable WDT function
 *
 * @param[in]   Mode WWDT mode that will be enabled/disabled, should be:
 *              - WWDT_PROTECT_MODE : protect mode
 *              - WWDT_RESET_MODE   : reset mode
 *
 * @param[in]   NewState new state of protection function, should be:
 *              - ENABLE: The watchdog reload value can be changed at any time
 *              - DISABLE: The watchdog reload value can be changed only after
 *                         the counter is below the value of WDWARNINT and WDWINDOW
 *
 * @return      None
 **********************************************************************/
void WWDT_SetMode(uint8_t mode, FunctionalState NewState)
{
    if (mode == WWDT_PROTECT_MODE )
    {
        if(NewState == ENABLE)
            LPC_WDT->MOD |= WWDT_WDMOD_WDPROTECT;
        else
            LPC_WDT->MOD &= ~WWDT_WDMOD_WDPROTECT;
    }
    else if(mode == WWDT_RESET_MODE)
    {
        if(NewState == ENABLE)
            LPC_WDT->MOD |= WWDT_WDMOD_WDRESET;
        else
            LPC_WDT->MOD &= ~WWDT_WDMOD_WDRESET;
    }
}

/*********************************************************************//**
 * @brief       Enable/Disable WWDT activity
 *
 * @param[in]   NewState To enable/disable the WatchDog
 *
 * @return      None
 **********************************************************************/
void WWDT_Enable(FunctionalState NewState)
{
    if(NewState == ENABLE)
    {
        LPC_WDT->MOD |= WWDT_WDMOD_WDEN;
    }
    else
    {
        LPC_WDT->MOD &= ~WWDT_WDMOD_WDEN;
    }
}

/*********************************************************************//**
 * @brief       Enable/Disable WWDT activity. In case of Enable, it will
 *              do feeding WatchDog for its normal operation.
 *
 * @param[in]   NewState To enable/disable the WatchDog
 *
 * @return      None
 **********************************************************************/
void WWDT_Cmd(FunctionalState NewState)
{
    if(NewState == ENABLE)
    {
        LPC_WDT->MOD |= WWDT_WDMOD_WDEN;

        //Load the Feed register to start using WDT
        WWDT_Feed();
    }
    else
    {
        LPC_WDT->MOD &= ~WWDT_WDMOD_WDEN;
    }
}

/********************************************************************//**
 * @brief       Set the warning value to register to generate interrupt
 *              (if enable) if the watchdog timer matches this value.
 *
 * @param[in]   warnVal The value (counter) will write directly to Warning 
 *              Register w/o pre-calc
 *
 * @return      WWDT_FUNC_OK if success
 *********************************************************************/
int8_t WWDT_SetWarningRaw(uint32_t warnVal)
{
    int8_t retval = WWDT_FUNC_OK;

    if(warnVal < WWDT_WARNINT_MIN)
    {
        warnVal = WWDT_WARNINT_MIN;
        retval = WWDT_FUNC_BAD_PARAM;
    }
    else if (warnVal > WWDT_WARNINT_MAX)
    {
        warnVal = WWDT_WARNINT_MAX;
        retval = WWDT_FUNC_BAD_PARAM;
    }

    LPC_WDT->WARNINT = warnVal;

    return retval;
}

/********************************************************************//**
 * @brief       Update WDT warning time (cal by usec) to warning register
 *
 * @param[in]   WarnTime The time (usec) to generate watchdog warning
 *              interrupt should be in range: 2048 .. 8192
 *
 * @return      WWDT_FUNC_OK if success
 *********************************************************************/
int8_t WWDT_SetWarning(uint32_t WarnTime)
{
    return WWDT_SetWarningRaw(WDT_GET_FROM_USEC(WarnTime));
}


/********************************************************************//**
 * @brief       Update WDT Windows value (by counter) to Window Register
 *              of WatchDog
 *
 * @param[in]   wndVal The value (counter) will write directly to Window
 *              Register w/o pre-calc
 *
 * @return      WWDT_FUNC_OK if success
 *********************************************************************/
int8_t WWDT_SetWindowRaw(uint32_t wndVal)
{
    int8_t retval = WWDT_FUNC_OK;

    if(wndVal < WWDT_WINDOW_MIN)
    {
        wndVal = WWDT_WINDOW_MIN;
        retval = WWDT_FUNC_BAD_PARAM;
    }
    else if (wndVal > WWDT_WINDOW_MAX)
    {
        wndVal = WWDT_WINDOW_MAX;
        retval = WWDT_FUNC_BAD_PARAM;
    }

    LPC_WDT->WINDOW = wndVal;

    return retval;
}


/********************************************************************//**
 * @brief       Update WDT Windows value (by usec) to Window Register
 *              of WatchDog
 *
 * @param[in]   WindowedTime Expected time (usec) to set watchdog window event
 *
 * @return      WWDT_FUNC_OK if success
 *********************************************************************/
int8_t WWDT_SetWindow(uint32_t WindowedTime)
{
    return WWDT_SetWindowRaw(WDT_GET_FROM_USEC(WindowedTime));
}


/********************************************************************//**
 * @brief       Update WDT timeout value for WatchDog event(s)
 *
 * @param[in]   TimeOut Time Out value (usec) to be updated, should be
 *              in range: 2048 .. 134217728
 *
 * @return      None
 *********************************************************************/
void WDT_UpdateTimeOut(uint32_t TimeOut)
{
    /* check WDPROTECT,
     * if it is enable, wait until the counter is below the value of
     * WDWARNINT and WDWINDOW
     */
    if(LPC_WDT->MOD & (1<<4))
    {
        while((LPC_WDT->TV <(LPC_WDT->WARNINT & WWDT_WDWARNINT_MASK))\
                        &&(LPC_WDT->TV <(LPC_WDT->WINDOW & WWDT_WDTC_MASK)));
    }

    WWDT_SetTimeOut(TimeOut);
}


/********************************************************************//**
 * @brief       Read WWDT status flag
 *
 * @param[in]   Status kind of status flag that you want to get, should be:
 *              - WWDT_WARNINT_FLAG: watchdog interrupt flag
 *              - WWDT_TIMEOUT_FLAG: watchdog time-out flag
 *
 * @return      Time out flag status of WDT
 *********************************************************************/
FlagStatus WWDT_GetStatus (uint8_t Status)
{
    if(Status == WWDT_WARNINT_FLAG)
    {
        return ((FlagStatus)(LPC_WDT->MOD & (1<<3)));
    }
    else if (Status == WWDT_TIMEOUT_FLAG)
    {
        return ((FlagStatus)(LPC_WDT->MOD & (1<<2)));
    }
    return RESET;
}

/********************************************************************//**
 * @brief       Clear WWDT status flag
 *
 * @param[in]   Status kind of status flag that you want to get, should be:
 *              - WWDT_WARNINT_FLAG: watchdog interrupt flag
 *              - WWDT_TIMEOUT_FLAG: watchdog time-out flag
 *
 * @return      None
 *********************************************************************/
void WWDT_ClearStatusFlag (uint8_t flag)
{
    if(flag == WWDT_WARNINT_FLAG)
    {
        // Write 1 to this bit to clear itself
        LPC_WDT->MOD |= WWDT_WDMOD_WDINT;
    }
    else if(flag == WWDT_TIMEOUT_FLAG)
    {
        // Write 0 to this bit to clear itself
        LPC_WDT->MOD &= ~ WWDT_WDMOD_WDTOF;
    }
}

/********************************************************************//**
 * @brief       Clear WDT Time out flag
 * @param       None
 * @return      None
 *********************************************************************/
void WWDT_ClrTimeOutFlag (void)
{
    LPC_WDT->MOD &= ~ WWDT_WDMOD_WDTOF;
}


/********************************************************************//**
 * @brief       Following the standard sequence to Feed the WatchDog Timer
 *
 * @param       None
 *
 * @return      None
 *********************************************************************/
void WWDT_FeedStdSeq (void)
{
    LPC_WDT->FEED = 0xAA;

    LPC_WDT->FEED = 0x55;
}

/********************************************************************//**
 * @brief       After set WDTEN, call this function to start Watchdog
 *              or reload the Watchdog timer
 *
 * @param       None
 *
 * @return      None
 *********************************************************************/
void WWDT_Feed (void)
{
    // Disable irq interrupt
    __disable_irq();

    WWDT_FeedStdSeq();

    // Then enable irq interrupt
    __enable_irq();
}

/********************************************************************//**
 * @brief       Get the current value of WDT
 *
 * @param       None
 *
 * @return      Current value of WDT
 *********************************************************************/
uint32_t WWDT_GetCurrentCount(void)
{
    return LPC_WDT->TV;
}

/**
 * @}
 */

#endif /*_WDT*/
/**
 * @}
 */

/* --------------------------------- End Of File ------------------------------ */