summaryrefslogtreecommitdiff
path: root/drivers/fsl_wdog.h
blob: 580adb95a0f7e9913ff05892c675262dd3082082 (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
/*
 * Copyright (c) 2015, Freescale Semiconductor, Inc.
 * Copyright 2016-2017 NXP
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * o Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * o Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * o Neither the name of the copyright holder nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#ifndef _FSL_WDOG_H_
#define _FSL_WDOG_H_

#include "fsl_common.h"

/*!
 * @addtogroup wdog
 * @{
 */


/*******************************************************************************
 * Definitions
 *******************************************************************************/

/*! @name Driver version */
/*@{*/
/*! @brief Defines WDOG driver version 2.0.0. */
#define FSL_WDOG_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
/*@}*/

/*! @name Unlock sequence */
/*@{*/
#define WDOG_FIRST_WORD_OF_UNLOCK (0xC520U)  /*!< First word of unlock sequence */
#define WDOG_SECOND_WORD_OF_UNLOCK (0xD928U) /*!< Second word of unlock sequence */
/*@}*/

/*! @name Refresh sequence */
/*@{*/
#define WDOG_FIRST_WORD_OF_REFRESH (0xA602U)  /*!< First word of refresh sequence */
#define WDOG_SECOND_WORD_OF_REFRESH (0xB480U) /*!< Second word of refresh sequence */
/*@}*/

/*! @brief Describes WDOG clock source. */
typedef enum _wdog_clock_source
{
    kWDOG_LpoClockSource = 0U,       /*!< WDOG clock sourced from LPO*/
    kWDOG_AlternateClockSource = 1U, /*!< WDOG clock sourced from alternate clock source*/
} wdog_clock_source_t;

/*! @brief Defines WDOG work mode. */
typedef struct _wdog_work_mode
{
#if defined(FSL_FEATURE_WDOG_HAS_WAITEN) && FSL_FEATURE_WDOG_HAS_WAITEN
    bool enableWait;  /*!< Enables or disables WDOG in wait mode  */
#endif                /* FSL_FEATURE_WDOG_HAS_WAITEN */
    bool enableStop;  /*!< Enables or disables WDOG in stop mode  */
    bool enableDebug; /*!< Enables or disables WDOG in debug mode */
} wdog_work_mode_t;

/*! @brief Describes the selection of the clock prescaler. */
typedef enum _wdog_clock_prescaler
{
    kWDOG_ClockPrescalerDivide1 = 0x0U, /*!< Divided by 1 */
    kWDOG_ClockPrescalerDivide2 = 0x1U, /*!< Divided by 2 */
    kWDOG_ClockPrescalerDivide3 = 0x2U, /*!< Divided by 3 */
    kWDOG_ClockPrescalerDivide4 = 0x3U, /*!< Divided by 4 */
    kWDOG_ClockPrescalerDivide5 = 0x4U, /*!< Divided by 5 */
    kWDOG_ClockPrescalerDivide6 = 0x5U, /*!< Divided by 6 */
    kWDOG_ClockPrescalerDivide7 = 0x6U, /*!< Divided by 7 */
    kWDOG_ClockPrescalerDivide8 = 0x7U, /*!< Divided by 8 */
} wdog_clock_prescaler_t;

/*! @brief Describes WDOG configuration structure. */
typedef struct _wdog_config
{
    bool enableWdog;                  /*!< Enables or disables WDOG */
    wdog_clock_source_t clockSource;  /*!< Clock source select */
    wdog_clock_prescaler_t prescaler; /*!< Clock prescaler value */
    wdog_work_mode_t workMode;        /*!< Configures WDOG work mode in debug stop and wait mode */
    bool enableUpdate;                /*!< Update write-once register enable */
    bool enableInterrupt;             /*!< Enables or disables WDOG interrupt */
    bool enableWindowMode;            /*!< Enables or disables WDOG window mode */
    uint32_t windowValue;             /*!< Window value */
    uint32_t timeoutValue;            /*!< Timeout value */
} wdog_config_t;

/*! @brief Describes WDOG test mode. */
typedef enum _wdog_test_mode
{
    kWDOG_QuickTest = 0U, /*!< Selects quick test */
    kWDOG_ByteTest = 1U,  /*!< Selects byte test */
} wdog_test_mode_t;

/*! @brief Describes WDOG tested byte selection in byte test mode. */
typedef enum _wdog_tested_byte
{
    kWDOG_TestByte0 = 0U, /*!< Byte 0 selected in byte test mode */
    kWDOG_TestByte1 = 1U, /*!< Byte 1 selected in byte test mode */
    kWDOG_TestByte2 = 2U, /*!< Byte 2 selected in byte test mode */
    kWDOG_TestByte3 = 3U, /*!< Byte 3 selected in byte test mode */
} wdog_tested_byte_t;

/*! @brief Describes WDOG test mode configuration structure. */
typedef struct _wdog_test_config
{
    wdog_test_mode_t testMode;     /*!< Selects test mode */
    wdog_tested_byte_t testedByte; /*!< Selects tested byte in byte test mode */
    uint32_t timeoutValue;         /*!< Timeout value */
} wdog_test_config_t;

/*!
 * @brief WDOG interrupt configuration structure, default settings all disabled.
 *
 * This structure contains the settings for all of the WDOG interrupt configurations.
 */
enum _wdog_interrupt_enable_t
{
    kWDOG_InterruptEnable = WDOG_STCTRLH_IRQRSTEN_MASK, /*!< WDOG timeout generates an interrupt before reset*/
};

/*!
 * @brief WDOG status flags.
 *
 * This structure contains the WDOG status flags for use in the WDOG functions.
 */
enum _wdog_status_flags_t
{
    kWDOG_RunningFlag = WDOG_STCTRLH_WDOGEN_MASK, /*!< Running flag, set when WDOG is enabled*/
    kWDOG_TimeoutFlag = WDOG_STCTRLL_INTFLG_MASK, /*!< Interrupt flag, set when an exception occurs*/
};

/*******************************************************************************
 * API
 *******************************************************************************/

#if defined(__cplusplus)
extern "C" {
#endif /* __cplusplus */

/*!
 * @name WDOG Initialization and De-initialization
 * @{
 */

/*!
 * @brief Initializes the WDOG configuration sturcture.
 *
 * This function initializes the WDOG configuration structure to default values. The default
 * values are as follows.
 * @code
 *   wdogConfig->enableWdog = true;
 *   wdogConfig->clockSource = kWDOG_LpoClockSource;
 *   wdogConfig->prescaler = kWDOG_ClockPrescalerDivide1;
 *   wdogConfig->workMode.enableWait = true;
 *   wdogConfig->workMode.enableStop = false;
 *   wdogConfig->workMode.enableDebug = false;
 *   wdogConfig->enableUpdate = true;
 *   wdogConfig->enableInterrupt = false;
 *   wdogConfig->enableWindowMode = false;
 *   wdogConfig->windowValue = 0;
 *   wdogConfig->timeoutValue = 0xFFFFU;
 * @endcode
 *
 * @param config Pointer to the WDOG configuration structure.
 * @see wdog_config_t
 */
void WDOG_GetDefaultConfig(wdog_config_t *config);

/*!
 * @brief Initializes the WDOG.
 *
 * This function initializes the WDOG. When called, the WDOG runs according to the configuration.
 * To reconfigure WDOG without forcing a reset first, enableUpdate must be set to true
 * in the configuration.
 *
 * This is an example.
 * @code
 *   wdog_config_t config;
 *   WDOG_GetDefaultConfig(&config);
 *   config.timeoutValue = 0x7ffU;
 *   config.enableUpdate = true;
 *   WDOG_Init(wdog_base,&config);
 * @endcode
 *
 * @param base   WDOG peripheral base address
 * @param config The configuration of WDOG
 */
void WDOG_Init(WDOG_Type *base, const wdog_config_t *config);

/*!
 * @brief Shuts down the WDOG.
 *
 * This function shuts down the WDOG.
 * Ensure that the WDOG_STCTRLH.ALLOWUPDATE is 1 which indicates that the register update is enabled.
 */
void WDOG_Deinit(WDOG_Type *base);

/*!
 * @brief Configures the WDOG functional test.
 *
 * This function is used to configure the WDOG functional test. When called, the WDOG goes into test mode
 * and runs according to the configuration.
 * Ensure that the WDOG_STCTRLH.ALLOWUPDATE is 1 which means that the register update is enabled.
 *
 * This is an example.
 * @code
 *   wdog_test_config_t test_config;
 *   test_config.testMode = kWDOG_QuickTest;
 *   test_config.timeoutValue = 0xfffffu;
 *   WDOG_SetTestModeConfig(wdog_base, &test_config);
 * @endcode
 * @param base   WDOG peripheral base address
 * @param config The functional test configuration of WDOG
 */
void WDOG_SetTestModeConfig(WDOG_Type *base, wdog_test_config_t *config);

/* @} */

/*!
 * @name WDOG Functional Operation
 * @{
 */

/*!
 * @brief Enables the WDOG module.
 *
 * This function write value into WDOG_STCTRLH register to enable the WDOG, it is a write-once register,
 * make sure that the WCT window is still open and this register has not been written in this WCT
 * while this function is called.
 *
 * @param base WDOG peripheral base address
 */
static inline void WDOG_Enable(WDOG_Type *base)
{
    base->STCTRLH |= WDOG_STCTRLH_WDOGEN_MASK;
}

/*!
 * @brief Disables the WDOG module.
 *
 * This function writes a value into the WDOG_STCTRLH register to disable the WDOG. It is a write-once register.
 * Ensure that the WCT window is still open and that register has not been written to in this WCT
 * while the function is called.
 *
 * @param base WDOG peripheral base address
 */
static inline void WDOG_Disable(WDOG_Type *base)
{
    base->STCTRLH &= ~WDOG_STCTRLH_WDOGEN_MASK;
}

/*!
 * @brief Enables the WDOG interrupt.
 *
 * This function writes a value into the WDOG_STCTRLH register to enable the WDOG interrupt. It is a write-once register.
 * Ensure that the WCT window is still open and the register has not been written to in this WCT
 * while the function is called.
 *
 * @param base WDOG peripheral base address
 * @param mask The interrupts to enable
 *        The parameter can be combination of the following source if defined.
 *        @arg kWDOG_InterruptEnable
 */
static inline void WDOG_EnableInterrupts(WDOG_Type *base, uint32_t mask)
{
    base->STCTRLH |= mask;
}

/*!
 * @brief Disables the WDOG interrupt.
 *
 * This function writes a value into the WDOG_STCTRLH register to disable the WDOG interrupt. It is a write-once register.
 * Ensure that the WCT window is still open and the register has not been written to in this WCT
 * while the function is called.
 *
 * @param base WDOG peripheral base address
 * @param mask The interrupts to disable
 *        The parameter can be combination of the following source if defined.
 *        @arg kWDOG_InterruptEnable
 */
static inline void WDOG_DisableInterrupts(WDOG_Type *base, uint32_t mask)
{
    base->STCTRLH &= ~mask;
}

/*!
 * @brief Gets the WDOG all status flags.
 *
 * This function gets all status flags.
 *
 * This is an example for getting the Running Flag.
 * @code
 *   uint32_t status;
 *   status = WDOG_GetStatusFlags (wdog_base) & kWDOG_RunningFlag;
 * @endcode
 * @param base        WDOG peripheral base address
 * @return            State of the status flag: asserted (true) or not-asserted (false).@see _wdog_status_flags_t
 *                    - true: a related status flag has been set.
 *                    - false: a related status flag is not set.
 */
uint32_t WDOG_GetStatusFlags(WDOG_Type *base);

/*!
 * @brief Clears the WDOG flag.
 *
 * This function clears the WDOG status flag.
 *
 * This is an example for clearing the timeout (interrupt) flag.
 * @code
 *   WDOG_ClearStatusFlags(wdog_base,kWDOG_TimeoutFlag);
 * @endcode
 * @param base        WDOG peripheral base address
 * @param mask        The status flags to clear.
 *                    The parameter could be any combination of the following values.
 *                    kWDOG_TimeoutFlag
 */
void WDOG_ClearStatusFlags(WDOG_Type *base, uint32_t mask);

/*!
 * @brief Sets the WDOG timeout value.
 *
 * This function sets the timeout value.
 * It should be ensured that the time-out value for the WDOG is always greater than
 * 2xWCT time + 20 bus clock cycles.
 * This function writes a value into WDOG_TOVALH and WDOG_TOVALL registers which are wirte-once.
 * Ensure the WCT window is still open and the two registers have not been written to in this WCT
 * while the function is called.
 *
 * @param base WDOG peripheral base address
 * @param timeoutCount WDOG timeout value; count of WDOG clock tick.
 */
static inline void WDOG_SetTimeoutValue(WDOG_Type *base, uint32_t timeoutCount)
{
    base->TOVALH = (uint16_t)((timeoutCount >> 16U) & 0xFFFFU);
    base->TOVALL = (uint16_t)((timeoutCount)&0xFFFFU);
}

/*!
 * @brief Sets the WDOG window value.
 *
 * This function sets the WDOG window value.
 * This function writes a value into WDOG_WINH and WDOG_WINL registers which are wirte-once.
 * Ensure the WCT window is still open and the two registers have not been written to in this WCT
 * while the function is called.
 *
 * @param base WDOG peripheral base address
 * @param windowValue WDOG window value.
 */
static inline void WDOG_SetWindowValue(WDOG_Type *base, uint32_t windowValue)
{
    base->WINH = (uint16_t)((windowValue >> 16U) & 0xFFFFU);
    base->WINL = (uint16_t)((windowValue)&0xFFFFU);
}

/*!
 * @brief Unlocks the WDOG register written.
 *
 * This function unlocks the WDOG register written.
 * Before starting the unlock sequence and following congfiguration, disable the global interrupts.
 * Otherwise, an interrupt may invalidate the unlocking sequence and the WCT may expire.
 * After the configuration finishes, re-enable the global interrupts.
 *
 * @param base WDOG peripheral base address
 */
static inline void WDOG_Unlock(WDOG_Type *base)
{
    base->UNLOCK = WDOG_FIRST_WORD_OF_UNLOCK;
    base->UNLOCK = WDOG_SECOND_WORD_OF_UNLOCK;
}

/*!
 * @brief Refreshes the WDOG timer.
 *
 * This function feeds the WDOG.
 * This function should be called before the WDOG timer is in timeout. Otherwise, a reset is asserted.
 *
 * @param base WDOG peripheral base address
 */
void WDOG_Refresh(WDOG_Type *base);

/*!
 * @brief Gets the WDOG reset count.
 *
 * This function gets the WDOG reset count value.
 *
 * @param base WDOG peripheral base address
 * @return     WDOG reset count value.
 */
static inline uint16_t WDOG_GetResetCount(WDOG_Type *base)
{
    return base->RSTCNT;
}
/*!
 * @brief Clears the WDOG reset count.
 *
 * This function clears the WDOG reset count value.
 *
 * @param base WDOG peripheral base address
 */
static inline void WDOG_ClearResetCount(WDOG_Type *base)
{
    base->RSTCNT |= UINT16_MAX;
}

/*@}*/

#if defined(__cplusplus)
}
#endif /* __cplusplus */

/*! @}*/

#endif /* _FSL_WDOG_H_ */