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 #include "fsl_tsi_v5.h"
9 
10 /* Component ID definition, used by tools. */
11 #ifndef FSL_COMPONENT_ID
12 #define FSL_COMPONENT_ID "platform.drivers.tsi_v5"
13 #endif
14 
15 /*******************************************************************************
16  * Prototypes
17  ******************************************************************************/
18 
19 /*******************************************************************************
20  * Variables
21  ******************************************************************************/
22 /* Array of TSI clock name. */
23 const clock_ip_name_t s_tsiClock[] = TSI_CLOCKS;
24 /* Array of TSI IRQ name. */
25 const IRQn_Type s_TsiIRQ[] = TSI_IRQS;
26 /* Array of TSI peripheral base address. */
27 TSI_Type *const s_tsiBases[] = TSI_BASE_PTRS;
28 /*******************************************************************************
29  * Code
30  ******************************************************************************/
31 /*!
32  * brief Get the TSI instance from peripheral base address.
33  *
34  * param base TSI peripheral base address.
35  * return TSI instance.
36  */
TSI_GetInstance(TSI_Type * base)37 uint32_t TSI_GetInstance(TSI_Type *base)
38 {
39     uint32_t instance;
40 
41     /* Find the instance index from base address mappings. */
42     for (instance = 0U; instance < ARRAY_SIZE(s_tsiBases); instance++)
43     {
44         if (s_tsiBases[instance] == base)
45         {
46             break;
47         }
48     }
49 
50     assert(instance < ARRAY_SIZE(s_tsiBases));
51 
52     return instance;
53 }
54 
55 /*!
56  * brief Initialize hardware to Self-cap mode.
57  *
58  * details Initialize the peripheral to the targeted state specified by parameter config,
59  *          such as sets sensitivity adjustment, current settings.
60  * param  base    TSI peripheral base address.
61  * param  config  Pointer to TSI self-cap configuration structure.
62  * return none
63  */
TSI_InitSelfCapMode(TSI_Type * base,const tsi_selfCap_config_t * config)64 void TSI_InitSelfCapMode(TSI_Type *base, const tsi_selfCap_config_t *config)
65 {
66     uint32_t temp = 0U;
67     assert(config != NULL);
68 
69     bool is_module_enabled = false;
70     bool is_int_enabled    = false;
71 
72 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
73     uint32_t instance = TSI_GetInstance(base);
74 
75     /* Enable tsi clock */
76     (void)CLOCK_EnableClock(s_tsiClock[instance]);
77 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
78     if ((bool)(base->GENCS & TSI_GENCS_TSIEN_MASK))
79     {
80         is_module_enabled = true;
81         TSI_EnableModule(base, false);
82     }
83     if ((bool)(base->GENCS & TSI_GENCS_TSIIEN_MASK))
84     {
85         is_int_enabled = true;
86         TSI_DisableInterrupts(base, (uint32_t)kTSI_GlobalInterruptEnable);
87     }
88 
89     /* common settings */
90 #if !(defined(FSL_FEATURE_TSI_HAS_SHIELD_REGISTER) && FSL_FEATURE_TSI_HAS_SHIELD_REGISTER)
91     temp = (base->MODE) & ~(TSI_MODE_SETCLK_MASK | TSI_MODE_MODE_MASK | TSI_MODE_S_SEN_MASK | TSI_MODE_S_W_SHIELD_MASK);
92     base->MODE = temp | (TSI_MODE_S_W_SHIELD(config->enableShield) | TSI_MODE_S_SEN(config->enableSensitivity) |
93                          TSI_MODE_SETCLK(config->commonConfig.mainClock) | TSI_MODE_MODE(config->commonConfig.mode));
94 #else
95     temp       = (base->MODE) & ~(TSI_MODE_SETCLK_MASK | TSI_MODE_MODE_MASK | TSI_MODE_S_SEN_MASK);
96     base->MODE = temp | (TSI_MODE_S_SEN(config->enableSensitivity) | TSI_MODE_SETCLK(config->commonConfig.mainClock) |
97                          TSI_MODE_MODE(config->commonConfig.mode));
98     base->SHIELD |= (uint32_t)config->enableShield;
99 #endif
100 
101     base->GENCS =
102         (base->GENCS & (~(ALL_FLAGS_MASK | TSI_GENCS_DVOLT_MASK))) | TSI_GENCS_DVOLT(config->commonConfig.dvolt);
103 
104     temp       = (base->SINC) & ~(TSI_SINC_CUTOFF_MASK | TSI_SINC_ORDER_MASK | TSI_SINC_DECIMATION_MASK);
105     base->SINC = temp | (TSI_SINC_CUTOFF(config->commonConfig.cutoff) | TSI_SINC_ORDER(config->commonConfig.order) |
106                          TSI_SINC_DECIMATION(config->commonConfig.decimation));
107 
108     temp = (base->SSC0) & ~(TSI_SSC0_CHARGE_NUM_MASK | TSI_SSC0_BASE_NOCHARGE_NUM_MASK | TSI_SSC0_PRBS_OUTSEL_MASK |
109                             TSI_SSC0_SSC_PRESCALE_NUM_MASK | TSI_SSC0_SSC_MODE_MASK);
110     base->SSC0 =
111         temp | (TSI_SSC0_PRBS_OUTSEL(config->commonConfig.prbsOutsel) |
112                 TSI_SSC0_SSC_MODE(config->commonConfig.ssc_mode) | TSI_SSC0_CHARGE_NUM(config->commonConfig.chargeNum) |
113                 TSI_SSC0_BASE_NOCHARGE_NUM(config->commonConfig.noChargeNum) |
114                 TSI_SSC0_SSC_PRESCALE_NUM(config->commonConfig.ssc_prescaler));
115 
116     /* Self-cap mode specific settings */
117     temp = (base->MODE) & ~(TSI_MODE_S_XDN_MASK | TSI_MODE_S_CTRIM_MASK | TSI_MODE_S_XIN_MASK | TSI_MODE_S_XCH_MASK);
118     base->MODE = temp | (TSI_MODE_S_XDN(config->xdn) | TSI_MODE_S_CTRIM(config->ctrim) |
119                          TSI_MODE_S_XIN(config->inputCurrent) | TSI_MODE_S_XCH(config->chargeCurrent));
120 
121     if (is_module_enabled)
122     {
123         TSI_EnableModule(base, true);
124     }
125     if (is_int_enabled)
126     {
127         TSI_EnableInterrupts(base, (uint32_t)kTSI_GlobalInterruptEnable);
128     }
129 }
130 
131 /*!
132  * brief Initialize hardware to Mutual-cap mode.
133  *
134  * details Initialize the peripheral to the targeted state specified by parameter config,
135  *          such as sets Vref generator setting, sensitivity boost settings, Pmos/Nmos settings.
136  * param  base    TSI peripheral base address.
137  * param  config  Pointer to TSI mutual-cap configuration structure.
138  * return none
139  */
TSI_InitMutualCapMode(TSI_Type * base,const tsi_mutualCap_config_t * config)140 void TSI_InitMutualCapMode(TSI_Type *base, const tsi_mutualCap_config_t *config)
141 {
142     uint32_t temp = 0U;
143     assert(config != NULL);
144 
145     bool is_module_enabled = false;
146     bool is_int_enabled    = false;
147 
148 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
149     uint32_t instance = TSI_GetInstance(base);
150 
151     /* Enable tsi clock */
152     (void)CLOCK_EnableClock(s_tsiClock[instance]);
153 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
154     if ((bool)(base->GENCS & TSI_GENCS_TSIEN_MASK))
155     {
156         is_module_enabled = true;
157         TSI_EnableModule(base, false);
158     }
159     if ((bool)(base->GENCS & (uint32_t)TSI_GENCS_TSIIEN_MASK))
160     {
161         is_int_enabled = true;
162         TSI_DisableInterrupts(base, (uint32_t)kTSI_GlobalInterruptEnable);
163     }
164 
165     /* Common settings */
166     temp       = (base->MODE) & ~(TSI_MODE_SETCLK_MASK | TSI_MODE_MODE_MASK);
167     base->MODE = temp | (TSI_MODE_SETCLK(config->commonConfig.mainClock) | TSI_MODE_MODE(config->commonConfig.mode));
168 
169     base->GENCS =
170         (base->GENCS & (~(ALL_FLAGS_MASK | TSI_GENCS_DVOLT_MASK))) | TSI_GENCS_DVOLT(config->commonConfig.dvolt);
171 
172     temp       = (base->SINC) & ~(TSI_SINC_CUTOFF_MASK | TSI_SINC_ORDER_MASK | TSI_SINC_DECIMATION_MASK);
173     base->SINC = temp | (TSI_SINC_CUTOFF(config->commonConfig.cutoff) | TSI_SINC_ORDER(config->commonConfig.order) |
174                          TSI_SINC_DECIMATION(config->commonConfig.decimation));
175 
176     temp = (base->SSC0) & ~(TSI_SSC0_CHARGE_NUM_MASK | TSI_SSC0_BASE_NOCHARGE_NUM_MASK | TSI_SSC0_PRBS_OUTSEL_MASK |
177                             TSI_SSC0_SSC_PRESCALE_NUM_MASK | TSI_SSC0_SSC_MODE_MASK);
178     base->SSC0 =
179         temp | (TSI_SSC0_PRBS_OUTSEL(config->commonConfig.prbsOutsel) |
180                 TSI_SSC0_SSC_MODE(config->commonConfig.ssc_mode) | TSI_SSC0_CHARGE_NUM(config->commonConfig.chargeNum) |
181                 TSI_SSC0_BASE_NOCHARGE_NUM(config->commonConfig.noChargeNum) |
182                 TSI_SSC0_SSC_PRESCALE_NUM(config->commonConfig.ssc_prescaler));
183 
184     /* Mutual-cap mode specific configurations */
185     temp       = (base->MUL0) & ~(TSI_MUL0_M_PRE_CURRENT_MASK | TSI_MUL0_M_PRE_RES_MASK | TSI_MUL0_M_SEN_RES_MASK);
186     base->MUL0 = temp | (TSI_MUL0_M_PRE_CURRENT(config->preCurrent) | TSI_MUL0_M_PRE_RES(config->preResistor) |
187                          TSI_MUL0_M_SEN_RES(config->senseResistor));
188 
189     temp       = (base->MUL1) & ~(TSI_MUL1_M_SEN_BOOST_MASK | TSI_MUL1_M_MODE_MASK | TSI_MUL1_M_PMIRRORL_MASK |
190                             TSI_MUL1_M_PMIRRORR_MASK | TSI_MUL1_M_NMIRROR_MASK);
191     base->MUL1 = temp | (TSI_MUL1_M_SEN_BOOST(config->boostCurrent) | TSI_MUL1_M_MODE(config->txDriveMode) |
192                          TSI_MUL1_M_PMIRRORL(config->pmosLeftCurrent) | TSI_MUL1_M_PMIRRORR(config->pmosRightCurrent) |
193                          TSI_MUL1_M_NMIRROR(config->nmosCurrent));
194 
195     if (is_module_enabled)
196     {
197         TSI_EnableModule(base, true);
198     }
199     if (is_int_enabled)
200     {
201         TSI_EnableInterrupts(base, (uint32_t)kTSI_GlobalInterruptEnable);
202     }
203 }
204 
205 /*!
206  * brief De-initialize hardware.
207  *
208  * details De-initialize the peripheral to default state.
209  *
210  * param  base  TSI peripheral base address.
211  * return none
212  */
TSI_Deinit(TSI_Type * base)213 void TSI_Deinit(TSI_Type *base)
214 {
215     base->GENCS = 0U;
216     base->DATA  = 0U;
217     base->TSHD  = 0U;
218 
219 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
220     uint32_t instance = TSI_GetInstance(base);
221 
222     /* Disable tsi clock */
223     (void)CLOCK_DisableClock(s_tsiClock[instance]);
224 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
225 }
226 
227 /*!
228  * brief Get TSI self-cap mode user configure structure.
229  * This interface sets userConfig structure to a default value. The configuration structure only
230  * includes the settings for the whole TSI.
231  * The user configure is set to a value:
232  * code
233     userConfig->commonConfig.mainClock     = kTSI_MainClockSlection_0;
234     userConfig->commonConfig.mode          = kTSI_SensingModeSlection_Self;
235     userConfig->commonConfig.dvolt         = kTSI_DvoltOption_2;
236     userConfig->commonConfig.cutoff        = kTSI_SincCutoffDiv_1;
237     userConfig->commonConfig.order         = kTSI_SincFilterOrder_1;
238     userConfig->commonConfig.decimation    = kTSI_SincDecimationValue_8;
239     userConfig->commonConfig.chargeNum     = kTSI_SscChargeNumValue_3;
240     userConfig->commonConfig.prbsOutsel    = kTSI_SscPrbsOutsel_2;
241     userConfig->commonConfig.noChargeNum   = kTSI_SscNoChargeNumValue_2;
242     userConfig->commonConfig.ssc_mode      = kTSI_ssc_prbs_method;
243     userConfig->commonConfig.ssc_prescaler = kTSI_ssc_div_by_1;
244     userConfig->enableSensitivity          = true;
245     userConfig->enableShield               = false;
246     userConfig->xdn                        = kTSI_SensitivityXdnOption_1;
247     userConfig->ctrim                      = kTSI_SensitivityCtrimOption_7;
248     userConfig->inputCurrent               = kTSI_CurrentMultipleInputValue_0;
249     userConfig->chargeCurrent              = kTSI_CurrentMultipleChargeValue_1;
250    endcode
251  *
252  * param userConfig Pointer to TSI user configure structure.
253  */
TSI_GetSelfCapModeDefaultConfig(tsi_selfCap_config_t * userConfig)254 void TSI_GetSelfCapModeDefaultConfig(tsi_selfCap_config_t *userConfig)
255 {
256     /* Initializes the configure structure to zero. */
257     (void)memset(userConfig, 0, sizeof(*userConfig));
258 
259     userConfig->commonConfig.mainClock     = kTSI_MainClockSlection_0;
260     userConfig->commonConfig.mode          = kTSI_SensingModeSlection_Self;
261     userConfig->commonConfig.dvolt         = kTSI_DvoltOption_2;
262     userConfig->commonConfig.cutoff        = kTSI_SincCutoffDiv_1;
263     userConfig->commonConfig.order         = kTSI_SincFilterOrder_1;
264     userConfig->commonConfig.decimation    = kTSI_SincDecimationValue_8;
265     userConfig->commonConfig.chargeNum     = kTSI_SscChargeNumValue_3;
266     userConfig->commonConfig.prbsOutsel    = kTSI_SscPrbsOutsel_2;
267     userConfig->commonConfig.noChargeNum   = kTSI_SscNoChargeNumValue_2;
268     userConfig->commonConfig.ssc_mode      = kTSI_ssc_prbs_method;
269     userConfig->commonConfig.ssc_prescaler = kTSI_ssc_div_by_1;
270     userConfig->enableSensitivity          = true;
271     userConfig->enableShield               = kTSI_shieldAllOff;
272     userConfig->xdn                        = kTSI_SensitivityXdnOption_1;
273     userConfig->ctrim                      = kTSI_SensitivityCtrimOption_7;
274     userConfig->inputCurrent               = kTSI_CurrentMultipleInputValue_0;
275     userConfig->chargeCurrent              = kTSI_CurrentMultipleChargeValue_1;
276 }
277 
278 /*!
279  * brief Get TSI mutual-cap mode default user configure structure.
280  * This interface sets userConfig structure to a default value. The configuration structure only
281  * includes the settings for the whole TSI.
282  * The user configure is set to a value:
283  * code
284     userConfig->commonConfig.mainClock     = kTSI_MainClockSlection_1;
285     userConfig->commonConfig.mode          = kTSI_SensingModeSlection_Mutual;
286     userConfig->commonConfig.dvolt         = kTSI_DvoltOption_0;
287     userConfig->commonConfig.cutoff        = kTSI_SincCutoffDiv_1;
288     userConfig->commonConfig.order         = kTSI_SincFilterOrder_1;
289     userConfig->commonConfig.decimation    = kTSI_SincDecimationValue_8;
290     userConfig->commonConfig.chargeNum     = kTSI_SscChargeNumValue_4;
291     userConfig->commonConfig.prbsOutsel    = kTSI_SscPrbsOutsel_2;
292     userConfig->commonConfig.noChargeNum   = kTSI_SscNoChargeNumValue_5;
293     userConfig->commonConfig.ssc_mode      = kTSI_ssc_prbs_method;
294     userConfig->commonConfig.ssc_prescaler = kTSI_ssc_div_by_1;
295     userConfig->preCurrent                 = kTSI_MutualPreCurrent_4uA;
296     userConfig->preResistor                = kTSI_MutualPreResistor_4k;
297     userConfig->senseResistor              = kTSI_MutualSenseResistor_10k;
298     userConfig->boostCurrent               = kTSI_MutualSenseBoostCurrent_0uA;
299     userConfig->txDriveMode                = kTSI_MutualTxDriveModeOption_0;
300     userConfig->pmosLeftCurrent            = kTSI_MutualPmosCurrentMirrorLeft_32;
301     userConfig->pmosRightCurrent           = kTSI_MutualPmosCurrentMirrorRight_1;
302     userConfig->enableNmosMirror           = true;
303     userConfig->nmosCurrent                = kTSI_MutualNmosCurrentMirror_1;
304    endcode
305  *
306  * param userConfig Pointer to TSI user configure structure.
307  */
TSI_GetMutualCapModeDefaultConfig(tsi_mutualCap_config_t * userConfig)308 void TSI_GetMutualCapModeDefaultConfig(tsi_mutualCap_config_t *userConfig)
309 {
310     /* Initializes the configure structure to zero. */
311     (void)memset(userConfig, 0, sizeof(*userConfig));
312 
313     userConfig->commonConfig.mainClock     = kTSI_MainClockSlection_1;
314     userConfig->commonConfig.mode          = kTSI_SensingModeSlection_Mutual;
315     userConfig->commonConfig.dvolt         = kTSI_DvoltOption_0;
316     userConfig->commonConfig.cutoff        = kTSI_SincCutoffDiv_1;
317     userConfig->commonConfig.order         = kTSI_SincFilterOrder_1;
318     userConfig->commonConfig.decimation    = kTSI_SincDecimationValue_8;
319     userConfig->commonConfig.chargeNum     = kTSI_SscChargeNumValue_4;
320     userConfig->commonConfig.prbsOutsel    = kTSI_SscPrbsOutsel_2;
321     userConfig->commonConfig.noChargeNum   = kTSI_SscNoChargeNumValue_5;
322     userConfig->commonConfig.ssc_mode      = kTSI_ssc_prbs_method;
323     userConfig->commonConfig.ssc_prescaler = kTSI_ssc_div_by_1;
324     userConfig->preCurrent                 = kTSI_MutualPreCurrent_4uA;
325     userConfig->preResistor                = kTSI_MutualPreResistor_4k;
326     userConfig->senseResistor              = kTSI_MutualSenseResistor_10k;
327     userConfig->boostCurrent               = kTSI_MutualSenseBoostCurrent_0uA;
328     userConfig->txDriveMode                = kTSI_MutualTxDriveModeOption_0;
329     userConfig->pmosLeftCurrent            = kTSI_MutualPmosCurrentMirrorLeft_32;
330     userConfig->pmosRightCurrent           = kTSI_MutualPmosCurrentMirrorRight_1;
331     userConfig->enableNmosMirror           = true;
332     userConfig->nmosCurrent                = kTSI_MutualNmosCurrentMirror_1;
333 }
334 
335 /*!
336  * brief Hardware base counter value for calibration.
337  *
338  * details Calibrate the peripheral to fetch the initial counter value of
339  *          the enabled channels.
340  *          This API is mostly used at initial application setup, it shall be called
341  *          after the \ref TSI_Init API, then user can use the calibrated
342  *          counter values to setup applications(such as to determine
343  *          under which counter value we can confirm a touch event occurs).
344  *
345  * param   base    TSI peripheral base address.
346  * param   calBuff Data buffer that store the calibrated counter value.
347  * return  none
348  * note    This API is mainly used for self-cap mode;
349  * note    The calibration work in mutual-cap mode shall be done in applications due to different board layout.
350  *
351  */
TSI_SelfCapCalibrate(TSI_Type * base,tsi_calibration_data_t * calBuff)352 void TSI_SelfCapCalibrate(TSI_Type *base, tsi_calibration_data_t *calBuff)
353 {
354     assert(calBuff != NULL);
355 
356     uint8_t i           = 0U;
357     bool is_int_enabled = false;
358 
359     if ((bool)(base->GENCS & TSI_GENCS_TSIIEN_MASK))
360     {
361         is_int_enabled = true;
362         TSI_DisableInterrupts(base, (uint32_t)kTSI_GlobalInterruptEnable);
363     }
364     for (i = 0U; i < (uint8_t)FSL_FEATURE_TSI_CHANNEL_COUNT; i++)
365     {
366         TSI_SetSelfCapMeasuredChannel(base, i);
367         TSI_StartSoftwareTrigger(base);
368         while (!(bool)((TSI_GetStatusFlags(base) & (uint32_t)kTSI_EndOfScanFlag)))
369         {
370         }
371         calBuff->calibratedData[i] = TSI_GetCounter(base);
372         TSI_ClearStatusFlags(base, (uint32_t)kTSI_EndOfScanFlag);
373     }
374     if (is_int_enabled)
375     {
376         TSI_EnableInterrupts(base, (uint32_t)kTSI_GlobalInterruptEnable);
377     }
378 }
379 
380 /*!
381  * brief Enables TSI interrupt requests.
382  * param base TSI peripheral base address.
383  * param mask interrupt source
384  *     The parameter can be combination of the following source if defined:
385  *     arg kTSI_GlobalInterruptEnable
386  *     arg kTSI_EndOfScanInterruptEnable
387  *     arg kTSI_OutOfRangeInterruptEnable
388  */
TSI_EnableInterrupts(TSI_Type * base,uint32_t mask)389 void TSI_EnableInterrupts(TSI_Type *base, uint32_t mask)
390 {
391     uint32_t regValue = base->GENCS & (~ALL_FLAGS_MASK);
392 
393     if ((bool)(mask & (uint32_t)kTSI_GlobalInterruptEnable))
394     {
395         regValue |= TSI_GENCS_TSIIEN_MASK;
396     }
397     if ((bool)(mask & (uint32_t)kTSI_OutOfRangeInterruptEnable))
398     {
399         regValue &= (~TSI_GENCS_ESOR_MASK);
400     }
401     if ((bool)(mask & (uint32_t)kTSI_EndOfScanInterruptEnable))
402     {
403         regValue |= TSI_GENCS_ESOR_MASK;
404     }
405 
406     base->GENCS = regValue; /* write value to register */
407 }
408 
409 /*!
410  * brief Disables TSI interrupt requests.
411  * param base TSI peripheral base address.
412  * param mask interrupt source
413  *     The parameter can be combination of the following source if defined:
414  *     arg kTSI_GlobalInterruptEnable
415  *     arg kTSI_EndOfScanInterruptEnable
416  *     arg kTSI_OutOfRangeInterruptEnable
417  */
TSI_DisableInterrupts(TSI_Type * base,uint32_t mask)418 void TSI_DisableInterrupts(TSI_Type *base, uint32_t mask)
419 {
420     uint32_t regValue = base->GENCS & (~ALL_FLAGS_MASK);
421 
422     if ((bool)(mask & (uint32_t)kTSI_GlobalInterruptEnable))
423     {
424         regValue &= (~TSI_GENCS_TSIIEN_MASK);
425     }
426     if ((bool)(mask & (uint32_t)kTSI_OutOfRangeInterruptEnable))
427     {
428         regValue |= TSI_GENCS_ESOR_MASK;
429     }
430     if ((bool)(mask & (uint32_t)kTSI_EndOfScanInterruptEnable))
431     {
432         regValue &= (~TSI_GENCS_ESOR_MASK);
433     }
434 
435     base->GENCS = regValue; /* write value to register */
436 }
437 
438 /*!
439  * brief Clear interrupt flag.
440  *
441  * This function clear tsi interrupt flag,
442  * automatically cleared flags can not be cleared by this function.
443  *
444  * param base TSI peripheral base address.
445  * param mask The status flags to clear.
446  */
TSI_ClearStatusFlags(TSI_Type * base,uint32_t mask)447 void TSI_ClearStatusFlags(TSI_Type *base, uint32_t mask)
448 {
449     uint32_t regValue = base->GENCS & (~ALL_FLAGS_MASK);
450 
451     if ((bool)(mask & (uint32_t)kTSI_EndOfScanFlag))
452     {
453         regValue |= TSI_GENCS_EOSF_MASK;
454     }
455     if ((bool)(mask & (uint32_t)kTSI_OutOfRangeFlag))
456     {
457         regValue |= TSI_GENCS_OUTRGF_MASK;
458     }
459 
460     base->GENCS = regValue; /* write value to register */
461 }
462 
463 #if defined(FSL_FEATURE_TSI_HAS_SHIELD_REGISTER) && FSL_FEATURE_TSI_HAS_SHIELD_REGISTER
464 /*!
465  * brief Enable/disable TSI shield channels.
466  *
467  * Configure relevant channels according to the channelsMask value
468  *
469  * param base TSI peripheral base address.
470  * param channelsMask Channels mask, 1 means channel 0, 3 means channel 0 and channel 1.
471  * param enable True means enable TSI shield channels, false means disable.
472  */
TSI_EnableShieldChannels(TSI_Type * base,uint32_t channelsMask,bool enable)473 void TSI_EnableShieldChannels(TSI_Type *base, uint32_t channelsMask, bool enable)
474 {
475     if (enable)
476     {
477         base->SHIELD |= channelsMask;
478     }
479     else
480     {
481         base->SHIELD &= ~channelsMask;
482     }
483 }
484 
485 /*!
486  * brief Set TSI shield channels.
487  *
488  * Configure all channels according to the channelsMask value
489  *
490  * param base TSI peripheral base address.
491  * param channelsMask Channels mask, 0 means disable all channels, 3 means enable channel 1 and channel 2, disable other
492  * channels.
493  */
TSI_ShieldChannelConfig(TSI_Type * base,uint32_t channelsMask)494 void TSI_ShieldChannelConfig(TSI_Type *base, uint32_t channelsMask)
495 {
496     base->SHIELD = (base->SHIELD & (uint32_t)kTSI_shieldAllOff) | channelsMask;
497 }
498 #endif
499