fsl_adc16.c 12 KB

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