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_MAX78000_WUT_H_
28 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX78000_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_CAPTURE = MXC_V_WUT_CTRL_TMODE_CAPTURE, /// Wakeup Timer Mode CAPTURE
76     MXC_WUT_MODE_COMPARE = MXC_V_WUT_CTRL_TMODE_COMPARE, /// Wakeup Timer Mode COMPARE
77     MXC_WUT_MODE_GATED = MXC_V_WUT_CTRL_TMODE_GATED, /// Wakeup Timer Mode GATED
78     MXC_WUT_MODE_CAPTURE_COMPARE =
79         MXC_V_WUT_CTRL_TMODE_CAPTURECOMPARE /// Wakeup Timer Mode CAPTURECOMPARE
80 } mxc_wut_mode_t;
81 
82 /**
83  * @brief Wakeup Timer units of time enumeration
84  */
85 typedef enum {
86     MXC_WUT_UNIT_NANOSEC = 0, /**< Nanosecond Unit Indicator. */
87     MXC_WUT_UNIT_MICROSEC, /**< Microsecond Unit Indicator. */
88     MXC_WUT_UNIT_MILLISEC, /**< Millisecond Unit Indicator. */
89     MXC_WUT_UNIT_SEC /**< Second Unit Indicator. */
90 } mxc_wut_unit_t;
91 
92 /**
93  * @brief Wakeup Timer Configuration
94  */
95 typedef struct {
96     mxc_wut_mode_t mode; /// Desired timer mode
97     uint32_t cmp_cnt; /// Compare register value in timer ticks
98 } mxc_wut_cfg_t;
99 
100 /**
101  * @brief   The callback routine used by the MXC_WUT_TrimCrystalAsync()
102  *          function to indicate the transaction has completed.
103  *
104  * @param   result      Error code.
105  */
106 typedef void (*mxc_wut_complete_cb_t)(int result);
107 
108 /* **** Definitions **** */
109 
110 /* **** Function Prototypes **** */
111 
112 /**
113  * @brief      Initialize timer module clock.
114  * @param      pres       Prescaler value.
115  */
116 void MXC_WUT_Init(mxc_wut_pres_t pres);
117 
118 /**
119  * @brief      Shutdown timer module clock.
120  */
121 void MXC_WUT_Shutdown(void);
122 
123 /**
124  * @brief      Enable the timer.
125  */
126 void MXC_WUT_Enable(void);
127 
128 /**
129  * @brief      Disable the timer.
130  */
131 void MXC_WUT_Disable(void);
132 
133 /**
134  * @brief      Configure the timer.
135  * @param      cfg  Pointer to timer configuration struct.
136  */
137 void MXC_WUT_Config(const mxc_wut_cfg_t *cfg);
138 
139 /**
140  * @brief   Get the timer compare count.
141  * @return  Returns the current compare count.
142  */
143 uint32_t MXC_WUT_GetCompare(void);
144 
145 /**
146  * @brief   Get the timer count.
147  * @return  Returns the current count.
148  */
149 uint32_t MXC_WUT_GetCount(void);
150 
151 /**
152  * @brief   Clear the timer interrupt.
153  */
154 void MXC_WUT_IntClear(void);
155 
156 /**
157  * @brief   Get the timer interrupt status.
158  * @return  Returns the interrupt status. 1 if interrupt has occurred.
159  */
160 uint32_t MXC_WUT_IntStatus(void);
161 
162 /**
163  * @brief   Set the timer compare count.
164  * @param   cmp_cnt New compare count.
165  * @note    This function does not protect against output glitches in PWM mode.
166  *          Use MXC_WUT_PWMSetPeriod when in PWM mode.
167  */
168 void MXC_WUT_SetCompare(uint32_t cmp_cnt);
169 
170 /**
171  * @brief   Set the timer count.
172  * @param   cnt     New count.
173  */
174 void MXC_WUT_SetCount(uint32_t cnt);
175 
176 /**
177  * @brief   Convert real time to timer ticks.
178  * @param   time    Number of units of time.
179  * @param   units   Which units of time you want to convert.
180  * @param   ticks   Pointer to store the number of ticks calculated.
181  * @return     #E_NO_ERROR If everything is successful.
182  * @return     @ref MXC_Error_Codes If function is unsuccessful.
183  */
184 int MXC_WUT_GetTicks(uint32_t time, mxc_wut_unit_t units, uint32_t *ticks);
185 
186 /**
187  * @brief   Convert timer ticks to real time.
188  * @param   ticks   Number of ticks.
189  * @param   time    Pointer to store number of units of time.
190  * @param   units   Pointer to store the units that time represents.
191  * @return     #E_NO_ERROR If everything is successful.
192  * @return     @ref MXC_Error_Codes If function is unsuccessful.
193  */
194 int MXC_WUT_GetTime(uint32_t ticks, uint32_t *time, mxc_wut_unit_t *units);
195 
196 /**
197  * @brief   Wait for an edge of the WUT count register.
198  */
199 void MXC_WUT_Edge(void);
200 
201 /**
202  * @brief   Store the count and snapshot values.
203  */
204 void MXC_WUT_Store(void);
205 
206 /**
207  * @brief   Restore the DBB clock with the stored count and snapshot values.
208  * @param   dbbFreq  Frequency of DBB clock.
209  */
210 void MXC_WUT_RestoreBBClock(uint32_t dbbFreq);
211 
212 /**
213  * @brief   Get the difference between the stored counter value
214  *          and the current counter value.
215  * @return  Returns the current counter value - stored counter value.
216  */
217 uint32_t MXC_WUT_GetSleepTicks(void);
218 
219 /**
220  * @brief   Delays for the given number of milliseconds.
221  * @param   waitMs  Number of milliseconds to wait.
222  */
223 void MXC_WUT_Delay_MS(uint32_t waitMs);
224 
225 /**@} end of group wut */
226 
227 #ifdef __cplusplus
228 }
229 #endif
230 
231 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX78000_WUT_H_
232