fsl_adc.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  4. * Copyright 2016-2017 NXP
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include "fsl_adc.h"
  35. /* Component ID definition, used by tools. */
  36. #ifndef FSL_COMPONENT_ID
  37. #define FSL_COMPONENT_ID "platform.drivers.adc_12b1msps_sar"
  38. #endif
  39. /*******************************************************************************
  40. * Prototypes
  41. ******************************************************************************/
  42. /*!
  43. * @brief Get instance number for ADC module.
  44. *
  45. * @param base ADC peripheral base address
  46. */
  47. static uint32_t ADC_GetInstance(ADC_Type *base);
  48. /*******************************************************************************
  49. * Variables
  50. ******************************************************************************/
  51. /*! @brief Pointers to ADC bases for each instance. */
  52. static ADC_Type *const s_adcBases[] = ADC_BASE_PTRS;
  53. /*! @brief Pointers to ADC clocks for each instance. */
  54. static const clock_ip_name_t s_adcClocks[] = ADC_CLOCKS;
  55. /*******************************************************************************
  56. * Code
  57. ******************************************************************************/
  58. static uint32_t ADC_GetInstance(ADC_Type *base)
  59. {
  60. uint32_t instance;
  61. /* Find the instance index from base address mappings. */
  62. for (instance = 0; instance < ARRAY_SIZE(s_adcBases); instance++)
  63. {
  64. if (s_adcBases[instance] == base)
  65. {
  66. break;
  67. }
  68. }
  69. assert(instance < ARRAY_SIZE(s_adcBases));
  70. return instance;
  71. }
  72. void ADC_Init(ADC_Type *base, const adc_config_t *config)
  73. {
  74. assert(NULL != config);
  75. uint32_t tmp32;
  76. /* Enable the clock. */
  77. CLOCK_EnableClock(s_adcClocks[ADC_GetInstance(base)]);
  78. /* ADCx_CFG */
  79. tmp32 = base->CFG & (ADC_CFG_AVGS_MASK | ADC_CFG_ADTRG_MASK); /* Reserve AVGS and ADTRG bits. */
  80. tmp32 |= ADC_CFG_REFSEL(config->referenceVoltageSource) | ADC_CFG_ADSTS(config->samplePeriodMode) |
  81. ADC_CFG_ADICLK(config->clockSource) | ADC_CFG_ADIV(config->clockDriver) | ADC_CFG_MODE(config->resolution);
  82. if (config->enableOverWrite)
  83. {
  84. tmp32 |= ADC_CFG_OVWREN_MASK;
  85. }
  86. if (config->enableLongSample)
  87. {
  88. tmp32 |= ADC_CFG_ADLSMP_MASK;
  89. }
  90. if (config->enableLowPower)
  91. {
  92. tmp32 |= ADC_CFG_ADLPC_MASK;
  93. }
  94. if (config->enableHighSpeed)
  95. {
  96. tmp32 |= ADC_CFG_ADHSC_MASK;
  97. }
  98. base->CFG = tmp32;
  99. /* ADCx_GC */
  100. tmp32 = base->GC & ~(ADC_GC_ADCO_MASK | ADC_GC_ADACKEN_MASK);
  101. if (config->enableContinuousConversion)
  102. {
  103. tmp32 |= ADC_GC_ADCO_MASK;
  104. }
  105. if (config->enableAsynchronousClockOutput)
  106. {
  107. tmp32 |= ADC_GC_ADACKEN_MASK;
  108. }
  109. base->GC = tmp32;
  110. }
  111. void ADC_Deinit(ADC_Type *base)
  112. {
  113. /* Disable the clock. */
  114. CLOCK_DisableClock(s_adcClocks[ADC_GetInstance(base)]);
  115. }
  116. void ADC_GetDefaultConfig(adc_config_t *config)
  117. {
  118. assert(NULL != config);
  119. config->enableAsynchronousClockOutput = true;
  120. config->enableOverWrite = false;
  121. config->enableContinuousConversion = false;
  122. config->enableHighSpeed = false;
  123. config->enableLowPower = false;
  124. config->enableLongSample = false;
  125. config->referenceVoltageSource = kADC_ReferenceVoltageSourceAlt0;
  126. config->samplePeriodMode = kADC_SamplePeriod2or12Clocks;
  127. config->clockSource = kADC_ClockSourceAD;
  128. config->clockDriver = kADC_ClockDriver1;
  129. config->resolution = kADC_Resolution12Bit;
  130. }
  131. void ADC_SetChannelConfig(ADC_Type *base, uint32_t channelGroup, const adc_channel_config_t *config)
  132. {
  133. assert(NULL != config);
  134. assert(channelGroup < ADC_HC_COUNT);
  135. uint32_t tmp32;
  136. tmp32 = ADC_HC_ADCH(config->channelNumber);
  137. if (config->enableInterruptOnConversionCompleted)
  138. {
  139. tmp32 |= ADC_HC_AIEN_MASK;
  140. }
  141. base->HC[channelGroup] = tmp32;
  142. }
  143. /*
  144. *To complete calibration, the user must follow the below procedure:
  145. * 1. Configure ADC_CFG with actual operating values for maximum accuracy.
  146. * 2. Configure the ADC_GC values along with CAL bit.
  147. * 3. Check the status of CALF bit in ADC_GS and the CAL bit in ADC_GC.
  148. * 4. When CAL bit becomes '0' then check the CALF status and COCO[0] bit status.
  149. */
  150. status_t ADC_DoAutoCalibration(ADC_Type *base)
  151. {
  152. status_t status = kStatus_Success;
  153. #if !(defined(FSL_FEATURE_ADC_SUPPORT_HARDWARE_TRIGGER_REMOVE) && FSL_FEATURE_ADC_SUPPORT_HARDWARE_TRIGGER_REMOVE)
  154. bool bHWTrigger = false;
  155. /* The calibration would be failed when in hardwar mode.
  156. * Remember the hardware trigger state here and restore it later if the hardware trigger is enabled.*/
  157. if (0U != (ADC_CFG_ADTRG_MASK & base->CFG))
  158. {
  159. bHWTrigger = true;
  160. ADC_EnableHardwareTrigger(base, false);
  161. }
  162. #endif
  163. /* Clear the CALF and launch the calibration. */
  164. base->GS = ADC_GS_CALF_MASK; /* Clear the CALF. */
  165. base->GC |= ADC_GC_CAL_MASK; /* Launch the calibration. */
  166. /* Check the status of CALF bit in ADC_GS and the CAL bit in ADC_GC. */
  167. while (0U != (base->GC & ADC_GC_CAL_MASK))
  168. {
  169. /* Check the CALF when the calibration is active. */
  170. if (0U != (ADC_GetStatusFlags(base) & kADC_CalibrationFailedFlag))
  171. {
  172. status = kStatus_Fail;
  173. break;
  174. }
  175. }
  176. /* When CAL bit becomes '0' then check the CALF status and COCO[0] bit status. */
  177. if (0U == ADC_GetChannelStatusFlags(base, 0U)) /* Check the COCO[0] bit status. */
  178. {
  179. status = kStatus_Fail;
  180. }
  181. if (0U != (ADC_GetStatusFlags(base) & kADC_CalibrationFailedFlag)) /* Check the CALF status. */
  182. {
  183. status = kStatus_Fail;
  184. }
  185. /* Clear conversion done flag. */
  186. ADC_GetChannelConversionValue(base, 0U);
  187. #if !(defined(FSL_FEATURE_ADC_SUPPORT_HARDWARE_TRIGGER_REMOVE) && FSL_FEATURE_ADC_SUPPORT_HARDWARE_TRIGGER_REMOVE)
  188. /* Restore original trigger mode. */
  189. if (true == bHWTrigger)
  190. {
  191. ADC_EnableHardwareTrigger(base, true);
  192. }
  193. #endif
  194. return status;
  195. }
  196. void ADC_SetOffsetConfig(ADC_Type *base, const adc_offest_config_t *config)
  197. {
  198. assert(NULL != config);
  199. uint32_t tmp32;
  200. tmp32 = ADC_OFS_OFS(config->offsetValue);
  201. if (config->enableSigned)
  202. {
  203. tmp32 |= ADC_OFS_SIGN_MASK;
  204. }
  205. base->OFS = tmp32;
  206. }
  207. void ADC_SetHardwareCompareConfig(ADC_Type *base, const adc_hardware_compare_config_t *config)
  208. {
  209. uint32_t tmp32;
  210. tmp32 = base->GC & ~(ADC_GC_ACFE_MASK | ADC_GC_ACFGT_MASK | ADC_GC_ACREN_MASK);
  211. if (NULL == config) /* Pass "NULL" to disable the feature. */
  212. {
  213. base->GC = tmp32;
  214. return;
  215. }
  216. /* Enable the feature. */
  217. tmp32 |= ADC_GC_ACFE_MASK;
  218. /* Select the hardware compare working mode. */
  219. switch (config->hardwareCompareMode)
  220. {
  221. case kADC_HardwareCompareMode0:
  222. break;
  223. case kADC_HardwareCompareMode1:
  224. tmp32 |= ADC_GC_ACFGT_MASK;
  225. break;
  226. case kADC_HardwareCompareMode2:
  227. tmp32 |= ADC_GC_ACREN_MASK;
  228. break;
  229. case kADC_HardwareCompareMode3:
  230. tmp32 |= ADC_GC_ACFGT_MASK | ADC_GC_ACREN_MASK;
  231. break;
  232. default:
  233. break;
  234. }
  235. base->GC = tmp32;
  236. /* Load the compare values. */
  237. tmp32 = ADC_CV_CV1(config->value1) | ADC_CV_CV2(config->value2);
  238. base->CV = tmp32;
  239. }
  240. void ADC_SetHardwareAverageConfig(ADC_Type *base, adc_hardware_average_mode_t mode)
  241. {
  242. uint32_t tmp32;
  243. if (mode == kADC_HardwareAverageDiasable)
  244. {
  245. base->GC &= ~ADC_GC_AVGE_MASK;
  246. }
  247. else
  248. {
  249. tmp32 = base->CFG & ~ADC_CFG_AVGS_MASK;
  250. tmp32 |= ADC_CFG_AVGS(mode);
  251. base->CFG = tmp32;
  252. base->GC |= ADC_GC_AVGE_MASK; /* Enable the hardware compare. */
  253. }
  254. }
  255. void ADC_ClearStatusFlags(ADC_Type *base, uint32_t mask)
  256. {
  257. uint32_t tmp32 = 0;
  258. if (0U != (mask & kADC_CalibrationFailedFlag))
  259. {
  260. tmp32 |= ADC_GS_CALF_MASK;
  261. }
  262. if (0U != (mask & kADC_ConversionActiveFlag))
  263. {
  264. tmp32 |= ADC_GS_ADACT_MASK;
  265. }
  266. base->GS = tmp32;
  267. }