fsl_adc.h 25 KB

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