os/kernelhwsrv/kerneltest/e32test/iic/iic_psl/spi.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2008-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
// e32test/iic/iic_psl/spi.cpp
sl@0
    15
//
sl@0
    16
#include "spi.h"
sl@0
    17
sl@0
    18
#ifdef IIC_INSTRUMENTATION_MACRO
sl@0
    19
#include <drivers/iic_trace.h>
sl@0
    20
#endif
sl@0
    21
sl@0
    22
#define NUM_CHANNELS 4 // Arbitrary
sl@0
    23
sl@0
    24
// Macros to be updated(?) with interaction with Configuration Repository
sl@0
    25
const TInt KChannelTypeArray[NUM_CHANNELS] = {DIicBusChannel::EMaster, DIicBusChannel::EMaster, DIicBusChannel::ESlave, DIicBusChannel::EMaster};
sl@0
    26
#define CHANNEL_TYPE(n) (KChannelTypeArray[n])	
sl@0
    27
const DIicBusChannel::TChannelDuplex KChannelDuplexArray[NUM_CHANNELS] = {DIicBusChannel::EHalfDuplex, DIicBusChannel::EHalfDuplex, DIicBusChannel::EHalfDuplex, DIicBusChannel::EFullDuplex};
sl@0
    28
#define CHANNEL_DUPLEX(n) (KChannelDuplexArray[n]) 
sl@0
    29
sl@0
    30
#ifdef LOG_SPI
sl@0
    31
#define SPI_PRINT(str) Kern::Printf str
sl@0
    32
#else
sl@0
    33
#define SPI_PRINT(str)
sl@0
    34
#endif
sl@0
    35
sl@0
    36
_LIT(KSpiThreadName,"SpiChannelThread");
sl@0
    37
sl@0
    38
const TInt KSpiThreadPriority = 5; // Arbitrary, can be 0-7, 7 highest
sl@0
    39
sl@0
    40
#ifdef STANDALONE_CHANNEL
sl@0
    41
_LIT(KPddNameSpi,"spi_ctrless.pdd");
sl@0
    42
#else
sl@0
    43
_LIT(KPddNameSpi,"spi.pdd");
sl@0
    44
#endif
sl@0
    45
sl@0
    46
#ifndef STANDALONE_CHANNEL
sl@0
    47
LOCAL_C TInt8 AssignChanNum()
sl@0
    48
	{
sl@0
    49
	static TInt8 iBaseChanNum = KSpiChannelNumBase;
sl@0
    50
	SPI_PRINT(("SPI AssignChanNum - on entry, iBaseCanNum = 0x%x\n",iBaseChanNum));
sl@0
    51
	return iBaseChanNum++; // Arbitrary, for illustration
sl@0
    52
	}
sl@0
    53
#endif
sl@0
    54
sl@0
    55
