1 /*
2 * Copyright (c) 2015, Freescale Semiconductor, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * o Redistributions of source code must retain the above copyright notice, this list
9 * of conditions and the following disclaimer.
10 *
11 * o Redistributions in binary form must reproduce the above copyright notice, this
12 * list of conditions and the following disclaimer in the documentation and/or
13 * other materials provided with the distribution.
14 *
15 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from this
17 * software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #ifndef __GPT_H__
32 #define __GPT_H__
33
34 #include <stdint.h>
35 #include <stdbool.h>
36 #include <assert.h>
37 #include "device_imx.h"
38
39 /*!
40 * @addtogroup gpt_driver
41 * @{
42 */
43
44 /*******************************************************************************
45 * Definitions
46 ******************************************************************************/
47
48 /*! @brief Clock source. */
49 enum _gpt_clock_source
50 {
51 gptClockSourceNone = 0U, /*!< No source selected.*/
52 gptClockSourcePeriph = 1U, /*!< Use peripheral module clock.*/
53 gptClockSourceLowFreq = 4U, /*!< Use 32 K clock.*/
54 gptClockSourceOsc = 5U, /*!< Use 24 M OSC clock.*/
55 };
56
57 /*! @brief Input capture channel number. */
58 enum _gpt_input_capture_channel
59 {
60 gptInputCaptureChannel1 = 0U, /*!< Input Capture Channel1.*/
61 gptInputCaptureChannel2 = 1U, /*!< Input Capture Channel2.*/
62 };
63
64 /*! @brief Input capture operation mode. */
65 enum _gpt_input_operation_mode
66 {
67 gptInputOperationDisabled = 0U, /*!< Don't capture.*/
68 gptInputOperationRiseEdge = 1U, /*!< Capture on rising edge of input pin.*/
69 gptInputOperationFallEdge = 2U, /*!< Capture on falling edge of input pin.*/
70 gptInputOperationBothEdge = 3U, /*!< Capture on both edges of input pin.*/
71 };
72
73 /*! @brief Output compare channel number. */
74 enum _gpt_output_compare_channel
75 {
76 gptOutputCompareChannel1 = 0U, /*!< Output Compare Channel1.*/
77 gptOutputCompareChannel2 = 1U, /*!< Output Compare Channel2.*/
78 gptOutputCompareChannel3 = 2U, /*!< Output Compare Channel3.*/
79 };
80
81 /*! @brief Output compare operation mode. */
82 enum _gpt_output_operation_mode
83 {
84 gptOutputOperationDisconnected = 0U, /*!< Don't change output pin.*/
85 gptOutputOperationToggle = 1U, /*!< Toggle output pin.*/
86 gptOutputOperationClear = 2U, /*!< Set output pin low.*/
87 gptOutputOperationSet = 3U, /*!< Set output pin high.*/
88 gptOutputOperationActivelow = 4U, /*!< Generate a active low pulse on output pin.*/
89 };
90
91 /*! @brief Status flag. */
92 enum _gpt_status_flag
93 {
94 gptStatusFlagOutputCompare1 = 1U << 0, /*!< Output compare channel 1 event.*/
95 gptStatusFlagOutputCompare2 = 1U << 1, /*!< Output compare channel 2 event.*/
96 gptStatusFlagOutputCompare3 = 1U << 2, /*!< Output compare channel 3 event.*/
97 gptStatusFlagInputCapture1 = 1U << 3, /*!< Capture channel 1 event.*/
98 gptStatusFlagInputCapture2 = 1U << 4, /*!< Capture channel 2 event.*/
99 gptStatusFlagRollOver = 1U << 5, /*!< Counter reaches maximum value and rolled over to 0 event.*/
100 };
101
102 /*! @brief Structure to configure the running mode. */
103 typedef struct _gpt_init_config
104 {
105 bool freeRun; /*!< true: FreeRun mode, false: Restart mode. */
106 bool waitEnable; /*!< GPT enabled in wait mode. */
107 bool stopEnable; /*!< GPT enabled in stop mode. */
108 bool dozeEnable; /*!< GPT enabled in doze mode. */
109 bool dbgEnable; /*!< GPT enabled in debug mode. */
110 bool enableMode; /*!< true: counter reset to 0 when enabled, false: counter retain its value when enabled. */
111 } gpt_init_config_t;
112
113 /*******************************************************************************
114 * API
115 ******************************************************************************/
116
117 #if defined(__cplusplus)
118 extern "C" {
119 #endif
120
121 /*!
122 * @name GPT State Control
123 * @{
124 */
125
126 /*!
127 * @brief Initialize GPT to reset state and initialize running mode.
128 *
129 * @param base GPT base pointer.
130 * @param initConfig GPT mode setting configuration.
131 */
132 void GPT_Init(GPT_Type* base, const gpt_init_config_t* initConfig);
133
134 /*!
135 * @brief Software reset of GPT module.
136 *
137 * @param base GPT base pointer.
138 */
GPT_SoftReset(GPT_Type * base)139 static inline void GPT_SoftReset(GPT_Type* base)
140 {
141 base->CR |= GPT_CR_SWR_MASK;
142 /* Wait reset finished. */
143 while (base->CR & GPT_CR_SWR_MASK) {};
144 }
145
146 /*!
147 * @brief Set clock source of GPT.
148 *
149 * @param base GPT base pointer.
150 * @param source Clock source (see @ref _gpt_clock_source enumeration).
151 */
152 void GPT_SetClockSource(GPT_Type* base, uint32_t source);
153
154 /*!
155 * @brief Get clock source of GPT.
156 *
157 * @param base GPT base pointer.
158 * @return clock source (see @ref _gpt_clock_source enumeration).
159 */
GPT_GetClockSource(GPT_Type * base)160 static inline uint32_t GPT_GetClockSource(GPT_Type* base)
161 {
162 return (base->CR & GPT_CR_CLKSRC_MASK) >> GPT_CR_CLKSRC_SHIFT;
163 }
164
165 /*!
166 * @brief Set pre scaler of GPT.
167 *
168 * @param base GPT base pointer.
169 * @param prescaler Pre-scaler of GPT (0-4095, divider = prescaler + 1).
170 */
GPT_SetPrescaler(GPT_Type * base,uint32_t prescaler)171 static inline void GPT_SetPrescaler(GPT_Type* base, uint32_t prescaler)
172 {
173 assert(prescaler <= GPT_PR_PRESCALER_MASK);
174
175 base->PR = (base->PR & ~GPT_PR_PRESCALER_MASK) | GPT_PR_PRESCALER(prescaler);
176 }
177
178 /*!
179 * @brief Get pre scaler of GPT.
180 *
181 * @param base GPT base pointer.
182 * @return pre scaler of GPT (0-4095).
183 */
GPT_GetPrescaler(GPT_Type * base)184 static inline uint32_t GPT_GetPrescaler(GPT_Type* base)
185 {
186 return (base->PR & GPT_PR_PRESCALER_MASK) >> GPT_PR_PRESCALER_SHIFT;
187 }
188
189 /*!
190 * @brief OSC 24M pre-scaler before selected by clock source.
191 *
192 * @param base GPT base pointer.
193 * @param prescaler OSC pre-scaler(0-15, divider = prescaler + 1).
194 */
GPT_SetOscPrescaler(GPT_Type * base,uint32_t prescaler)195 static inline void GPT_SetOscPrescaler(GPT_Type* base, uint32_t prescaler)
196 {
197 assert(prescaler <= (GPT_PR_PRESCALER24M_MASK >> GPT_PR_PRESCALER24M_SHIFT));
198
199 base->PR = (base->PR & ~GPT_PR_PRESCALER24M_MASK) | GPT_PR_PRESCALER24M(prescaler);
200 }
201
202 /*!
203 * @brief Get pre-scaler of GPT.
204 *
205 * @param base GPT base pointer.
206 * @return OSC pre scaler of GPT (0-15).
207 */
GPT_GetOscPrescaler(GPT_Type * base)208 static inline uint32_t GPT_GetOscPrescaler(GPT_Type* base)
209 {
210 return (base->PR & GPT_PR_PRESCALER24M_MASK) >> GPT_PR_PRESCALER24M_SHIFT;
211 }
212
213 /*!
214 * @brief Enable GPT module.
215 *
216 * @param base GPT base pointer.
217 */
GPT_Enable(GPT_Type * base)218 static inline void GPT_Enable(GPT_Type* base)
219 {
220 base->CR |= GPT_CR_EN_MASK;
221 }
222
223 /*!
224 * @brief Disable GPT module.
225 *
226 * @param base GPT base pointer.
227 */
GPT_Disable(GPT_Type * base)228 static inline void GPT_Disable(GPT_Type* base)
229 {
230 base->CR &= ~GPT_CR_EN_MASK;
231 }
232
233 /*!
234 * @brief Get GPT counter value.
235 *
236 * @param base GPT base pointer.
237 * @return GPT counter value.
238 */
GPT_ReadCounter(GPT_Type * base)239 static inline uint32_t GPT_ReadCounter(GPT_Type* base)
240 {
241 return base->CNT;
242 }
243
244 /*@}*/
245
246 /*!
247 * @name GPT Input/Output Signal Control
248 * @{
249 */
250
251 /*!
252 * @brief Set GPT operation mode of input capture channel.
253 *
254 * @param base GPT base pointer.
255 * @param channel GPT capture channel (see @ref _gpt_input_capture_channel enumeration).
256 * @param mode GPT input capture operation mode (see @ref _gpt_input_operation_mode enumeration).
257 */
GPT_SetInputOperationMode(GPT_Type * base,uint32_t channel,uint32_t mode)258 static inline void GPT_SetInputOperationMode(GPT_Type* base, uint32_t channel, uint32_t mode)
259 {
260 assert (channel <= gptInputCaptureChannel2);
261
262 base->CR = (base->CR & ~(GPT_CR_IM1_MASK << (channel * 2))) | (GPT_CR_IM1(mode) << (channel * 2));
263 }
264
265 /*!
266 * @brief Get GPT operation mode of input capture channel.
267 *
268 * @param base GPT base pointer.
269 * @param channel GPT capture channel (see @ref _gpt_input_capture_channel enumeration).
270 * @return GPT input capture operation mode (see @ref _gpt_input_operation_mode enumeration).
271 */
GPT_GetInputOperationMode(GPT_Type * base,uint32_t channel)272 static inline uint32_t GPT_GetInputOperationMode(GPT_Type* base, uint32_t channel)
273 {
274 assert (channel <= gptInputCaptureChannel2);
275
276 return (base->CR >> (GPT_CR_IM1_SHIFT + channel * 2)) & (GPT_CR_IM1_MASK >> GPT_CR_IM1_SHIFT);
277 }
278
279 /*!
280 * @brief Get GPT input capture value of certain channel.
281 *
282 * @param base GPT base pointer.
283 * @param channel GPT capture channel (see @ref _gpt_input_capture_channel enumeration).
284 * @return GPT input capture value.
285 */
GPT_GetInputCaptureValue(GPT_Type * base,uint32_t channel)286 static inline uint32_t GPT_GetInputCaptureValue(GPT_Type* base, uint32_t channel)
287 {
288 assert (channel <= gptInputCaptureChannel2);
289
290 return *(&base->ICR1 + channel);
291 }
292
293 /*!
294 * @brief Set GPT operation mode of output compare channel.
295 *
296 * @param base GPT base pointer.
297 * @param channel GPT output compare channel (see @ref _gpt_output_compare_channel enumeration).
298 * @param mode GPT output operation mode (see @ref _gpt_output_operation_mode enumeration).
299 */
GPT_SetOutputOperationMode(GPT_Type * base,uint32_t channel,uint32_t mode)300 static inline void GPT_SetOutputOperationMode(GPT_Type* base, uint32_t channel, uint32_t mode)
301 {
302 assert (channel <= gptOutputCompareChannel3);
303
304 base->CR = (base->CR & ~(GPT_CR_OM1_MASK << (channel * 3))) | (GPT_CR_OM1(mode) << (channel * 3));
305 }
306
307 /*!
308 * @brief Get GPT operation mode of output compare channel.
309 *
310 * @param base GPT base pointer.
311 * @param channel GPT output compare channel (see @ref _gpt_output_compare_channel enumeration).
312 * @return GPT output operation mode (see @ref _gpt_output_operation_mode enumeration).
313 */
GPT_GetOutputOperationMode(GPT_Type * base,uint32_t channel)314 static inline uint32_t GPT_GetOutputOperationMode(GPT_Type* base, uint32_t channel)
315 {
316 assert (channel <= gptOutputCompareChannel3);
317
318 return (base->CR >> (GPT_CR_OM1_SHIFT + channel * 3)) & (GPT_CR_OM1_MASK >> GPT_CR_OM1_SHIFT);
319 }
320
321 /*!
322 * @brief Set GPT output compare value of output compare channel.
323 *
324 * @param base GPT base pointer.
325 * @param channel GPT output compare channel (see @ref _gpt_output_compare_channel enumeration).
326 * @param value GPT output compare value.
327 */
GPT_SetOutputCompareValue(GPT_Type * base,uint32_t channel,uint32_t value)328 static inline void GPT_SetOutputCompareValue(GPT_Type* base, uint32_t channel, uint32_t value)
329 {
330 assert (channel <= gptOutputCompareChannel3);
331
332 *(&base->OCR1 + channel) = value;
333 }
334
335 /*!
336 * @brief Get GPT output compare value of output compare channel.
337 *
338 * @param base GPT base pointer.
339 * @param channel GPT output compare channel (see @ref _gpt_output_compare_channel enumeration).
340 * @return GPT output compare value.
341 */
GPT_GetOutputCompareValue(GPT_Type * base,uint32_t channel)342 static inline uint32_t GPT_GetOutputCompareValue(GPT_Type* base, uint32_t channel)
343 {
344 assert (channel <= gptOutputCompareChannel3);
345
346 return *(&base->OCR1 + channel);
347 }
348
349 /*!
350 * @brief Force GPT output action on output compare channel, ignoring comparator.
351 *
352 * @param base GPT base pointer.
353 * @param channel GPT output compare channel (see @ref _gpt_output_compare_channel enumeration).
354 */
GPT_ForceOutput(GPT_Type * base,uint32_t channel)355 static inline void GPT_ForceOutput(GPT_Type* base, uint32_t channel)
356 {
357 assert (channel <= gptOutputCompareChannel3);
358
359 base->CR |= (GPT_CR_FO1_MASK << channel);
360 }
361
362 /*@}*/
363
364 /*!
365 * @name GPT Interrupt and Status Control
366 * @{
367 */
368
369 /*!
370 * @brief Get GPT status flag.
371 *
372 * @param base GPT base pointer.
373 * @param flags GPT status flag mask (see @ref _gpt_status_flag for bit definition).
374 * @return GPT status, each bit represents one status flag.
375 */
GPT_GetStatusFlag(GPT_Type * base,uint32_t flags)376 static inline uint32_t GPT_GetStatusFlag(GPT_Type* base, uint32_t flags)
377 {
378 return base->SR & flags;
379 }
380
381 /*!
382 * @brief Clear one or more GPT status flag.
383 *
384 * @param base GPT base pointer.
385 * @param flags GPT status flag mask (see @ref _gpt_status_flag for bit definition).
386 */
GPT_ClearStatusFlag(GPT_Type * base,uint32_t flags)387 static inline void GPT_ClearStatusFlag(GPT_Type* base, uint32_t flags)
388 {
389 base->SR = flags;
390 }
391
392 /*!
393 * @brief Enable or Disable GPT interrupts.
394 *
395 * @param base GPT base pointer.
396 * @param flags GPT status flag mask (see @ref _gpt_status_flag for bit definition).
397 * @param enable Enable/Disable GPT interrupts.
398 * -true: Enable GPT interrupts.
399 * -false: Disable GPT interrupts.
400 */
401 void GPT_SetIntCmd(GPT_Type* base, uint32_t flags, bool enable);
402
403 /*@}*/
404
405 #if defined(__cplusplus)
406 }
407 #endif
408
409 /*! @}*/
410
411 #endif /* __GPT_H__ */
412 /*******************************************************************************
413 * EOF
414 ******************************************************************************/
415