S32 SDK
adc_driver.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 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  */
37 #include <stddef.h>
38 #include "adc_driver.h"
39 #include "adc_hw_access.h"
40 
41 /*******************************************************************************
42  * Variables
43  ******************************************************************************/
44 
45 /* Table of base addresses for ADC instances. */
47 
48 /*FUNCTION**********************************************************************
49  *
50  * Function Name : ADC_DRV_InitConverterStruct
51  * Description : This function initializes the members of the adc_converter_config_t
52  * structure to default values (Reference Manual resets). This function should be called
53  * on a structure before using it to configure the converter (ADC_DRV_ConfigConverter), otherwise all members
54  * must be written (initialized) by the caller. This function insures that all members are written
55  * with safe values, so the user can modify only the desired members.
56  *
57  * Implements : ADC_DRV_InitConverterStruct_Activity
58  *END**************************************************************************/
60 {
61  DEV_ASSERT(config != NULL);
62 
63  config->clockDivide = ADC_CLK_DIVIDE_1;
64  config->sampleTime = (uint8_t)ADC_DEFAULT_SAMPLE_TIME;
66  config->inputClock = ADC_CLK_ALT_1;
67  config->trigger = ADC_TRIGGER_SOFTWARE;
70  config->dmaEnable = false;
72  config->continuousConvEnable = false;
73 }
74 
75 /*FUNCTION**********************************************************************
76  *
77  * Function Name : ADC_DRV_ConfigConverter
78  * Description : This function configures the ADC converter with the options
79  * provided in the configuration structure.
80  *
81  * Implements : ADC_DRV_ConfigConverter_Activity
82  *END**************************************************************************/
83 void ADC_DRV_ConfigConverter(const uint32_t instance,
84  const adc_converter_config_t * const config)
85 {
86  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
87  DEV_ASSERT(config != NULL);
88 
89  ADC_Type * const base = s_adcBase[instance];
90  ADC_SetClockDivide(base, config->clockDivide);
91  ADC_SetSampleTime(base, config->sampleTime);
92  ADC_SetResolution(base, config->resolution);
93  ADC_SetInputClock(base, config->inputClock);
94  ADC_SetTriggerMode(base, config->trigger);
95  ADC_SetPretriggerSelect(instance, config->pretriggerSel);
96  ADC_SetTriggerSelect(instance, config->triggerSel);
97  ADC_SetDMAEnableFlag(base, config->dmaEnable);
98  ADC_SetVoltageReference(base, config->voltageRef);
99  ADC_SetContinuousConvFlag(base, config->continuousConvEnable);
100 }
101 
102 /*FUNCTION**********************************************************************
103  *
104  * Function Name : ADC_DRV_GetConverterConfig
105  * Description : This functions returns the current converter configuration in
106  * the form of a configuration structure.
107  *
108  * Implements : ADC_DRV_GetConverterConfig_Activity
109  *END**************************************************************************/
110 void ADC_DRV_GetConverterConfig(const uint32_t instance,
111  adc_converter_config_t * const config)
112 {
113  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
114  DEV_ASSERT(config != NULL);
115 
116  const ADC_Type * const base = s_adcBase[instance];
117  config->clockDivide = ADC_GetClockDivide(base);
118  config->sampleTime = ADC_GetSampleTime(base);
119  config->resolution = ADC_GetResolution(base);
120  config->inputClock = ADC_GetInputClock(base);
121  config->trigger = ADC_GetTriggerMode(base);
122  config->triggerSel = ADC_GetTriggerSelect(instance);
123  config->pretriggerSel = ADC_GetPretriggerSelect(instance);
124  config->dmaEnable = ADC_GetDMAEnableFlag(base);
125  config->voltageRef = ADC_GetVoltageReference(base);
126  config->continuousConvEnable = ADC_GetContinuousConvFlag(base);
127 }
128 
129 /*FUNCTION**********************************************************************
130  *
131  * Function Name : ADC_DRV_Reset
132  * Description : This function writes all the internal ADC registers with
133  * their Reference Manual reset values.
134  *
135  * Implements : ADC_DRV_Reset_Activity
136  *END**************************************************************************/
137 void ADC_DRV_Reset(const uint32_t instance)
138 {
139  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
140 
141  ADC_Type * const baseAddr = s_adcBase[instance];
142  uint8_t idx = 0U;
143 
144  for(idx = 0U; idx < ADC_SC1_COUNT; idx++)
145  {
146  baseAddr->SC1[idx] = ADC_SC1_ADCH(ADC_INPUTCHAN_DISABLED) | ADC_SC1_AIEN(0x00U);
147  }
148 
151 
152  for(idx = 0U; idx < ADC_CV_COUNT; idx++)
153  {
154  baseAddr->CV[idx] = ADC_CV_CV(0U);
155  }
156 
157  baseAddr->SC2 = ADC_SC2_REFSEL(ADC_VOLTAGEREF_VREF) | ADC_SC2_DMAEN(0x00U) | ADC_SC2_ACREN(0x00U) | ADC_SC2_ACFGT(0x00U) | ADC_SC2_ACFE(0x00U) |
158  ADC_SC2_ADTRG(0x00U);
159  baseAddr->SC3 = ADC_SC3_AVGS(ADC_AVERAGE_4) | ADC_SC3_AVGE(0x00U) | ADC_SC3_ADCO(0x00U) | ADC_SC3_CAL(0x00U);
160  baseAddr->USR_OFS = ADC_USR_OFS_USR_OFS(0U);
161  baseAddr->UG = ADC_UG_UG(ADC_DEFAULT_USER_GAIN);
162 
163 #if FEATURE_ADC_HAS_EXTRA_NUM_REGS
164  for(idx = 0U; idx < ADC_aSC1_COUNT; idx++)
165  {
166  baseAddr->aSC1[idx] = ADC_aSC1_ADCH(ADC_INPUTCHAN_DISABLED) | ADC_aSC1_AIEN(0x00U);
167  }
168 #endif /* FEATURE_ADC_HAS_EXTRA_NUM_REGS */
169 
170  ADC_SetPretriggerSelect(instance, ADC_PRETRIGGER_SEL_PDB);
171  ADC_SetTriggerSelect(instance, ADC_TRIGGER_SEL_PDB);
173 }
174 
175 /*FUNCTION**********************************************************************
176  *
177  * Function Name : ADC_DRV_InitHwCompareStruct
178  * Description : This function initializes the Hardware Compare configuration
179  * structure to default values (Reference Manual resets). This function should be
180  * called before configuring the Hardware Compare feature (ADC_DRV_ConfigHwCompare),
181  * otherwise all members must be written by the caller. This function insures that all
182  * members are written with safe values, so the user can modify the desired members.
183  *
184  * Implements : ADC_DRV_InitHwCompareStruct_Activity
185  *END**************************************************************************/
187 {
188  DEV_ASSERT(config != NULL);
189 
190  config->compareEnable = false;
191  config->compareGreaterThanEnable = false;
192  config->compareRangeFuncEnable = false;
193  config->compVal1 = 0U;
194  config->compVal2 = 0U;
195 }
196 
197 /*FUNCTION**********************************************************************
198  *
199  * Function Name : ADC_DRV_ConfigHwCompare
200  * Description : This functions sets the configuration for the Hardware
201  * Compare feature using the configuration structure.
202  *
203  * Implements : ADC_DRV_ConfigHwCompare_Activity
204  *END**************************************************************************/
205 void ADC_DRV_ConfigHwCompare(const uint32_t instance,
206  const adc_compare_config_t * const config)
207 {
208  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
209  DEV_ASSERT(config != NULL);
210 
211  ADC_Type * const base = s_adcBase[instance];
212  ADC_SetHwCompareEnableFlag(base, config->compareEnable);
213  ADC_SetHwCompareGtEnableFlag(base, config->compareGreaterThanEnable);
214  ADC_SetHwCompareRangeEnableFlag(base, config->compareRangeFuncEnable);
215  ADC_SetHwCompareComp1Value(base, config->compVal1);
216  ADC_SetHwCompareComp2Value(base, config->compVal2);
217 }
218 
219 /*FUNCTION**********************************************************************
220  *
221  * Function Name : ADC_DRV_GetHwCompareConfig
222  * Description : This function returns the configuration for the Hardware
223  * Compare feature.
224  *
225  * Implements : ADC_DRV_GetHwCompareConfig_Activity
226  *END**************************************************************************/
227 void ADC_DRV_GetHwCompareConfig(const uint32_t instance,
228  adc_compare_config_t * const config)
229 {
230  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
231  DEV_ASSERT(config != NULL);
232 
233  const ADC_Type * const base = s_adcBase[instance];
234  config->compareEnable = ADC_GetHwCompareEnableFlag(base);
235  config->compareGreaterThanEnable = ADC_GetHwCompareGtEnableFlag(base);
236  config->compareRangeFuncEnable = ADC_GetHwCompareRangeEnableFlag(base);
237  config->compVal1 = ADC_GetHwCompareComp1Value(base);
238  config->compVal2 = ADC_GetHwCompareComp2Value(base);
239 }
240 
241 /*FUNCTION**********************************************************************
242  *
243  * Function Name : ADC_DRV_InitHwAverageStruct
244  * Description : This function initializes the Hardware Average configuration
245  * structure to default values (Reference Manual resets). This function should be
246  * called before configuring the Hardware Average feature (ADC_DRV_ConfigHwAverage),
247  * otherwise all members must be written by the caller. This function insures that all
248  * members are written with safe values, so the user can modify the desired members.
249  *
250  * Implements : ADC_DRV_InitHwAverageStruct_Activity
251  *END**************************************************************************/
253 {
254  DEV_ASSERT(config != NULL);
255 
256  config->hwAvgEnable = false;
257  config->hwAverage = ADC_AVERAGE_4;
258 }
259 
260 /*FUNCTION**********************************************************************
261  *
262  * Function Name : ADC_DRV_ConfigHwAverage
263  * Description : This function sets the configuration for the Hardware
264  * Average feature.
265  *
266  * Implements : ADC_DRV_ConfigHwAverage_Activity
267  *END**************************************************************************/
268 void ADC_DRV_ConfigHwAverage(const uint32_t instance,
269  const adc_average_config_t * const config)
270 {
271  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
272  DEV_ASSERT(config != NULL);
273 
274  ADC_Type * const base = s_adcBase[instance];
275  ADC_SetHwAverageEnableFlag(base, config->hwAvgEnable);
276  ADC_SetHwAverageMode(base, config->hwAverage);
277 }
278 
279 /*FUNCTION**********************************************************************
280  *
281  * Function Name : ADC_DRV_GetHwAverageConfig
282  * Description : This function returns the configuration for the Hardware
283  * Average feature.
284  *
285  * Implements : ADC_DRV_GetHwAverageConfig_Activity
286  *END**************************************************************************/
287 void ADC_DRV_GetHwAverageConfig(const uint32_t instance,
288  adc_average_config_t * const config)
289 {
290  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
291  DEV_ASSERT(config != NULL);
292 
293  const ADC_Type * const base = s_adcBase[instance];
294  config->hwAvgEnable = ADC_GetHwAverageEnableFlag(base);
295  config->hwAverage = ADC_GetHwAverageMode(base);
296 }
297 
298 /*FUNCTION**********************************************************************
299  *
300  * Function Name : ADC_DRV_InitChanStruct
301  * Description : This function initializes the control channel
302  * configuration structure to default values (Reference Manual resets). This function should
303  * be called on a structure before using it to configure a channel (ADC_DRV_ConfigChan), otherwise
304  * all members must be written by the caller. This function insures that all members are written
305  * with safe values, so the user can modify only the desired members.
306  *
307  * Implements : ADC_DRV_InitChanStruct_Activity
308  *END**************************************************************************/
310 {
311  DEV_ASSERT(config != NULL);
312 
313  config->interruptEnable = false;
315 }
316 
317 /*FUNCTION**********************************************************************
318  *
319  * Function Name : ADC_DRV_ConfigChan
320  * Description : This function sets a control channel configuration.
321  *
322  * When Software Trigger mode is enabled, configuring control channel index 0,
323  * implicitly triggers a new conversion on the selected ADC input channel.
324  * Therefore, ADC_DRV_ConfigChan can be used for sw-triggering conversions.
325  *
326  * Configuring any control channel while it is actively controlling a conversion
327  * (sw or hw triggered) will implicitly abort the on-going conversion.
328  *
329  * Implements : ADC_DRV_ConfigChan_Activity
330  *END**************************************************************************/
331 void ADC_DRV_ConfigChan(const uint32_t instance,
332  const uint8_t chanIndex,
333  const adc_chan_config_t * const config)
334 {
335  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
336  DEV_ASSERT(chanIndex < ADC_CTRL_CHANS_COUNT);
337  DEV_ASSERT(config != NULL);
338 
339  ADC_Type * const base = s_adcBase[instance];
340  ADC_SetChanInterruptEnableFlag(base, chanIndex, config->interruptEnable);
341  ADC_SetInputChannel(base, chanIndex, config->channel);
342 }
343 
344 /*FUNCTION**********************************************************************
345  *
346  * Function Name : ADC_DRV_GetChanConfig
347  * Description : This function returns the current configuration for a control
348  * channel.
349  *
350  * Implements : ADC_DRV_GetChanConfig_Activity
351  *END**************************************************************************/
352 void ADC_DRV_GetChanConfig(const uint32_t instance,
353  const uint8_t chanIndex,
354  adc_chan_config_t * const config)
355 {
356  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
357  DEV_ASSERT(chanIndex < ADC_CTRL_CHANS_COUNT);
358  DEV_ASSERT(config != NULL);
359 
360  const ADC_Type * const base = s_adcBase[instance];
361  config->interruptEnable = ADC_GetChanInterruptEnableFlag(base, chanIndex);
362  config->channel = ADC_GetInputChannel(base, chanIndex);
363 }
364 
365 /*FUNCTION**********************************************************************
366  *
367  * Function Name : ADC_DRV_SetSwPretrigger
368  * Description : This function sets the software pretrigger - affects only first 4 control channels.
369  *
370  * Implements : ADC_DRV_SetSwPretrigger_Activity
371  *END**************************************************************************/
372 void ADC_DRV_SetSwPretrigger(const uint32_t instance,
373  const adc_sw_pretrigger_t swPretrigger)
374 {
375  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
376  SIM_Type * const simBase = SIM;
377  uint32_t intermValue = 0U;
379 
380  /* If SW Pretrigger Select is not enabled, the SW pretriggers will be ignored by ADC. */
381  DEV_ASSERT((ADC_GetPretriggerSelect(instance) == ADC_PRETRIGGER_SEL_SW) || \
382  (swPretrigger == ADC_SW_PRETRIGGER_DISABLED));
383 
384  intermValue = simBase->ADCOPT & (~ mask[instance]);
385  switch(instance)
386  {
387  case 0:
388  intermValue |= SIM_ADCOPT_ADC0SWPRETRG(swPretrigger);
389  break;
390  case 1:
391  intermValue |= SIM_ADCOPT_ADC1SWPRETRG(swPretrigger);
392  break;
393  default:
394  DEV_ASSERT(false);
395  break;
396  }
397 
398  simBase->ADCOPT = intermValue;
399 }
400 
401 /*FUNCTION**********************************************************************
402  *
403  * Function Name : ADC_DRV_WaitConvDone
404  * Description : This functions waits for a conversion to complete by
405  * continuously polling the Conversion Active Flag.
406  *
407  * Implements : ADC_DRV_WaitConvDone_Activity
408  *END**************************************************************************/
409 void ADC_DRV_WaitConvDone(const uint32_t instance)
410 {
411  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
412 
413  const ADC_Type * const base = s_adcBase[instance];
414  while (ADC_GetConvActiveFlag(base) == true)
415  {
416  /* Wait for conversion to finish */
417  }
418 }
419 
420 /*FUNCTION**********************************************************************
421  *
422  * Function Name : ADC_DRV_GetConvCompleteFlag
423  * Description : This function returns the state of the Conversion Complete
424  * flag for a control channel. This flag is set when a conversion is complete
425  * or the condition generated by the Hardware Compare feature is evaluated to true.
426  *
427  * Implements : ADC_DRV_GetConvCompleteFlag_Activity
428  *END**************************************************************************/
429 bool ADC_DRV_GetConvCompleteFlag(const uint32_t instance,
430  const uint8_t chanIndex)
431 {
432  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
433  DEV_ASSERT(chanIndex < ADC_CTRL_CHANS_COUNT);
434 
435  const ADC_Type * const base = s_adcBase[instance];
436 
437 #if FEATURE_ADC_HAS_EXTRA_NUM_REGS
438  uint32_t tmp = base->aSC1[chanIndex];
439  tmp = (tmp & ADC_aSC1_COCO_MASK) >> ADC_aSC1_COCO_SHIFT;
440 #else
441  uint32_t tmp = base->SC1[chanIndex];
442  tmp = (tmp & ADC_SC1_COCO_MASK) >> ADC_SC1_COCO_SHIFT;
443 #endif /* FEATURE_ADC_HAS_EXTRA_NUM_REGS */
444 
445  return (tmp != 0u) ? true : false;
446 }
447 
448 /*FUNCTION**********************************************************************
449  *
450  * Function Name : ADC_DRV_GetChanResult
451  * Description : This function returns the conversion result from a
452  * control channel.
453  *
454  * Implements : ADC_DRV_GetChanResult_Activity
455  *END**************************************************************************/
456 void ADC_DRV_GetChanResult(const uint32_t instance,
457  const uint8_t chanIndex,
458  uint16_t * const result)
459 {
460  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
461  DEV_ASSERT(result != NULL);
462 
463  const ADC_Type * const base = s_adcBase[instance];
464 
465 #if FEATURE_ADC_HAS_EXTRA_NUM_REGS
466 
467  DEV_ASSERT(chanIndex < ADC_aR_COUNT);
468 
469  uint32_t tmp = base->aR[chanIndex];
470  tmp = (tmp & ADC_aR_D_MASK) >> ADC_aR_D_SHIFT;
471 #else
472 
473  DEV_ASSERT(chanIndex < ADC_R_COUNT);
474 
475  uint32_t tmp = base->R[chanIndex];
476  tmp = (tmp & ADC_R_D_MASK) >> ADC_R_D_SHIFT;
477 #endif /* FEATURE_ADC_HAS_EXTRA_NUM_REGS */
478 
479  *result = (uint16_t)tmp;
480 }
481 
482 /*FUNCTION**********************************************************************
483  *
484  * Function Name : ADC_DRV_AutoCalibration
485  * Description : This functions executes an Auto-Calibration sequence. It
486  * is recommended to run this sequence before using the ADC converter.
487  *
488  * Implements : ADC_DRV_AutoCalibration_Activity
489  *END**************************************************************************/
490 void ADC_DRV_AutoCalibration(const uint32_t instance)
491 {
492  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
493 
494  ADC_Type * const base = s_adcBase[instance];
495  /* set hardware average to maximum and set software trigger*/
496  bool hwavgen = ADC_GetHwAverageEnableFlag(base);
497  adc_average_t hwavg = ADC_GetHwAverageMode(base);
498  adc_trigger_t trig = ADC_GetTriggerMode(base);
499  ADC_SetHwAverageMode(base, ADC_AVERAGE_32);
500  ADC_SetHwAverageEnableFlag(base, true);
501  ADC_SetTriggerMode(base, ADC_TRIGGER_SOFTWARE);
502 
503  base->CLPS = 0x00u;
504  base->CLP3 = 0x00u;
505  base->CLP2 = 0x00u;
506  base->CLP1 = 0x00u;
507  base->CLP0 = 0x00u;
508  base->CLPX = 0x00u;
509  base->CLP9 = 0x00u;
510 
511  /* start calibration */
512  ADC_SetCalibrationActiveFlag(base, true);
513  while (ADC_GetCalibrationActiveFlag(base))
514  {
515  /* Wait for calibration to finish */
516  }
517 
518  /* restore hardware average and trigger settings*/
519  ADC_SetHwAverageEnableFlag(base, hwavgen);
520  ADC_SetHwAverageMode(base, hwavg);
521  ADC_SetTriggerMode(base, trig);
522 }
523 
524 /*FUNCTION**********************************************************************
525  *
526  * Function Name : ADC_DRV_InitUserCalibrationStruct
527  * Description : This function initializes the User Calibration configuration
528  * structure to default values (Reference Manual resets). This function should be called
529  * on a structure before using it to configure the User Calibration feature (ADC_DRV_ConfigUserCalibration),
530  * otherwise all members must be written by the caller. This function insures that all members are written
531  * with safe values, so the user can modify only the desired members.
532  *
533  * Implements : ADC_DRV_InitUserCalibrationStruct_Activity
534  *END**************************************************************************/
536 {
537  DEV_ASSERT(config != NULL);
538 
539  config->userGain = (uint16_t)ADC_DEFAULT_USER_GAIN;
540  config->userOffset = (uint16_t)0U;
541 }
542 
543 /*FUNCTION**********************************************************************
544  *
545  * Function Name : ADC_DRV_ConfigUserCalibration
546  * Description : This function sets the configuration for the user calibration
547  * registers.
548  *
549  * Implements : ADC_DRV_ConfigUserCalibration_Activity
550  *END**************************************************************************/
551 void ADC_DRV_ConfigUserCalibration(const uint32_t instance,
552  const adc_calibration_t * const config)
553 {
554  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
555  DEV_ASSERT(config != NULL);
556 
557  ADC_Type * const base = s_adcBase[instance];
558  ADC_SetUserGainValue(base, config->userGain);
559  ADC_SetUserOffsetValue(base, config->userOffset);
560 }
561 
562 /*FUNCTION**********************************************************************
563  *
564  * Function Name : ADC_DRV_GetUserCalibration
565  * Description : This function returns the current user calibration
566  * register values.
567  *
568  * Implements : ADC_DRV_GetUserCalibration_Activity
569  *END**************************************************************************/
570 void ADC_DRV_GetUserCalibration(const uint32_t instance,
571  adc_calibration_t * const config)
572 {
573  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
574  DEV_ASSERT(config != NULL);
575 
576  const ADC_Type * const base = s_adcBase[instance];
577  config->userGain = ADC_GetUserGainValue(base);
578  config->userOffset = ADC_GetUserOffsetValue(base);
579 }
580 
581 /*FUNCTION**********************************************************************
582  *
583  * Function Name : ADC_DRV_GetInterruptNumber
584  * Description : This function returns the interrupt number for the specified ADC instance.
585  *
586  * Implements : ADC_DRV_GetInterruptNumber_Activity
587  *END**************************************************************************/
588 IRQn_Type ADC_DRV_GetInterruptNumber(const uint32_t instance)
589 {
590  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
591 
592  static const IRQn_Type adcIrqId[ADC_INSTANCE_COUNT] = ADC_IRQS;
593  IRQn_Type irqId = adcIrqId[instance];
594 
595  return irqId;
596 }
597 
598 /*FUNCTION**********************************************************************
599  *
600  * Function Name : ADC_DRV_ClearLatchedTriggers
601  * Description : This function clears all trigger latched flags of the ADC instance.
602  *
603  * Implements : ADC_DRV_ClearLatchedTriggers_Activity
604  *END**************************************************************************/
605 void ADC_DRV_ClearLatchedTriggers(const uint32_t instance,
606  const adc_latch_clear_t clearMode)
607 {
608  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
609  DEV_ASSERT((clearMode == ADC_LATCH_CLEAR_WAIT) || (clearMode == ADC_LATCH_CLEAR_FORCE));
610 
611  ADC_Type * const base = s_adcBase[instance];
612  if (clearMode == ADC_LATCH_CLEAR_FORCE)
613  {
614  ADC_ClearLatchTriggers(base);
615  }
616 
617  while (ADC_GetTriggerLatchFlags(base) != 0u)
618  {
619  /* Wait for latched triggers to be processed */
620  }
621 }
622 
623 /*FUNCTION**********************************************************************
624  *
625  * Function Name : ADC_DRV_ClearTriggerErrors
626  * Description : This function clears all trigger error flags of the ADC instance.
627  *
628  * Implements : ADC_DRV_ClearTriggerErrors_Activity
629  *END**************************************************************************/
630 void ADC_DRV_ClearTriggerErrors(const uint32_t instance)
631 {
632  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
633 
634  ADC_Type * const base = s_adcBase[instance];
635 
636  base->SC2 |= ADC_SC2_TRGSTERR_MASK;
637 }
638 
639 /*FUNCTION**********************************************************************
640  *
641  * Function Name : ADC_DRV_GetTriggerErrorFlags
642  * Description : This function returns the trigger error flags bits of the ADC instance.
643  *
644  * Implements : ADC_DRV_GetTriggerErrorFlags_Activity
645  *END**************************************************************************/
646 uint32_t ADC_DRV_GetTriggerErrorFlags(const uint32_t instance)
647 {
648  DEV_ASSERT(instance < ADC_INSTANCE_COUNT);
649 
650  const ADC_Type * const base = s_adcBase[instance];
651 
652  uint32_t trig_errors = (base->SC2 & ADC_SC2_TRGSTERR_MASK) >> ADC_SC2_TRGSTERR_SHIFT;
653 
654  return trig_errors;
655 }
656 
657 /******************************************************************************
658  * EOF
659  *****************************************************************************/
static ADC_Type *const s_adcBase[ADC_INSTANCE_COUNT]
Definition: adc_driver.c:46
#define ADC_SC1_COCO_SHIFT
Definition: S32K142.h:401
Defines the user calibration configuration.
Definition: adc_driver.h:281
#define ADC_USR_OFS_USR_OFS(x)
Definition: S32K142.h:508
__IO uint32_t CFG1
Definition: S32K142.h:329
__IO uint32_t USR_OFS
Definition: S32K142.h:337
__IO uint32_t CLPS
Definition: S32K142.h:342
uint32_t ADC_DRV_GetTriggerErrorFlags(const uint32_t instance)
This function returns the trigger error flags bits of the ADC instance.
Definition: adc_driver.c:646
void ADC_DRV_AutoCalibration(const uint32_t instance)
Executes an Auto-Calibration.
Definition: adc_driver.c:490
adc_trigger_sel_t triggerSel
Definition: adc_driver.h:222
#define ADC_SC3_ADCO(x)
Definition: S32K142.h:489
#define ADC_SC2_ACREN(x)
Definition: S32K142.h:448
#define ADC_SC1_ADCH(x)
Definition: S32K142.h:395
adc_voltage_reference_t voltageRef
Definition: adc_driver.h:224
#define ADC_SC2_REFSEL(x)
Definition: S32K142.h:440
void ADC_DRV_GetHwAverageConfig(const uint32_t instance, adc_average_config_t *const config)
Gets the current Hardware Average configuration.
Definition: adc_driver.c:287
#define ADC_DEFAULT_USER_GAIN
ADC default User Gain from RM.
void ADC_DRV_InitHwCompareStruct(adc_compare_config_t *const config)
Initializes the Hardware Compare configuration structure.
Definition: adc_driver.c:186
void ADC_DRV_InitHwAverageStruct(adc_average_config_t *const config)
Initializes the Hardware Average configuration structure.
Definition: adc_driver.c:252
#define SIM
Definition: S32K142.h:10664
bool ADC_DRV_GetConvCompleteFlag(const uint32_t instance, const uint8_t chanIndex)
Gets the control channel Conversion Complete Flag state.
Definition: adc_driver.c:429
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:83
adc_input_clock_t inputClock
Definition: adc_driver.h:219
#define ADC_CFG2_SMPLTS(x)
Definition: S32K142.h:425
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:551
#define ADC_DEFAULT_SAMPLE_TIME
ADC default Sample Time from RM.
void ADC_DRV_Reset(const uint32_t instance)
Resets the converter (sets all configurations to reset values)
Definition: adc_driver.c:137
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:372
__IO uint32_t CLP9
Definition: S32K142.h:348
adc_clk_divide_t clockDivide
Definition: adc_driver.h:216
#define ADC_SC2_DMAEN(x)
Definition: S32K142.h:444
#define ADC_SC3_AVGE(x)
Definition: S32K142.h:485
__IO uint32_t SC3
Definition: S32K142.h:334
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:456
#define ADC_R_COUNT
Definition: S32K142.h:323
uint16_t userGain
Definition: adc_driver.h:283
Defines the hardware average configuration.
Definition: adc_driver.h:253
#define SIM_ADCOPT_ADC0SWPRETRG_MASK
Definition: S32K142.h:10775
adc_trigger_t trigger
Definition: adc_driver.h:220
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:331
#define ADC_CTRL_CHANS_COUNT
ADC number of control channels.
__IO uint32_t CFG2
Definition: S32K142.h:330
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:205
#define ADC_SC2_ACFGT(x)
Definition: S32K142.h:452
#define DEV_ASSERT(x)
Definition: devassert.h:77
#define ADC_SC2_ADTRG(x)
Definition: S32K142.h:460
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:352
__IO uint32_t CLP3
Definition: S32K142.h:343
__IO uint32_t CLP0
Definition: S32K142.h:346
void ADC_DRV_InitChanStruct(adc_chan_config_t *const config)
Initializes the control channel configuration structure.
Definition: adc_driver.c:309
#define ADC_CFG1_MODE(x)
Definition: S32K142.h:412
adc_sw_pretrigger_t
Software pretriggers which may be set from Trigger Latching and Arbitration Unit. ...
Definition: adc_driver.h:113
__IO uint32_t CLP2
Definition: S32K142.h:344
void ADC_DRV_InitConverterStruct(adc_converter_config_t *const config)
Initializes the converter configuration structure.
Definition: adc_driver.c:59
void ADC_DRV_ClearTriggerErrors(const uint32_t instance)
Clear all latch trigger error.
Definition: adc_driver.c:630
#define ADC_SC1_AIEN(x)
Definition: S32K142.h:399
#define ADC_SC2_TRGSTERR_MASK
Definition: S32K142.h:473
void ADC_DRV_GetConverterConfig(const uint32_t instance, adc_converter_config_t *const config)
Gets the current converter configuration.
Definition: adc_driver.c:110
__I uint32_t R[ADC_R_COUNT]
Definition: S32K142.h:331
void ADC_DRV_InitUserCalibrationStruct(adc_calibration_t *const config)
Initializes the User Calibration configuration structure.
Definition: adc_driver.c:535
#define ADC_CFG1_ADICLK(x)
Definition: S32K142.h:408
__IO uint32_t CLP1
Definition: S32K142.h:345
IRQn_Type ADC_DRV_GetInterruptNumber(const uint32_t instance)
Returns the interrupt number for the ADC instance.
Definition: adc_driver.c:588
#define ADC_SC3_CAL(x)
Definition: S32K142.h:493
__IO uint32_t UG
Definition: S32K142.h:341
#define ADC_CV_CV(x)
Definition: S32K142.h:435
Defines the converter configuration.
Definition: adc_driver.h:214
#define SIM_ADCOPT_ADC1SWPRETRG(x)
Definition: S32K142.h:10790
__IO uint32_t CV[ADC_CV_COUNT]
Definition: S32K142.h:332
__IO uint32_t SC2
Definition: S32K142.h:333
#define ADC_INSTANCE_COUNT
Definition: S32K142.h:359
adc_trigger_t
Trigger type selection.
Definition: adc_driver.h:79
__IO uint32_t CLPX
Definition: S32K142.h:347
#define SIM_ADCOPT_ADC1SWPRETRG_MASK
Definition: S32K142.h:10787
#define ADC_SC3_AVGS(x)
Definition: S32K142.h:481
#define ADC_SC2_ACFE(x)
Definition: S32K142.h:456
void ADC_DRV_GetUserCalibration(const uint32_t instance, adc_calibration_t *const config)
Gets the current User Calibration configuration.
Definition: adc_driver.c:570
#define ADC_BASE_PTRS
Definition: S32K142.h:374
#define ADC_R_D_SHIFT
Definition: S32K142.h:428
__IO uint32_t SC1[ADC_SC1_COUNT]
Definition: S32K142.h:328
#define SIM_ADCOPT_ADC0SWPRETRG(x)
Definition: S32K142.h:10778
#define ADC_IRQS
Definition: S32K142.h:380
#define ADC_SC2_TRGSTERR_SHIFT
Definition: S32K142.h:474
adc_average_t
Hardware average selection.
Definition: adc_driver.h:138
#define ADC_SC1_COCO_MASK
Definition: S32K142.h:400
adc_average_t hwAverage
Definition: adc_driver.h:256
void ADC_DRV_ClearLatchedTriggers(const uint32_t instance, const adc_latch_clear_t clearMode)
Clear latched triggers under processing.
Definition: adc_driver.c:605
#define ADC_CV_COUNT
Definition: S32K142.h:324
#define ADC_UG_UG(x)
Definition: S32K142.h:528
__IO uint32_t ADCOPT
Definition: S32K142.h:10638
adc_latch_clear_t
Defines the trigger latch clear method Implements : adc_latch_clear_t_Class.
Definition: adc_driver.h:291
void ADC_DRV_WaitConvDone(const uint32_t instance)
Waits for a conversion/calibration to finish.
Definition: adc_driver.c:409
#define ADC_SC1_COUNT
Definition: S32K142.h:322
#define ADC_R_D_MASK
Definition: S32K142.h:427
adc_resolution_t resolution
Definition: adc_driver.h:218
#define ADC_CFG1_ADIV(x)
Definition: S32K142.h:416
uint16_t userOffset
Definition: adc_driver.h:284
adc_pretrigger_sel_t pretriggerSel
Definition: adc_driver.h:221
void ADC_DRV_GetHwCompareConfig(const uint32_t instance, adc_compare_config_t *const config)
Gets the current Hardware Compare configuration.
Definition: adc_driver.c:227
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:268
Defines the hardware compare configuration.
Definition: adc_driver.h:236
adc_inputchannel_t channel
Definition: adc_driver.h:270
IRQn_Type
Defines the Interrupt Numbers definitions.
Definition: S32K142.h:192
Defines the control channel configuration.
Definition: adc_driver.h:267