NONSHARABLE_CLASS(DSimulatedSpiDevice) : public DPhysicalDevice
sl@0
    56
	{
sl@0
    57
// Class to faciliate loading of the IIC classes
sl@0
    58
public:
sl@0
    59
	class TCaps
sl@0
    60
		{
sl@0
    61
	public:
sl@0
    62
		TVersion iVersion;
sl@0
    63
		};
sl@0
    64
public:
sl@0
    65
	DSimulatedSpiDevice();
sl@0
    66
	virtual TInt Install();
sl@0
    67
	virtual TInt Create(DBase*& aChannel, TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
sl@0
    68
	virtual TInt Validate(TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
sl@0
    69
	virtual void GetCaps(TDes8& aDes) const;
sl@0
    70
	inline static TVersion VersionRequired();
sl@0
    71
	};
sl@0
    72
sl@0
    73
TVersion DSimulatedSpiDevice::VersionRequired()
sl@0
    74
	{
sl@0
    75
	SPI_PRINT(("DSimulatedSpiDevice::VersionRequired\n"));
sl@0
    76
	return TVersion(KIicClientMajorVersionNumber,KIicClientMinorVersionNumber,KIicClientBuildVersionNumber);
sl@0
    77
	}
sl@0
    78
sl@0
    79
/** Factory class constructor */
sl@0
    80
DSimulatedSpiDevice::DSimulatedSpiDevice()
sl@0
    81
	{
sl@0
    82
	SPI_PRINT(("DSimulatedSpiDevice::DSimulatedSpiDevice\n"));
sl@0
    83
    iVersion = DSimulatedSpiDevice::VersionRequired();
sl@0
    84
	}
sl@0
    85
sl@0
    86
TInt DSimulatedSpiDevice::Install()
sl@0
    87
    {
sl@0
    88
	SPI_PRINT(("DSimulatedSpiDevice::Install\n"));
sl@0
    89
    return(SetName(&KPddNameSpi));
sl@0
    90
    }
sl@0
    91
sl@0
    92
/**  Called by the kernel's device driver framework to create a Physical Channel. */
sl@0
    93
TInt DSimulatedSpiDevice::Create(DBase*& /*aChannel*/, TInt /*aUint*/, const TDesC8* /*anInfo*/, const TVersion& /*aVer*/)
sl@0
    94
    {
sl@0
    95
	SPI_PRINT(("DSimulatedSpiDevice::Create\n"));
sl@0
    96
    return KErrNone;
sl@0
    97
    }
sl@0
    98
sl@0
    99
/**  Called by the kernel's device driver framework to check if this PDD is suitable for use with a Logical Channel.*/
sl@0
   100
TInt DSimulatedSpiDevice::Validate(TInt /*aUnit*/, const TDesC8* /*anInfo*/, const TVersion& aVer)
sl@0
   101
    {
sl@0
   102
	SPI_PRINT(("DSimulatedSpiDevice::Validate\n"));
sl@0
   103
   	if (!Kern::QueryVersionSupported(DSimulatedSpiDevice::VersionRequired(),aVer))
sl@0
   104
		return(KErrNotSupported);
sl@0
   105
    return KErrNone;
sl@0
   106
    }
sl@0
   107
sl@0
   108
/** Return the driver capabilities */
sl@0
   109
void DSimulatedSpiDevice::GetCaps(TDes8& aDes) const
sl@0
   110
    {
sl@0
   111
	SPI_PRINT(("DSimulatedSpiDevice::GetCaps\n"));
sl@0
   112
	// Create a capabilities object
sl@0
   113
	TCaps caps;
sl@0
   114
	caps.iVersion = iVersion;
sl@0
   115
	// Zero the buffer
sl@0
   116
	TInt maxLen = aDes.MaxLength();
sl@0
   117
	aDes.FillZ(maxLen);
sl@0
   118
	// Copy capabilities
sl@0
   119
	TInt size=sizeof(caps);
sl@0
   120
	if(size>maxLen)
sl@0
   121
	   size=maxLen;
sl@0
   122
	aDes.Copy((TUint8*)&caps,size);
sl@0
   123
    }
sl@0
   124
sl@0
   125
// supported channels for this implementation
sl@0
   126
static DIicBusChannel* ChannelPtrArray[NUM_CHANNELS];
sl@0
   127
sl@0
   128
//DECLARE_EXTENSION_WITH_PRIORITY(BUS_IMPLMENTATION_PRIORITY)	
sl@0
   129
DECLARE_STANDARD_PDD()		// SPI test driver to be explicitly loaded as an LDD, not kernel extension
sl@0
   130
	{
sl@0
   131
	SPI_PRINT(("\n\nSPI PDD, channel creation loop follows ...\n"));
sl@0
   132
sl@0
   133
#ifndef STANDALONE_CHANNEL
sl@0
   134
	DIicBusChannel* chan=NULL;
sl@0
   135
	for(TInt i=0; i<NUM_CHANNELS; i++)
sl@0
   136
		{
sl@0
   137
	SPI_PRINT(("\n"));
sl@0
   138
		if(CHANNEL_TYPE(i) == (DIicBusChannel::EMaster))
sl@0
   139
			{
sl@0
   140
			chan=new DSimulatedIicBusChannelMasterSpi(BUS_TYPE,CHANNEL_DUPLEX(i));
sl@0
   141
			if(!chan)
sl@0
   142
				return NULL;
sl@0
   143
			SPI_PRINT(("SPI chan created at 0x%x\n",chan));
sl@0
   144
			if(((DSimulatedIicBusChannelMasterSpi*)chan)->Create()!=KErrNone)
sl@0
   145
				return NULL;
sl@0
   146
			}
sl@0
   147
		else if(CHANNEL_TYPE(i) == DIicBusChannel::EMasterSlave)
sl@0
   148
			{
sl@0
   149
			DIicBusChannel* chanM=new DSimulatedIicBusChannelMasterSpi(BUS_TYPE,CHANNEL_DUPLEX(i));
sl@0
   150
			if(!chanM)
sl@0
   151
				return NULL;
sl@0
   152
			DIicBusChannel* chanS=new DSimulatedIicBusChannelSlaveSpi(BUS_TYPE,CHANNEL_DUPLEX(i));
sl@0
   153
			if(!chanS)
sl@0
   154
				return NULL;
sl@0
   155
			// For MasterSlave channel, the channel number for both the Master and Slave channels must be the same
sl@0
   156
			TInt8 msChanNum = ((DSimulatedIicBusChannelMasterSpi*)chanM)->GetChanNum();
sl@0
   157
			((DSimulatedIicBusChannelSlaveSpi*)chanS)->SetChanNum(msChanNum);
sl@0
   158
sl@0
   159
			chan=new DIicBusChannelMasterSlave(BUS_TYPE,CHANNEL_DUPLEX(i),(DIicBusChannelMaster*)chanM,(DIicBusChannelSlave*)chanS); // Generic implementation
sl@0
   160
			if(!chan)
sl@0
   161
				return NULL;
sl@0
   162
			SPI_PRINT(("SPI chan created at 0x%x\n",chan));
sl@0
   163
			if(((DIicBusChannelMasterSlave*)chan)->DoCreate()!=KErrNone)
sl@0
   164
				return NULL;
sl@0
   165
			}
sl@0
   166
		else
sl@0
   167
			{
sl@0
   168
			chan=new DSimulatedIicBusChannelSlaveSpi(BUS_TYPE,CHANNEL_DUPLEX(i));
sl@0
   169
			if(!chan)
sl@0
   170
				return NULL;
sl@0
   171
			SPI_PRINT(("SPI chan created at 0x%x\n",chan));
sl@0
   172
			if(((DSimulatedIicBusChannelSlaveSpi*)chan)->Create()!=KErrNone)
sl@0
   173
				return NULL;
sl@0
   174
			}
sl@0
   175
		ChannelPtrArray[i]=chan;
sl@0
   176
		}
sl@0
   177
	SPI_PRINT(("\nSPI PDD, channel creation loop done- about to invoke RegisterChannels\n\n"));
sl@0
   178
#ifdef IIC_INSTRUMENTATION_MACRO
sl@0
   179
	IIC_REGISTERCHANS_START_PSL_TRACE;
sl@0
   180
#endif
sl@0
   181
sl@0
   182
	TInt r = KErrNone;
sl@0
   183
	r=DIicBusController::RegisterChannels(ChannelPtrArray,NUM_CHANNELS);
sl@0
   184
sl@0
   185
#ifdef IIC_INSTRUMENTATION_MACRO
sl@0
   186
	IIC_REGISTERCHANS_END_PSL_TRACE;
sl@0
   187
#endif
sl@0
   188
	SPI_PRINT(("\nSPI - returned from RegisterChannels with r=%d\n",r));
sl@0
   189
	if(r!=KErrNone)
sl@0
   190
		{
sl@0
   191
		delete chan;
sl@0
   192
		return NULL;
sl@0
   193
		}
sl@0
   194
#endif
sl@0
   195
	return new DSimulatedSpiDevice;
sl@0
   196
	}
sl@0
   197
sl@0
   198
#ifdef STANDALONE_CHANNEL
sl@0
   199
EXPORT_C
sl@0
   200
#endif
sl@0
   201
	DSimulatedIicBusChannelMasterSpi::DSimulatedIicBusChannelMasterSpi(const TBusType aBusType, const TChannelDuplex aChanDuplex)
sl@0
   202
	: DIicBusChannelMaster(aBusType,aChanDuplex)
sl@0
   203
	{
sl@0
   204
	SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::DSimulatedIicBusChannelMasterSpi, aBusType=%d,aChanDuplex=%d\n",aBusType,aChanDuplex));
sl@0
   205
#ifndef STANDALONE_CHANNEL
sl@0
   206
	iChannelNumber = AssignChanNum();
sl@0
   207
#endif
sl@0
   208
	SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::DSimulatedIicBusChannelMasterSpi, iChannelNumber=%d\n",iChannelNumber));
sl@0
   209
	iTestState = ETestNone;
sl@0
   210
	iChannelState = EIdle;
sl@0
   211
	}
sl@0
   212
sl@0
   213
// The time-out call back invoked when the Slave exeecds the allowed response time
sl@0
   214
TInt DSimulatedIicBusChannelMasterSpi::HandleSlaveTimeout()
sl@0
   215
	{
sl@0
   216
	SPI_PRINT(("HandleSlaveTimeout \n"));
sl@0
   217
	return AsynchStateMachine(ETimeExpired); 
sl@0
   218
	}
sl@0
   219
sl@0
   220
TInt DSimulatedIicBusChannelMasterSpi::DoCreate()
sl@0
   221
	{
sl@0
   222
	SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::DoCreate\n"));
sl@0
   223
	TInt r=Init();	// PIL Base class initialisation
sl@0
   224
	r=Kern::DynamicDfcQCreate(iDynamicDfcQ,KSpiThreadPriority,KSpiThreadName);
sl@0
   225
	if(r == KErrNone)
sl@0
   226
		SetDfcQ((TDfcQue*)iDynamicDfcQ);
sl@0
   227
	DSimulatedIicBusChannelMasterSpi::SetRequestDelayed(this,EFalse);
sl@0
   228
	return r;
sl@0
   229
	}
sl@0
   230
sl@0
   231
TInt DSimulatedIicBusChannelMasterSpi::CheckHdr(TDes8* aHdr)
sl@0
   232
	{
sl@0
   233
	SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CheckHdr\n"));
sl@0
   234
sl@0
   235
	TConfigSpiBufV01* spiBuf = (TConfigSpiBufV01*)aHdr;
sl@0
   236
	TConfigSpiV01* spiPtr = &((*spiBuf)());
sl@0
   237
sl@0
   238
	// Valid values for the device ID will depend on the bus configuration
sl@0
   239
	//
sl@0
   240
	// Check that the values for word width, clock speed and clock mode are recognised
sl@0
   241
	if((spiPtr->iWordWidth < 0) || (spiPtr->iWordWidth > ESpiWordWidth_16))
sl@0
   242
		{
sl@0
   243
		SPI_PRINT(("ERROR: DSimulatedIicBusChannelMasterSpi::CheckHdr unrecognised word width identifier %d\n",spiPtr->iWordWidth));
sl@0
   244
		return KErrArgument;
sl@0
   245
		}
sl@0
   246
	if(spiPtr->iClkSpeedHz < 0)
sl@0
   247
		{
sl@0
   248
		SPI_PRINT(("ERROR: DSimulatedIicBusChannelMasterSpi::CheckHdr negative clock speed specified %d\n",spiPtr->iClkSpeedHz));
sl@0
   249
		return KErrArgument;
sl@0
   250
		}
sl@0
   251
	if((spiPtr->iClkMode < 0) || (spiPtr->iClkMode > ESpiPolarityHighRisingEdge))
sl@0
   252
		{
sl@0
   253
		SPI_PRINT(("ERROR: DSimulatedIicBusChannelMasterSpi::CheckHdr unrecognised clock mode identifier %d\n",spiPtr->iClkMode));
sl@0
   254
		return KErrArgument;
sl@0
   255
		}
sl@0
   256
	// Values for the timeout period are arbitrary - can only check it is not a negative value
sl@0
   257
	if(spiPtr->iTimeoutPeriod < 0)
sl@0
   258
		{
sl@0
   259
		SPI_PRINT(("ERROR: DSimulatedIicBusChannelMasterSpi::CheckHdr negative timeout period %d\n",spiPtr->iTimeoutPeriod));
sl@0
   260
		return KErrArgument;
sl@0
   261
		}
sl@0
   262
	if((spiPtr->iEndianness < 0) || (spiPtr->iEndianness > ELittleEndian))
sl@0
   263
		{
sl@0
   264
		SPI_PRINT(("ERROR: DSimulatedIicBusChannelMasterSpi::CheckHdr unrecognised endianness identifier %d\n",spiPtr->iEndianness));
sl@0
   265
		return KErrArgument;
sl@0
   266
		}
sl@0
   267
	if((spiPtr->iBitOrder < 0) || (spiPtr->iBitOrder > EMsbFirst))
sl@0
   268
		{
sl@0
   269
		SPI_PRINT(("ERROR: DSimulatedIicBusChannelMasterSpi::CheckHdr unrecognised bit order identifier %d\n",spiPtr->iBitOrder));
sl@0
   270
		return KErrArgument;
sl@0
   271
		}
sl@0
   272
	if((spiPtr->iSSPinActiveMode < 0) || (spiPtr->iSSPinActiveMode > ESpiCSPinActiveHigh))
sl@0
   273
		{
sl@0
   274
		SPI_PRINT(("ERROR: DSimulatedIicBusChannelMasterSpi::CheckHdr unrecognised Slave select pin mode identifier %d\n",spiPtr->iSSPinActiveMode));
sl@0
   275
		return KErrArgument;
sl@0
   276
		}
sl@0
   277
	SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CheckHdr word width = %d\n",spiPtr->iWordWidth));
sl@0
   278
	SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CheckHdr clock speed = %d\n",spiPtr->iClkSpeedHz));
sl@0
   279
	SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CheckHdr clock mode = %d\n",spiPtr->iClkMode));
sl@0
   280
	SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CheckHdr timeout period = %d\n",spiPtr->iTimeoutPeriod));
sl@0
   281
	SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CheckHdr endianness = %d\n",spiPtr->iEndianness));
sl@0
   282
	SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CheckHdr bit order = %d\n",spiPtr->iBitOrder));
sl@0
   283
	SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CheckHdr transaction wait cycles = %d\n",spiPtr->iTransactionWaitCycles));
sl@0
   284
	SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CheckHdr slave select pin mode = %d\n",spiPtr->iSSPinActiveMode));
sl@0
   285
sl@0
   286
	// For the set of tests expecft the following values
sl@0
   287
	// ESpiWordWidth_8, 100kHz, ESpiPolarityLowRisingEdge,aTimeoutPeriod=100
sl@0
   288
	// EBigEndian, EMsbFirst, 10 wait cycles, Slave select active low
sl@0
   289
	if(	(spiPtr->iWordWidth != ESpiWordWidth_8)	||
sl@0
   290
		(spiPtr->iClkSpeedHz != 100000)	||
sl@0
   291
		(spiPtr->iClkMode != ESpiPolarityLowRisingEdge)	||
sl@0
   292
		(spiPtr->iTimeoutPeriod != 100) ||
sl@0
   293
		(spiPtr->iEndianness != ELittleEndian) ||
sl@0
   294
		(spiPtr->iBitOrder != EMsbFirst) ||
sl@0
   295
		(spiPtr->iTransactionWaitCycles != 10) ||
sl@0
   296
		(spiPtr->iSSPinActiveMode != ESpiCSPinActiveLow) )
sl@0
   297
		return KErrCorrupt;
sl@0
   298
	return KErrNone;
sl@0
   299
	}
sl@0
   300
sl@0
   301
TInt DSimulatedIicBusChannelMasterSpi::CompareTransactionOne(TIicBusTransaction* aTransaction)
sl@0
   302
// Compares the indicated TIicBusTransaction with the expected content
sl@0
   303
// Returns KErrNone if there is a match, KErrCorrupt otherwise.
sl@0
   304
	{
sl@0
   305
	TInt i;
sl@0
   306
	// Check the transaction header
sl@0
   307
	// Should contain the default header for SPI
sl@0
   308
	TDes8* bufPtr = GetTransactionHeader(aTransaction);
sl@0
   309
	if(bufPtr == NULL)
sl@0
   310
		{
sl@0
   311
		SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CompareTransactionOne ERROR - NULL header\n"));
sl@0
   312
		return KErrCorrupt;
sl@0
   313
		}
sl@0
   314
	TConfigSpiV01 *buf = (TConfigSpiV01 *)(bufPtr->Ptr());
sl@0
   315
	if(buf->iWordWidth != ESpiWordWidth_8)
sl@0
   316
		{
sl@0
   317
		SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CompareTransactionOne ERROR - Header wordwidth mis-match\n"));
sl@0
   318
		return KErrCorrupt;
sl@0
   319
		}
sl@0
   320
	if(buf->iClkSpeedHz !=100000)
sl@0
   321
		{
sl@0
   322
		SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CompareTransactionOne ERROR - Header clockspeed mis-match\n"));
sl@0
   323
		return KErrCorrupt;
sl@0
   324
		}
sl@0
   325
	if(buf->iClkMode != ESpiPolarityLowRisingEdge)
sl@0
   326
		{
sl@0
   327
		SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CompareTransactionOne ERROR - Header polarity mis-match\n"));
sl@0
   328
		return KErrCorrupt;
sl@0
   329
		}
sl@0
   330
	if(buf->iTimeoutPeriod != 100)
sl@0
   331
		{
sl@0
   332
		SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CompareTransactionOne ERROR - Header timeout mis-match\n"));
sl@0
   333
		return KErrCorrupt;
sl@0
   334
		}
sl@0
   335
sl@0
   336
	SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CompareTransactionOne header OK\n"));
sl@0
   337
	
sl@0
   338
	// Check the half-duplex transfer list
sl@0
   339
	TIicBusTransfer* tfer = GetTransHalfDuplexTferPtr(aTransaction);
sl@0
   340
	if(tfer == NULL)
sl@0
   341
		{
sl@0
   342
		SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CompareTransactionOne ERROR - NULL half-duplex transfer\n"));
sl@0
   343
		return KErrCorrupt;
sl@0
   344
		}
sl@0
   345
	// Process each transfer in the list
sl@0
   346
	TInt8 dummy;
sl@0
   347
sl@0
   348
	// tfer1 = new TIicBusTransfer(TIicBusTransfer::EMasterWrite,8,buf1);
sl@0
   349
	// buf1 contains copy of TUint8 KTransOneTferOne[21] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
sl@0
   350
	dummy=GetTferType(tfer);
sl@0
   351
	if(dummy!=TIicBusTransfer::EMasterWrite)
sl@0
   352
		{
sl@0
   353
		SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CompareTransactionOne ERROR - tfer1 type=%d\n"));
sl@0
   354
		return KErrCorrupt;
sl@0
   355
		}
sl@0
   356
	dummy=GetTferBufGranularity(tfer);
sl@0
   357
	if(dummy!=8)
sl@0
   358
		{
sl@0
   359
		SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CompareTransactionOne ERROR - tfer1 granularity=%d\n",dummy));
sl@0
   360
		return KErrCorrupt;
sl@0
   361
		}
sl@0
   362
	if((bufPtr = (TDes8*)GetTferBuffer(tfer)) == NULL)
sl@0
   363
		{
sl@0
   364
		SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CompareTransactionOne ERROR - tfer1 buffer NULL\n"));
sl@0
   365
		return KErrCorrupt;
sl@0
   366
		}
sl@0
   367
	for(i=0;i<21;++i)
sl@0
   368
		{
sl@0
   369
		if((*bufPtr)[i]!=i)
sl@0
   370
			{
sl@0
   371
			SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CompareTransactionOne ERROR tfer1 buffer element %d has 0x%x\n",i,(*bufPtr)[i]));
sl@0
   372
			return KErrCorrupt;
sl@0
   373
			}
sl@0
   374
		}
sl@0
   375
	SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CompareTransactionOne tfer1 OK\n"));
sl@0
   376
sl@0
   377
sl@0
   378
	tfer=GetTferNextTfer(tfer);
sl@0
   379
	// tfer2 = new TIicBusTransfer(TIicBusTransfer::EMasterRead,8,buf2);
sl@0
   380
	// buf2 contains copy of TUint8 KTransOneTferTwo[8] = {17,18,19,20,21,22,23,24};
sl@0
   381
	dummy=GetTferType(tfer);
sl@0
   382
	if(dummy!=TIicBusTransfer::EMasterRead)
sl@0
   383
		{
sl@0
   384
		SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CompareTransactionOne ERROR - tfer2 type=%d\n",dummy));
sl@0
   385
		return KErrCorrupt;
sl@0
   386
		}
sl@0
   387
	dummy=GetTferBufGranularity(tfer);
sl@0
   388
	if(dummy!=8)
sl@0
   389
		{
sl@0
   390
		SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CompareTransactionOne ERROR - tfer2 granularity=%d\n",dummy));
sl@0
   391
		return KErrCorrupt;
sl@0
   392
		}
sl@0
   393
	if((bufPtr = (TDes8*)GetTferBuffer(tfer)) == NULL)
sl@0
   394
		{
sl@0
   395
		SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CompareTransactionOne ERROR - tfer2 buffer NULL\n"));
sl@0
   396
		return KErrCorrupt;
sl@0
   397
		}
sl@0
   398
	for(i=0;i<8;++i)
sl@0
   399
		{
sl@0
   400
		if((*bufPtr)[i]!=(17+i))
sl@0
   401
			{
sl@0
   402
			SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CompareTransactionOne ERROR tfer2 buffer element %d has 0x%x\n",i,(*bufPtr)[i]));
sl@0
   403
			return KErrCorrupt;
sl@0
   404
			}
sl@0
   405
		}
sl@0
   406
	SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CompareTransactionOne tfer2 OK\n"));
sl@0
   407
sl@0
   408
	tfer=GetTferNextTfer(tfer);
sl@0
   409
	// tfer3 = new TIicBusTransfer(TIicBusTransfer::EMasterWrite,8,buf3);
sl@0
   410
	// buf2 contains copy of TUint8 KTransOneTferThree[6] = {87,85,83,81,79,77};
sl@0
   411
	dummy=GetTferType(tfer);
sl@0
   412
	if(dummy!=TIicBusTransfer::EMasterWrite)
sl@0
   413
		{
sl@0
   414
		SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CompareTransactionOne ERROR - tfer3 type=%d\n"));
sl@0
   415
		return KErrCorrupt;
sl@0
   416
		}
sl@0
   417
	dummy=GetTferBufGranularity(tfer);
sl@0
   418
	if(dummy!=8)
sl@0
   419
		{
sl@0
   420
		SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CompareTransactionOne ERROR - tfer3 granularity=%d\n",dummy));
sl@0
   421
		return KErrCorrupt;
sl@0
   422
		}
sl@0
   423
	if((bufPtr = (TDes8*)GetTferBuffer(tfer)) == NULL)
sl@0
   424
		{
sl@0
   425
		SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CompareTransactionOne ERROR - tfer3 buffer NULL\n"));
sl@0
   426
		return KErrCorrupt;
sl@0
   427
		}
sl@0
   428
	for(i=0;i<6;++i)
sl@0
   429
		{
sl@0
   430
		if((*bufPtr)[i]!=(87-(2*i)))
sl@0
   431
			{
sl@0
   432
			SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CompareTransactionOne ERROR tfer3 buffer element %d has 0x%x\n",i,(*bufPtr)[i]));
sl@0
   433
			return KErrCorrupt;
sl@0
   434
			}
sl@0
   435
		}
sl@0
   436
	SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CompareTransactionOne tfer3 OK\n"));
sl@0
   437
sl@0
   438
	// Shouldn't be any more transfers in the half duplex list
sl@0
   439
	if((tfer=GetTferNextTfer(tfer))!=NULL)
sl@0
   440
		{
sl@0
   441
		SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CompareTransactionOne ERROR - tfer3 iNext=0x%x\n",tfer));
sl@0
   442
		return KErrCorrupt;
sl@0
   443
		}
sl@0
   444
	SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CompareTransactionOne half-duplex transfer OK\n"));
sl@0
   445
sl@0
   446
	// The full duplex transfer should be represented by a NULL pointer
sl@0
   447
	if((tfer=GetTransFullDuplexTferPtr(aTransaction))!=NULL)
sl@0
   448
		{
sl@0
   449
		SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CompareTransactionOne ERROR - full duplex pointer=0x%x\n",tfer));
sl@0
   450
		return KErrCorrupt;
sl@0
   451
		}
sl@0
   452
	SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CompareTransactionOne full duplex pointer is NULL (OK)\n"));
sl@0
   453
sl@0
   454
	// Synchronous transaction, so check the callback pointer is NULL
sl@0
   455
	TIicBusCallback* dumCb = NULL;
sl@0
   456
	dumCb=GetTransCallback(aTransaction);
sl@0
   457
	if(dumCb!=NULL)
sl@0
   458
		{
sl@0
   459
		SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CompareTransactionOne ERROR - callback pointer=0x%x\n",dumCb));
sl@0
   460
		return KErrCorrupt;
sl@0
   461
		}
sl@0
   462
	SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CompareTransactionOne callback pointer is NULL (OK)\n"));
sl@0
   463
sl@0
   464
	// Check the transaction flags are set to zero
sl@0
   465
	TUint8 dumFlags;
sl@0
   466
	dumFlags=GetTransFlags(aTransaction);
sl@0
   467
	if(dumFlags!=0)
sl@0
   468
		{
sl@0
   469
		SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CompareTransactionOne ERROR - flags=0x%x\n",dumFlags));
sl@0
   470
		return KErrCorrupt;
sl@0
   471
		}
sl@0
   472
	SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::CompareTransactionOne flags are zero (OK)\n"));
sl@0
   473
sl@0
   474
	return KErrNone;
sl@0
   475
	}
sl@0
   476
sl@0
   477
sl@0
   478
// The THwCallbackFunc gets called if the hardware preparation completes successfully.
sl@0
   479
void THwCallbackFunc(TAny* aPtr)
sl@0
   480
	{
sl@0
   481
	SPI_PRINT(("Hardware preparation completed, calling the callback function"));
sl@0
   482
	DSimulatedIicBusChannelMasterSpi* aChanMasterSpi=(DSimulatedIicBusChannelMasterSpi* )aPtr;
sl@0
   483
	TInt r = aChanMasterSpi->DoSimulatedTransaction();
sl@0
   484
	((DSimulatedIicBusChannelMasterSpi*)aChanMasterSpi)->CompleteReq(r);
sl@0
   485
	}
sl@0
   486
sl@0
   487
TInt DSimulatedIicBusChannelMasterSpi::DoSimulatedTransaction()
sl@0
   488
	{
sl@0
   489
	TInt r = AsynchStateMachine(EHwTransferDone);
sl@0
   490
	if(iTimeoutTimer.Cancel() == FALSE)
sl@0
   491
		{
sl@0
   492
		SPI_PRINT(("timer is not cancelled"));
sl@0
   493
		}
sl@0
   494
	return r;
sl@0
   495
	}
sl@0
   496
sl@0
   497
TInt DSimulatedIicBusChannelMasterSpi::DoHwPreparation()
sl@0
   498
	{
sl@0
   499
	SPI_PRINT(("Preparing hardware for the transaction"));
sl@0
   500
	
sl@0
   501
	TInt r = KErrNone;
sl@0
   502
	// The hardware preparation can either complete successfully or fail. 
sl@0
   503
	// If successful, invoke the callback function of THwDoneCallBack
sl@0
   504
	// if not, execute the timeout machanism
sl@0
   505
	// Currently DoHwPreparation is just a simple function. It should be more complicated 
sl@0
   506
	// for the real hardware.
sl@0
   507
	switch(iTestState)
sl@0
   508
		{
sl@0
   509
		case (ETestSlaveTimeOut):
sl@0
   510
			{
sl@0
   511
			// In the timeout test, do nothing until the timeout callback function is invoked.
sl@0
   512
			SPI_PRINT(("Simulating the timeout process."));
sl@0
   513
			break;
sl@0
   514
			}
sl@0
   515
		case (ETestNone):
sl@0
   516
			{
sl@0
   517
			// Pretend the hardware preparation's been done, and a callback function is invoked to call 
sl@0
   518
			// the Asynchronous State Machine
sl@0
   519
			SPI_PRINT(("Hardware preparing work is executing normally."));
sl@0
   520
			iCb->Enque();
sl@0
   521
			break;
sl@0
   522
			}
sl@0
   523
		default:
sl@0
   524
			{
sl@0
   525
			SPI_PRINT(("Not a valid test."));
sl@0
   526
			r = KErrNotSupported;
sl@0
   527
			break;
sl@0
   528
			}	
sl@0
   529
		}
sl@0
   530
	
sl@0
   531
	return r; 
sl@0
   532
	}
sl@0
   533
sl@0
   534
// Gateway function for PSL implementation, invoked for DFC processing
sl@0
   535
TInt DSimulatedIicBusChannelMasterSpi::DoRequest(TIicBusTransaction* aTransaction)
sl@0
   536
	{
sl@0
   537
	SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::DoRequest invoked with aTransaction=0x%x\n",aTransaction));
sl@0
   538
	
sl@0
   539
	TInt r = KErrNone;
sl@0
   540
	iCurrTrans=aTransaction;
sl@0
   541
	
sl@0
   542
	// Check if the Asynchronous State Machine is available. If not, then return KErrInUse. 
sl@0
   543
	// This is used to stop the second transaction executing if the machine has already been holding 
sl@0
   544
	// by a transaction. However, this situation cannot be tested because of the malfunction in PIL
sl@0
   545
	if(iChannelState!= EIdle)
sl@0
   546
		return KErrInUse;
sl@0
   547
	
sl@0
   548
	iChannelState = EBusy;
sl@0
   549
#ifdef IIC_INSTRUMENTATION_MACRO
sl@0
   550
	IIC_MPROCESSTRANS_START_PSL_TRACE;
sl@0
   551
#endif
sl@0
   552
	r = ProcessTrans(aTransaction);
sl@0
   553
sl@0
   554
	SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::DoRequest - exiting\n"));
sl@0
   555
	return r;
sl@0
   556
	}
sl@0
   557
sl@0
   558
TInt DSimulatedIicBusChannelMasterSpi::ProcessTrans(TIicBusTransaction* aTransaction)
sl@0
   559
		{
sl@0
   560
		TInt r=KErrNone;
sl@0
   561
		
sl@0
   562
		switch(iTestState)
sl@0
   563
			{
sl@0
   564
			case(ETestWaitTransOne):
sl@0
   565
				{
sl@0
   566
				// The transaction received should exhibit the expected data
sl@0
   567
				// Return KErrArgument if this is not the case
sl@0
   568
				// The timer should be started at the beginning of every transaction
sl@0
   569
				// For simplicity, we omit the timer here. 
sl@0
   570
				r=CompareTransactionOne(iCurrTrans);
sl@0
   571
				iChannelState = EIdle;
sl@0
   572
				CompleteRequest(KErrNone);
sl@0
   573
				break;
sl@0
   574
				}
sl@0
   575
			case(ETestSlaveTimeOut):
sl@0
   576
				{
sl@0
   577
				// Test the timeout funciton
sl@0
   578
				SPI_PRINT(("Test the slave timeout function\n"));
sl@0
   579
				
sl@0
   580
				// Simulate a timeout 
sl@0
   581
				// Start a timer and then wait for the Slave response to timeout
sl@0
   582
				// A real bus would use its own means to identify a timeout
sl@0
   583
				TInt aTime=1000000/NKern::TickPeriod();
sl@0
   584
				r = StartSlaveTimeOutTimer(aTime);
sl@0
   585
				if(r != KErrNone)
sl@0
   586
					return r;
sl@0
   587
				r = DoHwPreparation();
sl@0
   588
				break;
sl@0
   589
				}
sl@0
   590
			case(ETestWaitPriorityTest):
sl@0
   591
				{	
sl@0
   592
				static TInt TranCount = 0;
sl@0
   593
				if(TranCount >= KPriorityTestNum) return KErrUnknown;
sl@0
   594
				// block the channel
sl@0
   595
				while(IsRequestDelayed(this))
sl@0
   596
					{
sl@0
   597
					SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::DoRequest - starting Sleep...\n"));
sl@0
   598
					NKern::Sleep(1000);	// 1000 is arbitrary
sl@0
   599
					SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::DoRequest - completed Sleep, check if still delayed\n"));
sl@0
   600
					}; 
sl@0
   601
				// get transaction header			
sl@0
   602
				TDes8* bufPtr = GetTransactionHeader(aTransaction);
sl@0
   603
				if(bufPtr == NULL)
sl@0
   604
				{
sl@0
   605
				SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::DoRequest ERROR - NULL header\n"));
sl@0
   606
				return KErrCorrupt;
sl@0
   607
				}
sl@0
   608
sl@0
   609
				if(TranCount == 0)
sl@0
   610
					{			
sl@0
   611
					// ignore the blocking transaction
sl@0
   612
					TranCount++;
sl@0
   613
					}
sl@0
   614
				else
sl@0
   615
					{
sl@0
   616
					// store transaction header
sl@0
   617
					iPriorityTestResult[TranCount++] = (*bufPtr)[0];
sl@0
   618
					SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::DoRequest Priority test transaction no.:%d Priority:%d", 
sl@0
   619
						(*bufPtr)[0], aTransaction->iKey));
sl@0
   620
					}
sl@0
   621
				iChannelState = EIdle;
sl@0
   622
				CompleteRequest(KErrNone);
sl@0
   623
				if(TranCount == KPriorityTestNum) iPriorityTestDone = ETrue;
sl@0
   624
				break;
sl@0
   625
				}
sl@0
   626
sl@0
   627
sl@0
   628
sl@0
   629
			case(ETestNone):
sl@0
   630
				{
sl@0
   631
				SPI_PRINT(("Nothing to be tested, just do the usual transaction"));
sl@0
   632
				
sl@0
   633
				// Start the timer on the Slave response. 
sl@0
   634
				// Since no timeout, this timer will be cancelled in the THwCallbackFunc
sl@0
   635
				r = StartSlaveTimeOutTimer(100000);
sl@0
   636
				if(r != KErrNone)
sl@0
   637
					return r;
sl@0
   638
				// Use a class THwDoneCallBack derived from TDfc to trigger the asynchronous state machine 
sl@0
   639
				// when the hardware preparation completes successfully. 
sl@0
   640
				// The priority is set with an arbitrary number 5
sl@0
   641
				iCb = new THwDoneCallBack(THwCallbackFunc, this, iDynamicDfcQ, 5);
sl@0
   642
				r = DoHwPreparation(); 
sl@0
   643
				break;
sl@0
   644
				}
sl@0
   645
			default:
sl@0
   646
				{
sl@0
   647
				SPI_PRINT(("Test status not matched"));
sl@0
   648
				return KErrNotSupported;
sl@0
   649
				}
sl@0
   650
			}
sl@0
   651
sl@0
   652
		return r;
sl@0
   653
		}
