1 /**
2  * @file    wut.h
3  * @brief   Wakeup Timer (WUT) 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_WUT_H_
28 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX78002_WUT_H_
29 
30 /* **** Includes **** */
31 #include "mxc_device.h"
32 #include "wut_regs.h"
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 /**
39  * @defgroup wut Wakeup Timer (WUT)
40  * @ingroup periphlibs
41  * @{
42  */
43 
44 /**
45  * @brief Wakeup Timer prescaler values
46  */
47 typedef enum {
48     MXC_WUT_PRES_1 = MXC_S_WUT_CTRL_PRES_DIV1, /// Divide input clock by 1
49     MXC_WUT_PRES_2 = MXC_S_WUT_CTRL_PRES_DIV2, /// Divide input clock by 2
50     MXC_WUT_PRES_4 = MXC_S_WUT_CTRL_PRES_DIV4, /// Divide input clock by 4
51     MXC_WUT_PRES_8 = MXC_S_WUT_CTRL_PRES_DIV8, /// Divide input clock by 8
52     MXC_WUT_PRES_16 = MXC_S_WUT_CTRL_PRES_DIV16, /// Divide input clock by 16
53     MXC_WUT_PRES_32 = MXC_S_WUT_CTRL_PRES_DIV32, /// Divide input clock by 32
54     MXC_WUT_PRES_64 = MXC_S_WUT_CTRL_PRES_DIV64, /// Divide input clock by 64
55     MXC_WUT_PRES_128 = MXC_S_WUT_CTRL_PRES_DIV128, /// Divide input clock by 128
56     MXC_WUT_PRES_256 = MXC_F_WUT_CTRL_PRES3 |
57                        MXC_S_WUT_CTRL_PRES_DIV1, /// Divide input clock by 256
58     MXC_WUT_PRES_512 = MXC_F_WUT_CTRL_PRES3 |
59                        MXC_S_WUT_CTRL_PRES_DIV2, /// Divide input clock by 512
60     MXC_WUT_PRES_1024 = MXC_F_WUT_CTRL_PRES3 |
61                         MXC_S_WUT_CTRL_PRES_DIV4, /// Divide input clock by 1024
62     MXC_WUT_PRES_2048 = MXC_F_WUT_CTRL_PRES3 |
63                         MXC_S_WUT_CTRL_PRES_DIV8, /// Divide input clock by 2048
64     MXC_WUT_PRES_4096 = MXC_F_WUT_CTRL_PRES3 |
65                         MXC_S_WUT_CTRL_PRES_DIV16 /// Divide input clock by 4096
66 } mxc_wut_pres_t;
67 
68 /**
69  * @brief Wakeup Timer modes
70  */
71 typedef enum {
72     MXC_WUT_MODE_ONESHOT = MXC_V_WUT_CTRL_TMODE_ONESHOT, /// Wakeup Timer Mode ONESHOT
73     MXC_WUT_MODE_CONTINUOUS = MXC_V_WUT_CTRL_TMODE_CONTINUOUS, /// Wakeup Timer Mode CONTINUOUS
74     MXC_WUT_MODE_COUNTER = MXC_V_WUT_CTRL_TMODE_COUNTER, /// Wakeup Timer Mode COUNTER
75     MXC_WUT_MODE_PWM = MXC_V_WUT_CTRL_TMODE_PWM, /// Wakeup Timer Mode PWM
76     MXC_WUT_MODE_CAPTURE = MXC_V_WUT_CTRL_TMODE_CAPTURE, /// Wakeup Timer Mode CAPTURE
77     MXC_WUT_MODE_COMPARE = MXC_V_WUT_CTRL_TMODE_COMPARE, /// Wakeup Timer Mode COMPARE
78     MXC_WUT_MODE_GATED = MXC_V_WUT_CTRL_TMODE_GATED, /// Wakeup Timer Mode GATED
79     MXC_WUT_MODE_CAPTURE_COMPARE =
80         MXC_V_WUT_CTRL_TMODE_CAPTURECOMPARE /// Wakeup Timer Mode CAPTURECOMPARE
81 } mxc_wut_mode_t;
82 
83 /**
84  * @brief Wakeup Timer units of time enumeration
85  */
86 typedef enum {
87     MXC_WUT_UNIT_NANOSEC = 0, /**< Nanosecond Unit Indicator. */
88     MXC_WUT_UNIT_MICROSEC, /**< Microsecond Unit Indicator. */
89     MXC_WUT_UNIT_MILLISEC, /**< Millisecond Unit Indicator. */
90     MXC_WUT_UNIT_SEC /**< Second Unit Indicator. */
91 } mxc_wut_unit_t;
92 
93 /**
94  * @brief Wakeup Timer Configuration
95  */
96 typedef struct {
97     mxc_wut_mode_t mode; /// Desired timer mode
98     uint32_t cmp_cnt; /// Compare register value in timer ticks
99 } mxc_wut_cfg_t;
100 
101 /* **** Definitions **** */
102 
103 /* **** Function Prototypes **** */
104 
105 /**
106  * @brief      Initialize timer module clock.
107  * @param      pres       Prescaler value.
108  */
109 void MXC_WUT_Init(mxc_wut_pres_t pres);
110 
111 /**
112  * @brief      Shutdown timer module clock.
113  */
114 void MXC_WUT_Shutdown(void);
115 
116 /**
117  * @brief      Enable the timer.
118  */
119 void MXC_WUT_Enable(void);
120 
121 /**
122  * @brief      Disable the timer.
123  */
124 void MXC_WUT_Disable(void);
125 
126 /**
127  * @brief      Configure the timer.
128  * @param      cfg  Pointer to timer configuration struct.
129  */
130 void MXC_WUT_Config(const mxc_wut_cfg_t *cfg);
131 
132 /**
133  * @brief   Get the timer compare count.
134  * @return  Returns the current compare count.
135  */
136 uint32_t MXC_WUT_GetCompare(void);
137 
138 /**
139  * @brief   Get the timer capture count.
140  * @return  Returns the most recent capture count.
141  */
142 uint32_t MXC_WUT_GetCapture(void);
143 
144 /**
145  * @brief   Get the timer count.
146  * @return  Returns the current count.
147  */
148 uint32_t MXC_WUT_GetCount(void);
149 
150 /**
151  * @brief   Clear the timer interrupt.
152  */
153 void MXC_WUT_IntClear(void);
154 
155 /**
156  * @brief   Get the timer interrupt status.
157  * @return  Returns the interrupt status. 1 if interrupt has occurred.
158  */
159 uint32_t MXC_WUT_IntStatus(void);
160 
161 /**
162  * @brief   Set the timer compare count.
163  * @param   cmp_cnt New compare count.
164  * @note    This function does not protect against output glitches in PWM mode.
165  *          Use MXC_WUT_PWMSetPeriod when in PWM mode.
166  */
167 void MXC_WUT_SetCompare(uint32_t cmp_cnt);
168 
169 /**
170  * @brief   Set the timer count.
171  * @param   cnt     New count.
172  */
173 void MXC_WUT_SetCount(uint32_t cnt);
174 
175 /**
176  * @brief   Convert real time to timer ticks.
177  * @param   time    Number of units of time.
178  * @param   units   Which units of time you want to convert.
179  * @param   ticks   Pointer to store the number of ticks calculated.
180  * @return     #E_NO_ERROR If everything is successful.
181  * @return     @ref MXC_Error_Codes If function is unsuccessful.
182  */
183 int MXC_WUT_GetTicks(uint32_t time, mxc_wut_unit_t units, uint32_t *ticks);
184 
185 /**
186  * @brief   Convert timer ticks to real time.
187  * @param   ticks   Number of ticks.
188  * @param   time    Pointer to store number of units of time.
189  * @param   units   Pointer to store the units that time represents.
190  * @return     #E_NO_ERROR If everything is successful.
191  * @return     @ref MXC_Error_Codes If function is unsuccessful.
192  */
193 int MXC_WUT_GetTime(uint32_t ticks, uint32_t *time, mxc_wut_unit_t *units);
194 
195 /**
196  * @brief   Wait for an edge of the WUT count register.
197  */
198 void MXC_WUT_Edge(void);
199 
200 /**
201  * @brief   Store the count and snapshot values.
202  */
203 void MXC_WUT_Store(void);
204 
205 /**
206  * @brief   Restore the DBB clock with the stored count and snapshot values.
207  * @param   dbbFreq  Frequency of DBB clock.
208  */
209 void MXC_WUT_RestoreBBClock(uint32_t dbbFreq);
210 
211 /**
212  * @brief   Get the difference between the stored counter value
213  *          and the current counter value.
214  * @return  Returns the current counter value - stored counter value.
215  */
216 uint32_t MXC_WUT_GetSleepTicks(void);
217 
218 /**
219  * @brief   Delays for the given number of milliseconds.
220  * @param   waitMs  Number of milliseconds to wait.
221  */
222 void MXC_WUT_Delay_MS(uint32_t waitMs);
223 
224 /**@} end of group wut */
225 
226 #ifdef __cplusplus
227 }
228 #endif
229 
230 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX78002_WUT_H_
231