1 /**
2  * @file    tmr.h
3  * @brief   Timer (TMR) 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_MAX32572_TMR_H_
28 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32572_TMR_H_
29 
30 /* **** Includes **** */
31 #include <stdint.h>
32 #include "mxc_device.h"
33 #include "tmr_regs.h"
34 #include "mxc_sys.h"
35 #include "gcr_regs.h"
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 /**
42  * @defgroup tmr Timer (TMR)
43  * @ingroup periphlibs
44  * @{
45  */
46 
47 /**
48  * @brief      Timer prescaler values
49  */
50 typedef enum {
51     MXC_TMR_PRES_1 = MXC_S_TMR_CTRL_CLKDIV_DIV1, ///< Divide input clock by 1
52     MXC_TMR_PRES_2 = MXC_S_TMR_CTRL_CLKDIV_DIV2, ///< Divide input clock by 2
53     MXC_TMR_PRES_4 = MXC_S_TMR_CTRL_CLKDIV_DIV4, ///< Divide input clock by 4
54     MXC_TMR_PRES_8 = MXC_S_TMR_CTRL_CLKDIV_DIV8, ///< Divide input clock by 8
55     MXC_TMR_PRES_16 = MXC_S_TMR_CTRL_CLKDIV_DIV16, ///< Divide input clock by 16
56     MXC_TMR_PRES_32 = MXC_S_TMR_CTRL_CLKDIV_DIV32, ///< Divide input clock by 32
57     MXC_TMR_PRES_64 = MXC_S_TMR_CTRL_CLKDIV_DIV64, ///< Divide input clock by 64
58     MXC_TMR_PRES_128 = MXC_S_TMR_CTRL_CLKDIV_DIV128, ///< Divide input clock by 128
59     MXC_TMR_PRES_256 = MXC_F_TMR_CTRL_CLKDIV3 |
60                        MXC_S_TMR_CTRL_CLKDIV_DIV1, ///< Divide input clock by 256
61     MXC_TMR_PRES_512 = MXC_F_TMR_CTRL_CLKDIV3 |
62                        MXC_S_TMR_CTRL_CLKDIV_DIV2, ///< Divide input clock by 512
63     MXC_TMR_PRES_1024 = MXC_F_TMR_CTRL_CLKDIV3 |
64                         MXC_S_TMR_CTRL_CLKDIV_DIV4, ///< Divide input clock by 1024
65     MXC_TMR_PRES_2048 = MXC_F_TMR_CTRL_CLKDIV3 |
66                         MXC_S_TMR_CTRL_CLKDIV_DIV8, ///< Divide input clock by 2048
67     MXC_TMR_PRES_4096 = MXC_F_TMR_CTRL_CLKDIV3 |
68                         MXC_S_TMR_CTRL_CLKDIV_DIV16, ///< Divide input clock by 4096
69 } mxc_tmr_pres_t;
70 
71 /**
72  * @brief      Timer modes
73  */
74 typedef enum {
75     MXC_TMR_MODE_ONESHOT = MXC_V_TMR_CTRL_MODE_ONESHOT, ///< Timer Mode ONESHOT
76     MXC_TMR_MODE_CONTINUOUS = MXC_V_TMR_CTRL_MODE_CONTINUOUS, ///< Timer Mode CONTINUOUS
77     MXC_TMR_MODE_COUNTER = MXC_V_TMR_CTRL_MODE_COUNTER, ///< Timer Mode COUNTER
78     MXC_TMR_MODE_PWM = MXC_V_TMR_CTRL_MODE_PWM, ///< Timer Mode PWM
79     MXC_TMR_MODE_CAPTURE = MXC_V_TMR_CTRL_MODE_CAPTURE, ///< Timer Mode CAPTURE
80     MXC_TMR_MODE_COMPARE = MXC_V_TMR_CTRL_MODE_COMPARE, ///< Timer Mode COMPARE
81     MXC_TMR_MODE_GATED = MXC_V_TMR_CTRL_MODE_GATED, ///< Timer Mode GATED
82     MXC_TMR_MODE_CAPTURE_COMPARE =
83         MXC_V_TMR_CTRL_MODE_CAPTURECOMPARE, ///< Timer Mode CAPTURECOMPARE
84 } mxc_tmr_mode_t;
85 
86 /**
87  * @brief      Timer units of time enumeration
88  */
89 typedef enum {
90     MXC_TMR_UNIT_NANOSEC = 0, ///< Nanosecond Unit Indicator
91     MXC_TMR_UNIT_MICROSEC, ///< Microsecond Unit Indicator
92     MXC_TMR_UNIT_MILLISEC, ///< Millisecond Unit Indicator
93     MXC_TMR_UNIT_SEC, ///< Second Unit Indicator
94 } mxc_tmr_unit_t;
95 
96 /**
97  * @brief      Timer Configuration
98  */
99 typedef struct {
100     mxc_tmr_pres_t pres; ///< Desired timer prescaler
101     mxc_tmr_mode_t mode; ///< Desired timer mode
102     uint32_t cmp_cnt; ///< Compare register value in timer ticks
103     unsigned pol; ///< Polarity (0 or 1)
104 } mxc_tmr_cfg_t;
105 
106 /* **** Definitions **** */
107 typedef void (*mxc_tmr_complete_t)(int error);
108 
109 /* **** Function Prototypes **** */
110 
111 /**
112  * @brief      Initialize timer module clock.
113  * @note       On default this function enables TMR peripheral clock and related GPIOs.
114  *             if you wish to manage clock and gpio related things in upper level instead of here.
115  *             Define MSDK_NO_GPIO_CLK_INIT flag in project.mk file.
116  *             By this flag this function will remove clock and gpio related codes from file.
117  *
118  * @param      tmr        Pointer to timer module to initialize.
119  * @param      cfg        configuration object
120  */
121 void MXC_TMR_Init(mxc_tmr_regs_t *tmr, mxc_tmr_cfg_t *cfg);
122 
123 /**
124  * @brief      Shutdown timer module clock.
125  * @param      tmr  Pointer to timer module to initialize.
126  */
127 void MXC_TMR_Shutdown(mxc_tmr_regs_t *tmr);
128 
129 /**
130  * @brief      Start the timer counting.
131  * @param      tmr  Pointer to timer module to initialize.
132  */
133 void MXC_TMR_Start(mxc_tmr_regs_t *tmr);
134 
135 /**
136  * @brief      Stop the timer.
137  * @param      tmr  Pointer to timer module to initialize.
138  */
139 void MXC_TMR_Stop(mxc_tmr_regs_t *tmr);
140 
141 /**
142  * @brief      Set the value of the first transition in PWM mode
143  * @param      tmr     Pointer to timer module to initialize.
144  * @param      pwm     New pwm count.
145  * @note       Will block until safe to change the period count.
146  * @return     E_BAD_PARAM if pwm > cnt.
147  */
148 int MXC_TMR_SetPWM(mxc_tmr_regs_t *tmr, uint32_t pwm);
149 
150 /**
151  * @brief      Get the timer compare count.
152  * @param      tmr     Pointer to timer module to initialize.
153  * @return     Returns the current compare count.
154  */
155 uint32_t MXC_TMR_GetCompare(mxc_tmr_regs_t *tmr);
156 
157 /**
158  * @brief      Get the timer capture count.
159  * @param      tmr     Pointer to timer module to initialize.
160  * @return     Returns the most recent capture count.
161  */
162 uint32_t MXC_TMR_GetCapture(mxc_tmr_regs_t *tmr);
163 
164 /**
165  * @brief      Get the timer count.
166  * @param      tmr     Pointer to timer module to initialize.
167  * @return     Returns the current count.
168  */
169 uint32_t MXC_TMR_GetCount(mxc_tmr_regs_t *tmr);
170 
171 /**
172  * @brief      Clear the timer interrupt.
173  * @param      tmr     Pointer to timer module to initialize.
174  */
175 void MXC_TMR_ClearFlags(mxc_tmr_regs_t *tmr);
176 
177 /**
178  * @brief      Get the timer interrupt status.
179  * @param      tmr     Pointer to timer module to initialize.
180  * @return     Returns the interrupt status. 1 if interrupt has occured.
181  */
182 uint32_t MXC_TMR_GetFlags(mxc_tmr_regs_t *tmr);
183 
184 /**
185  * @brief      enable interupt
186  *
187  * @param      tmr   The timer
188  */
189 void MXC_TMR_EnableInt(mxc_tmr_regs_t *tmr);
190 
191 /**
192  * @brief      disable interupt
193  *
194  * @param      tmr   The timer
195  */
196 void MXC_TMR_DisableInt(mxc_tmr_regs_t *tmr);
197 
198 /**
199  * @brief      Set the timer compare count.
200  * @param      tmr     Pointer to timer module to initialize.
201  * @param      cmp_cnt New compare count.
202  * @note       In PWM Mode use this to set the value of the second transition.
203  */
204 void MXC_TMR_SetCompare(mxc_tmr_regs_t *tmr, uint32_t cmp_cnt);
205 
206 /**
207  * @brief      Set the timer count.
208  * @param      tmr     Pointer to timer module to initialize.
209  * @param      cnt     New count.
210  */
211 void MXC_TMR_SetCount(mxc_tmr_regs_t *tmr, uint32_t cnt);
212 
213 /**
214  * @brief      Dealay for a set periord of time measured in microseconds
215  *
216  * @param      tmr   The timer
217  * @param[in]  us    microseconds to delay for
218  */
219 void MXC_TMR_Delay(mxc_tmr_regs_t *tmr, uint32_t us);
220 
221 /**
222  * @brief      Start a timer that will time out after a certain number of microseconds
223  *
224  * @param      tmr   The timer
225  * @param[in]  us    microseconds to time out after
226  */
227 void MXC_TMR_TO_Start(mxc_tmr_regs_t *tmr, uint32_t us);
228 
229 /**
230  * @brief      Check on time out timer
231  *
232  * @param      tmr   The timer
233  *
234  * @return     Returns E_TIMEOUT if timer timed out or E_NO_ERROR if it has not timed out
235  */
236 int MXC_TMR_TO_Check(mxc_tmr_regs_t *tmr);
237 
238 /**
239  * @brief      Stop the Timeout timer
240  *
241  * @param      tmr   The timer
242  */
243 void MXC_TMR_TO_Stop(mxc_tmr_regs_t *tmr);
244 
245 /**
246  * @brief      Clear timeout timer back to zero
247  *
248  * @param      tmr   The timer
249  */
250 void MXC_TMR_TO_Clear(mxc_tmr_regs_t *tmr);
251 
252 /**
253  * @brief      Get elapsed time of timeout timer
254  *
255  * @param      tmr   The timer
256  *
257  * @return     Time that has elapsed in timeout timer
258  */
259 unsigned int MXC_TMR_TO_Elapsed(mxc_tmr_regs_t *tmr);
260 
261 /**
262  * @brief      Amount of time remaining until timeour
263  *
264  * @param      tmr   The timer
265  *
266  * @return     Time that is left until timeout
267  */
268 unsigned int MXC_TMR_TO_Remaining(mxc_tmr_regs_t *tmr);
269 
270 /**
271  * @brief      Start stopwatch
272  *
273  * @param      tmr      The timer
274  */
275 void MXC_TMR_SW_Start(mxc_tmr_regs_t *tmr);
276 
277 /**
278  * @brief      Stopwatch stop
279  *
280  * @param      tmr   The timer
281  *
282  * @return     the time when the stopwatch is stopped.
283  */
284 unsigned int MXC_TMR_SW_Stop(mxc_tmr_regs_t *tmr);
285 
286 /**
287  * @brief      Get time from timer
288  *
289  * @param      tmr    The timer
290  * @param[in]  ticks  The ticks
291  * @param      time   The time
292  * @param      units  The units
293  *
294  * @return     E_NO_ERROR or error check mxc_errors.
295  */
296 int MXC_TMR_GetTime(mxc_tmr_regs_t *tmr, uint32_t ticks, uint32_t *time, mxc_tmr_unit_t *units);
297 
298 /**@} end of group tmr */
299 
300 #ifdef __cplusplus
301 }
302 #endif
303 
304 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32572_TMR_H_
305