123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- #include "fsl_tsi_v4.h"
- #ifndef FSL_COMPONENT_ID
- #define FSL_COMPONENT_ID "platform.drivers.tsi_v4"
- #endif
- void TSI_Init(TSI_Type *base, const tsi_config_t *config)
- {
- assert(config != NULL);
- bool is_module_enabled = false;
- bool is_int_enabled = false;
- #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
- CLOCK_EnableClock(kCLOCK_Tsi0);
- #endif
- if (base->GENCS & TSI_GENCS_TSIEN_MASK)
- {
- is_module_enabled = true;
- TSI_EnableModule(base, false);
- }
- if (base->GENCS & TSI_GENCS_TSIIEN_MASK)
- {
- is_int_enabled = true;
- TSI_DisableInterrupts(base, kTSI_GlobalInterruptEnable);
- }
-
- if(config->mode == kTSI_AnalogModeSel_Capacitive)
- {
- TSI_SetHighThreshold(base, config->thresh);
- TSI_SetLowThreshold(base, config->thresl);
- TSI_SetElectrodeOSCPrescaler(base, config->prescaler);
- TSI_SetReferenceChargeCurrent(base, config->refchrg);
- TSI_SetElectrodeChargeCurrent(base, config->extchrg);
- TSI_SetNumberOfScans(base, config->nscn);
- TSI_SetAnalogMode(base, config->mode);
- TSI_SetOscVoltageRails(base, config->dvolt);
- }
- else
- {
- TSI_SetHighThreshold(base, config->thresh);
- TSI_SetLowThreshold(base, config->thresl);
- TSI_SetElectrodeOSCPrescaler(base, config->prescaler);
- TSI_SetReferenceChargeCurrent(base, config->refchrg);
- TSI_SetNumberOfScans(base, config->nscn);
- TSI_SetAnalogMode(base, config->mode);
- TSI_SetOscVoltageRails(base, config->dvolt);
- TSI_SetElectrodeSeriesResistor(base, config->resistor);
- TSI_SetFilterBits(base, config->filter);
- }
-
- if (is_module_enabled)
- {
- TSI_EnableModule(base, true);
- }
- if (is_int_enabled)
- {
- TSI_EnableInterrupts(base, kTSI_GlobalInterruptEnable);
- }
- }
- void TSI_Deinit(TSI_Type *base)
- {
- base->GENCS = 0U;
- base->DATA = 0U;
- base->TSHD = 0U;
- #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
- CLOCK_DisableClock(kCLOCK_Tsi0);
- #endif
- }
- void TSI_GetNormalModeDefaultConfig(tsi_config_t *userConfig)
- {
- userConfig->thresh = 0U;
- userConfig->thresl = 0U;
- userConfig->prescaler = kTSI_ElecOscPrescaler_2div;
- userConfig->extchrg = kTSI_ExtOscChargeCurrent_500nA;
- userConfig->refchrg = kTSI_RefOscChargeCurrent_4uA;
- userConfig->nscn = kTSI_ConsecutiveScansNumber_5time;
- userConfig->mode = kTSI_AnalogModeSel_Capacitive;
- userConfig->dvolt = kTSI_OscVolRailsOption_0;
- }
- void TSI_GetLowPowerModeDefaultConfig(tsi_config_t *userConfig)
- {
- userConfig->thresh = 400U;
- userConfig->thresl = 0U;
- userConfig->prescaler = kTSI_ElecOscPrescaler_2div;
- userConfig->extchrg = kTSI_ExtOscChargeCurrent_500nA;
- userConfig->refchrg = kTSI_RefOscChargeCurrent_4uA;
- userConfig->nscn = kTSI_ConsecutiveScansNumber_5time;
- userConfig->mode = kTSI_AnalogModeSel_Capacitive;
- userConfig->dvolt = kTSI_OscVolRailsOption_0;
- }
- void TSI_Calibrate(TSI_Type *base, tsi_calibration_data_t *calBuff)
- {
- assert(calBuff != NULL);
- uint8_t i = 0U;
- bool is_int_enabled = false;
- if (base->GENCS & TSI_GENCS_TSIIEN_MASK)
- {
- is_int_enabled = true;
- TSI_DisableInterrupts(base, kTSI_GlobalInterruptEnable);
- }
- for (i = 0U; i < FSL_FEATURE_TSI_CHANNEL_COUNT; i++)
- {
- TSI_SetMeasuredChannelNumber(base, i);
- TSI_StartSoftwareTrigger(base);
- while (!(TSI_GetStatusFlags(base) & kTSI_EndOfScanFlag))
- {
- }
- calBuff->calibratedData[i] = TSI_GetCounter(base);
- TSI_ClearStatusFlags(base, kTSI_EndOfScanFlag);
- }
- if (is_int_enabled)
- {
- TSI_EnableInterrupts(base, kTSI_GlobalInterruptEnable);
- }
- }
- void TSI_EnableInterrupts(TSI_Type *base, uint32_t mask)
- {
- uint32_t regValue = base->GENCS & (~ALL_FLAGS_MASK);
- if (mask & kTSI_GlobalInterruptEnable)
- {
- regValue |= TSI_GENCS_TSIIEN_MASK;
- }
- if (mask & kTSI_OutOfRangeInterruptEnable)
- {
- regValue &= (~TSI_GENCS_ESOR_MASK);
- }
- if (mask & kTSI_EndOfScanInterruptEnable)
- {
- regValue |= TSI_GENCS_ESOR_MASK;
- }
- base->GENCS = regValue;
- }
- void TSI_DisableInterrupts(TSI_Type *base, uint32_t mask)
- {
- uint32_t regValue = base->GENCS & (~ALL_FLAGS_MASK);
- if (mask & kTSI_GlobalInterruptEnable)
- {
- regValue &= (~TSI_GENCS_TSIIEN_MASK);
- }
- if (mask & kTSI_OutOfRangeInterruptEnable)
- {
- regValue |= TSI_GENCS_ESOR_MASK;
- }
- if (mask & kTSI_EndOfScanInterruptEnable)
- {
- regValue &= (~TSI_GENCS_ESOR_MASK);
- }
- base->GENCS = regValue;
- }
- void TSI_ClearStatusFlags(TSI_Type *base, uint32_t mask)
- {
- uint32_t regValue = base->GENCS & (~ALL_FLAGS_MASK);
- if (mask & kTSI_EndOfScanFlag)
- {
- regValue |= TSI_GENCS_EOSF_MASK;
- }
- if (mask & kTSI_OutOfRangeFlag)
- {
- regValue |= TSI_GENCS_OUTRGF_MASK;
- }
- base->GENCS = regValue;
- }
|