1 /*
2  * Copyright 2017 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 #ifndef _FSL_PWT_H_
8 #define _FSL_PWT_H_
9 
10 #include "fsl_common.h"
11 
12 /*!
13  * @addtogroup pwt_driver
14  * @{
15  */
16 
17 /*******************************************************************************
18  * Definitions
19  ******************************************************************************/
20 #define FSL_PWT_DRIVER_VERSION (MAKE_VERSION(2, 0, 1)) /*!< Version 2.0.1 */
21 
22 /*! @brief PWT clock source selection */
23 typedef enum _pwt_clock_source
24 {
25     kPWT_TimerClock = 0U, /*!< The Timer clock is used as the clock source of PWT counter */
26     kPWT_AlternativeClock /*!< Alternative clock is used as the clock source of PWT counter */
27 } pwt_clock_source_t;
28 
29 /*! @brief PWT prescaler factor selection for clock source */
30 typedef enum _pwt_clock_prescale
31 {
32     kPWT_Prescale_Divide_1 = 0U, /*!< PWT clock divided by 1 */
33     kPWT_Prescale_Divide_2,      /*!< PWT clock divided by 2 */
34     kPWT_Prescale_Divide_4,      /*!< PWT clock divided by 4 */
35     kPWT_Prescale_Divide_8,      /*!< PWT clock divided by 8 */
36     kPWT_Prescale_Divide_16,     /*!< PWT clock divided by 16 */
37     kPWT_Prescale_Divide_32,     /*!< PWT clock divided by 32 */
38     kPWT_Prescale_Divide_64,     /*!< PWT clock divided by 64 */
39     kPWT_Prescale_Divide_128     /*!< PWT clock divided by 128 */
40 } pwt_clock_prescale_t;
41 
42 /*! @brief PWT Input Edge */
43 typedef enum _pwt_input_edge
44 {
45     kPWT_StartFall_CaptureFall_Edge = 0U, /*!< The first falling-edge starts the pulse width measurement, and on all the
46                                              subsequent falling edges, the pulse width is captured. */
47     kPWT_StartRise_CaptureRiseAndFall_Edge, /*!< The first rising edge starts the pulse width measurement, and on all
48                                                the subsequent rising and falling edges, the pulse width is captured. */
49     kPWT_StartFall_CaptureRiseAndFall_Edge, /*!< The first falling edge starts the pulse width measurement, and on all
50                                                the subsequent rising and falling edges, the pulse width is captured.*/
51     kPWT_StartRise_CaptureRise_Edge /*!< The first-rising edge starts the pulse width measurement, and on all the
52                                        subsequent rising edges, the pulse width is captured. */
53 } pwt_input_edge_t;
54 
55 /*! @brief PWT input port selection */
56 typedef enum _pwt_input_select
57 {
58     kPWT_InputPort_0 = 0U, /*!< PWT input comes from PWTIN[0] */
59     kPWT_InputPort_1,      /*!< PWT input comes from PWTIN[1] */
60     kPWT_InputPort_2,      /*!< PWT input comes from PWTIN[2] */
61     kPWT_InputPort_3       /*!< PWT input comes from PWTIN[3] */
62 } pwt_input_select_t;
63 
64 /*!
65  * @brief List of PWT interrupts
66  */
67 typedef enum _pwt_interrupt_enable
68 {
69     kPWT_ModuleInterruptEnable          = PWT_R1_PWTIE_MASK,  /*!< Module Interrupt */
70     kPWT_PulseWidthReadyInterruptEnable = PWT_R1_PRDYIE_MASK, /*!< Pulse width data ready interrupt */
71     kPWT_CounterOverflowInterruptEnable = PWT_R1_POVIE_MASK   /*!< Counter overflow interrupt */
72 } pwt_interrupt_enable_t;
73 
74 /*!
75  * @brief List of PWT flags
76  */
77 typedef enum _pwt_status_flags
78 {
79     kPWT_CounterOverflowFlag = PWT_R1_PWTOV_MASK,  /*!< Counter overflow flag */
80     kPWT_PulseWidthValidFlag = PWT_R1_PWTRDY_MASK, /*!< Pulse width valid flag */
81 } pwt_status_flags_t;
82 
83 /*!
84  * @brief PWT configuration structure
85  *
86  * This structure holds the configuration settings for the PWT peripheral. To initialize this
87  * structure to reasonable defaults, call the PWT_GetDefaultConfig() function and pass a
88  * pointer to the configuration structure instance.
89  *
90  * The configuration structure can be made constant so as to reside in flash.
91  */
92 typedef struct _pwt_config
93 {
94     pwt_clock_source_t clockSource; /*!< Clock source for the counter */
95     pwt_clock_prescale_t prescale;  /*!< Pre-scaler to divide down the clock */
96     pwt_input_select_t inputSelect; /*!< PWT Pulse input port selection */
97     pwt_input_edge_t edge;          /*!< PWT Input Edge */
98 } pwt_config_t;
99 
100 /*******************************************************************************
101  * API
102  ******************************************************************************/
103 
104 #if defined(__cplusplus)
105 extern "C" {
106 #endif
107 
108 /*!
109  * @name Initialization and deinitialization
110  * @{
111  */
112 
113 /*!
114  * @brief Ungates the PWT clock and configures the peripheral for basic operation.
115  *
116  * @note This API should be called at the beginning of the application using the PWT driver.
117  *
118  * @param base   PWT peripheral base address
119  * @param config Pointer to the user configuration structure.
120  */
121 void PWT_Init(PWT_Type *base, const pwt_config_t *config);
122 
123 /*!
124  * @brief Gates the PWT clock.
125  *
126  * @param base PWT peripheral base address
127  */
128 void PWT_Deinit(PWT_Type *base);
129 
130 /*!
131  * @brief  Fills in the PWT configuration structure with the default settings.
132  *
133  * The default values are:
134  * @code
135  *   config->clockSource = kPWT_TimerClock;
136  *   config->prescale = kPWT_Prescale_Divide_1;
137  *   config->inputSelect = kPWT_InputPort_0;
138  *   config->edge = kPWT_StartRise_CaptureRiseAndFall_Edge;
139  * @endcode
140  * @param config Pointer to the user configuration structure.
141  */
142 void PWT_GetDefaultConfig(pwt_config_t *config);
143 
144 /*! @}*/
145 
146 /*!
147  * @name Interrupt Interface
148  * @{
149  */
150 
151 /*!
152  * @brief Enables the selected PWT interrupts.
153  *
154  * @param base PWT peripheral base address
155  * @param mask The interrupts to enable. This is a logical OR of members of the
156  *             enumeration ::pwt_interrupt_enable_t
157  */
PWT_EnableInterrupts(PWT_Type * base,uint32_t mask)158 static inline void PWT_EnableInterrupts(PWT_Type *base, uint32_t mask)
159 {
160     base->R1 |= mask;
161 }
162 
163 /*!
164  * @brief Disables the selected PWT interrupts.
165  *
166  * @param base PWT peripheral base address
167  * @param mask The interrupts to enable. This is a logical OR of members of the
168  *             enumeration ::pwt_interrupt_enable_t
169  */
PWT_DisableInterrupts(PWT_Type * base,uint32_t mask)170 static inline void PWT_DisableInterrupts(PWT_Type *base, uint32_t mask)
171 {
172     base->R1 &= ~mask;
173 }
174 
175 /*!
176  * @brief Gets the enabled PWT interrupts.
177  *
178  * @param base PWT peripheral base address
179  *
180  * @return The enabled interrupts. This is the logical OR of members of the
181  *         enumeration ::pwt_interrupt_enable_t
182  */
PWT_GetEnabledInterrupts(PWT_Type * base)183 static inline uint32_t PWT_GetEnabledInterrupts(PWT_Type *base)
184 {
185     return (base->R1 & (PWT_R1_PWTIE_MASK | PWT_R1_PRDYIE_MASK | PWT_R1_POVIE_MASK));
186 }
187 
188 /*! @}*/
189 
190 /*!
191  * @name Status Interface
192  * @{
193  */
194 
195 /*!
196  * @brief Gets the PWT status flags.
197  *
198  * @param base PWT peripheral base address
199  *
200  * @return The status flags. This is the logical OR of members of the
201  *         enumeration ::pwt_status_flags_t
202  */
PWT_GetStatusFlags(PWT_Type * base)203 static inline uint32_t PWT_GetStatusFlags(PWT_Type *base)
204 {
205     return (base->R1 & (PWT_R1_PWTRDY_MASK | PWT_R1_PWTOV_MASK));
206 }
207 
208 /*!
209  * @brief Clears the PWT status flags.
210  *
211  * @param base PWT peripheral base address
212  * @param mask The status flags to clear. This is a logical OR of members of the
213  *             enumeration ::pwt_status_flags_t
214  */
PWT_ClearStatusFlags(PWT_Type * base,uint32_t mask)215 static inline void PWT_ClearStatusFlags(PWT_Type *base, uint32_t mask)
216 {
217     base->R1 &= ~mask;
218 }
219 
220 /*! @}*/
221 
222 /*!
223  * @name Timer Start and Stop
224  * @{
225  */
226 
227 /*!
228  * @brief Starts the PWT counter.
229  *
230  * @param base PWT peripheral base address
231  */
PWT_StartTimer(PWT_Type * base)232 static inline void PWT_StartTimer(PWT_Type *base)
233 {
234     base->R1 |= PWT_R1_PWTEN_MASK;
235 }
236 
237 /*!
238  * @brief Stops the PWT counter.
239  *
240  * @param base PWT peripheral base address
241  */
PWT_StopTimer(PWT_Type * base)242 static inline void PWT_StopTimer(PWT_Type *base)
243 {
244     base->R1 &= ~PWT_R1_PWTEN_MASK;
245 }
246 
247 /*! @}*/
248 
249 /*!
250  * @brief Reads the current counter value.
251  *
252  * This function returns the timer counting value
253  *
254  * @param base    PWT peripheral base address
255  *
256  * @return Current 16-bit timer counter value
257  */
PWT_GetCurrentTimerCount(PWT_Type * base)258 static inline uint16_t PWT_GetCurrentTimerCount(PWT_Type *base)
259 {
260     return (uint16_t)((base->R2 & PWT_R2_PWTC_MASK) >> PWT_R2_PWTC_SHIFT);
261 }
262 /*!
263  * @brief Reads the positive pulse width.
264  *
265  * This function reads the low and high registers and returns the 16-bit positive pulse width
266  *
267  * @param base PWT peripheral base address.
268  *
269  * @return The 16-bit positive pulse width.
270  */
PWT_ReadPositivePulseWidth(PWT_Type * base)271 static inline uint16_t PWT_ReadPositivePulseWidth(PWT_Type *base)
272 {
273     return (uint16_t)((base->R1 & PWT_R1_PPW_MASK) >> PWT_R1_PPW_SHIFT);
274 }
275 
276 /*!
277  * @brief Reads the negative pulse width.
278  *
279  * This function reads the low and high registers and returns the 16-bit negative pulse width
280  *
281  * @param base PWT peripheral base address.
282  *
283  * @return The 16-bit negative pulse width.
284  */
PWT_ReadNegativePulseWidth(PWT_Type * base)285 static inline uint16_t PWT_ReadNegativePulseWidth(PWT_Type *base)
286 {
287     return (uint16_t)((base->R2 & PWT_R2_NPW_MASK) >> PWT_R2_NPW_SHIFT);
288 }
289 
290 /*!
291  * @brief Performs a software reset on the PWT module.
292  *
293  * @param base PWT peripheral base address
294  */
PWT_Reset(PWT_Type * base)295 static inline void PWT_Reset(PWT_Type *base)
296 {
297     base->R1 |= PWT_R1_PWTSR_MASK;
298 }
299 
300 #if defined(__cplusplus)
301 }
302 #endif
303 
304 /*! @}*/
305 
306 #endif /* _FSL_PWT_H_*/
307