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 #include "fsl_gpt.h"
10 
11 /* Component ID definition, used by tools. */
12 #ifndef FSL_COMPONENT_ID
13 #define FSL_COMPONENT_ID "platform.drivers.gpt"
14 #endif
15 
16 /*******************************************************************************
17  * Prototypes
18  ******************************************************************************/
19 
20 /*******************************************************************************
21  * Variables
22  ******************************************************************************/
23 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
24 /*! @brief Pointers to GPT bases for each instance. */
25 static GPT_Type *const s_gptBases[] = GPT_BASE_PTRS;
26 
27 /*! @brief Pointers to GPT clocks for each instance. */
28 static const clock_ip_name_t s_gptClocks[] = GPT_CLOCKS;
29 
30 /*******************************************************************************
31  * Code
32  ******************************************************************************/
GPT_GetInstance(GPT_Type * base)33 static uint32_t GPT_GetInstance(GPT_Type *base)
34 {
35     uint32_t instance;
36 
37     /* Find the instance index from base address mappings. */
38     for (instance = 0U; instance < ARRAY_SIZE(s_gptBases); instance++)
39     {
40         if (s_gptBases[instance] == base)
41         {
42             break;
43         }
44     }
45 
46     assert(instance < ARRAY_SIZE(s_gptBases));
47 
48     return instance;
49 }
50 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
51 
52 /*!
53  * brief Initialize GPT to reset state and initialize running mode.
54  *
55  * param base GPT peripheral base address.
56  * param initConfig GPT mode setting configuration.
57  */
GPT_Init(GPT_Type * base,const gpt_config_t * initConfig)58 void GPT_Init(GPT_Type *base, const gpt_config_t *initConfig)
59 {
60     assert(NULL != initConfig);
61 
62 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
63     /* Ungate the GPT clock*/
64     (void)CLOCK_EnableClock(s_gptClocks[GPT_GetInstance(base)]);
65 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
66     base->CR = 0U;
67 
68     GPT_SoftwareReset(base);
69 
70     base->CR =
71         (initConfig->enableFreeRun ? GPT_CR_FRR_MASK : 0UL) | (initConfig->enableRunInWait ? GPT_CR_WAITEN_MASK : 0UL) |
72         (initConfig->enableRunInStop ? GPT_CR_STOPEN_MASK : 0UL) |
73         (initConfig->enableRunInDoze ? GPT_CR_DOZEEN_MASK : 0UL) |
74         (initConfig->enableRunInDbg ? GPT_CR_DBGEN_MASK : 0UL) | (initConfig->enableMode ? GPT_CR_ENMOD_MASK : 0UL);
75 
76     /* Set the GPT clock source. */
77     if (initConfig->clockSource == kGPT_ClockSource_Osc)
78     {
79         base->CR = (base->CR & ~GPT_CR_CLKSRC_MASK) | GPT_CR_EN_24M_MASK | GPT_CR_CLKSRC(initConfig->clockSource);
80     }
81     else
82     {
83         base->CR = (base->CR & ~(GPT_CR_CLKSRC_MASK | GPT_CR_EN_24M_MASK)) | GPT_CR_CLKSRC(initConfig->clockSource);
84     }
85     GPT_SetClockDivider(base, initConfig->divider);
86 }
87 
88 /*!
89  * brief Disables the module and gates the GPT clock.
90  *
91  * param base GPT peripheral base address.
92  */
GPT_Deinit(GPT_Type * base)93 void GPT_Deinit(GPT_Type *base)
94 {
95     /* Disable GPT timers */
96     base->CR = 0U;
97 
98 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
99     /* Gate the GPT clock*/
100     (void)CLOCK_DisableClock(s_gptClocks[GPT_GetInstance(base)]);
101 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
102 }
103 
104 /*!
105  * brief Fills in the GPT configuration structure with default settings.
106  *
107  * The default values are:
108  * code
109  *    config->clockSource = kGPT_ClockSource_Periph;
110  *    config->divider = 1U;
111  *    config->enableRunInStop = true;
112  *    config->enableRunInWait = true;
113  *    config->enableRunInDoze = false;
114  *    config->enableRunInDbg = false;
115  *    config->enableFreeRun = false;
116  *    config->enableMode  = true;
117  * endcode
118  * param config Pointer to the user configuration structure.
119  */
GPT_GetDefaultConfig(gpt_config_t * config)120 void GPT_GetDefaultConfig(gpt_config_t *config)
121 {
122     assert(NULL != config);
123 
124     /* Initializes the configure structure to zero. */
125     (void)memset(config, 0, sizeof(*config));
126 
127     config->clockSource     = kGPT_ClockSource_Periph;
128     config->divider         = 1U;
129     config->enableRunInStop = true;
130     config->enableRunInWait = true;
131     config->enableRunInDoze = false;
132     config->enableRunInDbg  = false;
133     config->enableFreeRun   = false;
134     config->enableMode      = true;
135 }