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