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