fsl_adc.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 adc_12b1msps_sar
  39. * @{
  40. */
  41. /*******************************************************************************
  42. * Definitions
  43. ******************************************************************************/
  44. /*! @brief ADC driver version */
  45. #define FSL_ADC_DRIVER_VERSION (MAKE_VERSION(2, 0, 0)) /*!< Version 2.0.0. */
  46. /*!
  47. * @brief Converter's status flags.
  48. */
  49. typedef enum _adc_status_flags
  50. {
  51. kADC_ConversionActiveFlag = ADC_GS_ADACT_MASK, /*!< Conversion is active,not support w1c. */
  52. kADC_CalibrationFailedFlag = ADC_GS_CALF_MASK, /*!< Calibration is failed,support w1c. */
  53. kADC_AsynchronousWakeupInterruptFlag =
  54. ADC_GS_AWKST_MASK, /*!< Asynchronous wakeup interrupt occured, support w1c. */
  55. } adc_status_flags_t;
  56. /*!
  57. * @brief Reference voltage source.
  58. */
  59. typedef enum _adc_reference_voltage_source
  60. {
  61. kADC_ReferenceVoltageSourceAlt0 = 0U, /*!< For external pins pair of VrefH and VrefL. */
  62. } adc_reference_voltage_source_t;
  63. /*!
  64. * @brief Sample time duration.
  65. */
  66. typedef enum _adc_sample_period_mode
  67. {
  68. /* This group of enumeration is for internal use which is related to register setting. */
  69. kADC_SamplePeriod2or12Clocks = 0U, /*!< Long sample 12 clocks or short sample 2 clocks. */
  70. kADC_SamplePeriod4or16Clocks = 1U, /*!< Long sample 16 clocks or short sample 4 clocks. */
  71. kADC_SamplePeriod6or20Clocks = 2U, /*!< Long sample 20 clocks or short sample 6 clocks. */
  72. kADC_SamplePeriod8or24Clocks = 3U, /*!< Long sample 24 clocks or short sample 8 clocks. */
  73. /* This group of enumeration is for a public user. */
  74. /* For long sample mode. */
  75. kADC_SamplePeriodLong12Clcoks = kADC_SamplePeriod2or12Clocks, /*!< Long sample 12 clocks. */
  76. kADC_SamplePeriodLong16Clcoks = kADC_SamplePeriod4or16Clocks, /*!< Long sample 16 clocks. */
  77. kADC_SamplePeriodLong20Clcoks = kADC_SamplePeriod6or20Clocks, /*!< Long sample 20 clocks. */
  78. kADC_SamplePeriodLong24Clcoks = kADC_SamplePeriod8or24Clocks, /*!< Long sample 24 clocks. */
  79. /* For short sample mode. */
  80. kADC_SamplePeriodShort2Clocks = kADC_SamplePeriod2or12Clocks, /*!< Short sample 2 clocks. */
  81. kADC_SamplePeriodShort4Clocks = kADC_SamplePeriod4or16Clocks, /*!< Short sample 4 clocks. */
  82. kADC_SamplePeriodShort6Clocks = kADC_SamplePeriod6or20Clocks, /*!< Short sample 6 clocks. */
  83. kADC_SamplePeriodShort8Clocks = kADC_SamplePeriod8or24Clocks, /*!< Short sample 8 clocks. */
  84. } adc_sample_period_mode_t;
  85. /*!
  86. * @brief Clock source.
  87. */
  88. typedef enum _adc_clock_source
  89. {
  90. kADC_ClockSourceIPG = 0U, /*!< Select IPG clock to generate ADCK. */
  91. kADC_ClockSourceIPGDiv2 = 1U, /*!< Select IPG clock divided by 2 to generate ADCK. */
  92. #if !(defined(FSL_FEATURE_ADC_SUPPORT_ALTCLK_REMOVE) && FSL_FEATURE_ADC_SUPPORT_ALTCLK_REMOVE)
  93. kADC_ClockSourceALT = 2U, /*!< Select alternate clock to generate ADCK. */
  94. #endif
  95. kADC_ClockSourceAD = 3U, /*!< Select Asynchronous clock to generate ADCK. */
  96. } adc_clock_source_t;
  97. /*!
  98. * @brief Clock divider for the converter.
  99. */
  100. typedef enum _adc_clock_drvier
  101. {
  102. kADC_ClockDriver1 = 0U, /*!< For divider 1 from the input clock to the module. */
  103. kADC_ClockDriver2 = 1U, /*!< For divider 2 from the input clock to the module. */
  104. kADC_ClockDriver4 = 2U, /*!< For divider 4 from the input clock to the module. */
  105. kADC_ClockDriver8 = 3U, /*!< For divider 8 from the input clock to the module. */
  106. } adc_clock_driver_t;
  107. /*!
  108. * @brief Converter's resolution.
  109. */
  110. typedef enum _adc_resolution
  111. {
  112. kADC_Resolution8Bit = 0U, /*!< Single End 8-bit resolution. */
  113. kADC_Resolution10Bit = 1U, /*!< Single End 10-bit resolution. */
  114. kADC_Resolution12Bit = 2U, /*!< Single End 12-bit resolution. */
  115. } adc_resolution_t;
  116. /*!
  117. * @brief Converter hardware compare mode.
  118. */
  119. typedef enum _adc_hardware_compare_mode
  120. {
  121. kADC_HardwareCompareMode0 = 0U, /*!< Compare true if the result is less than the value1. */
  122. kADC_HardwareCompareMode1 = 1U, /*!< Compare true if the result is greater than or equal to value1. */
  123. kADC_HardwareCompareMode2 = 2U, /*!< Value1 <= Value2, compare true if the result is less than value1 Or
  124. the result is Greater than value2.
  125. Value1 > Value2, compare true if the result is less than value1 And the
  126. result is greater than value2*/
  127. kADC_HardwareCompareMode3 = 3U, /*!< Value1 <= Value2, compare true if the result is greater than or equal
  128. to value1 And the result is less than or equal to value2.
  129. Value1 > Value2, compare true if the result is greater than or equal to
  130. value1 Or the result is less than or equal to value2. */
  131. } adc_hardware_compare_mode_t;
  132. /*!
  133. * @brief Converter hardware average mode.
  134. */
  135. typedef enum _adc_hardware_average_mode
  136. {
  137. kADC_HardwareAverageCount4 = 0U, /*!< For hardware average with 4 samples. */
  138. kADC_HardwareAverageCount8 = 1U, /*!< For hardware average with 8 samples. */
  139. kADC_HardwareAverageCount16 = 2U, /*!< For hardware average with 16 samples. */
  140. kADC_HardwareAverageCount32 = 3U, /*!< For hardware average with 32 samples. */
  141. kADC_HardwareAverageDiasable = 4U, /*!< Disable the hardware average function. */
  142. } adc_hardware_average_mode_t;
  143. /*!
  144. * @brief Converter configuration.
  145. */
  146. typedef struct _adc_config
  147. {
  148. bool enableOverWrite; /*!< Enable the overwriting. */
  149. bool enableContinuousConversion; /*!< Enable the continuous conversion mode. */
  150. bool enableHighSpeed; /*!< Enable the high-speed mode. */
  151. bool enableLowPower; /*!< Enable the low power mode. */
  152. bool enableLongSample; /*!< Enable the long sample mode. */
  153. bool enableAsynchronousClockOutput; /*!< Enable the asynchronous clock output. */
  154. adc_reference_voltage_source_t referenceVoltageSource; /*!< Select the reference voltage source. */
  155. adc_sample_period_mode_t samplePeriodMode; /*!< Select the sample period in long sample mode or short mode. */
  156. adc_clock_source_t clockSource; /*!< Select the input clock source to generate the internal clock ADCK. */
  157. adc_clock_driver_t clockDriver; /*!< Select the divide ratio used by the ADC to generate the internal clock ADCK. */
  158. adc_resolution_t resolution; /*!< Select the ADC resolution mode. */
  159. } adc_config_t;
  160. /*!
  161. * @brief Converter Offset configuration.
  162. */
  163. typedef struct _adc_offest_config
  164. {
  165. bool enableSigned; /*!< if false,The offset value is added with the raw result.
  166. if true,The offset value is subtracted from the raw converted value. */
  167. uint32_t offsetValue; /*!< User configurable offset value(0-4095). */
  168. } adc_offest_config_t;
  169. /*!
  170. * @brief ADC hardware compare configuration.
  171. *
  172. * In kADC_HardwareCompareMode0, compare true if the result is less than the value1.
  173. * In kADC_HardwareCompareMode1, compare true if the result is greater than or equal to value1.
  174. * In kADC_HardwareCompareMode2, Value1 <= Value2, compare true if the result is less than value1 Or the result is
  175. * Greater than value2.
  176. * Value1 > Value2, compare true if the result is less than value1 And the result is
  177. * Greater than value2.
  178. * In kADC_HardwareCompareMode3, Value1 <= Value2, compare true if the result is greater than or equal to value1 And the
  179. * result is less than or equal to value2.
  180. * Value1 > Value2, compare true if the result is greater than or equal to value1 Or the
  181. * result is less than or equal to value2.
  182. */
  183. typedef struct _adc_hardware_compare_config
  184. {
  185. adc_hardware_compare_mode_t hardwareCompareMode; /*!< Select the hardware compare mode.
  186. See "adc_hardware_compare_mode_t". */
  187. uint16_t value1; /*!< Setting value1(0-4095) for hardware compare mode. */
  188. uint16_t value2; /*!< Setting value2(0-4095) for hardware compare mode. */
  189. } adc_hardware_compare_config_t;
  190. /*!
  191. * @brief ADC channel conversion configuration.
  192. */
  193. typedef struct _adc_channel_config
  194. {
  195. uint32_t channelNumber; /*!< Setting the conversion channel number. The available range is 0-31.
  196. See channel connection information for each chip in Reference
  197. Manual document. */
  198. bool enableInterruptOnConversionCompleted; /*!< Generate an interrupt request once the conversion is completed. */
  199. } adc_channel_config_t;
  200. /*******************************************************************************
  201. * API
  202. ******************************************************************************/
  203. #if defined(__cplusplus)
  204. extern "C" {
  205. #endif
  206. /*!
  207. * @name Initialization
  208. * @{
  209. */
  210. /*!
  211. * @brief Initialize the ADC module.
  212. *
  213. * @param base ADC peripheral base address.
  214. * @param config Pointer to "adc_config_t" structure.
  215. */
  216. void ADC_Init(ADC_Type *base, const adc_config_t *config);
  217. /*!
  218. * @brief De-initializes the ADC module.
  219. *
  220. * @param base ADC peripheral base address.
  221. */
  222. void ADC_Deinit(ADC_Type *base);
  223. /*!
  224. * @brief Gets an available pre-defined settings for the converter's configuration.
  225. *
  226. * This function initializes the converter configuration structure with available settings. The default values are:
  227. * @code
  228. * config->enableAsynchronousClockOutput = true;
  229. * config->enableOverWrite = false;
  230. * config->enableContinuousConversion = false;
  231. * config->enableHighSpeed = false;
  232. * config->enableLowPower = false;
  233. * config->enableLongSample = false;
  234. * config->referenceVoltageSource = kADC_ReferenceVoltageSourceAlt0;
  235. * config->samplePeriodMode = kADC_SamplePeriod2or12Clocks;
  236. * config->clockSource = kADC_ClockSourceAD;
  237. * config->clockDriver = kADC_ClockDriver1;
  238. * config->resolution = kADC_Resolution12Bit;
  239. * @endcode
  240. * @param base ADC peripheral base address.
  241. * @param config Pointer to the configuration structure.
  242. */
  243. void ADC_GetDefaultConfig(adc_config_t *config);
  244. /*!
  245. * @brief Configures the conversion channel.
  246. *
  247. * This operation triggers the conversion when in software trigger mode. When in hardware trigger mode, this API
  248. * configures the channel while the external trigger source helps to trigger the conversion.
  249. *
  250. * Note that the "Channel Group" has a detailed description.
  251. * To allow sequential conversions of the ADC to be triggered by internal peripherals, the ADC has more than one
  252. * group of status and control registers, one for each conversion. The channel group parameter indicates which group of
  253. * registers are used, for example channel group 0 is for Group A registers and channel group 1 is for Group B
  254. * registers. The
  255. * channel groups are used in a "ping-pong" approach to control the ADC operation. At any point, only one of
  256. * the channel groups is actively controlling ADC conversions. The channel group 0 is used for both software and
  257. * hardware
  258. * trigger modes. Channel groups 1 and greater indicate potentially multiple channel group registers for
  259. * use only in hardware trigger mode. See the chip configuration information in the appropriate MCU reference manual
  260. * about the
  261. * number of SC1n registers (channel groups) specific to this device. None of the channel groups 1 or greater are used
  262. * for software trigger operation. Therefore, writing to these channel groups does not initiate a new conversion.
  263. * Updating the channel group 0 while a different channel group is actively controlling a conversion is allowed and
  264. * vice versa. Writing any of the channel group registers while that specific channel group is actively controlling a
  265. * conversion aborts the current conversion.
  266. *
  267. * @param base ADC peripheral base address.
  268. * @param channelGroup Channel group index.
  269. * @param config Pointer to the "adc_channel_config_t" structure for the conversion channel.
  270. */
  271. void ADC_SetChannelConfig(ADC_Type *base, uint32_t channelGroup, const adc_channel_config_t *config);
  272. /*!
  273. * @brief Gets the conversion value.
  274. *
  275. * @param base ADC peripheral base address.
  276. * @param channelGroup Channel group index.
  277. *
  278. * @return Conversion value.
  279. */
  280. static inline uint32_t ADC_GetChannelConversionValue(ADC_Type *base, uint32_t channelGroup)
  281. {
  282. assert(channelGroup < ADC_R_COUNT);
  283. return base->R[channelGroup];
  284. }
  285. /*!
  286. * @brief Gets the status flags of channel.
  287. *
  288. * A conversion is completed when the result of the conversion is transferred into the data
  289. * result registers. (provided the compare function & hardware averaging is disabled), this is
  290. * indicated by the setting of COCOn. If hardware averaging is enabled, COCOn sets only,
  291. * if the last of the selected number of conversions is complete. If the compare function is
  292. * enabled, COCOn sets and conversion result data is transferred only if the compare
  293. * condition is true. If both hardware averaging and compare functions are enabled, then
  294. * COCOn sets only if the last of the selected number of conversions is complete and the
  295. * compare condition is true.
  296. *
  297. * @param base ADC peripheral base address.
  298. * @param channelGroup Channel group index.
  299. *
  300. * @return Status flags of channel.return 0 means COCO flag is 0,return 1 means COCOflag is 1.
  301. */
  302. static inline uint32_t ADC_GetChannelStatusFlags(ADC_Type *base, uint32_t channelGroup)
  303. {
  304. assert(channelGroup < ADC_HC_COUNT);
  305. /* If flag is set,return 1,otherwise, return 0. */
  306. return (((base->HS) & (1U << channelGroup)) >> channelGroup);
  307. }
  308. /*!
  309. * @brief Automates the hardware calibration.
  310. *
  311. * This auto calibration helps to adjust the plus/minus side gain automatically.
  312. * Execute the calibration before using the converter. Note that the software trigger should be used
  313. * during calibration.
  314. *
  315. * @param base ADC peripheral base address.
  316. *
  317. * @return Execution status.
  318. * @retval kStatus_Success Calibration is done successfully.
  319. * @retval kStatus_Fail Calibration has failed.
  320. */
  321. status_t ADC_DoAutoCalibration(ADC_Type *base);
  322. /*!
  323. * @brief Set user defined offset.
  324. *
  325. * @param base ADC peripheral base address.
  326. * @param config Pointer to "adc_offest_config_t" structure.
  327. */
  328. void ADC_SetOffsetConfig(ADC_Type *base, const adc_offest_config_t *config);
  329. /*!
  330. * @brief Enables generating the DMA trigger when the conversion is complete.
  331. *
  332. * @param base ADC peripheral base address.
  333. * @param enable Switcher of the DMA feature. "true" means enabled, "false" means not enabled.
  334. */
  335. static inline void ADC_EnableDMA(ADC_Type *base, bool enable)
  336. {
  337. if (enable)
  338. {
  339. base->GC |= ADC_GC_DMAEN_MASK;
  340. }
  341. else
  342. {
  343. base->GC &= ~ADC_GC_DMAEN_MASK;
  344. }
  345. }
  346. /*!
  347. * @brief Enables the hardware trigger mode.
  348. *
  349. * @param base ADC peripheral base address.
  350. * @param enable Switcher of the trigger mode. "true" means hardware tirgger mode,"false" means software mode.
  351. */
  352. #if !(defined(FSL_FEATURE_ADC_SUPPORT_HARDWARE_TRIGGER_REMOVE) && FSL_FEATURE_ADC_SUPPORT_HARDWARE_TRIGGER_REMOVE)
  353. static inline void ADC_EnableHardwareTrigger(ADC_Type *base, bool enable)
  354. {
  355. if (enable)
  356. {
  357. base->CFG |= ADC_CFG_ADTRG_MASK;
  358. }
  359. else
  360. {
  361. base->CFG &= ~ADC_CFG_ADTRG_MASK;
  362. }
  363. }
  364. #endif
  365. /*!
  366. * @brief Configures the hardware compare mode.
  367. *
  368. * The hardware compare mode provides a way to process the conversion result automatically by using hardware. Only the
  369. * result
  370. * in the compare range is available. To compare the range, see "adc_hardware_compare_mode_t" or the appopriate
  371. * reference
  372. * manual for more information.
  373. *
  374. * @param base ADC peripheral base address.
  375. * @param Pointer to "adc_hardware_compare_config_t" structure.
  376. *
  377. */
  378. void ADC_SetHardwareCompareConfig(ADC_Type *base, const adc_hardware_compare_config_t *config);
  379. /*!
  380. * @brief Configures the hardware average mode.
  381. *
  382. * The hardware average mode provides a way to process the conversion result automatically by using hardware. The
  383. * multiple
  384. * conversion results are accumulated and averaged internally making them easier to read.
  385. *
  386. * @param base ADC peripheral base address.
  387. * @param mode Setting the hardware average mode. See "adc_hardware_average_mode_t".
  388. */
  389. void ADC_SetHardwareAverageConfig(ADC_Type *base, adc_hardware_average_mode_t mode);
  390. /*!
  391. * @brief Gets the converter's status flags.
  392. *
  393. * @param base ADC peripheral base address.
  394. *
  395. * @return Flags' mask if indicated flags are asserted. See "adc_status_flags_t".
  396. */
  397. static inline uint32_t ADC_GetStatusFlags(ADC_Type *base)
  398. {
  399. return base->GS;
  400. }
  401. /*!
  402. * @brief Clears the converter's status falgs.
  403. *
  404. * @param base ADC peripheral base address.
  405. * @param mask Mask value for the cleared flags. See "adc_status_flags_t".
  406. */
  407. void ADC_ClearStatusFlags(ADC_Type *base, uint32_t mask);
  408. #if defined(__cplusplus)
  409. }
  410. #endif
  411. #endif /* _FSL_ADC_H_ */