kernel/eka/drivers/adc/d_adc.cpp
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // e32\drivers\adc\d_adc.cpp
       
    15 // Generic ADC driver
       
    16 // 
       
    17 //
       
    18 
       
    19 
       
    20 #include <adc.h>
       
    21 
       
    22 /******************************************************
       
    23  * ADC Channel
       
    24  ******************************************************/
       
    25 EXPORT_C TAdcChannel::TAdcChannel(TInt anAdc)
       
    26 	:	iChannelId(-1), iCommandCount(0), iCommandList(NULL),
       
    27 		iReadings(NULL)
       
    28 	{
       
    29 	iNext=NULL;
       
    30 //	iPriority=0;
       
    31 	if (anAdc>=0 && anAdc<DAdc::NumberOfAdcs)
       
    32 		iAdc=DAdc::TheAdcs[anAdc];
       
    33 	else
       
    34 		iAdc=NULL;
       
    35 	}
       
    36 
       
    37 EXPORT_C void TAdcChannel::Read(TInt* aReadingBuffer)
       
    38 //
       
    39 // Initiate a reading of this channel
       
    40 //
       
    41 	{
       
    42 	TInt irq=NKern::DisableAllInterrupts();
       
    43 	if (!iNext)
       
    44 		{
       
    45 		iReadings=aReadingBuffer;
       
    46 		iAdc->Add(this);
       
    47 		}
       
    48 	NKern::RestoreInterrupts(irq);
       
    49 	}
       
    50 
       
    51 EXPORT_C void TAdcChannel::Preamble()
       
    52 //
       
    53 // Default preamble does nothing
       
    54 //
       
    55 	{
       
    56 	}
       
    57 
       
    58 EXPORT_C void TAdcChannel::Postamble()
       
    59 //
       
    60 // Default postamble does nothing
       
    61 //
       
    62 	{
       
    63 	}
       
    64 
       
    65 
       
    66 /******************************************************
       
    67  * ADC Controller
       
    68  ******************************************************/
       
    69 LOCAL_C void timerExpired(TAny* aPtr)
       
    70 	{
       
    71 	((DAdc*)aPtr)->TimerExpired();
       
    72 	}
       
    73 
       
    74 DAdc::DAdc()
       
    75 	:	iTimer(timerExpired,this)
       
    76 	{
       
    77 //	iCurrentChannel=NULL;
       
    78 //	iCurrentCommand=0;
       
    79 //	iCommandPtr=0;
       
    80 //	iCommandCount=0;
       
    81 //	iMinPriority=0;
       
    82 	}
       
    83 
       
    84 DAdc::~DAdc()
       
    85 	{
       
    86 	}
       
    87 
       
    88 EXPORT_C TInt DAdc::SetMinPriority(TInt anAdc, TInt aPriority)
       
    89 	{
       
    90 	if (anAdc<0 || anAdc>=NumberOfAdcs)
       
    91 		return KErrArgument;
       
    92 	if (aPriority<0 || aPriority>KNumAdcChannelPriorities)
       
    93 		return KErrArgument;
       
    94 	return TheAdcs[anAdc]->DoSetMinPriority(aPriority);
       
    95 	}
       
    96 
       
    97 TInt DAdc::DoSetMinPriority(TInt aPriority)
       
    98 	{
       
    99 	TInt irq=NKern::DisableAllInterrupts();
       
   100 	if (aPriority<iMinPriority)
       
   101 		{
       
   102 		if (iList.iPresent[0])
       
   103 			{
       
   104 			TAdcChannel* pN=iList.First();
       
   105 			if (pN->iPriority>=aPriority)
       
   106 				{
       
   107 				iList.Remove(pN);
       
   108 				Execute(pN);
       
   109 				}
       
   110 			}
       
   111 		}
       
   112 	iMinPriority=aPriority;
       
   113 	NKern::RestoreInterrupts(irq);
       
   114 	return KErrNone;
       
   115 	}
       
   116 
       
   117 void DAdc::Add(TAdcChannel* aChannel)
       
   118 //
       
   119 // Queue another ADC reading request
       
   120 //
       
   121 	{
       
   122 	if (iCurrentChannel || (aChannel->iPriority<iMinPriority))
       
   123 		iList.Add(aChannel);
       
   124 	else
       
   125 		Execute(aChannel);
       
   126 	}
       
   127 
       
   128 void DAdc::Execute(TAdcChannel* aChannel)
       
   129 //
       
   130 // Begin execution of an ADC request
       
   131 // This runs in an ISR or with interrupts disabled
       
   132 //
       
   133 	{
       
   134 	aChannel->iNext=(TPriListLink*)1;	// so channel will not be queued again
       
   135 	iCurrentChannel=aChannel;
       
   136 	iCommandPtr=aChannel->iCommandList;
       
   137 	iCommandCount=aChannel->iCommandCount;
       
   138 	NextCommand();
       
   139 	}
       
   140 
       
   141 void DAdc::NextCommand()
       
   142 //
       
   143 // Perform the next command in the command list
       
   144 // This runs in an ISR or with interrupts disabled
       
   145 //
       
   146 	{
       
   147 	TBool wait=EFalse;
       
   148 	TAdcChannel* pC=iCurrentChannel;
       
   149 	if (iCommandCount)
       
   150 		{
       
   151 		TInt c=*iCommandPtr++;
       
   152 		iCurrentCommand=c;
       
   153 		iCommandCount--;
       
   154 		if (c & EAdcCmdPreamble)
       
   155 			pC->Preamble();
       
   156 		if (c & EAdcCmdPostamble)
       
   157 			pC->Postamble();
       
   158 		if (c & EAdcCmdWait)
       
   159 			{
       
   160 			iTimer.OneShot(c & 0xffff);
       
   161 			wait=ETrue;
       
   162 			}
       
   163 		else if (c & EAdcCmdReading)
       
   164 			{
       
   165 			StartConversion(pC->iChannelId);
       
   166 			wait=ETrue;
       
   167 			}
       
   168 		}
       
   169 	if (iCommandCount==0 && !wait)
       
   170 		{
       
   171 		iCurrentChannel=NULL;
       
   172 		pC->iNext=NULL;
       
   173 		if (iList.iPresent[0])
       
   174 			{
       
   175 			TAdcChannel* pN=iList.First();
       
   176 			if (pN->iPriority>=iMinPriority)
       
   177 				{
       
   178 				iList.Remove(pN);
       
   179 				Execute(pN);
       
   180 				}
       
   181 			}
       
   182 		pC->Complete();
       
   183 		}
       
   184 	}
       
   185 
       
   186 void DAdc::Start()
       
   187 //
       
   188 // Called on completion of initialisation to start processing requests
       
   189 // This runs in an ISR
       
   190 //
       
   191 	{
       
   192 	iCurrentChannel=NULL;
       
   193 	if (iList.iPresent[0])
       
   194 		{
       
   195 		TAdcChannel* pN=iList.First();
       
   196 		iList.Remove(pN);
       
   197 		Execute(pN);
       
   198 		}
       
   199 	}
       
   200 
       
   201 void DAdc::ConversionComplete(TInt aValue)
       
   202 //
       
   203 // Called when a conversion has completed
       
   204 // This runs in an ISR
       
   205 //
       
   206 	{
       
   207 	if ((iCurrentCommand & EAdcCmdDiscard)==0)
       
   208 		*(iCurrentChannel->iReadings)++=aValue;
       
   209 	NextCommand();
       
   210 	}
       
   211 
       
   212 void DAdc::TimerExpired()
       
   213 //
       
   214 // Called in ISR when timer expires
       
   215 //
       
   216 	{
       
   217 	NextCommand();
       
   218 	}
       
   219