fsl_adc.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 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, 1)) /*!< Version 2.0.1. */
  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 occured, 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 base ADC peripheral base address.
  215. * @param config Pointer to the configuration structure.
  216. */
  217. void ADC_GetDefaultConfig(adc_config_t *config);
  218. /*!
  219. * @brief Configures the conversion channel.
  220. *
  221. * This operation triggers the conversion when in software trigger mode. When in hardware trigger mode, this API
  222. * configures the channel while the external trigger source helps to trigger the conversion.
  223. *
  224. * Note that the "Channel Group" has a detailed description.
  225. * To allow sequential conversions of the ADC to be triggered by internal peripherals, the ADC has more than one
  226. * group of status and control registers, one for each conversion. The channel group parameter indicates which group of
  227. * registers are used, for example channel group 0 is for Group A registers and channel group 1 is for Group B
  228. * registers. The
  229. * channel groups are used in a "ping-pong" approach to control the ADC operation. At any point, only one of
  230. * the channel groups is actively controlling ADC conversions. The channel group 0 is used for both software and
  231. * hardware
  232. * trigger modes. Channel groups 1 and greater indicate potentially multiple channel group registers for
  233. * use only in hardware trigger mode. See the chip configuration information in the appropriate MCU reference manual
  234. * about the
  235. * number of SC1n registers (channel groups) specific to this device. None of the channel groups 1 or greater are used
  236. * for software trigger operation. Therefore, writing to these channel groups does not initiate a new conversion.
  237. * Updating the channel group 0 while a different channel group is actively controlling a conversion is allowed and
  238. * vice versa. Writing any of the channel group registers while that specific channel group is actively controlling a
  239. * conversion aborts the current conversion.
  240. *
  241. * @param base ADC peripheral base address.
  242. * @param channelGroup Channel group index.
  243. * @param config Pointer to the "adc_channel_config_t" structure for the conversion channel.
  244. */
  245. void ADC_SetChannelConfig(ADC_Type *base, uint32_t channelGroup, const adc_channel_config_t *config);
  246. /*!
  247. * @brief Gets the conversion value.
  248. *
  249. * @param base ADC peripheral base address.
  250. * @param channelGroup Channel group index.
  251. *
  252. * @return Conversion value.
  253. */
  254. static inline uint32_t ADC_GetChannelConversionValue(ADC_Type *base, uint32_t channelGroup)
  255. {
  256. assert(channelGroup < ADC_R_COUNT);
  257. return base->R[channelGroup];
  258. }
  259. /*!
  260. * @brief Gets the status flags of channel.
  261. *
  262. * A conversion is completed when the result of the conversion is transferred into the data
  263. * result registers. (provided the compare function & hardware averaging is disabled), this is
  264. * indicated by the setting of COCOn. If hardware averaging is enabled, COCOn sets only,
  265. * if the last of the selected number of conversions is complete. If the compare function is
  266. * enabled, COCOn sets and conversion result data is transferred only if the compare
  267. * condition is true. If both hardware averaging and compare functions are enabled, then
  268. * COCOn sets only if the last of the selected number of conversions is complete and the
  269. * compare condition is true.
  270. *
  271. * @param base ADC peripheral base address.
  272. * @param channelGroup Channel group index.
  273. *
  274. * @return Status flags of channel.return 0 means COCO flag is 0,return 1 means COCOflag is 1.
  275. */
  276. static inline uint32_t ADC_GetChannelStatusFlags(ADC_Type *base, uint32_t channelGroup)
  277. {
  278. assert(channelGroup < ADC_HC_COUNT);
  279. /* If flag is set,return 1,otherwise, return 0. */
  280. return (((base->HS) & (1U << channelGroup)) >> channelGroup);
  281. }
  282. /*!
  283. * @brief Automates the hardware calibration.
  284. *
  285. * This auto calibration helps to adjust the plus/minus side gain automatically.
  286. * Execute the calibration before using the converter. Note that the software trigger should be used
  287. * during calibration.
  288. *
  289. * @param base ADC peripheral base address.
  290. *
  291. * @return Execution status.
  292. * @retval kStatus_Success Calibration is done successfully.
  293. * @retval kStatus_Fail Calibration has failed.
  294. */
  295. status_t ADC_DoAutoCalibration(ADC_Type *base);
  296. /*!
  297. * @brief Set user defined offset.
  298. *
  299. * @param base ADC peripheral base address.
  300. * @param config Pointer to "adc_offest_config_t" structure.
  301. */
  302. void ADC_SetOffsetConfig(ADC_Type *base, const adc_offest_config_t *config);
  303. /*!
  304. * @brief Enables generating the DMA trigger when the conversion is complete.
  305. *
  306. * @param base ADC peripheral base address.
  307. * @param enable Switcher of the DMA feature. "true" means enabled, "false" means not enabled.
  308. */
  309. static inline void ADC_EnableDMA(ADC_Type *base, bool enable)
  310. {
  311. if (enable)
  312. {
  313. base->GC |= ADC_GC_DMAEN_MASK;
  314. }
  315. else
  316. {
  317. base->GC &= ~ADC_GC_DMAEN_MASK;
  318. }
  319. }
  320. /*!
  321. * @brief Enables the hardware trigger mode.
  322. *
  323. * @param base ADC peripheral base address.
  324. * @param enable Switcher of the trigger mode. "true" means hardware tirgger mode,"false" means software mode.
  325. */
  326. #if !(defined(FSL_FEATURE_ADC_SUPPORT_HARDWARE_TRIGGER_REMOVE) && FSL_FEATURE_ADC_SUPPORT_HARDWARE_TRIGGER_REMOVE)
  327. static inline void ADC_EnableHardwareTrigger(ADC_Type *base, bool enable)
  328. {
  329. if (enable)
  330. {
  331. base->CFG |= ADC_CFG_ADTRG_MASK;
  332. }
  333. else
  334. {
  335. base->CFG &= ~ADC_CFG_ADTRG_MASK;
  336. }
  337. }
  338. #endif
  339. /*!
  340. * @brief Configures the hardware compare mode.
  341. *
  342. * The hardware compare mode provides a way to process the conversion result automatically by using hardware. Only the
  343. * result
  344. * in the compare range is available. To compare the range, see "adc_hardware_compare_mode_t" or the appopriate
  345. * reference
  346. * manual for more information.
  347. *
  348. * @param base ADC peripheral base address.
  349. * @param Pointer to "adc_hardware_compare_config_t" structure.
  350. *
  351. */
  352. void ADC_SetHardwareCompareConfig(ADC_Type *base, const adc_hardware_compare_config_t *config);
  353. /*!
  354. * @brief Configures the hardware average mode.
  355. *
  356. * The hardware average mode provides a way to process the conversion result automatically by using hardware. The
  357. * multiple
  358. * conversion results are accumulated and averaged internally making them easier to read.
  359. *
  360. * @param base ADC peripheral base address.
  361. * @param mode Setting the hardware average mode. See "adc_hardware_average_mode_t".
  362. */
  363. void ADC_SetHardwareAverageConfig(ADC_Type *base, adc_hardware_average_mode_t mode);
  364. /*!
  365. * @brief Gets the converter's status flags.
  366. *
  367. * @param base ADC peripheral base address.
  368. *
  369. * @return Flags' mask if indicated flags are asserted. See "adc_status_flags_t".
  370. */
  371. static inline uint32_t ADC_GetStatusFlags(ADC_Type *base)
  372. {
  373. return base->GS;
  374. }
  375. /*!
  376. * @brief Clears the converter's status falgs.
  377. *
  378. * @param base ADC peripheral base address.
  379. * @param mask Mask value for the cleared flags. See "adc_status_flags_t".
  380. */
  381. void ADC_ClearStatusFlags(ADC_Type *base, uint32_t mask);
  382. #if defined(__cplusplus)
  383. }
  384. #endif
  385. #endif /* _FSL_ADC_H_ */