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_MAX32665_WUT_H_
28 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32665_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_DIV4, /// Divide input clock by 512
60     MXC_WUT_PRES_1024 = MXC_F_WUT_CTRL_PRES3 |
61                         MXC_S_WUT_CTRL_PRES_DIV8, /// Divide input clock by 1024
62     MXC_WUT_PRES_2048 = MXC_F_WUT_CTRL_PRES3 |
63                         MXC_S_WUT_CTRL_PRES_DIV16, /// Divide input clock by 2048
64     MXC_WUT_PRES_4096 = MXC_F_WUT_CTRL_PRES3 |
65                         MXC_S_WUT_CTRL_PRES_DIV32 /// 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 /**
102  * @brief   The callback routine used by the MXC_WUT_TrimCrystalAsync()
103  *          function to indicate the transaction has completed.
104  *
105  * @param   result      Error code.
106  */
107 typedef void (*mxc_wut_complete_cb_t)(int result);
108 
109 /* **** Definitions **** */
110 
111 /* **** Function Prototypes **** */
112 
113 /**
114  * @brief      Initialize timer module clock.
115  * @param      pres       Prescaler value.
116  */
117 void MXC_WUT_Init(mxc_wut_pres_t pres);
118 
119 /**
120  * @brief      Shutdown timer module clock.
121  */
122 void MXC_WUT_Shutdown(void);
123 
124 /**
125  * @brief      Enable the timer.
126  */
127 void MXC_WUT_Enable(void);
128 
129 /**
130  * @brief      Disable the timer.
131  */
132 void MXC_WUT_Disable(void);
133 
134 /**
135  * @brief      Configure the timer.
136  * @param      cfg  Pointer to timer configuration struct.
137  */
138 void MXC_WUT_Config(const mxc_wut_cfg_t *cfg);
139 
140 /**
141  * @brief   Get the timer compare count.
142  * @return  Returns the current compare count.
143  */
144 uint32_t MXC_WUT_GetCompare(void);
145 
146 /**
147  * @brief   Get the timer capture count.
148  * @return  Returns the most recent capture count.
149  */
150 uint32_t MXC_WUT_GetCapture(void);
151 
152 /**
153  * @brief   Get the timer count.
154  * @return  Returns the current count.
155  */
156 uint32_t MXC_WUT_GetCount(void);
157 
158 /**
159  * @brief   Clear the timer interrupt.
160  */
161 void MXC_WUT_IntClear(void);
162 
163 /**
164  * @brief   Get the timer interrupt status.
165  * @return  Returns the interrupt status. 1 if interrupt has occurred.
166  */
167 uint32_t MXC_WUT_IntStatus(void);
168 
169 /**
170  * @brief   Set the timer compare count.
171  * @param   cmp_cnt New compare count.
172  * @note    This function does not protect against output glitches in PWM mode.
173  *          Use MXC_WUT_PWMSetPeriod when in PWM mode.
174  */
175 void MXC_WUT_SetCompare(uint32_t cmp_cnt);
176 
177 /**
178  * @brief   Set the timer count.
179  * @param   cnt     New count.
180  */
181 void MXC_WUT_SetCount(uint32_t cnt);
182 
183 /**
184  * @brief   Convert real time to timer ticks.
185  * @param   time    Number of units of time.
186  * @param   units   Which units of time you want to convert.
187  * @param   ticks   Pointer to store the number of ticks calculated.
188  * @return     #E_NO_ERROR If everything is successful.
189  * @return     @ref MXC_Error_Codes If function is unsuccessful.
190  */
191 int MXC_WUT_GetTicks(uint32_t time, mxc_wut_unit_t units, uint32_t *ticks);
192 
193 /**
194  * @brief   Convert timer ticks to real time.
195  * @param   ticks   Number of ticks.
196  * @param   time    Pointer to store number of units of time.
197  * @param   units   Pointer to store the units that time represents.
198  * @return     #E_NO_ERROR If everything is successful.
199  * @return     @ref MXC_Error_Codes If function is unsuccessful.
200  */
201 int MXC_WUT_GetTime(uint32_t ticks, uint32_t *time, mxc_wut_unit_t *units);
202 
203 /**
204  * @brief   Wait for an edge of the WUT count register.
205  */
206 void MXC_WUT_Edge(void);
207 
208 /**
209  * @brief   Store the count and snapshot values.
210  */
211 void MXC_WUT_Store(void);
212 
213 /**
214  * @brief   Restore the DBB clock with the stored count and snapshot values.
215  * @param   dbbFreq  Frequency of DBB clock.
216  */
217 void MXC_WUT_RestoreBBClock(uint32_t dbbFreq);
218 
219 /**
220  * @brief   Get the difference between the stored counter value
221  *          and the current counter value.
222  * @return  Returns the current counter value - stored counter value.
223  */
224 uint32_t MXC_WUT_GetSleepTicks(void);
225 
226 /**
227  * @brief   Delays for the given number of milliseconds.
228  * @param   waitMs  Number of milliseconds to wait.
229  */
230 void MXC_WUT_Delay_MS(uint32_t waitMs);
231 
232 /**
233  * @brief   Trim the 32 kHz crystal load settings, blocks until complete.
234  * @details This procedure uses the WUT and the BLE DBB, driven by the 32 MHz crystal,
235  *          to trim the load settings of the 32 kHz crystal. This procedure will only
236  *          work if the BLE DBB is initialized and running.
237  *
238  * @return  #E_NO_ERROR If everything is successful.
239  */
240 int MXC_WUT_TrimCrystal(void);
241 
242 /**
243  * @brief   Trim the 32 kHz crystal load settings, non-blocking interrupt based.
244  * @details This procedure uses the WUT and the BLE DBB, driven by the 32 MHz crystal,
245  *          to trim the load settings of the 32 kHz crystal. This procedure will only
246  *          work if the BLE DBB is initialized and running.
247  *
248  * @param   cb Callback for when the trim is complete.
249  * @return  #E_NO_ERROR If everything is successful.
250  */
251 int MXC_WUT_TrimCrystalAsync(mxc_wut_complete_cb_t cb);
252 
253 /**
254  * @brief   Check to see if the trim procedure is ongoing.
255  * @details Must leave the 32 MHz clock and BLE DBB running while the trim procedure is pending.
256  * @return  #E_NO_ERROR If trim is complete, E_BUSY if trim procedure is ongoing.
257  */
258 int MXC_WUT_TrimPending(void);
259 
260 /**
261  * @brief   Interrupt handler for trim procedure.
262  *
263  * @return  #E_NO_ERROR If trim is complete, E_BUSY if trim procedure is ongoing.
264  */
265 int MXC_WUT_Handler(void);
266 
267 /**@} end of group wut */
268 
269 #ifdef __cplusplus
270 }
271 #endif
272 
273 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32665_WUT_H_
274