]> rtime.felk.cvut.cz Git - arc.git/blob - arch/ppc/mpc55xx/drivers/Adc_560x.c
Splitting of Adc to eQADC and Adc_560x
[arc.git] / arch / ppc / mpc55xx / drivers / Adc_560x.c
1 /* -------------------------------- Arctic Core ------------------------------\r
2  * Arctic Core - the open source AUTOSAR platform http://arccore.com\r
3  *\r
4  * Copyright (C) 2009  ArcCore AB <contact@arccore.com>\r
5  *\r
6  * This source code is free software; you can redistribute it and/or modify it\r
7  * under the terms of the GNU General Public License version 2 as published by the\r
8  * Free Software Foundation; See <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>.\r
9  *\r
10  * This program is distributed in the hope that it will be useful, but\r
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\r
13  * for more details.\r
14  * -------------------------------- Arctic Core ------------------------------*/\r
15 \r
16 #include <assert.h>\r
17 #include <stdlib.h>\r
18 //#include "System.h"\r
19 #include "mpc55xx.h"\r
20 #include "Modules.h"\r
21 #include "Mcu.h"\r
22 #include "Adc.h"\r
23 #include "Det.h"\r
24 #include "Os.h"\r
25 #include "isr.h"\r
26 #include "irq.h"\r
27 #include "arc.h"\r
28 \r
29 /* Uncomment and use DMA for 5606 only if you now what you are doing */\r
30 #define DONT_USE_DMA_IN_ADC_MPC560X\r
31 \r
32 /* Are we gonna use Dma? */\r
33 #if ( defined(CFG_MPC5606S) && !defined(DONT_USE_DMA_IN_ADC_MPC560X) )\r
34         #define ADC_USES_DMA\r
35         #include "Dma.h"\r
36 #endif\r
37 \r
38 #if ( defined(ADC_USES_DMA) && !defined(USE_DMA) )\r
39         #error Adc is configured to use Dma but the module is not enabled.\r
40 #endif\r
41 \r
42 #define ADC_GROUP0              0\r
43 \r
44 typedef enum\r
45 {\r
46   ADC_UNINIT,\r
47   ADC_INIT,\r
48 }Adc_StateType;\r
49 \r
50 /* Function prototypes. */\r
51 static void Adc_ConfigureADC (const Adc_ConfigType *ConfigPtr);\r
52 static void Adc_ConfigureADCInterrupts (void);\r
53 \r
54 void Adc_GroupConversionComplete (Adc_GroupType group);\r
55 \r
56 /* Development error checking. */\r
57 static Std_ReturnType Adc_CheckReadGroup (Adc_GroupType group);\r
58 static Std_ReturnType Adc_CheckStartGroupConversion (Adc_GroupType group);\r
59 static Std_ReturnType Adc_CheckStopGroupConversion (Adc_GroupType group);\r
60 static Std_ReturnType Adc_CheckInit (const Adc_ConfigType *ConfigPtr);\r
61 #if (ADC_DEINIT_API == STD_ON)\r
62 static Std_ReturnType Adc_CheckDeInit (void);\r
63 #endif\r
64 static Std_ReturnType Adc_CheckSetupResultBuffer (Adc_GroupType group);\r
65 static Std_ReturnType Adc_CheckGetStreamLastPointer (Adc_GroupType group);\r
66 \r
67 /* static variable declarations */\r
68 static Adc_StateType adcState = ADC_UNINIT;\r
69 static const Adc_ConfigType *AdcConfigPtr;      /* Pointer to configuration structure. */\r
70 \r
71 /* Validate functions used for development error check */\r
72 #if ( ADC_DEV_ERROR_DETECT == STD_ON )\r
73 Std_ReturnType ValidateInit(Adc_APIServiceIDType api)\r
74 {\r
75         Std_ReturnType res = E_OK;\r
76         if(!(ADC_INIT == adcState)) {\r
77                 Det_ReportError(MODULE_ID_ADC,0,api,ADC_E_UNINIT );\r
78                 res = E_NOT_OK;\r
79         }\r
80         return res;\r
81 }\r
82 Std_ReturnType ValidateGroup(Adc_GroupType group,Adc_APIServiceIDType api)\r
83 {\r
84         Std_ReturnType res = E_OK;\r
85         if(!((group >= 0) && (group < AdcConfig->nbrOfGroups))) {\r
86                 Det_ReportError(MODULE_ID_ADC,0,api,ADC_E_PARAM_GROUP );\r
87                 res = E_NOT_OK;\r
88         }\r
89         return res;\r
90 }\r
91 #endif\r
92 \r
93 #if (ADC_DEINIT_API == STD_ON)\r
94 Std_ReturnType Adc_DeInit (const Adc_ConfigType *ConfigPtr)\r
95 {\r
96   (void)ConfigPtr;\r
97 \r
98   if (E_OK == Adc_CheckDeInit())\r
99   {\r
100     for(Adc_GroupType group = ADC_GROUP0; group < AdcConfigPtr->nbrOfGroups; group++)\r
101     {\r
102       /* Set group status to idle. */\r
103       AdcConfigPtr->groupConfigPtr[group].status->groupStatus = ADC_IDLE;\r
104     }\r
105 \r
106     /* Disable DMA transfer*/\r
107 #ifndef CFG_MPC5604B\r
108     ADC_0.DMAE.B.DMAEN = 0;\r
109 #endif\r
110     /* Power down ADC */\r
111     ADC_0.MCR.R = 0x0001;\r
112 \r
113     /* Disable all interrupt*/\r
114     ADC_0.IMR.R = 0;\r
115 \r
116     /* Clean internal status. */\r
117     AdcConfigPtr = (Adc_ConfigType *)NULL;\r
118     adcState = ADC_UNINIT;\r
119   }\r
120 \r
121   return (E_OK);\r
122 }\r
123 #endif\r
124 \r
125 Std_ReturnType Adc_Init (const Adc_ConfigType *ConfigPtr)\r
126 {\r
127   if (E_OK == Adc_CheckInit(ConfigPtr))\r
128   {\r
129             /* First of all, store the location of the configuration data. */\r
130             AdcConfigPtr = ConfigPtr;\r
131 \r
132             /* Enable ADC. */\r
133              Adc_ConfigureADC(ConfigPtr);\r
134 \r
135              Adc_ConfigureADCInterrupts();\r
136 \r
137             /* Move on to INIT state. */\r
138             adcState = ADC_INIT;\r
139             return E_OK;\r
140   }\r
141   else\r
142   {\r
143     return E_NOT_OK;\r
144   }\r
145 }\r
146 \r
147 Std_ReturnType Adc_SetupResultBuffer (Adc_GroupType group, Adc_ValueGroupType *bufferPtr)\r
148 {\r
149   Std_ReturnType returnValue = E_NOT_OK;\r
150 \r
151   /* Check for development errors. */\r
152   if (E_OK == Adc_CheckSetupResultBuffer (group))\r
153   {\r
154     AdcConfigPtr->groupConfigPtr[group].status->resultBufferPtr = bufferPtr;\r
155     \r
156     returnValue = E_OK;\r
157   }\r
158 \r
159   return (returnValue);\r
160 }\r
161 \r
162 Adc_StreamNumSampleType Adc_GetStreamLastPointer(Adc_GroupType group, Adc_ValueGroupType** PtrToSamplePtr)\r
163 {\r
164         Adc_StreamNumSampleType nofSample = 0;\r
165         Adc_GroupDefType *groupPtr = (Adc_GroupDefType *)&AdcConfigPtr->groupConfigPtr[group];\r
166         \r
167         /** @req ADC216 */\r
168         /* Check for development errors. */\r
169         if ( (E_OK == Adc_CheckGetStreamLastPointer (group)) &&\r
170                  (groupPtr->status->groupStatus != ADC_BUSY) )\r
171         {\r
172             /* Set resultPtr to application buffer. */\r
173                 if(groupPtr->status->currSampleCount > 0){\r
174                         *PtrToSamplePtr = &groupPtr->status->resultBufferPtr[groupPtr->status->currSampleCount-1];\r
175                 }\r
176 \r
177             if ((ADC_CONV_MODE_ONESHOT == groupPtr->conversionMode) &&\r
178                 (ADC_STREAM_COMPLETED  == groupPtr->status->groupStatus))\r
179             {\r
180                         /** @req ADC327. */\r
181                         groupPtr->status->groupStatus = ADC_IDLE;\r
182             }\r
183             else if ((ADC_CONV_MODE_CONTINOUS == groupPtr->conversionMode) &&\r
184                      (ADC_ACCESS_MODE_STREAMING == groupPtr->accessMode) &&\r
185                      (ADC_STREAM_BUFFER_LINEAR == groupPtr->streamBufferMode) &&\r
186                      (ADC_STREAM_COMPLETED    == groupPtr->status->groupStatus))\r
187             {\r
188                         /** @req ADC327. */\r
189                         groupPtr->status->groupStatus = ADC_IDLE;\r
190             }\r
191             else if ( (ADC_CONV_MODE_CONTINOUS == groupPtr->conversionMode) &&\r
192                       ((ADC_STREAM_COMPLETED    == groupPtr->status->groupStatus) ||\r
193                        (ADC_COMPLETED           == groupPtr->status->groupStatus)) )\r
194             {\r
195                 /* Restart continous mode, and reset result buffer */\r
196                 if ((ADC_CONV_MODE_CONTINOUS == groupPtr->conversionMode) &&\r
197                     (ADC_STREAM_COMPLETED    == groupPtr->status->groupStatus))\r
198                 {\r
199                           /* Start continous conversion again */\r
200                         Adc_StartGroupConversion(group);\r
201                 }\r
202                         /** @req ADC326 */\r
203                         /** @req ADC328 */\r
204             }\r
205             else{/* Keep status. */}\r
206         }\r
207         else\r
208         {\r
209                 /* Some condition not met */\r
210                 *PtrToSamplePtr = NULL;\r
211         }\r
212 \r
213         return nofSample;\r
214 \r
215 }\r
216 \r
217 #if (ADC_READ_GROUP_API == STD_ON)\r
218 Std_ReturnType Adc_ReadGroup (Adc_GroupType group, Adc_ValueGroupType *dataBufferPtr)\r
219 {\r
220   Std_ReturnType returnValue = E_OK;\r
221   uint8_t channel;\r
222   Adc_GroupDefType *groupPtr = (Adc_GroupDefType *)&AdcConfigPtr->groupConfigPtr[group];\r
223 \r
224   if (E_OK == Adc_CheckReadGroup (group))\r
225   {\r
226     /* Copy the result to application buffer. */\r
227     for (channel = 0; channel < groupPtr->numberOfChannels; channel++)\r
228         {\r
229                 if(groupPtr->status->currSampleCount > 0){\r
230                         dataBufferPtr[channel] = (&(groupPtr->status->resultBufferPtr[groupPtr->status->currSampleCount-1]))[channel];\r
231                 }else{\r
232                         dataBufferPtr[channel] = groupPtr->status->resultBufferPtr[channel];\r
233                 }\r
234         }\r
235 \r
236     if ((ADC_CONV_MODE_ONESHOT == groupPtr->conversionMode) &&\r
237         (ADC_STREAM_COMPLETED  == groupPtr->status->groupStatus))\r
238     {\r
239                 /** @req ADC330. */\r
240                 groupPtr->status->groupStatus = ADC_IDLE;\r
241     }\r
242     else if ((ADC_CONV_MODE_CONTINOUS == groupPtr->conversionMode) &&\r
243              (ADC_STREAM_BUFFER_LINEAR == groupPtr->streamBufferMode) &&\r
244              (ADC_ACCESS_MODE_STREAMING == groupPtr->accessMode) &&\r
245              (ADC_STREAM_COMPLETED    == groupPtr->status->groupStatus))\r
246     {\r
247                 /** @req ADC330. */\r
248                 groupPtr->status->groupStatus = ADC_IDLE;\r
249     }\r
250     else if ((ADC_CONV_MODE_CONTINOUS == groupPtr->conversionMode) &&\r
251              ((ADC_STREAM_COMPLETED    == groupPtr->status->groupStatus) ||\r
252               (ADC_COMPLETED           == groupPtr->status->groupStatus)))\r
253     {\r
254         /** @req ADC329 */\r
255       /* Restart continous mode, and reset result buffer */\r
256       if ((ADC_CONV_MODE_CONTINOUS == groupPtr->conversionMode) &&\r
257           (ADC_STREAM_COMPLETED    == groupPtr->status->groupStatus))\r
258       {\r
259                   /* Start continous conversion again */\r
260         Adc_StartGroupConversion(group);\r
261       }\r
262       /** @req ADC329 */\r
263       /** @req ADC331 */\r
264     }\r
265     else{/* Keep status. */}\r
266   }\r
267   else\r
268   {\r
269     /* An error have been raised from Adc_CheckReadGroup(). */\r
270     returnValue = E_NOT_OK;\r
271   }\r
272 \r
273   return (returnValue);\r
274 }\r
275 #endif\r
276 \r
277 Adc_StatusType Adc_GetGroupStatus (Adc_GroupType group)\r
278 {\r
279         Adc_StatusType returnValue;\r
280 \r
281 #if ( ADC_DEV_ERROR_DETECT == STD_ON )\r
282         if( (ValidateInit(ADC_GETGROUPSTATUS_ID) == E_NOT_OK) ||\r
283                 (ValidateGroup(group, ADC_GETGROUPSTATUS_ID) == E_NOT_OK))\r
284         {\r
285                 returnValue = ADC_IDLE;\r
286         }\r
287         else\r
288         {\r
289                 returnValue = AdcConfigPtr->groupConfigPtr[group].status->groupStatus;\r
290         }\r
291 #else\r
292   returnValue = AdcConfigPtr->groupConfigPtr[group].status->groupStatus;\r
293 #endif\r
294   return (returnValue);\r
295 }\r
296 \r
297 void Adc_GroupConversionComplete (Adc_GroupType group)\r
298 {\r
299         Adc_GroupDefType *adcGroup = (Adc_GroupDefType *)&AdcConfigPtr->groupConfigPtr[group];\r
300 \r
301   if(ADC_ACCESS_MODE_SINGLE == adcGroup->accessMode )\r
302   {\r
303           adcGroup->status->groupStatus = ADC_STREAM_COMPLETED;\r
304           /* Call notification if enabled. */\r
305         #if (ADC_GRP_NOTIF_CAPABILITY == STD_ON)\r
306           if (adcGroup->status->notifictionEnable && adcGroup->groupCallback != NULL)\r
307           {\r
308                   adcGroup->groupCallback();\r
309           }\r
310         #endif\r
311                   /* Disable trigger normal conversions for ADC0 */\r
312                   ADC_0.MCR.B.NSTART=0;\r
313   }\r
314   else\r
315   {\r
316         if(ADC_STREAM_BUFFER_LINEAR == adcGroup->streamBufferMode)\r
317         {\r
318                 adcGroup->status->currSampleCount++;\r
319                 if(adcGroup->status->currSampleCount < adcGroup->streamNumSamples)\r
320                 {\r
321                   adcGroup->status->currResultBufPtr += adcGroup->numberOfChannels;\r
322                   adcGroup->status->groupStatus = ADC_COMPLETED;\r
323 \r
324 #if defined (ADC_USES_DMA)\r
325                   /* Increase current result buffer ptr */\r
326                 Dma_ConfigureDestinationAddress((uint32_t)adcGroup->status->currResultBufPtr, adcGroup->dmaResultChannel);\r
327 #endif\r
328 \r
329                 ADC_0.IMR.B.MSKECH = 1;\r
330             ADC_0.MCR.B.NSTART=1;\r
331                 }\r
332                 else\r
333                 {\r
334                   /* All sample completed. */\r
335                   adcGroup->status->groupStatus = ADC_STREAM_COMPLETED;\r
336 \r
337                   /* Call notification if enabled. */\r
338                 #if (ADC_GRP_NOTIF_CAPABILITY == STD_ON)\r
339                   if (adcGroup->status->notifictionEnable && adcGroup->groupCallback != NULL){\r
340                         adcGroup->groupCallback();\r
341                   }\r
342                 #endif\r
343                   /* Disable trigger normal conversions for ADC0 */\r
344                   ADC_0.MCR.B.NSTART=0;\r
345                 }\r
346         }\r
347         else if(ADC_STREAM_BUFFER_CIRCULAR == adcGroup->streamBufferMode)\r
348         {\r
349                 adcGroup->status->currSampleCount++;\r
350                 if(adcGroup->status->currSampleCount < adcGroup->streamNumSamples)\r
351                 {\r
352                         adcGroup->status->currResultBufPtr += adcGroup->numberOfChannels;\r
353 #if defined (ADC_USES_DMA)\r
354                         /* Increase current result buffer ptr */\r
355                         Dma_ConfigureDestinationAddress((uint32_t)adcGroup->status->currResultBufPtr, adcGroup->dmaResultChannel);\r
356 #endif\r
357                         adcGroup->status->groupStatus = ADC_COMPLETED;\r
358 \r
359                         ADC_0.IMR.B.MSKECH = 1;\r
360                     ADC_0.MCR.B.NSTART=1;\r
361                 }\r
362                 else\r
363                 {\r
364                   /* Sample completed. */\r
365                   /* Disable trigger normal conversions for ADC*/\r
366                   ADC_0.MCR.B.NSTART=0;\r
367                   adcGroup->status->groupStatus = ADC_STREAM_COMPLETED;\r
368                   /* Call notification if enabled. */\r
369                 #if (ADC_GRP_NOTIF_CAPABILITY == STD_ON)\r
370                   if (adcGroup->status->notifictionEnable && adcGroup->groupCallback != NULL)\r
371                   {\r
372                           adcGroup->groupCallback();\r
373                   }\r
374                 #endif\r
375                 }\r
376         }\r
377         else\r
378         {\r
379                 //nothing to do.\r
380         }\r
381   }\r
382 }\r
383 void Adc_Group0ConversionComplete (void)\r
384 {\r
385         /* Clear ECH Flag and disable interruput */\r
386         ADC_0.ISR.B.ECH = 1;\r
387         ADC_0.IMR.B.MSKECH = 0;\r
388 \r
389         // Check which group is busy, only one is allowed to be busy at a time in a hw unit\r
390         for (int group = 0; group < ADC_NBR_OF_GROUPS; group++)\r
391         {\r
392           if((AdcConfigPtr->groupConfigPtr[group].status->groupStatus == ADC_BUSY) ||\r
393        (AdcConfigPtr->groupConfigPtr[group].status->groupStatus == ADC_COMPLETED))\r
394           {\r
395 #if !defined (ADC_USES_DMA)\r
396                 /* Copy to result buffer */\r
397                 for(uint8 index=0; index < AdcConfigPtr->groupConfigPtr[group].numberOfChannels; index++)\r
398                 {\r
399 #if defined(CFG_MPC5606S)\r
400                         AdcConfigPtr->groupConfigPtr[group].status->currResultBufPtr[index] = ADC_0.CDR[32+AdcConfigPtr->groupConfigPtr[group].channelList[index]].B.CDATA;\r
401 #else\r
402                         AdcConfigPtr->groupConfigPtr[group].status->currResultBufPtr[index] = ADC_0.CDR[AdcConfigPtr->groupConfigPtr[group].channelList[index]].B.CDATA;\r
403 #endif\r
404                 }\r
405 #endif\r
406 \r
407             Adc_GroupConversionComplete((Adc_GroupType)group);\r
408                 break;\r
409           }\r
410         }\r
411 }\r
412 \r
413 void Adc_WatchdogError (void){\r
414 }\r
415 void Adc_ADCError (void){\r
416 }\r
417 \r
418 static void  Adc_ConfigureADC (const Adc_ConfigType *ConfigPtr)\r
419 {\r
420   /* Set ADC CLOCK */\r
421   ADC_0.MCR.B.ADCLKSEL = ConfigPtr->hwConfigPtr->adcPrescale;\r
422 \r
423   ADC_0.DSDR.B.DSD = 254;\r
424 \r
425   /* Power on ADC */\r
426   ADC_0.MCR.B.PWDN = 0;\r
427 \r
428 #if defined(ADC_USES_DMA)\r
429   /* Enable DMA. */\r
430   ADC_0.DMAE.B.DMAEN = 1;\r
431 #endif\r
432 }\r
433 \r
434 void Adc_ConfigureADCInterrupts (void)\r
435 {\r
436         ISR_INSTALL_ISR2(  "Adc_Err", Adc_ADCError, ADC_ER_INT,     2, 0 );\r
437         ISR_INSTALL_ISR2(  "Adc_Grp", Adc_Group0ConversionComplete, ADC_EOC_INT,     2, 0 );\r
438         ISR_INSTALL_ISR2(  "Adc_Wdg", Adc_WatchdogError, ADC_WD_INT,     2, 0 );\r
439 }\r
440 \r
441 #if (ADC_ENABLE_START_STOP_GROUP_API == STD_ON)\r
442 void Adc_StartGroupConversion (Adc_GroupType group)\r
443 {\r
444         Adc_GroupDefType *groupPtr = (Adc_GroupDefType *)&AdcConfigPtr->groupConfigPtr[group];\r
445 \r
446         /* Run development error check. */\r
447         if (E_OK == Adc_CheckStartGroupConversion (group))\r
448         {\r
449                 /* Disable trigger normal conversions for ADC0 */\r
450                 ADC_0.MCR.B.NSTART = 0;\r
451 \r
452                 /* Set group state to BUSY. */\r
453                 groupPtr->status->groupStatus = ADC_BUSY;\r
454 \r
455                 groupPtr->status->currSampleCount = 0;\r
456                 groupPtr->status->currResultBufPtr = groupPtr->status->resultBufferPtr; /* Set current result buffer */\r
457 \r
458 #if defined(ADC_USES_DMA)\r
459                 Dma_ConfigureChannel ((Dma_TcdType *)groupPtr->groupDMAResults, groupPtr->dmaResultChannel);\r
460                 Dma_ConfigureDestinationAddress ((uint32_t)groupPtr->status->currResultBufPtr, groupPtr->dmaResultChannel);\r
461 #endif\r
462                 /* Always use single shot in streaming mode */\r
463                 if( groupPtr->accessMode == ADC_ACCESS_MODE_STREAMING)\r
464                 {\r
465                         /* Set conversion mode. */\r
466                         ADC_0.MCR.B.MODE = ADC_CONV_MODE_ONESHOT;\r
467                 }\r
468                 else\r
469                 {\r
470                         /* Set conversion mode. */\r
471                         ADC_0.MCR.B.MODE = groupPtr->conversionMode;\r
472                 }\r
473 \r
474                 /* Enable Overwrite*/\r
475                 ADC_0.MCR.B.OWREN = 1;\r
476 \r
477                 /* Set Conversion Time. */\r
478 #if defined(CFG_MPC5606S)\r
479                 uint32 groupChannelIdMask = 0;\r
480 \r
481                 ADC_0.CTR[1].B.INPLATCH = groupPtr->adcChannelConvTime.INPLATCH;\r
482                 ADC_0.CTR[1].B.INPCMP = groupPtr->adcChannelConvTime.INPCMP;\r
483                 ADC_0.CTR[1].B.INPSAMP = groupPtr->adcChannelConvTime.INPSAMP;\r
484 \r
485                 for(uint8 i =0; i < groupPtr->numberOfChannels; i++)\r
486                 {\r
487                         groupChannelIdMask |= (1 << groupPtr->channelList[i]);\r
488                 }\r
489 \r
490 #if defined(ADC_USES_DMA)\r
491                 ADC_0.DMAE.R = 0x01;\r
492                 /* Enable DMA Transfer */\r
493                 ADC_0.DMAR[1].R = groupChannelIdMask;\r
494                 Dma_StartChannel(DMA_ADC_GROUP0_RESULT_CHANNEL);        /* Enable EDMA channel for ADC */\r
495 #endif\r
496 \r
497                 /* Enable Normal conversion */\r
498                 ADC_0.NCMR[1].R = groupChannelIdMask;\r
499 \r
500                 /* Enable Channel Interrupt */\r
501                 ADC_0.CIMR[1].R = groupChannelIdMask;\r
502 \r
503 #else\r
504                 uint32 groupChannelIdMask[3] = {0,0,0};\r
505 \r
506                 ADC_0.CTR[0].B.INPLATCH = groupPtr->adcChannelConvTime.INPLATCH;\r
507                 ADC_0.CTR[0].B.INPCMP = groupPtr->adcChannelConvTime.INPCMP;\r
508                 ADC_0.CTR[0].B.INPSAMP = groupPtr->adcChannelConvTime.INPSAMP;\r
509                 ADC_0.CTR[1].B.INPLATCH = groupPtr->adcChannelConvTime.INPLATCH;\r
510                 ADC_0.CTR[1].B.INPCMP = groupPtr->adcChannelConvTime.INPCMP;\r
511                 ADC_0.CTR[1].B.INPSAMP = groupPtr->adcChannelConvTime.INPSAMP;\r
512                 ADC_0.CTR[2].B.INPLATCH = groupPtr->adcChannelConvTime.INPLATCH;\r
513                 ADC_0.CTR[2].B.INPCMP = groupPtr->adcChannelConvTime.INPCMP;\r
514                 ADC_0.CTR[2].B.INPSAMP = groupPtr->adcChannelConvTime.INPSAMP;\r
515 \r
516                 for(uint8 i =0; i < groupPtr->numberOfChannels; i++)\r
517                 {\r
518                         if(groupPtr->channelList[i] <= 15){\r
519                                 groupChannelIdMask[0] |= (1 << groupPtr->channelList[i]);\r
520                         }else if((groupPtr->channelList[i] >= 32) && (groupPtr->channelList[i] <=47)){\r
521                                 groupChannelIdMask[1] |= (1 << (groupPtr->channelList[i] - 32));\r
522                         }else if((groupPtr->channelList[i] >= 64) && (groupPtr->channelList[i] <=95)){\r
523                                 groupChannelIdMask[2] |= (1 << (groupPtr->channelList[i] - 64));\r
524                         }\r
525                 }\r
526 \r
527                 /* Enable Normal conversion */\r
528                 ADC_0.NCMR[0].R = groupChannelIdMask[0];\r
529                 ADC_0.NCMR[1].R = groupChannelIdMask[1];\r
530                 ADC_0.NCMR[2].R = groupChannelIdMask[2];\r
531 \r
532                 /* Enable Channel Interrupt */\r
533                 ADC_0.CIMR[0].R = groupChannelIdMask[0];\r
534                 ADC_0.CIMR[1].R = groupChannelIdMask[1];\r
535                 ADC_0.CIMR[2].R = groupChannelIdMask[2];\r
536 #endif\r
537                 /* Clear interrupts */\r
538                 ADC_0.ISR.B.ECH = 1;\r
539                 /* Enable ECH interrupt */\r
540                 ADC_0.IMR.B.MSKECH = 1;\r
541 \r
542                 /* Trigger normal conversions for ADC0 */\r
543                 ADC_0.MCR.B.NSTART = 1;\r
544         }\r
545         else\r
546         {\r
547         /* Error have been set within Adc_CheckStartGroupConversion(). */\r
548         }\r
549 }\r
550 \r
551 void Adc_StopGroupConversion (Adc_GroupType group)\r
552 {\r
553   if (E_OK == Adc_CheckStopGroupConversion (group))\r
554   {\r
555         /* Disable trigger normal conversions for ADC0 */\r
556         ADC_0.MCR.B.NSTART = 0;\r
557 \r
558         /* Set group state to IDLE. */\r
559         AdcConfigPtr->groupConfigPtr[group].status->groupStatus = ADC_IDLE;\r
560 \r
561         /* Disable group notification if enabled. */\r
562     if(1 == AdcConfigPtr->groupConfigPtr[group].status->notifictionEnable){\r
563         Adc_DisableGroupNotification (group);\r
564     }\r
565   }\r
566   else\r
567   {\r
568         /* Error have been set within Adc_CheckStartGroupConversion(). */\r
569   }\r
570 }\r
571 #endif  /* endof #if (ADC_ENABLE_START_STOP_GROUP_API == STD_ON) */\r
572 \r
573 #define SYSTEM_CLOCK_DIVIDE(f)    ((f / 2) - 1)\r
574 \r
575 #if (ADC_GRP_NOTIF_CAPABILITY == STD_ON)\r
576 void Adc_EnableGroupNotification (Adc_GroupType group)\r
577 {\r
578         Std_ReturnType res;\r
579 \r
580 #if ( ADC_DEV_ERROR_DETECT == STD_ON )\r
581         if( (ValidateInit(ADC_ENABLEGROUPNOTIFICATION_ID) == E_NOT_OK) ||\r
582                 (ValidateGroup(group, ADC_ENABLEGROUPNOTIFICATION_ID) == E_NOT_OK))\r
583         {\r
584                 res = E_NOT_OK;\r
585         }\r
586         else if (AdcConfigPtr->groupConfigPtr[group].groupCallback == NULL)\r
587         {\r
588                 res = E_NOT_OK;\r
589                 Det_ReportError(MODULE_ID_ADC,0,ADC_ENABLEGROUPNOTIFICATION_ID ,ADC_E_NOTIF_CAPABILITY );\r
590         }\r
591         else\r
592         {\r
593                 /* Nothing strange. Go on... */\r
594                 res = E_OK;\r
595         }\r
596 #else\r
597         res = E_OK;\r
598 #endif\r
599         if (E_OK == res){\r
600                 AdcConfigPtr->groupConfigPtr[group].status->notifictionEnable = 1;\r
601         }\r
602 }\r
603 \r
604 void Adc_DisableGroupNotification (Adc_GroupType group)\r
605 {\r
606         Std_ReturnType res;\r
607 \r
608 #if ( ADC_DEV_ERROR_DETECT == STD_ON )\r
609         if( (ValidateInit(ADC_DISABLEGROUPNOTIFICATION_ID) == E_NOT_OK) ||\r
610                 (ValidateGroup(group, ADC_DISABLEGROUPNOTIFICATION_ID) == E_NOT_OK))\r
611         {\r
612                 res = E_NOT_OK;\r
613         }\r
614         else if (AdcConfigPtr->groupConfigPtr[group].groupCallback == NULL)\r
615         {\r
616                 res = E_NOT_OK;\r
617                 Det_ReportError(MODULE_ID_ADC,0,ADC_DISABLEGROUPNOTIFICATION_ID ,ADC_E_NOTIF_CAPABILITY );\r
618         }\r
619         else\r
620         {\r
621                 /* Nothing strange. Go on... */\r
622                 res = E_OK;\r
623         }\r
624 #else\r
625         res = E_OK;\r
626 #endif\r
627         if (E_OK == res){\r
628                 AdcConfigPtr->groupConfigPtr[group].status->notifictionEnable = 0;\r
629         }\r
630 }\r
631 #endif\r
632 \r
633 \r
634 \r
635 /* Development error checking functions. */\r
636 #if (ADC_READ_GROUP_API == STD_ON)\r
637 static Std_ReturnType Adc_CheckReadGroup (Adc_GroupType group)\r
638 {\r
639   Std_ReturnType returnValue = E_OK;\r
640 \r
641 #if ( ADC_DEV_ERROR_DETECT == STD_ON )\r
642 \r
643   if( (ValidateInit(ADC_READGROUP_ID) == E_NOT_OK) ||\r
644       (ValidateGroup(group, ADC_READGROUP_ID) == E_NOT_OK))\r
645   {\r
646           returnValue = E_NOT_OK;\r
647   }\r
648   else if (ADC_IDLE == AdcConfigPtr->groupConfigPtr[group].status->groupStatus)\r
649   {\r
650     /* ADC388. */\r
651     returnValue = E_NOT_OK;\r
652     Det_ReportError(MODULE_ID_ADC,0,ADC_READGROUP_ID ,ADC_E_IDLE );\r
653   }\r
654   else\r
655   {\r
656     /* Nothing strange. Go on... */\r
657     returnValue = E_OK;\r
658   }\r
659 #endif\r
660   return (returnValue);\r
661 }\r
662 #endif\r
663 \r
664 #if (ADC_ENABLE_START_STOP_GROUP_API == STD_ON)\r
665 static Std_ReturnType Adc_CheckStartGroupConversion (Adc_GroupType group)\r
666 {\r
667   Std_ReturnType returnValue = E_OK;\r
668 \r
669 #if ( ADC_DEV_ERROR_DETECT == STD_ON )\r
670 \r
671   if( (ValidateInit(ADC_STARTGROUPCONVERSION_ID) == E_NOT_OK) ||\r
672       (ValidateGroup(group, ADC_STARTGROUPCONVERSION_ID) == E_NOT_OK))\r
673   {\r
674           returnValue = E_NOT_OK;\r
675   }\r
676   else if ( NULL == AdcConfigPtr->groupConfigPtr[group].status->resultBufferPtr )\r
677   {\r
678       /* ResultBuffer not set, ADC424 */\r
679           Det_ReportError(MODULE_ID_ADC,0,ADC_STARTGROUPCONVERSION_ID, ADC_E_BUFFER_UNINIT );\r
680           returnValue = E_NOT_OK;\r
681   }\r
682   else if (!(ADC_TRIGG_SRC_SW == AdcConfigPtr->groupConfigPtr[group].triggerSrc))\r
683   {\r
684     /* Wrong trig source, ADC133. */\r
685     Det_ReportError(MODULE_ID_ADC,0,ADC_STARTGROUPCONVERSION_ID, ADC_E_WRONG_TRIGG_SRC);\r
686     returnValue = E_NOT_OK;\r
687   }\r
688   else if (!((ADC_IDLE             == AdcConfigPtr->groupConfigPtr[group].status->groupStatus) ||\r
689              (ADC_STREAM_COMPLETED == AdcConfigPtr->groupConfigPtr[group].status->groupStatus)))\r
690   {\r
691     /* Group status not OK, ADC351, ADC428 */\r
692     Det_ReportError(MODULE_ID_ADC,0,ADC_STARTGROUPCONVERSION_ID, ADC_E_BUSY );\r
693 \r
694     //returnValue = E_NOT_OK;\r
695     returnValue = E_OK;\r
696   }\r
697   else\r
698   {\r
699     returnValue = E_OK;\r
700   }\r
701 #endif\r
702 \r
703   return (returnValue);\r
704 }\r
705 \r
706 static Std_ReturnType Adc_CheckStopGroupConversion (Adc_GroupType group)\r
707 {\r
708   Std_ReturnType returnValue = E_OK;\r
709 \r
710 #if ( ADC_DEV_ERROR_DETECT == STD_ON )\r
711   if( (ValidateInit(ADC_STOPGROUPCONVERSION_ID) == E_NOT_OK) ||\r
712       (ValidateGroup(group, ADC_STOPGROUPCONVERSION_ID) == E_NOT_OK))\r
713   {\r
714           returnValue = E_NOT_OK;\r
715   }\r
716   else if (!(ADC_TRIGG_SRC_SW == AdcConfigPtr->groupConfigPtr[group].triggerSrc))\r
717   {\r
718         /* Wrong trig source, ADC164. */\r
719         Det_ReportError(MODULE_ID_ADC,0,ADC_STOPGROUPCONVERSION_ID, ADC_E_WRONG_TRIGG_SRC);\r
720         returnValue = E_NOT_OK;\r
721   }\r
722   else if (ADC_IDLE == AdcConfigPtr->groupConfigPtr[group].status->groupStatus)\r
723   {\r
724         /* Group status not OK, ADC241 */\r
725         Det_ReportError(MODULE_ID_ADC,0,ADC_STOPGROUPCONVERSION_ID, ADC_E_IDLE );\r
726         returnValue = E_NOT_OK;\r
727   }\r
728   else\r
729   {\r
730         returnValue = E_OK;\r
731   }\r
732 #endif\r
733 \r
734   return (returnValue);\r
735 }\r
736 #endif\r
737 \r
738 static Std_ReturnType Adc_CheckInit (const Adc_ConfigType *ConfigPtr)\r
739 {\r
740   Std_ReturnType returnValue = E_OK;\r
741 \r
742 #if ( ADC_DEV_ERROR_DETECT == STD_ON )\r
743   if (!(ADC_UNINIT == adcState))\r
744   {\r
745     /* Oops, already initialised. */\r
746     Det_ReportError(MODULE_ID_ADC,0,ADC_INIT_ID, ADC_E_ALREADY_INITIALIZED );\r
747     returnValue = E_NOT_OK;\r
748   }\r
749   else if (ConfigPtr == NULL)\r
750   {\r
751     /* Wrong config! */\r
752     Det_ReportError(MODULE_ID_ADC,0,ADC_INIT_ID, ADC_E_PARAM_CONFIG );\r
753     returnValue = E_NOT_OK;\r
754   }\r
755   else\r
756   {\r
757     /* Looks good!! */\r
758     returnValue = E_OK;\r
759   }\r
760 #endif\r
761   return (returnValue);\r
762 }\r
763 \r
764 #if (ADC_DEINIT_API == STD_ON)\r
765 static Std_ReturnType Adc_CheckDeInit (void)\r
766 {\r
767         Std_ReturnType returnValue = E_OK;\r
768 \r
769 #if ( ADC_DEV_ERROR_DETECT == STD_ON )\r
770         if(ValidateInit(ADC_DEINIT_ID) == E_OK)\r
771         {\r
772                 for (Adc_GroupType group = ADC_GROUP0; group < AdcConfigPtr->nbrOfGroups; group++)\r
773                 {\r
774                         /*  Check ADC is IDLE or COMPLETE*/\r
775                         if((AdcConfigPtr->groupConfigPtr[group].status->groupStatus != ADC_IDLE) && (AdcConfigPtr->groupConfigPtr[group].status->groupStatus != ADC_STREAM_COMPLETED))\r
776                         {\r
777                                 Det_ReportError(MODULE_ID_ADC,0,ADC_DEINIT_ID, ADC_E_BUSY );\r
778                                 returnValue = E_NOT_OK;\r
779                         }\r
780                 }\r
781         }\r
782         else\r
783         {\r
784                 returnValue = E_NOT_OK;\r
785         }\r
786 #endif\r
787         return (returnValue);\r
788 }\r
789 #endif\r
790 \r
791 static Std_ReturnType Adc_CheckSetupResultBuffer (Adc_GroupType group)\r
792 {\r
793   Std_ReturnType returnValue = E_OK;\r
794 \r
795 #if ( ADC_DEV_ERROR_DETECT == STD_ON )\r
796   if(ValidateGroup(group, ADC_SETUPRESULTBUFFER_ID) == E_NOT_OK)\r
797   {\r
798           returnValue = E_NOT_OK;\r
799   }\r
800 #endif\r
801   return (returnValue);\r
802 }\r
803 \r
804 static Std_ReturnType Adc_CheckGetStreamLastPointer (Adc_GroupType group)\r
805 {\r
806   Std_ReturnType returnValue = E_OK;\r
807 \r
808 #if ( ADC_DEV_ERROR_DETECT == STD_ON )\r
809   if( (ValidateInit(ADC_GETSTREAMLASTPOINTER_ID) == E_NOT_OK) ||\r
810           (ValidateGroup(group, ADC_GETSTREAMLASTPOINTER_ID) == E_NOT_OK))\r
811   {\r
812           returnValue = E_NOT_OK;\r
813   }\r
814   else if(AdcConfigPtr->groupConfigPtr[group].status->groupStatus == ADC_IDLE)\r
815   { /** @req ADC215 Check ADC is not in IDLE */\r
816         Det_ReportError(MODULE_ID_ADC,0,ADC_GETSTREAMLASTPOINTER_ID, ADC_E_IDLE );\r
817         returnValue = E_NOT_OK;\r
818   }\r
819 #endif\r
820   return (returnValue);\r
821 }\r
822 \r
823 \r
824 \r