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