fsl_tsi_v4.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 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. #ifndef _FSL_TSI_V4_H_
  35. #define _FSL_TSI_V4_H_
  36. #include "fsl_common.h"
  37. /*!
  38. * @addtogroup tsi_v4_driver
  39. * @{
  40. */
  41. /*******************************************************************************
  42. * Definitions
  43. ******************************************************************************/
  44. /*! @name Driver version */
  45. /*@{*/
  46. /*! @brief TSI driver version */
  47. #define FSL_TSI_DRIVER_VERSION (MAKE_VERSION(2, 1, 2))
  48. /*@}*/
  49. /*! @brief TSI status flags macro collection */
  50. #define ALL_FLAGS_MASK (TSI_GENCS_EOSF_MASK | TSI_GENCS_OUTRGF_MASK)
  51. /*! @brief resistor bit shift in EXTCHRG bit-field */
  52. #define TSI_V4_EXTCHRG_RESISTOR_BIT_SHIFT TSI_GENCS_EXTCHRG_SHIFT
  53. /*! @brief filter bits shift in EXTCHRG bit-field */
  54. #define TSI_V4_EXTCHRG_FILTER_BITS_SHIFT (1U + TSI_GENCS_EXTCHRG_SHIFT)
  55. /*! @brief macro of clearing the resistor bit in EXTCHRG bit-field */
  56. #define TSI_V4_EXTCHRG_RESISTOR_BIT_CLEAR \
  57. ((uint32_t)((~(ALL_FLAGS_MASK | TSI_GENCS_EXTCHRG_MASK)) | (3U << TSI_V4_EXTCHRG_FILTER_BITS_SHIFT)))
  58. /*! @brief macro of clearing the filter bits in EXTCHRG bit-field */
  59. #define TSI_V4_EXTCHRG_FILTER_BITS_CLEAR \
  60. ((uint32_t)((~(ALL_FLAGS_MASK | TSI_GENCS_EXTCHRG_MASK)) | (1U << TSI_V4_EXTCHRG_RESISTOR_BIT_SHIFT)))
  61. /*!
  62. * @brief TSI number of scan intervals for each electrode.
  63. *
  64. * These constants define the tsi number of consecutive scans in a TSI instance for each electrode.
  65. */
  66. typedef enum _tsi_n_consecutive_scans
  67. {
  68. kTSI_ConsecutiveScansNumber_1time = 0U, /*!< Once per electrode */
  69. kTSI_ConsecutiveScansNumber_2time = 1U, /*!< Twice per electrode */
  70. kTSI_ConsecutiveScansNumber_3time = 2U, /*!< 3 times consecutive scan */
  71. kTSI_ConsecutiveScansNumber_4time = 3U, /*!< 4 times consecutive scan */
  72. kTSI_ConsecutiveScansNumber_5time = 4U, /*!< 5 times consecutive scan */
  73. kTSI_ConsecutiveScansNumber_6time = 5U, /*!< 6 times consecutive scan */
  74. kTSI_ConsecutiveScansNumber_7time = 6U, /*!< 7 times consecutive scan */
  75. kTSI_ConsecutiveScansNumber_8time = 7U, /*!< 8 times consecutive scan */
  76. kTSI_ConsecutiveScansNumber_9time = 8U, /*!< 9 times consecutive scan */
  77. kTSI_ConsecutiveScansNumber_10time = 9U, /*!< 10 times consecutive scan */
  78. kTSI_ConsecutiveScansNumber_11time = 10U, /*!< 11 times consecutive scan */
  79. kTSI_ConsecutiveScansNumber_12time = 11U, /*!< 12 times consecutive scan */
  80. kTSI_ConsecutiveScansNumber_13time = 12U, /*!< 13 times consecutive scan */
  81. kTSI_ConsecutiveScansNumber_14time = 13U, /*!< 14 times consecutive scan */
  82. kTSI_ConsecutiveScansNumber_15time = 14U, /*!< 15 times consecutive scan */
  83. kTSI_ConsecutiveScansNumber_16time = 15U, /*!< 16 times consecutive scan */
  84. kTSI_ConsecutiveScansNumber_17time = 16U, /*!< 17 times consecutive scan */
  85. kTSI_ConsecutiveScansNumber_18time = 17U, /*!< 18 times consecutive scan */
  86. kTSI_ConsecutiveScansNumber_19time = 18U, /*!< 19 times consecutive scan */
  87. kTSI_ConsecutiveScansNumber_20time = 19U, /*!< 20 times consecutive scan */
  88. kTSI_ConsecutiveScansNumber_21time = 20U, /*!< 21 times consecutive scan */
  89. kTSI_ConsecutiveScansNumber_22time = 21U, /*!< 22 times consecutive scan */
  90. kTSI_ConsecutiveScansNumber_23time = 22U, /*!< 23 times consecutive scan */
  91. kTSI_ConsecutiveScansNumber_24time = 23U, /*!< 24 times consecutive scan */
  92. kTSI_ConsecutiveScansNumber_25time = 24U, /*!< 25 times consecutive scan */
  93. kTSI_ConsecutiveScansNumber_26time = 25U, /*!< 26 times consecutive scan */
  94. kTSI_ConsecutiveScansNumber_27time = 26U, /*!< 27 times consecutive scan */
  95. kTSI_ConsecutiveScansNumber_28time = 27U, /*!< 28 times consecutive scan */
  96. kTSI_ConsecutiveScansNumber_29time = 28U, /*!< 29 times consecutive scan */
  97. kTSI_ConsecutiveScansNumber_30time = 29U, /*!< 30 times consecutive scan */
  98. kTSI_ConsecutiveScansNumber_31time = 30U, /*!< 31 times consecutive scan */
  99. kTSI_ConsecutiveScansNumber_32time = 31U /*!< 32 times consecutive scan */
  100. } tsi_n_consecutive_scans_t;
  101. /*!
  102. * @brief TSI electrode oscillator prescaler.
  103. *
  104. * These constants define the TSI electrode oscillator prescaler in a TSI instance.
  105. */
  106. typedef enum _tsi_electrode_osc_prescaler
  107. {
  108. kTSI_ElecOscPrescaler_1div = 0U, /*!< Electrode oscillator frequency divided by 1 */
  109. kTSI_ElecOscPrescaler_2div = 1U, /*!< Electrode oscillator frequency divided by 2 */
  110. kTSI_ElecOscPrescaler_4div = 2U, /*!< Electrode oscillator frequency divided by 4 */
  111. kTSI_ElecOscPrescaler_8div = 3U, /*!< Electrode oscillator frequency divided by 8 */
  112. kTSI_ElecOscPrescaler_16div = 4U, /*!< Electrode oscillator frequency divided by 16 */
  113. kTSI_ElecOscPrescaler_32div = 5U, /*!< Electrode oscillator frequency divided by 32 */
  114. kTSI_ElecOscPrescaler_64div = 6U, /*!< Electrode oscillator frequency divided by 64 */
  115. kTSI_ElecOscPrescaler_128div = 7U /*!< Electrode oscillator frequency divided by 128 */
  116. } tsi_electrode_osc_prescaler_t;
  117. /*!
  118. * @brief TSI analog mode select.
  119. *
  120. * Set up TSI analog modes in a TSI instance.
  121. */
  122. typedef enum _tsi_analog_mode
  123. {
  124. kTSI_AnalogModeSel_Capacitive = 0U, /*!< Active TSI capacitive sensing mode */
  125. kTSI_AnalogModeSel_NoiseNoFreqLim = 4U, /*!< Single threshold noise detection mode with no freq. limitation. */
  126. kTSI_AnalogModeSel_NoiseFreqLim = 8U, /*!< Single threshold noise detection mode with freq. limitation. */
  127. kTSI_AnalogModeSel_AutoNoise = 12U /*!< Active TSI analog in automatic noise detection mode */
  128. } tsi_analog_mode_t;
  129. /*!
  130. * @brief TSI Reference oscillator charge and discharge current select.
  131. *
  132. * These constants define the TSI Reference oscillator charge current select in a TSI (REFCHRG) instance.
  133. */
  134. typedef enum _tsi_reference_osc_charge_current
  135. {
  136. kTSI_RefOscChargeCurrent_500nA = 0U, /*!< Reference oscillator charge current is 500 µA */
  137. kTSI_RefOscChargeCurrent_1uA = 1U, /*!< Reference oscillator charge current is 1 µA */
  138. kTSI_RefOscChargeCurrent_2uA = 2U, /*!< Reference oscillator charge current is 2 µA */
  139. kTSI_RefOscChargeCurrent_4uA = 3U, /*!< Reference oscillator charge current is 4 µA */
  140. kTSI_RefOscChargeCurrent_8uA = 4U, /*!< Reference oscillator charge current is 8 µA */
  141. kTSI_RefOscChargeCurrent_16uA = 5U, /*!< Reference oscillator charge current is 16 µA */
  142. kTSI_RefOscChargeCurrent_32uA = 6U, /*!< Reference oscillator charge current is 32 µA */
  143. kTSI_RefOscChargeCurrent_64uA = 7U /*!< Reference oscillator charge current is 64 µA */
  144. } tsi_reference_osc_charge_current_t;
  145. /*!
  146. * @brief TSI oscilator's voltage rails.
  147. *
  148. * These bits indicate the oscillator's voltage rails.
  149. */
  150. typedef enum _tsi_osc_voltage_rails
  151. {
  152. kTSI_OscVolRailsOption_0 = 0U, /*!< DVOLT value option 0, the value may differ on different platforms */
  153. kTSI_OscVolRailsOption_1 = 1U, /*!< DVOLT value option 1, the value may differ on different platforms */
  154. kTSI_OscVolRailsOption_2 = 2U, /*!< DVOLT value option 2, the value may differ on different platforms */
  155. kTSI_OscVolRailsOption_3 = 3U /*!< DVOLT value option 3, the value may differ on different platforms */
  156. } tsi_osc_voltage_rails_t;
  157. /*!
  158. * @brief TSI External oscillator charge and discharge current select.
  159. *
  160. * These bits indicate the electrode oscillator charge and discharge current value
  161. * in TSI (EXTCHRG) instance.
  162. */
  163. typedef enum _tsi_external_osc_charge_current
  164. {
  165. kTSI_ExtOscChargeCurrent_500nA = 0U, /*!< External oscillator charge current is 500 µA */
  166. kTSI_ExtOscChargeCurrent_1uA = 1U, /*!< External oscillator charge current is 1 µA */
  167. kTSI_ExtOscChargeCurrent_2uA = 2U, /*!< External oscillator charge current is 2 µA */
  168. kTSI_ExtOscChargeCurrent_4uA = 3U, /*!< External oscillator charge current is 4 µA */
  169. kTSI_ExtOscChargeCurrent_8uA = 4U, /*!< External oscillator charge current is 8 µA */
  170. kTSI_ExtOscChargeCurrent_16uA = 5U, /*!< External oscillator charge current is 16 µA */
  171. kTSI_ExtOscChargeCurrent_32uA = 6U, /*!< External oscillator charge current is 32 µA */
  172. kTSI_ExtOscChargeCurrent_64uA = 7U /*!< External oscillator charge current is 64 µA */
  173. } tsi_external_osc_charge_current_t;
  174. /*!
  175. * @brief TSI series resistance RS value select.
  176. *
  177. * These bits indicate the electrode RS series resistance for the noise mode
  178. * in TSI (EXTCHRG) instance.
  179. */
  180. typedef enum _tsi_series_resistance
  181. {
  182. kTSI_SeriesResistance_32k = 0U, /*!< Series Resistance is 32 kilo ohms */
  183. kTSI_SeriesResistance_187k = 1U /*!< Series Resistance is 18 7 kilo ohms */
  184. } tsi_series_resistor_t;
  185. /*!
  186. * @brief TSI series filter bits select.
  187. *
  188. * These bits indicate the count of the filter bits
  189. * in TSI noise mode EXTCHRG[2:1] bits
  190. */
  191. typedef enum _tsi_filter_bits
  192. {
  193. kTSI_FilterBits_3 = 0U, /*!< 3 filter bits, 8 peaks increments the cnt+1 */
  194. kTSI_FilterBits_2 = 1U, /*!< 2 filter bits, 4 peaks increments the cnt+1 */
  195. kTSI_FilterBits_1 = 2U, /*!< 1 filter bits, 2 peaks increments the cnt+1 */
  196. kTSI_FilterBits_0 = 3U /*!< no filter bits,1 peak increments the cnt+1 */
  197. } tsi_filter_bits_t;
  198. /*! @brief TSI status flags. */
  199. typedef enum _tsi_status_flags
  200. {
  201. kTSI_EndOfScanFlag = TSI_GENCS_EOSF_MASK, /*!< End-Of-Scan flag */
  202. kTSI_OutOfRangeFlag = TSI_GENCS_OUTRGF_MASK /*!< Out-Of-Range flag */
  203. } tsi_status_flags_t;
  204. /*! @brief TSI feature interrupt source.*/
  205. typedef enum _tsi_interrupt_enable
  206. {
  207. kTSI_GlobalInterruptEnable = 1U, /*!< TSI module global interrupt */
  208. kTSI_OutOfRangeInterruptEnable = 2U, /*!< Out-Of-Range interrupt */
  209. kTSI_EndOfScanInterruptEnable = 4U /*!< End-Of-Scan interrupt */
  210. } tsi_interrupt_enable_t;
  211. /*! @brief TSI calibration data storage. */
  212. typedef struct _tsi_calibration_data
  213. {
  214. uint16_t calibratedData[FSL_FEATURE_TSI_CHANNEL_COUNT]; /*!< TSI calibration data storage buffer */
  215. } tsi_calibration_data_t;
  216. /*!
  217. * @brief TSI configuration structure.
  218. *
  219. * This structure contains the settings for the most common TSI configurations including
  220. * the TSI module charge currents, number of scans, thresholds, and so on.
  221. */
  222. typedef struct _tsi_config
  223. {
  224. uint16_t thresh; /*!< High threshold. */
  225. uint16_t thresl; /*!< Low threshold. */
  226. tsi_electrode_osc_prescaler_t prescaler; /*!< Prescaler */
  227. tsi_external_osc_charge_current_t extchrg; /*!< Electrode charge current */
  228. tsi_reference_osc_charge_current_t refchrg; /*!< Reference charge current */
  229. tsi_n_consecutive_scans_t nscn; /*!< Number of scans. */
  230. tsi_analog_mode_t mode; /*!< TSI mode of operation. */
  231. tsi_osc_voltage_rails_t dvolt; /*!< Oscillator's voltage rails. */
  232. tsi_series_resistor_t resistor; /*!< Series resistance value */
  233. tsi_filter_bits_t filter; /*!< Noise mode filter bits */
  234. } tsi_config_t;
  235. /*******************************************************************************
  236. * API
  237. ******************************************************************************/
  238. #ifdef __cplusplus
  239. extern "C" {
  240. #endif
  241. /*!
  242. * @brief Initializes hardware.
  243. *
  244. * @details Initializes the peripheral to the targeted state specified by parameter configuration,
  245. * such as sets prescalers, number of scans, clocks, delta voltage
  246. * series resistor, filter bits, reference, and electrode charge current and threshold.
  247. * @param base TSI peripheral base address.
  248. * @param config Pointer to TSI module configuration structure.
  249. * @return none
  250. */
  251. void TSI_Init(TSI_Type *base, const tsi_config_t *config);
  252. /*!
  253. * @brief De-initializes hardware.
  254. *
  255. * @details De-initializes the peripheral to default state.
  256. *
  257. * @param base TSI peripheral base address.
  258. * @return none
  259. */
  260. void TSI_Deinit(TSI_Type *base);
  261. /*!
  262. * @brief Gets the TSI normal mode user configuration structure.
  263. * This interface sets userConfig structure to a default value. The configuration structure only
  264. * includes the settings for the whole TSI.
  265. * The user configure is set to these values:
  266. * @code
  267. userConfig->prescaler = kTSI_ElecOscPrescaler_2div;
  268. userConfig->extchrg = kTSI_ExtOscChargeCurrent_500nA;
  269. userConfig->refchrg = kTSI_RefOscChargeCurrent_4uA;
  270. userConfig->nscn = kTSI_ConsecutiveScansNumber_10time;
  271. userConfig->mode = kTSI_AnalogModeSel_Capacitive;
  272. userConfig->dvolt = kTSI_OscVolRailsOption_0;
  273. userConfig->thresh = 0U;
  274. userConfig->thresl = 0U;
  275. @endcode
  276. *
  277. * @param userConfig Pointer to the TSI user configuration structure.
  278. */
  279. void TSI_GetNormalModeDefaultConfig(tsi_config_t *userConfig);
  280. /*!
  281. * @brief Gets the TSI low power mode default user configuration structure.
  282. * This interface sets userConfig structure to a default value. The configuration structure only
  283. * includes the settings for the whole TSI.
  284. * The user configure is set to these values:
  285. * @code
  286. userConfig->prescaler = kTSI_ElecOscPrescaler_2div;
  287. userConfig->extchrg = kTSI_ExtOscChargeCurrent_500nA;
  288. userConfig->refchrg = kTSI_RefOscChargeCurrent_4uA;
  289. userConfig->nscn = kTSI_ConsecutiveScansNumber_10time;
  290. userConfig->mode = kTSI_AnalogModeSel_Capacitive;
  291. userConfig->dvolt = kTSI_OscVolRailsOption_0;
  292. userConfig->thresh = 400U;
  293. userConfig->thresl = 0U;
  294. @endcode
  295. *
  296. * @param userConfig Pointer to the TSI user configuration structure.
  297. */
  298. void TSI_GetLowPowerModeDefaultConfig(tsi_config_t *userConfig);
  299. /*!
  300. * @brief Hardware calibration.
  301. *
  302. * @details Calibrates the peripheral to fetch the initial counter value of
  303. * the enabled electrodes.
  304. * This API is mostly used at initial application setup. Call
  305. * this function after the \ref TSI_Init API and use the calibrated
  306. * counter values to set up applications (such as to determine
  307. * under which counter value we can confirm a touch event occurs).
  308. *
  309. * @param base TSI peripheral base address.
  310. * @param calBuff Data buffer that store the calibrated counter value.
  311. * @return none
  312. *
  313. */
  314. void TSI_Calibrate(TSI_Type *base, tsi_calibration_data_t *calBuff);
  315. /*!
  316. * @brief Enables the TSI interrupt requests.
  317. * @param base TSI peripheral base address.
  318. * @param mask interrupt source
  319. * The parameter can be combination of the following source if defined:
  320. * @arg kTSI_GlobalInterruptEnable
  321. * @arg kTSI_EndOfScanInterruptEnable
  322. * @arg kTSI_OutOfRangeInterruptEnable
  323. */
  324. void TSI_EnableInterrupts(TSI_Type *base, uint32_t mask);
  325. /*!
  326. * @brief Disables the TSI interrupt requests.
  327. * @param base TSI peripheral base address.
  328. * @param mask interrupt source
  329. * The parameter can be combination of the following source if defined:
  330. * @arg kTSI_GlobalInterruptEnable
  331. * @arg kTSI_EndOfScanInterruptEnable
  332. * @arg kTSI_OutOfRangeInterruptEnable
  333. */
  334. void TSI_DisableInterrupts(TSI_Type *base, uint32_t mask);
  335. /*!
  336. * @brief Gets an interrupt flag.
  337. * This function gets the TSI interrupt flags.
  338. *
  339. * @param base TSI peripheral base address.
  340. * @return The mask of these status flags combination.
  341. */
  342. static inline uint32_t TSI_GetStatusFlags(TSI_Type *base)
  343. {
  344. return (base->GENCS & (kTSI_EndOfScanFlag | kTSI_OutOfRangeFlag));
  345. }
  346. /*!
  347. * @brief Clears the interrupt flag.
  348. *
  349. * This function clears the TSI interrupt flag,
  350. * automatically cleared flags can't be cleared by this function.
  351. *
  352. * @param base TSI peripheral base address.
  353. * @param mask The status flags to clear.
  354. */
  355. void TSI_ClearStatusFlags(TSI_Type *base, uint32_t mask);
  356. /*!
  357. * @brief Gets the TSI scan trigger mode.
  358. *
  359. * @param base TSI peripheral base address.
  360. * @return Scan trigger mode.
  361. */
  362. static inline uint32_t TSI_GetScanTriggerMode(TSI_Type *base)
  363. {
  364. return (base->GENCS & TSI_GENCS_STM_MASK);
  365. }
  366. /*!
  367. * @brief Gets the scan in progress flag.
  368. *
  369. * @param base TSI peripheral base address.
  370. * @return True - scan is in progress.
  371. * False - scan is not in progress.
  372. */
  373. static inline bool TSI_IsScanInProgress(TSI_Type *base)
  374. {
  375. return (base->GENCS & TSI_GENCS_SCNIP_MASK);
  376. }
  377. /*!
  378. * @brief Sets the prescaler.
  379. *
  380. * @param base TSI peripheral base address.
  381. * @param prescaler Prescaler value.
  382. * @return none.
  383. */
  384. static inline void TSI_SetElectrodeOSCPrescaler(TSI_Type *base, tsi_electrode_osc_prescaler_t prescaler)
  385. {
  386. base->GENCS = (base->GENCS & ~(TSI_GENCS_PS_MASK | ALL_FLAGS_MASK)) | (TSI_GENCS_PS(prescaler));
  387. }
  388. /*!
  389. * @brief Sets the number of scans (NSCN).
  390. *
  391. * @param base TSI peripheral base address.
  392. * @param number Number of scans.
  393. * @return none.
  394. */
  395. static inline void TSI_SetNumberOfScans(TSI_Type *base, tsi_n_consecutive_scans_t number)
  396. {
  397. base->GENCS = (base->GENCS & ~(TSI_GENCS_NSCN_MASK | ALL_FLAGS_MASK)) | (TSI_GENCS_NSCN(number));
  398. }
  399. /*!
  400. * @brief Enables/disables the TSI module.
  401. *
  402. * @param base TSI peripheral base address.
  403. * @param enable Choose whether to enable or disable module;
  404. * - true Enable TSI module;
  405. * - false Disable TSI module;
  406. * @return none.
  407. */
  408. static inline void TSI_EnableModule(TSI_Type *base, bool enable)
  409. {
  410. if (enable)
  411. {
  412. base->GENCS = (base->GENCS & ~ALL_FLAGS_MASK) | TSI_GENCS_TSIEN_MASK; /* Enable module */
  413. }
  414. else
  415. {
  416. base->GENCS = (base->GENCS & ~ALL_FLAGS_MASK) & (~TSI_GENCS_TSIEN_MASK); /* Disable module */
  417. }
  418. }
  419. /*!
  420. * @brief Sets the TSI low power STOP mode as enabled or disabled.
  421. * This enables the TSI module function in low power modes.
  422. *
  423. * @param base TSI peripheral base address.
  424. * @param enable Choose to enable or disable STOP mode.
  425. * - true Enable module in STOP mode;
  426. * - false Disable module in STOP mode;
  427. * @return none.
  428. */
  429. static inline void TSI_EnableLowPower(TSI_Type *base, bool enable)
  430. {
  431. if (enable)
  432. {
  433. base->GENCS = (base->GENCS & ~ALL_FLAGS_MASK) | TSI_GENCS_STPE_MASK; /* Module enabled in low power stop modes */
  434. }
  435. else
  436. {
  437. base->GENCS = (base->GENCS & ~ALL_FLAGS_MASK) & (~TSI_GENCS_STPE_MASK); /* Module disabled in low power stop modes */
  438. }
  439. }
  440. /*!
  441. * @brief Enables/disables the hardware trigger scan.
  442. *
  443. * @param base TSI peripheral base address.
  444. * @param enable Choose to enable hardware trigger or software trigger scan.
  445. * - true Enable hardware trigger scan;
  446. * - false Enable software trigger scan;
  447. * @return none.
  448. */
  449. static inline void TSI_EnableHardwareTriggerScan(TSI_Type *base, bool enable)
  450. {
  451. if (enable)
  452. {
  453. base->GENCS = (base->GENCS & ~ALL_FLAGS_MASK) | TSI_GENCS_STM_MASK; /* Enable hardware trigger scan */
  454. }
  455. else
  456. {
  457. base->GENCS = (base->GENCS & ~ALL_FLAGS_MASK) & (~TSI_GENCS_STM_MASK); /* Enable software trigger scan */
  458. }
  459. }
  460. /*!
  461. * @brief Starts a software trigger measurement (triggers a new measurement).
  462. *
  463. * @param base TSI peripheral base address.
  464. * @return none.
  465. */
  466. static inline void TSI_StartSoftwareTrigger(TSI_Type *base)
  467. {
  468. base->DATA |= TSI_DATA_SWTS_MASK;
  469. }
  470. /*!
  471. * @brief Sets the measured channel number.
  472. *
  473. * @param base TSI peripheral base address.
  474. * @param channel Channel number 0 ... 15.
  475. * @return none.
  476. */
  477. static inline void TSI_SetMeasuredChannelNumber(TSI_Type *base, uint8_t channel)
  478. {
  479. assert(channel < FSL_FEATURE_TSI_CHANNEL_COUNT);
  480. base->DATA = ((base->DATA) & ~TSI_DATA_TSICH_MASK) | (TSI_DATA_TSICH(channel));
  481. }
  482. /*!
  483. * @brief Gets the current measured channel number.
  484. *
  485. * @param base TSI peripheral base address.
  486. * @return uint8_t Channel number 0 ... 15.
  487. */
  488. static inline uint8_t TSI_GetMeasuredChannelNumber(TSI_Type *base)
  489. {
  490. return (uint8_t)((base->DATA & TSI_DATA_TSICH_MASK) >> TSI_DATA_TSICH_SHIFT);
  491. }
  492. /*!
  493. * @brief Enables/disables the DMA transfer.
  494. *
  495. * @param base TSI peripheral base address.
  496. * @param enable Choose to enable DMA transfer or not.
  497. * - true Enable DMA transfer;
  498. * - false Disable DMA transfer;
  499. * @return none.
  500. */
  501. static inline void TSI_EnableDmaTransfer(TSI_Type *base, bool enable)
  502. {
  503. if (enable)
  504. {
  505. base->DATA |= TSI_DATA_DMAEN_MASK; /* Enable DMA transfer */
  506. }
  507. else
  508. {
  509. base->DATA &= ~TSI_DATA_DMAEN_MASK; /* Disable DMA transfer */
  510. }
  511. }
  512. #if defined(FSL_FEATURE_TSI_HAS_END_OF_SCAN_DMA_ENABLE) && (FSL_FEATURE_TSI_HAS_END_OF_SCAN_DMA_ENABLE == 1)
  513. /*!
  514. * @brief Decides whether to enable end of scan DMA transfer request only.
  515. *
  516. * @param base TSI peripheral base address.
  517. * @param enable Choose whether to enable End of Scan DMA transfer request only.
  518. * - true Enable End of Scan DMA transfer request only;
  519. * - false Both End-of-Scan and Out-of-Range can generate DMA transfer request.
  520. * @return none.
  521. */
  522. static inline void TSI_EnableEndOfScanDmaTransferOnly(TSI_Type *base, bool enable)
  523. {
  524. if (enable)
  525. {
  526. base->GENCS = (base->GENCS & ~ALL_FLAGS_MASK) | TSI_GENCS_EOSDMEO_MASK; /* Enable End of Scan DMA transfer request only; */
  527. }
  528. else
  529. {
  530. base->GENCS =
  531. (base->GENCS & ~ALL_FLAGS_MASK) & (~TSI_GENCS_EOSDMEO_MASK); /* Both End-of-Scan and Out-of-Range can generate DMA transfer request. */
  532. }
  533. }
  534. #endif /* End of (FSL_FEATURE_TSI_HAS_END_OF_SCAN_DMA_ENABLE == 1)*/
  535. /*!
  536. * @brief Gets the conversion counter value.
  537. *
  538. * @param base TSI peripheral base address.
  539. * @return Accumulated scan counter value ticked by the reference clock.
  540. */
  541. static inline uint16_t TSI_GetCounter(TSI_Type *base)
  542. {
  543. return (uint16_t)(base->DATA & TSI_DATA_TSICNT_MASK);
  544. }
  545. /*!
  546. * @brief Sets the TSI wake-up channel low threshold.
  547. *
  548. * @param base TSI peripheral base address.
  549. * @param low_threshold Low counter threshold.
  550. * @return none.
  551. */
  552. static inline void TSI_SetLowThreshold(TSI_Type *base, uint16_t low_threshold)
  553. {
  554. assert(low_threshold < 0xFFFFU);
  555. base->TSHD = ((base->TSHD) & ~TSI_TSHD_THRESL_MASK) | (TSI_TSHD_THRESL(low_threshold));
  556. }
  557. /*!
  558. * @brief Sets the TSI wake-up channel high threshold.
  559. *
  560. * @param base TSI peripheral base address.
  561. * @param high_threshold High counter threshold.
  562. * @return none.
  563. */
  564. static inline void TSI_SetHighThreshold(TSI_Type *base, uint16_t high_threshold)
  565. {
  566. assert(high_threshold < 0xFFFFU);
  567. base->TSHD = ((base->TSHD) & ~TSI_TSHD_THRESH_MASK) | (TSI_TSHD_THRESH(high_threshold));
  568. }
  569. /*!
  570. * @brief Sets the analog mode of the TSI module.
  571. *
  572. * @param base TSI peripheral base address.
  573. * @param mode Mode value.
  574. * @return none.
  575. */
  576. static inline void TSI_SetAnalogMode(TSI_Type *base, tsi_analog_mode_t mode)
  577. {
  578. base->GENCS = (base->GENCS & ~(TSI_GENCS_MODE_MASK | ALL_FLAGS_MASK)) | (TSI_GENCS_MODE(mode));
  579. }
  580. /*!
  581. * @brief Gets the noise mode result of the TSI module.
  582. *
  583. * @param base TSI peripheral base address.
  584. * @return Value of the GENCS[MODE] bit-fields.
  585. */
  586. static inline uint8_t TSI_GetNoiseModeResult(TSI_Type *base)
  587. {
  588. return (base->GENCS & TSI_GENCS_MODE_MASK) >> TSI_GENCS_MODE_SHIFT;
  589. }
  590. /*!
  591. * @brief Sets the reference oscillator charge current.
  592. *
  593. * @param base TSI peripheral base address.
  594. * @param current The reference oscillator charge current.
  595. * @return none.
  596. */
  597. static inline void TSI_SetReferenceChargeCurrent(TSI_Type *base, tsi_reference_osc_charge_current_t current)
  598. {
  599. base->GENCS = (base->GENCS & ~(TSI_GENCS_REFCHRG_MASK | ALL_FLAGS_MASK)) | (TSI_GENCS_REFCHRG(current));
  600. }
  601. /*!
  602. * @brief Sets the external electrode charge current.
  603. *
  604. * @param base TSI peripheral base address.
  605. * @param current External electrode charge current.
  606. * @return none.
  607. */
  608. static inline void TSI_SetElectrodeChargeCurrent(TSI_Type *base, tsi_external_osc_charge_current_t current)
  609. {
  610. base->GENCS = (base->GENCS & ~(TSI_GENCS_EXTCHRG_MASK | ALL_FLAGS_MASK)) | (TSI_GENCS_EXTCHRG(current));
  611. }
  612. /*!
  613. * @brief Sets the oscillator's voltage rails.
  614. *
  615. * @param base TSI peripheral base address.
  616. * @param dvolt The voltage rails.
  617. * @return none.
  618. */
  619. static inline void TSI_SetOscVoltageRails(TSI_Type *base, tsi_osc_voltage_rails_t dvolt)
  620. {
  621. base->GENCS = (base->GENCS & ~(TSI_GENCS_DVOLT_MASK | ALL_FLAGS_MASK)) | (TSI_GENCS_DVOLT(dvolt));
  622. }
  623. /*!
  624. * @brief Sets the electrode series resistance value in EXTCHRG[0] bit.
  625. *
  626. * @param base TSI peripheral base address.
  627. * @param resistor Series resistance.
  628. * @return none.
  629. */
  630. static inline void TSI_SetElectrodeSeriesResistor(TSI_Type *base, tsi_series_resistor_t resistor)
  631. {
  632. base->GENCS = (base->GENCS & TSI_V4_EXTCHRG_RESISTOR_BIT_CLEAR) | TSI_GENCS_EXTCHRG(resistor);
  633. }
  634. /*!
  635. * @brief Sets the electrode filter bits value in EXTCHRG[2:1] bits.
  636. *
  637. * @param base TSI peripheral base address.
  638. * @param filter Series resistance.
  639. * @return none.
  640. */
  641. static inline void TSI_SetFilterBits(TSI_Type *base, tsi_filter_bits_t filter)
  642. {
  643. base->GENCS = (base->GENCS & TSI_V4_EXTCHRG_FILTER_BITS_CLEAR) | (filter << TSI_V4_EXTCHRG_FILTER_BITS_SHIFT);
  644. }
  645. #ifdef __cplusplus
  646. }
  647. #endif /* __cplusplus */
  648. /*! @}*/
  649. #endif /* _FSL_TSI_V4_H_ */