sl@0
   654
	
sl@0
   655
	
sl@0
   656
TInt DSimulatedIicBusChannelMasterSpi::AsynchStateMachine(TInt aReason)
sl@0
   657
	{
sl@0
   658
	TInt r=KErrNone; 
sl@0
   659
	
sl@0
   660
	// The asynchronous state machine has two states, it could either be idle or busy.
sl@0
   661
	// Initially, it's in idle state. When a user queues a transaction, the hardware preparation starts
sl@0
   662
	// and the state changes to busy. After the hardware preparation completes, either successfully or not, 
sl@0
   663
	// the state machine will do the corresponding work for the transaction and then goes back to the idle state.  
sl@0
   664
	switch(iChannelState)
sl@0
   665
		{
sl@0
   666
		case(EIdle):
sl@0
   667
			{
sl@0
   668
			 return KErrGeneral;
sl@0
   669
			}
sl@0
   670
		case (EBusy):
sl@0
   671
			{
sl@0
   672
			switch(aReason)
sl@0
   673
				{
sl@0
   674
				case(EHwTransferDone):
sl@0
   675
					{
sl@0
   676
								
sl@0
   677
					// Simulate processing - for now, do nothing!
sl@0
   678
					//
sl@0
   679
					SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::AsynchStateMachine aTransaction->iHeader=0x%x\n",GetTransactionHeader(iCurrTrans)));
sl@0
   680
					SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::AsynchStateMachine aTransaction->iHalfDuplexTrans=0x%x\n",GetTransHalfDuplexTferPtr(iCurrTrans)));
sl@0
   681
					SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::AsynchStateMachine aTransaction->iFullDuplexTrans=0x%x\n",GetTransFullDuplexTferPtr(iCurrTrans)));
sl@0
   682
					SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::AsynchStateMachine aTransaction->iCallback=0x%x\n",GetTransCallback(iCurrTrans)));
sl@0
   683
					SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::AsynchStateMachine aTransaction->iFlags=0x%x\n",GetTransFlags(iCurrTrans)));
sl@0
   684
sl@0
   685
					SPI_PRINT(("\nDSimulatedIicBusChannelMasterSpi::AsynchStateMachine, iHeader info \n"));
sl@0
   686
					TDes8* bufPtr = GetTransactionHeader(iCurrTrans);
sl@0
   687
					if(bufPtr == NULL)
sl@0
   688
						{
sl@0
   689
						SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::AsynchStateMachine ERROR - NULL header\n"));
sl@0
   690
						return KErrCorrupt;
sl@0
   691
						}
sl@0
   692
					TConfigSpiV01 *buf = (TConfigSpiV01 *)(bufPtr->Ptr());
sl@0
   693
					SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::AsynchStateMachine, header word width=0x%x\n",buf->iWordWidth));
sl@0
   694
					SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::AsynchStateMachine, header clock speed=0x%x\n",buf->iClkSpeedHz));
sl@0
   695
					SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::AsynchStateMachine, header clock mode=0x%x\n",buf->iClkMode));
sl@0
   696
					SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::AsynchStateMachine, header timeout period=0x%x\n",buf->iTimeoutPeriod));
sl@0
   697
					SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::AsynchStateMachine, header endianness=0x%x\n",buf->iEndianness));
sl@0
   698
					SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::AsynchStateMachine, header bit order=0x%x\n",buf->iBitOrder));
sl@0
   699
					SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::AsynchStateMachine, header wait cycles=0x%x\n",buf->iTransactionWaitCycles));
sl@0
   700
					SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::AsynchStateMachine, header Slave select pin mode=0x%x\n",buf->iSSPinActiveMode));
sl@0
   701
					(void)buf;	// Silence compiler when SPI_PRINT not used
sl@0
   702
							
sl@0
   703
					SPI_PRINT(("\nDSimulatedIicBusChannelMasterSpi::AsynchStateMachine, iHalfDuplexTrans info \n"));
sl@0
   704
					TIicBusTransfer* halfDuplexPtr=GetTransHalfDuplexTferPtr(iCurrTrans);
sl@0
   705
					while(halfDuplexPtr != NULL)
sl@0
   706
						{
sl@0
   707
						SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::AsynchStateMachine transfer type=0x%x\n",GetTferType(halfDuplexPtr)));
sl@0
   708
						SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::AsynchStateMachine granularity=0x%x\n",GetTferBufGranularity(halfDuplexPtr)));
sl@0
   709
						SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::AsynchStateMachine transfer buffer=0x%x\n",GetTferBuffer(halfDuplexPtr)));
sl@0
   710
						SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::AsynchStateMachine next transfer =0x%x\n",GetTferNextTfer(halfDuplexPtr)));
sl@0
   711
						halfDuplexPtr=GetTferNextTfer(halfDuplexPtr);
sl@0
   712
						}
sl@0
   713
					SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::AsynchStateMachine - End of iHalfDuplexTrans info"));
sl@0
   714
					
sl@0
   715
					while(IsRequestDelayed(this))
sl@0
   716
						{
sl@0
   717
						SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::AsynchStateMachine - starting Sleep...\n"));
sl@0
   718
						NKern::Sleep(1000);	// 1000 is arbitrary
sl@0
   719
						SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::AsynchStateMachine - completed Sleep, check if still delayed\n"));
sl@0
   720
						}; 
sl@0
   721
				
sl@0
   722
					iChannelState=EIdle; 
sl@0
   723
					delete iCb;
sl@0
   724
					break;
sl@0
   725
					}
sl@0
   726
				case(ETimeExpired):
sl@0
   727
					{
sl@0
   728
					SPI_PRINT(("Time expired, the Asynchrnous State Machine will be Idle again, and wait for the next request."));
sl@0
   729
					iChannelState=EIdle;
sl@0
   730
					r = KErrTimedOut; 
sl@0
   731
					break;
sl@0
   732
					}
sl@0
   733
				default:
sl@0
   734
					{
sl@0
   735
					SPI_PRINT(("Request can not be handled, return error code."));
sl@0
   736
					return KErrNotSupported;
sl@0
   737
					}
sl@0
   738
				}
sl@0
   739
			break;
sl@0
   740
			}
sl@0
   741
		default:
sl@0
   742
			{
sl@0
   743
			SPI_PRINT(("No matched state"));
sl@0
   744
			return KErrGeneral;
sl@0
   745
			}
sl@0
   746
		}
sl@0
   747
	return r; 
sl@0
   748
	}
