fsl_adc.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. #include "fsl_adc.h"
  31. /*******************************************************************************
  32. * Prototypes
  33. ******************************************************************************/
  34. /*!
  35. * @brief Get instance number for ADC module.
  36. *
  37. * @param base ADC peripheral base address
  38. */
  39. static uint32_t ADC_GetInstance(ADC_Type *base);
  40. /*******************************************************************************
  41. * Variables
  42. ******************************************************************************/
  43. /*! @brief Pointers to ADC bases for each instance. */
  44. static ADC_Type *const s_adcBases[] = ADC_BASE_PTRS;
  45. /*! @brief Pointers to ADC clocks for each instance. */
  46. static const clock_ip_name_t s_adcClocks[] = ADC_CLOCKS;
  47. /*******************************************************************************
  48. * Code
  49. ******************************************************************************/
  50. static uint32_t ADC_GetInstance(ADC_Type *base)
  51. {
  52. uint32_t instance;
  53. /* Find the instance index from base address mappings. */
  54. for (instance = 0; instance < ARRAY_SIZE(s_adcBases); instance++)
  55. {
  56. if (s_adcBases[instance] == base)
  57. {
  58. break;
  59. }
  60. }
  61. assert(instance < ARRAY_SIZE(s_adcBases));
  62. return instance;
  63. }
  64. void ADC_Init(ADC_Type *base, const adc_config_t *config)
  65. {
  66. assert(NULL != config);
  67. uint32_t tmp32;
  68. /* Enable the clock. */
  69. CLOCK_EnableClock(s_adcClocks[ADC_GetInstance(base)]);
  70. /* ADCx_CFG */
  71. tmp32 = base->CFG & (ADC_CFG_AVGS_MASK | ADC_CFG_ADTRG_MASK); /* Reserve AVGS and ADTRG bits. */
  72. tmp32 |= ADC_CFG_REFSEL(config->referenceVoltageSource) | ADC_CFG_ADSTS(config->samplePeriodMode) |
  73. ADC_CFG_ADICLK(config->clockSource) | ADC_CFG_ADIV(config->clockDriver) | ADC_CFG_MODE(config->resolution);
  74. if (config->enableOverWrite)
  75. {
  76. tmp32 |= ADC_CFG_OVWREN_MASK;
  77. }
  78. if (config->enableLongSample)
  79. {
  80. tmp32 |= ADC_CFG_ADLSMP_MASK;
  81. }
  82. if (config->enableLowPower)
  83. {
  84. tmp32 |= ADC_CFG_ADLPC_MASK;
  85. }
  86. if (config->enableHighSpeed)
  87. {
  88. tmp32 |= ADC_CFG_ADHSC_MASK;
  89. }
  90. base->CFG = tmp32;
  91. /* ADCx_GC */
  92. tmp32 = base->GC & ~(ADC_GC_ADCO_MASK | ADC_GC_ADACKEN_MASK);
  93. if (config->enableContinuousConversion)
  94. {
  95. tmp32 |= ADC_GC_ADCO_MASK;
  96. }
  97. if (config->enableAsynchronousClockOutput)
  98. {
  99. tmp32 |= ADC_GC_ADACKEN_MASK;
  100. }
  101. base->GC = tmp32;
  102. }
  103. void ADC_Deinit(ADC_Type *base)
  104. {
  105. /* Disable the clock. */
  106. CLOCK_DisableClock(s_adcClocks[ADC_GetInstance(base)]);
  107. }
  108. void ADC_GetDefaultConfig(adc_config_t *config)
  109. {
  110. assert(NULL != config);
  111. config->enableAsynchronousClockOutput = true;
  112. config->enableOverWrite = false;
  113. config->enableContinuousConversion = false;
  114. config->enableHighSpeed = false;
  115. config->enableLowPower = false;
  116. config->enableLongSample = false;
  117. config->referenceVoltageSource = kADC_ReferenceVoltageSourceAlt0;
  118. config->samplePeriodMode = kADC_SamplePeriod2or12Clocks;
  119. config->clockSource = kADC_ClockSourceAD;
  120. config->clockDriver = kADC_ClockDriver1;
  121. config->resolution = kADC_Resolution12Bit;
  122. }
  123. void ADC_SetChannelConfig(ADC_Type *base, uint32_t channelGroup, const adc_channel_config_t *config)
  124. {
  125. assert(NULL != config);
  126. assert(channelGroup < ADC_HC_COUNT);
  127. uint32_t tmp32;
  128. tmp32 = ADC_HC_ADCH(config->channelNumber);
  129. if (config->enableInterruptOnConversionCompleted)
  130. {
  131. tmp32 |= ADC_HC_AIEN_MASK;
  132. }
  133. base->HC[channelGroup] = tmp32;
  134. }
  135. /*
  136. *To complete calibration, the user must follow the below procedure:
  137. * 1. Configure ADC_CFG with actual operating values for maximum accuracy.
  138. * 2. Configure the ADC_GC values along with CAL bit.
  139. * 3. Check the status of CALF bit in ADC_GS and the CAL bit in ADC_GC.
  140. * 4. When CAL bit becomes '0' then check the CALF status and COCO[0] bit status.
  141. */
  142. status_t ADC_DoAutoCalibration(ADC_Type *base)
  143. {
  144. status_t status = kStatus_Success;
  145. #if !(defined(FSL_FEATURE_ADC_SUPPORT_HARDWARE_TRIGGER_REMOVE) && FSL_FEATURE_ADC_SUPPORT_HARDWARE_TRIGGER_REMOVE)
  146. bool bHWTrigger = false;
  147. /* The calibration would be failed when in hardwar mode.
  148. * Remember the hardware trigger state here and restore it later if the hardware trigger is enabled.*/
  149. if (0U != (ADC_CFG_ADTRG_MASK & base->CFG))
  150. {
  151. bHWTrigger = true;
  152. ADC_EnableHardwareTrigger(base, false);
  153. }
  154. #endif
  155. /* Clear the CALF and launch the calibration. */
  156. base->GS = ADC_GS_CALF_MASK; /* Clear the CALF. */
  157. base->GC |= ADC_GC_CAL_MASK; /* Launch the calibration. */
  158. /* Check the status of CALF bit in ADC_GS and the CAL bit in ADC_GC. */
  159. while (0U != (base->GC & ADC_GC_CAL_MASK))
  160. {
  161. /* Check the CALF when the calibration is active. */
  162. if (0U != (ADC_GetStatusFlags(base) & kADC_CalibrationFailedFlag))
  163. {
  164. status = kStatus_Fail;
  165. break;
  166. }
  167. }
  168. /* When CAL bit becomes '0' then check the CALF status and COCO[0] bit status. */
  169. if (0U == ADC_GetChannelStatusFlags(base, 0U)) /* Check the COCO[0] bit status. */
  170. {
  171. status = kStatus_Fail;
  172. }
  173. if (0U != (ADC_GetStatusFlags(base) & kADC_CalibrationFailedFlag)) /* Check the CALF status. */
  174. {
  175. status = kStatus_Fail;
  176. }
  177. /* Clear conversion done flag. */
  178. ADC_GetChannelConversionValue(base, 0U);
  179. #if !(defined(FSL_FEATURE_ADC_SUPPORT_HARDWARE_TRIGGER_REMOVE) && FSL_FEATURE_ADC_SUPPORT_HARDWARE_TRIGGER_REMOVE)
  180. /* Restore original trigger mode. */
  181. if (true == bHWTrigger)
  182. {
  183. ADC_EnableHardwareTrigger(base, true);
  184. }
  185. #endif
  186. return status;
  187. }
  188. void ADC_SetOffsetConfig(ADC_Type *base, const adc_offest_config_t *config)
  189. {
  190. assert(NULL != config);
  191. uint32_t tmp32;
  192. tmp32 = ADC_OFS_OFS(config->offsetValue);
  193. if (config->enableSigned)
  194. {
  195. tmp32 |= ADC_OFS_SIGN_MASK;
  196. }
  197. base->OFS = tmp32;
  198. }
  199. void ADC_SetHardwareCompareConfig(ADC_Type *base, const adc_hardware_compare_config_t *config)
  200. {
  201. uint32_t tmp32;
  202. tmp32 = base->GC & ~(ADC_GC_ACFE_MASK | ADC_GC_ACFGT_MASK | ADC_GC_ACREN_MASK);
  203. if (NULL == config) /* Pass "NULL" to disable the feature. */
  204. {
  205. base->GC = tmp32;
  206. return;
  207. }
  208. /* Enable the feature. */
  209. tmp32 |= ADC_GC_ACFE_MASK;
  210. /* Select the hardware compare working mode. */
  211. switch (config->hardwareCompareMode)
  212. {
  213. case kADC_HardwareCompareMode0:
  214. break;
  215. case kADC_HardwareCompareMode1:
  216. tmp32 |= ADC_GC_ACFGT_MASK;
  217. break;
  218. case kADC_HardwareCompareMode2:
  219. tmp32 |= ADC_GC_ACREN_MASK;
  220. break;
  221. case kADC_HardwareCompareMode3:
  222. tmp32 |= ADC_GC_ACFGT_MASK | ADC_GC_ACREN_MASK;
  223. break;
  224. default:
  225. break;
  226. }
  227. base->GC = tmp32;
  228. /* Load the compare values. */
  229. tmp32 = ADC_CV_CV1(config->value1) | ADC_CV_CV2(config->value2);
  230. base->CV = tmp32;
  231. }
  232. void ADC_SetHardwareAverageConfig(ADC_Type *base, adc_hardware_average_mode_t mode)
  233. {
  234. uint32_t tmp32;
  235. if (mode == kADC_HardwareAverageDiasable)
  236. {
  237. base->GC &= ~ADC_GC_AVGE_MASK;
  238. }
  239. else
  240. {
  241. tmp32 = base->CFG & ~ADC_CFG_AVGS_MASK;
  242. tmp32 |= ADC_CFG_AVGS(mode);
  243. base->CFG = tmp32;
  244. base->GC |= ADC_GC_AVGE_MASK; /* Enable the hardware compare. */
  245. }
  246. }
  247. void ADC_ClearStatusFlags(ADC_Type *base, uint32_t mask)
  248. {
  249. uint32_t tmp32 = 0;
  250. if (0U != (mask & kADC_CalibrationFailedFlag))
  251. {
  252. tmp32 |= ADC_GS_CALF_MASK;
  253. }
  254. if (0U != (mask & kADC_ConversionActiveFlag))
  255. {
  256. tmp32 |= ADC_GS_ADACT_MASK;
  257. }
  258. base->GS = tmp32;
  259. }