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_MAX32660_TMR_H_
28 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32660_TMR_H_
29 
30 /* **** Includes **** */
31 #include "mxc_device.h"
32 #include "mxc_errors.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_CN_PRES_DIV_BY_1, ///< Divide input clock by 1
52     MXC_TMR_PRES_2 = MXC_S_TMR_CN_PRES_DIV_BY_2, ///< Divide input clock by 2
53     MXC_TMR_PRES_4 = MXC_S_TMR_CN_PRES_DIV_BY_4, ///< Divide input clock by 4
54     MXC_TMR_PRES_8 = MXC_S_TMR_CN_PRES_DIV_BY_8, ///< Divide input clock by 8
55     MXC_TMR_PRES_16 = MXC_S_TMR_CN_PRES_DIV_BY_16, ///< Divide input clock by 16
56     MXC_TMR_PRES_32 = MXC_S_TMR_CN_PRES_DIV_BY_32, ///< Divide input clock by 32
57     MXC_TMR_PRES_64 = MXC_S_TMR_CN_PRES_DIV_BY_64, ///< Divide input clock by 64
58     MXC_TMR_PRES_128 = MXC_S_TMR_CN_PRES_DIV_BY_128, ///< Divide input clock by 128
59     MXC_TMR_PRES_256 = MXC_S_TMR_CN_PRES_DIV_BY_256 |
60                        MXC_F_TMR_CN_PRES3, ///< Divide input clock by 256
61     MXC_TMR_PRES_512 = MXC_S_TMR_CN_PRES_DIV_BY_512 |
62                        MXC_F_TMR_CN_PRES3, ///< Divide input clock by 512
63     MXC_TMR_PRES_1024 = MXC_S_TMR_CN_PRES_DIV_BY_1024 |
64                         MXC_F_TMR_CN_PRES3, ///< Divide input clock by 1024
65     MXC_TMR_PRES_2048 = MXC_S_TMR_CN_PRES_DIV_BY_2048 |
66                         MXC_F_TMR_CN_PRES3, ///< Divide input clock by 2048
67     MXC_TMR_PRES_4096 = MXC_S_TMR_CN_PRES_DIV_BY_4096 |
68                         MXC_F_TMR_CN_PRES3, ///< Divide input clock by 4096
69     MXC_TMR_PRES_8192 = MXC_S_TMR_CN_PRES_DIV_BY_8192 |
70                         MXC_F_TMR_CN_PRES3, ///< Divide input clock by 8192
71 
72     // Legacy names
73     TMR_PRES_1 = MXC_TMR_PRES_1,
74     TMR_PRES_2 = MXC_TMR_PRES_2,
75     TMR_PRES_4 = MXC_TMR_PRES_4,
76     TMR_PRES_8 = MXC_TMR_PRES_8,
77     TMR_PRES_16 = MXC_TMR_PRES_16,
78     TMR_PRES_32 = MXC_TMR_PRES_32,
79     TMR_PRES_64 = MXC_TMR_PRES_64,
80     TMR_PRES_128 = MXC_TMR_PRES_128,
81     TMR_PRES_256 = MXC_TMR_PRES_256,
82     TMR_PRES_512 = MXC_TMR_PRES_512,
83     TMR_PRES_1024 = MXC_TMR_PRES_1024,
84     TMR_PRES_2048 = MXC_TMR_PRES_2048,
85     TMR_PRES_4096 = MXC_TMR_PRES_4096
86 } mxc_tmr_pres_t;
87 
88 /**
89  * @brief Timer modes
90  */
91 typedef enum {
92     MXC_TMR_MODE_ONESHOT = MXC_S_TMR_CN_TMODE_ONE_SHOT, ///< Timer Mode ONESHOT
93     MXC_TMR_MODE_CONTINUOUS = MXC_V_TMR_CN_TMODE_CONTINUOUS, ///< Timer Mode CONTINUOUS
94     MXC_TMR_MODE_COUNTER = MXC_V_TMR_CN_TMODE_COUNTER, ///< Timer Mode COUNTER
95     MXC_TMR_MODE_PWM = MXC_V_TMR_CN_TMODE_PWM, ///< Timer Mode PWM
96     MXC_TMR_MODE_CAPTURE = MXC_V_TMR_CN_TMODE_CAPTURE, ///< Timer Mode CAPTURE
97     MXC_TMR_MODE_COMPARE = MXC_V_TMR_CN_TMODE_COMPARE, ///< Timer Mode COMPARE
98     MXC_TMR_MODE_GATED = MXC_V_TMR_CN_TMODE_GATED, ///< Timer Mode GATED
99     MXC_TMR_MODE_CAPTURE_COMPARE = MXC_S_TMR_CN_TMODE_CAPCOMP, ///< Timer Mode CAPTURECOMPARE
100 
101     // Legacy names
102     TMR_MODE_ONESHOT = MXC_TMR_MODE_ONESHOT,
103     TMR_MODE_CONTINUOUS = MXC_TMR_MODE_CONTINUOUS,
104     TMR_MODE_COUNTER = MXC_TMR_MODE_COUNTER,
105     TMR_MODE_PWM = MXC_TMR_MODE_PWM,
106     TMR_MODE_CAPTURE = MXC_TMR_MODE_CAPTURE,
107     TMR_MODE_COMPARE = MXC_TMR_MODE_COMPARE,
108     TMR_MODE_GATED = MXC_TMR_MODE_GATED,
109     TMR_MODE_CAPTURE_COMPARE = MXC_TMR_MODE_CAPTURE_COMPARE
110 } mxc_tmr_mode_t;
111 
112 /**
113  * @brief Timer bit mode
114  *
115  */
116 typedef enum {
117     MXC_TMR_BIT_MODE_32, ///< Timer Mode 32 bit
118     MXC_TMR_BIT_MODE_16A, ///< Timer Mode Lower 16 bit
119     MXC_TMR_BIT_MODE_16B, ///< Timer Mode Upper 16 bit
120 
121     // Legacy names
122     TMR_BIT_MODE_32 = MXC_TMR_BIT_MODE_32,
123     TMR_BIT_MODE_16A = MXC_TMR_BIT_MODE_16A,
124     TMR_BIT_MODE_16B = MXC_TMR_BIT_MODE_16B,
125 } mxc_tmr_bit_mode_t;
126 
127 /**
128  * @brief Timer units of time enumeration
129  */
130 typedef enum {
131     MXC_TMR_UNIT_NANOSEC, ///< Nanosecond Unit Indicator
132     MXC_TMR_UNIT_MICROSEC, ///< Microsecond Unit Indicator
133     MXC_TMR_UNIT_MILLISEC, ///< Millisecond Unit Indicator
134     MXC_TMR_UNIT_SEC, ///< Second Unit Indicator
135 
136     // Legacy names
137     TMR_UNIT_NANOSEC = MXC_TMR_UNIT_NANOSEC,
138     TMR_UNIT_MICROSEC = MXC_TMR_UNIT_MICROSEC,
139     TMR_UNIT_MILLISEC = MXC_TMR_UNIT_MILLISEC,
140     TMR_UNIT_SEC = MXC_TMR_UNIT_SEC,
141 } mxc_tmr_unit_t;
142 
143 /**
144  * @brief Timer Configuration
145  */
146 typedef struct {
147     mxc_tmr_pres_t pres; ///< Desired timer prescaler
148     mxc_tmr_mode_t mode; ///< Desired timer mode
149     uint32_t cmp_cnt; ///< Compare register value in timer ticks
150     unsigned pol; ///< Polarity (0 or 1)
151 } mxc_tmr_cfg_t;
152 
153 /* **** Definitions **** */
154 typedef void (*mxc_tmr_complete_t)(int error);
155 
156 /* **** Function Prototypes **** */
157 
158 /**
159  * @brief   Initialize timer module clock.
160  * @note    On default this function enables TMR peripheral clock and related GPIOs.
161  *          if you wish to manage clock and gpio related things in upper level instead of here.
162  *          Define MSDK_NO_GPIO_CLK_INIT flag in project.mk file.
163  *          By this flag this function will remove clock and gpio related codes from file.
164  *
165  * @param   tmr        Pointer to timer module to initialize.
166  * @param   cfg        System configuration object
167  *
168  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
169  */
170 int MXC_TMR_Init(mxc_tmr_regs_t *tmr, mxc_tmr_cfg_t *cfg);
171 
172 /**
173  * @brief   Shutdown timer module clock.
174  * @param   tmr  Pointer to timer module to initialize.
175  */
176 void MXC_TMR_Shutdown(mxc_tmr_regs_t *tmr);
177 
178 /**
179  * @brief   Start the timer counting.
180  * @param   tmr  Pointer to timer module to initialize.
181  */
182 void MXC_TMR_Start(mxc_tmr_regs_t *tmr);
183 
184 /**
185  * @brief   Stop the timer.
186  * @param   tmr  Pointer to timer module to initialize.
187  */
188 void MXC_TMR_Stop(mxc_tmr_regs_t *tmr);
189 
190 /**
191  * @brief   Set the value of the first transition in PWM mode
192  * @param   tmr     Pointer to timer module to initialize.
193  * @param   pwm     New pwm count.
194  * @note    Will block until safe to change the period count.
195  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
196  */
197 int MXC_TMR_SetPWM(mxc_tmr_regs_t *tmr, uint32_t pwm);
198 
199 /**
200  * @brief   Get the timer compare count.
201  * @param   tmr     Pointer to timer module to initialize.
202  * @return  Returns the current compare count.
203  */
204 uint32_t MXC_TMR_GetCompare(mxc_tmr_regs_t *tmr);
205 
206 /**
207  * @brief   Get the timer capture count.
208  * @param   tmr     Pointer to timer module to initialize.
209  * @return  Returns the most recent capture count.
210  */
211 uint32_t MXC_TMR_GetCapture(mxc_tmr_regs_t *tmr);
212 
213 /**
214  * @brief   Get the timer count.
215  * @param   tmr     Pointer to timer module to initialize.
216  * @return  Returns the current count.
217  */
218 uint32_t MXC_TMR_GetCount(mxc_tmr_regs_t *tmr);
219 
220 /**
221  * @brief   Calculate count for required frequency.
222  * @param   tmr         Timer
223  * @param   prescalar   prescalar
224  * @param   frequency   required frequency.
225  * @return  Returns the period count.
226  */
227 uint32_t MXC_TMR_GetPeriod(mxc_tmr_regs_t *tmr, uint32_t prescalar, uint32_t frequency);
228 
229 /**
230  * @brief   Clear the timer interrupt.
231  * @param   tmr     Pointer to timer module to initialize.
232  */
233 void MXC_TMR_ClearFlags(mxc_tmr_regs_t *tmr);
234 
235 /**
236  * @brief   Get the timer interrupt status.
237  * @param   tmr     Pointer to timer module to initialize.
238  * @return  Returns the interrupt status. 1 if interrupt has occured.
239  */
240 uint32_t MXC_TMR_GetFlags(mxc_tmr_regs_t *tmr);
241 
242 /**
243  * @brief   enable interupt
244  *
245  * @param   tmr   The timer
246  */
247 void MXC_TMR_EnableInt(mxc_tmr_regs_t *tmr);
248 
249 /**
250  * @brief   disable interupt
251  *
252  * @param   tmr   The timer
253  */
254 void MXC_TMR_DisableInt(mxc_tmr_regs_t *tmr);
255 
256 /**
257  * @brief   Set the timer compare count.
258  * @param   tmr     Pointer to timer module to initialize.
259  * @param   cmp_cnt New compare count.
260  * @note    In PWM Mode use this to set the value of the second transition.
261  */
262 void MXC_TMR_SetCompare(mxc_tmr_regs_t *tmr, uint32_t cmp_cnt);
263 
264 /**
265  * @brief   Set the timer count.
266  * @param   tmr     Pointer to timer module to initialize.
267  * @param   cnt     New count.
268  */
269 void MXC_TMR_SetCount(mxc_tmr_regs_t *tmr, uint32_t cnt);
270 
271 /**
272  * @brief   Dealay for a set periord of time measured in microseconds
273  *
274  * @param   tmr   The timer
275  * @param   us    microseconds to delay for
276  */
277 void MXC_TMR_Delay(mxc_tmr_regs_t *tmr, uint32_t us);
278 
279 /**
280  * @brief   Start a timer that will time out after a certain number of microseconds
281  * @note    This uses the 32-it Timer
282  *
283  * @param   tmr   The timer
284  * @param   us    microseconds to time out after
285  */
286 void MXC_TMR_TO_Start(mxc_tmr_regs_t *tmr, uint32_t us);
287 
288 /**
289  * @brief   Check on time out timer
290  *
291  * @param   tmr   The timer
292  *
293  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
294  */
295 int MXC_TMR_TO_Check(mxc_tmr_regs_t *tmr);
296 
297 /**
298  * @brief   Stop the Timeout timer
299  *
300  * @param   tmr   The timer
301  */
302 void MXC_TMR_TO_Stop(mxc_tmr_regs_t *tmr);
303 
304 /**
305  * @brief   Clear timeout timer back to zero
306  *
307  * @param   tmr   The timer
308  */
309 void MXC_TMR_TO_Clear(mxc_tmr_regs_t *tmr);
310 
311 /**
312  * @brief   Get elapsed time of timeout timer
313  *
314  * @param   tmr   The timer
315  *
316  * @return  Time that has elapsed in timeout timer
317  */
318 unsigned int MXC_TMR_TO_Elapsed(mxc_tmr_regs_t *tmr);
319 
320 /**
321  * @brief   Amount of time remaining until timeour
322  *
323  * @param   tmr   The timer
324  *
325  * @return  Time that is left until timeout
326  */
327 unsigned int MXC_TMR_TO_Remaining(mxc_tmr_regs_t *tmr);
328 
329 /**
330  * @brief   Start stopwatch
331  *
332  * @param   tmr      The timer
333  */
334 void MXC_TMR_SW_Start(mxc_tmr_regs_t *tmr);
335 
336 /**
337  * @brief   Stopwatch stop
338  *
339  * @param   tmr   The timer
340  *
341  * @return  the time when the stopwatch is stopped.
342  */
343 unsigned int MXC_TMR_SW_Stop(mxc_tmr_regs_t *tmr);
344 
345 /**
346  * @brief   Get time from timer
347  *
348  * @param   tmr    The timer
349  * @param   ticks  The ticks
350  * @param   time   The time
351  * @param   units  The units
352  *
353  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
354  */
355 int MXC_TMR_GetTime(mxc_tmr_regs_t *tmr, uint32_t ticks, uint32_t *time, mxc_tmr_unit_t *units);
356 
357 /**@} end of group tmr */
358 
359 #ifdef __cplusplus
360 }
361 #endif
362 
363 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32660_TMR_H_
364