1 /*
2 * Copyright (c) 2016, Freescale Semiconductor, Inc.
3 * Copyright 2016-2019 NXP
4 * All rights reserved.
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8
9 #include "fsl_tsc.h"
10
11 /* Component ID definition, used by tools. */
12 #ifndef FSL_COMPONENT_ID
13 #define FSL_COMPONENT_ID "platform.drivers.tsc"
14 #endif
15
16 /*******************************************************************************
17 * Prototypes
18 ******************************************************************************/
19 /*!
20 * @brief Get instance number for TSC module.
21 *
22 * @param base TSC peripheral base address
23 */
24 static uint32_t TSC_GetInstance(TSC_Type *base);
25
26 /*******************************************************************************
27 * Variables
28 ******************************************************************************/
29 /*! @brief Pointers to TSC bases for each instance. */
30 static TSC_Type *const s_tscBases[] = TSC_BASE_PTRS;
31
32 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
33 /*! @brief Pointers to ADC clocks for each instance. */
34 static const clock_ip_name_t s_tscClocks[] = TSC_CLOCKS;
35 #endif
36
37 /*******************************************************************************
38 * Code
39 ******************************************************************************/
TSC_GetInstance(TSC_Type * base)40 static uint32_t TSC_GetInstance(TSC_Type *base)
41 {
42 uint32_t instance;
43
44 /* Find the instance index from base address mappings. */
45 for (instance = 0; instance < ARRAY_SIZE(s_tscBases); instance++)
46 {
47 if (s_tscBases[instance] == base)
48 {
49 break;
50 }
51 }
52
53 assert(instance < ARRAY_SIZE(s_tscBases));
54
55 return instance;
56 }
57
58 /*!
59 * brief Initialize the TSC module.
60 *
61 * param base TSC peripheral base address.
62 * param config Pointer to "tsc_config_t" structure.
63 */
TSC_Init(TSC_Type * base,const tsc_config_t * config)64 void TSC_Init(TSC_Type *base, const tsc_config_t *config)
65 {
66 assert(NULL != config);
67 assert(config->measureDelayTime <= 0xFFFFFFU);
68
69 uint32_t tmp32;
70
71 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
72 /* Enable the TSC clock. */
73 CLOCK_EnableClock(s_tscClocks[TSC_GetInstance(base)]);
74 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
75 /* Configure TSC_BASIC_SETTING register. */
76 tmp32 = TSC_BASIC_SETTING_MEASURE_DELAY_TIME(config->measureDelayTime) |
77 TSC_BASIC_SETTING__4_5_WIRE(config->detectionMode);
78 if (config->enableAutoMeasure)
79 {
80 tmp32 |= TSC_BASIC_SETTING_AUTO_MEASURE_MASK;
81 }
82 base->BASIC_SETTING = tmp32;
83 /* Configure TSC_PS_INPUT_BUFFER_ADDR register. */
84 base->PRE_CHARGE_TIME = TSC_PRE_CHARGE_TIME_PRE_CHARGE_TIME(config->prechargeTime);
85 }
86
87 /*!
88 * brief De-initializes the TSC module.
89 *
90 * param base TSC peripheral base address.
91 */
TSC_Deinit(TSC_Type * base)92 void TSC_Deinit(TSC_Type *base)
93 {
94 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
95 /* Disable the TSC clcok. */
96 CLOCK_DisableClock(s_tscClocks[TSC_GetInstance(base)]);
97 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
98 }
99
100 /*!
101 * brief Gets an available pre-defined settings for the controller's configuration.
102 *
103 * This function initializes the converter configuration structure with available settings.
104 * The default values of measureDelayTime and prechargeTime is tested on LCD8000-43T screen and work normally.
105 * The default values are:
106 * code
107 * config->enableAutoMeausre = false;
108 * config->measureDelayTime = 0xFFFFU;
109 * config->prechargeTime = 0xFFFFU;
110 * config->detectionMode = kTSC_4WireDetectionMode;
111 * endCode
112 * param config Pointer to "tsc_config_t" structure.
113 */
TSC_GetDefaultConfig(tsc_config_t * config)114 void TSC_GetDefaultConfig(tsc_config_t *config)
115 {
116 /* Initializes the configure structure to zero. */
117 (void)memset(config, 0, sizeof(*config));
118
119 config->enableAutoMeasure = false;
120 config->measureDelayTime = 0xFFFFU;
121 config->prechargeTime = 0xFFFFU;
122 config->detectionMode = kTSC_Detection4WireMode;
123 }
124
125 /*!
126 * brief Get Y coordinate value or X coordinate value. The value is an ADC conversion value.
127 *
128 * param base TSC peripheral base address.
129 * param selection Select alternative measure value which is Y coordinate value or X coordinate value.
130 * See "tsc_corrdinate_value_selection_t".
131 * return If selection is "kTSC_XCoordinateValueSelection", the API returns x-coordinate vlaue.
132 * If selection is "kTSC_YCoordinateValueSelection", the API returns y-coordinate vlaue.
133 */
TSC_GetMeasureValue(TSC_Type * base,tsc_corrdinate_value_selection_t selection)134 uint32_t TSC_GetMeasureValue(TSC_Type *base, tsc_corrdinate_value_selection_t selection)
135 {
136 uint32_t tmp32 = 0;
137
138 if (selection == kTSC_XCoordinateValueSelection)
139 {
140 tmp32 = ((base->MEASEURE_VALUE) & TSC_MEASEURE_VALUE_X_VALUE_MASK) >> TSC_MEASEURE_VALUE_X_VALUE_SHIFT;
141 }
142 else if (selection == kTSC_YCoordinateValueSelection)
143 {
144 tmp32 = ((base->MEASEURE_VALUE) & TSC_MEASEURE_VALUE_Y_VALUE_MASK) >> TSC_MEASEURE_VALUE_Y_VALUE_SHIFT;
145 }
146 else
147 {
148 /* Intentional empty */
149 }
150
151 return tmp32;
152 }
153
154 /*!
155 * brief Send hardware trigger signal to ADC in debug mode. The trigger signal must last at least 1 ips clock period.
156 *
157 * param base TSC peripheral base address.
158 * param hwts Hardware trigger select signal, select which channel to start conversion. See "tsc_trigger_signal_t".
159 * On ADC side, HWTS = 1 << x indicates the x logic channel is selected to start hardware ADC conversion.
160 * param enable Switcher of the trigger signal. "true" means generate trigger signal, "false" means don't generate
161 * trigger signal.
162 */
TSC_DebugTriggerSignalToADC(TSC_Type * base,tsc_trigger_signal_t hwts,bool enable)163 void TSC_DebugTriggerSignalToADC(TSC_Type *base, tsc_trigger_signal_t hwts, bool enable)
164 {
165 if (enable)
166 {
167 /* TSC_DEBUG_MODE_EXT_HWTS field should be writed before writing TSC_DEBUG_MODE_TRIGGER field.
168 If the two fields are writed at the same time, the trigger couldn't work as expect. */
169 base->DEBUG_MODE &= ~TSC_DEBUG_MODE_EXT_HWTS_MASK;
170 base->DEBUG_MODE |= TSC_DEBUG_MODE_EXT_HWTS(hwts);
171 base->DEBUG_MODE |= TSC_DEBUG_MODE_TRIGGER_MASK;
172 }
173 else
174 {
175 base->DEBUG_MODE &= ~TSC_DEBUG_MODE_TRIGGER_MASK;
176 }
177 }
178
179 /*!
180 * brief Enable/Disable detection in debug mode.
181 *
182 * param base TSC peripheral base address.
183 * param detectionMode Set detect mode. See "tsc_detection_mode_t"
184 * param enable Switcher of detect enable. "true" means enable detection, "false" means disable detection.
185 */
TSC_DebugEnableDetection(TSC_Type * base,tsc_detection_mode_t detectionMode,bool enable)186 void TSC_DebugEnableDetection(TSC_Type *base, tsc_detection_mode_t detectionMode, bool enable)
187 {
188 if (detectionMode == kTSC_Detection4WireMode)
189 {
190 if (enable)
191 {
192 base->DEBUG_MODE2 |= TSC_DEBUG_MODE2_DETECT_ENABLE_FOUR_WIRE_MASK;
193 }
194 else
195 {
196 base->DEBUG_MODE2 &= ~TSC_DEBUG_MODE2_DETECT_ENABLE_FOUR_WIRE_MASK;
197 }
198 }
199 else if (detectionMode == kTSC_Detection5WireMode)
200 {
201 if (enable)
202 {
203 base->DEBUG_MODE2 |= TSC_DEBUG_MODE2_DETECT_ENABLE_FIVE_WIRE_MASK;
204 }
205 else
206 {
207 base->DEBUG_MODE2 &= ~TSC_DEBUG_MODE2_DETECT_ENABLE_FIVE_WIRE_MASK;
208 }
209 }
210 else
211 {
212 /* Intentional empty */
213 }
214 }
215
216 /*!
217 * brief Set TSC port mode in debug mode.(pull down, pull up and 200k-pull up)
218 *
219 * param base TSC peripheral base address.
220 * param port TSC controller ports.
221 * param mode TSC port mode.(pull down, pull up and 200k-pull up)
222 */
TSC_DebugSetPortMode(TSC_Type * base,tsc_port_source_t port,tsc_port_mode_t mode)223 void TSC_DebugSetPortMode(TSC_Type *base, tsc_port_source_t port, tsc_port_mode_t mode)
224 {
225 uint32_t tmp32;
226
227 tmp32 = base->DEBUG_MODE2;
228 switch (port)
229 {
230 case kTSC_WiperPortSource:
231 tmp32 &= ~(TSC_DEBUG_MODE2_WIPER_200K_PULL_UP_MASK | TSC_DEBUG_MODE2_WIPER_PULL_UP_MASK |
232 TSC_DEBUG_MODE2_WIPER_PULL_DOWN_MASK);
233 tmp32 |= ((uint32_t)mode << TSC_DEBUG_MODE2_WIPER_PULL_DOWN_SHIFT);
234 break;
235 case kTSC_YnlrPortSource:
236 tmp32 &= ~(TSC_DEBUG_MODE2_YNLR_200K_PULL_UP_MASK | TSC_DEBUG_MODE2_YNLR_PULL_UP_MASK |
237 TSC_DEBUG_MODE2_YNLR_PULL_DOWN_MASK);
238 tmp32 |= ((uint32_t)mode << TSC_DEBUG_MODE2_YNLR_PULL_DOWN_SHIFT);
239 break;
240 case kTSC_YpllPortSource:
241 tmp32 &= ~(TSC_DEBUG_MODE2_YPLL_200K_PULL_UP_MASK | TSC_DEBUG_MODE2_YPLL_PULL_UP_MASK |
242 TSC_DEBUG_MODE2_YPLL_PULL_DOWN_MASK);
243 tmp32 |= ((uint32_t)mode << TSC_DEBUG_MODE2_YPLL_PULL_DOWN_SHIFT);
244 break;
245 case kTSC_XnurPortSource:
246 tmp32 &= ~(TSC_DEBUG_MODE2_XNUR_200K_PULL_UP_MASK | TSC_DEBUG_MODE2_XNUR_PULL_UP_MASK |
247 TSC_DEBUG_MODE2_XNUR_PULL_DOWN_MASK);
248 tmp32 |= ((uint32_t)mode << TSC_DEBUG_MODE2_XNUR_PULL_DOWN_SHIFT);
249 break;
250 case kTSC_XpulPortSource:
251 tmp32 &= ~(TSC_DEBUG_MODE2_XPUL_200K_PULL_UP_MASK | TSC_DEBUG_MODE2_XPUL_PULL_UP_MASK |
252 TSC_DEBUG_MODE2_XPUL_PULL_DOWN_MASK);
253 tmp32 |= ((uint32_t)mode << TSC_DEBUG_MODE2_XPUL_PULL_DOWN_SHIFT);
254 break;
255 default:
256 assert(false);
257 break;
258 }
259 base->DEBUG_MODE2 = tmp32;
260 }
261