1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2024 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef FSL_GPT_H_
10 #define FSL_GPT_H_
11 
12 #include "fsl_common.h"
13 
14 /*!
15  * @addtogroup gpt
16  * @{
17  */
18 
19 /*******************************************************************************
20  * Definitions
21  ******************************************************************************/
22 
23 /*! @name Driver version */
24 /*! @{ */
25 #define FSL_GPT_DRIVER_VERSION (MAKE_VERSION(2, 0, 5))
26 /*! @} */
27 
28 /*!
29  * @brief List of clock sources
30  * @note Actual number of clock sources is SoC dependent
31  */
32 typedef enum _gpt_clock_source
33 {
34     kGPT_ClockSource_Off      = 0U, /*!< GPT Clock Source Off.*/
35     kGPT_ClockSource_Periph   = 1U, /*!< GPT Clock Source from Peripheral Clock.*/
36     kGPT_ClockSource_HighFreq = 2U, /*!< GPT Clock Source from High Frequency Reference Clock.*/
37     kGPT_ClockSource_Ext      = 3U, /*!< GPT Clock Source from external pin.*/
38     kGPT_ClockSource_LowFreq  = 4U, /*!< GPT Clock Source from Low Frequency Reference Clock.*/
39     kGPT_ClockSource_Osc      = 5U, /*!< GPT Clock Source from Crystal oscillator.*/
40 } gpt_clock_source_t;
41 
42 /*! @brief List of input capture channel number. */
43 typedef enum _gpt_input_capture_channel
44 {
45     kGPT_InputCapture_Channel1 = 0U, /*!< GPT Input Capture Channel1.*/
46     kGPT_InputCapture_Channel2 = 1U, /*!< GPT Input Capture Channel2.*/
47 } gpt_input_capture_channel_t;
48 
49 /*! @brief List of input capture operation mode. */
50 typedef enum _gpt_input_operation_mode
51 {
52     kGPT_InputOperation_Disabled = 0U, /*!< Don't capture.*/
53     kGPT_InputOperation_RiseEdge = 1U, /*!< Capture on rising edge of input pin.*/
54     kGPT_InputOperation_FallEdge = 2U, /*!< Capture on falling edge of input pin.*/
55     kGPT_InputOperation_BothEdge = 3U, /*!< Capture on both edges of input pin.*/
56 } gpt_input_operation_mode_t;
57 
58 /*! @brief List of output compare channel number. */
59 typedef enum _gpt_output_compare_channel
60 {
61     kGPT_OutputCompare_Channel1 = 0U, /*!< Output Compare Channel1.*/
62     kGPT_OutputCompare_Channel2 = 1U, /*!< Output Compare Channel2.*/
63     kGPT_OutputCompare_Channel3 = 2U, /*!< Output Compare Channel3.*/
64 } gpt_output_compare_channel_t;
65 
66 /*! @brief List of output compare operation mode. */
67 typedef enum _gpt_output_operation_mode
68 {
69     kGPT_OutputOperation_Disconnected = 0U, /*!< Don't change output pin.*/
70     kGPT_OutputOperation_Toggle       = 1U, /*!< Toggle output pin.*/
71     kGPT_OutputOperation_Clear        = 2U, /*!< Set output pin low.*/
72     kGPT_OutputOperation_Set          = 3U, /*!< Set output pin high.*/
73     kGPT_OutputOperation_Activelow    = 4U, /*!< Generate a active low pulse on output pin.*/
74 } gpt_output_operation_mode_t;
75 
76 /*! @brief List of GPT interrupts */
77 typedef enum _gpt_interrupt_enable
78 {
79     kGPT_OutputCompare1InterruptEnable = GPT_IR_OF1IE_MASK, /*!< Output Compare Channel1 interrupt enable*/
80     kGPT_OutputCompare2InterruptEnable = GPT_IR_OF2IE_MASK, /*!< Output Compare Channel2 interrupt enable*/
81     kGPT_OutputCompare3InterruptEnable = GPT_IR_OF3IE_MASK, /*!< Output Compare Channel3 interrupt enable*/
82     kGPT_InputCapture1InterruptEnable  = GPT_IR_IF1IE_MASK, /*!< Input Capture Channel1 interrupt enable*/
83     kGPT_InputCapture2InterruptEnable  = GPT_IR_IF2IE_MASK, /*!< Input Capture Channel1 interrupt enable*/
84     kGPT_RollOverFlagInterruptEnable   = GPT_IR_ROVIE_MASK, /*!< Counter rolled over interrupt enable*/
85 } gpt_interrupt_enable_t;
86 
87 /*! @brief Status flag. */
88 typedef enum _gpt_status_flag
89 {
90     kGPT_OutputCompare1Flag = GPT_SR_OF1_MASK, /*!< Output compare channel 1 event.*/
91     kGPT_OutputCompare2Flag = GPT_SR_OF2_MASK, /*!< Output compare channel 2 event.*/
92     kGPT_OutputCompare3Flag = GPT_SR_OF3_MASK, /*!< Output compare channel 3 event.*/
93     kGPT_InputCapture1Flag  = GPT_SR_IF1_MASK, /*!< Input Capture channel 1 event.*/
94     kGPT_InputCapture2Flag  = GPT_SR_IF2_MASK, /*!< Input Capture channel 2 event.*/
95     kGPT_RollOverFlag       = GPT_SR_ROV_MASK, /*!< Counter reaches maximum value and rolled over to 0 event.*/
96 } gpt_status_flag_t;
97 
98 /*! @brief Structure to configure the running mode. */
99 typedef struct _gpt_init_config
100 {
101     gpt_clock_source_t clockSource; /*!< clock source for GPT module. */
102     uint32_t divider;               /*!< clock divider (prescaler+1) from clock source to counter. */
103     bool enableFreeRun;             /*!< true: FreeRun mode, false: Restart mode. */
104     bool enableRunInWait;           /*!< GPT enabled in wait mode. */
105     bool enableRunInStop;           /*!< GPT enabled in stop mode. */
106     bool enableRunInDoze;           /*!< GPT enabled in doze mode. */
107     bool enableRunInDbg;            /*!< GPT enabled in debug mode. */
108     bool enableMode;                /*!< true:  counter reset to 0 when enabled;
109                                     false: counter retain its value when enabled. */
110 } gpt_config_t;
111 
112 /*******************************************************************************
113  * API
114  ******************************************************************************/
115 
116 #if defined(__cplusplus)
117 extern "C" {
118 #endif
119 
120 /*!
121  * @name Initialization and deinitialization
122  * @{
123  */
124 
125 /*!
126  * @brief Initialize GPT to reset state and initialize running mode.
127  *
128  * @param base GPT peripheral base address.
129  * @param initConfig GPT mode setting configuration.
130  */
131 void GPT_Init(GPT_Type *base, const gpt_config_t *initConfig);
132 
133 /*!
134  * @brief Disables the module and gates the GPT clock.
135  *
136  * @param base GPT peripheral base address.
137  */
138 void GPT_Deinit(GPT_Type *base);
139 
140 /*!
141  * @brief Fills in the GPT configuration structure with default settings.
142  *
143  * The default values are:
144  * @code
145  *    config->clockSource = kGPT_ClockSource_Periph;
146  *    config->divider = 1U;
147  *    config->enableRunInStop = true;
148  *    config->enableRunInWait = true;
149  *    config->enableRunInDoze = false;
150  *    config->enableRunInDbg = false;
151  *    config->enableFreeRun = false;
152  *    config->enableMode  = true;
153  * @endcode
154  * @param config Pointer to the user configuration structure.
155  */
156 void GPT_GetDefaultConfig(gpt_config_t *config);
157 
158 /*!
159  * @name Software Reset
160  * @{
161  */
162 
163 /*!
164  * @brief Software reset of GPT module.
165  *
166  * @param base GPT peripheral base address.
167  */
GPT_SoftwareReset(GPT_Type * base)168 static inline void GPT_SoftwareReset(GPT_Type *base)
169 {
170     base->CR |= GPT_CR_SWR_MASK;
171     /* Wait reset finished. */
172     while ((base->CR & GPT_CR_SWR_MASK) == GPT_CR_SWR_MASK)
173     {
174     }
175 }
176 
177 /*!
178  * @name Clock source and frequency control
179  * @{
180  */
181 
182 /*!
183  * @brief Set clock source of GPT.
184  *
185  * @param base GPT peripheral base address.
186  * @param gptClkSource Clock source (see @ref gpt_clock_source_t typedef enumeration).
187  */
GPT_SetClockSource(GPT_Type * base,gpt_clock_source_t gptClkSource)188 static inline void GPT_SetClockSource(GPT_Type *base, gpt_clock_source_t gptClkSource)
189 {
190 #if defined(FSL_FEATURE_GPT_HAS_ERRATA_3777) && FSL_FEATURE_GPT_HAS_ERRATA_3777
191     uint32_t regCR;
192     uint32_t regIR;
193 
194     /* Step1: Disable GPT. */
195     base->CR &= ~GPT_CR_EN_MASK;
196 
197     /* Step2: Disable interrupts. Backup IR register first. */
198     regIR = base->IR;
199     base->IR = 0U;
200 
201     /* Step3: Configure Output Mode to unconnected or disconnected. Backup CR register first. */
202     regCR = base->CR & (GPT_CR_OM1_MASK | GPT_CR_OM2_MASK | GPT_CR_OM3_MASK | GPT_CR_IM1_MASK | GPT_CR_IM2_MASK);
203     base->CR &= ~(GPT_CR_OM1_MASK | GPT_CR_OM2_MASK | GPT_CR_OM3_MASK);
204 
205     /* Step4: Disable Input Capture Modes. */
206     base->CR &= ~(GPT_CR_IM1_MASK | GPT_CR_IM2_MASK);
207 #endif
208 
209     /* Step5: Set clock source. */
210     if (gptClkSource == kGPT_ClockSource_Osc)
211     {
212         base->CR = (base->CR & ~GPT_CR_CLKSRC_MASK) | GPT_CR_EN_24M_MASK | GPT_CR_CLKSRC(gptClkSource);
213     }
214     else
215     {
216         base->CR = (base->CR & ~(GPT_CR_CLKSRC_MASK | GPT_CR_EN_24M_MASK)) | GPT_CR_CLKSRC(gptClkSource);
217     }
218 
219 #if defined(FSL_FEATURE_GPT_HAS_ERRATA_3777) && FSL_FEATURE_GPT_HAS_ERRATA_3777
220     /* Step6: Clear Status register. */
221     base->SR = GPT_SR_OF1_MASK | GPT_SR_OF2_MASK | GPT_SR_OF3_MASK |
222                GPT_SR_IF1_MASK | GPT_SR_IF2_MASK | GPT_SR_ROV_MASK;
223 
224     /* Step7: Main Counter and Prescaler Counter are reset to 0 after GPT is enabled. Restore backup value. */
225     base->CR |= GPT_CR_ENMOD_MASK;
226     base->CR |= regCR;
227     base->IR |= regIR;
228 
229     /* Step8: Enable GPT. */
230     base->CR |= GPT_CR_EN_MASK;
231 #endif
232 }
233 
234 /*!
235  * @brief Get clock source of GPT.
236  *
237  * @param base GPT peripheral base address.
238  * @return clock source (see @ref gpt_clock_source_t typedef enumeration).
239  */
GPT_GetClockSource(GPT_Type * base)240 static inline gpt_clock_source_t GPT_GetClockSource(GPT_Type *base)
241 {
242     return (gpt_clock_source_t)(uint8_t)((base->CR & GPT_CR_CLKSRC_MASK) >> GPT_CR_CLKSRC_SHIFT);
243 }
244 
245 /*!
246  * @brief Set pre scaler of GPT.
247  *
248  * @param base GPT peripheral base address.
249  * @param divider Divider of GPT (1-4096).
250  */
GPT_SetClockDivider(GPT_Type * base,uint32_t divider)251 static inline void GPT_SetClockDivider(GPT_Type *base, uint32_t divider)
252 {
253     assert(divider - 1U <= GPT_PR_PRESCALER_MASK);
254 
255     base->PR = (base->PR & ~GPT_PR_PRESCALER_MASK) | GPT_PR_PRESCALER(divider - 1U);
256 }
257 
258 /*!
259  * @brief Get clock divider in GPT module.
260  *
261  * @param base GPT peripheral base address.
262  * @return clock divider in GPT module (1-4096).
263  */
GPT_GetClockDivider(GPT_Type * base)264 static inline uint32_t GPT_GetClockDivider(GPT_Type *base)
265 {
266     return ((base->PR & GPT_PR_PRESCALER_MASK) >> GPT_PR_PRESCALER_SHIFT) + 1U;
267 }
268 
269 /*!
270  * @brief OSC 24M pre-scaler before selected by clock source.
271  *
272  * @param base GPT peripheral base address.
273  * @param divider OSC Divider(1-16).
274  */
GPT_SetOscClockDivider(GPT_Type * base,uint32_t divider)275 static inline void GPT_SetOscClockDivider(GPT_Type *base, uint32_t divider)
276 {
277     assert(divider - 1U <= (GPT_PR_PRESCALER24M_MASK >> GPT_PR_PRESCALER24M_SHIFT));
278 
279     base->PR = (base->PR & ~GPT_PR_PRESCALER24M_MASK) | GPT_PR_PRESCALER24M(divider - 1U);
280 }
281 
282 /*!
283  * @brief Get OSC 24M clock divider in GPT module.
284  *
285  * @param base GPT peripheral base address.
286  * @return OSC clock divider in GPT module (1-16).
287  */
GPT_GetOscClockDivider(GPT_Type * base)288 static inline uint32_t GPT_GetOscClockDivider(GPT_Type *base)
289 {
290     return ((base->PR & GPT_PR_PRESCALER24M_MASK) >> GPT_PR_PRESCALER24M_SHIFT) + 1U;
291 }
292 
293 /*! @}*/
294 
295 /*!
296  * @name Timer Start and Stop
297  * @{
298  */
299 /*!
300  * @brief Start GPT timer.
301  *
302  * @param base GPT peripheral base address.
303  */
GPT_StartTimer(GPT_Type * base)304 static inline void GPT_StartTimer(GPT_Type *base)
305 {
306     base->CR |= GPT_CR_EN_MASK;
307 }
308 
309 /*!
310  * @brief Stop GPT timer.
311  *
312  * @param base GPT peripheral base address.
313  */
GPT_StopTimer(GPT_Type * base)314 static inline void GPT_StopTimer(GPT_Type *base)
315 {
316     base->CR &= ~GPT_CR_EN_MASK;
317 }
318 
319 /*!
320  * @name Read the timer period
321  * @{
322  */
323 
324 /*!
325  * @brief Reads the current GPT counting value.
326  *
327  * @param base GPT peripheral base address.
328  * @return Current GPT counter value.
329  */
GPT_GetCurrentTimerCount(GPT_Type * base)330 static inline uint32_t GPT_GetCurrentTimerCount(GPT_Type *base)
331 {
332     return base->CNT;
333 }
334 
335 /*! @} */
336 
337 /*!
338  * @name GPT Input/Output Signal Control
339  * @{
340  */
341 
342 /*!
343  * @brief Set GPT operation mode of input capture channel.
344  *
345  * @param base GPT peripheral base address.
346  * @param channel GPT capture channel (see @ref gpt_input_capture_channel_t typedef enumeration).
347  * @param mode GPT input capture operation mode (see @ref gpt_input_operation_mode_t typedef enumeration).
348  */
GPT_SetInputOperationMode(GPT_Type * base,gpt_input_capture_channel_t channel,gpt_input_operation_mode_t mode)349 static inline void GPT_SetInputOperationMode(GPT_Type *base,
350                                              gpt_input_capture_channel_t channel,
351                                              gpt_input_operation_mode_t mode)
352 {
353     assert(channel <= kGPT_InputCapture_Channel2);
354 
355     base->CR =
356         (base->CR & ~(GPT_CR_IM1_MASK << ((uint32_t)channel * 2UL))) | (GPT_CR_IM1(mode) << ((uint32_t)channel * 2UL));
357 }
358 
359 /*!
360  * @brief Get GPT operation mode of input capture channel.
361  *
362  * @param base GPT peripheral base address.
363  * @param channel GPT capture channel (see @ref gpt_input_capture_channel_t typedef enumeration).
364  * @return GPT input capture operation mode (see @ref gpt_input_operation_mode_t typedef enumeration).
365  */
GPT_GetInputOperationMode(GPT_Type * base,gpt_input_capture_channel_t channel)366 static inline gpt_input_operation_mode_t GPT_GetInputOperationMode(GPT_Type *base, gpt_input_capture_channel_t channel)
367 {
368     assert(channel <= kGPT_InputCapture_Channel2);
369 
370     return (gpt_input_operation_mode_t)(uint8_t)((base->CR >> (GPT_CR_IM1_SHIFT + (uint32_t)channel * 2UL)) &
371                                                  (GPT_CR_IM1_MASK >> GPT_CR_IM1_SHIFT));
372 }
373 
374 /*!
375  * @brief Get GPT input capture value of certain channel.
376  *
377  * @param base GPT peripheral base address.
378  * @param channel GPT capture channel (see @ref gpt_input_capture_channel_t typedef enumeration).
379  * @return GPT input capture value.
380  */
GPT_GetInputCaptureValue(GPT_Type * base,gpt_input_capture_channel_t channel)381 static inline uint32_t GPT_GetInputCaptureValue(GPT_Type *base, gpt_input_capture_channel_t channel)
382 {
383     assert(channel <= kGPT_InputCapture_Channel2);
384 
385     return base->ICR[(uint32_t)channel];
386 }
387 
388 /*!
389  * @brief Set GPT operation mode of output compare channel.
390  *
391  * @param base GPT peripheral base address.
392  * @param channel GPT output compare channel (see @ref gpt_output_compare_channel_t typedef enumeration).
393  * @param mode GPT output operation mode (see @ref gpt_output_operation_mode_t typedef enumeration).
394  */
GPT_SetOutputOperationMode(GPT_Type * base,gpt_output_compare_channel_t channel,gpt_output_operation_mode_t mode)395 static inline void GPT_SetOutputOperationMode(GPT_Type *base,
396                                               gpt_output_compare_channel_t channel,
397                                               gpt_output_operation_mode_t mode)
398 {
399     assert(channel <= kGPT_OutputCompare_Channel3);
400 
401     base->CR =
402         (base->CR & ~(GPT_CR_OM1_MASK << ((uint32_t)channel * 3UL))) | (GPT_CR_OM1(mode) << ((uint32_t)channel * 3UL));
403 }
404 
405 /*!
406  * @brief Get GPT operation mode of output compare channel.
407  *
408  * @param base GPT peripheral base address.
409  * @param channel GPT output compare channel (see @ref gpt_output_compare_channel_t typedef enumeration).
410  * @return GPT output operation mode (see @ref gpt_output_operation_mode_t typedef enumeration).
411  */
GPT_GetOutputOperationMode(GPT_Type * base,gpt_output_compare_channel_t channel)412 static inline gpt_output_operation_mode_t GPT_GetOutputOperationMode(GPT_Type *base,
413                                                                      gpt_output_compare_channel_t channel)
414 {
415     assert(channel <= kGPT_OutputCompare_Channel3);
416 
417     return (gpt_output_operation_mode_t)(uint8_t)((base->CR >> (GPT_CR_OM1_SHIFT + (uint32_t)channel * 3UL)) &
418                                                   (GPT_CR_OM1_MASK >> GPT_CR_OM1_SHIFT));
419 }
420 
421 /*!
422  * @brief Set GPT output compare value of output compare channel.
423  *
424  * @param base GPT peripheral base address.
425  * @param channel GPT output compare channel (see @ref gpt_output_compare_channel_t typedef enumeration).
426  * @param value GPT output compare value.
427  */
GPT_SetOutputCompareValue(GPT_Type * base,gpt_output_compare_channel_t channel,uint32_t value)428 static inline void GPT_SetOutputCompareValue(GPT_Type *base, gpt_output_compare_channel_t channel, uint32_t value)
429 {
430     assert(channel <= kGPT_OutputCompare_Channel3);
431 
432     base->OCR[(uint32_t)channel] = value;
433 }
434 
435 /*!
436  * @brief Get GPT output compare value of output compare channel.
437  *
438  * @param base GPT peripheral base address.
439  * @param channel GPT output compare channel (see @ref gpt_output_compare_channel_t typedef enumeration).
440  * @return GPT output compare value.
441  */
GPT_GetOutputCompareValue(GPT_Type * base,gpt_output_compare_channel_t channel)442 static inline uint32_t GPT_GetOutputCompareValue(GPT_Type *base, gpt_output_compare_channel_t channel)
443 {
444     assert(channel <= kGPT_OutputCompare_Channel3);
445 
446     return base->OCR[(uint32_t)channel];
447 }
448 
449 /*!
450  * @brief Force GPT output action on output compare channel, ignoring comparator.
451  *
452  * @param base GPT peripheral base address.
453  * @param channel GPT output compare channel (see @ref gpt_output_compare_channel_t typedef enumeration).
454  */
GPT_ForceOutput(GPT_Type * base,gpt_output_compare_channel_t channel)455 static inline void GPT_ForceOutput(GPT_Type *base, gpt_output_compare_channel_t channel)
456 {
457     assert(channel <= kGPT_OutputCompare_Channel3);
458 
459     base->CR |= (GPT_CR_FO1_MASK << (uint32_t)channel);
460 }
461 
462 /*! @} */
463 
464 /*!
465  * @name GPT Interrupt and Status Interface
466  * @{
467  */
468 
469 /*!
470  * @brief Enables the selected GPT interrupts.
471  *
472  * @param base GPT peripheral base address.
473  * @param mask The interrupts to enable. This is a logical OR of members of the
474  *             enumeration ::gpt_interrupt_enable_t
475  */
GPT_EnableInterrupts(GPT_Type * base,uint32_t mask)476 static inline void GPT_EnableInterrupts(GPT_Type *base, uint32_t mask)
477 {
478     base->IR |= mask;
479 }
480 
481 /*!
482  * @brief Disables the selected GPT interrupts.
483  *
484  * @param base    GPT peripheral base address
485  * @param mask    The interrupts to disable. This is a logical OR of members of the
486  *                enumeration ::gpt_interrupt_enable_t
487  */
GPT_DisableInterrupts(GPT_Type * base,uint32_t mask)488 static inline void GPT_DisableInterrupts(GPT_Type *base, uint32_t mask)
489 {
490     base->IR &= ~mask;
491 }
492 
493 /*!
494  * @brief Gets the enabled GPT interrupts.
495  *
496  * @param base    GPT peripheral base address
497  *
498  * @return The enabled interrupts. This is the logical OR of members of the
499  *         enumeration ::gpt_interrupt_enable_t
500  */
GPT_GetEnabledInterrupts(GPT_Type * base)501 static inline uint32_t GPT_GetEnabledInterrupts(GPT_Type *base)
502 {
503     return (base->IR & (GPT_IR_OF1IE_MASK | GPT_IR_OF2IE_MASK | GPT_IR_OF3IE_MASK | GPT_IR_IF1IE_MASK |
504                         GPT_IR_IF2IE_MASK | GPT_IR_ROVIE_MASK));
505 }
506 
507 /*!
508  * @name Status Interface
509  * @{
510  */
511 
512 /*!
513  * @brief Get GPT status flags.
514  *
515  * @param base GPT peripheral base address.
516  * @param flags GPT status flag mask (see @ref gpt_status_flag_t for bit definition).
517  * @return GPT status, each bit represents one status flag.
518  */
GPT_GetStatusFlags(GPT_Type * base,gpt_status_flag_t flags)519 static inline uint32_t GPT_GetStatusFlags(GPT_Type *base, gpt_status_flag_t flags)
520 {
521     return base->SR & (uint32_t)flags;
522 }
523 
524 /*!
525  * @brief Clears the GPT status flags.
526  *
527  * @param base GPT peripheral base address.
528  * @param flags GPT status flag mask (see @ref gpt_status_flag_t for bit definition).
529  */
GPT_ClearStatusFlags(GPT_Type * base,gpt_status_flag_t flags)530 static inline void GPT_ClearStatusFlags(GPT_Type *base, gpt_status_flag_t flags)
531 {
532     base->SR = (uint32_t)flags;
533 }
534 
535 /*! @} */
536 
537 #if defined(__cplusplus)
538 }
539 #endif
540 
541 /*! @}*/
542 
543 #endif /* FSL_GPT_H_ */
544