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