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_MAX32662_TMR_H_
28 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32662_TMR_H_
29 
30 /* **** Includes **** */
31 #include "gcr_regs.h"
32 #include "mcr_regs.h"
33 #include "mxc_device.h"
34 #include "mxc_pins.h"
35 #include "mxc_sys.h"
36 #include "stdbool.h"
37 #include "tmr_regs.h"
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 /**
44  * @defgroup tmr Timer (TMR)
45  * @ingroup periphlibs
46  * @{
47  */
48 
49 /**
50  * @brief Timer prescaler values
51  */
52 typedef enum {
53     MXC_TMR_PRES_1 = MXC_S_TMR_CTRL0_CLKDIV_A_DIV_BY_1, /**< Divide input clock by 1 */
54     MXC_TMR_PRES_2 = MXC_S_TMR_CTRL0_CLKDIV_A_DIV_BY_2, /**< Divide input clock by 2 */
55     MXC_TMR_PRES_4 = MXC_S_TMR_CTRL0_CLKDIV_A_DIV_BY_4, /**< Divide input clock by 4 */
56     MXC_TMR_PRES_8 = MXC_S_TMR_CTRL0_CLKDIV_A_DIV_BY_8, /**< Divide input clock by 8 */
57     MXC_TMR_PRES_16 = MXC_S_TMR_CTRL0_CLKDIV_A_DIV_BY_16, /**< Divide input clock by 16 */
58     MXC_TMR_PRES_32 = MXC_S_TMR_CTRL0_CLKDIV_A_DIV_BY_32, /**< Divide input clock by 32 */
59     MXC_TMR_PRES_64 = MXC_S_TMR_CTRL0_CLKDIV_A_DIV_BY_64, /**< Divide input clock by 64 */
60     MXC_TMR_PRES_128 = MXC_S_TMR_CTRL0_CLKDIV_A_DIV_BY_128, /**< Divide input clock by 128 */
61     MXC_TMR_PRES_256 = MXC_S_TMR_CTRL0_CLKDIV_A_DIV_BY_256, /**< Divide input clock by 256 */
62     MXC_TMR_PRES_512 = MXC_S_TMR_CTRL0_CLKDIV_A_DIV_BY_512, /**< Divide input clock by 512 */
63     MXC_TMR_PRES_1024 = MXC_S_TMR_CTRL0_CLKDIV_A_DIV_BY_1024, /**< Divide input clock by 1024 */
64     MXC_TMR_PRES_2048 = MXC_S_TMR_CTRL0_CLKDIV_A_DIV_BY_2048, /**< Divide input clock by 2048 */
65     MXC_TMR_PRES_4096 = MXC_S_TMR_CTRL0_CLKDIV_A_DIV_BY_4096, /**< Divide input clock by 4096 */
66 
67     // Legacy names
68     TMR_PRES_1 = MXC_TMR_PRES_1,
69     TMR_PRES_2 = MXC_TMR_PRES_2,
70     TMR_PRES_4 = MXC_TMR_PRES_4,
71     TMR_PRES_8 = MXC_TMR_PRES_8,
72     TMR_PRES_16 = MXC_TMR_PRES_16,
73     TMR_PRES_32 = MXC_TMR_PRES_32,
74     TMR_PRES_64 = MXC_TMR_PRES_64,
75     TMR_PRES_128 = MXC_TMR_PRES_128,
76     TMR_PRES_256 = MXC_TMR_PRES_256,
77     TMR_PRES_512 = MXC_TMR_PRES_512,
78     TMR_PRES_1024 = MXC_TMR_PRES_1024,
79     TMR_PRES_2048 = MXC_TMR_PRES_2048,
80     TMR_PRES_4096 = MXC_TMR_PRES_4096
81 } mxc_tmr_pres_t;
82 
83 /**
84  * @brief Timer modes
85  */
86 typedef enum {
87     MXC_TMR_MODE_ONESHOT = MXC_V_TMR_CTRL0_MODE_A_ONE_SHOT, ///< Timer Mode ONESHOT
88     MXC_TMR_MODE_CONTINUOUS = MXC_V_TMR_CTRL0_MODE_A_CONTINUOUS, ///< Timer Mode CONTINUOUS
89     MXC_TMR_MODE_COUNTER = MXC_V_TMR_CTRL0_MODE_A_COUNTER, ///< Timer Mode COUNTER
90     MXC_TMR_MODE_PWM = MXC_V_TMR_CTRL0_MODE_A_PWM, ///< Timer Mode PWM
91     MXC_TMR_MODE_CAPTURE = MXC_V_TMR_CTRL0_MODE_A_CAPTURE, ///< Timer Mode CAPTURE
92     MXC_TMR_MODE_COMPARE = MXC_V_TMR_CTRL0_MODE_A_COMPARE, ///< Timer Mode COMPARE
93     MXC_TMR_MODE_GATED = MXC_V_TMR_CTRL0_MODE_A_GATED, ///< Timer Mode GATED
94     MXC_TMR_MODE_CAPTURE_COMPARE = MXC_V_TMR_CTRL0_MODE_A_CAPCOMP, ///< Timer Mode CAPTURECOMPARE
95     MXC_TMR_MODE_DUAL_EDGE = MXC_V_TMR_CTRL0_MODE_A_DUAL_EDGE, ///< Timer Mode DUALEDGE
96 
97     // Legacy names
98     TMR_MODE_ONESHOT = MXC_TMR_MODE_ONESHOT,
99     TMR_MODE_CONTINUOUS = MXC_TMR_MODE_CONTINUOUS,
100     TMR_MODE_COUNTER = MXC_TMR_MODE_COUNTER,
101     TMR_MODE_PWM = MXC_TMR_MODE_PWM,
102     TMR_MODE_CAPTURE = MXC_TMR_MODE_CAPTURE,
103     TMR_MODE_COMPARE = MXC_TMR_MODE_COMPARE,
104     TMR_MODE_GATED = MXC_TMR_MODE_GATED,
105     TMR_MODE_CAPTURE_COMPARE = MXC_TMR_MODE_CAPTURE_COMPARE,
106     TMR_MODE_DUAL_EDGE = MXC_TMR_MODE_DUAL_EDGE
107 } mxc_tmr_mode_t;
108 
109 /**
110  * @brief Timer bit mode
111  *
112  */
113 typedef enum {
114     MXC_TMR_BIT_MODE_32 = 0, /**< Timer Mode 32 bit  */
115     MXC_TMR_BIT_MODE_16A, /**< Timer Mode Lower 16 bit */
116     MXC_TMR_BIT_MODE_16B, /**< Timer Mode Upper 16 bit */
117 
118     // Legacy names
119     TMR_BIT_MODE_32 = MXC_TMR_BIT_MODE_32,
120     TMR_BIT_MODE_16A = MXC_TMR_BIT_MODE_16A,
121     TMR_BIT_MODE_16B = MXC_TMR_BIT_MODE_16B,
122 } mxc_tmr_bit_mode_t;
123 
124 /**
125  * @brief Timer units of time enumeration
126  */
127 typedef enum {
128     MXC_TMR_UNIT_NANOSEC = 0, /**< Nanosecond Unit Indicator */
129     MXC_TMR_UNIT_MICROSEC, /**< Microsecond Unit Indicator */
130     MXC_TMR_UNIT_MILLISEC, /**< Millisecond Unit Indicator */
131     MXC_TMR_UNIT_SEC, /**< Second Unit Indicator */
132 
133     // Legacy names
134     TMR_UNIT_NANOSEC = MXC_TMR_UNIT_NANOSEC,
135     TMR_UNIT_MICROSEC = MXC_TMR_UNIT_MICROSEC,
136     TMR_UNIT_MILLISEC = MXC_TMR_UNIT_MILLISEC,
137     TMR_UNIT_SEC = MXC_TMR_UNIT_SEC,
138 } mxc_tmr_unit_t;
139 
140 /**
141  * @brief      Peripheral Clock settings
142  */
143 typedef enum {
144     MXC_TMR_APB_CLK = 0, /**< PCLK */
145     MXC_TMR_EXT_CLK = 1, /**< External Clock */
146     MXC_TMR_IBRO_CLK = 2, /**< 7.3728MHz Clock */
147     MXC_TMR_ERFO_CLK = 3, /**< 32MHz Clock */
148     MXC_TMR_ERTCO_CLK = 4, /**< 32.768KHz Clock */
149     MXC_TMR_INRO_CLK = 5, /**< 80KHz Clock */
150 
151     // Legacy names
152     /*8M and 60M clocks can be used for Timers 0,1,2 and 3*/
153     MXC_TMR_60M_CLK, // Not supported as TMR clock source
154     MXC_TMR_32M_CLK = MXC_TMR_ERFO_CLK,
155     MXC_TMR_8M_CLK = MXC_TMR_IBRO_CLK,
156     /*32K clock can be used for Timers 0,1,2,3 and 4*/
157     MXC_TMR_32K_CLK = MXC_TMR_ERTCO_CLK,
158     /*80K and EXT clocks can only be used for Timers 4 and 5*/
159     MXC_TMR_80K_CLK = MXC_TMR_INRO_CLK,
160 } mxc_tmr_clock_t;
161 
162 /**
163  * @brief Timer Configuration
164  */
165 typedef struct {
166     mxc_tmr_pres_t pres; /**< Desired timer prescaler */
167     mxc_tmr_mode_t mode; /**< Desired timer mode */
168     mxc_tmr_bit_mode_t bitMode; /**< Desired timer bits */
169     mxc_tmr_clock_t clock; /**< Desired clock source */
170     uint32_t cmp_cnt; /**< Compare register value in timer ticks */
171     unsigned pol; /**< Polarity (0 or 1) */
172 } mxc_tmr_cfg_t;
173 
174 /* **** Definitions **** */
175 typedef void (*mxc_tmr_complete_t)(int error);
176 
177 /* **** Function Prototypes **** */
178 
179 /**
180  * @brief   Initialize timer module clock.
181  * @note    On default this function enables TMR peripheral clock and related GPIOs.
182  *          if you wish to manage clock and gpio related things in upper level instead of here.
183  *          Define MSDK_NO_GPIO_CLK_INIT flag in project.mk file.
184  *          By this flag this function will remove clock and gpio related codes from file.
185  *
186  * @param   tmr        Pointer to timer module to initialize.
187  * @param   cfg        System configuration object
188  * @param   init_pins  True will initialize pins corresponding to the TMR and False will not if pins are pinned out otherwise it will not
189  *                     be used,  has no effect incase of MSDK_NO_GPIO_CLK_INIT has been defined.
190  * @param   pin_sel    Selects which set of pins to initialize (if init_pins set to true) for the given timer instance,
191  *                     has no effect incase of MSDK_NO_GPIO_CLK_INIT has been defined.
192  *
193  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
194  */
195 int MXC_TMR_Init(mxc_tmr_regs_t *tmr, mxc_tmr_cfg_t *cfg, bool init_pins, sys_map_t pin_sel);
196 
197 /**
198  * @brief   Shutdown timer module clock.
199  * @param   tmr  Pointer to timer module to initialize.
200  */
201 void MXC_TMR_Shutdown(mxc_tmr_regs_t *tmr);
202 
203 /**
204  * @brief   Start the timer counting.
205  * @param   tmr  Pointer to timer module to initialize.
206  */
207 void MXC_TMR_Start(mxc_tmr_regs_t *tmr);
208 
209 /**
210  * @brief   Stop the timer.
211  * @param   tmr  Pointer to timer module to initialize.
212  */
213 void MXC_TMR_Stop(mxc_tmr_regs_t *tmr);
214 
215 /**
216  * @brief   Set the value of the first transition in PWM mode
217  * @param   tmr     Pointer to timer module to initialize.
218  * @param   pwm     New pwm count.
219  * @note    Will block until safe to change the period count.
220  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
221  */
222 int MXC_TMR_SetPWM(mxc_tmr_regs_t *tmr, uint32_t pwm);
223 
224 /**
225  * @brief   Get the timer compare count.
226  * @param   tmr     Pointer to timer module to initialize.
227  * @return  Returns the current compare count.
228  */
229 uint32_t MXC_TMR_GetCompare(mxc_tmr_regs_t *tmr);
230 
231 /**
232  * @brief   Get the timer capture count.
233  * @param   tmr     Pointer to timer module to initialize.
234  * @return  Returns the most recent capture count.
235  */
236 uint32_t MXC_TMR_GetCapture(mxc_tmr_regs_t *tmr);
237 
238 /**
239  * @brief   Get the timer count.
240  * @param   tmr     Pointer to timer module to initialize.
241  * @return  Returns the current count.
242  */
243 uint32_t MXC_TMR_GetCount(mxc_tmr_regs_t *tmr);
244 
245 /**
246  * @brief   Calculate count for required frequency.
247  * @param   tmr         Timer
248  * @param   clock       Clock source.
249  * @param   prescalar   prescalar
250  * @param   frequency   required frequency.
251  * @return  Returns the period count.
252  */
253 uint32_t MXC_TMR_GetPeriod(mxc_tmr_regs_t *tmr, mxc_tmr_clock_t clock, uint32_t prescalar,
254                            uint32_t frequency);
255 
256 /**
257  * @brief   Clear the timer interrupt.
258  * @param   tmr     Pointer to timer module to initialize.
259  */
260 void MXC_TMR_ClearFlags(mxc_tmr_regs_t *tmr);
261 
262 /**
263  * @brief   Get the timer interrupt status.
264  * @param   tmr     Pointer to timer module to initialize.
265  * @return  Returns the interrupt status. 1 if interrupt has occured.
266  */
267 uint32_t MXC_TMR_GetFlags(mxc_tmr_regs_t *tmr);
268 
269 /**
270  * @brief   enable interupt
271  *
272  * @param   tmr   Pointer to timer module to initialize.
273  */
274 void MXC_TMR_EnableInt(mxc_tmr_regs_t *tmr);
275 
276 /**
277  * @brief   disable interupt
278  *
279  * @param   tmr   Pointer to timer module to initialize.
280  */
281 void MXC_TMR_DisableInt(mxc_tmr_regs_t *tmr);
282 
283 /**
284  * @brief   Enable wakeup from sleep
285  *
286  * @param   tmr   Pointer to timer module to initialize.
287  * @param   cfg   System configuration object
288  */
289 void MXC_TMR_EnableWakeup(mxc_tmr_regs_t *tmr, mxc_tmr_cfg_t *cfg);
290 
291 /**
292  * @brief   Disable wakeup from sleep
293  *
294  * @param   tmr   Pointer to timer module to initialize.
295  * @param   cfg   System configuration object
296  */
297 void MXC_TMR_DisableWakeup(mxc_tmr_regs_t *tmr, mxc_tmr_cfg_t *cfg);
298 
299 /**
300  * @brief   Set the timer compare count.
301  * @param   tmr     Pointer to timer module to initialize.
302  * @param   cmp_cnt New compare count.
303  * @note    In PWM Mode use this to set the value of the second transition.
304  */
305 void MXC_TMR_SetCompare(mxc_tmr_regs_t *tmr, uint32_t cmp_cnt);
306 
307 /**
308  * @brief   Set the timer count.
309  * @param   tmr     Pointer to timer module to initialize.
310  * @param   cnt     New count.
311  */
312 void MXC_TMR_SetCount(mxc_tmr_regs_t *tmr, uint32_t cnt);
313 
314 /**
315  * @brief   Dealay for a set periord of time measured in microseconds
316  *
317  * @param   tmr   The timer
318  * @param   us    microseconds to delay for
319  */
320 void MXC_TMR_Delay(mxc_tmr_regs_t *tmr, uint32_t us);
321 
322 /**
323  * @brief   Start a timer that will time out after a certain number of microseconds
324  *
325  * @param   tmr   The timer
326  * @param   us    microseconds to time out after
327  */
328 void MXC_TMR_TO_Start(mxc_tmr_regs_t *tmr, uint32_t us);
329 
330 /**
331  * @brief   Check on time out timer
332  *
333  * @param   tmr   The timer
334  *
335  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
336  */
337 int MXC_TMR_TO_Check(mxc_tmr_regs_t *tmr);
338 
339 /**
340  * @brief   Stop the Timeout timer
341  *
342  * @param   tmr   The timer
343  */
344 void MXC_TMR_TO_Stop(mxc_tmr_regs_t *tmr);
345 
346 /**
347  * @brief   Clear timeout timer back to zero
348  *
349  * @param   tmr   The timer
350  */
351 void MXC_TMR_TO_Clear(mxc_tmr_regs_t *tmr);
352 
353 /**
354  * @brief   Get elapsed time of timeout timer
355  *
356  * @param   tmr   The timer
357  *
358  * @return  Time that has elapsed in timeout timer
359  */
360 unsigned int MXC_TMR_TO_Elapsed(mxc_tmr_regs_t *tmr);
361 
362 /**
363  * @brief   Amount of time remaining until timeour
364  *
365  * @param   tmr   The timer
366  *
367  * @return  Time that is left until timeout
368  */
369 unsigned int MXC_TMR_TO_Remaining(mxc_tmr_regs_t *tmr);
370 
371 /**
372  * @brief   Start stopwatch
373  *
374  * @param   tmr      The timer
375  */
376 void MXC_TMR_SW_Start(mxc_tmr_regs_t *tmr);
377 
378 /**
379  * @brief   Stopwatch stop
380  *
381  * @param   tmr   The timer
382  *
383  * @return  the time when the stopwatch is stopped.
384  */
385 unsigned int MXC_TMR_SW_Stop(mxc_tmr_regs_t *tmr);
386 
387 /**
388  * @brief   Get time from timer
389  *
390  * @param   tmr    The timer
391  * @param   ticks  The ticks
392  * @param   time   The time
393  * @param   units  The units
394  *
395  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
396  */
397 int MXC_TMR_GetTime(mxc_tmr_regs_t *tmr, uint32_t ticks, uint32_t *time, mxc_tmr_unit_t *units);
398 
399 /**
400  * @brief   Get ticks from timer
401  *
402  * @param   tmr    The timer
403  * @param   time   The time
404  * @param   units  The units
405  * @param   ticks  The ticks
406  *
407  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
408  */
409 int MXC_TMR_GetTicks(mxc_tmr_regs_t *tmr, uint32_t time, mxc_tmr_unit_t units, uint32_t *ticks);
410 
411 /**@} end of group tmr */
412 
413 #ifdef __cplusplus
414 }
415 #endif
416 
417 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32662_TMR_H_
418