fsl_adc.h 17 KB

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