sl@0
   749
sl@0
   750
sl@0
   751
TBool DSimulatedIicBusChannelMasterSpi::IsRequestDelayed(DSimulatedIicBusChannelMasterSpi* aChan)
sl@0
   752
	{
sl@0
   753
	SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::IsRequestDelayed invoked for aChan=0x%x\n",aChan));
sl@0
   754
	return aChan->iReqDelayed;
sl@0
   755
	}
sl@0
   756
sl@0
   757
void DSimulatedIicBusChannelMasterSpi::SetRequestDelayed(DSimulatedIicBusChannelMasterSpi* aChan,TBool aDelay) 
sl@0
   758
	{
sl@0
   759
	SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::SetRequestDelayed invoked for aChan=0x%x, with aDelay=0x%d\n",aChan,aDelay));
sl@0
   760
	aChan->iReqDelayed=aDelay;
sl@0
   761
	}
sl@0
   762
sl@0
   763
TInt DSimulatedIicBusChannelMasterSpi::StaticExtension(TUint aFunction, TAny* aParam1, TAny* aParam2)
sl@0
   764
	{
sl@0
   765
	SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::StaticExtension\n"));
sl@0
   766
#ifdef IIC_INSTRUMENTATION_MACRO
sl@0
   767
	IIC_MSTATEXT_START_PSL_TRACE;
sl@0
   768
#endif
sl@0
   769
	(void)aParam1;
sl@0
   770
	(void)aParam2;
sl@0
   771
	TInt r = KErrNone;
sl@0
   772
	// Test values of aFunction were shifted left one place by the (test) client driver
sl@0
   773
	// and for Slave values the two msb were cleared
sl@0
   774
	// Return to its original value.
sl@0
   775
	if(aFunction>KTestControlIoPilOffset)
sl@0
   776
		aFunction >>= 1;
sl@0
   777
	switch(aFunction)
sl@0
   778
		{
sl@0
   779
		case(RBusDevIicClient::ECtlIoDumpChan):
sl@0
   780
			{
sl@0
   781
#ifdef _DEBUG
sl@0
   782
			DumpChannel();
sl@0
   783
#endif
sl@0
   784
			break;
sl@0
   785
			}
sl@0
   786
		case(RBusDevIicClient::ECtlIoBlockReqCompletion):
sl@0
   787
			{
sl@0
   788
			SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::Blocking request completion\n"));
sl@0
   789
			SetRequestDelayed(this, ETrue);
sl@0
   790
			break;
sl@0
   791
			}
sl@0
   792
		case(RBusDevIicClient::ECtlIoUnblockReqCompletion):
sl@0
   793
			{
sl@0
   794
			SPI_PRINT(("DSimulatedIicBusChannelMasterSpi::Unlocking request completion\n"));
sl@0
   795
			SetRequestDelayed(this, EFalse);
sl@0
   796
			break;
sl@0
   797
			}
sl@0
   798
		case(RBusDevIicClient::ECtlIoDeRegChan):
sl@0
   799
			{
sl@0
   800
#ifdef IIC_INSTRUMENTATION_MACRO
sl@0
   801
	IIC_DEREGISTERCHAN_START_PSL_TRACE;
sl@0
   802
#endif
sl@0
   803
			SPI_PRINT(("DSimulatedIicBusChannelMasterSpi: deregister channel\n"));
sl@0
   804
#ifndef STANDALONE_CHANNEL
sl@0
   805
			r=DIicBusController::DeRegisterChannel(this);
sl@0
   806
#endif
sl@0
   807
sl@0
   808
#ifdef IIC_INSTRUMENTATION_MACRO
sl@0
   809
	IIC_DEREGISTERCHAN_END_PSL_TRACE;
sl@0
   810
#endif
sl@0
   811
			break;
sl@0
   812
			}
sl@0
   813
sl@0
   814
		case(RBusDevIicClient::ECtlIoPriorityTest):
sl@0
   815
			{
sl@0
   816
			SPI_PRINT(("DSimulatedIicBusChannelMasterSpi: warned to expect priority test\n"));
sl@0
   817
			iPriorityTestDone = EFalse;
sl@0
   818
			iTestState=ETestWaitPriorityTest;
sl@0
   819
			break;
sl@0
   820
			}
sl@0
   821
		case(RBusDevIicClient::EGetTestResult):
sl@0
   822
			{
sl@0
   823
			if(!iPriorityTestDone) return KErrNotReady;
sl@0
   824
			
sl@0
   825
			SPI_PRINT(("DSimulatedIicBusChannelMasterSpi: get priority test order\n"));
sl@0
   826
sl@0
   827
			//iPriorityTestResult[0] is the blocking transaction, ignore it. start from entry 1.
sl@0
   828
			for(TInt i=1; i<KPriorityTestNum; i++)
sl@0
   829
				{
sl@0
   830
				if(iPriorityTestResult[i]!=(KPriorityTestNum-i-1))
sl@0
   831
					{
sl@0
   832
					r = KErrGeneral;
sl@0
   833
					break;
sl@0
   834
					}
sl@0
   835
				}
sl@0
   836
				r = KErrNone;
sl@0
   837
			break;
sl@0
   838
			}
sl@0
   839
sl@0
   840
		case(RBusDevIicClient::ECtlIoTracnOne):
sl@0
   841
			{
sl@0
   842
			SPI_PRINT(("DSimulatedIicBusChannelMasterSpi: warned to expect Transaction One\n"));
sl@0
   843
			iTestState=ETestWaitTransOne;
sl@0
   844
			break;
sl@0
   845
			}
sl@0
   846
		case(RBusDevIicClient::ECtlIoNone):
sl@0
   847
			{
sl@0
   848
			SPI_PRINT(("DSimulatedIicBusChannelMasterSpi: terminate ControlIO state\n"));
sl@0
   849
			iTestState=ETestNone;
sl@0
   850
			break;
sl@0
   851
			}
sl@0
   852
		case(RBusDevIicClient::ECtlIoSetTimeOutFlag):
sl@0
   853
			{
sl@0
   854
			SPI_PRINT(("DSimulatedIicBusChannelMasterSpi: test slave time out\n"));
sl@0
   855
			iTestState=ETestSlaveTimeOut;
sl@0
   856
			break;
sl@0
   857
			}
sl@0
   858
		default:
sl@0
   859
			{
sl@0
   860
			Kern::Printf("aFunction %d is not recognised \n",aFunction);
sl@0
   861
			r=KErrNotSupported;
sl@0
   862
			}
sl@0
   863
		}
sl@0
   864
#ifdef IIC_INSTRUMENTATION_MACRO
sl@0
   865
	IIC_MSTATEXT_END_PSL_TRACE;
sl@0
   866
#endif		
sl@0
   867
	return r;
sl@0
   868
	}
