PDL for FM4  Version1.2
Peripheral Driverl Library for FM4
adc.c
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2013 Spansion LLC. All Rights Reserved.
3 *
4 * This software is owned and published by:
5 * Spansion LLC, 915 DeGuigne Dr. Sunnyvale, CA 94088-3453 ("Spansion").
6 *
7 * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
8 * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
9 *
10 * This software contains source code for use with Spansion
11 * components. This software is licensed by Spansion to be adapted only
12 * for use in systems utilizing Spansion components. Spansion shall not be
13 * responsible for misuse or illegal use of this software for devices not
14 * supported herein. Spansion is providing this software "AS IS" and will
15 * not be responsible for issues arising from incorrect user implementation
16 * of the software.
17 *
18 * SPANSION MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
19 * REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
20 * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
21 * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
22 * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
23 * WARRANTY OF NONINFRINGEMENT.
24 * SPANSION SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
25 * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
26 * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
27 * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
28 * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
30 * SAVINGS OR PROFITS,
31 * EVEN IF SPANSION HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
33 * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
34 * FROM, THE SOFTWARE.
35 *
36 * This software may be replicated in part or whole for the licensed use,
37 * with the restriction that this Disclaimer and Copyright notice must be
38 * included with each copy of this software, whether used in part or whole,
39 * at all times.
40 */
41 /******************************************************************************/
62 /******************************************************************************/
63 /* Include files */
64 /******************************************************************************/
65 #include "adc.h"
66 
67 #if (defined(PDL_PERIPHERAL_ADC_ACTIVE))
68 
74 
75 /******************************************************************************/
76 /* Local pre-processor symbols/macros ('#define') */
77 /******************************************************************************/
78 
79 /******************************************************************************/
80 /* Global variable definitions (declared in header file with 'extern') */
81 /******************************************************************************/
82 
85 {
86  #if (PDL_PERIPHERAL_ENABLE_ADC0 == PDL_ON)
87  {
88  &ADC0, // pstcInstance
89  {0, 0 ,0, 0, 0 ,0} // stcInternData (not initialized yet)
90  },
91  #endif
92  #if (PDL_PERIPHERAL_ENABLE_ADC1 == PDL_ON)
93  {
94  &ADC1, // pstcInstance
95  {0, 0 ,0, 0, 0 ,0} // stcInternData (not initialized yet)
96  },
97  #endif
98  #if (PDL_PERIPHERAL_ENABLE_ADC2 == PDL_ON)
99  {
100  &ADC2, // pstcInstance
101  {0, 0 ,0, 0, 0 ,0} // stcInternData (not initialized yet)
102  }
103  #endif
104 };
105 
106 /******************************************************************************/
107 /* Local type definitions ('typedef') */
108 /******************************************************************************/
109 typedef union
110 {
112  stc_adc_sccr_field_t stcAdcSccrReadout;
113 
115 
116 typedef union
117 {
118  stc_adc_pccr_field_t stcAdcPccrReadout;
120 
122 
123 /******************************************************************************/
124 /* Local function prototypes ('static') */
125 /******************************************************************************/
126 static stc_adc_intern_data_t* AdcGetInternDataPtr(volatile stc_adcn_t* pstcAdc);
127 
128 #if (PDL_INTERRUPT_ENABLE_ADC0 == PDL_ON) || \
129  (PDL_INTERRUPT_ENABLE_ADC1 == PDL_ON) || \
130  (PDL_INTERRUPT_ENABLE_ADC2 == PDL_ON)
131 static void Adc_InitInterrupt(volatile stc_adcn_t* pstcAdc);
132 static void Adc_DeInitInterrupt(volatile stc_adcn_t* pstcAdc);
133 #endif
134 
135 /******************************************************************************/
136 /* Local variable definitions ('static') */
137 /******************************************************************************/
138 
139 /******************************************************************************/
140 /* Function implementation - global ('extern') and local ('static') */
141 /******************************************************************************/
142 
143 /******************************************************************************/
144 /* Local Functions */
145 /******************************************************************************/
146 
157 {
158  uint8_t u8Instance;
159 
160  for (u8Instance = 0u; u8Instance < ADC_INSTANCE_COUNT; u8Instance++)
161  {
162  if (pstcAdc == m_astcAdcInstanceDataLut[u8Instance].pstcInstance)
163  {
164  return &m_astcAdcInstanceDataLut[u8Instance].stcInternData;
165  }
166  }
167 
168  return NULL;
169 }
170 
171 #if (PDL_INTERRUPT_ENABLE_ADC0 == PDL_ON) || \
172  (PDL_INTERRUPT_ENABLE_ADC1 == PDL_ON) || \
173  (PDL_INTERRUPT_ENABLE_ADC2 == PDL_ON)
174 
185 void AdcIrqHandler( volatile stc_adcn_t* pstcAdc,
186  stc_adc_intern_data_t* pstcAdcInternData
187  )
188 {
189  if (pstcAdc->SCCR_f.SOVR) // Error case (FIFO overflow)
190  {
191  pstcAdc->SCCR_f.SOVR = 0u;
192 
193  if (pstcAdcInternData->pfnErrorCallbackAdc != NULL)
194  {
195  pstcAdcInternData->pfnErrorCallbackAdc();
196  }
197  }
198 
199  if (pstcAdc->PCCR_f.POVR) // Error case (Priority FIFO overflow)
200  {
201  pstcAdc->PCCR_f.POVR = 0u;
202 
203  if (pstcAdcInternData->pfnPrioErrorCallbackAdc != NULL)
204  {
205  pstcAdcInternData->pfnPrioErrorCallbackAdc();
206  }
207  }
208 
209  if (pstcAdc->ADCR_f.PCIF) // Priority Conversion
210  {
211  pstcAdc->ADCR_f.PCIF = 0u;
212 
213  if (pstcAdcInternData->pfnPrioConversion != NULL)
214  {
215  // Callback argument points to instance scan FIFO. User has to empty it in
216  // the callback function, because the FIFO depth is known.
217  pstcAdcInternData->pfnPrioConversion((uint32_t*)&pstcAdc->PCFD);
218  }
219  }
220 
221  if (pstcAdc->ADCR_f.SCIF) // Scan conversion interrupt request?
222  {
223  pstcAdc->ADCR_f.SCIF = 0u;
224 
225  if (pstcAdcInternData->pfnScanConversion != NULL)
226  {
227  // Callback argument points to instance FIFO. User has to empty it in
228  // the callback function, because the FIFO depth is known.
229  pstcAdcInternData->pfnScanConversion((uint32_t*)&pstcAdc->SCFD);
230  }
231  }
232 
233  if (pstcAdc->ADCR_f.CMPIF) // Compare result interrupt request?
234  {
235  pstcAdc->ADCR_f.CMPIF = 0u;
236 
237  if (pstcAdcInternData->pfnComparisonCallback != NULL)
238  {
239  pstcAdcInternData->pfnComparisonCallback();
240  }
241  }
242 
243  if (pstcAdc->WCMRCIF_f.RCINT) // Range result interrupt request?
244  {
245  pstcAdc->WCMRCIF_f.RCINT = 0u;
246 
247  if (pstcAdcInternData->pfnRangeCallback != NULL)
248  {
249  pstcAdcInternData->pfnRangeCallback();
250  }
251  }
252 
253  return;
254 } // AdcIrqHandler
255 
263 static void Adc_InitInterrupt(volatile stc_adcn_t* pstcAdc)
264 {
265  if (pstcAdc == (volatile stc_adcn_t*)(&ADC0))
266  {
267  NVIC_ClearPendingIRQ(ADC0_IRQn);
268  NVIC_EnableIRQ(ADC0_IRQn);
269  NVIC_SetPriority(ADC0_IRQn, PDL_IRQ_LEVEL_ADC0);
270  }
271  else if (pstcAdc == (volatile stc_adcn_t*)(&ADC1))
272  {
273  NVIC_ClearPendingIRQ(ADC1_IRQn);
274  NVIC_EnableIRQ(ADC1_IRQn);
275  NVIC_SetPriority(ADC1_IRQn, PDL_IRQ_LEVEL_ADC1);
276  }
277  else if (pstcAdc == (volatile stc_adcn_t*)(&ADC2))
278  {
279  NVIC_ClearPendingIRQ(ADC2_IRQn);
280  NVIC_EnableIRQ(ADC2_IRQn);
281  NVIC_SetPriority(ADC2_IRQn, PDL_IRQ_LEVEL_ADC2);
282  }
283 } // Adc_InitInterrupt
284 
292 static void Adc_DeInitInterrupt(volatile stc_adcn_t* pstcAdc)
293 {
294  if (pstcAdc == (volatile stc_adcn_t*)(&ADC0))
295  {
296  NVIC_ClearPendingIRQ(ADC0_IRQn);
297  NVIC_DisableIRQ(ADC0_IRQn);
298  NVIC_SetPriority(ADC0_IRQn, PDL_DEFAULT_INTERRUPT_LEVEL);
299  }
300  else if (pstcAdc == (volatile stc_adcn_t*)(&ADC1))
301  {
302  NVIC_ClearPendingIRQ(ADC1_IRQn);
303  NVIC_DisableIRQ(ADC1_IRQn);
304  NVIC_SetPriority(ADC1_IRQn, PDL_DEFAULT_INTERRUPT_LEVEL);
305  }
306  else if (pstcAdc == (volatile stc_adcn_t*)(&ADC2))
307  {
308  NVIC_ClearPendingIRQ(ADC2_IRQn);
309  NVIC_DisableIRQ(ADC2_IRQn);
310  NVIC_SetPriority(ADC2_IRQn, PDL_DEFAULT_INTERRUPT_LEVEL);
311  }
312 } // Adc_DeInitInterrupt
313 #endif // #if (PDL_INTERRUPT_ENABLE_ADC0 == PDL_ON) || (PDL_INTERRUPT_ENABLE_ADC1 == PDL_ON) || (PDL_INTERRUPT_ENABLE_ADC2 == PDL_ON)
314 
315 /******************************************************************************/
316 /* Global Functions */
317 /******************************************************************************/
318 
334 en_result_t Adc_Init( volatile stc_adcn_t* pstcAdc,
335  stc_adc_config_t* pstcConfig )
336 {
337  stc_adc_sccr_field_t stcSCCR;
338  stc_adc_pccr_field_t stcPCCR;
339  stc_adc_adst0_field_t stcADST0;
340  stc_adc_adst1_field_t stcADST1;
341  stc_adc_pcis_field_t stcPCIS;
342  stc_adc_cmpcr_field_t stcCMPCR;
343  // Pointer to internal data
344  stc_adc_intern_data_t* pstcAdcInternData ;
345 
346  PDL_ZERO_STRUCT(stcSCCR);
347  PDL_ZERO_STRUCT(stcPCCR);
348  PDL_ZERO_STRUCT(stcADST0);
349  PDL_ZERO_STRUCT(stcADST1);
350  PDL_ZERO_STRUCT(stcPCIS);
351  PDL_ZERO_STRUCT(stcCMPCR);
352 
353 
354  // Check for NULL pointer
355  if ((pstcAdc == NULL) ||
356  (pstcConfig == NULL)
357  )
358  {
359  return ErrorInvalidParameter ;
360  }
361 
362  // Disable ADC in any case first
363  pstcAdc->ADCEN_f.ENBL = 0u;
364 
365  // Clear all relevant registers
366  pstcAdc->ADCR = 0u;
367  pstcAdc->ADSR = 0u;
368  pstcAdc->SCCR = 0u;
369 
370  // Get pointer to internal data structure ...
371  pstcAdcInternData = AdcGetInternDataPtr( pstcAdc ) ;
372  // ... and check for NULL
373  if ( pstcAdcInternData != NULL )
374  {
375 #if (PDL_INTERRUPT_ENABLE_ADC0 == PDL_ON) || \
376  (PDL_INTERRUPT_ENABLE_ADC1 == PDL_ON) || \
377  (PDL_INTERRUPT_ENABLE_ADC2 == PDL_ON)
378  pstcAdcInternData->pfnScanConversion = NULL;
379  pstcAdcInternData->pfnPrioConversion = NULL;
380  pstcAdcInternData->pfnErrorCallbackAdc = NULL;
381  pstcAdcInternData->pfnPrioErrorCallbackAdc = NULL;
382  pstcAdcInternData->pfnComparisonCallback = NULL;
383  pstcAdcInternData->pfnRangeCallback = NULL;
384 #endif // #if (PDL_INTERRUPT_ENABLE_ADC0 == PDL_ON) || (PDL_INTERRUPT_ENABLE_ADC1 == PDL_ON) || (PDL_INTERRUPT_ENABLE_ADC2 == PDL_ON)
385  }
386 
387  // Set ADC input pin configuration
388  FM4_GPIO->ADE = (pstcConfig->u32CannelSelect.u32AD_CHn);
389 
390  // Set Scan Conversion Input Selection Register
391  pstcAdc->SCIS01 = (uint16_t)(0x0000FFFFul & pstcConfig->u32CannelSelect.u32AD_CHn);
392  pstcAdc->SCIS23 = (uint16_t)(0x0000FFFFul & (pstcConfig->u32CannelSelect.u32AD_CHn >> 16u));
393 
394  // Set Sampling Time Selection Register
395  pstcAdc->ADSS01 = (uint16_t)(0x0000FFFFul & pstcConfig->u32SamplingTimeSelect.u32AD_CHn);
396  pstcAdc->ADSS23 = (uint16_t)(0x0000FFFFul & pstcConfig->u32SamplingTimeSelect.u32AD_CHn >> 16u);
397 
398  // Set Sampling Times Config. 0
399  switch (pstcConfig->enSamplingTimeN0)
400  {
401  case Value1:
402  stcADST0.STX0 = 0u;
403  break;
404  case Value4:
405  stcADST0.STX0 = 1u;
406  break;
407  case Value8:
408  stcADST0.STX0 = 2u;
409  break;
410  case Value16:
411  stcADST0.STX0 = 3u;
412  break;
413  case Value32:
414  stcADST0.STX0 = 4u;
415  break;
416  case Value64:
417  stcADST0.STX0 = 5u;
418  break;
419  case Value128:
420  stcADST0.STX0 = 6u;
421  break;
422  case Value256:
423  stcADST0.STX0 = 7u;
424  break;
425  default:
426  return ErrorInvalidParameter;
427  }
428 
429  if (pstcConfig->u8SamplingTime0 > 31u)
430  {
431  return ErrorInvalidParameter;
432  }
433 
434  stcADST0.ST0 = pstcConfig->u8SamplingTime0;
435  pstcAdc->ADST0_f = stcADST0;
436 
437  // Set Sampling Times Config. 1
438  switch (pstcConfig->enSamplingTimeN1)
439  {
440  case Value1:
441  stcADST1.STX1 = 0u;
442  break;
443  case Value4:
444  stcADST1.STX1 = 1u;
445  break;
446  case Value8:
447  stcADST1.STX1 = 2u;
448  break;
449  case Value16:
450  stcADST1.STX1 = 3u;
451  break;
452  case Value32:
453  stcADST1.STX1 = 4u;
454  break;
455  case Value64:
456  stcADST1.STX1 = 5u;
457  break;
458  case Value128:
459  stcADST1.STX1 = 6u;
460  break;
461  case Value256:
462  stcADST1.STX1 = 7u;
463  break;
464  default:
465  return ErrorInvalidParameter;
466  }
467 
468  if (pstcConfig->u8SamplingTime1 > 31u)
469  {
470  return ErrorInvalidParameter;
471  }
472 
473  stcADST1.ST1 = pstcConfig->u8SamplingTime1;
474  pstcAdc->ADST1_f = stcADST1;
475 
476  // Frequency Division for ADC Instance
477  pstcAdc->ADCT = pstcConfig->u8SamplingMultiplier;
478 
479  // MSB, LSB alignment
480  pstcAdc->ADSR_f.FDAS = (TRUE == pstcConfig->bLsbAlignment) ? 1u : 0u;
481 
482  // Conversion mode single, repeated
483  switch (pstcConfig->enScanMode)
484  {
486  stcSCCR.RPT = 0u;
487  break;
489  stcSCCR.RPT = 1u;
490  break;
491  default:
492  return ErrorInvalidParameter;
493  }
494 
495  // Scan Conversion Timer Start Enable and Timer Source
496  if (TRUE == pstcConfig->bScanTimerStartEnable)
497  {
498  stcSCCR.SHEN = 1u;
499 
500  pstcAdc->SCTSL = (uint8_t) pstcConfig->enScanConversionTimerTrigger;
501  }
502 
503  // Update hardware
504  pstcAdc->SCCR_f = stcSCCR;
505 
506  // Scan Conversion FIFO Depth
507  if (pstcConfig->u8ScanFifoDepth > 15u)
508  {
509  return ErrorInvalidParameter;
510  }
511 
512  pstcAdc->SFNS = pstcConfig->u8ScanFifoDepth;
513 
514  // Priority external Trigger Analog Input
515  stcPCCR.ESCE = (TRUE == pstcConfig->bPrioExtTrigEnable) ? 1u : 0u;
516 
517  // Priority external Trigger Start Enable
518  stcPCCR.PEEN = (TRUE == pstcConfig->bPrioExtTrigStartEnable) ? 1u : 0u;
519 
520  // Priority Conversion Timer Start Enable and Timer Source
521  if (TRUE == pstcConfig->bPrioTimerStartEnable)
522  {
523  stcPCCR.PHEN = 1u;
524 
525  pstcAdc->PRTSL = (uint8_t) pstcConfig->enPrioConversionTimerTrigger;
526  }
527 
528  // Update Hardware
529  pstcAdc->PCCR_f = stcPCCR;
530 
531  // Priority Conversion FIFO Stage Count Setup
532  pstcAdc->PFNS = pstcConfig->u8PrioFifoDepth;
533 
534  // Priority Conversion Input Selection
535  if (pstcConfig->u8PrioLevel1AnalogChSel > 7u)
536  {
537  return ErrorInvalidParameter;
538  }
539  stcPCIS.P1A = pstcConfig->u8PrioLevel1AnalogChSel;
540 
541  if (pstcConfig->u8PrioLevel2AnalogChSel > 31u)
542  {
543  return ErrorInvalidParameter;
544  }
545  stcPCIS.P2A = pstcConfig->u8PrioLevel2AnalogChSel;
546 
547  pstcAdc->PCIS_f = stcPCIS;
548 
549  if (TRUE == pstcConfig->bComparisonEnable)
550  {
551  // Comparison Value
552  pstcAdc->CMPD = ((0x0FFCu & pstcConfig->u16ComparisonValue) << 6u);
553 
554  // Comparison Control
555  stcCMPCR.CMPEN = (TRUE == pstcConfig->bComparisonEnable) ? 1u : 0u;
556 
557 #if (PDL_INTERRUPT_ENABLE_ADC0 == PDL_ON) || \
558  (PDL_INTERRUPT_ENABLE_ADC1 == PDL_ON) || \
559  (PDL_INTERRUPT_ENABLE_ADC2 == PDL_ON)
560  stcCMPCR.CMD1 = (TRUE == pstcConfig->bCompIrqEqualGreater) ? 1u : 0u;
561 #endif
562 
563  stcCMPCR.CMD0 = (TRUE == pstcConfig->bCompareAllChannels) ? 1u : 0u;
564 
565  if (pstcConfig->u8ComapreChannel > 31u)
566  {
567  return ErrorInvalidParameter;
568  }
569  stcCMPCR.CCH = pstcConfig->u8ComapreChannel;
570 
571  pstcAdc->CMPCR_f = stcCMPCR;
572  }
573 
574  // Set enable time
575  pstcAdc->ADCEN_f.ENBLTIME = pstcConfig->u8EnableTime;
576 
577  if (TRUE == pstcConfig->bRangeComparisonEnable)
578  {
579  pstcAdc->WCMPDH = ((0x0FFCu & pstcConfig->u16UpperLimitRangeValue) << 6u);
580  pstcAdc->WCMPDL = ((0x0FFCu & pstcConfig->u16LowerLimitRangeValue) << 6u);
581 
582  if (pstcConfig->u8RangeCountValue > 7u)
583  {
584  return ErrorInvalidParameter;
585  }
586  pstcAdc->WCMPCR_f.RCOCD = pstcConfig->u8RangeCountValue;
587 
588  pstcAdc->WCMPCR_f.RCOIRRS = pstcConfig->bOutOfRange;
589  pstcAdc->WCMPSR_f.WCMD = pstcConfig->bRangeCompareAllChannels;
590 
591  if (pstcConfig->u8RangeComapreChannel > 31u)
592  {
593  return ErrorInvalidParameter;
594  }
595  pstcAdc->WCMPSR_f.WCCH = pstcConfig->u8RangeComapreChannel;
596 
597  pstcAdc->WCMPCR_f.RCOE = pstcConfig->bRangeComparisonEnable;
598  }
599 
600  // Set Interrupt Configuration, if ADC Interrupts are enabled in PDL
601 #if (PDL_INTERRUPT_ENABLE_ADC0 == PDL_ON) || \
602  (PDL_INTERRUPT_ENABLE_ADC1 == PDL_ON) || \
603  (PDL_INTERRUPT_ENABLE_ADC2 == PDL_ON)
604 
605  // Interrupt enable bits
606  pstcAdc->ADCR_f.SCIE = (TRUE == pstcConfig->bScanConversionIrqEnable) ? 1u : 0u;
607  pstcAdc->ADCR_f.PCIE = (TRUE == pstcConfig->bPrioConversionIrqEnable) ? 1u : 0u;
608  pstcAdc->ADCR_f.CMPIE = (TRUE == pstcConfig->bConversionCompIrqEnable) ? 1u : 0u;
609  pstcAdc->ADCR_f.OVRIE = (TRUE == pstcConfig->bFifoOverrunIrqEnable) ? 1u : 0u;
610  pstcAdc->WCMPCR_f.RCOIE = (TRUE == pstcConfig->bRangeComparisonIrqEnable) ? 1u : 0u;
611 
612  // Callback pointers
613  pstcAdcInternData->pfnScanConversion = pstcConfig->pfnScanConversion;
614  pstcAdcInternData->pfnPrioConversion = pstcConfig->pfnPrioConversion;
615  pstcAdcInternData->pfnErrorCallbackAdc = pstcConfig->pfnErrorCallbackAdc;
616  pstcAdcInternData->pfnPrioErrorCallbackAdc = pstcConfig->pfnPrioErrorCallbackAdc;
617  pstcAdcInternData->pfnComparisonCallback = pstcConfig->pfnComparisonCallback;
618  pstcAdcInternData->pfnRangeCallback = pstcConfig->pfnRangeCallback;
619 
620  Adc_InitInterrupt(pstcAdc);
621 #endif // #if (PDL_INTERRUPT_ENABLE_ADC0 == PDL_ON) || (PDL_INTERRUPT_ENABLE_ADC1 == PDL_ON) || (PDL_INTERRUPT_ENABLE_ADC2 == PDL_ON)
622 
623  return Ok;
624 } // Adc_Init
625 
637 en_result_t Adc_DeInit( volatile stc_adcn_t* pstcAdc )
638 {
639  // Check for NULL pointer
640  if (pstcAdc == NULL)
641  {
642  return ErrorInvalidParameter ;
643  }
644 
645  pstcAdc->ADCEN = 0u; // Diasble ADC (including ENBL bit)
646 
647 #if (PDL_INTERRUPT_ENABLE_ADC0 == PDL_ON) || \
648  (PDL_INTERRUPT_ENABLE_ADC1 == PDL_ON) || \
649  (PDL_INTERRUPT_ENABLE_ADC2 == PDL_ON)
650 
651  Adc_DeInitInterrupt(pstcAdc); // Disable Interrupts
652 
653 #endif // #if (PDL_INTERRUPT_ENABLE_ADC0 == PDL_ON) || (PDL_INTERRUPT_ENABLE_ADC1 == PDL_ON) || (PDL_INTERRUPT_ENABLE_ADC2 == PDL_ON)
654 
655  pstcAdc->ADCR = 0u;
656  pstcAdc->ADSR = 0u;
657  pstcAdc->SCCR = 0u;
658  pstcAdc->SFNS = 0u;
659  pstcAdc->SCIS01 = 0u;
660  pstcAdc->SCIS23 = 0u;
661  pstcAdc->PCCR = 0u;
662  pstcAdc->PFNS = 0u;
663  pstcAdc->PCFD = 0u;
664  pstcAdc->PCIS = 0u;
665  pstcAdc->CMPD = 0u;
666  pstcAdc->CMPCR = 0u;
667  pstcAdc->ADSS01 = 0u;
668  pstcAdc->ADSS23 = 0u;
669  pstcAdc->ADST0 = 0u;
670  pstcAdc->ADST1 = 0u;
671  pstcAdc->ADCEN = 0u;
672  pstcAdc->WCMPDH = 0u;
673  pstcAdc->WCMPCR = 0u;
674  pstcAdc->WCMPDL = 0u;
675  pstcAdc->WCMPSR = 0u;
676  pstcAdc->WCMRCOT = 0u;
677  pstcAdc->WCMRCIF = 0u;
678  pstcAdc->SCTSL = 0u;
679  pstcAdc->PRTSL = 0u;
680 
681  return Ok;
682 } // Adc_DeInit
683 
697 {
698  uint32_t u32TimeOutCounter = PDL_ADC_READY_WAIT_COUNT;
699 
700  // Check for NULL pointer
701  if (pstcAdc == NULL)
702  {
703  return ErrorInvalidParameter ;
704  }
705 
706  // Enable ADC
707  pstcAdc->ADCEN_f.ENBL = 1u;
708 
709  // Polling for readiness
710  while (u32TimeOutCounter)
711  {
712  if (TRUE == pstcAdc->ADCEN_f.READY)
713  {
714  return Ok;
715  }
716 
717  u32TimeOutCounter--;
718  }
719 
720  return ErrorTimeout;
721 } // Adc_EnableWaitReady
722 
734 en_result_t Adc_Enable( volatile stc_adcn_t* pstcAdc )
735 {
736  // Check for NULL pointer
737  if (pstcAdc == NULL)
738  {
739  return ErrorInvalidParameter ;
740  }
741 
742  // Enable ADC
743  pstcAdc->ADCEN_f.ENBL = 1u;
744 
745  return Ok;
746 } // Adc_Enable
747 
760 en_result_t Adc_Ready( volatile stc_adcn_t* pstcAdc )
761 {
762  // Check for NULL pointer
763  if (pstcAdc == NULL)
764  {
765  return ErrorInvalidParameter ;
766  }
767 
768  if (FALSE == pstcAdc->ADCEN_f.READY)
769  {
770  return ErrorNotReady;
771  }
772 
773  return Ok;
774 } // Adc_Ready
775 
788 {
789  // Check for NULL pointer
790  if (pstcAdc == NULL)
791  {
792  return ErrorInvalidParameter ;
793  }
794 
795  pstcAdc->SCCR_f.SSTR = 1u; // Trigger AD Conversion
796 
797  return Ok;
798 } // Adc_SwStart
799 
812 {
813  // Check for NULL pointer
814  if (pstcAdc == NULL)
815  {
816  return ErrorInvalidParameter ;
817  }
818 
819  pstcAdc->PCCR_f.PSTR = 1u; // Trigger AD Conversion
820 
821  return Ok;
822 }
823 
836 {
837  // Check for NULL pointer
838  if (pstcAdc == NULL)
839  {
840  return ErrorInvalidParameter ;
841  }
842 
843  pstcAdc->ADSR_f.ADSTP = 1u;
844 
845  return Ok;
846 } // Adc_SwStop
847 
861 {
862  // Check for NULL pointer
863  if (pstcAdc == NULL)
864  {
865  return ErrorInvalidParameter ;
866  }
867 
868  if (1u == pstcAdc->ADSR_f.SCS)
869  {
870  return OperationInProgress;
871  }
872 
873  return Ok;
874 } // Adc_ScanStatus
875 
889 {
890  // Check for NULL pointer
891  if (pstcAdc == NULL)
892  {
893  return ErrorInvalidParameter ;
894  }
895 
896  if (1u == pstcAdc->ADSR_f.PCS)
897  {
898  return OperationInProgress;
899  }
900 
901  return Ok;
902 } // Adc_PrioStatus
903 
919 {
920  // Check for NULL pointer
921  if (pstcAdc == NULL)
922  {
923  return ErrorInvalidParameter ;
924  }
925 
926  if (1u == pstcAdc->ADSR_f.PCNS)
927  {
928  return OperationInProgress;
929  }
930 
931  return Ok;
932 } // Adc_Prio2Status
933 
949 {
950  un_adc_sccr_t unAdcSccr;
951  // Check for NULL pointer
952  if (pstcAdc == NULL)
953  {
954  return AdcFifoError;
955  }
956 
957  unAdcSccr.u8AdcSccrReadout = pstcAdc->SCCR; // Get state of FIFO
958 
959  // Analyze read-out state
960  if (1u == unAdcSccr.stcAdcSccrReadout.SOVR)
961  {
962  return AdcFifoOverrun;
963  }
964 
965  if (1u == unAdcSccr.stcAdcSccrReadout.SFUL)
966  {
967  return AdcFifoFull;
968  }
969 
970  if (0u == unAdcSccr.stcAdcSccrReadout.SEMP)
971  {
972  return AdcFifoFilled;
973  }
974 
975  if (1u == unAdcSccr.stcAdcSccrReadout.SEMP)
976  {
977  return AdcFifoEmpty;
978  }
979 
980  return AdcFifoError; // Unknown error
981 } // Adc_ScanFifioStatus
982 
995 {
996  // Check for NULL pointer
997  if (pstcAdc == NULL)
998  {
999  return ErrorInvalidParameter;
1000  }
1001 
1002  pstcAdc->SCCR_f.SFCLR = 1u;
1003 
1004  return Ok;
1005 } // Adc_ScanFifioClear
1006 
1022 {
1023  un_adc_pccr_t unAdcPccr;
1024 
1025  // Check for NULL pointer
1026  if (pstcAdc == NULL)
1027  {
1028  return AdcFifoError;
1029  }
1030 
1031  unAdcPccr.u8AdcPccrReadout = pstcAdc->PCCR; // Get state of FIFO
1032 
1033  // Analyze read-out state
1034  if (1u == unAdcPccr.stcAdcPccrReadout.POVR)
1035  {
1036  return AdcFifoOverrun;
1037  }
1038 
1039  if (1u == unAdcPccr.stcAdcPccrReadout.PFUL)
1040  {
1041  return AdcFifoFull;
1042  }
1043 
1044  if (0u == unAdcPccr.stcAdcPccrReadout.PEMP)
1045  {
1046  return AdcFifoFilled;
1047  }
1048 
1049  if (1u == unAdcPccr.stcAdcPccrReadout.PEMP)
1050  {
1051  return AdcFifoEmpty;
1052  }
1053 
1054  return AdcFifoError; // Unknown error
1055 } // Adc_PrioFifioStatus
1056 
1069 {
1070  // Check for NULL pointer
1071  if (pstcAdc == NULL)
1072  {
1073  return ErrorInvalidParameter;
1074  }
1075 
1076  pstcAdc->PCCR_f.PFCLR = 1u;
1077 
1078  return Ok;
1079 } // Adc_PrioFifioClear
1080 
1095 uint32_t Adc_ReadScanFifo( volatile stc_adcn_t* pstcAdc )
1096 {
1097  // Check for NULL pointer
1098  if (pstcAdc == NULL)
1099  {
1100  return 0xFFFFFFFFu;
1101  }
1102 
1103  return pstcAdc->SCFD;
1104 } // Adc_ReadScanFifo
1105 
1119 uint8_t Adc_GetScanChannel( uint32_t u32FifoData )
1120 {
1121  return (uint8_t) (0x0000001Fu & u32FifoData);
1122 } // Adc_GetScanChannel
1123 
1138 {
1139  if (0u == (0x00001000u & u32FifoData))
1140  {
1141  return AdcFifoDataValid;
1142  }
1143 
1144  return AdcFifoDataInvalid;
1145 } // Adc_GetScanDataValid
1146 
1162 {
1163  if (0x00000100u == (0x00000300u & u32FifoData))
1164  {
1165  return AdcFifoSoftwareStart;
1166  }
1167 
1168  if (0x00000200u == (0x00000300u & u32FifoData))
1169  {
1170  return AdcFifoTimerStart;
1171  }
1172 
1173  return AdcFifoErrorStart;
1174 } // Adc_GetPDataCause
1175 
1190 uint32_t Adc_ReadPrioFifo( volatile stc_adcn_t* pstcAdc )
1191 {
1192  // Check for NULL pointer
1193  if (pstcAdc == NULL)
1194  {
1195  return 0xFFFFFFFFu;
1196  }
1197 
1198  return pstcAdc->PCFD;
1199 } // Adc_ReadPrioFifo
1200 
1214 uint8_t Adc_GetPrioChannel( uint32_t u32FifoData )
1215 {
1216  return (uint8_t) (0x0000001Fu & u32FifoData);
1217 } // Adc_GetPrioChannel
1218 
1233 {
1234  if (0u == (0x00001000u & u32FifoData))
1235  {
1236  return AdcFifoDataValid;
1237  }
1238 
1239  return AdcFifoDataInvalid;
1240 } // Adc_GetPrioDataValid
1241 
1258 {
1259  if (0x00000100u == (0x00000700u & u32FifoData))
1260  {
1261  return AdcFifoSoftwareStart;
1262  }
1263 
1264  if (0x00000200u == (0x00000700u & u32FifoData))
1265  {
1266  return AdcFifoTimerStart;
1267  }
1268 
1269  if (0x00000400u == (0x00000700u & u32FifoData))
1270  {
1271  return AdcFifoExternalTrigger;
1272  }
1273 
1274  return AdcFifoErrorStart;
1275 } // Adc_GetPrioDataCause
1276 
1290 {
1291  // Check for NULL pointer
1292  if (pstcAdc == NULL)
1293  {
1294  return AdcRangeResultError;
1295  }
1296 
1297  if (1u == pstcAdc->WCMRCIF_f.RCINT)
1298  {
1299  return AdcRangeResultValid;
1300  }
1301 
1302  return AdcRangeResultInvalid;
1303 } // Adc_GetRangeResultValid
1304 
1306 
1307 #endif // #if (defined(PDL_PERIPHERAL_ADC_ACTIVE))
1308 
1309 /******************************************************************************/
1310 /* EOF (not truncated) */
1311 /******************************************************************************/