fsl_tsi_v4.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2014 - 2015, Freescale Semiconductor, Inc.
  4. * Copyright 2016-2017 NXP
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include "fsl_tsi_v4.h"
  35. /* Component ID definition, used by tools. */
  36. #ifndef FSL_COMPONENT_ID
  37. #define FSL_COMPONENT_ID "platform.drivers.tsi_v4"
  38. #endif
  39. void TSI_Init(TSI_Type *base, const tsi_config_t *config)
  40. {
  41. assert(config != NULL);
  42. bool is_module_enabled = false;
  43. bool is_int_enabled = false;
  44. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  45. CLOCK_EnableClock(kCLOCK_Tsi0);
  46. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  47. if (base->GENCS & TSI_GENCS_TSIEN_MASK)
  48. {
  49. is_module_enabled = true;
  50. TSI_EnableModule(base, false);
  51. }
  52. if (base->GENCS & TSI_GENCS_TSIIEN_MASK)
  53. {
  54. is_int_enabled = true;
  55. TSI_DisableInterrupts(base, kTSI_GlobalInterruptEnable);
  56. }
  57. if(config->mode == kTSI_AnalogModeSel_Capacitive)
  58. {
  59. TSI_SetHighThreshold(base, config->thresh);
  60. TSI_SetLowThreshold(base, config->thresl);
  61. TSI_SetElectrodeOSCPrescaler(base, config->prescaler);
  62. TSI_SetReferenceChargeCurrent(base, config->refchrg);
  63. TSI_SetElectrodeChargeCurrent(base, config->extchrg);
  64. TSI_SetNumberOfScans(base, config->nscn);
  65. TSI_SetAnalogMode(base, config->mode);
  66. TSI_SetOscVoltageRails(base, config->dvolt);
  67. }
  68. else /* For noise modes */
  69. {
  70. TSI_SetHighThreshold(base, config->thresh);
  71. TSI_SetLowThreshold(base, config->thresl);
  72. TSI_SetElectrodeOSCPrescaler(base, config->prescaler);
  73. TSI_SetReferenceChargeCurrent(base, config->refchrg);
  74. TSI_SetNumberOfScans(base, config->nscn);
  75. TSI_SetAnalogMode(base, config->mode);
  76. TSI_SetOscVoltageRails(base, config->dvolt);
  77. TSI_SetElectrodeSeriesResistor(base, config->resistor);
  78. TSI_SetFilterBits(base, config->filter);
  79. }
  80. if (is_module_enabled)
  81. {
  82. TSI_EnableModule(base, true);
  83. }
  84. if (is_int_enabled)
  85. {
  86. TSI_EnableInterrupts(base, kTSI_GlobalInterruptEnable);
  87. }
  88. }
  89. void TSI_Deinit(TSI_Type *base)
  90. {
  91. base->GENCS = 0U;
  92. base->DATA = 0U;
  93. base->TSHD = 0U;
  94. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  95. CLOCK_DisableClock(kCLOCK_Tsi0);
  96. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  97. }
  98. void TSI_GetNormalModeDefaultConfig(tsi_config_t *userConfig)
  99. {
  100. userConfig->thresh = 0U;
  101. userConfig->thresl = 0U;
  102. userConfig->prescaler = kTSI_ElecOscPrescaler_2div;
  103. userConfig->extchrg = kTSI_ExtOscChargeCurrent_500nA;
  104. userConfig->refchrg = kTSI_RefOscChargeCurrent_4uA;
  105. userConfig->nscn = kTSI_ConsecutiveScansNumber_5time;
  106. userConfig->mode = kTSI_AnalogModeSel_Capacitive;
  107. userConfig->dvolt = kTSI_OscVolRailsOption_0;
  108. }
  109. void TSI_GetLowPowerModeDefaultConfig(tsi_config_t *userConfig)
  110. {
  111. userConfig->thresh = 400U;
  112. userConfig->thresl = 0U;
  113. userConfig->prescaler = kTSI_ElecOscPrescaler_2div;
  114. userConfig->extchrg = kTSI_ExtOscChargeCurrent_500nA;
  115. userConfig->refchrg = kTSI_RefOscChargeCurrent_4uA;
  116. userConfig->nscn = kTSI_ConsecutiveScansNumber_5time;
  117. userConfig->mode = kTSI_AnalogModeSel_Capacitive;
  118. userConfig->dvolt = kTSI_OscVolRailsOption_0;
  119. }
  120. void TSI_Calibrate(TSI_Type *base, tsi_calibration_data_t *calBuff)
  121. {
  122. assert(calBuff != NULL);
  123. uint8_t i = 0U;
  124. bool is_int_enabled = false;
  125. if (base->GENCS & TSI_GENCS_TSIIEN_MASK)
  126. {
  127. is_int_enabled = true;
  128. TSI_DisableInterrupts(base, kTSI_GlobalInterruptEnable);
  129. }
  130. for (i = 0U; i < FSL_FEATURE_TSI_CHANNEL_COUNT; i++)
  131. {
  132. TSI_SetMeasuredChannelNumber(base, i);
  133. TSI_StartSoftwareTrigger(base);
  134. while (!(TSI_GetStatusFlags(base) & kTSI_EndOfScanFlag))
  135. {
  136. }
  137. calBuff->calibratedData[i] = TSI_GetCounter(base);
  138. TSI_ClearStatusFlags(base, kTSI_EndOfScanFlag);
  139. }
  140. if (is_int_enabled)
  141. {
  142. TSI_EnableInterrupts(base, kTSI_GlobalInterruptEnable);
  143. }
  144. }
  145. void TSI_EnableInterrupts(TSI_Type *base, uint32_t mask)
  146. {
  147. uint32_t regValue = base->GENCS & (~ALL_FLAGS_MASK);
  148. if (mask & kTSI_GlobalInterruptEnable)
  149. {
  150. regValue |= TSI_GENCS_TSIIEN_MASK;
  151. }
  152. if (mask & kTSI_OutOfRangeInterruptEnable)
  153. {
  154. regValue &= (~TSI_GENCS_ESOR_MASK);
  155. }
  156. if (mask & kTSI_EndOfScanInterruptEnable)
  157. {
  158. regValue |= TSI_GENCS_ESOR_MASK;
  159. }
  160. base->GENCS = regValue; /* write value to register */
  161. }
  162. void TSI_DisableInterrupts(TSI_Type *base, uint32_t mask)
  163. {
  164. uint32_t regValue = base->GENCS & (~ALL_FLAGS_MASK);
  165. if (mask & kTSI_GlobalInterruptEnable)
  166. {
  167. regValue &= (~TSI_GENCS_TSIIEN_MASK);
  168. }
  169. if (mask & kTSI_OutOfRangeInterruptEnable)
  170. {
  171. regValue |= TSI_GENCS_ESOR_MASK;
  172. }
  173. if (mask & kTSI_EndOfScanInterruptEnable)
  174. {
  175. regValue &= (~TSI_GENCS_ESOR_MASK);
  176. }
  177. base->GENCS = regValue; /* write value to register */
  178. }
  179. void TSI_ClearStatusFlags(TSI_Type *base, uint32_t mask)
  180. {
  181. uint32_t regValue = base->GENCS & (~ALL_FLAGS_MASK);
  182. if (mask & kTSI_EndOfScanFlag)
  183. {
  184. regValue |= TSI_GENCS_EOSF_MASK;
  185. }
  186. if (mask & kTSI_OutOfRangeFlag)
  187. {
  188. regValue |= TSI_GENCS_OUTRGF_MASK;
  189. }
  190. base->GENCS = regValue; /* write value to register */
  191. }