1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #include "fsl_lptmr.h"
10 
11 /* Component ID definition, used by tools. */
12 #ifndef FSL_COMPONENT_ID
13 #define FSL_COMPONENT_ID "platform.drivers.lptmr"
14 #endif
15 
16 /*******************************************************************************
17  * Prototypes
18  ******************************************************************************/
19 #if defined(LPTMR_CLOCKS)
20 /*!
21  * @brief Gets the instance from the base address to be used to gate or ungate the module clock
22  *
23  * @param base LPTMR peripheral base address
24  *
25  * @return The LPTMR instance
26  */
27 static uint32_t LPTMR_GetInstance(LPTMR_Type *base);
28 #endif /* LPTMR_CLOCKS */
29 
30 /*******************************************************************************
31  * Variables
32  ******************************************************************************/
33 #if defined(LPTMR_CLOCKS)
34 /*! @brief Pointers to LPTMR bases for each instance. */
35 static LPTMR_Type *const s_lptmrBases[] = LPTMR_BASE_PTRS;
36 
37 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
38 /*! @brief Pointers to LPTMR clocks for each instance. */
39 static const clock_ip_name_t s_lptmrClocks[] = LPTMR_CLOCKS;
40 
41 #if defined(LPTMR_PERIPH_CLOCKS)
42 /* Array of LPTMR functional clock name. */
43 static const clock_ip_name_t s_lptmrPeriphClocks[] = LPTMR_PERIPH_CLOCKS;
44 #endif
45 
46 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
47 #endif /* LPTMR_CLOCKS */
48 
49 /*******************************************************************************
50  * Code
51  ******************************************************************************/
52 #if defined(LPTMR_CLOCKS)
LPTMR_GetInstance(LPTMR_Type * base)53 static uint32_t LPTMR_GetInstance(LPTMR_Type *base)
54 {
55     uint32_t instance;
56 
57     /* Find the instance index from base address mappings. */
58     for (instance = 0; instance < ARRAY_SIZE(s_lptmrBases); instance++)
59     {
60         if (s_lptmrBases[instance] == base)
61         {
62             break;
63         }
64     }
65 
66     assert(instance < ARRAY_SIZE(s_lptmrBases));
67 
68     return instance;
69 }
70 #endif /* LPTMR_CLOCKS */
71 
72 /*!
73  * brief Ungates the LPTMR clock and configures the peripheral for a basic operation.
74  *
75  * note This API should be called at the beginning of the application using the LPTMR driver.
76  *
77  * param base   LPTMR peripheral base address
78  * param config A pointer to the LPTMR configuration structure.
79  */
LPTMR_Init(LPTMR_Type * base,const lptmr_config_t * config)80 void LPTMR_Init(LPTMR_Type *base, const lptmr_config_t *config)
81 {
82     assert(NULL != config);
83 
84 #if defined(LPTMR_CLOCKS)
85 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
86 
87     uint32_t instance = LPTMR_GetInstance(base);
88 
89     /* Ungate the LPTMR clock*/
90     CLOCK_EnableClock(s_lptmrClocks[instance]);
91 #if defined(LPTMR_PERIPH_CLOCKS)
92     CLOCK_EnableClock(s_lptmrPeriphClocks[instance]);
93 #endif
94 
95 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
96 #endif /* LPTMR_CLOCKS */
97 
98     /* Configure the timers operation mode and input pin setup */
99     base->CSR = (LPTMR_CSR_TMS(config->timerMode) | LPTMR_CSR_TFC(config->enableFreeRunning) |
100                  LPTMR_CSR_TPP(config->pinPolarity) | LPTMR_CSR_TPS(config->pinSelect));
101 
102     /* Configure the prescale value and clock source */
103     base->PSR = (LPTMR_PSR_PRESCALE(config->value) | LPTMR_PSR_PBYP(config->bypassPrescaler) |
104                  LPTMR_PSR_PCS(config->prescalerClockSource));
105 }
106 
107 /*!
108  * brief Gates the LPTMR clock.
109  *
110  * param base LPTMR peripheral base address
111  */
LPTMR_Deinit(LPTMR_Type * base)112 void LPTMR_Deinit(LPTMR_Type *base)
113 {
114     /* Disable the LPTMR and reset the internal logic */
115     base->CSR &= ~LPTMR_CSR_TEN_MASK;
116 
117 #if defined(LPTMR_CLOCKS)
118 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
119 
120     uint32_t instance = LPTMR_GetInstance(base);
121 
122     /* Gate the LPTMR clock*/
123     CLOCK_DisableClock(s_lptmrClocks[instance]);
124 #if defined(LPTMR_PERIPH_CLOCKS)
125     CLOCK_DisableClock(s_lptmrPeriphClocks[instance]);
126 #endif
127 
128 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
129 #endif /* LPTMR_CLOCKS */
130 }
131 
132 /*!
133  * brief Fills in the LPTMR configuration structure with default settings.
134  *
135  * The default values are as follows.
136  * code
137  *    config->timerMode = kLPTMR_TimerModeTimeCounter;
138  *    config->pinSelect = kLPTMR_PinSelectInput_0;
139  *    config->pinPolarity = kLPTMR_PinPolarityActiveHigh;
140  *    config->enableFreeRunning = false;
141  *    config->bypassPrescaler = true;
142  *    config->prescalerClockSource = kLPTMR_PrescalerClock_1;
143  *    config->value = kLPTMR_Prescale_Glitch_0;
144  * endcode
145  * param config A pointer to the LPTMR configuration structure.
146  */
LPTMR_GetDefaultConfig(lptmr_config_t * config)147 void LPTMR_GetDefaultConfig(lptmr_config_t *config)
148 {
149     assert(NULL != config);
150 
151     /* Initializes the configure structure to zero. */
152     (void)memset(config, 0, sizeof(*config));
153 
154     /* Use time counter mode */
155     config->timerMode = kLPTMR_TimerModeTimeCounter;
156     /* Use input 0 as source in pulse counter mode */
157     config->pinSelect = kLPTMR_PinSelectInput_0;
158     /* Pulse input pin polarity is active-high */
159     config->pinPolarity = kLPTMR_PinPolarityActiveHigh;
160     /* Counter resets whenever TCF flag is set */
161     config->enableFreeRunning = false;
162     /* Bypass the prescaler */
163     config->bypassPrescaler = true;
164     /* LPTMR clock source */
165 #if !(defined(FSL_FEATURE_LPTMR_HAS_NO_PRESCALER_CLOCK_SOURCE_1_SUPPORT) && \
166       FSL_FEATURE_LPTMR_HAS_NO_PRESCALER_CLOCK_SOURCE_1_SUPPORT)
167     config->prescalerClockSource = kLPTMR_PrescalerClock_1;
168 #else
169     config->prescalerClockSource = kLPTMR_PrescalerClock_0;
170 #endif /* FSL_FEATURE_LPTMR_HAS_NO_PRESCALER_CLOCK_SOURCE_1_SUPPORT */
171     /* Divide the prescaler clock by 2 */
172     config->value = kLPTMR_Prescale_Glitch_0;
173 }
174