1 /**
2  * @file    wdt.h
3  * @brief   Watchdog timer (WDT) function prototypes and data types.
4  */
5 
6 /******************************************************************************
7  *
8  * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
9  * Analog Devices, Inc.),
10  * Copyright (C) 2023-2024 Analog Devices, Inc.
11  *
12  * Licensed under the Apache License, Version 2.0 (the "License");
13  * you may not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  *     http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  *
24  ******************************************************************************/
25 
26 /* Define to prevent redundant inclusion */
27 #ifndef LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX78002_WDT_H_
28 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX78002_WDT_H_
29 
30 /* **** Includes **** */
31 #include <stdint.h>
32 #include "mxc_device.h"
33 #include "wdt_regs.h"
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 /**
40  * @defgroup wdt WDT
41  * @ingroup periphlibs
42  * @{
43  */
44 
45 /* **** Definitions **** */
46 
47 /** @brief Watchdog upper limit period enumeration.
48     Used to configure the period of the watchdog interrupt */
49 typedef enum {
50     MXC_WDT_PERIOD_2_31 = MXC_S_WDT_CTRL_INT_LATE_VAL_WDT2POW31, ///< Period 2^31
51     MXC_WDT_PERIOD_2_30 = MXC_S_WDT_CTRL_INT_LATE_VAL_WDT2POW30, ///< Period 2^30
52     MXC_WDT_PERIOD_2_29 = MXC_S_WDT_CTRL_INT_LATE_VAL_WDT2POW29, ///< Period 2^29
53     MXC_WDT_PERIOD_2_28 = MXC_S_WDT_CTRL_INT_LATE_VAL_WDT2POW28, ///< Period 2^28
54     MXC_WDT_PERIOD_2_27 = MXC_S_WDT_CTRL_INT_LATE_VAL_WDT2POW27, ///< Period 2^27
55     MXC_WDT_PERIOD_2_26 = MXC_S_WDT_CTRL_INT_LATE_VAL_WDT2POW26, ///< Period 2^26
56     MXC_WDT_PERIOD_2_25 = MXC_S_WDT_CTRL_INT_LATE_VAL_WDT2POW25, ///< Period 2^25
57     MXC_WDT_PERIOD_2_24 = MXC_S_WDT_CTRL_INT_LATE_VAL_WDT2POW24, ///< Period 2^24
58     MXC_WDT_PERIOD_2_23 = MXC_S_WDT_CTRL_INT_LATE_VAL_WDT2POW23, ///< Period 2^23
59     MXC_WDT_PERIOD_2_22 = MXC_S_WDT_CTRL_INT_LATE_VAL_WDT2POW22, ///< Period 2^22
60     MXC_WDT_PERIOD_2_21 = MXC_S_WDT_CTRL_INT_LATE_VAL_WDT2POW21, ///< Period 2^21
61     MXC_WDT_PERIOD_2_20 = MXC_S_WDT_CTRL_INT_LATE_VAL_WDT2POW20, ///< Period 2^20
62     MXC_WDT_PERIOD_2_19 = MXC_S_WDT_CTRL_INT_LATE_VAL_WDT2POW19, ///< Period 2^19
63     MXC_WDT_PERIOD_2_18 = MXC_S_WDT_CTRL_INT_LATE_VAL_WDT2POW18, ///< Period 2^18
64     MXC_WDT_PERIOD_2_17 = MXC_S_WDT_CTRL_INT_LATE_VAL_WDT2POW17, ///< Period 2^17
65     MXC_WDT_PERIOD_2_16 = MXC_S_WDT_CTRL_INT_LATE_VAL_WDT2POW16, ///< Period 2^16
66 } mxc_wdt_period_t;
67 
68 /**
69  * @brief Watchdog interrupt flag enumeration
70  */
71 typedef enum {
72     MXC_WDT_INT_TOO_LATE = MXC_F_WDT_CTRL_INT_LATE,
73     MXC_WDT_INT_TOO_SOON = MXC_F_WDT_CTRL_INT_EARLY,
74 } mxc_wdt_int_t;
75 
76 /**
77  * @brief Watchdog reset flag enumeration
78  */
79 typedef enum {
80     MXC_WDT_RST_TOO_LATE = MXC_F_WDT_CTRL_RST_LATE,
81     MXC_WDT_RST_TOO_SOON = MXC_F_WDT_CTRL_RST_EARLY,
82 } mxc_wdt_rst_t;
83 
84 /**
85  * @brief Watchdog mode enumeration
86  */
87 typedef enum {
88     MXC_WDT_COMPATIBILITY = 0,
89     MXC_WDT_WINDOWED = 1,
90 } mxc_wdt_mode_t;
91 
92 /**
93  * @brief Peripheral Clock settings
94  */
95 typedef enum {
96     MXC_WDT_PCLK = 0,
97     MXC_WDT_IPO_CLK,
98     MXC_WDT_IBRO_CLK,
99     MXC_WDT_INRO_CLK,
100     MXC_WDT_ERTCO_CLK,
101     MXC_WDT_EXT_CLK,
102     MXC_WDT_ERFO_CLK
103 } mxc_wdt_clock_t;
104 
105 /**
106  * @brief Timer Configuration
107  */
108 typedef struct {
109     mxc_wdt_mode_t mode; ///< WDT mode
110     mxc_wdt_period_t upperResetPeriod; ///< Reset upper limit
111     mxc_wdt_period_t lowerResetPeriod; ///< Reset lower limit
112     mxc_wdt_period_t upperIntPeriod; ///< Interrupt upper limit
113     mxc_wdt_period_t lowerIntPeriod; ///< Interrupt lower limit
114 } mxc_wdt_cfg_t;
115 /* **** Function Prototypes **** */
116 
117 /**
118  * @brief Initialize the Watchdog Timer
119  * @note  On default this function enables WDT peripheral clock.
120  *        if you wish to manage clock and gpio related things in upper level instead of here.
121  *        Define MSDK_NO_GPIO_CLK_INIT flag in project.mk file.
122  *        By this flag this function will remove clock and gpio related codes from file.
123  *
124  * @param       wdt     Pointer to the watchdog registers
125  * @param       cfg     watchdog configuration
126  * @return      See \ref MXC_Error_Codes for the list of error codes.
127  */
128 int MXC_WDT_Init(mxc_wdt_regs_t *wdt, mxc_wdt_cfg_t *cfg);
129 
130 /**
131  * @brief Shutdown the Watchdog Timer
132  * @param       wdt     Pointer to the watchdog registers
133  * @return      See \ref MXC_Error_Codes for the list of error codes.
134  */
135 int MXC_WDT_Shutdown(mxc_wdt_regs_t *wdt);
136 
137 /**
138  * @brief       Set the period of the watchdog interrupt.
139  * @param       wdt     Pointer to watchdog registers.
140  * @param       cfg     watchdog configuration.
141  */
142 void MXC_WDT_SetIntPeriod(mxc_wdt_regs_t *wdt, mxc_wdt_cfg_t *cfg);
143 
144 /**
145  * @brief       Set the period of the watchdog reset.
146  * @param       wdt     Pointer to watchdog registers.
147  * @param       cfg     watchdog configuration.
148  */
149 void MXC_WDT_SetResetPeriod(mxc_wdt_regs_t *wdt, mxc_wdt_cfg_t *cfg);
150 
151 /**
152  * @brief       Enable the watchdog timer.
153  * @param       wdt     Pointer to watchdog registers.
154  */
155 void MXC_WDT_Enable(mxc_wdt_regs_t *wdt);
156 
157 /**
158  * @brief       Disable the watchdog timer.
159  * @param       wdt     Pointer to watchdog registers.
160  */
161 void MXC_WDT_Disable(mxc_wdt_regs_t *wdt);
162 
163 /**
164  * @brief       Enable the watchdog interrupt.
165  * @param       wdt     Pointer to watchdog registers.
166  */
167 void MXC_WDT_EnableInt(mxc_wdt_regs_t *wdt);
168 
169 /**
170  * @brief       Disable the watchdog interrupt.
171  * @param       wdt     Pointer to watchdog registers.
172  */
173 void MXC_WDT_DisableInt(mxc_wdt_regs_t *wdt);
174 
175 /**
176  * @brief       Enable the watchdog reset.
177  * @param       wdt     Pointer to watchdog registers.
178  */
179 void MXC_WDT_EnableReset(mxc_wdt_regs_t *wdt);
180 
181 /**
182  * @brief       Disable the watchdog reset.
183  * @param       wdt     Pointer to watchdog registers.
184  */
185 void MXC_WDT_DisableReset(mxc_wdt_regs_t *wdt);
186 
187 /**
188  * @brief       Reset the watchdog timer.
189  * @param       wdt     Pointer to watchdog registers.
190  */
191 void MXC_WDT_ResetTimer(mxc_wdt_regs_t *wdt);
192 
193 /**
194  * @brief       Get the status of the reset flag.
195  * @param       wdt     Pointer to watchdog registers.
196  * @returns     1 if the previous reset was caused by the watchdog, 0 otherwise.
197  */
198 int MXC_WDT_GetResetFlag(mxc_wdt_regs_t *wdt);
199 
200 /**
201  * @brief       Clears the reset flag.
202  * @param       wdt     Pointer to watchdog registers.
203  */
204 void MXC_WDT_ClearResetFlag(mxc_wdt_regs_t *wdt);
205 
206 /**
207  * @brief       Get the status of the interrupt flag.
208  * @param       wdt     Pointer to watchdog registers.
209  * @returns     1 if the interrupt is pending, 0 otherwise.
210  */
211 int MXC_WDT_GetIntFlag(mxc_wdt_regs_t *wdt);
212 
213 /**
214  * @brief       Clears the interrupt flag.
215  * @param       wdt     Pointer to watchdog registers.
216  */
217 void MXC_WDT_ClearIntFlag(mxc_wdt_regs_t *wdt);
218 
219 /**
220  * @brief       Sets clock source.
221  * @param       wdt             Pointer to watchdog registers.
222  * @param       clock_source    Clock source.
223  */
224 int MXC_WDT_SetClockSource(mxc_wdt_regs_t *wdt, mxc_wdt_clock_t clock_source);
225 
226 /**@} end of group wdt */
227 
228 #ifdef __cplusplus
229 }
230 #endif
231 
232 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX78002_WDT_H_
233