1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2019 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 #ifndef _FSL_QTMR_H_
9 #define _FSL_QTMR_H_
10 
11 #include "fsl_common.h"
12 
13 /*!
14  * @addtogroup qtmr
15  * @{
16  */
17 
18 /*******************************************************************************
19  * Definitions
20  ******************************************************************************/
21 
22 /*! @name Driver version */
23 /*@{*/
24 #define FSL_QTMR_DRIVER_VERSION (MAKE_VERSION(2, 0, 1)) /*!< Version. */
25 /*@}*/
26 
27 /*! @brief Quad Timer primary clock source selection*/
28 typedef enum _qtmr_primary_count_source
29 {
30     kQTMR_ClockCounter0InputPin = 0, /*!< Use counter 0 input pin */
31     kQTMR_ClockCounter1InputPin,     /*!< Use counter 1 input pin */
32     kQTMR_ClockCounter2InputPin,     /*!< Use counter 2 input pin */
33     kQTMR_ClockCounter3InputPin,     /*!< Use counter 3 input pin */
34     kQTMR_ClockCounter0Output,       /*!< Use counter 0 output */
35     kQTMR_ClockCounter1Output,       /*!< Use counter 1 output */
36     kQTMR_ClockCounter2Output,       /*!< Use counter 2 output */
37     kQTMR_ClockCounter3Output,       /*!< Use counter 3 output */
38     kQTMR_ClockDivide_1,             /*!< IP bus clock divide by 1 prescaler */
39     kQTMR_ClockDivide_2,             /*!< IP bus clock divide by 2 prescaler */
40     kQTMR_ClockDivide_4,             /*!< IP bus clock divide by 4 prescaler */
41     kQTMR_ClockDivide_8,             /*!< IP bus clock divide by 8 prescaler */
42     kQTMR_ClockDivide_16,            /*!< IP bus clock divide by 16 prescaler */
43     kQTMR_ClockDivide_32,            /*!< IP bus clock divide by 32 prescaler */
44     kQTMR_ClockDivide_64,            /*!< IP bus clock divide by 64 prescaler */
45     kQTMR_ClockDivide_128            /*!< IP bus clock divide by 128 prescaler */
46 } qtmr_primary_count_source_t;
47 
48 /*! @brief Quad Timer input sources selection*/
49 typedef enum _qtmr_input_source
50 {
51     kQTMR_Counter0InputPin = 0, /*!< Use counter 0 input pin */
52     kQTMR_Counter1InputPin,     /*!< Use counter 1 input pin */
53     kQTMR_Counter2InputPin,     /*!< Use counter 2 input pin */
54     kQTMR_Counter3InputPin      /*!< Use counter 3 input pin */
55 } qtmr_input_source_t;
56 
57 /*! @brief Quad Timer counting mode selection */
58 typedef enum _qtmr_counting_mode
59 {
60     kQTMR_NoOperation = 0,          /*!< No operation */
61     kQTMR_PriSrcRiseEdge,           /*!< Count rising edges of primary source */
62     kQTMR_PriSrcRiseAndFallEdge,    /*!< Count rising and falling edges of primary source */
63     kQTMR_PriSrcRiseEdgeSecInpHigh, /*!< Count rise edges of pri SRC while sec inp high active */
64     kQTMR_QuadCountMode,            /*!< Quadrature count mode, uses pri and sec sources */
65     kQTMR_PriSrcRiseEdgeSecDir,     /*!< Count rising edges of pri SRC; sec SRC specifies dir */
66     kQTMR_SecSrcTrigPriCnt,         /*!< Edge of sec SRC trigger primary count until compare*/
67     kQTMR_CascadeCount              /*!< Cascaded count mode (up/down) */
68 } qtmr_counting_mode_t;
69 
70 /*! @brief Quad Timer output mode selection*/
71 typedef enum _qtmr_output_mode
72 {
73     kQTMR_AssertWhenCountActive = 0,    /*!< Assert OFLAG while counter is active*/
74     kQTMR_ClearOnCompare,               /*!< Clear OFLAG on successful compare */
75     kQTMR_SetOnCompare,                 /*!< Set OFLAG on successful compare */
76     kQTMR_ToggleOnCompare,              /*!< Toggle OFLAG on successful compare */
77     kQTMR_ToggleOnAltCompareReg,        /*!< Toggle OFLAG using alternating compare registers */
78     kQTMR_SetOnCompareClearOnSecSrcInp, /*!< Set OFLAG on compare, clear on sec SRC input edge */
79     kQTMR_SetOnCompareClearOnCountRoll, /*!< Set OFLAG on compare, clear on counter rollover */
80     kQTMR_EnableGateClock               /*!< Enable gated clock output while count is active */
81 } qtmr_output_mode_t;
82 
83 /*! @brief Quad Timer input capture edge mode, rising edge, or falling edge */
84 typedef enum _qtmr_input_capture_edge
85 {
86     kQTMR_NoCapture = 0,       /*!< Capture is disabled */
87     kQTMR_RisingEdge,          /*!< Capture on rising edge (IPS=0) or falling edge (IPS=1)*/
88     kQTMR_FallingEdge,         /*!< Capture on falling edge (IPS=0) or rising edge (IPS=1)*/
89     kQTMR_RisingAndFallingEdge /*!< Capture on both edges */
90 } qtmr_input_capture_edge_t;
91 
92 /*! @brief Quad Timer input capture edge mode, rising edge, or falling edge */
93 typedef enum _qtmr_preload_control
94 {
95     kQTMR_NoPreload = 0, /*!< Never preload */
96     kQTMR_LoadOnComp1,   /*!< Load upon successful compare with value in COMP1 */
97     kQTMR_LoadOnComp2    /*!< Load upon successful compare with value in COMP2*/
98 } qtmr_preload_control_t;
99 
100 /*! @brief List of Quad Timer run options when in Debug mode */
101 typedef enum _qtmr_debug_action
102 {
103     kQTMR_RunNormalInDebug = 0U, /*!< Continue with normal operation */
104     kQTMR_HaltCounter,           /*!< Halt counter */
105     kQTMR_ForceOutToZero,        /*!< Force output to logic 0 */
106     kQTMR_HaltCountForceOutZero  /*!< Halt counter and force output to logic 0 */
107 } qtmr_debug_action_t;
108 
109 /*! @brief List of Quad Timer interrupts */
110 typedef enum _qtmr_interrupt_enable
111 {
112     kQTMR_CompareInterruptEnable  = (1U << 0), /*!< Compare interrupt.*/
113     kQTMR_Compare1InterruptEnable = (1U << 1), /*!< Compare 1 interrupt.*/
114     kQTMR_Compare2InterruptEnable = (1U << 2), /*!< Compare 2 interrupt.*/
115     kQTMR_OverflowInterruptEnable = (1U << 3), /*!< Timer overflow interrupt.*/
116     kQTMR_EdgeInterruptEnable     = (1U << 4)  /*!< Input edge interrupt.*/
117 } qtmr_interrupt_enable_t;
118 
119 /*! @brief List of Quad Timer flags */
120 typedef enum _qtmr_status_flags
121 {
122     kQTMR_CompareFlag  = (1U << 0), /*!< Compare flag */
123     kQTMR_Compare1Flag = (1U << 1), /*!< Compare 1 flag */
124     kQTMR_Compare2Flag = (1U << 2), /*!< Compare 2 flag */
125     kQTMR_OverflowFlag = (1U << 3), /*!< Timer overflow flag */
126     kQTMR_EdgeFlag     = (1U << 4)  /*!< Input edge flag */
127 } qtmr_status_flags_t;
128 
129 /*!
130  * @brief Quad Timer config structure
131  *
132  * This structure holds the configuration settings for the Quad Timer peripheral. To initialize this
133  * structure to reasonable defaults, call the QTMR_GetDefaultConfig() function and pass a
134  * pointer to your config structure instance.
135  *
136  * The config struct can be made const so it resides in flash
137  */
138 typedef struct _qtmr_config
139 {
140     qtmr_primary_count_source_t primarySource; /*!< Specify the primary count source */
141     qtmr_input_source_t secondarySource;       /*!< Specify the secondary count source */
142     bool enableMasterMode;                     /*!< true: Broadcast compare function output to other counters;
143                                                     false no broadcast */
144     bool enableExternalForce;                  /*!< true: Compare from another counter force state of OFLAG signal
145                                                     false: OFLAG controlled by local counter */
146     uint8_t faultFilterCount;                  /*!< Fault filter count */
147     uint8_t faultFilterPeriod;                 /*!< Fault filter period;value of 0 will bypass the filter */
148     qtmr_debug_action_t debugMode;             /*!< Operation in Debug mode */
149 } qtmr_config_t;
150 
151 /*******************************************************************************
152  * API
153  ******************************************************************************/
154 
155 #if defined(__cplusplus)
156 extern "C" {
157 #endif
158 
159 /*!
160  * @name Initialization and deinitialization
161  * @{
162  */
163 
164 /*!
165  * @brief Ungates the Quad Timer clock and configures the peripheral for basic operation.
166  *
167  * @note This API should be called at the beginning of the application using the Quad Timer driver.
168  *
169  * @param base   Quad Timer peripheral base address
170  * @param config Pointer to user's Quad Timer config structure
171  */
172 void QTMR_Init(TMR_Type *base, const qtmr_config_t *config);
173 
174 /*!
175  * @brief Stops the counter and gates the Quad Timer clock
176  *
177  * @param base Quad Timer peripheral base address
178  */
179 void QTMR_Deinit(TMR_Type *base);
180 
181 /*!
182  * @brief  Fill in the Quad Timer config struct with the default settings
183  *
184  * The default values are:
185  * @code
186  *    config->debugMode = kQTMR_RunNormalInDebug;
187  *    config->enableExternalForce = false;
188  *    config->enableMasterMode = false;
189  *    config->faultFilterCount = 0;
190  *    config->faultFilterPeriod = 0;
191  *    config->primarySource = kQTMR_ClockDivide_2;
192  *    config->secondarySource = kQTMR_Counter0InputPin;
193  * @endcode
194  * @param config Pointer to user's Quad Timer config structure.
195  */
196 void QTMR_GetDefaultConfig(qtmr_config_t *config);
197 
198 /*! @}*/
199 
200 /*!
201  * @brief Sets up Quad timer module for PWM signal output.
202  *
203  * The function initializes the timer module according to the parameters passed in by the user. The
204  * function also sets up the value compare registers to match the PWM signal requirements.
205  *
206  * @param base             Quad Timer peripheral base address
207  * @param pwmFreqHz        PWM signal frequency in Hz
208  * @param dutyCyclePercent PWM pulse width, value should be between 0 to 100
209  *                         0=inactive signal(0% duty cycle)...
210  *                         100=active signal (100% duty cycle)
211  * @param outputPolarity   true: invert polarity of the output signal, false: no inversion
212  * @param srcClock_Hz      Main counter clock in Hz.
213  *
214  * @return Returns an error if there was error setting up the signal.
215  */
216 status_t QTMR_SetupPwm(
217     TMR_Type *base, uint32_t pwmFreqHz, uint8_t dutyCyclePercent, bool outputPolarity, uint32_t srcClock_Hz);
218 
219 /*!
220  * @brief Allows the user to count the source clock cycles until a capture event arrives.
221  *
222  * The count is stored in the capture register.
223  *
224  * @param base            Quad Timer peripheral base address
225  * @param capturePin      Pin through which we receive the input signal to trigger the capture
226  * @param inputPolarity   true: invert polarity of the input signal, false: no inversion
227  * @param reloadOnCapture true: reload the counter when an input capture occurs, false: no reload
228  * @param captureMode     Specifies which edge of the input signal  triggers a capture
229  */
230 void QTMR_SetupInputCapture(TMR_Type *base,
231                             qtmr_input_source_t capturePin,
232                             bool inputPolarity,
233                             bool reloadOnCapture,
234                             qtmr_input_capture_edge_t captureMode);
235 
236 /*!
237  * @name Interrupt Interface
238  * @{
239  */
240 
241 /*!
242  * @brief Enables the selected Quad Timer interrupts
243  *
244  * @param base Quad Timer peripheral base address
245  * @param mask The interrupts to enable. This is a logical OR of members of the
246  *             enumeration ::qtmr_interrupt_enable_t
247  */
248 void QTMR_EnableInterrupts(TMR_Type *base, uint32_t mask);
249 
250 /*!
251  * @brief Disables the selected Quad Timer interrupts
252  *
253  * @param base Quad Timer peripheral base address
254  * @param mask The interrupts to enable. This is a logical OR of members of the
255  *             enumeration ::qtmr_interrupt_enable_t
256  */
257 void QTMR_DisableInterrupts(TMR_Type *base, uint32_t mask);
258 
259 /*!
260  * @brief Gets the enabled Quad Timer interrupts
261  *
262  * @param base Quad Timer peripheral base address
263  *
264  * @return The enabled interrupts. This is the logical OR of members of the
265  *         enumeration ::qtmr_interrupt_enable_t
266  */
267 uint32_t QTMR_GetEnabledInterrupts(TMR_Type *base);
268 
269 /*! @}*/
270 
271 /*!
272  * @name Status Interface
273  * @{
274  */
275 
276 /*!
277  * @brief Gets the Quad Timer status flags
278  *
279  * @param base Quad Timer peripheral base address
280  *
281  * @return The status flags. This is the logical OR of members of the
282  *         enumeration ::qtmr_status_flags_t
283  */
284 uint32_t QTMR_GetStatus(TMR_Type *base);
285 
286 /*!
287  * @brief Clears the Quad Timer status flags.
288  *
289  * @param base Quad Timer peripheral base address
290  * @param mask The status flags to clear. This is a logical OR of members of the
291  *             enumeration ::qtmr_status_flags_t
292  */
293 void QTMR_ClearStatusFlags(TMR_Type *base, uint32_t mask);
294 
295 /*! @}*/
296 
297 /*!
298  * @name Read and Write the timer period
299  * @{
300  */
301 
302 /*!
303  * @brief Sets the timer period in ticks.
304  *
305  * Timers counts from initial value till it equals the count value set here. The counter
306  * will then reinitialize to the value specified in the Load register.
307  *
308  * @note
309  * 1. This function will write the time period in ticks to COMP1 or COMP2 register
310  *    depending on the count direction
311  * 2. User can call the utility macros provided in fsl_common.h to convert to ticks
312  * 3. This function supports cases, providing only primary source clock without secondary source clock.
313  *
314  * @param base  Quad Timer peripheral base address
315  * @param ticks Timer period in units of ticks
316  */
317 void QTMR_SetTimerPeriod(TMR_Type *base, uint16_t ticks);
318 
319 /*!
320  * @brief Reads the current timer counting value.
321  *
322  * This function returns the real-time timer counting value, in a range from 0 to a
323  * timer period.
324  *
325  * @note User can call the utility macros provided in fsl_common.h to convert ticks to usec or msec
326  *
327  * @param base Quad Timer peripheral base address
328  *
329  * @return Current counter value in ticks
330  */
QTMR_GetCurrentTimerCount(TMR_Type * base)331 static inline uint16_t QTMR_GetCurrentTimerCount(TMR_Type *base)
332 {
333     return base->CNTR;
334 }
335 
336 /*! @}*/
337 
338 /*!
339  * @name Timer Start and Stop
340  * @{
341  */
342 
343 /*!
344  * @brief Starts the Quad Timer counter.
345  *
346  *
347  * @param base        Quad Timer peripheral base address
348  * @param clockSource Quad Timer clock source
349  */
QTMR_StartTimer(TMR_Type * base,qtmr_counting_mode_t clockSource)350 static inline void QTMR_StartTimer(TMR_Type *base, qtmr_counting_mode_t clockSource)
351 {
352     uint16_t reg = base->CTRL;
353 
354     reg &= ~(uint16_t)TMR_CTRL_CM_MASK;
355     reg |= TMR_CTRL_CM(clockSource);
356     base->CTRL = reg;
357 }
358 
359 /*!
360  * @brief Stops the Quad Timer counter.
361  *
362  * @param base Quad Timer peripheral base address
363  */
QTMR_StopTimer(TMR_Type * base)364 static inline void QTMR_StopTimer(TMR_Type *base)
365 {
366     base->CTRL &= ~(uint16_t)TMR_CTRL_CM_MASK;
367 }
368 
369 /*! @}*/
370 
371 #if defined(__cplusplus)
372 }
373 #endif
374 
375 /*! @}*/
376 
377 #endif /* _FSL_QTMR_H_ */
378