fsl_adc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. #include "fsl_adc.h"
  9. /* Component ID definition, used by tools. */
  10. #ifndef FSL_COMPONENT_ID
  11. #define FSL_COMPONENT_ID "platform.drivers.adc_12b1msps_sar"
  12. #endif
  13. /*******************************************************************************
  14. * Prototypes
  15. ******************************************************************************/
  16. /*!
  17. * @brief Get instance number for ADC module.
  18. *
  19. * @param base ADC peripheral base address
  20. */
  21. static uint32_t ADC_GetInstance(ADC_Type *base);
  22. /*******************************************************************************
  23. * Variables
  24. ******************************************************************************/
  25. /*! @brief Pointers to ADC bases for each instance. */
  26. static ADC_Type *const s_adcBases[] = ADC_BASE_PTRS;
  27. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  28. /*! @brief Pointers to ADC clocks for each instance. */
  29. static const clock_ip_name_t s_adcClocks[] = ADC_CLOCKS;
  30. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  31. /*******************************************************************************
  32. * Code
  33. ******************************************************************************/
  34. static uint32_t ADC_GetInstance(ADC_Type *base)
  35. {
  36. uint32_t instance;
  37. /* Find the instance index from base address mappings. */
  38. for (instance = 0; instance < ARRAY_SIZE(s_adcBases); instance++)
  39. {
  40. if (s_adcBases[instance] == base)
  41. {
  42. break;
  43. }
  44. }
  45. assert(instance < ARRAY_SIZE(s_adcBases));
  46. return instance;
  47. }
  48. /*!
  49. * brief Initialize the ADC module.
  50. *
  51. * param base ADC peripheral base address.
  52. * param config Pointer to "adc_config_t" structure.
  53. */
  54. void ADC_Init(ADC_Type *base, const adc_config_t *config)
  55. {
  56. assert(NULL != config);
  57. uint32_t tmp32;
  58. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  59. /* Enable the clock. */
  60. CLOCK_EnableClock(s_adcClocks[ADC_GetInstance(base)]);
  61. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  62. /* ADCx_CFG */
  63. tmp32 = base->CFG & (ADC_CFG_AVGS_MASK | ADC_CFG_ADTRG_MASK); /* Reserve AVGS and ADTRG bits. */
  64. tmp32 |= ADC_CFG_REFSEL(config->referenceVoltageSource) | ADC_CFG_ADSTS(config->samplePeriodMode) |
  65. ADC_CFG_ADICLK(config->clockSource) | ADC_CFG_ADIV(config->clockDriver) | ADC_CFG_MODE(config->resolution);
  66. if (config->enableOverWrite)
  67. {
  68. tmp32 |= ADC_CFG_OVWREN_MASK;
  69. }
  70. if (config->enableLongSample)
  71. {
  72. tmp32 |= ADC_CFG_ADLSMP_MASK;
  73. }
  74. if (config->enableLowPower)
  75. {
  76. tmp32 |= ADC_CFG_ADLPC_MASK;
  77. }
  78. if (config->enableHighSpeed)
  79. {
  80. tmp32 |= ADC_CFG_ADHSC_MASK;
  81. }
  82. base->CFG = tmp32;
  83. /* ADCx_GC */
  84. tmp32 = base->GC & ~(ADC_GC_ADCO_MASK | ADC_GC_ADACKEN_MASK);
  85. if (config->enableContinuousConversion)
  86. {
  87. tmp32 |= ADC_GC_ADCO_MASK;
  88. }
  89. if (config->enableAsynchronousClockOutput)
  90. {
  91. tmp32 |= ADC_GC_ADACKEN_MASK;
  92. }
  93. base->GC = tmp32;
  94. }
  95. /*!
  96. * brief De-initializes the ADC module.
  97. *
  98. * param base ADC peripheral base address.
  99. */
  100. void ADC_Deinit(ADC_Type *base)
  101. {
  102. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  103. /* Disable the clock. */
  104. CLOCK_DisableClock(s_adcClocks[ADC_GetInstance(base)]);
  105. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  106. }
  107. /*!
  108. * brief Gets an available pre-defined settings for the converter's configuration.
  109. *
  110. * This function initializes the converter configuration structure with available settings. The default values are:
  111. * code
  112. * config->enableAsynchronousClockOutput = true;
  113. * config->enableOverWrite = false;
  114. * config->enableContinuousConversion = false;
  115. * config->enableHighSpeed = false;
  116. * config->enableLowPower = false;
  117. * config->enableLongSample = false;
  118. * config->referenceVoltageSource = kADC_ReferenceVoltageSourceAlt0;
  119. * config->samplePeriodMode = kADC_SamplePeriod2or12Clocks;
  120. * config->clockSource = kADC_ClockSourceAD;
  121. * config->clockDriver = kADC_ClockDriver1;
  122. * config->resolution = kADC_Resolution12Bit;
  123. * endcode
  124. * param base ADC peripheral base address.
  125. * param config Pointer to the configuration structure.
  126. */
  127. void ADC_GetDefaultConfig(adc_config_t *config)
  128. {
  129. assert(NULL != config);
  130. /* Initializes the configure structure to zero. */
  131. (void)memset(config, 0, sizeof(*config));
  132. config->enableAsynchronousClockOutput = true;
  133. config->enableOverWrite = false;
  134. config->enableContinuousConversion = false;
  135. config->enableHighSpeed = false;
  136. config->enableLowPower = false;
  137. config->enableLongSample = false;
  138. config->referenceVoltageSource = kADC_ReferenceVoltageSourceAlt0;
  139. config->samplePeriodMode = kADC_SamplePeriod2or12Clocks;
  140. config->clockSource = kADC_ClockSourceAD;
  141. config->clockDriver = kADC_ClockDriver1;
  142. config->resolution = kADC_Resolution12Bit;
  143. }
  144. /*!
  145. * brief Configures the conversion channel.
  146. *
  147. * This operation triggers the conversion when in software trigger mode. When in hardware trigger mode, this API
  148. * configures the channel while the external trigger source helps to trigger the conversion.
  149. *
  150. * Note that the "Channel Group" has a detailed description.
  151. * To allow sequential conversions of the ADC to be triggered by internal peripherals, the ADC has more than one
  152. * group of status and control registers, one for each conversion. The channel group parameter indicates which group of
  153. * registers are used, for example channel group 0 is for Group A registers and channel group 1 is for Group B
  154. * registers. The
  155. * channel groups are used in a "ping-pong" approach to control the ADC operation. At any point, only one of
  156. * the channel groups is actively controlling ADC conversions. The channel group 0 is used for both software and
  157. * hardware
  158. * trigger modes. Channel groups 1 and greater indicate potentially multiple channel group registers for
  159. * use only in hardware trigger mode. See the chip configuration information in the appropriate MCU reference manual
  160. * about the
  161. * number of SC1n registers (channel groups) specific to this device. None of the channel groups 1 or greater are used
  162. * for software trigger operation. Therefore, writing to these channel groups does not initiate a new conversion.
  163. * Updating the channel group 0 while a different channel group is actively controlling a conversion is allowed and
  164. * vice versa. Writing any of the channel group registers while that specific channel group is actively controlling a
  165. * conversion aborts the current conversion.
  166. *
  167. * param base ADC peripheral base address.
  168. * param channelGroup Channel group index.
  169. * param config Pointer to the "adc_channel_config_t" structure for the conversion channel.
  170. */
  171. void ADC_SetChannelConfig(ADC_Type *base, uint32_t channelGroup, const adc_channel_config_t *config)
  172. {
  173. assert(NULL != config);
  174. assert(channelGroup < (uint32_t)FSL_FEATURE_ADC_CONVERSION_CONTROL_COUNT);
  175. uint32_t tmp32;
  176. tmp32 = ADC_HC_ADCH(config->channelNumber);
  177. if (config->enableInterruptOnConversionCompleted)
  178. {
  179. tmp32 |= ADC_HC_AIEN_MASK;
  180. }
  181. base->HC[channelGroup] = tmp32;
  182. }
  183. /*
  184. *To complete calibration, the user must follow the below procedure:
  185. * 1. Configure ADC_CFG with actual operating values for maximum accuracy.
  186. * 2. Configure the ADC_GC values along with CAL bit.
  187. * 3. Check the status of CALF bit in ADC_GS and the CAL bit in ADC_GC.
  188. * 4. When CAL bit becomes '0' then check the CALF status and COCO[0] bit status.
  189. */
  190. /*!
  191. * brief Automates the hardware calibration.
  192. *
  193. * This auto calibration helps to adjust the plus/minus side gain automatically.
  194. * Execute the calibration before using the converter. Note that the software trigger should be used
  195. * during calibration.
  196. *
  197. * param base ADC peripheral base address.
  198. *
  199. * return Execution status.
  200. * retval kStatus_Success Calibration is done successfully.
  201. * retval kStatus_Fail Calibration has failed.
  202. */
  203. status_t ADC_DoAutoCalibration(ADC_Type *base)
  204. {
  205. status_t status = kStatus_Success;
  206. #if !(defined(FSL_FEATURE_ADC_SUPPORT_HARDWARE_TRIGGER_REMOVE) && FSL_FEATURE_ADC_SUPPORT_HARDWARE_TRIGGER_REMOVE)
  207. bool bHWTrigger = false;
  208. /* The calibration would be failed when in hardwar mode.
  209. * Remember the hardware trigger state here and restore it later if the hardware trigger is enabled.*/
  210. if (0U != (ADC_CFG_ADTRG_MASK & base->CFG))
  211. {
  212. bHWTrigger = true;
  213. ADC_EnableHardwareTrigger(base, false);
  214. }
  215. #endif
  216. /* Clear the CALF and launch the calibration. */
  217. base->GS = ADC_GS_CALF_MASK; /* Clear the CALF. */
  218. base->GC |= ADC_GC_CAL_MASK; /* Launch the calibration. */
  219. /* Check the status of CALF bit in ADC_GS and the CAL bit in ADC_GC. */
  220. while (0U != (base->GC & ADC_GC_CAL_MASK))
  221. {
  222. /* Check the CALF when the calibration is active. */
  223. if (0U != (ADC_GetStatusFlags(base) & (uint32_t)kADC_CalibrationFailedFlag))
  224. {
  225. status = kStatus_Fail;
  226. break;
  227. }
  228. }
  229. /* When CAL bit becomes '0' then check the CALF status and COCO[0] bit status. */
  230. if (0U == ADC_GetChannelStatusFlags(base, 0U)) /* Check the COCO[0] bit status. */
  231. {
  232. status = kStatus_Fail;
  233. }
  234. if (0U != (ADC_GetStatusFlags(base) & (uint32_t)kADC_CalibrationFailedFlag)) /* Check the CALF status. */
  235. {
  236. status = kStatus_Fail;
  237. }
  238. /* Clear conversion done flag. */
  239. (void)ADC_GetChannelConversionValue(base, 0U);
  240. #if !(defined(FSL_FEATURE_ADC_SUPPORT_HARDWARE_TRIGGER_REMOVE) && FSL_FEATURE_ADC_SUPPORT_HARDWARE_TRIGGER_REMOVE)
  241. /* Restore original trigger mode. */
  242. if (true == bHWTrigger)
  243. {
  244. ADC_EnableHardwareTrigger(base, true);
  245. }
  246. #endif
  247. return status;
  248. }
  249. /*!
  250. * brief Set user defined offset.
  251. *
  252. * param base ADC peripheral base address.
  253. * param config Pointer to "adc_offest_config_t" structure.
  254. */
  255. void ADC_SetOffsetConfig(ADC_Type *base, const adc_offest_config_t *config)
  256. {
  257. assert(NULL != config);
  258. uint32_t tmp32;
  259. tmp32 = ADC_OFS_OFS(config->offsetValue);
  260. if (config->enableSigned)
  261. {
  262. tmp32 |= ADC_OFS_SIGN_MASK;
  263. }
  264. base->OFS = tmp32;
  265. }
  266. /*!
  267. * brief Configures the hardware compare mode.
  268. *
  269. * The hardware compare mode provides a way to process the conversion result automatically by using hardware. Only the
  270. * result
  271. * in the compare range is available. To compare the range, see "adc_hardware_compare_mode_t" or the appopriate
  272. * reference
  273. * manual for more information.
  274. *
  275. * param base ADC peripheral base address.
  276. * param Pointer to "adc_hardware_compare_config_t" structure.
  277. *
  278. */
  279. void ADC_SetHardwareCompareConfig(ADC_Type *base, const adc_hardware_compare_config_t *config)
  280. {
  281. uint32_t tmp32;
  282. tmp32 = base->GC & ~(ADC_GC_ACFE_MASK | ADC_GC_ACFGT_MASK | ADC_GC_ACREN_MASK);
  283. if (NULL == config) /* Pass "NULL" to disable the feature. */
  284. {
  285. base->GC = tmp32;
  286. return;
  287. }
  288. /* Enable the feature. */
  289. tmp32 |= ADC_GC_ACFE_MASK;
  290. /* Select the hardware compare working mode. */
  291. switch (config->hardwareCompareMode)
  292. {
  293. case kADC_HardwareCompareMode0:
  294. break;
  295. case kADC_HardwareCompareMode1:
  296. tmp32 |= ADC_GC_ACFGT_MASK;
  297. break;
  298. case kADC_HardwareCompareMode2:
  299. tmp32 |= ADC_GC_ACREN_MASK;
  300. break;
  301. case kADC_HardwareCompareMode3:
  302. tmp32 |= ADC_GC_ACFGT_MASK | ADC_GC_ACREN_MASK;
  303. break;
  304. default:
  305. assert(false);
  306. break;
  307. }
  308. base->GC = tmp32;
  309. /* Load the compare values. */
  310. tmp32 = ADC_CV_CV1(config->value1) | ADC_CV_CV2(config->value2);
  311. base->CV = tmp32;
  312. }
  313. /*!
  314. * brief Configures the hardware average mode.
  315. *
  316. * The hardware average mode provides a way to process the conversion result automatically by using hardware. The
  317. * multiple
  318. * conversion results are accumulated and averaged internally making them easier to read.
  319. *
  320. * param base ADC peripheral base address.
  321. * param mode Setting the hardware average mode. See "adc_hardware_average_mode_t".
  322. */
  323. void ADC_SetHardwareAverageConfig(ADC_Type *base, adc_hardware_average_mode_t mode)
  324. {
  325. uint32_t tmp32;
  326. if (mode == kADC_HardwareAverageDiasable)
  327. {
  328. base->GC &= ~ADC_GC_AVGE_MASK;
  329. }
  330. else
  331. {
  332. tmp32 = base->CFG & ~ADC_CFG_AVGS_MASK;
  333. tmp32 |= ADC_CFG_AVGS(mode);
  334. base->CFG = tmp32;
  335. base->GC |= ADC_GC_AVGE_MASK; /* Enable the hardware compare. */
  336. }
  337. }
  338. /*!
  339. * brief Clears the converter's status falgs.
  340. *
  341. * param base ADC peripheral base address.
  342. * param mask Mask value for the cleared flags. See "adc_status_flags_t".
  343. */
  344. void ADC_ClearStatusFlags(ADC_Type *base, uint32_t mask)
  345. {
  346. uint32_t tmp32 = 0;
  347. if (0U != (mask & (uint32_t)kADC_CalibrationFailedFlag))
  348. {
  349. tmp32 |= ADC_GS_CALF_MASK;
  350. }
  351. if (0U != (mask & (uint32_t)kADC_ConversionActiveFlag))
  352. {
  353. tmp32 |= ADC_GS_ADACT_MASK;
  354. }
  355. base->GS = tmp32;
  356. }