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