fsl_adc16.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2015, 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_adc16.h"
  35. /*******************************************************************************
  36. * Prototypes
  37. ******************************************************************************/
  38. /*!
  39. * @brief Get instance number for ADC16 module.
  40. *
  41. * @param base ADC16 peripheral base address
  42. */
  43. static uint32_t ADC16_GetInstance(ADC_Type *base);
  44. /*******************************************************************************
  45. * Variables
  46. ******************************************************************************/
  47. /*! @brief Pointers to ADC16 bases for each instance. */
  48. static ADC_Type *const s_adc16Bases[] = ADC_BASE_PTRS;
  49. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  50. /*! @brief Pointers to ADC16 clocks for each instance. */
  51. static const clock_ip_name_t s_adc16Clocks[] = ADC16_CLOCKS;
  52. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  53. /*******************************************************************************
  54. * Code
  55. ******************************************************************************/
  56. static uint32_t ADC16_GetInstance(ADC_Type *base)
  57. {
  58. uint32_t instance;
  59. /* Find the instance index from base address mappings. */
  60. for (instance = 0; instance < ARRAY_SIZE(s_adc16Bases); instance++)
  61. {
  62. if (s_adc16Bases[instance] == base)
  63. {
  64. break;
  65. }
  66. }
  67. assert(instance < ARRAY_SIZE(s_adc16Bases));
  68. return instance;
  69. }
  70. void ADC16_Init(ADC_Type *base, const adc16_config_t *config)
  71. {
  72. assert(NULL != config);
  73. uint32_t tmp32;
  74. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  75. /* Enable the clock. */
  76. CLOCK_EnableClock(s_adc16Clocks[ADC16_GetInstance(base)]);
  77. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  78. /* ADCx_CFG1. */
  79. tmp32 = ADC_CFG1_ADICLK(config->clockSource) | ADC_CFG1_MODE(config->resolution);
  80. if (kADC16_LongSampleDisabled != config->longSampleMode)
  81. {
  82. tmp32 |= ADC_CFG1_ADLSMP_MASK;
  83. }
  84. tmp32 |= ADC_CFG1_ADIV(config->clockDivider);
  85. if (config->enableLowPower)
  86. {
  87. tmp32 |= ADC_CFG1_ADLPC_MASK;
  88. }
  89. base->CFG1 = tmp32;
  90. /* ADCx_CFG2. */
  91. tmp32 = base->CFG2 & ~(ADC_CFG2_ADACKEN_MASK | ADC_CFG2_ADHSC_MASK | ADC_CFG2_ADLSTS_MASK);
  92. if (kADC16_LongSampleDisabled != config->longSampleMode)
  93. {
  94. tmp32 |= ADC_CFG2_ADLSTS(config->longSampleMode);
  95. }
  96. if (config->enableHighSpeed)
  97. {
  98. tmp32 |= ADC_CFG2_ADHSC_MASK;
  99. }
  100. if (config->enableAsynchronousClock)
  101. {
  102. tmp32 |= ADC_CFG2_ADACKEN_MASK;
  103. }
  104. base->CFG2 = tmp32;
  105. /* ADCx_SC2. */
  106. tmp32 = base->SC2 & ~(ADC_SC2_REFSEL_MASK);
  107. tmp32 |= ADC_SC2_REFSEL(config->referenceVoltageSource);
  108. base->SC2 = tmp32;
  109. /* ADCx_SC3. */
  110. if (config->enableContinuousConversion)
  111. {
  112. base->SC3 |= ADC_SC3_ADCO_MASK;
  113. }
  114. else
  115. {
  116. base->SC3 &= ~ADC_SC3_ADCO_MASK;
  117. }
  118. }
  119. void ADC16_Deinit(ADC_Type *base)
  120. {
  121. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  122. /* Disable the clock. */
  123. CLOCK_DisableClock(s_adc16Clocks[ADC16_GetInstance(base)]);
  124. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  125. }
  126. void ADC16_GetDefaultConfig(adc16_config_t *config)
  127. {
  128. assert(NULL != config);
  129. config->referenceVoltageSource = kADC16_ReferenceVoltageSourceVref;
  130. config->clockSource = kADC16_ClockSourceAsynchronousClock;
  131. config->enableAsynchronousClock = true;
  132. config->clockDivider = kADC16_ClockDivider8;
  133. config->resolution = kADC16_ResolutionSE12Bit;
  134. config->longSampleMode = kADC16_LongSampleDisabled;
  135. config->enableHighSpeed = false;
  136. config->enableLowPower = false;
  137. config->enableContinuousConversion = false;
  138. }
  139. #if defined(FSL_FEATURE_ADC16_HAS_CALIBRATION) && FSL_FEATURE_ADC16_HAS_CALIBRATION
  140. status_t ADC16_DoAutoCalibration(ADC_Type *base)
  141. {
  142. bool bHWTrigger = false;
  143. volatile uint32_t tmp32; /* 'volatile' here is for the dummy read of ADCx_R[0] register. */
  144. status_t status = kStatus_Success;
  145. /* The calibration would be failed when in hardwar mode.
  146. * Remember the hardware trigger state here and restore it later if the hardware trigger is enabled.*/
  147. if (0U != (ADC_SC2_ADTRG_MASK & base->SC2))
  148. {
  149. bHWTrigger = true;
  150. base->SC2 &= ~ADC_SC2_ADTRG_MASK;
  151. }
  152. /* Clear the CALF and launch the calibration. */
  153. base->SC3 |= ADC_SC3_CAL_MASK | ADC_SC3_CALF_MASK;
  154. while (0U == (kADC16_ChannelConversionDoneFlag & ADC16_GetChannelStatusFlags(base, 0U)))
  155. {
  156. /* Check the CALF when the calibration is active. */
  157. if (0U != (kADC16_CalibrationFailedFlag & ADC16_GetStatusFlags(base)))
  158. {
  159. status = kStatus_Fail;
  160. break;
  161. }
  162. }
  163. tmp32 = base->R[0]; /* Dummy read to clear COCO caused by calibration. */
  164. /* Restore the hardware trigger setting if it was enabled before. */
  165. if (bHWTrigger)
  166. {
  167. base->SC2 |= ADC_SC2_ADTRG_MASK;
  168. }
  169. /* Check the CALF at the end of calibration. */
  170. if (0U != (kADC16_CalibrationFailedFlag & ADC16_GetStatusFlags(base)))
  171. {
  172. status = kStatus_Fail;
  173. }
  174. if (kStatus_Success != status) /* Check if the calibration process is succeed. */
  175. {
  176. return status;
  177. }
  178. /* Calculate the calibration values. */
  179. tmp32 = base->CLP0 + base->CLP1 + base->CLP2 + base->CLP3 + base->CLP4 + base->CLPS;
  180. tmp32 = 0x8000U | (tmp32 >> 1U);
  181. base->PG = tmp32;
  182. #if defined(FSL_FEATURE_ADC16_HAS_DIFF_MODE) && FSL_FEATURE_ADC16_HAS_DIFF_MODE
  183. tmp32 = base->CLM0 + base->CLM1 + base->CLM2 + base->CLM3 + base->CLM4 + base->CLMS;
  184. tmp32 = 0x8000U | (tmp32 >> 1U);
  185. base->MG = tmp32;
  186. #endif /* FSL_FEATURE_ADC16_HAS_DIFF_MODE */
  187. return kStatus_Success;
  188. }
  189. #endif /* FSL_FEATURE_ADC16_HAS_CALIBRATION */
  190. #if defined(FSL_FEATURE_ADC16_HAS_MUX_SELECT) && FSL_FEATURE_ADC16_HAS_MUX_SELECT
  191. void ADC16_SetChannelMuxMode(ADC_Type *base, adc16_channel_mux_mode_t mode)
  192. {
  193. if (kADC16_ChannelMuxA == mode)
  194. {
  195. base->CFG2 &= ~ADC_CFG2_MUXSEL_MASK;
  196. }
  197. else /* kADC16_ChannelMuxB. */
  198. {
  199. base->CFG2 |= ADC_CFG2_MUXSEL_MASK;
  200. }
  201. }
  202. #endif /* FSL_FEATURE_ADC16_HAS_MUX_SELECT */
  203. void ADC16_SetHardwareCompareConfig(ADC_Type *base, const adc16_hardware_compare_config_t *config)
  204. {
  205. uint32_t tmp32 = base->SC2 & ~(ADC_SC2_ACFE_MASK | ADC_SC2_ACFGT_MASK | ADC_SC2_ACREN_MASK);
  206. if (!config) /* Pass "NULL" to disable the feature. */
  207. {
  208. base->SC2 = tmp32;
  209. return;
  210. }
  211. /* Enable the feature. */
  212. tmp32 |= ADC_SC2_ACFE_MASK;
  213. /* Select the hardware compare working mode. */
  214. switch (config->hardwareCompareMode)
  215. {
  216. case kADC16_HardwareCompareMode0:
  217. break;
  218. case kADC16_HardwareCompareMode1:
  219. tmp32 |= ADC_SC2_ACFGT_MASK;
  220. break;
  221. case kADC16_HardwareCompareMode2:
  222. tmp32 |= ADC_SC2_ACREN_MASK;
  223. break;
  224. case kADC16_HardwareCompareMode3:
  225. tmp32 |= ADC_SC2_ACFGT_MASK | ADC_SC2_ACREN_MASK;
  226. break;
  227. default:
  228. break;
  229. }
  230. base->SC2 = tmp32;
  231. /* Load the compare values. */
  232. base->CV1 = ADC_CV1_CV(config->value1);
  233. base->CV2 = ADC_CV2_CV(config->value2);
  234. }
  235. #if defined(FSL_FEATURE_ADC16_HAS_HW_AVERAGE) && FSL_FEATURE_ADC16_HAS_HW_AVERAGE
  236. void ADC16_SetHardwareAverage(ADC_Type *base, adc16_hardware_average_mode_t mode)
  237. {
  238. uint32_t tmp32 = base->SC3 & ~(ADC_SC3_AVGE_MASK | ADC_SC3_AVGS_MASK);
  239. if (kADC16_HardwareAverageDisabled != mode)
  240. {
  241. tmp32 |= ADC_SC3_AVGE_MASK | ADC_SC3_AVGS(mode);
  242. }
  243. base->SC3 = tmp32;
  244. }
  245. #endif /* FSL_FEATURE_ADC16_HAS_HW_AVERAGE */
  246. #if defined(FSL_FEATURE_ADC16_HAS_PGA) && FSL_FEATURE_ADC16_HAS_PGA
  247. void ADC16_SetPGAConfig(ADC_Type *base, const adc16_pga_config_t *config)
  248. {
  249. uint32_t tmp32;
  250. if (!config) /* Passing "NULL" is to disable the feature. */
  251. {
  252. base->PGA = 0U;
  253. return;
  254. }
  255. /* Enable the PGA and set the gain value. */
  256. tmp32 = ADC_PGA_PGAEN_MASK | ADC_PGA_PGAG(config->pgaGain);
  257. /* Configure the misc features for PGA. */
  258. if (config->enableRunInNormalMode)
  259. {
  260. tmp32 |= ADC_PGA_PGALPb_MASK;
  261. }
  262. #if defined(FSL_FEATURE_ADC16_HAS_PGA_CHOPPING) && FSL_FEATURE_ADC16_HAS_PGA_CHOPPING
  263. if (config->disablePgaChopping)
  264. {
  265. tmp32 |= ADC_PGA_PGACHPb_MASK;
  266. }
  267. #endif /* FSL_FEATURE_ADC16_HAS_PGA_CHOPPING */
  268. #if defined(FSL_FEATURE_ADC16_HAS_PGA_OFFSET_MEASUREMENT) && FSL_FEATURE_ADC16_HAS_PGA_OFFSET_MEASUREMENT
  269. if (config->enableRunInOffsetMeasurement)
  270. {
  271. tmp32 |= ADC_PGA_PGAOFSM_MASK;
  272. }
  273. #endif /* FSL_FEATURE_ADC16_HAS_PGA_OFFSET_MEASUREMENT */
  274. base->PGA = tmp32;
  275. }
  276. #endif /* FSL_FEATURE_ADC16_HAS_PGA */
  277. uint32_t ADC16_GetStatusFlags(ADC_Type *base)
  278. {
  279. uint32_t ret = 0;
  280. if (0U != (base->SC2 & ADC_SC2_ADACT_MASK))
  281. {
  282. ret |= kADC16_ActiveFlag;
  283. }
  284. #if defined(FSL_FEATURE_ADC16_HAS_CALIBRATION) && FSL_FEATURE_ADC16_HAS_CALIBRATION
  285. if (0U != (base->SC3 & ADC_SC3_CALF_MASK))
  286. {
  287. ret |= kADC16_CalibrationFailedFlag;
  288. }
  289. #endif /* FSL_FEATURE_ADC16_HAS_CALIBRATION */
  290. return ret;
  291. }
  292. void ADC16_ClearStatusFlags(ADC_Type *base, uint32_t mask)
  293. {
  294. #if defined(FSL_FEATURE_ADC16_HAS_CALIBRATION) && FSL_FEATURE_ADC16_HAS_CALIBRATION
  295. if (0U != (mask & kADC16_CalibrationFailedFlag))
  296. {
  297. base->SC3 |= ADC_SC3_CALF_MASK;
  298. }
  299. #endif /* FSL_FEATURE_ADC16_HAS_CALIBRATION */
  300. }
  301. void ADC16_SetChannelConfig(ADC_Type *base, uint32_t channelGroup, const adc16_channel_config_t *config)
  302. {
  303. assert(channelGroup < ADC_SC1_COUNT);
  304. assert(NULL != config);
  305. uint32_t sc1 = ADC_SC1_ADCH(config->channelNumber); /* Set the channel number. */
  306. #if defined(FSL_FEATURE_ADC16_HAS_DIFF_MODE) && FSL_FEATURE_ADC16_HAS_DIFF_MODE
  307. /* Enable the differential conversion. */
  308. if (config->enableDifferentialConversion)
  309. {
  310. sc1 |= ADC_SC1_DIFF_MASK;
  311. }
  312. #endif /* FSL_FEATURE_ADC16_HAS_DIFF_MODE */
  313. /* Enable the interrupt when the conversion is done. */
  314. if (config->enableInterruptOnConversionCompleted)
  315. {
  316. sc1 |= ADC_SC1_AIEN_MASK;
  317. }
  318. base->SC1[channelGroup] = sc1;
  319. }
  320. uint32_t ADC16_GetChannelStatusFlags(ADC_Type *base, uint32_t channelGroup)
  321. {
  322. assert(channelGroup < ADC_SC1_COUNT);
  323. uint32_t ret = 0U;
  324. if (0U != (base->SC1[channelGroup] & ADC_SC1_COCO_MASK))
  325. {
  326. ret |= kADC16_ChannelConversionDoneFlag;
  327. }
  328. return ret;
  329. }