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 /*******************************************************************************
12  * Prototypes
13  ******************************************************************************/
14 #if defined(LPTMR_CLOCKS)
15 /*!
16  * @brief Gets the instance from the base address to be used to gate or ungate the module clock
17  *
18  * @param base LPTMR peripheral base address
19  *
20  * @return The LPTMR instance
21  */
22 static uint32_t LPTMR_GetInstance(LPTMR_Type *base);
23 #endif /* LPTMR_CLOCKS */
24 
25 /*******************************************************************************
26  * Variables
27  ******************************************************************************/
28 #if defined(LPTMR_CLOCKS)
29 /*! @brief Pointers to LPTMR bases for each instance. */
30 static LPTMR_Type *const s_lptmrBases[] = LPTMR_BASE_PTRS;
31 
32 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
33 /*! @brief Pointers to LPTMR clocks for each instance. */
34 static const clock_ip_name_t s_lptmrClocks[] = LPTMR_CLOCKS;
35 
36 #if defined(LPTMR_PERIPH_CLOCKS)
37 /* Array of LPTMR functional clock name. */
38 static const clock_ip_name_t s_lptmrPeriphClocks[] = LPTMR_PERIPH_CLOCKS;
39 #endif
40 
41 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
42 #endif /* LPTMR_CLOCKS */
43 
44 /*******************************************************************************
45  * Code
46  ******************************************************************************/
47 #if defined(LPTMR_CLOCKS)
LPTMR_GetInstance(LPTMR_Type * base)48 static uint32_t LPTMR_GetInstance(LPTMR_Type *base)
49 {
50     uint32_t instance;
51 
52     /* Find the instance index from base address mappings. */
53     for (instance = 0; instance < ARRAY_SIZE(s_lptmrBases); instance++)
54     {
55         if (s_lptmrBases[instance] == base)
56         {
57             break;
58         }
59     }
60 
61     assert(instance < ARRAY_SIZE(s_lptmrBases));
62 
63     return instance;
64 }
65 #endif /* LPTMR_CLOCKS */
66 
LPTMR_Init(LPTMR_Type * base,const lptmr_config_t * config)67 void LPTMR_Init(LPTMR_Type *base, const lptmr_config_t *config)
68 {
69     assert(config);
70 
71 #if defined(LPTMR_CLOCKS)
72 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
73 
74     uint32_t instance = LPTMR_GetInstance(base);
75 
76     /* Ungate the LPTMR clock*/
77     CLOCK_EnableClock(s_lptmrClocks[instance]);
78 #if defined(LPTMR_PERIPH_CLOCKS)
79     CLOCK_EnableClock(s_lptmrPeriphClocks[instance]);
80 #endif
81 
82 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
83 #endif /* LPTMR_CLOCKS */
84 
85     /* Configure the timers operation mode and input pin setup */
86     base->CSR = (LPTMR_CSR_TMS(config->timerMode) | LPTMR_CSR_TFC(config->enableFreeRunning) |
87                  LPTMR_CSR_TPP(config->pinPolarity) | LPTMR_CSR_TPS(config->pinSelect));
88 
89     /* Configure the prescale value and clock source */
90     base->PSR = (LPTMR_PSR_PRESCALE(config->value) | LPTMR_PSR_PBYP(config->bypassPrescaler) |
91                  LPTMR_PSR_PCS(config->prescalerClockSource));
92 }
93 
LPTMR_Deinit(LPTMR_Type * base)94 void LPTMR_Deinit(LPTMR_Type *base)
95 {
96     /* Disable the LPTMR and reset the internal logic */
97     base->CSR &= ~LPTMR_CSR_TEN_MASK;
98 
99 #if defined(LPTMR_CLOCKS)
100 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
101 
102     uint32_t instance = LPTMR_GetInstance(base);
103 
104     /* Gate the LPTMR clock*/
105     CLOCK_DisableClock(s_lptmrClocks[instance]);
106 #if defined(LPTMR_PERIPH_CLOCKS)
107     CLOCK_DisableClock(s_lptmrPeriphClocks[instance]);
108 #endif
109 
110 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
111 #endif /* LPTMR_CLOCKS */
112 }
113 
LPTMR_GetDefaultConfig(lptmr_config_t * config)114 void LPTMR_GetDefaultConfig(lptmr_config_t *config)
115 {
116     assert(config);
117 
118     /* Use time counter mode */
119     config->timerMode = kLPTMR_TimerModeTimeCounter;
120     /* Use input 0 as source in pulse counter mode */
121     config->pinSelect = kLPTMR_PinSelectInput_0;
122     /* Pulse input pin polarity is active-high */
123     config->pinPolarity = kLPTMR_PinPolarityActiveHigh;
124     /* Counter resets whenever TCF flag is set */
125     config->enableFreeRunning = false;
126     /* Bypass the prescaler */
127     config->bypassPrescaler = true;
128     /* LPTMR clock source */
129     config->prescalerClockSource = kLPTMR_PrescalerClock_1;
130     /* Divide the prescaler clock by 2 */
131     config->value = kLPTMR_Prescale_Glitch_0;
132 }
133