sl@0
   869
sl@0
   870
void DSimulatedIicBusChannelMasterSpi::CompleteReq(TInt aResult)
sl@0
   871
	{
sl@0
   872
#ifdef IIC_INSTRUMENTATION_MACRO
sl@0
   873
	IIC_MPROCESSTRANS_END_PSL_TRACE;
sl@0
   874
#endif
sl@0
   875
	CompleteRequest(aResult);
sl@0
   876
	}
sl@0
   877
sl@0
   878
sl@0
   879
void DSimulatedIicBusChannelSlaveSpi::SlaveAsyncSimCallback(TAny* aPtr)
sl@0
   880
	{
sl@0
   881
	SPI_PRINT(("SlaveAsyncSimCallback\n"));
sl@0
   882
	DSimulatedIicBusChannelSlaveSpi* channel = (DSimulatedIicBusChannelSlaveSpi*)aPtr;
sl@0
   883
	TInt r=KErrNone;	// Just simulate successfull capture
sl@0
   884
#ifdef IIC_INSTRUMENTATION_MACRO
sl@0
   885
	IIC_SCAPTCHANASYNC_END_PSL_TRACE;
sl@0
   886
#endif
sl@0
   887
	channel->ChanCaptureCb(r);
sl@0
   888
	}
sl@0
   889
