1 /* -------------------------------- Arctic Core ------------------------------
\r
2 * Arctic Core - the open source AUTOSAR platform http://arccore.com
\r
4 * Copyright (C) 2009 ArcCore AB <contact@arccore.com>
\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
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
14 * -------------------------------- Arctic Core ------------------------------*/
\r
29 #define BM_ADPU 0x80
\r
30 #define BM_AFFC 0x40
\r
31 #define BM_AWAI 0x20
\r
32 #define BM_ETRIGLE 0x10
\r
33 #define BM_ETRIGP 0x08
\r
34 #define BM_ETRIG_E 0x04
\r
35 #define BM_ASCIE 0x02
\r
36 #define BM_ASCIF 0x01
\r
43 #define BM_FIFO 0x04
\r
44 #define BM_FRZ1 0x02
\r
45 #define BM_FRZ0 0x01
\r
48 #define BM_SRES8 0x80
\r
49 #define BM_SMP1 0x40
\r
50 #define BM_SMP0 0x20
\r
51 #define BM_PRS4 0x10
\r
52 #define BM_PRS3 0x08
\r
53 #define BM_PRS2 0x04
\r
54 #define BM_PRS1 0x02
\r
55 #define BM_PRS0 0x01
\r
59 #define BM_DSGN 0x40
\r
60 #define BM_SCAN 0x20
\r
61 #define BM_MULT 0x10
\r
72 /* Function prototypes. */
\r
74 /* Development error checking. */
\r
75 static Std_ReturnType Adc_CheckReadGroup (Adc_GroupType group);
\r
76 static Std_ReturnType Adc_CheckStartGroupConversion (Adc_GroupType group);
\r
77 static Std_ReturnType Adc_CheckInit (const Adc_ConfigType *ConfigPtr);
\r
78 static Std_ReturnType Adc_CheckSetupResultBuffer (Adc_GroupType group);
\r
80 static void Adc_GroupConversionComplete (void);
\r
82 static Adc_StateType adcState = ADC_UNINIT;
\r
84 /* Pointer to configuration structure. */
\r
85 static const Adc_ConfigType *AdcConfigPtr;
\r
87 #if (ADC_DEINIT_API == STD_ON)
\r
88 Std_ReturnType Adc_DeInit (const Adc_ConfigType *ConfigPtr)
\r
95 Std_ReturnType Adc_Init (const Adc_ConfigType *ConfigPtr)
\r
97 Std_ReturnType returnValue;
\r
98 Adc_GroupType group;
\r
100 if (E_OK == Adc_CheckInit(ConfigPtr))
\r
102 /* First of all, store the location of the configuration data. */
\r
103 AdcConfigPtr = ConfigPtr;
\r
105 // Connect interrupt to correct isr
\r
106 ISR_INSTALL_ISR2("ADC",Adc_GroupConversionComplete,IRQ_TYPE_ATD0,6,0);
\r
109 ATD0CTL2 = BM_ADPU | BM_AFFC | BM_ASCIE; /* power enable, Fast Flag Clear, irq enable*/
\r
110 ATD0CTL3 = 0x00; /* 8 conversions per sequence default */
\r
112 ATD0CTL4 = (ConfigPtr->hwConfigPtr->resolution << 7) |
\r
113 (ConfigPtr->hwConfigPtr->convTime << 5) |
\r
114 ConfigPtr->hwConfigPtr->adcPrescale;
\r
116 for (group = ADC_GROUP0; group < ADC_NBR_OF_GROUPS; group++)
\r
119 ConfigPtr->groupConfigPtr[group].status->groupStatus = ADC_IDLE;
\r
122 /* Move on to INIT state. */
\r
123 adcState = ADC_INIT;
\r
124 returnValue = E_OK;
\r
128 returnValue = E_NOT_OK;
\r
131 return (returnValue);
\r
134 Std_ReturnType Adc_SetupResultBuffer (Adc_GroupType group, Adc_ValueGroupType *bufferPtr)
\r
136 Std_ReturnType returnValue;
\r
138 /* Check for development errors. */
\r
139 if (E_OK == Adc_CheckSetupResultBuffer (group))
\r
141 AdcConfigPtr->groupConfigPtr[group].status->resultBufferPtr = bufferPtr;
\r
142 returnValue = E_OK;
\r
146 /* An error have been raised from Adc_CheckSetupResultBuffer(). */
\r
147 returnValue = E_NOT_OK;
\r
150 return (returnValue);
\r
153 #if (ADC_READ_GROUP_API == STD_ON)
\r
154 Std_ReturnType Adc_ReadGroup (Adc_GroupType group, Adc_ValueGroupType *dataBufferPtr)
\r
156 Std_ReturnType returnValue;
\r
157 Adc_ChannelType channel;
\r
159 if (E_OK == Adc_CheckReadGroup (group))
\r
161 if ((ADC_CONV_MODE_CONTINUOUS == AdcConfigPtr->groupConfigPtr[group].conversionMode) &&
\r
162 ((ADC_STREAM_COMPLETED == AdcConfigPtr->groupConfigPtr[group].status->groupStatus) ||
\r
163 (ADC_COMPLETED == AdcConfigPtr->groupConfigPtr[group].status->groupStatus)))
\r
165 /* ADC329, ADC331. */
\r
166 AdcConfigPtr->groupConfigPtr[group].status->groupStatus = ADC_BUSY;
\r
167 returnValue = E_OK;
\r
169 else if ((ADC_CONV_MODE_ONESHOT == AdcConfigPtr->groupConfigPtr[group].conversionMode) &&
\r
170 (ADC_STREAM_COMPLETED == AdcConfigPtr->groupConfigPtr[group].status->groupStatus))
\r
173 AdcConfigPtr->groupConfigPtr[group].status->groupStatus = ADC_IDLE;
\r
175 returnValue = E_OK;
\r
180 returnValue = E_OK;
\r
183 if (E_OK == returnValue)
\r
185 /* Copy the result to application buffer. */
\r
186 for (channel = 0; channel < AdcConfigPtr->groupConfigPtr[group].numberOfChannels; channel++)
\r
188 dataBufferPtr[channel] = AdcConfigPtr->groupConfigPtr[group].resultBuffer[channel];
\r
194 /* An error have been raised from Adc_CheckReadGroup(). */
\r
195 returnValue = E_NOT_OK;
\r
198 return (returnValue);
\r
202 Adc_StatusType Adc_GetGroupStatus (Adc_GroupType group)
\r
204 Adc_StatusType returnValue;
\r
205 if ((ADC_INIT == adcState) && (AdcConfigPtr != NULL))
\r
207 /* Adc initilised, OK to move on... */
\r
208 returnValue = AdcConfigPtr->groupConfigPtr[group].status->groupStatus;
\r
212 returnValue = ADC_IDLE;
\r
213 #if ( ADC_DEV_ERROR_DETECT == STD_ON )
\r
214 Det_ReportError(MODULE_ID_ADC,0,ADC_GETGROUPSTATUS_ID, ADC_E_UNINIT );
\r
218 return (returnValue);
\r
221 static void Adc_GroupConversionComplete (void)
\r
224 Adc_GroupDefType *groupPtr = NULL;
\r
225 /* Clear SCF flag. Not needed if AFFC is set but better to always do it */
\r
228 // Check which group is busy, only one is allowed to be busy at a time in a hw unit
\r
229 for (index = ADC_GROUP0; index < ADC_NBR_OF_GROUPS; index++)
\r
231 if(AdcConfigPtr->groupConfigPtr[index].status->groupStatus == ADC_BUSY)
\r
233 groupPtr = (Adc_GroupDefType *)&AdcConfigPtr->groupConfigPtr[index];
\r
237 if(groupPtr != NULL)
\r
240 volatile uint16_t *ptr = &ATD0DR0;
\r
241 for(index=0; index<groupPtr->numberOfChannels; index++)
\r
243 groupPtr->resultBuffer[index] = *(ptr + groupPtr->channelList[index]);
\r
246 /* Sample completed. */
\r
247 groupPtr->status->groupStatus = ADC_STREAM_COMPLETED;
\r
249 /* Call notification if enabled. */
\r
250 #if (ADC_GRP_NOTIF_CAPABILITY == STD_ON)
\r
251 if (groupPtr->status->notifictionEnable && groupPtr->groupCallback != NULL)
\r
253 groupPtr->groupCallback();
\r
259 #if (ADC_ENABLE_START_STOP_GROUP_API == STD_ON)
\r
260 void Adc_StartGroupConversion (Adc_GroupType group)
\r
262 /* Run development error check. */
\r
263 if (E_OK == Adc_CheckStartGroupConversion (group))
\r
265 /* Set single scan enable bit if this group is one shot. */
\r
266 if (AdcConfigPtr->groupConfigPtr[group].conversionMode == ADC_CONV_MODE_ONESHOT)
\r
268 /* Start next AD conversion. */
\r
269 ATD0CTL5 = BM_DJM | BM_MULT; /* 10010000 Right Justified,unsigned */
\r
270 /* No SCAN/MULT/AD0 start select */
\r
271 /* Set group state to BUSY. */
\r
272 AdcConfigPtr->groupConfigPtr[group].status->groupStatus = ADC_BUSY;
\r
277 /* Start AD conversion. */
\r
278 ATD0CTL5 = BM_DJM | BM_MULT | BM_SCAN; /* 10010000 Right Justified,unsigned */
\r
279 /* SCAN/MULT/AD0 start select */
\r
280 /* Set group state to BUSY. */
\r
281 AdcConfigPtr->groupConfigPtr[group].status->groupStatus = ADC_BUSY;
\r
287 #if (ADC_GRP_NOTIF_CAPABILITY == STD_ON)
\r
288 void Adc_EnableGroupNotification (Adc_GroupType group)
\r
290 AdcConfigPtr->groupConfigPtr[group].status->notifictionEnable = 1;
\r
293 void Adc_DisableGroupNotification (Adc_GroupType group)
\r
295 AdcConfigPtr->groupConfigPtr[group].status->notifictionEnable = 0;
\r
300 /* Development error checking functions. */
\r
301 #if (ADC_READ_GROUP_API == STD_ON)
\r
302 static Std_ReturnType Adc_CheckReadGroup (Adc_GroupType group)
\r
304 Std_ReturnType returnValue;
\r
306 #if ( ADC_DEV_ERROR_DETECT == STD_ON )
\r
308 if (ADC_UNINIT == adcState)
\r
311 returnValue = E_NOT_OK;
\r
312 Det_ReportError(MODULE_ID_ADC,0,ADC_READGROUP_ID ,ADC_E_UNINIT );
\r
314 else if ((group < ADC_GROUP0) || (group >= ADC_NBR_OF_GROUPS))
\r
317 returnValue = E_NOT_OK;
\r
318 Det_ReportError(MODULE_ID_ADC,0,ADC_READGROUP_ID ,ADC_E_PARAM_GROUP );
\r
320 else if (ADC_IDLE == AdcConfigPtr->groupConfigPtr[group].status->groupStatus)
\r
323 returnValue = E_NOT_OK;
\r
324 Det_ReportError(MODULE_ID_ADC,0,ADC_READGROUP_ID ,ADC_E_IDLE );
\r
328 /* Nothing strange. Go on... */
\r
329 returnValue = E_OK;
\r
332 returnValue = E_OK;
\r
334 return (returnValue);
\r
338 #if (ADC_ENABLE_START_STOP_GROUP_API == STD_ON)
\r
339 static Std_ReturnType Adc_CheckStartGroupConversion (Adc_GroupType group)
\r
341 Std_ReturnType returnValue;
\r
342 #if ( ADC_DEV_ERROR_DETECT == STD_ON )
\r
343 if (!(ADC_INIT == adcState))
\r
345 /* ADC not initialised, ADC294. */
\r
346 Det_ReportError(MODULE_ID_ADC,0,ADC_STARTGROUPCONVERSION_ID, ADC_E_UNINIT );
\r
347 returnValue = E_NOT_OK;
\r
349 else if (!((group >= 0) && (group < ADC_NBR_OF_GROUPS)))
\r
351 /* Wrong group ID, ADC125 */
\r
352 Det_ReportError(MODULE_ID_ADC,0,ADC_STARTGROUPCONVERSION_ID, ADC_E_PARAM_GROUP );
\r
353 returnValue = E_NOT_OK;
\r
355 else if (!(ADC_TRIGG_SRC_SW == AdcConfigPtr->groupConfigPtr[group].triggerSrc))
\r
357 /* Wrong trig source, ADC133. */
\r
358 Det_ReportError(MODULE_ID_ADC,0,ADC_STARTGROUPCONVERSION_ID, ADC_E_WRONG_TRIGG_SRC);
\r
359 returnValue = E_NOT_OK;
\r
361 else if (!((ADC_IDLE == AdcConfigPtr->groupConfigPtr[group].status->groupStatus) ||
\r
362 (ADC_STREAM_COMPLETED == AdcConfigPtr->groupConfigPtr[group].status->groupStatus)))
\r
364 /* Group status not OK, ADC351, ADC428 */
\r
365 Det_ReportError(MODULE_ID_ADC,0,ADC_STARTGROUPCONVERSION_ID, ADC_E_BUSY );
\r
367 returnValue = E_NOT_OK;
\r
371 returnValue = E_OK;
\r
374 returnValue = E_OK;
\r
376 return (returnValue);
\r
380 static Std_ReturnType Adc_CheckInit (const Adc_ConfigType *ConfigPtr)
\r
382 Std_ReturnType returnValue;
\r
384 #if ( ADC_DEV_ERROR_DETECT == STD_ON )
\r
385 if (!(ADC_UNINIT == adcState))
\r
387 /* Oops, already initialised. */
\r
388 Det_ReportError(MODULE_ID_ADC,0,ADC_INIT_ID, ADC_E_ALREADY_INITIALIZED );
\r
389 returnValue = E_NOT_OK;
\r
391 else if (ConfigPtr == NULL)
\r
393 /* Wrong config! */
\r
394 Det_ReportError(MODULE_ID_ADC,0,ADC_INIT_ID, ADC_E_PARAM_CONFIG );
\r
395 returnValue = E_NOT_OK;
\r
400 returnValue = E_OK;
\r
403 returnValue = E_OK;
\r
405 return (returnValue);
\r
408 static Std_ReturnType Adc_CheckSetupResultBuffer (Adc_GroupType group)
\r
410 Std_ReturnType returnValue;
\r
412 #if ( ADC_DEV_ERROR_DETECT == STD_ON )
\r
413 if (ADC_UNINIT == adcState)
\r
415 /* Driver not initialised. */
\r
416 Det_ReportError(MODULE_ID_ADC,0,ADC_SETUPRESULTBUFFER_ID,ADC_E_UNINIT );
\r
417 returnValue = E_NOT_OK;
\r
419 else if (group < ADC_NBR_OF_GROUPS)
\r
422 Det_ReportError(MODULE_ID_ADC,0,ADC_SETUPRESULTBUFFER_ID,ADC_E_PARAM_GROUP );
\r
423 returnValue = E_NOT_OK;
\r
428 returnValue = E_OK;
\r
431 returnValue = E_OK;
\r
433 return (returnValue);
\r