1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/device/t_commsk.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,451 @@
1.4 +// Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of the License "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// e32test\device\t_commsk.cpp
1.18 +//
1.19 +//
1.20 +
1.21 +#include <e32base.h>
1.22 +#include <e32base_private.h>
1.23 +#include <e32test.h>
1.24 +#include <e32cons.h>
1.25 +#include <e32svr.h>
1.26 +#include <e32hal.h>
1.27 +#include <d32comm.h>
1.28 +#include <e32uid.h>
1.29 +#include <hal.h>
1.30 +
1.31 +#if defined (__WINS__)
1.32 +#define PDD_NAME _L("ECDRV.PDD")
1.33 +#define LDD_NAME _L("ECOMM.LDD")
1.34 +#else
1.35 +#define PDD_NAME _L("EUARTn")
1.36 +#define LDD_NAME _L("ECOMM")
1.37 +#endif
1.38 +
1.39 +// Our own comms object with synchronous writes
1.40 +class RComm : public RBusDevComm
1.41 + {
1.42 +public:
1.43 + TInt WriteS(const TDesC8& aDes);
1.44 + TInt WriteS(const TDesC8& aDes,TInt aLength);
1.45 + TInt WriteSE(const TDes8& Entered, TInt aLength);
1.46 + };
1.47 +
1.48 +LOCAL_D RTest test(_L("T_COMMSK"));
1.49 +RComm* theSerialPort;
1.50 +TCommCaps theCaps1Buf;
1.51 +TCommCapsV01& theCaps1=theCaps1Buf();
1.52 +TBool MediaChangeTestingEnabled;
1.53 +
1.54 +const TInt KWriteSize=250;
1.55 +const TInt KXonNumReads=0x10;
1.56 +const TInt KXonReadSize=0x400; // Must be bigger than comm buffer high water==0x300
1.57 +
1.58 +class TResult
1.59 + {
1.60 +public:
1.61 + enum TResTest {ETestPower,ESimpleWriting};
1.62 + TResult();
1.63 + void Display(CConsoleBase *aConsole,TInt aCycles);
1.64 + void Add(TResTest aTst,TInt anErr);
1.65 + inline TInt SetFreeMem(TInt aVal)
1.66 + {return(iFreeMem=aVal);}
1.67 +private:
1.68 + TInt iFreeMem;
1.69 + TInt iTestPowerFails;
1.70 + TInt iWriteFails;
1.71 + };
1.72 +
1.73 +class TSpeedAndName
1.74 + {
1.75 +public:
1.76 + TUint iMask;
1.77 + TBps iSpeed;
1.78 + const TText* iName;
1.79 + };
1.80 +
1.81 +const TSpeedAndName KSpeeds[]=
1.82 + {
1.83 + {KCapsBps50,EBps50,_S("50")},
1.84 + {KCapsBps2400,EBps2400,_S("2400")},
1.85 + {KCapsBps9600,EBps9600,_S("9600")},
1.86 + {KCapsBps19200,EBps19200,_S("19200")},
1.87 + {KCapsBps57600,EBps57600,_S("57600")},
1.88 + {KCapsBps115200,EBps115200,_S("115200")},
1.89 + };
1.90 +
1.91 +class TFrameAndName
1.92 + {
1.93 +public:
1.94 + TDataBits iData;
1.95 + TStopBits iStop;
1.96 + TParity iParity;
1.97 + const TText* iName;
1.98 + };
1.99 +
1.100 +const TFrameAndName KFrameTypes[]=
1.101 + {
1.102 + {EData8,EStop1,EParityNone,_S("8,N,1")},
1.103 + {EData8,EStop1,EParityEven,_S("8,E,1")},
1.104 + {EData8,EStop1,EParityOdd,_S("8,O,1")},
1.105 +
1.106 + {EData8,EStop2,EParityNone,_S("8,N,2")},
1.107 + {EData8,EStop2,EParityEven,_S("8,E,2")},
1.108 + {EData8,EStop2,EParityOdd,_S("8,O,2")},
1.109 +
1.110 + {EData7,EStop2,EParityNone,_S("7,N,2")},
1.111 + {EData7,EStop2,EParityEven,_S("7,E,2")},
1.112 + {EData7,EStop2,EParityOdd,_S("7,O,2")},
1.113 +
1.114 + {EData7,EStop1,EParityNone,_S("7,N,1")},
1.115 + {EData7,EStop1,EParityEven,_S("7,E,1")},
1.116 + {EData7,EStop1,EParityOdd,_S("7,O,1")},
1.117 + };
1.118 +
1.119 +class THandShakeAndName
1.120 + {
1.121 +public:
1.122 + TUint iHandshake;
1.123 + const TText* iName;
1.124 + };
1.125 +
1.126 +THandShakeAndName KHandshakes[]=
1.127 + {
1.128 + {KConfigObeyDSR,_S("DSR/DTR")},
1.129 + {KConfigObeyCTS,_S("CTS/RTS")},
1.130 + };
1.131 +
1.132 +enum TSerialTestFault {EBadArg,EUnknownSignal};
1.133 +
1.134 +TInt RComm::WriteS(const TDesC8& aDes)
1.135 +
1.136 +// Syncronous write
1.137 +
1.138 + {
1.139 + return(WriteS(aDes,aDes.Length()));
1.140 + }
1.141 +
1.142 +TInt RComm::WriteS(const TDesC8& aDes,TInt aLength)
1.143 +//
1.144 +// Syncronous write
1.145 +//
1.146 + {
1.147 +
1.148 + TRequestStatus s;
1.149 + Write(s,aDes,aLength);
1.150 + User::WaitForRequest(s);
1.151 + return(s.Int());
1.152 + }
1.153 +
1.154 +TResult::TResult()
1.155 + {
1.156 +
1.157 + iTestPowerFails=0;
1.158 + iFreeMem=0;
1.159 + iWriteFails=0;
1.160 + }
1.161 +
1.162 +void TResult::Display(CConsoleBase *aConsole,TInt aCycles)
1.163 +//
1.164 +// Display test results:
1.165 +//
1.166 + {
1.167 +
1.168 + test.Console()->ClearScreen();
1.169 + TInt xStartPos=0;
1.170 + TInt yStartPos=2;
1.171 +
1.172 + aConsole->SetPos(xStartPos,yStartPos);
1.173 + test.Printf(_L("Total cycles : %d"),aCycles);
1.174 +
1.175 + // Display results of Power Down/Up followed by a simple write/read to the Serial Port
1.176 + aConsole->SetPos(xStartPos,yStartPos+2);
1.177 + if (MediaChangeTestingEnabled)
1.178 + test.Printf(_L("Media Change failures: %d\n"),iTestPowerFails);
1.179 + else
1.180 + test.Printf(_L("Power test failures : %d\n"),iTestPowerFails);
1.181 + aConsole->SetPos(xStartPos,yStartPos+3);
1.182 + test.Printf(_L("Serial test failures : %d\n"),iWriteFails);
1.183 +
1.184 + aConsole->SetPos(xStartPos,yStartPos+5);
1.185 + test.Printf(_L("Free mem (in bytes) : %d"),iFreeMem);
1.186 + }
1.187 +
1.188 +void TResult::Add(TResTest aTst,TInt anErr)
1.189 +//
1.190 +// Increment the corresponding variable if test fails
1.191 +//
1.192 + {
1.193 +
1.194 + if (anErr!=KErrNone)
1.195 + {
1.196 + switch (aTst)
1.197 + {
1.198 + case ETestPower:
1.199 + if (anErr!=KErrNone)
1.200 + ++iTestPowerFails;
1.201 + break;
1.202 + case ESimpleWriting:
1.203 + if (anErr!=KErrNone)
1.204 + ++iWriteFails;
1.205 + break;
1.206 + default:
1.207 + iTestPowerFails=0;
1.208 + break;
1.209 + }
1.210 + }
1.211 + }
1.212 +
1.213 +#ifdef _DEBUG_DEVCOMM
1.214 +LOCAL_C void CommDebug(RBusDevComm& aComm)
1.215 + {
1.216 + TCommDebugInfoPckg infopckg;
1.217 + TCommDebugInfo& info = infopckg();
1.218 + aComm.DebugInfo(infopckg);
1.219 +
1.220 + test.Printf(_L(" LDD State : TX RX \r\n"));
1.221 + test.Printf(_L(" Busy : %10d %10d\r\n"), info.iTxBusy, info.iRxBusy);
1.222 + test.Printf(_L(" Held : %10d %10d\r\n"), info.iTxHeld, info.iRxHeld);
1.223 + test.Printf(_L(" Length : %10d %10d\r\n"), info.iTxLength, info.iRxLength);
1.224 + test.Printf(_L(" Offset : %10d %10d\r\n"), info.iTxOffset, info.iRxOffset);
1.225 + test.Printf(_L(" Int Count : %10d %10d\r\n"), info.iTxIntCount, info.iRxIntCount);
1.226 + test.Printf(_L(" Err Count : %10d %10d\r\n"), info.iTxErrCount, info.iRxErrCount);
1.227 + test.Printf(_L(" Buf Count : %10d %10d\r\n"), info.iTxBufCount, info.iRxBufCount);
1.228 + test.Printf(_L(" Fill/Drain : %10d %10d\r\n"), info.iFillingTxBuf, info.iFillingTxBuf);
1.229 + test.Printf(_L(" XON : %10d %10d\r\n"), info.iTxXon, info.iRxXon);
1.230 + test.Printf(_L(" XOFF : %10d %10d\r\n"), info.iTxXoff, info.iRxXoff);
1.231 + test.Printf(_L(" Chars : %10d %10d\r\n"), info.iTxChars, info.iRxChars);
1.232 + }
1.233 +#else
1.234 +void CommDebug(RBusDevComm& /*aComm*/)
1.235 + {
1.236 + test.Printf(_L("Debug Dump not available\r\n"));
1.237 + }
1.238 +#endif
1.239 +
1.240 +LOCAL_C void Panic(TSerialTestFault const& aFault)
1.241 +//
1.242 +// Panic the test code
1.243 +//
1.244 + {
1.245 + User::Panic(_L("Comm Test"),aFault);
1.246 + }
1.247 +
1.248 +LOCAL_C void StripeMem(TDes8& aBuf,TUint aStartChar,TUint anEndChar)
1.249 +//
1.250 +// Mark a buffer with repeating byte pattern
1.251 +//
1.252 + {
1.253 +
1.254 + __ASSERT_ALWAYS(aStartChar<=anEndChar,Panic(EBadArg));
1.255 + if (aStartChar==anEndChar)
1.256 + {
1.257 + aBuf.Fill(aStartChar);
1.258 + return;
1.259 + }
1.260 +
1.261 + TUint character=aStartChar;
1.262 + for (TInt i=0;i<aBuf.Length();i++)
1.263 + {
1.264 + aBuf[i]=(TText8)character;
1.265 + if(++character>anEndChar)
1.266 + character=aStartChar;
1.267 + }
1.268 + }
1.269 +
1.270 +LOCAL_C TInt TestSimpleWriting()
1.271 + {
1.272 +
1.273 + const TInt KBufSize=100;
1.274 + TUint8* inBuf=new TUint8[KBufSize];
1.275 + TUint8* outBuf=new TUint8[KBufSize];
1.276 + TPtr8 outDes(outBuf,KBufSize,KBufSize);
1.277 + TPtr8 inDes(inBuf,KBufSize,KBufSize);
1.278 + StripeMem(outDes,'A','Z');
1.279 + inDes.FillZ();
1.280 + inDes.SetLength(1);
1.281 +
1.282 + TRequestStatus readStatus;
1.283 + TRequestStatus timeStatus;
1.284 +
1.285 + theSerialPort->WriteS(outDes,KBufSize);
1.286 + theSerialPort->Read(readStatus,inDes);
1.287 +
1.288 + RTimer tim;
1.289 + test(tim.CreateLocal()==KErrNone);
1.290 + tim.After(timeStatus,2000000); // Async. timer request - 2Secs
1.291 +
1.292 +
1.293 + User::WaitForRequest(timeStatus,readStatus);
1.294 +
1.295 + if (readStatus!=KRequestPending)
1.296 + {
1.297 + // Serial request is complete - cancel timer
1.298 + tim.Cancel();
1.299 + User::WaitForRequest(timeStatus);
1.300 + if (readStatus==KErrNone)
1.301 + {
1.302 + test(inDes.Length()==inDes.MaxLength());
1.303 + test(inDes.Length()==KBufSize);
1.304 + outDes.SetLength(inDes.Length());
1.305 + return((inDes.Compare(outDes)==0)?KErrNone:KErrGeneral);
1.306 + }
1.307 + else
1.308 + return KErrGeneral;
1.309 + }
1.310 +
1.311 + else if (timeStatus!=KRequestPending)
1.312 + {
1.313 + // Timed out before Serial test completed
1.314 + theSerialPort->ReadCancel(); // Cancel serial read
1.315 + User::WaitForRequest(readStatus);
1.316 + return KErrTimedOut;
1.317 + }
1.318 + else
1.319 + Panic(EUnknownSignal);
1.320 + return(KErrNone); // Never gets here
1.321 + }
1.322 +
1.323 +LOCAL_C TInt SetUp()
1.324 +//
1.325 +// Set up the serial port
1.326 +//
1.327 + {
1.328 +
1.329 + theSerialPort->QueryReceiveBuffer();
1.330 +
1.331 + TCommConfig cBuf;
1.332 + TCommConfigV01& c=cBuf();
1.333 + theSerialPort->Config(cBuf);
1.334 +
1.335 + c.iFifo=EFifoEnable;
1.336 + c.iDataBits=EData8;
1.337 + c.iStopBits=EStop1;
1.338 + c.iParity=EParityNone;
1.339 + c.iRate=EBps115200;
1.340 + c.iHandshake=0;
1.341 +
1.342 + TInt r=theSerialPort->SetConfig(cBuf);
1.343 + return (r);
1.344 + }
1.345 +
1.346 +LOCAL_C TInt TestPower()
1.347 + {
1.348 +
1.349 + User::After(1000000); // Allow 1 second before power down
1.350 + RTimer timer;
1.351 + TRequestStatus done;
1.352 + timer.CreateLocal();
1.353 + TTime wakeup;
1.354 + wakeup.HomeTime();
1.355 + wakeup+=TTimeIntervalSeconds(10);
1.356 + timer.At(done,wakeup);
1.357 + UserHal::SwitchOff();
1.358 + User::WaitForRequest(done);
1.359 + return (done.Int());
1.360 + }
1.361 +
1.362 +GLDEF_C TInt E32Main()
1.363 + {
1.364 +
1.365 +// test.SetLogged(EFalse); // Turn off serial port debugging!
1.366 +
1.367 + test.Title();
1.368 + test.Start(_L("Comms Soak Test"));
1.369 +
1.370 + TBuf <0x100> cmd;
1.371 + User::CommandLine(cmd);
1.372 + TInt port=0;
1.373 + if ((cmd.Length()>0) && (cmd[0]>='1' && cmd[0]<='4'))
1.374 + port=(TInt)(cmd[0]-'0');
1.375 +
1.376 + test.Next(_L("Load Ldd/Pdd"));
1.377 + TInt r;
1.378 + TBuf<10> pddName=PDD_NAME;
1.379 +#if !defined (__WINS__)
1.380 + pddName[5]=(TText)('1'+port);
1.381 + TInt muid=0;
1.382 + if (HAL::Get(HAL::EMachineUid, muid)==KErrNone)
1.383 + {
1.384 + // Brutus uses EUART4 for both COM3 and COM4
1.385 + if (muid==HAL::EMachineUid_Brutus && port==4)
1.386 + pddName[5]=(TText)'4';
1.387 + }
1.388 +#endif
1.389 + r=User::LoadPhysicalDevice(pddName);
1.390 + test.Printf(_L("Load %S returned %d\n\r"),&pddName,r);
1.391 + test(r==KErrNone || r==KErrAlreadyExists);
1.392 +
1.393 + r=User::LoadLogicalDevice(LDD_NAME);
1.394 + test.Printf(_L("Load LDD returned %d\n\r"),r);
1.395 + test(r==KErrNone || r==KErrAlreadyExists);
1.396 +
1.397 + theSerialPort=new RComm;
1.398 + test(theSerialPort!=NULL);
1.399 +
1.400 + // Set up the serial port
1.401 + theSerialPort->Open(port);
1.402 + r=SetUp();
1.403 + test(r==KErrNone);
1.404 +
1.405 + MediaChangeTestingEnabled=EFalse;
1.406 + test.Printf(_L("\r\nThis test requires a loopback conector on the Serial Port\r\n"));
1.407 + test.Printf(_L("<<Hit M for media change testing>>\r\n"));
1.408 + test.Printf(_L("<<Any other key for power testing>>\r\n"));
1.409 + TChar c=(TUint)test.Getch();
1.410 + c.UpperCase();
1.411 + if (c=='M')
1.412 + MediaChangeTestingEnabled=ETrue;
1.413 +
1.414 + // Continuous Test
1.415 + TInt cycles=0;
1.416 + TResult results;
1.417 +
1.418 + TRequestStatus stat;
1.419 + test.Console()->Read(stat);
1.420 +
1.421 + while (stat==KRequestPending)
1.422 + {
1.423 + // Calculate the amount of free memory
1.424 + TMemoryInfoV1Buf membuf;
1.425 + UserHal::MemoryInfo(membuf);
1.426 + TMemoryInfoV1 &memoryInfo=membuf();
1.427 + results.SetFreeMem(memoryInfo.iFreeRamInBytes);
1.428 +
1.429 + if (MediaChangeTestingEnabled)
1.430 + {
1.431 + // Media Change Test (if enabled) then SetUp Serial Port and test we can still read/write
1.432 +// UserSvr::ForceRemountMedia(ERemovableMedia0); // Generate media change
1.433 +// User::After(1000000); // Allow 1 second after power down
1.434 + }
1.435 + else
1.436 + {
1.437 + // Power Down then test we can still read/write
1.438 + r=TestPower();
1.439 + results.Add(TResult::ETestPower,r);
1.440 + }
1.441 + r=TestSimpleWriting();
1.442 + results.Add(TResult::ESimpleWriting,r);
1.443 +
1.444 + // Display the results of the test cycle
1.445 + cycles++;
1.446 + results.Display(test.Console(),cycles);
1.447 + }
1.448 +
1.449 + User::WaitForRequest(stat);
1.450 + theSerialPort->Close();
1.451 + test.End();
1.452 +
1.453 + return(KErrNone);
1.454 + }