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_MAX32665_TMR_H_
28 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32665_TMR_H_
29 
30 /* **** Includes **** */
31 #include "mxc_device.h"
32 #include "tmr_regs.h"
33 #include "mxc_sys.h"
34 #include "gcr_regs.h"
35 #include "mxc_delay.h"
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 /**
42  * @defgroup tmr Timer (TMR)
43  * @ingroup periphlibs
44  * @{
45  */
46 
47 /**
48  * @brief      Timer prescaler values
49  */
50 typedef enum {
51     MXC_TMR_PRES_1 = MXC_S_TMR_CN_PRES_DIV1, ///< Divide input clock by 1
52     MXC_TMR_PRES_2 = MXC_S_TMR_CN_PRES_DIV2, ///< Divide input clock by 2
53     MXC_TMR_PRES_4 = MXC_S_TMR_CN_PRES_DIV4, ///< Divide input clock by 4
54     MXC_TMR_PRES_8 = MXC_S_TMR_CN_PRES_DIV8, ///< Divide input clock by 8
55     MXC_TMR_PRES_16 = MXC_S_TMR_CN_PRES_DIV16, ///< Divide input clock by 16
56     MXC_TMR_PRES_32 = MXC_S_TMR_CN_PRES_DIV32, ///< Divide input clock by 32
57     MXC_TMR_PRES_64 = MXC_S_TMR_CN_PRES_DIV64, ///< Divide input clock by 64
58     MXC_TMR_PRES_128 = MXC_S_TMR_CN_PRES_DIV128, ///< Divide input clock by 128
59     MXC_TMR_PRES_256 = MXC_F_TMR_CN_PRES3 | MXC_S_TMR_CN_PRES_DIV1, ///< Divide input clock by 256
60     MXC_TMR_PRES_512 = MXC_F_TMR_CN_PRES3 | MXC_S_TMR_CN_PRES_DIV4, ///< Divide input clock by 512
61     MXC_TMR_PRES_1024 = MXC_F_TMR_CN_PRES3 | MXC_S_TMR_CN_PRES_DIV8, ///< Divide input clock by 1024
62     MXC_TMR_PRES_2048 = MXC_F_TMR_CN_PRES3 |
63                         MXC_S_TMR_CN_PRES_DIV16, ///< Divide input clock by 2048
64     MXC_TMR_PRES_4096 = MXC_F_TMR_CN_PRES3 |
65                         MXC_S_TMR_CN_PRES_DIV32, ///< 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_CN_TMODE_ONESHOT, ///< Timer Mode ONESHOT
88     MXC_TMR_MODE_CONTINUOUS = MXC_V_TMR_CN_TMODE_CONTINUOUS, ///< Timer Mode CONTINUOUS
89     MXC_TMR_MODE_COUNTER = MXC_V_TMR_CN_TMODE_COUNTER, ///< Timer Mode COUNTER
90     MXC_TMR_MODE_PWM = MXC_V_TMR_CN_TMODE_PWM, ///< Timer Mode PWM
91     MXC_TMR_MODE_CAPTURE = MXC_V_TMR_CN_TMODE_CAPTURE, ///< Timer Mode CAPTURE
92     MXC_TMR_MODE_COMPARE = MXC_V_TMR_CN_TMODE_COMPARE, ///< Timer Mode COMPARE
93     MXC_TMR_MODE_GATED = MXC_V_TMR_CN_TMODE_GATED, ///< Timer Mode GATED
94     MXC_TMR_MODE_CAPTURE_COMPARE = MXC_V_TMR_CN_TMODE_CAPTURECOMPARE, ///< Timer Mode CAPTURECOMPARE
95 
96     // Legacy names
97     TMR_MODE_ONESHOT = MXC_TMR_MODE_ONESHOT,
98     TMR_MODE_CONTINUOUS = MXC_TMR_MODE_CONTINUOUS,
99     TMR_MODE_COUNTER = MXC_TMR_MODE_COUNTER,
100     TMR_MODE_PWM = MXC_TMR_MODE_PWM,
101     TMR_MODE_CAPTURE = MXC_TMR_MODE_CAPTURE,
102     TMR_MODE_COMPARE = MXC_TMR_MODE_COMPARE,
103     TMR_MODE_GATED = MXC_TMR_MODE_GATED,
104     TMR_MODE_CAPTURE_COMPARE = MXC_TMR_MODE_CAPTURE_COMPARE
105 } mxc_tmr_mode_t;
106 
107 /**
108  * @brief      Timer units of time enumeration
109  */
110 typedef enum {
111     MXC_TMR_UNIT_NANOSEC = 0, ///< Nanosecond Unit Indicator
112     MXC_TMR_UNIT_MICROSEC, ///< Microsecond Unit Indicator
113     MXC_TMR_UNIT_MILLISEC, ///< Millisecond Unit Indicator
114     MXC_TMR_UNIT_SEC, ///< Second Unit Indicator
115 
116     // Legacy names
117     TMR_UNIT_NANOSEC = MXC_TMR_UNIT_NANOSEC,
118     TMR_UNIT_MICROSEC = MXC_TMR_UNIT_MICROSEC,
119     TMR_UNIT_MILLISEC = MXC_TMR_UNIT_MILLISEC,
120     TMR_UNIT_SEC = MXC_TMR_UNIT_SEC,
121 } mxc_tmr_unit_t;
122 
123 /**
124  * @brief      Timer Configuration
125  */
126 typedef struct {
127     mxc_tmr_pres_t pres; ///< Desired timer prescaler
128     mxc_tmr_mode_t mode; ///< Desired timer mode
129     uint32_t cmp_cnt; ///< Compare register value in timer ticks
130     unsigned pol; ///< Polarity (0 or 1)
131 } mxc_tmr_cfg_t;
132 
133 /* **** Definitions **** */
134 typedef void (*mxc_tmr_complete_t)(int error);
135 
136 /* **** Function Prototypes **** */
137 
138 /**
139  * @brief      Initialize timer module clock.
140  * @note       On default this function enables TMR peripheral clock and related GPIOs.
141  *             if you wish to manage clock and gpio related things in upper level instead of here.
142  *             Define MSDK_NO_GPIO_CLK_INIT flag in project.mk file.
143  *             By this flag this function will remove clock and gpio related codes from file.
144  *
145  * @param      tmr        Pointer to timer module to initialize.
146  * @param      cfg        configuration object
147  */
148 void MXC_TMR_Init(mxc_tmr_regs_t *tmr, mxc_tmr_cfg_t *cfg);
149 
150 /**
151  * @brief      Shutdown timer module clock.
152  * @param      tmr  Pointer to timer module to initialize.
153  */
154 void MXC_TMR_Shutdown(mxc_tmr_regs_t *tmr);
155 
156 /**
157  * @brief      Start the timer counting.
158  * @param      tmr  Pointer to timer module to initialize.
159  */
160 void MXC_TMR_Start(mxc_tmr_regs_t *tmr);
161 
162 /**
163  * @brief      Stop the timer.
164  * @param      tmr  Pointer to timer module to initialize.
165  */
166 void MXC_TMR_Stop(mxc_tmr_regs_t *tmr);
167 
168 /**
169  * @brief      Set the value of the first transition in PWM mode
170  * @param      tmr     Pointer to timer module to initialize.
171  * @param      pwm     New pwm count.
172  * @note       Will block until safe to change the period count.
173  * @return     E_BAD_PARAM if pwm > cnt.
174  */
175 int MXC_TMR_SetPWM(mxc_tmr_regs_t *tmr, uint32_t pwm);
176 
177 /**
178  * @brief      Get the timer compare count.
179  * @param      tmr     Pointer to timer module to initialize.
180  * @return     Returns the current compare count.
181  */
182 uint32_t MXC_TMR_GetCompare(mxc_tmr_regs_t *tmr);
183 
184 /**
185  * @brief      Get the timer capture count.
186  * @param      tmr     Pointer to timer module to initialize.
187  * @return     Returns the most recent capture count.
188  */
189 uint32_t MXC_TMR_GetCapture(mxc_tmr_regs_t *tmr);
190 
191 /**
192  * @brief      Get the timer count.
193  * @param      tmr     Pointer to timer module to initialize.
194  * @return     Returns the current count.
195  */
196 uint32_t MXC_TMR_GetCount(mxc_tmr_regs_t *tmr);
197 
198 /**
199  * @brief      Clear the timer interrupt.
200  * @param      tmr     Pointer to timer module to initialize.
201  */
202 void MXC_TMR_ClearFlags(mxc_tmr_regs_t *tmr);
203 
204 /**
205  * @brief      Get the timer interrupt status.
206  * @param      tmr     Pointer to timer module to initialize.
207  * @return     Returns the interrupt status. 1 if interrupt has occured.
208  */
209 uint32_t MXC_TMR_GetFlags(mxc_tmr_regs_t *tmr);
210 
211 /**
212  * @brief      Set the timer compare count.
213  * @param      tmr     Pointer to timer module to initialize.
214  * @param      cmp_cnt New compare count.
215  * @note       In PWM Mode use this to set the value of the second transition.
216  */
217 void MXC_TMR_SetCompare(mxc_tmr_regs_t *tmr, uint32_t cmp_cnt);
218 
219 /**
220  * @brief      Set the timer count.
221  * @param      tmr     Pointer to timer module to initialize.
222  * @param      cnt     New count.
223  */
224 void MXC_TMR_SetCount(mxc_tmr_regs_t *tmr, uint32_t cnt);
225 
226 /**
227  * @brief      Dealay for a set periord of time measured in microseconds
228  *
229  * @param      tmr   The timer
230  * @param[in]  us    microseconds to delay for
231  */
232 void MXC_TMR_Delay(mxc_tmr_regs_t *tmr, uint32_t us);
233 
234 /**
235  * @brief      Start a timer that will time out after a certain number of microseconds
236  *
237  * @param      tmr   The timer
238  * @param[in]  us    microseconds to time out after
239  */
240 void MXC_TMR_TO_Start(mxc_tmr_regs_t *tmr, uint32_t us);
241 
242 /**
243  * @brief      Check on time out timer
244  *
245  * @param      tmr   The timer
246  *
247  * @return     Returns E_TIMEOUT if timer timed out or E_NO_ERROR if it has not timed out
248  */
249 int MXC_TMR_TO_Check(mxc_tmr_regs_t *tmr);
250 
251 /**
252  * @brief      Stop the Timeout timer
253  *
254  * @param      tmr   The timer
255  */
256 void MXC_TMR_TO_Stop(mxc_tmr_regs_t *tmr);
257 
258 /**
259  * @brief      Clear timeout timer back to zero
260  *
261  * @param      tmr   The timer
262  */
263 void MXC_TMR_TO_Clear(mxc_tmr_regs_t *tmr);
264 
265 /**
266  * @brief      Get elapsed time of timeout timer
267  *
268  * @param      tmr   The timer
269  *
270  * @return     Time that has elapsed in timeout timer
271  */
272 unsigned int MXC_TMR_TO_Elapsed(mxc_tmr_regs_t *tmr);
273 
274 /**
275  * @brief      Amount of time remaining until timeour
276  *
277  * @param      tmr   The timer
278  *
279  * @return     Time that is left until timeout
280  */
281 unsigned int MXC_TMR_TO_Remaining(mxc_tmr_regs_t *tmr);
282 
283 /**
284  * @brief      Start stopwatch
285  *
286  * @param      tmr      The timer
287  */
288 void MXC_TMR_SW_Start(mxc_tmr_regs_t *tmr);
289 
290 /**
291  * @brief      Stopwatch stop
292  *
293  * @param      tmr   The timer
294  *
295  * @return     the time when the stopwatch is stopped.
296  */
297 unsigned int MXC_TMR_SW_Stop(mxc_tmr_regs_t *tmr);
298 
299 /**
300  * @brief      Get time from timer
301  *
302  * @param      tmr    The timer
303  * @param[in]  ticks  The ticks
304  * @param      time   The time
305  * @param      units  The units
306  *
307  * @return     E_NO_ERROR or error check mxc_errors.
308  */
309 int MXC_TMR_GetTime(mxc_tmr_regs_t *tmr, uint32_t ticks, uint32_t *time, mxc_tmr_unit_t *units);
310 
311 /**@} end of group tmr */
312 
313 #ifdef __cplusplus
314 }
315 #endif
316 
317 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32665_TMR_H_
318