sl@0
   890
#ifdef STANDALONE_CHANNEL
sl@0
   891
EXPORT_C
sl@0
   892
#endif
sl@0
   893
	DSimulatedIicBusChannelSlaveSpi::DSimulatedIicBusChannelSlaveSpi(const DIicBusChannel::TBusType aBusType, const DIicBusChannel::TChannelDuplex aChanDuplex)
sl@0
   894
	: DIicBusChannelSlave(aBusType,aChanDuplex,0), // 0 to be ignored by base class
sl@0
   895
	iSlaveTimer(SlaveAsyncSimCallback,this)
sl@0
   896
	{
sl@0
   897
	SPI_PRINT(("DSimulatedIicBusChannelSlaveSpi::DSimulatedIicBusChannelSlaveSpi, aBusType=%d,aChanDuplex=%d\n",aBusType,aChanDuplex));
sl@0
   898
#ifndef STANDALONE_CHANNEL
sl@0
   899
	iChannelNumber = AssignChanNum();
sl@0
   900
#endif
sl@0
   901
	SPI_PRINT(("DSimulatedIicBusChannelSlaveSpi::DSimulatedIicBusChannelSlaveSpi, iChannelNumber=%d\n",iChannelNumber));
sl@0
   902
	}
sl@0
   903
sl@0
   904
