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_MAX32672_TMR_H_
28 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32672_TMR_H_
29 
30 /* **** Includes **** */
31 #include <stdint.h>
32 #include <stdbool.h>
33 #include "mxc_device.h"
34 #include "tmr_regs.h"
35 #include "mxc_sys.h"
36 #include "gcr_regs.h"
37 #include "mcr_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, ///< 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, ///< 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       Clock settings
142  * @note        8M and 32M clocks can be used for Timers 0,1,2 and 3
143  *              32K and 80K clocks can only be used for Timers 4 and 5
144  */
145 typedef enum {
146     MXC_TMR_APB_CLK = 0, ///< PCLK CLock
147     MXC_TMR_EXT_CLK = 1, ///< External Clock
148     MXC_TMR_IBRO_CLK = 2, ///< 7.3728MHz Clock
149     MXC_TMR_ERFO_CLK = 3, ///< 32MHz Clock
150     MXC_TMR_ERTCO_CLK = 4, ///< 32.768KHz Clock
151     MXC_TMR_INRO_CLK = 5, ///< 80KHz Clock
152 
153     // Legacy names
154     MXC_TMR_8M_CLK = MXC_TMR_IBRO_CLK, ///< 8MHz Clock
155     MXC_TMR_32M_CLK = MXC_TMR_ERFO_CLK, ///< 32MHz Clock
156     MXC_TMR_32K_CLK = MXC_TMR_ERTCO_CLK, ///< 32KHz Clock
157     MXC_TMR_80K_CLK = MXC_TMR_INRO_CLK, ///< 80KHz Clock
158 } mxc_tmr_clock_t;
159 
160 /**
161  * @brief Timer Configuration
162  */
163 typedef struct {
164     mxc_tmr_pres_t pres; ///< Desired timer prescaler
165     mxc_tmr_mode_t mode; ///< Desired timer mode
166     mxc_tmr_bit_mode_t bitMode; ///< Desired timer bits
167     mxc_tmr_clock_t clock; ///< Desired clock source
168     uint32_t cmp_cnt; ///< Compare register value in timer ticks
169     unsigned pol; ///< Polarity (0 or 1)
170 } mxc_tmr_cfg_t;
171 
172 /* **** Definitions **** */
173 typedef void (*mxc_tmr_complete_t)(int error);
174 
175 /* **** Function Prototypes **** */
176 
177 /**
178  * @brief   Initialize timer module clock.
179  * @note    On default this function enables TMR peripheral clock and related GPIOs.
180  *          if you wish to manage clock and gpio related things in upper level instead of here.
181  *          Define MSDK_NO_GPIO_CLK_INIT flag in project.mk file.
182  *          By this flag this function will remove clock and gpio related codes from file.
183  *
184  * @param   tmr        Pointer to timer module to initialize.
185  * @param   cfg        System configuration object
186  * @param   bool       True will initialize pins corresponding to the TMR and False will not if pins are pinned out otherwise it will not
187  *                     be used, has no effect incase of MSDK_NO_GPIO_CLK_INIT has been defined.
188  *
189  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
190  */
191 int MXC_TMR_Init(mxc_tmr_regs_t *tmr, mxc_tmr_cfg_t *cfg, bool init_pins);
192 
193 /**
194  * @brief   Shutdown timer module clock.
195  * @param   tmr  Pointer to timer module to initialize.
196  */
197 void MXC_TMR_Shutdown(mxc_tmr_regs_t *tmr);
198 
199 /**
200  * @brief   Start the timer counting.
201  * @param   tmr  Pointer to timer module to initialize.
202  */
203 void MXC_TMR_Start(mxc_tmr_regs_t *tmr);
204 
205 /**
206  * @brief   Stop the timer.
207  * @param   tmr  Pointer to timer module to initialize.
208  */
209 void MXC_TMR_Stop(mxc_tmr_regs_t *tmr);
210 
211 /**
212  * @brief   Set the value of the first transition in PWM mode
213  * @param   tmr     Pointer to timer module to initialize.
214  * @param   pwm     New pwm count.
215  * @note    Will block until safe to change the period count.
216  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
217  */
218 int MXC_TMR_SetPWM(mxc_tmr_regs_t *tmr, uint32_t pwm);
219 
220 /**
221  * @brief   Get the timer compare count.
222  * @param   tmr     Pointer to timer module to initialize.
223  * @return  Returns the current compare count.
224  */
225 uint32_t MXC_TMR_GetCompare(mxc_tmr_regs_t *tmr);
226 
227 /**
228  * @brief   Get the timer capture count.
229  * @param   tmr     Pointer to timer module to initialize.
230  * @return  Returns the most recent capture count.
231  */
232 uint32_t MXC_TMR_GetCapture(mxc_tmr_regs_t *tmr);
233 
234 /**
235  * @brief   Get the timer count.
236  * @param   tmr     Pointer to timer module to initialize.
237  * @return  Returns the current count.
238  */
239 uint32_t MXC_TMR_GetCount(mxc_tmr_regs_t *tmr);
240 
241 /**
242  * @brief   Calculate count for required frequency.
243  * @param   tmr         Timer
244  * @param   clock       Clock source.
245  * @param   prescalar   prescalar
246  * @param   frequency   required frequency.
247  * @return  Returns the period count.
248  */
249 uint32_t MXC_TMR_GetPeriod(mxc_tmr_regs_t *tmr, mxc_tmr_clock_t clock, uint32_t prescalar,
250                            uint32_t frequency);
251 
252 /**
253  * @brief   Clear the timer interrupt.
254  * @param   tmr     Pointer to timer module to initialize.
255  */
256 void MXC_TMR_ClearFlags(mxc_tmr_regs_t *tmr);
257 
258 /**
259  * @brief   Get the timer interrupt status.
260  * @param   tmr     Pointer to timer module to initialize.
261  * @return  Returns the interrupt status. 1 if interrupt has occured.
262  */
263 uint32_t MXC_TMR_GetFlags(mxc_tmr_regs_t *tmr);
264 
265 /**
266  * @brief   enable interupt
267  *
268  * @param   tmr   Pointer to timer module to initialize.
269  */
270 void MXC_TMR_EnableInt(mxc_tmr_regs_t *tmr);
271 
272 /**
273  * @brief   disable interupt
274  *
275  * @param   tmr   Pointer to timer module to initialize.
276  */
277 void MXC_TMR_DisableInt(mxc_tmr_regs_t *tmr);
278 
279 /**
280  * @brief   Enable wakeup from sleep
281  *
282  * @param   tmr   Pointer to timer module to initialize.
283  * @param   cfg   System configuration object
284  */
285 void MXC_TMR_EnableWakeup(mxc_tmr_regs_t *tmr, mxc_tmr_cfg_t *cfg);
286 
287 /**
288  * @brief   Disable wakeup from sleep
289  *
290  * @param   tmr   Pointer to timer module to initialize.
291  * @param   cfg   System configuration object
292  */
293 void MXC_TMR_DisableWakeup(mxc_tmr_regs_t *tmr, mxc_tmr_cfg_t *cfg);
294 
295 /**
296  * @brief   Set the timer compare count.
297  * @param   tmr     Pointer to timer module to initialize.
298  * @param   cmp_cnt New compare count.
299  * @note    In PWM Mode use this to set the value of the second transition.
300  */
301 void MXC_TMR_SetCompare(mxc_tmr_regs_t *tmr, uint32_t cmp_cnt);
302 
303 /**
304  * @brief   Set the timer count.
305  * @param   tmr     Pointer to timer module to initialize.
306  * @param   cnt     New count.
307  */
308 void MXC_TMR_SetCount(mxc_tmr_regs_t *tmr, uint32_t cnt);
309 
310 /**
311  * @brief   Dealay for a set periord of time measured in microseconds
312  *
313  * @param   tmr   The timer
314  * @param   us    microseconds to delay for
315  */
316 void MXC_TMR_Delay(mxc_tmr_regs_t *tmr, uint32_t us);
317 
318 /**
319  * @brief   Start a timer that will time out after a certain number of microseconds
320  * @note    This uses the 32-it Timer
321  *
322  * @param   tmr   The timer
323  * @param   us    microseconds to time out after
324  */
325 void MXC_TMR_TO_Start(mxc_tmr_regs_t *tmr, uint32_t us);
326 
327 /**
328  * @brief   Check on time out timer
329  *
330  * @param   tmr   The timer
331  *
332  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
333  */
334 int MXC_TMR_TO_Check(mxc_tmr_regs_t *tmr);
335 
336 /**
337  * @brief   Stop the Timeout timer
338  *
339  * @param   tmr   The timer
340  */
341 void MXC_TMR_TO_Stop(mxc_tmr_regs_t *tmr);
342 
343 /**
344  * @brief   Clear timeout timer back to zero
345  *
346  * @param   tmr   The timer
347  */
348 void MXC_TMR_TO_Clear(mxc_tmr_regs_t *tmr);
349 
350 /**
351  * @brief   Get elapsed time of timeout timer
352  *
353  * @param   tmr   The timer
354  *
355  * @return  Time that has elapsed in timeout timer
356  */
357 unsigned int MXC_TMR_TO_Elapsed(mxc_tmr_regs_t *tmr);
358 
359 /**
360  * @brief   Amount of time remaining until timeour
361  *
362  * @param   tmr   The timer
363  *
364  * @return  Time that is left until timeout
365  */
366 unsigned int MXC_TMR_TO_Remaining(mxc_tmr_regs_t *tmr);
367 
368 /**
369  * @brief   Start stopwatch
370  *
371  * @param   tmr      The timer
372  */
373 void MXC_TMR_SW_Start(mxc_tmr_regs_t *tmr);
374 
375 /**
376  * @brief   Stopwatch stop
377  *
378  * @param   tmr   The timer
379  *
380  * @return  the time when the stopwatch is stopped.
381  */
382 unsigned int MXC_TMR_SW_Stop(mxc_tmr_regs_t *tmr);
383 
384 /**
385  * @brief   Get time from timer
386  *
387  * @param   tmr    The timer
388  * @param   ticks  The ticks
389  * @param   time   The time
390  * @param   units  The units
391  *
392  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
393  */
394 int MXC_TMR_GetTime(mxc_tmr_regs_t *tmr, uint32_t ticks, uint32_t *time, mxc_tmr_unit_t *units);
395 
396 /**@} end of group tmr */
397 
398 #ifdef __cplusplus
399 }
400 #endif
401 
402 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32672_TMR_H_
403