adc_driver.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2018 NXP
4  * All rights reserved.
5  *
6  * THIS SOFTWARE IS PROVIDED BY NXP "AS IS" AND ANY EXPRESSED OR
7  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
9  * IN NO EVENT SHALL NXP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
10  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
11  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
12  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
13  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
14  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
15  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
16  * THE POSSIBILITY OF SUCH DAMAGE.
17  */
42 #include <stddef.h>
43 #include "adc_driver.h"
44 #include "adc_hw_access.h"
45 #include "clock_manager.h"
46 
47 
48 /*******************************************************************************
49  * Variables
50  ******************************************************************************/
51 
52 /* Table of base addresses for ADC instances. */
54 
55 /*FUNCTION**********************************************************************
56  *
57  * Function Name : ADC_DRV_InitConverterStruct
58  * Description : This function initializes the members of the adc_converter_config_t
59  * structure to default values (Reference Manual resets). This function should be called
60  * on a structure before using it to configure the converter (ADC_DRV_ConfigConverter), otherwise all members
61  * must be written (initialized) by the caller. This function insures that all members are written
62  * with safe values, so the user can modify only the desired members.
63  *
64  * Implements : ADC_DRV_InitConverterStruct_Activity
65  *END**************************************************************************/
67 {
68  DEV_ASSERT(config != NULL);
69 
70  config->clockDivide = ADC_CLK_DIVIDE_1;
71  config->sampleTime = (uint8_t)ADC_DEFAULT_SAMPLE_TIME;
73  config->inputClock = ADC_CLK_ALT_1;
74  config->trigger = ADC_TRIGGER_SOFTWARE;
77  config->dmaEnable = false;
79  config->continuousConvEnable = false;
80  config->supplyMonitoringEnable = false;
81 }
82 
83 /*FUNCTION**********************************************************************
84  *
85  * Function Name : ADC_DRV_ConfigConverter
86  * Description : This function configures the ADC converter with the options
87  * provided in the configuration structure.
88  *
89  * Implements : ADC_DRV_ConfigConverter_Activity
90  *END**************************************************************************/
91 void ADC_DRV_ConfigConverter(const uint32_t instance,
92  const adc_converter_config_t * const config)
93 {
94  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
95  DEV_ASSERT(config != NULL);
96  /* Some alternative clocks can be unavailable depending on the device */
98 
99  ADC_Type * const base = s_adcBase[instance];
101  uint32_t adc_freq = 0u;
102  status_t clk_status = CLOCK_SYS_GetFreq(adc_clocks[instance], &adc_freq);
103  DEV_ASSERT(clk_status == STATUS_SUCCESS);
104  (void) clk_status;
105 
106  adc_freq = adc_freq / (uint32_t)(1UL << ((uint32_t)(config->clockDivide)));
107  DEV_ASSERT((adc_freq >= ADC_CLOCK_FREQ_MIN_RUNTIME) && (adc_freq <= ADC_CLOCK_FREQ_MAX_RUNTIME));
108 
109  ADC_SetClockDivide(base, config->clockDivide);
110  ADC_SetSampleTime(base, config->sampleTime);
111  ADC_SetResolution(base, config->resolution);
112  ADC_SetInputClock(base, config->inputClock);
113  ADC_SetTriggerMode(base, config->trigger);
114  ADC_SetPretriggerSelect(instance, config->pretriggerSel);
115  ADC_SetTriggerSelect(instance, config->triggerSel);
116  ADC_SetDMAEnableFlag(base, config->dmaEnable);
117  ADC_SetVoltageReference(base, config->voltageRef);
118  ADC_SetContinuousConvFlag(base, config->continuousConvEnable);
119 
120  /* Supply monitoring is only available for ADC 0. */
121  DEV_ASSERT((config->supplyMonitoringEnable == false) || (instance == 0u));
122  if(instance == 0u)
123  {
124  SIM_Type * const simBase = SIM;
125  ADC_SetSupplyMonitoringEnableFlag(simBase, config->supplyMonitoringEnable);
126  }
127 }
128 
129 /*FUNCTION**********************************************************************
130  *
131  * Function Name : ADC_DRV_GetConverterConfig
132  * Description : This functions returns the current converter configuration in
133  * the form of a configuration structure.
134  *
135  * Implements : ADC_DRV_GetConverterConfig_Activity
136  *END**************************************************************************/
137 void ADC_DRV_GetConverterConfig(const uint32_t instance,
138  adc_converter_config_t * const config)
139 {
140  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
141  DEV_ASSERT(config != NULL);
142 
143  const ADC_Type * const base = s_adcBase[instance];
144  config->clockDivide = ADC_GetClockDivide(base);
145  config->sampleTime = ADC_GetSampleTime(base);
146  config->resolution = ADC_GetResolution(base);
147  config->inputClock = ADC_GetInputClock(base);
148  config->trigger = ADC_GetTriggerMode(base);
149  config->triggerSel = ADC_GetTriggerSelect(instance);
150  config->pretriggerSel = ADC_GetPretriggerSelect(instance);
151  config->dmaEnable = ADC_GetDMAEnableFlag(base);
152  config->voltageRef = ADC_GetVoltageReference(base);
153  config->continuousConvEnable = ADC_GetContinuousConvFlag(base);
154 
155  /* Supply monitoring is only available for ADC 0. */
156  if(instance == 0u)
157  {
158  const SIM_Type * const simBase = SIM;
159  config->supplyMonitoringEnable = ((simBase->CHIPCTL & SIM_CHIPCTL_ADC_SUPPLYEN_MASK) != 0u) ? true : false;
160  }
161  else
162  {
163  config->supplyMonitoringEnable = false;
164  }
165 }
166 
167 /*FUNCTION**********************************************************************
168  *
169  * Function Name : ADC_DRV_Reset
170  * Description : This function writes all the internal ADC registers with
171  * their Reference Manual reset values.
172  *
173  * Implements : ADC_DRV_Reset_Activity
174  *END**************************************************************************/
175 void ADC_DRV_Reset(const uint32_t instance)
176 {
177  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
178 
179  ADC_Type * const baseAddr = s_adcBase[instance];
180  uint8_t idx = 0U;
181 
182  for(idx = 0U; idx < ADC_SC1_COUNT; idx++)
183  {
184  baseAddr->SC1[idx] = ADC_SC1_ADCH(ADC_INPUTCHAN_DISABLED) | ADC_SC1_AIEN(0x00U);
185  }
186 
189 
190  for(idx = 0U; idx < ADC_CV_COUNT; idx++)
191  {
192  baseAddr->CV[idx] = ADC_CV_CV(0U);
193  }
194 
195  baseAddr->SC2 = ADC_SC2_REFSEL(ADC_VOLTAGEREF_VREF) | ADC_SC2_DMAEN(0x00U) | ADC_SC2_ACREN(0x00U) | ADC_SC2_ACFGT(0x00U) | ADC_SC2_ACFE(0x00U) |
196  ADC_SC2_ADTRG(0x00U);
197  baseAddr->SC3 = ADC_SC3_AVGS(ADC_AVERAGE_4) | ADC_SC3_AVGE(0x00U) | ADC_SC3_ADCO(0x00U) | ADC_SC3_CAL(0x00U);
198  baseAddr->USR_OFS = ADC_USR_OFS_USR_OFS(0U);
199  baseAddr->UG = ADC_UG_UG(ADC_DEFAULT_USER_GAIN);
200 
201 #if FEATURE_ADC_HAS_EXTRA_NUM_REGS
202  for(idx = 0U; idx < ADC_aSC1_COUNT; idx++)
203  {
204  baseAddr->aSC1[idx] = ADC_aSC1_ADCH(ADC_INPUTCHAN_DISABLED) | ADC_aSC1_AIEN(0x00U);
205  }
206 #endif /* FEATURE_ADC_HAS_EXTRA_NUM_REGS */
207 
208  ADC_SetPretriggerSelect(instance, ADC_PRETRIGGER_SEL_PDB);
209  ADC_SetTriggerSelect(instance, ADC_TRIGGER_SEL_PDB);
211 
212  /* Reset ADC Supply Monitoring - available only for ADC 0 */
213  if(instance == 0u)
214  {
215  SIM_Type * const simBase = SIM;
216  ADC_SetSupplyMonitoringEnableFlag(simBase, false);
217 
219  }
220 }
221 
222 /*FUNCTION**********************************************************************
223  *
224  * Function Name : ADC_DRV_InitHwCompareStruct
225  * Description : This function initializes the Hardware Compare configuration
226  * structure to default values (Reference Manual resets). This function should be
227  * called before configuring the Hardware Compare feature (ADC_DRV_ConfigHwCompare),
228  * otherwise all members must be written by the caller. This function insures that all
229  * members are written with safe values, so the user can modify the desired members.
230  *
231  * Implements : ADC_DRV_InitHwCompareStruct_Activity
232  *END**************************************************************************/
234 {
235  DEV_ASSERT(config != NULL);
236 
237  config->compareEnable = false;
238  config->compareGreaterThanEnable = false;
239  config->compareRangeFuncEnable = false;
240  config->compVal1 = 0U;
241  config->compVal2 = 0U;
242 }
243 
244 /*FUNCTION**********************************************************************
245  *
246  * Function Name : ADC_DRV_ConfigHwCompare
247  * Description : This functions sets the configuration for the Hardware
248  * Compare feature using the configuration structure.
249  *
250  * Implements : ADC_DRV_ConfigHwCompare_Activity
251  *END**************************************************************************/
252 void ADC_DRV_ConfigHwCompare(const uint32_t instance,
253  const adc_compare_config_t * const config)
254 {
255  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
256  DEV_ASSERT(config != NULL);
257 
258  ADC_Type * const base = s_adcBase[instance];
259  ADC_SetHwCompareEnableFlag(base, config->compareEnable);
260  ADC_SetHwCompareGtEnableFlag(base, config->compareGreaterThanEnable);
261  ADC_SetHwCompareRangeEnableFlag(base, config->compareRangeFuncEnable);
262  ADC_SetHwCompareComp1Value(base, config->compVal1);
263  ADC_SetHwCompareComp2Value(base, config->compVal2);
264 }
265 
266 /*FUNCTION**********************************************************************
267  *
268  * Function Name : ADC_DRV_GetHwCompareConfig
269  * Description : This function returns the configuration for the Hardware
270  * Compare feature.
271  *
272  * Implements : ADC_DRV_GetHwCompareConfig_Activity
273  *END**************************************************************************/
274 void ADC_DRV_GetHwCompareConfig(const uint32_t instance,
275  adc_compare_config_t * const config)
276 {
277  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
278  DEV_ASSERT(config != NULL);
279 
280  const ADC_Type * const base = s_adcBase[instance];
281  config->compareEnable = ADC_GetHwCompareEnableFlag(base);
282  config->compareGreaterThanEnable = ADC_GetHwCompareGtEnableFlag(base);
283  config->compareRangeFuncEnable = ADC_GetHwCompareRangeEnableFlag(base);
284  config->compVal1 = ADC_GetHwCompareComp1Value(base);
285  config->compVal2 = ADC_GetHwCompareComp2Value(base);
286 }
287 
288 /*FUNCTION**********************************************************************
289  *
290  * Function Name : ADC_DRV_InitHwAverageStruct
291  * Description : This function initializes the Hardware Average configuration
292  * structure to default values (Reference Manual resets). This function should be
293  * called before configuring the Hardware Average feature (ADC_DRV_ConfigHwAverage),
294  * otherwise all members must be written by the caller. This function insures that all
295  * members are written with safe values, so the user can modify the desired members.
296  *
297  * Implements : ADC_DRV_InitHwAverageStruct_Activity
298  *END**************************************************************************/
300 {
301  DEV_ASSERT(config != NULL);
302 
303  config->hwAvgEnable = false;
304  config->hwAverage = ADC_AVERAGE_4;
305 }
306 
307 /*FUNCTION**********************************************************************
308  *
309  * Function Name : ADC_DRV_ConfigHwAverage
310  * Description : This function sets the configuration for the Hardware
311  * Average feature.
312  *
313  * Implements : ADC_DRV_ConfigHwAverage_Activity
314  *END**************************************************************************/
315 void ADC_DRV_ConfigHwAverage(const uint32_t instance,
316  const adc_average_config_t * const config)
317 {
318  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
319  DEV_ASSERT(config != NULL);
320 
321  ADC_Type * const base = s_adcBase[instance];
322  ADC_SetHwAverageEnableFlag(base, config->hwAvgEnable);
323  ADC_SetHwAverageMode(base, config->hwAverage);
324 }
325 
326 /*FUNCTION**********************************************************************
327  *
328  * Function Name : ADC_DRV_GetHwAverageConfig
329  * Description : This function returns the configuration for the Hardware
330  * Average feature.
331  *
332  * Implements : ADC_DRV_GetHwAverageConfig_Activity
333  *END**************************************************************************/
334 void ADC_DRV_GetHwAverageConfig(const uint32_t instance,
335  adc_average_config_t * const config)
336 {
337  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
338  DEV_ASSERT(config != NULL);
339 
340  const ADC_Type * const base = s_adcBase[instance];
341  config->hwAvgEnable = ADC_GetHwAverageEnableFlag(base);
342  config->hwAverage = ADC_GetHwAverageMode(base);
343 }
344 
345 /*FUNCTION**********************************************************************
346  *
347  * Function Name : ADC_DRV_InitChanStruct
348  * Description : This function initializes the control channel
349  * configuration structure to default values (Reference Manual resets). This function should
350  * be called on a structure before using it to configure a channel (ADC_DRV_ConfigChan), otherwise
351  * all members must be written by the caller. This function insures that all members are written
352  * with safe values, so the user can modify only the desired members.
353  *
354  * Implements : ADC_DRV_InitChanStruct_Activity
355  *END**************************************************************************/
357 {
358  DEV_ASSERT(config != NULL);
359 
360  config->interruptEnable = false;
362 }
363 
364 /*FUNCTION**********************************************************************
365  *
366  * Function Name : ADC_DRV_ConfigChan
367  * Description : This function sets a control channel configuration.
368  *
369  * When Software Trigger mode is enabled, configuring control channel index 0,
370  * implicitly triggers a new conversion on the selected ADC input channel.
371  * Therefore, ADC_DRV_ConfigChan can be used for sw-triggering conversions.
372  *
373  * Configuring any control channel while it is actively controlling a conversion
374  * (sw or hw triggered) will implicitly abort the on-going conversion.
375  *
376  * Implements : ADC_DRV_ConfigChan_Activity
377  *END**************************************************************************/
378 void ADC_DRV_ConfigChan(const uint32_t instance,
379  const uint8_t chanIndex,
380  const adc_chan_config_t * const config)
381 {
382  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
383  DEV_ASSERT(chanIndex < ADC_CTRL_CHANS_COUNT);
384  DEV_ASSERT(config != NULL);
385 
386  ADC_Type * const base = s_adcBase[instance];
387 
388  /* ADC_INPUTCHAN_SUPPLY_ can only be used with ADC 0,
389  * If used, the feature must be enabled separately via supplyMonitoringEnable flag in adc_converter_config_t. */
390  DEV_ASSERT((instance == 0u) || ((uint32_t)config->channel < (uint32_t)ADC_INPUTCHAN_SUPPLY_VDD) || \
391  ((uint32_t)config->channel > (uint32_t)ADC_INPUTCHAN_SUPPLY_VDD_LV));
392  ADC_SetInputChannel(base, chanIndex, config->channel, config->interruptEnable);
393 }
394 
395 /*FUNCTION**********************************************************************
396  *
397  * Function Name : ADC_DRV_GetChanConfig
398  * Description : This function returns the current configuration for a control
399  * channel.
400  *
401  * Implements : ADC_DRV_GetChanConfig_Activity
402  *END**************************************************************************/
403 void ADC_DRV_GetChanConfig(const uint32_t instance,
404  const uint8_t chanIndex,
405  adc_chan_config_t * const config)
406 {
407  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
408  DEV_ASSERT(chanIndex < ADC_CTRL_CHANS_COUNT);
409  DEV_ASSERT(config != NULL);
410 
411  const ADC_Type * const base = s_adcBase[instance];
412  config->interruptEnable = ADC_GetChanInterruptEnableFlag(base, chanIndex);
413  config->channel = ADC_GetInputChannel(base, chanIndex);
414 }
415 
416 /*FUNCTION**********************************************************************
417  *
418  * Function Name : ADC_DRV_SetSwPretrigger
419  * Description : This function sets the software pretrigger - affects only first 4 control channels.
420  *
421  * Implements : ADC_DRV_SetSwPretrigger_Activity
422  *END**************************************************************************/
423 void ADC_DRV_SetSwPretrigger(const uint32_t instance,
424  const adc_sw_pretrigger_t swPretrigger)
425 {
426  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
427  SIM_Type * const simBase = SIM;
428  uint32_t intermValue = 0U;
429 #if (ADC_INSTANCE_COUNT == 1U)
430  const uint32_t mask[ADC_INSTANCE_COUNT] = {SIM_ADCOPT_ADC0SWPRETRG_MASK};
431 #elif (ADC_INSTANCE_COUNT == 2U)
433 #endif
434  /* If SW Pretrigger Select is not enabled, the SW pretriggers will be ignored by ADC. */
435  DEV_ASSERT((ADC_GetPretriggerSelect(instance) == ADC_PRETRIGGER_SEL_SW) || \
436  (swPretrigger == ADC_SW_PRETRIGGER_DISABLED));
437 
438  intermValue = simBase->ADCOPT & (~ mask[instance]);
439  switch(instance)
440  {
441  case 0:
442  intermValue |= SIM_ADCOPT_ADC0SWPRETRG(swPretrigger);
443  break;
444  case 1:
445  intermValue |= SIM_ADCOPT_ADC1SWPRETRG(swPretrigger);
446  break;
447  default:
448  DEV_ASSERT(false);
449  break;
450  }
451 
452  simBase->ADCOPT = intermValue;
453 }
454 
455 /*FUNCTION**********************************************************************
456  *
457  * Function Name : ADC_DRV_WaitConvDone
458  * Description : This functions waits for a conversion to complete by
459  * continuously polling the Conversion Active Flag.
460  *
461  * Implements : ADC_DRV_WaitConvDone_Activity
462  *END**************************************************************************/
463 void ADC_DRV_WaitConvDone(const uint32_t instance)
464 {
465  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
466 
467  const ADC_Type * const base = s_adcBase[instance];
468  while (ADC_GetConvActiveFlag(base) == true)
469  {
470  /* Wait for conversion to finish */
471  }
472 }
473 
474 /*FUNCTION**********************************************************************
475  *
476  * Function Name : ADC_DRV_GetConvCompleteFlag
477  * Description : This function returns the state of the Conversion Complete
478  * flag for a control channel. This flag is set when a conversion is complete
479  * or the condition generated by the Hardware Compare feature is evaluated to true.
480  *
481  * Implements : ADC_DRV_GetConvCompleteFlag_Activity
482  *END**************************************************************************/
483 bool ADC_DRV_GetConvCompleteFlag(const uint32_t instance,
484  const uint8_t chanIndex)
485 {
486  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
487  DEV_ASSERT(chanIndex < ADC_CTRL_CHANS_COUNT);
488 
489  const ADC_Type * const base = s_adcBase[instance];
490 
491 #if FEATURE_ADC_HAS_EXTRA_NUM_REGS
492  uint32_t tmp = base->aSC1[chanIndex];
493  tmp = (tmp & ADC_aSC1_COCO_MASK) >> ADC_aSC1_COCO_SHIFT;
494 #else
495  uint32_t tmp = base->SC1[chanIndex];
496  tmp = (tmp & ADC_SC1_COCO_MASK) >> ADC_SC1_COCO_SHIFT;
497 #endif /* FEATURE_ADC_HAS_EXTRA_NUM_REGS */
498 
499  return (tmp != 0u) ? true : false;
500 }
501 
502 /*FUNCTION**********************************************************************
503  *
504  * Function Name : ADC_DRV_GetChanResult
505  * Description : This function returns the conversion result from a
506  * control channel.
507  *
508  * Implements : ADC_DRV_GetChanResult_Activity
509  *END**************************************************************************/
510 void ADC_DRV_GetChanResult(const uint32_t instance,
511  const uint8_t chanIndex,
512  uint16_t * const result)
513 {
514  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
515  DEV_ASSERT(result != NULL);
516 
517  const ADC_Type * const base = s_adcBase[instance];
518 
519 #if FEATURE_ADC_HAS_EXTRA_NUM_REGS
520 
521  DEV_ASSERT(chanIndex < ADC_aR_COUNT);
522 
523  uint32_t tmp = base->aR[chanIndex];
524  tmp = (tmp & ADC_aR_D_MASK) >> ADC_aR_D_SHIFT;
525 #else
526 
527  DEV_ASSERT(chanIndex < ADC_R_COUNT);
528 
529  uint32_t tmp = base->R[chanIndex];
530  tmp = (tmp & ADC_R_D_MASK) >> ADC_R_D_SHIFT;
531 #endif /* FEATURE_ADC_HAS_EXTRA_NUM_REGS */
532 
533  *result = (uint16_t)tmp;
534 }
535 
536 /*FUNCTION**********************************************************************
537  *
538  * Function Name : ADC_DRV_AutoCalibration
539  * Description : This functions executes an Auto-Calibration sequence. It
540  * is recommended to run this sequence before using the ADC converter.
541  * this function will check and reset clock divide based the adc frequency.
542  * an error will be displayed if adc_freq too big
543  * this function will set satisfy clock divide,start calibration.
544  * final this function: restore adc clock divide,hardware average and trigger settings.
545  *
546  * Implements : ADC_DRV_AutoCalibration_Activity
547  *END**************************************************************************/
548 void ADC_DRV_AutoCalibration(const uint32_t instance)
549 {
550  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
551 
552  ADC_Type * const base = s_adcBase[instance];
553  /* set hardware average to maximum and set software trigger*/
554  bool hwavgen = ADC_GetHwAverageEnableFlag(base);
555  adc_average_t hwavg = ADC_GetHwAverageMode(base);
556  adc_trigger_t trig = ADC_GetTriggerMode(base);
557  ADC_SetHwAverageMode(base, ADC_AVERAGE_32);
558  ADC_SetHwAverageEnableFlag(base, true);
559  ADC_SetTriggerMode(base, ADC_TRIGGER_SOFTWARE);
560 
561  base->CLPS = 0x00u;
562  base->CLP3 = 0x00u;
563  base->CLP2 = 0x00u;
564  base->CLP1 = 0x00u;
565  base->CLP0 = 0x00u;
566  base->CLPX = 0x00u;
567  base->CLP9 = 0x00u;
568 
569  /*Set clock divider */
571  uint32_t adc_freq = 0u;
572  adc_clk_divide_t adc_clk_divide_res = ADC_GetClockDivide(base);
573  adc_clk_divide_t adc_clk_divide = ADC_CLK_DIVIDE_1;
574  status_t clk_status = CLOCK_SYS_GetFreq(adc_clocks[instance], &adc_freq);
575  DEV_ASSERT(clk_status == STATUS_SUCCESS);
576  (void) clk_status;
577  if ((adc_freq / (uint32_t)(1UL << ((uint32_t)(adc_clk_divide_res)))) <= (ADC_CLOCK_FREQ_MAX_RUNTIME / 2U))
578  {
579  /* no acttion if adc_freq is satisfy */
580  }
581  else
582  {
583  if (adc_freq <= (ADC_CLOCK_FREQ_MAX_RUNTIME / 2U))
584  {
585  adc_clk_divide = ADC_CLK_DIVIDE_1;
586  }
587  else if ((adc_freq / 2U) <= (ADC_CLOCK_FREQ_MAX_RUNTIME / 2U))
588  {
589  adc_clk_divide = ADC_CLK_DIVIDE_2;
590  }
591  else if ((adc_freq / 4U) <= (ADC_CLOCK_FREQ_MAX_RUNTIME / 2U))
592  {
593  adc_clk_divide = ADC_CLK_DIVIDE_4;
594  }
595  else if ((adc_freq / 8U) <= (ADC_CLOCK_FREQ_MAX_RUNTIME / 2U))
596  {
597  adc_clk_divide = ADC_CLK_DIVIDE_8;
598  }
599  else
600  {
601  /* frequency is greater than required clock for calibration */
602  DEV_ASSERT(false);
603  }
604  ADC_SetClockDivide(base, adc_clk_divide);
605  }
606  /* start calibration */
607  ADC_SetCalibrationActiveFlag(base, true);
608  while (ADC_GetCalibrationActiveFlag(base))
609  {
610  /* Wait for calibration to finish */
611  }
612 
613  /* restore adc clock divide*/
614  ADC_SetClockDivide(base, adc_clk_divide_res);
615  /* restore hardware average and trigger settings*/
616  ADC_SetHwAverageEnableFlag(base, hwavgen);
617  ADC_SetHwAverageMode(base, hwavg);
618  ADC_SetTriggerMode(base, trig);
619 }
620 
621 /*FUNCTION**********************************************************************
622  *
623  * Function Name : ADC_DRV_InitUserCalibrationStruct
624  * Description : This function initializes the User Calibration configuration
625  * structure to default values (Reference Manual resets). This function should be called
626  * on a structure before using it to configure the User Calibration feature (ADC_DRV_ConfigUserCalibration),
627  * otherwise all members must be written by the caller. This function insures that all members are written
628  * with safe values, so the user can modify only the desired members.
629  *
630  * Implements : ADC_DRV_InitUserCalibrationStruct_Activity
631  *END**************************************************************************/
633 {
634  DEV_ASSERT(config != NULL);
635 
636  config->userGain = (uint16_t)ADC_DEFAULT_USER_GAIN;
637  config->userOffset = (uint16_t)0U;
638 }
639 
640 /*FUNCTION**********************************************************************
641  *
642  * Function Name : ADC_DRV_ConfigUserCalibration
643  * Description : This function sets the configuration for the user calibration
644  * registers.
645  *
646  * Implements : ADC_DRV_ConfigUserCalibration_Activity
647  *END**************************************************************************/
648 void ADC_DRV_ConfigUserCalibration(const uint32_t instance,
649  const adc_calibration_t * const config)
650 {
651  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
652  DEV_ASSERT(config != NULL);
653 
654  ADC_Type * const base = s_adcBase[instance];
655  ADC_SetUserGainValue(base, config->userGain);
656  ADC_SetUserOffsetValue(base, config->userOffset);
657 }
658 
659 /*FUNCTION**********************************************************************
660  *
661  * Function Name : ADC_DRV_GetUserCalibration
662  * Description : This function returns the current user calibration
663  * register values.
664  *
665  * Implements : ADC_DRV_GetUserCalibration_Activity
666  *END**************************************************************************/
667 void ADC_DRV_GetUserCalibration(const uint32_t instance,
668  adc_calibration_t * const config)
669 {
670  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
671  DEV_ASSERT(config != NULL);
672 
673  const ADC_Type * const base = s_adcBase[instance];
674  config->userGain = ADC_GetUserGainValue(base);
675  config->userOffset = ADC_GetUserOffsetValue(base);
676 }
677 
678 /*FUNCTION**********************************************************************
679  *
680  * Function Name : ADC_DRV_GetInterruptNumber
681  * Description : This function returns the interrupt number for the specified ADC instance.
682  *
683  * Implements : ADC_DRV_GetInterruptNumber_Activity
684  *END**************************************************************************/
685 IRQn_Type ADC_DRV_GetInterruptNumber(const uint32_t instance)
686 {
687  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
688 
689  static const IRQn_Type adcIrqId[ADC_INSTANCE_COUNT] = ADC_IRQS;
690  IRQn_Type irqId = adcIrqId[instance];
691 
692  return irqId;
693 }
694 
695 /*FUNCTION**********************************************************************
696  *
697  * Function Name : ADC_DRV_ClearLatchedTriggers
698  * Description : This function clears all trigger latched flags of the ADC instance.
699  *
700  * Implements : ADC_DRV_ClearLatchedTriggers_Activity
701  *END**************************************************************************/
702 void ADC_DRV_ClearLatchedTriggers(const uint32_t instance,
703  const adc_latch_clear_t clearMode)
704 {
705  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
706  DEV_ASSERT((clearMode == ADC_LATCH_CLEAR_WAIT) || (clearMode == ADC_LATCH_CLEAR_FORCE));
707 
708  ADC_Type * const base = s_adcBase[instance];
709  if (clearMode == ADC_LATCH_CLEAR_FORCE)
710  {
711  ADC_ClearLatchTriggers(base);
712  }
713 
714  while (ADC_GetTriggerLatchFlags(base) != 0u)
715  {
716  /* Wait for latched triggers to be processed */
717  }
718 }
719 
720 /*FUNCTION**********************************************************************
721  *
722  * Function Name : ADC_DRV_ClearTriggerErrors
723  * Description : This function clears all trigger error flags of the ADC instance.
724  *
725  * Implements : ADC_DRV_ClearTriggerErrors_Activity
726  *END**************************************************************************/
727 void ADC_DRV_ClearTriggerErrors(const uint32_t instance)
728 {
729  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
730 
731  ADC_Type * const base = s_adcBase[instance];
732 
733  base->SC2 |= ADC_SC2_TRGSTERR_MASK;
734 }
735 
736 /*FUNCTION**********************************************************************
737  *
738  * Function Name : ADC_DRV_GetTriggerErrorFlags
739  * Description : This function returns the trigger error flags bits of the ADC instance.
740  *
741  * Implements : ADC_DRV_GetTriggerErrorFlags_Activity
742  *END**************************************************************************/
743 uint32_t ADC_DRV_GetTriggerErrorFlags(const uint32_t instance)
744 {
745  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
746 
747  const ADC_Type * const base = s_adcBase[instance];
748 
749  uint32_t trig_errors = (base->SC2 & ADC_SC2_TRGSTERR_MASK) >> ADC_SC2_TRGSTERR_SHIFT;
750 
751  return trig_errors;
752 }
753 
754 /******************************************************************************
755  * EOF
756  *****************************************************************************/
static ADC_Type *const s_adcBase[ADC_INSTANCE_COUNT]
Definition: adc_driver.c:53
#define ADC_SC1_COCO_SHIFT
Definition: S32K118.h:337
Defines the user calibration configuration.
Definition: adc_driver.h:320
#define ADC_USR_OFS_USR_OFS(x)
Definition: S32K118.h:444
uint32_t ADC_DRV_GetTriggerErrorFlags(const uint32_t instance)
Get the trigger error flags bits of the ADC instance.
Definition: adc_driver.c:743
void ADC_DRV_AutoCalibration(const uint32_t instance)
Executes an Auto-Calibration.
Definition: adc_driver.c:548
adc_trigger_sel_t triggerSel
Definition: adc_driver.h:260
#define ADC_SC3_ADCO(x)
Definition: S32K118.h:425
#define ADC_SC2_ACREN(x)
Definition: S32K118.h:384
#define ADC_SC1_ADCH(x)
Definition: S32K118.h:331
adc_voltage_reference_t voltageRef
Definition: adc_driver.h:262
#define ADC_SC2_REFSEL(x)
Definition: S32K118.h:376
volatile uint32_t CFG2
Definition: S32K118.h:270
adc_clk_divide_t
Clock Divider selection.
Definition: adc_driver.h:60
void ADC_DRV_GetHwAverageConfig(const uint32_t instance, adc_average_config_t *const config)
Gets the current Hardware Average configuration.
Definition: adc_driver.c:334
void ADC_DRV_InitHwCompareStruct(adc_compare_config_t *const config)
Initializes the Hardware Compare configuration structure.
Definition: adc_driver.c:233
void ADC_DRV_InitHwAverageStruct(adc_average_config_t *const config)
Initializes the Hardware Average configuration structure.
Definition: adc_driver.c:299
volatile uint32_t UG
Definition: S32K118.h:281
#define SIM
Definition: S32K118.h:9629
#define ADC_CLOCKS
bool ADC_DRV_GetConvCompleteFlag(const uint32_t instance, const uint8_t chanIndex)
Gets the control channel Conversion Complete Flag state.
Definition: adc_driver.c:483
void ADC_DRV_ConfigConverter(const uint32_t instance, const adc_converter_config_t *const config)
Configures the converter with the given configuration structure.
Definition: adc_driver.c:91
volatile uint32_t CHIPCTL
Definition: S32K118.h:9598
adc_input_clock_t inputClock
Definition: adc_driver.h:257
#define ADC_CFG2_SMPLTS(x)
Definition: S32K118.h:361
#define ADC_DEFAULT_USER_GAIN
ADC default User Gain from RM.
void ADC_DRV_ConfigUserCalibration(const uint32_t instance, const adc_calibration_t *const config)
Configures the User Calibration feature with the given configuration structure.
Definition: adc_driver.c:648
void ADC_DRV_Reset(const uint32_t instance)
Resets the converter (sets all configurations to reset values)
Definition: adc_driver.c:175
void ADC_DRV_SetSwPretrigger(const uint32_t instance, const adc_sw_pretrigger_t swPretrigger)
This function sets the software pretrigger - affects only first 4 control channels.
Definition: adc_driver.c:423
volatile uint32_t CV[2u]
Definition: S32K118.h:272
adc_clk_divide_t clockDivide
Definition: adc_driver.h:254
#define ADC_SC2_DMAEN(x)
Definition: S32K118.h:380
volatile uint32_t CLP2
Definition: S32K118.h:284
#define ADC_SC3_AVGE(x)
Definition: S32K118.h:421
void ADC_DRV_GetChanResult(const uint32_t instance, const uint8_t chanIndex, uint16_t *const result)
Gets the last result for the selected control channel.
Definition: adc_driver.c:510
#define ADC_CLOCK_FREQ_MIN_RUNTIME
#define ADC_R_COUNT
Definition: S32K118.h:263
uint16_t userGain
Definition: adc_driver.h:322
Defines the hardware average configuration.
Definition: adc_driver.h:292
#define SIM_CHIPCTL_ADC_SUPPLY_MASK
Definition: S32K118.h:9669
#define SIM_ADCOPT_ADC0SWPRETRG_MASK
Definition: S32K118.h:9740
adc_trigger_t trigger
Definition: adc_driver.h:258
void ADC_DRV_ConfigChan(const uint32_t instance, const uint8_t chanIndex, const adc_chan_config_t *const config)
Configures the selected control channel with the given configuration structure.
Definition: adc_driver.c:378
volatile uint32_t CFG1
Definition: S32K118.h:269
void ADC_DRV_ConfigHwCompare(const uint32_t instance, const adc_compare_config_t *const config)
Configures the Hardware Compare feature with the given configuration structure.
Definition: adc_driver.c:252
#define ADC_SC2_ACFGT(x)
Definition: S32K118.h:388
#define DEV_ASSERT(x)
Definition: devassert.h:77
#define ADC_SC2_ADTRG(x)
Definition: S32K118.h:396
void ADC_DRV_GetChanConfig(const uint32_t instance, const uint8_t chanIndex, adc_chan_config_t *const config)
Gets the current control channel configuration for the selected channel index.
Definition: adc_driver.c:403
volatile uint32_t CLPS
Definition: S32K118.h:282
volatile uint32_t CLP0
Definition: S32K118.h:286
void ADC_DRV_InitChanStruct(adc_chan_config_t *const config)
Initializes the control channel configuration structure.
Definition: adc_driver.c:356
volatile uint32_t CLP3
Definition: S32K118.h:283
#define ADC_CFG1_MODE(x)
Definition: S32K118.h:348
adc_sw_pretrigger_t
Software pretriggers which may be set from Trigger Latching and Arbitration Unit. ...
Definition: adc_driver.h:132
status_t CLOCK_SYS_GetFreq(clock_names_t clockName, uint32_t *frequency)
Gets the clock frequency for a specific clock name.
void ADC_DRV_InitConverterStruct(adc_converter_config_t *const config)
Initializes the converter configuration structure.
Definition: adc_driver.c:66
void ADC_DRV_ClearTriggerErrors(const uint32_t instance)
Clear all latch trigger error.
Definition: adc_driver.c:727
#define ADC_SC1_AIEN(x)
Definition: S32K118.h:335
#define ADC_SC2_TRGSTERR_MASK
Definition: S32K118.h:409
void ADC_DRV_GetConverterConfig(const uint32_t instance, adc_converter_config_t *const config)
Gets the current converter configuration.
Definition: adc_driver.c:137
volatile uint32_t CLP1
Definition: S32K118.h:285
#define ADC_CLOCK_FREQ_MAX_RUNTIME
volatile uint32_t SC3
Definition: S32K118.h:274
status_t
Status return codes. Common error codes will be a unified enumeration (C enum) that will contain all ...
Definition: status.h:44
IRQn_Type
Defines the Interrupt Numbers definitions.
Definition: S32K118.h:188
volatile uint32_t USR_OFS
Definition: S32K118.h:277
void ADC_DRV_InitUserCalibrationStruct(adc_calibration_t *const config)
Initializes the User Calibration configuration structure.
Definition: adc_driver.c:632
#define ADC_CFG1_ADICLK(x)
Definition: S32K118.h:344
IRQn_Type ADC_DRV_GetInterruptNumber(const uint32_t instance)
Returns the interrupt number for the ADC instance.
Definition: adc_driver.c:685
#define ADC_SC3_CAL(x)
Definition: S32K118.h:429
volatile const uint32_t R[16u]
Definition: S32K118.h:271
#define ADC_CV_CV(x)
Definition: S32K118.h:371
Defines the converter configuration.
Definition: adc_driver.h:252
#define SIM_ADCOPT_ADC1SWPRETRG(x)
Definition: S32K118.h:9755
#define ADC_INSTANCE_COUNT
Definition: S32K118.h:299
volatile uint32_t SC2
Definition: S32K118.h:273
adc_trigger_t
Trigger type selection.
Definition: adc_driver.h:98
#define SIM_ADCOPT_ADC1SWPRETRG_MASK
Definition: S32K118.h:9752
volatile uint32_t CLPX
Definition: S32K118.h:287
#define ADC_SC3_AVGS(x)
Definition: S32K118.h:417
#define ADC_SC2_ACFE(x)
Definition: S32K118.h:392
void ADC_DRV_GetUserCalibration(const uint32_t instance, adc_calibration_t *const config)
Gets the current User Calibration configuration.
Definition: adc_driver.c:667
#define ADC_BASE_PTRS
Definition: S32K118.h:310
#define ADC_R_D_SHIFT
Definition: S32K118.h:364
volatile uint32_t ADCOPT
Definition: S32K118.h:9603
#define SIM_ADCOPT_ADC0SWPRETRG(x)
Definition: S32K118.h:9743
volatile uint32_t CLP9
Definition: S32K118.h:288
#define ADC_IRQS
Definition: S32K118.h:316
#define ADC_SC2_TRGSTERR_SHIFT
Definition: S32K118.h:410
adc_average_t
Hardware average selection.
Definition: adc_driver.h:157
#define ADC_SC1_COCO_MASK
Definition: S32K118.h:336
adc_average_t hwAverage
Definition: adc_driver.h:295
void ADC_DRV_ClearLatchedTriggers(const uint32_t instance, const adc_latch_clear_t clearMode)
Clear latched triggers under processing.
Definition: adc_driver.c:702
#define ADC_DEFAULT_SAMPLE_TIME
ADC default Sample Time from RM.
#define ADC_CV_COUNT
Definition: S32K118.h:264
clock_names_t
Clock names.
#define ADC_UG_UG(x)
Definition: S32K118.h:464
#define NUMBER_OF_ALT_CLOCKS
adc_latch_clear_t
Defines the trigger latch clear method Implements : adc_latch_clear_t_Class.
Definition: adc_driver.h:330
#define ADC_CTRL_CHANS_COUNT
ADC number of control channels.
void ADC_DRV_WaitConvDone(const uint32_t instance)
Waits for a conversion/calibration to finish.
Definition: adc_driver.c:463
#define ADC_SC1_COUNT
Definition: S32K118.h:262
#define SIM_CHIPCTL_ADC_SUPPLYEN_MASK
Definition: S32K118.h:9673
#define ADC_R_D_MASK
Definition: S32K118.h:363
adc_resolution_t resolution
Definition: adc_driver.h:256
#define ADC_CFG1_ADIV(x)
Definition: S32K118.h:352
uint16_t userOffset
Definition: adc_driver.h:323
adc_pretrigger_sel_t pretriggerSel
Definition: adc_driver.h:259
void ADC_DRV_GetHwCompareConfig(const uint32_t instance, adc_compare_config_t *const config)
Gets the current Hardware Compare configuration.
Definition: adc_driver.c:274
void ADC_DRV_ConfigHwAverage(const uint32_t instance, const adc_average_config_t *const config)
Configures the Hardware Average feature with the given configuration structure.
Definition: adc_driver.c:315
Defines the hardware compare configuration.
Definition: adc_driver.h:275
adc_inputchannel_t channel
Definition: adc_driver.h:309
Defines the control channel configuration.
Definition: adc_driver.h:306
volatile uint32_t SC1[16u]
Definition: S32K118.h:268