TInt DSimulatedIicBusChannelSlaveSpi::CaptureChannelPsl(TDes8* /*aConfigHdr*/, TBool aAsynch)
sl@0
   905
	{
sl@0
   906
	SPI_PRINT(("DSimulatedIicBusChannelSlaveSpi::CaptureChannelPsl, aAsynch=%d\n",aAsynch));
sl@0
   907
	TInt r = KErrNone;
sl@0
   908
	if(aAsynch)
sl@0
   909
		{
sl@0
   910
#ifdef IIC_INSTRUMENTATION_MACRO
sl@0
   911
	IIC_SCAPTCHANASYNC_START_PSL_TRACE;
sl@0
   912
#endif
sl@0
   913
		// To simulate an asynchronous operation, just set a timer to expire
sl@0
   914
		iSlaveTimer.OneShot(1000, ETrue); // Arbitrary timeout - expiry executes callback in context of DfcThread1
sl@0
   915
		}
sl@0
   916
	else
sl@0
   917
		{
sl@0
   918
#ifdef IIC_INSTRUMENTATION_MACRO
sl@0
   919
	IIC_SCAPTCHANSYNC_START_PSL_TRACE;
sl@0
   920
#endif
sl@0
   921
		// PSL processing would happen here ...
sl@0
   922
		// Expected to include implementation of the header configuration information 
sl@0
   923
#ifdef IIC_INSTRUMENTATION_MACRO
sl@0
   924
	IIC_SCAPTCHANSYNC_END_PSL_TRACE;
sl@0
   925
#endif
sl@0
   926
		}
sl@0
   927
	SPI_PRINT(("DSimulatedIicBusChannelSlaveI2c::CaptureChanSync ... no real processing to do \n"));
sl@0
   928
sl@0
   929
	return r;
sl@0
   930
	}
