1 /*
2  * Copyright 2017-2019 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #include "fsl_wdog8.h"
9 
10 /* Component ID definition, used by tools. */
11 #ifndef FSL_COMPONENT_ID
12 #define FSL_COMPONENT_ID "platform.drivers.wdog8"
13 #endif
14 
15 /*******************************************************************************
16  * Code
17  ******************************************************************************/
18 
19 /*!
20  * brief Clears the WDOG8 flag.
21  *
22  * This function clears the WDOG8 status flag.
23  *
24  * Example to clear an interrupt flag:
25  * code
26  *   WDOG8_ClearStatusFlags(wdog_base,kWDOG8_InterruptFlag);
27  * endcode
28  * param base        WDOG8 peripheral base address.
29  * param mask        The status flags to clear.
30  *                    The parameter can be any combination of the following values:
31  *                    arg kWDOG8_InterruptFlag
32  */
WDOG8_ClearStatusFlags(WDOG_Type * base,uint8_t mask)33 void WDOG8_ClearStatusFlags(WDOG_Type *base, uint8_t mask)
34 {
35     if (0U != (mask & (uint8_t)kWDOG8_InterruptFlag))
36     {
37         base->CS2 |= WDOG_CS2_FLG_MASK;
38     }
39 }
40 
41 /*!
42  * brief Initializes the WDOG8 configuration structure.
43  *
44  * This function initializes the WDOG8 configuration structure to default values. The default
45  * values are:
46  * code
47  *   wdog8Config->enableWdog8 = true;
48  *   wdog8Config->clockSource = kWDOG8_ClockSource1;
49  *   wdog8Config->prescaler = kWDOG8_ClockPrescalerDivide1;
50  *   wdog8Config->workMode.enableWait = true;
51  *   wdog8Config->workMode.enableStop = false;
52  *   wdog8Config->workMode.enableDebug = false;
53  *   wdog8Config->testMode = kWDOG8_TestModeDisabled;
54  *   wdog8Config->enableUpdate = true;
55  *   wdog8Config->enableInterrupt = false;
56  *   wdog8Config->enableWindowMode = false;
57  *   wdog8Config->windowValue = 0U;
58  *   wdog8Config->timeoutValue = 0xFFFFU;
59  * endcode
60  *
61  * param config Pointer to the WDOG8 configuration structure.
62  * see wdog8_config_t
63  */
WDOG8_GetDefaultConfig(wdog8_config_t * config)64 void WDOG8_GetDefaultConfig(wdog8_config_t *config)
65 {
66     assert(NULL != config);
67 
68     /* Initializes the configure structure to zero. */
69     (void)memset(config, 0, sizeof(*config));
70 
71     config->enableWdog8          = true;
72     config->clockSource          = kWDOG8_ClockSource1;
73     config->prescaler            = kWDOG8_ClockPrescalerDivide1;
74     config->workMode.enableWait  = true;
75     config->workMode.enableStop  = false;
76     config->workMode.enableDebug = false;
77     config->testMode             = kWDOG8_TestModeDisabled;
78     config->enableUpdate         = true;
79     config->enableInterrupt      = false;
80     config->enableWindowMode     = false;
81     config->windowValue          = 0U;
82     config->timeoutValue         = 0xFFFFU;
83 }
84 
85 /*!
86  * brief Initializes the WDOG8 module.
87  *
88  * This function initializes the WDOG8.
89  * To reconfigure the WDOG8 without forcing a reset first, enableUpdate must be set to true
90  * in the configuration.
91  *
92  * Example:
93  * code
94  *   wdog8_config_t config;
95  *   WDOG8_GetDefaultConfig(&config);
96  *   config.timeoutValue = 0x7ffU;
97  *   config.enableUpdate = true;
98  *   WDOG8_Init(wdog_base,&config);
99  * endcode
100  *
101  * param base   WDOG8 peripheral base address.
102  * param config The configuration of the WDOG8.
103  */
WDOG8_Init(WDOG_Type * base,const wdog8_config_t * config)104 void WDOG8_Init(WDOG_Type *base, const wdog8_config_t *config)
105 {
106     assert(NULL != config);
107 
108     uint8_t value1        = 0U;
109     uint8_t value2        = 0U;
110     uint32_t primaskValue = 0U;
111 
112     value1 = WDOG_CS1_EN(config->enableWdog8) | WDOG_CS1_INT(config->enableInterrupt) |
113              WDOG_CS1_UPDATE(config->enableUpdate) | WDOG_CS1_DBG(config->workMode.enableDebug) |
114              WDOG_CS1_STOP(config->workMode.enableStop) | WDOG_CS1_WAIT(config->workMode.enableWait) |
115              WDOG_CS1_TST(config->testMode);
116     value2 =
117         WDOG_CS2_CLK(config->clockSource) | WDOG_CS2_WIN(config->enableWindowMode) | WDOG_CS2_PRES(config->prescaler);
118 
119     /* Disable the global interrupts. Otherwise, an interrupt could effectively invalidate the unlock sequence
120      * and the WCT may expire. After the configuration finishes, re-enable the global interrupts. */
121     primaskValue = DisableGlobalIRQ();
122     WDOG8_Unlock(base);
123     WDOG8_SetWindowValue(base, config->windowValue);
124     WDOG8_SetTimeoutValue(base, config->timeoutValue);
125     base->CS1 = value1;
126     base->CS2 = value2;
127     EnableGlobalIRQ(primaskValue);
128 }
129 
130 /*!
131  * brief De-initializes the WDOG8 module.
132  *
133  * This function shuts down the WDOG8.
134  * Ensure that the WDOG_CS1.UPDATE is 1, which means that the register update is enabled.
135  *
136  * param base   WDOG8 peripheral base address.
137  */
WDOG8_Deinit(WDOG_Type * base)138 void WDOG8_Deinit(WDOG_Type *base)
139 {
140     uint32_t primaskValue = 0U;
141 
142     /* Disable the global interrupts */
143     primaskValue = DisableGlobalIRQ();
144     WDOG8_Unlock(base);
145     WDOG8_Disable(base);
146     EnableGlobalIRQ(primaskValue);
147 }
148