iLLD_TC27xC  1.0
IfxVadc_Adc.c
Go to the documentation of this file.
1 /**
2  * \file IfxVadc_Adc.c
3  * \brief VADC ADC details
4  *
5  * \version iLLD_0_1_0_10
6  * \copyright Copyright (c) 2013 Infineon Technologies AG. All rights reserved.
7  *
8  *
9  * IMPORTANT NOTICE
10  *
11  *
12  * Infineon Technologies AG (Infineon) is supplying this file for use
13  * exclusively with Infineon's microcontroller products. This file can be freely
14  * distributed within development tools that are supporting such microcontroller
15  * products.
16  *
17  * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
18  * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
20  * INFINEON SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL,
21  * OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
22  *
23  */
24 
25 /******************************************************************************/
26 /*----------------------------------Includes----------------------------------*/
27 /******************************************************************************/
28 
29 #include "IfxVadc_Adc.h"
30 #include "_Utilities/Ifx_Assert.h"
31 
32 /** \addtogroup IfxLld_Vadc_Adc_Group
33  * \{ */
34 /******************************************************************************/
35 /*------------------------Inline Function Prototypes--------------------------*/
36 /******************************************************************************/
37 
38 /** \brief Gets the master id
39  * \param slave Index of the group
40  * \param masterIndex master kernel index
41  * \return Master Group Id
42  */
44 
45 /** \brief Gets the current master kernel index.
46  * \param slave Index of the group
47  * \param master Index of the group
48  * \return current master kernel index
49  */
51 
52 /** \} */
53 
54 /** \addtogroup IfxLld_Vadc_Adc_Variables
55  * \{ */
56 
57 /******************************************************************************/
58 /*------------------------Private Variables/Constants-------------------------*/
59 /******************************************************************************/
60 
61 static const IfxVadc_Adc_SYNCTR_STSEL IfxVadc_Adc_masterIndex[8][8] = {
62  /* 0 1 2 3 4 5 6 7 */
63  {0, 1, 2, 3, 0, 0, 0, 0}, /* Grp 0 */
64  {1, 0, 2, 3, 0, 0, 0, 0}, /* Grp 1 */
65  {1, 2, 0, 3, 0, 0, 0, 0}, /* Grp 2 */
66  {1, 2, 3, 0, 0, 0, 0, 0}, /* Grp 3 */
67  {0, 0, 0, 0, 0, 1, 2, 3}, /* Grp 4 */
68  {0, 0, 0, 0, 1, 0, 2, 3}, /* Grp 5 */
69  {0, 0, 0, 0, 1, 2, 0, 3}, /* Grp 6 */
70  {0, 0, 0, 0, 1, 2, 3, 0} /* Grp 7 */
71 };
72 
73 /** \} */
74 
75 /******************************************************************************/
76 /*---------------------Inline Function Implementations------------------------*/
77 /******************************************************************************/
78 
80 {
81  uint8 i, idxOffset;
82  IfxVadc_GroupId masterId = slave;
83 
84  if (masterIndex == 0)
85  {
86  masterId = slave;
87  }
88  else
89  {
90  idxOffset = (slave < 4) ? 0 : 4; /* 4 is the index in the above IfxVadc_Adc_masterIndex about which symmetry is observed */
91 
92  for (i = 0; i < 4; i++)
93  {
94  if (IfxVadc_Adc_masterIndex[slave][i + idxOffset] == masterIndex)
95  {
96  return i + idxOffset;
97  }
98  }
99  }
100 
101  return masterId;
102 }
103 
104 
106 {
107  return IfxVadc_Adc_masterIndex[slave][master];
108 }
109 
110 
111 /******************************************************************************/
112 /*-------------------------Function Implementations---------------------------*/
113 /******************************************************************************/
114 
116 {
117  Ifx_VADC *vadc = IfxVadc_Adc_getVadcFromGroup(group);
118  Ifx_VADC_G *vadcG = IfxVadc_Adc_getGroupRegsFromGroup(group);
119  /* Get group index */
120  IfxVadc_GroupId groupIndex = group->groupId;
121 
122  /* Request Access to configuration registers */
124  IfxVadc_resetGroup(vadcG);
126 }
127 
128 
130 {
131  Ifx_VADC_G *vadcG = IfxVadc_Adc_getGroupRegsFromGroup(channel->group);
132  IfxVadc_ChannelId channelIndex = channel->channel;
133 
134  config->channelId = channel->channel;
135  config->group = channel->group;
136 
137  Ifx_VADC_CHCTR tempChctr;
138  tempChctr.U = 0;
139  tempChctr = IfxVadc_getChannelControlConfig(vadcG, channelIndex);
140 
141  config->inputClass = tempChctr.B.ICLSEL;
142  config->reference = tempChctr.B.REFSEL;
143  config->resultRegister = tempChctr.B.RESREG;
144  config->globalResultUsage = tempChctr.B.RESTBS;
145  config->lowerBoundary = tempChctr.B.BNDSELL;
146  config->upperBoundary = tempChctr.B.BNDSELU;
147  config->boundaryMode = tempChctr.B.BNDSELX;
148  config->limitCheck = tempChctr.B.CHEVMODE;
149  config->synchonize = tempChctr.B.SYNC;
150  config->rightAlignedStorage = tempChctr.B.RESPOS;
151 
152  config->backgroundChannel = ((IfxVadc_getAssignedChannels(vadcG)).U & (1 << channelIndex)) ? FALSE : TRUE;
153 
154  /* Get Channel index */
155  IfxVadc_GroupId groupIndex = channel->group->groupId;
156  uint32 channelServiceRequestNodePtr = ((IfxVadc_getChannelServiceRequestNodePointer(vadcG)).U >> (channel->channel * 4)) & 0xF;
157 
158  volatile Ifx_SRC_SRCR *src = IfxVadc_getSrcAddress(groupIndex, channelServiceRequestNodePtr);
159 
160  if (src->B.SRE == 1)
161  {
162  config->channelSrcNr = channelServiceRequestNodePtr;
163  config->channelPriority = src->B.SRPN;
164  config->channelServProvider = src->B.TOS;
165  }
166  else
167  {
168  config->channelSrcNr = 0;
169  config->channelPriority = 0;
170  config->channelServProvider = 0;
171  }
172 
173  uint32 resultServiceRequestNodePtr;
174 
176  {
177  resultServiceRequestNodePtr = ((IfxVadc_getChannelResultServiceRequestNodePointer0(vadcG)).U >> (channel->channel * 4)) & 0xF;
178  }
179  else
180  {
181  resultServiceRequestNodePtr = ((IfxVadc_getChannelResultServiceRequestNodePointer1(vadcG)).U >> ((channel->channel - IfxVadc_ChannelResult_8) * 4)) & 0xF;
182  }
183 
184  src = IfxVadc_getSrcAddress(groupIndex, resultServiceRequestNodePtr);
185 
186  if (src->B.SRE == 1)
187  {
188  config->resultSrcNr = resultServiceRequestNodePtr;
189  config->resultPriority = src->B.SRPN;
190  config->resultServProvider = src->B.TOS;
191  }
192  else
193  {
194  config->resultSrcNr = 0;
195  config->resultPriority = 0;
196  config->resultServProvider = 0;
197  }
198 }
199 
200 
202 {
203  uint8 inputClassNum;
204  Ifx_VADC_G *vadcG = group->group;
205  Ifx_VADC *vadc = group->module.vadc;
206  float32 analogFrequency = IfxVadc_getAdcAnalogFrequency(vadc);
207 
208  config->groupId = group->groupId;
209  config->module = &group->module;
210 
212 
213  for (inputClassNum = 0; inputClassNum < IFXVADC_NUMBER_OF_INPUTCLASS; inputClassNum++)
214  {
215  config->inputClass[inputClassNum].resolution = IfxVadc_getChannelResolution(vadcG, inputClassNum);
216  config->inputClass[inputClassNum].sampleTime = IfxVadc_getChannelSampleTime(vadcG, inputClassNum, analogFrequency);
217  }
218 
220  {
224 
227 
229  {
231  }
233  {
235  }
236 
239  }
240  else
241  {
244  config->scanRequest.triggerConfig.gatingSource = IfxVadc_GatingSource_0; /* Use CCU6061 TRIG0 */
246  config->scanRequest.triggerConfig.triggerSource = IfxVadc_TriggerSource_0; /* Trigger source taken from Gating Input */
247  }
248 
250  {
254 
257 
259  {
261  }
263  {
265  }
266 
269  }
270  else
271  {
274  config->queueRequest.triggerConfig.gatingSource = IfxVadc_GatingSource_0; /* Use CCU6061 TRIG0 */
276  config->queueRequest.triggerConfig.triggerSource = IfxVadc_TriggerSource_0; /* Trigger source taken from Gating Input */
277  }
278 
280  {
284 
287 
289  {
291  }
293  {
295  }
296 
299  }
300  else
301  {
306  config->backgroundScanRequest.triggerConfig.triggerSource = IfxVadc_TriggerSource_0; /* Trigger source taken from Gating Input */
307  }
308 
310 
311  config->disablePostCalibration = ((IfxVadc_getGlobalConfigValue(vadc)).U >> (IFX_VADC_GLOBCFG_DPCAL0_OFF + group->groupId)) & 0x1;
312 }
313 
314 
316 {
318  Ifx_VADC *vadc = IfxVadc_Adc_getVadcFromGroup(config->group);
319  Ifx_VADC_G *vadcG = IfxVadc_Adc_getGroupRegsFromGroup(config->group);
320 
321  channel->group = config->group;
322  IfxVadc_GroupId groupIndex = channel->group->groupId;
323  IfxVadc_ChannelId channelIndex = config->channelId;
324 
325  /* Request Access to configuration registers */
327 
328  /* Configure Channel */
329  {
330  IfxVadc_setReferenceInput(vadcG, channelIndex, config->reference);
331  IfxVadc_storeGroupResult(vadcG, channelIndex, config->resultRegister);
332  IfxVadc_setLowerBoundary(vadcG, channelIndex, config->lowerBoundary);
333  IfxVadc_setUpperBoundary(vadcG, channelIndex, config->lowerBoundary);
334  IfxVadc_setSyncRequest(vadcG, channelIndex, config->synchonize);
335  IfxVadc_setInputClass(vadcG, channelIndex, config->inputClass);
336  IfxVadc_setChannelLimitCheckMode(vadcG, channelIndex, config->limitCheck);
337  IfxVadc_setResultPosition(vadcG, channelIndex, config->rightAlignedStorage);
338  IfxVadc_setBackgroundResultTarget(vadcG, channelIndex, config->globalResultUsage);
339  IfxVadc_setBoundaryMode(vadcG, channelIndex, config->boundaryMode);
340  }
341 
343 
344  if (config->backgroundChannel == FALSE)
345  {
346  IfxVadc_setGroupPriorityChannel(vadcG, channelIndex);
347  }
348  else
349  {
350  IfxVadc_setBackgroundPriorityChannel(vadcG, channelIndex);
351  }
352 
354 
355  if (config->channelPriority > 0)
356  {
357  volatile Ifx_SRC_SRCR *src = IfxVadc_getSrcAddress(groupIndex, config->channelSrcNr);
358  IfxVadc_setChannelEventSourceLine(vadcG, config->channelSrcNr, channel->channel);
359  IfxVadc_clearChannelRequest(vadcG, config->channelId);
360  IfxSrc_init(src, config->channelServProvider, config->channelPriority);
361  IfxSrc_enable(src);
362  }
363  else
364  {
365  /* do nothing */
366  /* FIXME disable the interrupt generation? */
367  }
368 
369  if (config->resultPriority > 0)
370  {
371  volatile Ifx_SRC_SRCR *src = IfxVadc_getSrcAddress(groupIndex, config->resultSrcNr);
372  IfxVadc_enableFifoMode(vadcG, config->resultRegister);
373 
375  {
377  }
378  else
379  {
381  }
382 
384  IfxSrc_init(src, config->resultServProvider, config->resultPriority);
385  IfxSrc_enable(src);
386  }
387  else
388  {
389  /* do nothing */
390  /* FIXME disable the interrupt generation? */
391  }
392 
394  /* Software initialization */
395  channel->resultreg = config->resultRegister;
396  channel->channel = config->channelId;
397 
398  return Status;
399 }
400 
401 
403 {
404  static const IfxVadc_Adc_ChannelConfig IfxVadc_Adc_defaultChannelConfig = {
406  .group = NULL_PTR,
407  .inputClass = IfxVadc_InputClasses_group0,
409  .resultRegister = IfxVadc_ChannelResult_0,
410  .globalResultUsage = FALSE,
411  .lowerBoundary = IfxVadc_BoundarySelection_group0,
412  .upperBoundary = IfxVadc_BoundarySelection_group0,
413  .boundaryMode = IfxVadc_BoundaryExtension_standard,
414  .limitCheck = IfxVadc_LimitCheck_noCheck,
415  .synchonize = FALSE,
416  .backgroundChannel = FALSE,
417  .rightAlignedStorage = FALSE,
418  .resultPriority = 0,
419  .resultSrcNr = IfxVadc_SrcNr_group0,
420  .resultServProvider = IfxSrc_Tos_cpu0,
421  .channelPriority = 0,
422  .channelSrcNr = IfxVadc_SrcNr_group0,
423  .channelServProvider = IfxSrc_Tos_cpu0
424  };
425  *config = IfxVadc_Adc_defaultChannelConfig;
426  config->group = group;
427 }
428 
429 
431 {
433  Ifx_VADC *vadc = config->module->vadc;
434  Ifx_VADC_G *vadcG = &vadc->G[config->groupId];
435 
436  /* check for write access */
437  group->group = vadcG;
438  group->module = *config->module;
439  IfxVadc_GroupId groupIndex = config->groupId;
440  group->groupId = groupIndex;
441  float32 analogFrequency = IfxVadc_getAdcAnalogFrequency(vadc);
442 
443  uint8 inputClassNum;
444 
445  /* Request Access to configuration registers */
447 
448  if (config->arbiter.requestSlotQueueEnabled == TRUE)
449  {
450  /* Enable Arbiter slot, set Priority and start mode */
452  }
453  else
454  {
455  /* Disable the slot */
457  }
458 
459  if (config->arbiter.requestSlotScanEnabled == TRUE)
460  {
461  /* Setup Arbitration priority and turn on enabled slot */
462  /* Enable Arbiter slot, set Priority and start mode */
464  }
465  else
466  {
467  /* Disable the slot */
469  }
470 
472  {
473  /* Setup Arbitration priority and turn on enabled slot */
474  /* Enable Arbiter slot, set Priority and start mode */
476  }
477  else
478  {
479  /* Disable the slot */
481  }
482 
483  /* master slave configuration */
484  if (config->master != groupIndex)
485  {
486  uint8 masterIndex = IfxVadc_Adc_getMasterKernelIndex(groupIndex, config->master);
487  IfxVadc_setMasterIndex(vadcG, masterIndex);
488  }
489  else
490  {
491  /* do nothing */
492  /* FIXME check if something needs to be done? */
493  }
494 
495  /* Setup arbiter */
496  /* turn off the group during initialization, see UM for sync mode */
498 
500 
501  /* Setup queue request if enabled */
502  if (config->arbiter.requestSlotQueueEnabled == TRUE)
503  {
504  const IfxVadc_Adc_QueueConfig *queueSlot = &config->queueRequest;
505 
506  /* configure external Trigger if enabled */
508  {
509  /* enable external trigger */
512 
513  /* if last input is used the trigger input selection is extend by Gating inputs */
515  {
517  }
518  else
519  {
520  /* do nothing, gating is configured later */
521  }
522  }
523  else
524  {
525  /* disable external trigger */
527  }
528 
529  /* configure Gating if enabled */
531  {
533  }
534  else
535  {
536  /* do nothing, handled by trigger settings */
537  }
538 
539  IfxVadc_clearQueue(vadcG, (queueSlot->flushQueueAfterInit) ? 1 : 0);
540  }
541  else
542  {
543  /* do nothing */
544  }
545 
546  /* Setup scan request if enabled */
547  if (config->arbiter.requestSlotScanEnabled == TRUE)
548  {
549  const IfxVadc_Adc_ScanConfig *scanSlot = &config->scanRequest;
550 
551  /* configure external Trigger if enabled */
553  {
554  /* enable external trigger */
556 
558 
559  if (scanSlot->triggerConfig.triggerSource == IfxVadc_TriggerSource_15) /* if last input is used the trigger input selection is extend by Gating inputs */
560  {
562  }
563  else
564  {
565  /* do nothing, gating is configured later */
566  }
567  }
568  else
569  {
571  }
572 
573  /* configure Gating if enabled */
575  {
577  }
578  else
579  {
580  /* do nothing, handled by trigger settings */
581  }
582 
583  IfxVadc_setAutoScan(vadcG, scanSlot->autoscanEnabled ? 1 : 0);
584  }
585  else
586  {
587  /* do nothing */
588  }
589 
591  {
592  const IfxVadc_Adc_BackgroundScanConfig *backgroundScanSlot = &config->backgroundScanRequest;
593 
594  /* configure external Trigger if enabled */
596  {
598 
600 
601  if (backgroundScanSlot->triggerConfig.triggerSource == IfxVadc_TriggerSource_15) /* if last input is used the trigger input selection is extend by Gating inputs */
602  {
604  }
605  else
606  {
607  /* do nothing, gating is configured later */
608  }
609  }
610  else
611  {
612  /* do nothing */
613  }
614 
615  /* configure Gating if enabled */
616  if ((backgroundScanSlot->triggerConfig.triggerSource != IfxVadc_TriggerSource_15))
617  {
618  IfxVadc_setBackgroundScanSlotGatingConfig(vadc, backgroundScanSlot->triggerConfig.gatingSource, backgroundScanSlot->triggerConfig.gatingMode);
619  }
620  else
621  {
622  /* do nothing, handled by trigger settings */
623  }
624 
625  IfxVadc_setAutoBackgroundScan(vadc, backgroundScanSlot->autoBackgroundScanEnabled ? 1 : 0);
626  }
627  else
628  {
629  /* do nothing */
630  }
631 
632  /* turn on group after initialisation, only in master mode */
634  IfxVadc_setAnalogConvertControl(vadcG, convertMode);
635 
636  /* Post Calibration */
637  IfxVadc_disablePostCalibration(vadc, groupIndex, config->disablePostCalibration);
638 
639  for (inputClassNum = 0; inputClassNum < IFXVADC_NUMBER_OF_INPUTCLASS; inputClassNum++)
640  {
641  /* configure Group input class registers */
642  IfxVadc_setInputClassResolution(vadcG, inputClassNum, config->inputClass[inputClassNum].resolution);
643  /* Calculate Sample time ticks */
644  IfxVadc_setInputClassSampleTimeTicks(vadcG, inputClassNum, analogFrequency, config->inputClass[inputClassNum].sampleTime);
645  }
646 
648 
649  return status;
650 }
651 
652 
654 {
655  static const IfxVadc_Adc_GroupConfig IfxVadc_Adc_defaultGroupConfig = {
656  .arbiter = {
658  .requestSlotQueueEnabled = FALSE,
659  .requestSlotScanEnabled = FALSE,
660  .requestSlotBackgroundScanEnabled = FALSE,
661  },
662  .backgroundScanRequest = {
663  .autoBackgroundScanEnabled = FALSE,
664  .triggerConfig.gatingMode = IfxVadc_GatingMode_disabled,
665  .triggerConfig.triggerMode = IfxVadc_TriggerMode_noExternalTrigger,
666  .triggerConfig.gatingSource = IfxVadc_GatingSource_0,
667  .triggerConfig.triggerSource = IfxVadc_TriggerSource_0,
668  .requestSlotPrio = IfxVadc_RequestSlotPriority_low,
669  .requestSlotStartMode = IfxVadc_RequestSlotStartMode_waitForStart,
670  },
671  .scanRequest = {
672  .autoscanEnabled = FALSE,
673  .triggerConfig.gatingMode = IfxVadc_GatingMode_disabled,
674  .triggerConfig.gatingSource = IfxVadc_GatingSource_0,
675  .triggerConfig.triggerMode = IfxVadc_TriggerMode_noExternalTrigger,
676  .triggerConfig.triggerSource = IfxVadc_TriggerSource_0,
677  .requestSlotPrio = IfxVadc_RequestSlotPriority_low,
678  .requestSlotStartMode = IfxVadc_RequestSlotStartMode_waitForStart,
679  },
680  .queueRequest = {
681  .flushQueueAfterInit = TRUE,
682  .triggerConfig.gatingMode = IfxVadc_GatingMode_disabled,
683  .triggerConfig.gatingSource = IfxVadc_GatingSource_0,
684  .triggerConfig.triggerMode = IfxVadc_TriggerMode_noExternalTrigger,
685  .triggerConfig.triggerSource = IfxVadc_TriggerSource_0,
686  .requestSlotPrio = IfxVadc_RequestSlotPriority_low,
687  .requestSlotStartMode = IfxVadc_RequestSlotStartMode_waitForStart,
688  },
689 
690  .inputClass[0].resolution = IfxVadc_ChannelResolution_12bit,
691  .inputClass[0].sampleTime = 1.0e-6, /* Set sample time to 1us */
692  .inputClass[1].resolution = IfxVadc_ChannelResolution_12bit,
693  .inputClass[1].sampleTime = 1.0e-6, /* Set sample time to 1us */
694  };
695 
696  *config = IfxVadc_Adc_defaultGroupConfig;
697  config->groupId = IfxVadc_GroupId_0;
698  config->module = vadc;
699  config->master = config->groupId;
700  config->disablePostCalibration = FALSE;
701 }
702 
703 
705 {
707  Ifx_VADC *vadcSFR = config->vadc;
708  vadc->vadc = vadcSFR;
709  float32 analogFrequency;
710  uint8 groupInputClassNum, groupNum;
711 
712  /* Enable VADC kernel clock */
713  IfxVadc_enableModule(vadcSFR);
714 
715  /* Set Analog Frequency */
716  if (IfxVadc_initializeFAdcI(vadcSFR, config->analogFrequency) == 0)
717  {
719  }
720  else
721  {
722  /* do nothing */
723  }
724 
725  /* Set digital Frequency */
726  IfxVadc_initializeFAdcD(vadcSFR, config->moduleFrequency);
727 
728  analogFrequency = IfxVadc_getAdcAnalogFrequency(vadcSFR);
729 
730  /* configure Group input class registers */
731  for (groupInputClassNum = 0; groupInputClassNum < IFXVADC_GLOBAL_INPUTCLASS_NUM; groupInputClassNum++)
732  {
733  /* configure ADC channel resolution ( conversion mode ) */
734  IfxVadc_setChannelResolution(vadcSFR, groupInputClassNum, config->inputClass[groupInputClassNum].resolution);
735  /* configure Sample time ticks */
736  IfxVadc_setSampleTimeTicks(vadcSFR, groupInputClassNum, analogFrequency, config->inputClass[groupInputClassNum].sampleTime);
737  }
738 
739  /* Start up calibration is requested */
740  if (config->startupCalibration == TRUE)
741  {
742  /* Ensure that all groups are enabled */
743  for (groupNum = 0; groupNum < IFXVADC_NUM_ADC_GROUPS; groupNum++)
744  {
748  }
749 
750  // execute calibration
752  }
753 
754  return status;
755 }
756 
757 
759 {
760  config->vadc = vadc;
762 
765  config->inputClass[0].sampleTime = 1.0e-6;
767  config->inputClass[1].sampleTime = 1.0e-6;
768  config->startupCalibration = FALSE;
769 }