sl@0
   931
sl@0
   932
TInt DSimulatedIicBusChannelSlaveSpi::CheckHdr(TDes8* /*aHdr*/)
sl@0
   933
	{
sl@0
   934
	SPI_PRINT(("DSimulatedIicBusChannelSlaveSpi::CheckHdr\n"));
sl@0
   935
	return KErrNone;
sl@0
   936
	}
sl@0
   937
sl@0
   938
TInt DSimulatedIicBusChannelSlaveSpi::DoCreate()
sl@0
   939
	{
sl@0
   940
	SPI_PRINT(("DSimulatedIicBusChannelSlaveSpi::DoCreate\n"));
sl@0
   941
	TInt r=Init();	// PIL Base class initialisation
sl@0
   942
	return r;
sl@0
   943
	}
sl@0
   944
sl@0
   945
TInt DSimulatedIicBusChannelSlaveSpi::DoRequest(TInt /*aTrigger*/)
sl@0
   946
	{
sl@0
   947
	SPI_PRINT(("DSimulatedIicBusChannelSlaveSpi::DoRequest\n"));
sl@0
   948
	return KErrNotSupported; 
sl@0
   949
	}
sl@0
   950
sl@0
   951
void DSimulatedIicBusChannelSlaveSpi::ProcessData(TInt /*aTrigger*/, TIicBusSlaveCallback*  /*aCb*/)
sl@0
   952
	{
sl@0
   953
	SPI_PRINT(("DSimulatedIicBusChannelSlaveSpi::ProcessData\n"));
sl@0
   954
	}
sl@0
   955
sl@0
   956
TInt DSimulatedIicBusChannelSlaveSpi::StaticExtension(TUint aFunction, TAny* aParam1, TAny* aParam2)
sl@0
   957
	{
sl@0
   958
	SPI_PRINT(("DSimulatedIicBusChannelSlaveSpi::StaticExtension\n"));
sl@0
   959
#ifdef IIC_INSTRUMENTATION_MACRO
sl@0
   960
	IIC_SSTATEXT_START_PSL_TRACE;
sl@0
   961
#endif
sl@0
   962
	(void)aParam1;
sl@0
   963
	(void)aParam2;
sl@0
   964
	// Test values of aFunction were shifted left one place by the (test) client driver
sl@0
   965
	// and for Slave values the two msb were cleared
sl@0
   966
	// Return to its original value.
sl@0
   967
	if(aFunction>KTestControlIoPilOffset)
sl@0
   968
		{
sl@0
   969
		aFunction |= 0xC0000000;
sl@0
   970
		aFunction >>= 1;
sl@0
   971
		}
sl@0
   972
	TInt r = KErrNone;
sl@0
   973
	switch(aFunction)
sl@0
   974
		{
sl@0
   975
		case(RBusDevIicClient::ECtlIoDumpChan):
sl@0
   976
			{
sl@0
   977
#ifdef _DEBUG
sl@0
   978
			DumpChannel();
sl@0
   979
#endif
sl@0
   980
			break;
sl@0
   981
			}
sl@0
   982
		default:
sl@0
   983
			{
sl@0
   984
			Kern::Printf("aFunction %d is not recognised \n",aFunction);
sl@0
   985
			r=KErrNotSupported;
sl@0
   986
			}
sl@0
   987
		}
sl@0
   988
sl@0
   989
#ifdef IIC_INSTRUMENTATION_MACRO
sl@0
   990
	IIC_SSTATEXT_START_PSL_TRACE;
sl@0
   991
#endif
sl@0
   992
	(void)aFunction;
sl@0
   993
	return r;
sl@0
   994
	}
sl@0
   995
sl@0
   996
sl@0
   997
sl@0
   998
sl@0
   999
sl@0
  1000