fsl_adc.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2016, 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_ADC_H__
  35. #define __FSL_ADC_H__
  36. #include "fsl_common.h"
  37. /*!
  38. * @addtogroup lpc_adc
  39. * @{
  40. */
  41. /*! @file */
  42. /*******************************************************************************
  43. * Definitions
  44. ******************************************************************************/
  45. /*! @name Driver version */
  46. /*@{*/
  47. /*! @brief ADC driver version 2.2.0. */
  48. #define FSL_ADC_DRIVER_VERSION (MAKE_VERSION(2, 2, 0))
  49. /*@}*/
  50. /*!
  51. * @brief Flags
  52. */
  53. enum _adc_status_flags
  54. {
  55. kADC_ThresholdCompareFlagOnChn0 = 1U << 0U, /*!< Threshold comparison event on Channel 0. */
  56. kADC_ThresholdCompareFlagOnChn1 = 1U << 1U, /*!< Threshold comparison event on Channel 1. */
  57. kADC_ThresholdCompareFlagOnChn2 = 1U << 2U, /*!< Threshold comparison event on Channel 2. */
  58. kADC_ThresholdCompareFlagOnChn3 = 1U << 3U, /*!< Threshold comparison event on Channel 3. */
  59. kADC_ThresholdCompareFlagOnChn4 = 1U << 4U, /*!< Threshold comparison event on Channel 4. */
  60. kADC_ThresholdCompareFlagOnChn5 = 1U << 5U, /*!< Threshold comparison event on Channel 5. */
  61. kADC_ThresholdCompareFlagOnChn6 = 1U << 6U, /*!< Threshold comparison event on Channel 6. */
  62. kADC_ThresholdCompareFlagOnChn7 = 1U << 7U, /*!< Threshold comparison event on Channel 7. */
  63. kADC_ThresholdCompareFlagOnChn8 = 1U << 8U, /*!< Threshold comparison event on Channel 8. */
  64. kADC_ThresholdCompareFlagOnChn9 = 1U << 9U, /*!< Threshold comparison event on Channel 9. */
  65. kADC_ThresholdCompareFlagOnChn10 = 1U << 10U, /*!< Threshold comparison event on Channel 10. */
  66. kADC_ThresholdCompareFlagOnChn11 = 1U << 11U, /*!< Threshold comparison event on Channel 11. */
  67. kADC_OverrunFlagForChn0 =
  68. 1U << 12U, /*!< Mirror the OVERRUN status flag from the result register for ADC channel 0. */
  69. kADC_OverrunFlagForChn1 =
  70. 1U << 13U, /*!< Mirror the OVERRUN status flag from the result register for ADC channel 1. */
  71. kADC_OverrunFlagForChn2 =
  72. 1U << 14U, /*!< Mirror the OVERRUN status flag from the result register for ADC channel 2. */
  73. kADC_OverrunFlagForChn3 =
  74. 1U << 15U, /*!< Mirror the OVERRUN status flag from the result register for ADC channel 3. */
  75. kADC_OverrunFlagForChn4 =
  76. 1U << 16U, /*!< Mirror the OVERRUN status flag from the result register for ADC channel 4. */
  77. kADC_OverrunFlagForChn5 =
  78. 1U << 17U, /*!< Mirror the OVERRUN status flag from the result register for ADC channel 5. */
  79. kADC_OverrunFlagForChn6 =
  80. 1U << 18U, /*!< Mirror the OVERRUN status flag from the result register for ADC channel 6. */
  81. kADC_OverrunFlagForChn7 =
  82. 1U << 19U, /*!< Mirror the OVERRUN status flag from the result register for ADC channel 7. */
  83. kADC_OverrunFlagForChn8 =
  84. 1U << 20U, /*!< Mirror the OVERRUN status flag from the result register for ADC channel 8. */
  85. kADC_OverrunFlagForChn9 =
  86. 1U << 21U, /*!< Mirror the OVERRUN status flag from the result register for ADC channel 9. */
  87. kADC_OverrunFlagForChn10 =
  88. 1U << 22U, /*!< Mirror the OVERRUN status flag from the result register for ADC channel 10. */
  89. kADC_OverrunFlagForChn11 =
  90. 1U << 23U, /*!< Mirror the OVERRUN status flag from the result register for ADC channel 11. */
  91. kADC_GlobalOverrunFlagForSeqA = 1U << 24U, /*!< Mirror the glabal OVERRUN status flag for conversion sequence A. */
  92. kADC_GlobalOverrunFlagForSeqB = 1U << 25U, /*!< Mirror the global OVERRUN status flag for conversion sequence B. */
  93. kADC_ConvSeqAInterruptFlag = 1U << 28U, /*!< Sequence A interrupt/DMA trigger. */
  94. kADC_ConvSeqBInterruptFlag = 1U << 29U, /*!< Sequence B interrupt/DMA trigger. */
  95. kADC_ThresholdCompareInterruptFlag = 1U << 30U, /*!< Threshold comparision interrupt flag. */
  96. kADC_OverrunInterruptFlag = 1U << 31U, /*!< Overrun interrupt flag. */
  97. };
  98. /*!
  99. * @brief Interrupts
  100. * @note Not all the interrupt options are listed here
  101. */
  102. enum _adc_interrupt_enable
  103. {
  104. kADC_ConvSeqAInterruptEnable = ADC_INTEN_SEQA_INTEN_MASK, /*!< Enable interrupt upon completion of each individual
  105. conversion in sequence A, or entire sequence. */
  106. kADC_ConvSeqBInterruptEnable = ADC_INTEN_SEQB_INTEN_MASK, /*!< Enable interrupt upon completion of each individual
  107. conversion in sequence B, or entire sequence. */
  108. kADC_OverrunInterruptEnable = ADC_INTEN_OVR_INTEN_MASK, /*!< Enable the detection of an overrun condition on any of
  109. the channel data registers will cause an overrun
  110. interrupt/DMA trigger. */
  111. };
  112. #if defined(FSL_FEATURE_ADC_HAS_CTRL_ASYNMODE) & FSL_FEATURE_ADC_HAS_CTRL_ASYNMODE
  113. /*!
  114. * @brief Define selection of clock mode.
  115. */
  116. typedef enum _adc_clock_mode
  117. {
  118. kADC_ClockSynchronousMode =
  119. 0U, /*!< The ADC clock would be derived from the system clock based on "clockDividerNumber". */
  120. kADC_ClockAsynchronousMode = 1U, /*!< The ADC clock would be based on the SYSCON block's divider. */
  121. } adc_clock_mode_t;
  122. #endif/* FSL_FEATURE_ADC_HAS_CTRL_ASYNMODE. */
  123. #if defined(FSL_FEATURE_ADC_HAS_CTRL_RESOL) & FSL_FEATURE_ADC_HAS_CTRL_RESOL
  124. /*!
  125. * @brief Define selection of resolution.
  126. */
  127. typedef enum _adc_resolution
  128. {
  129. kADC_Resolution6bit = 0U, /*!< 6-bit resolution. */
  130. kADC_Resolution8bit = 1U, /*!< 8-bit resolution. */
  131. kADC_Resolution10bit = 2U, /*!< 10-bit resolution. */
  132. kADC_Resolution12bit = 3U, /*!< 12-bit resolution. */
  133. } adc_resolution_t;
  134. #endif/* FSL_FEATURE_ADC_HAS_CTRL_RESOL. */
  135. #if defined(FSL_FEATURE_ADC_HAS_TRIM_REG) & FSL_FEATURE_ADC_HAS_TRIM_REG
  136. /*!
  137. * @brief Definfe range of the analog supply voltage VDDA.
  138. */
  139. typedef enum _adc_voltage_range
  140. {
  141. kADC_HighVoltageRange = 0U, /* High voltage. VDD = 2.7 V to 3.6 V. */
  142. kADC_LowVoltageRange = 1U, /* Low voltage. VDD = 2.4 V to 2.7 V. */
  143. } adc_vdda_range_t;
  144. #endif/* FSL_FEATURE_ADC_HAS_TRIM_REG. */
  145. /*!
  146. * @brief Define selection of polarity of selected input trigger for conversion sequence.
  147. */
  148. typedef enum _adc_trigger_polarity
  149. {
  150. kADC_TriggerPolarityNegativeEdge = 0U, /*!< A negative edge launches the conversion sequence on the trigger(s). */
  151. kADC_TriggerPolarityPositiveEdge = 1U, /*!< A positive edge launches the conversion sequence on the trigger(s). */
  152. } adc_trigger_polarity_t;
  153. /*!
  154. * @brief Define selection of conversion sequence's priority.
  155. */
  156. typedef enum _adc_priority
  157. {
  158. kADC_PriorityLow = 0U, /*!< This sequence would be preempted when another sequence is started. */
  159. kADC_PriorityHigh = 1U, /*!< This sequence would preempt other sequence even when it is started. */
  160. } adc_priority_t;
  161. /*!
  162. * @brief Define selection of conversion sequence's interrupt.
  163. */
  164. typedef enum _adc_seq_interrupt_mode
  165. {
  166. kADC_InterruptForEachConversion = 0U, /*!< The sequence interrupt/DMA trigger will be set at the end of each
  167. individual ADC conversion inside this conversion sequence. */
  168. kADC_InterruptForEachSequence = 1U, /*!< The sequence interrupt/DMA trigger will be set when the entire set of
  169. this sequence conversions completes. */
  170. } adc_seq_interrupt_mode_t;
  171. /*!
  172. * @brief Define status of threshold compare result.
  173. */
  174. typedef enum _adc_threshold_compare_status
  175. {
  176. kADC_ThresholdCompareInRange = 0U, /*!< LOW threshold <= conversion value <= HIGH threshold. */
  177. kADC_ThresholdCompareBelowRange = 1U, /*!< conversion value < LOW threshold. */
  178. kADC_ThresholdCompareAboveRange = 2U, /*!< conversion value > HIGH threshold. */
  179. } adc_threshold_compare_status_t;
  180. /*!
  181. * @brief Define status of threshold crossing detection result.
  182. */
  183. typedef enum _adc_threshold_crossing_status
  184. {
  185. /* The conversion on this channel had the same relationship (above or below) to the threshold value established by
  186. * the designated LOW threshold value as did the previous conversion on this channel. */
  187. kADC_ThresholdCrossingNoDetected = 0U, /*!< No threshold Crossing detected. */
  188. /* Indicates that a threshold crossing in the downward direction has occurred - i.e. the previous sample on this
  189. * channel was above the threshold value established by the designated LOW threshold value and the current sample is
  190. * below that threshold. */
  191. kADC_ThresholdCrossingDownward = 2U, /*!< Downward Threshold Crossing detected. */
  192. /* Indicates that a thre shold crossing in the upward direction has occurred - i.e. the previous sample on this
  193. * channel was below the threshold value established by the designated LOW threshold value and the current sample is
  194. * above that threshold. */
  195. kADC_ThresholdCrossingUpward = 3U, /*!< Upward Threshold Crossing Detected. */
  196. } adc_threshold_crossing_status_t;
  197. /*!
  198. * @brief Define interrupt mode for threshold compare event.
  199. */
  200. typedef enum _adc_threshold_interrupt_mode
  201. {
  202. kADC_ThresholdInterruptDisabled = 0U, /*!< Threshold comparison interrupt is disabled. */
  203. kADC_ThresholdInterruptOnOutside = 1U, /*!< Threshold comparison interrupt is enabled on outside threshold. */
  204. kADC_ThresholdInterruptOnCrossing = 2U, /*!< Threshold comparison interrupt is enabled on crossing threshold. */
  205. } adc_threshold_interrupt_mode_t;
  206. /*!
  207. * @brief Define structure for configuring the block.
  208. */
  209. typedef struct _adc_config
  210. {
  211. #if defined(FSL_FEATURE_ADC_HAS_CTRL_ASYNMODE) & FSL_FEATURE_ADC_HAS_CTRL_ASYNMODE
  212. adc_clock_mode_t clockMode; /*!< Select the clock mode for ADC converter. */
  213. #endif/* FSL_FEATURE_ADC_HAS_CTRL_ASYNMODE. */
  214. uint32_t clockDividerNumber; /*!< This field is only available when using kADC_ClockSynchronousMode for "clockMode"
  215. field. The divider would be plused by 1 based on the value in this field. The
  216. available range is in 8 bits. */
  217. #if defined(FSL_FEATURE_ADC_HAS_CTRL_RESOL) & FSL_FEATURE_ADC_HAS_CTRL_RESOL
  218. adc_resolution_t resolution; /*!< Select the conversion bits. */
  219. #endif/* FSL_FEATURE_ADC_HAS_CTRL_RESOL. */
  220. #if defined(FSL_FEATURE_ADC_HAS_CTRL_BYPASSCAL) & FSL_FEATURE_ADC_HAS_CTRL_BYPASSCAL
  221. bool enableBypassCalibration; /*!< By default, a calibration cycle must be performed each time the chip is
  222. powered-up. Re-calibration may be warranted periodically - especially if
  223. operating conditions have changed. To enable this option would avoid the need to
  224. calibrate if offset error is not a concern in the application. */
  225. #endif/* FSL_FEATURE_ADC_HAS_CTRL_BYPASSCAL. */
  226. #if defined(FSL_FEATURE_ADC_HAS_CTRL_TSAMP) & FSL_FEATURE_ADC_HAS_CTRL_TSAMP
  227. uint32_t sampleTimeNumber; /*!< By default, with value as "0U", the sample period would be 2.5 ADC clocks. Then,
  228. to plus the "sampleTimeNumber" value here. The available value range is in 3 bits.*/
  229. #endif/* FSL_FEATURE_ADC_HAS_CTRL_TSAMP. */
  230. #if defined(FSL_FEATURE_ADC_HAS_CTRL_LPWRMODE) & FSL_FEATURE_ADC_HAS_CTRL_LPWRMODE
  231. bool enableLowPowerMode; /*!< If disable low-power mode, ADC remains activated even when no conversions are requested.
  232. If enable low-power mode, The ADC is automatically powered-down when no conversions are
  233. taking place. */
  234. #endif/* FSL_FEATURE_ADC_HAS_CTRL_LPWRMODE. */
  235. #if defined(FSL_FEATURE_ADC_HAS_TRIM_REG) & FSL_FEATURE_ADC_HAS_TRIM_REG
  236. adc_vdda_range_t voltageRange; /*!< Configure the ADC for the appropriate operating range of the analog supply voltage VDDA.
  237. Failure to set the area correctly causes the ADC to return incorrect conversion results. */
  238. #endif/* FSL_FEATURE_ADC_HAS_TRIM_REG. */
  239. } adc_config_t;
  240. /*!
  241. * @brief Define structure for configuring conversion sequence.
  242. */
  243. typedef struct _adc_conv_seq_config
  244. {
  245. uint32_t channelMask; /*!< Selects which one or more of the ADC channels will be sampled and converted when this
  246. sequence is launched. The masked channels would be involved in current conversion
  247. sequence, beginning with the lowest-order. The available range is in 12-bit. */
  248. uint32_t triggerMask; /*!< Selects which one or more of the available hardware trigger sources will cause this
  249. conversion sequence to be initiated. The available range is 6-bit.*/
  250. adc_trigger_polarity_t triggerPolarity; /*!< Select the trigger to lauch conversion sequence. */
  251. bool enableSyncBypass; /*!< To enable this feature allows the hardware trigger input to bypass synchronization
  252. flip-flop stages and therefore shorten the time between the trigger input signal and the
  253. start of a conversion. */
  254. bool enableSingleStep; /*!< When enabling this feature, a trigger will launch a single conversion on the next
  255. channel in the sequence instead of the default response of launching an entire sequence
  256. of conversions. */
  257. adc_seq_interrupt_mode_t interruptMode; /*!< Select the interrpt/DMA trigger mode. */
  258. } adc_conv_seq_config_t;
  259. /*!
  260. * @brief Define structure of keeping conversion result information.
  261. */
  262. typedef struct _adc_result_info
  263. {
  264. uint32_t result; /*!< Keep the conversion data value. */
  265. adc_threshold_compare_status_t thresholdCompareStatus; /*!< Keep the threshold compare status. */
  266. adc_threshold_crossing_status_t thresholdCorssingStatus; /*!< Keep the threshold crossing status. */
  267. uint32_t channelNumber; /*!< Keep the channel number for this conversion. */
  268. bool overrunFlag; /*!< Keep the status whether the conversion is overrun or not. */
  269. /* The data available flag would be returned by the reading result API. */
  270. } adc_result_info_t;
  271. #if defined(__cplusplus)
  272. extern "C" {
  273. #endif
  274. /*******************************************************************************
  275. * API
  276. ******************************************************************************/
  277. /*!
  278. * @name Initialization and Deinitialization
  279. * @{
  280. */
  281. /*!
  282. * @brief Initialize the ADC module.
  283. *
  284. * @param base ADC peripheral base address.
  285. * @param config Pointer to configuration structure, see to #adc_config_t.
  286. */
  287. void ADC_Init(ADC_Type *base, const adc_config_t *config);
  288. /*!
  289. * @brief Deinitialize the ADC module.
  290. *
  291. * @param base ADC peripheral base address.
  292. */
  293. void ADC_Deinit(ADC_Type *base);
  294. /*!
  295. * @brief Gets an available pre-defined settings for initial configuration.
  296. *
  297. * This function initializes the initial configuration structure with an available settings. The default values are:
  298. * @code
  299. * config->clockMode = kADC_ClockSynchronousMode;
  300. * config->clockDividerNumber = 0U;
  301. * config->resolution = kADC_Resolution12bit;
  302. * config->enableBypassCalibration = false;
  303. * config->sampleTimeNumber = 0U;
  304. * @endcode
  305. * @param config Pointer to configuration structure.
  306. */
  307. void ADC_GetDefaultConfig(adc_config_t *config);
  308. #if !(defined(FSL_FEATURE_ADC_HAS_NO_CALIB_FUNC) && FSL_FEATURE_ADC_HAS_NO_CALIB_FUNC)
  309. #if defined(FSL_FEATURE_ADC_HAS_CALIB_REG) && FSL_FEATURE_ADC_HAS_CALIB_REG
  310. /*!
  311. * @brief Do the self hardware calibration.
  312. *
  313. * @param base ADC peripheral base address.
  314. * @retval true Calibration succeed.
  315. * @retval false Calibration failed.
  316. */
  317. bool ADC_DoSelfCalibration(ADC_Type *base);
  318. #else
  319. /*!
  320. * @brief Do the self calibration. To calibrate the ADC, set the ADC clock to 500 kHz.
  321. * In order to achieve the specified ADC accuracy, the A/D converter must be recalibrated, at a minimum,
  322. * following every chip reset before initiating normal ADC operation.
  323. *
  324. * @param base ADC peripheral base address.
  325. * @param frequency The ststem clock frequency to ADC.
  326. * @retval true Calibration succeed.
  327. * @retval false Calibration failed.
  328. */
  329. bool ADC_DoSelfCalibration(ADC_Type *base, uint32_t frequency);
  330. #endif/* FSL_FEATURE_ADC_HAS_CALIB_REG */
  331. #endif/* FSL_FEATURE_ADC_HAS_NO_CALIB_FUNC */
  332. #if !(defined(FSL_FEATURE_ADC_HAS_NO_INSEL) && FSL_FEATURE_ADC_HAS_NO_INSEL)
  333. /*!
  334. * @brief Enable the internal temperature sensor measurement.
  335. *
  336. * When enabling the internal temperature sensor measurement, the channel 0 would be connected to internal sensor
  337. * instead of external pin.
  338. *
  339. * @param base ADC peripheral base address.
  340. * @param enable Switcher to enable the feature or not.
  341. */
  342. static inline void ADC_EnableTemperatureSensor(ADC_Type *base, bool enable)
  343. {
  344. if (enable)
  345. {
  346. base->INSEL = (base->INSEL & ~ADC_INSEL_SEL_MASK) | ADC_INSEL_SEL(0x3);
  347. }
  348. else
  349. {
  350. base->INSEL = (base->INSEL & ~ADC_INSEL_SEL_MASK) | ADC_INSEL_SEL(0);
  351. }
  352. }
  353. #endif /* FSL_FEATURE_ADC_HAS_NO_INSEL. */
  354. /* @} */
  355. /*!
  356. * @name Control conversion sequence A.
  357. * @{
  358. */
  359. /*!
  360. * @brief Enable the conversion sequence A.
  361. *
  362. * In order to avoid spuriously triggering the sequence, the trigger to conversion sequence should be ready before the
  363. * sequence is ready. when the sequence is disabled, the trigger would be ignored. Also, it is suggested to disable the
  364. * sequence during changing the sequence's setting.
  365. *
  366. * @param base ADC peripheral base address.
  367. * @param enable Switcher to enable the feature or not.
  368. */
  369. static inline void ADC_EnableConvSeqA(ADC_Type *base, bool enable)
  370. {
  371. if (enable)
  372. {
  373. base->SEQ_CTRL[0] |= ADC_SEQ_CTRL_SEQ_ENA_MASK;
  374. }
  375. else
  376. {
  377. base->SEQ_CTRL[0] &= ~ADC_SEQ_CTRL_SEQ_ENA_MASK;
  378. }
  379. }
  380. /*!
  381. * @brief Configure the conversion sequence A.
  382. *
  383. * @param base ADC peripheral base address.
  384. * @param config Pointer to configuration structure, see to #adc_conv_seq_config_t.
  385. */
  386. void ADC_SetConvSeqAConfig(ADC_Type *base, const adc_conv_seq_config_t *config);
  387. /*!
  388. * @brief Do trigger the sequence's conversion by software.
  389. *
  390. * @param base ADC peripheral base address.
  391. */
  392. static inline void ADC_DoSoftwareTriggerConvSeqA(ADC_Type *base)
  393. {
  394. base->SEQ_CTRL[0] |= ADC_SEQ_CTRL_START_MASK;
  395. }
  396. /*!
  397. * @brief Enable the burst conversion of sequence A.
  398. *
  399. * Enable the burst mode would cause the conversion sequence to be cntinuously cycled through. Other triggers would be
  400. * ignored while this mode is enabled. Repeated conversions could be halted by disabling this mode. And the sequence
  401. * currently in process will be completed before cnversions are terminated.
  402. * Note that a new sequence could begin just before the burst mode is disabled.
  403. *
  404. * @param base ADC peripheral base address.
  405. * @param enable Switcher to enable this feature.
  406. */
  407. static inline void ADC_EnableConvSeqABurstMode(ADC_Type *base, bool enable)
  408. {
  409. if (enable)
  410. {
  411. base->SEQ_CTRL[0] |= ADC_SEQ_CTRL_BURST_MASK;
  412. }
  413. else
  414. {
  415. base->SEQ_CTRL[0] &= ~ADC_SEQ_CTRL_BURST_MASK;
  416. }
  417. }
  418. /*!
  419. * @brief Set the high priority for conversion sequence A.
  420. *
  421. * @param base ADC peripheral bass address.
  422. */
  423. static inline void ADC_SetConvSeqAHighPriority(ADC_Type *base)
  424. {
  425. base->SEQ_CTRL[0] |= ADC_SEQ_CTRL_LOWPRIO_MASK;
  426. }
  427. /* @} */
  428. /*!
  429. * @name Control conversion sequence B.
  430. * @{
  431. */
  432. /*!
  433. * @brief Enable the conversion sequence B.
  434. *
  435. * In order to avoid spuriously triggering the sequence, the trigger to conversion sequence should be ready before the
  436. * sequence is ready. when the sequence is disabled, the trigger would be ignored. Also, it is suggested to disable the
  437. * sequence during changing the sequence's setting.
  438. *
  439. * @param base ADC peripheral base address.
  440. * @param enable Switcher to enable the feature or not.
  441. */
  442. static inline void ADC_EnableConvSeqB(ADC_Type *base, bool enable)
  443. {
  444. if (enable)
  445. {
  446. base->SEQ_CTRL[1] |= ADC_SEQ_CTRL_SEQ_ENA_MASK;
  447. }
  448. else
  449. {
  450. base->SEQ_CTRL[1] &= ~ADC_SEQ_CTRL_SEQ_ENA_MASK;
  451. }
  452. }
  453. /*!
  454. * @brief Configure the conversion sequence B.
  455. *
  456. * @param base ADC peripheral base address.
  457. * @param config Pointer to configuration structure, see to #adc_conv_seq_config_t.
  458. */
  459. void ADC_SetConvSeqBConfig(ADC_Type *base, const adc_conv_seq_config_t *config);
  460. /*!
  461. * @brief Do trigger the sequence's conversion by software.
  462. *
  463. * @param base ADC peripheral base address.
  464. */
  465. static inline void ADC_DoSoftwareTriggerConvSeqB(ADC_Type *base)
  466. {
  467. base->SEQ_CTRL[1] |= ADC_SEQ_CTRL_START_MASK;
  468. }
  469. /*!
  470. * @brief Enable the burst conversion of sequence B.
  471. *
  472. * Enable the burst mode would cause the conversion sequence to be continuously cycled through. Other triggers would be
  473. * ignored while this mode is enabled. Repeated conversions could be halted by disabling this mode. And the sequence
  474. * currently in process will be completed before cnversions are terminated.
  475. * Note that a new sequence could begin just before the burst mode is disabled.
  476. *
  477. * @param base ADC peripheral base address.
  478. * @param enable Switcher to enable this feature.
  479. */
  480. static inline void ADC_EnableConvSeqBBurstMode(ADC_Type *base, bool enable)
  481. {
  482. if (enable)
  483. {
  484. base->SEQ_CTRL[1] |= ADC_SEQ_CTRL_BURST_MASK;
  485. }
  486. else
  487. {
  488. base->SEQ_CTRL[1] &= ~ADC_SEQ_CTRL_BURST_MASK;
  489. }
  490. }
  491. /*!
  492. * @brief Set the high priority for conversion sequence B.
  493. *
  494. * @param base ADC peripheral bass address.
  495. */
  496. static inline void ADC_SetConvSeqBHighPriority(ADC_Type *base)
  497. {
  498. base->SEQ_CTRL[0] &= ~ADC_SEQ_CTRL_LOWPRIO_MASK;
  499. }
  500. /* @} */
  501. /*!
  502. * @name Data result.
  503. * @{
  504. */
  505. /*!
  506. * @brief Get the global ADC conversion infomation of sequence A.
  507. *
  508. * @param base ADC peripheral base address.
  509. * @param info Pointer to information structure, see to #adc_result_info_t;
  510. * @retval true The conversion result is ready.
  511. * @retval false The conversion result is not ready yet.
  512. */
  513. bool ADC_GetConvSeqAGlobalConversionResult(ADC_Type *base, adc_result_info_t *info);
  514. /*!
  515. * @brief Get the global ADC conversion infomation of sequence B.
  516. *
  517. * @param base ADC peripheral base address.
  518. * @param info Pointer to information structure, see to #adc_result_info_t;
  519. * @retval true The conversion result is ready.
  520. * @retval false The conversion result is not ready yet.
  521. */
  522. bool ADC_GetConvSeqBGlobalConversionResult(ADC_Type *base, adc_result_info_t *info);
  523. /*!
  524. * @brief Get the channel's ADC conversion completed under each conversion sequence.
  525. *
  526. * @param base ADC peripheral base address.
  527. * @param channel The indicated channel number.
  528. * @param info Pointer to information structure, see to #adc_result_info_t;
  529. * @retval true The conversion result is ready.
  530. * @retval false The conversion result is not ready yet.
  531. */
  532. bool ADC_GetChannelConversionResult(ADC_Type *base, uint32_t channel, adc_result_info_t *info);
  533. /* @} */
  534. /*!
  535. * @name Threshold function.
  536. * @{
  537. */
  538. /*!
  539. * @brief Set the threshhold pair 0 with low and high value.
  540. *
  541. * @param base ADC peripheral base address.
  542. * @param lowValue LOW threshold value.
  543. * @param highValue HIGH threshold value.
  544. */
  545. static inline void ADC_SetThresholdPair0(ADC_Type *base, uint32_t lowValue, uint32_t highValue)
  546. {
  547. base->THR0_LOW = ADC_THR0_LOW_THRLOW(lowValue);
  548. base->THR0_HIGH = ADC_THR0_HIGH_THRHIGH(highValue);
  549. }
  550. /*!
  551. * @brief Set the threshhold pair 1 with low and high value.
  552. *
  553. * @param base ADC peripheral base address.
  554. * @param lowValue LOW threshold value. The available value is with 12-bit.
  555. * @param highValue HIGH threshold value. The available value is with 12-bit.
  556. */
  557. static inline void ADC_SetThresholdPair1(ADC_Type *base, uint32_t lowValue, uint32_t highValue)
  558. {
  559. base->THR1_LOW = ADC_THR1_LOW_THRLOW(lowValue);
  560. base->THR1_HIGH = ADC_THR1_HIGH_THRHIGH(highValue);
  561. }
  562. /*!
  563. * @brief Set given channels to apply the threshold pare 0.
  564. *
  565. * @param base ADC peripheral base address.
  566. * @param channelMask Indicated channels' mask.
  567. */
  568. static inline void ADC_SetChannelWithThresholdPair0(ADC_Type *base, uint32_t channelMask)
  569. {
  570. base->CHAN_THRSEL &= ~(channelMask);
  571. }
  572. /*!
  573. * @brief Set given channels to apply the threshold pare 1.
  574. *
  575. * @param base ADC peripheral base address.
  576. * @param channelMask Indicated channels' mask.
  577. */
  578. static inline void ADC_SetChannelWithThresholdPair1(ADC_Type *base, uint32_t channelMask)
  579. {
  580. base->CHAN_THRSEL |= channelMask;
  581. }
  582. /* @} */
  583. /*!
  584. * @name Interrupts.
  585. * @{
  586. */
  587. /*!
  588. * @brief Enable interrupts for conversion sequences.
  589. *
  590. * @param base ADC peripheral base address.
  591. * @param mask Mask of interrupt mask value for global block except each channal, see to #_adc_interrupt_enable.
  592. */
  593. static inline void ADC_EnableInterrupts(ADC_Type *base, uint32_t mask)
  594. {
  595. base->INTEN |= (0x7 & mask);
  596. }
  597. /*!
  598. * @brief Disable interrupts for conversion sequence.
  599. *
  600. * @param base ADC peripheral base address.
  601. * @param mask Mask of interrupt mask value for global block except each channel, see to #_adc_interrupt_enable.
  602. */
  603. static inline void ADC_DisableInterrupts(ADC_Type *base, uint32_t mask)
  604. {
  605. base->INTEN &= ~(0x7 & mask);
  606. }
  607. /*!
  608. * @brief Enable the interrupt of threshold compare event for each channel.
  609. * @deprecated Do not use this function. It has been superceded by @ADC_EnableThresholdCompareInterrupt
  610. */
  611. static inline void ADC_EnableShresholdCompareInterrupt(ADC_Type *base,
  612. uint32_t channel,
  613. adc_threshold_interrupt_mode_t mode)
  614. {
  615. base->INTEN = (base->INTEN & ~(0x3U << ((channel << 1U) + 3U))) | ((uint32_t)(mode) << ((channel << 1U) + 3U));
  616. }
  617. /*!
  618. * @brief Enable the interrupt of threshold compare event for each channel.
  619. *
  620. * @param base ADC peripheral base address.
  621. * @param channel Channel number.
  622. * @param mode Interrupt mode for threshold compare event, see to #adc_threshold_interrupt_mode_t.
  623. */
  624. static inline void ADC_EnableThresholdCompareInterrupt(ADC_Type *base,
  625. uint32_t channel,
  626. adc_threshold_interrupt_mode_t mode)
  627. {
  628. base->INTEN = (base->INTEN & ~(0x3U << ((channel << 1U) + 3U))) | ((uint32_t)(mode) << ((channel << 1U) + 3U));
  629. }
  630. /* @} */
  631. /*!
  632. * @name Status.
  633. * @{
  634. */
  635. /*!
  636. * @brief Get status flags of ADC module.
  637. *
  638. * @param base ADC peripheral base address.
  639. * @return Mask of status flags of module, see to #_adc_status_flags.
  640. */
  641. static inline uint32_t ADC_GetStatusFlags(ADC_Type *base)
  642. {
  643. return base->FLAGS;
  644. }
  645. /*!
  646. * @brief Clear status flags of ADC module.
  647. *
  648. * @param base ADC peripheral base address.
  649. * @param mask Mask of status flags of module, see to #_adc_status_flags.
  650. */
  651. static inline void ADC_ClearStatusFlags(ADC_Type *base, uint32_t mask)
  652. {
  653. base->FLAGS = mask; /* Write 1 to clear. */
  654. }
  655. /* @} */
  656. #if defined(__cplusplus)
  657. }
  658. #endif
  659. /* @} */
  660. #endif /* __FSL_ADC_H__ */