1/*
2 * Copyright (c) 2015, Freescale Semiconductor, Inc.
3 * Copyright 2016-2021 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. */
25static GPT_Type *const s_gptBases[] = GPT_BASE_PTRS;
26
27/*! @brief Pointers to GPT clocks for each instance. */
28static const clock_ip_name_t s_gptClocks[] = GPT_CLOCKS;
29
30/*******************************************************************************
31 * Code
32 ******************************************************************************/
33static 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 */
58void 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 GPT_SetClockSource(base, initConfig->clockSource);
77 GPT_SetClockDivider(base, initConfig->divider);
78}
79
80/*!
81 * brief Disables the module and gates the GPT clock.
82 *
83 * param base GPT peripheral base address.
84 */
85void GPT_Deinit(GPT_Type *base)
86{
87 /* Disable GPT timers */
88 base->CR = 0U;
89
90#if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
91 /* Gate the GPT clock*/
92 (void)CLOCK_DisableClock(s_gptClocks[GPT_GetInstance(base)]);
93#endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
94}
95
96/*!
97 * brief Fills in the GPT configuration structure with default settings.
98 *
99 * The default values are:
100 * code
101 * config->clockSource = kGPT_ClockSource_Periph;
102 * config->divider = 1U;
103 * config->enableRunInStop = true;
104 * config->enableRunInWait = true;
105 * config->enableRunInDoze = false;
106 * config->enableRunInDbg = false;
107 * config->enableFreeRun = false;
108 * config->enableMode = true;
109 * endcode
110 * param config Pointer to the user configuration structure.
111 */
112void GPT_GetDefaultConfig(gpt_config_t *config)
113{
114 assert(NULL != config);
115
116 /* Initializes the configure structure to zero. */
117 (void)memset(config, 0, sizeof(*config));
118
119 config->clockSource = kGPT_ClockSource_Periph;
120 config->divider = 1U;
121 config->enableRunInStop = true;
122 config->enableRunInWait = true;
123 config->enableRunInDoze = false;
124 config->enableRunInDbg = false;
125 config->enableFreeRun = false;
126 config->enableMode = true;
127}
128