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_MAX78002_TMR_H_
28 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX78002_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 = 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_ISO_CLK = 2, /**< 60MHz Clock */
147     MXC_TMR_IBRO_CLK = 3, /**< 7.3728MHz Clock */
148     MXC_TMR_ERTCO_CLK = 4, /**< 32.768KHz Clock */
149     MXC_TMR_INRO_CLK = 5, /**< 8-30KHz 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 = MXC_TMR_ISO_CLK,
154     MXC_TMR_8M_CLK = MXC_TMR_IBRO_CLK,
155     /*32K clock can be used for Timers 0,1,2,3 and 4*/
156     MXC_TMR_32K_CLK = MXC_TMR_ERTCO_CLK,
157     /*8K and EXT clocks can only be used for Timers 4 and 5*/
158     MXC_TMR_8K_CLK = MXC_TMR_INRO_CLK,
159 } mxc_tmr_clock_t;
160 
161 /**
162  * @brief Timer Configuration
163  */
164 typedef struct {
165     mxc_tmr_pres_t pres; /**< Desired timer prescaler */
166     mxc_tmr_mode_t mode; /**< Desired timer mode */
167     mxc_tmr_bit_mode_t bitMode; /**< Desired timer bits */
168     mxc_tmr_clock_t clock; /**< Desired clock source */
169     uint32_t cmp_cnt; /**< Compare register value in timer ticks */
170     unsigned pol; /**< Polarity (0 or 1) */
171 } mxc_tmr_cfg_t;
172 
173 /* **** Definitions **** */
174 typedef void (*mxc_tmr_complete_t)(int error);
175 
176 /* **** Function Prototypes **** */
177 
178 /**
179  * @brief   Initialize timer module clock.
180  * @note    On default this function enables TMR peripheral clock and related GPIOs.
181  *          if you wish to manage clock and gpio related things in upper level instead of here.
182  *          Define MSDK_NO_GPIO_CLK_INIT flag in project.mk file.
183  *          By this flag this function will remove clock and gpio related codes from file.
184  *
185  * @param   tmr        Pointer to timer module to initialize.
186  * @param   cfg        System configuration object
187  * @param   init_pins  True will initialize pins corresponding to the TMR and False will not if pins are pinned out otherwise it will not
188  *                     be used, has no effect incase of MSDK_NO_GPIO_CLK_INIT has been defined.
189  *
190  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
191  */
192 int MXC_TMR_Init(mxc_tmr_regs_t *tmr, mxc_tmr_cfg_t *cfg, bool init_pins);
193 
194 /**
195  * @brief   Shutdown timer module clock.
196  * @param   tmr  Pointer to timer module to initialize.
197  */
198 void MXC_TMR_Shutdown(mxc_tmr_regs_t *tmr);
199 
200 /**
201  * @brief   Start the timer counting.
202  * @param   tmr  Pointer to timer module to initialize.
203  */
204 void MXC_TMR_Start(mxc_tmr_regs_t *tmr);
205 
206 /**
207  * @brief   Stop the timer.
208  * @param   tmr  Pointer to timer module to initialize.
209  */
210 void MXC_TMR_Stop(mxc_tmr_regs_t *tmr);
211 
212 /**
213  * @brief   Set the value of the first transition in PWM mode
214  * @param   tmr     Pointer to timer module to initialize.
215  * @param   pwm     New pwm count.
216  * @note    Will block until safe to change the period count.
217  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
218  */
219 int MXC_TMR_SetPWM(mxc_tmr_regs_t *tmr, uint32_t pwm);
220 
221 /**
222  * @brief   Get the timer compare count.
223  * @param   tmr     Pointer to timer module to initialize.
224  * @return  Returns the current compare count.
225  */
226 uint32_t MXC_TMR_GetCompare(mxc_tmr_regs_t *tmr);
227 
228 /**
229  * @brief   Get the timer capture count.
230  * @param   tmr     Pointer to timer module to initialize.
231  * @return  Returns the most recent capture count.
232  */
233 uint32_t MXC_TMR_GetCapture(mxc_tmr_regs_t *tmr);
234 
235 /**
236  * @brief   Get the timer count.
237  * @param   tmr     Pointer to timer module to initialize.
238  * @return  Returns the current count.
239  */
240 uint32_t MXC_TMR_GetCount(mxc_tmr_regs_t *tmr);
241 
242 /**
243  * @brief   Calculate count for required frequency.
244  * @param   tmr         Timer
245  * @param   clock       Clock source.
246  * @param   prescalar   prescalar
247  * @param   frequency   required frequency.
248  * @return  Returns the period count.
249  */
250 uint32_t MXC_TMR_GetPeriod(mxc_tmr_regs_t *tmr, mxc_tmr_clock_t clock, uint32_t prescalar,
251                            uint32_t frequency);
252 
253 /**
254  * @brief   Clear the timer interrupt.
255  * @param   tmr     Pointer to timer module to initialize.
256  */
257 void MXC_TMR_ClearFlags(mxc_tmr_regs_t *tmr);
258 
259 /**
260  * @brief   Get the timer interrupt status.
261  * @param   tmr     Pointer to timer module to initialize.
262  * @return  Returns the interrupt status. 1 if interrupt has occured.
263  */
264 uint32_t MXC_TMR_GetFlags(mxc_tmr_regs_t *tmr);
265 
266 /**
267  * @brief   enable interupt
268  *
269  * @param   tmr   Pointer to timer module to initialize.
270  */
271 void MXC_TMR_EnableInt(mxc_tmr_regs_t *tmr);
272 
273 /**
274  * @brief   disable interupt
275  *
276  * @param   tmr   Pointer to timer module to initialize.
277  */
278 void MXC_TMR_DisableInt(mxc_tmr_regs_t *tmr);
279 
280 /**
281  * @brief   Enable wakeup from sleep
282  *
283  * @param   tmr   Pointer to timer module to initialize.
284  * @param   cfg   System configuration object
285  */
286 void MXC_TMR_EnableWakeup(mxc_tmr_regs_t *tmr, mxc_tmr_cfg_t *cfg);
287 
288 /**
289  * @brief   Disable wakeup from sleep
290  *
291  * @param   tmr   Pointer to timer module to initialize.
292  * @param   cfg   System configuration object
293  */
294 void MXC_TMR_DisableWakeup(mxc_tmr_regs_t *tmr, mxc_tmr_cfg_t *cfg);
295 
296 /**
297  * @brief   Set the timer compare count.
298  * @param   tmr     Pointer to timer module to initialize.
299  * @param   cmp_cnt New compare count.
300  * @note    In PWM Mode use this to set the value of the second transition.
301  */
302 void MXC_TMR_SetCompare(mxc_tmr_regs_t *tmr, uint32_t cmp_cnt);
303 
304 /**
305  * @brief   Set the timer count.
306  * @param   tmr     Pointer to timer module to initialize.
307  * @param   cnt     New count.
308  */
309 void MXC_TMR_SetCount(mxc_tmr_regs_t *tmr, uint32_t cnt);
310 
311 /**
312  * @brief   Dealay for a set periord of time measured in microseconds
313  *
314  * @param   tmr   The timer
315  * @param   us    microseconds to delay for
316  */
317 void MXC_TMR_Delay(mxc_tmr_regs_t *tmr, uint32_t us);
318 
319 /**
320  * @brief   Start a timer that will time out after a certain number of microseconds
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 /**
397  * @brief   Get ticks from timer
398  *
399  * @param   tmr    The timer
400  * @param   time   The time
401  * @param   units  The units
402  * @param   ticks  The ticks
403  *
404  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
405  */
406 int MXC_TMR_GetTicks(mxc_tmr_regs_t *tmr, uint32_t time, mxc_tmr_unit_t units, uint32_t *ticks);
407 
408 /**@} end of group tmr */
409 
410 #ifdef __cplusplus
411 }
412 #endif
413 
414 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX78002_TMR_H_
415