fsl_adc.h 19 KB

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