Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Related Pages

EchoTest.cxx

00001 // Test program to send a single ECHO primitive
00002 
00003 #include <iostream>
00004 using namespace std;
00005 
00006 #include <ctype.h>
00007 
00008 #include "RodModule.h"
00009 #include "BaseException.h"
00010 
00011 #include "primParams.h"
00012 #include "RCCVmeInterface.h"
00013 #include "parameters.h"
00014 
00015 int main(int argc, char *argv[]) {
00016 
00017 using namespace SctPixelRod;
00018 
00019   const unsigned long mapSize=0xc00040;         // VME map size 
00020   RodPrimList primList(1);                      // Primitive List
00021   long myTextLength;                            // Actual length of text message
00022   TEXT_BUFFER_TYPE myTextType;                  // Buffer type for latest message
00023   long numSlaves = 4;
00024   PrimState returnPState;
00025   TextBuffState returnTState;
00026 
00027   std::string fileName(""), option;
00028   long dataLength = -1;
00029   int slot = -1;
00030   int repetitions = 1;
00031 
00032   unsigned long baseAddress;
00033 
00034   if (argc > 1) {
00035     for (int i=1; i<argc; i++) {
00036       option = argv[i];
00037       if (option[0] != '-') break;
00038       switch (option[1]) {
00039         case 's': {
00040           slot = atoi(option.substr(2).c_str());
00041           break;
00042         }
00043         case 'l': {
00044           dataLength = atoi(option.substr(2).c_str());
00045           break;
00046         }
00047         case 'r': {
00048           repetitions = atoi(option.substr(2).c_str());
00049           break;
00050         }
00051         default: {
00052           break;
00053         }
00054       }
00055     }
00056   }
00057 
00058 // Prompt for slot number
00059   if (slot < 0 ) {
00060     cout << "Enter slot number (decimal):"; 
00061     cin >> slot;
00062     while ((slot < 1) || (slot > 21)) {
00063       cout << "Slot number out or range [1:21], re-enter: ";
00064       cin >> slot;
00065     }
00066   }
00067   baseAddress = slot << 24;
00068 
00069 // Create VME interface
00070   RCCVmeInterface *vme1;
00071   try {
00072     vme1 = new RCCVmeInterface();
00073   }
00074   catch (VmeException &v) {
00075     cout << "VmeException creating VME interface." << endl;
00076     cout << "ErrorClass = " << v.getErrorClass() << endl;
00077     cout << "ErrorCode = " << v.getErrorCode() << endl; 
00078 
00079     return 1;
00080   }
00081   
00082 // Create RodModule and initialize it
00083   RodModule* rod0 = new RodModule(baseAddress, mapSize, *vme1, numSlaves);
00084   try{
00085     rod0->initialize();
00086   }
00087   catch (HpiException &h) {
00088     cout << h;
00089 
00090     return 1;
00091   }
00092   catch (VmeException &v) {
00093     cout << "VmeException initialising RodModule." << endl;
00094     cout << "ErrorClass = " << v.getErrorClass() << endl;
00095     cout << "ErrorCode = " << v.getErrorCode() << endl; 
00096 
00097     return 1;
00098   }
00099   catch (RodException &r) {
00100     cout << r;
00101 
00102     return 1;
00103   }
00104   catch (BaseException &b) {
00105     cout << b;
00106 
00107     return 1;
00108   }
00109 
00110 // Create a buffer for text messages
00111   char * myTextBuffer;
00112   try {
00113     myTextBuffer= new char[TEXT_BUFF_SIZE];
00114   }
00115   catch (std::bad_alloc) {
00116     cout << "Unable to allocate text buffer in main.";
00117   }
00118 // Create and Send a simple ECHO primitive
00119   
00120   long* echoData;
00121 
00122   if(dataLength < 0) {
00123     cout << "Enter number of data words: ";
00124 
00125     cin >> dataLength;
00126   }
00127 
00128   try {
00129     echoData = new long[dataLength];
00130   }
00131   catch (std::bad_alloc) {
00132     cout << "Unable to allocate echoData in main.";
00133     return 1;
00134   }
00135 
00136   for (long i=0; i<dataLength; i++) {
00137     echoData[i] = 0xbeef0000 | i;
00138   }
00139   RodPrimitive* echo;
00140   try {
00141     echo= new RodPrimitive(dataLength+4, 1, ECHO, R_ECHO, echoData);
00142   }
00143   catch (std::bad_alloc) {
00144     cout << "Unable to allocate ECHO primitive in main.";
00145 
00146     return 1;
00147   }
00148 
00149   primList.insert(primList.begin(), *echo);
00150   try {
00151     primList.bufferBuild();
00152   }
00153   catch (PrimListException &p) {
00154     cout << p.getDescriptor() << " ";
00155     cout << p.getData1() << ", " << p.getData2() << "\n";
00156   };
00157 
00158   if(repetitions > 1) {
00159     cout << "Repeating " << repetitions << "times\n";
00160   }
00161 
00162   for(int reps=0; reps<repetitions; reps++) {
00163     if(repetitions > 1) {
00164       cerr << ".";
00165     }
00166 
00167     try {
00168       rod0->sendPrimList(&primList);
00169     }
00170     catch (VmeException &v) {
00171       cout << "VmeException in SendPrimList." << endl;
00172       cout << "ErrorClass = " << v.getErrorClass() << endl;
00173       cout << "ErrorCode = " << v.getErrorCode() << endl; 
00174 
00175       return 1;
00176     }
00177     catch (BaseException &h) {
00178       cout << h;
00179 
00180       return 1;
00181     }
00182   
00183 // Wait for ROD to begin executing and then wait for it to finish executing
00184 // Check for error messages in text buffer and read them out if they exist.
00185 // Note: this is NOT how primHandler and textHandler will be used once we
00186 // go to threads.  This is for debugging the code only.
00187     do {
00188       try {
00189         returnPState = rod0->primHandler();
00190       }
00191       catch (VmeException &v) {
00192         cout << "VmeException in first primHandler call." << endl;
00193         cout << "ErrorClass = " << v.getErrorClass() << endl;
00194         cout << "ErrorCode = " << v.getErrorCode() << endl; 
00195 
00196         return 1;
00197       }
00198       try {
00199         returnTState = rod0->textHandler();
00200       }
00201       catch (VmeException &v) {
00202         cout << "VmeException in first textHandler call." << endl;
00203         cout << "ErrorClass = " << v.getErrorClass() << endl;
00204         cout << "ErrorCode = " << v.getErrorCode() << endl; 
00205 
00206         return 1;
00207       }
00208       if (returnTState == TEXT_RQ_SET) {
00209         do {
00210           try {
00211             returnTState = rod0->textHandler();
00212           }
00213           catch (VmeException &v) {
00214             cout << "VmeException in second textHandler call." << endl;
00215             cout << "ErrorClass = " << v.getErrorClass() << endl;
00216             cout << "ErrorCode = " << v.getErrorCode() << endl; 
00217 
00218             return 1;
00219           }
00220         } while (returnTState != TEXT_READOUT);
00221         rod0->getTextBuffer(myTextBuffer, myTextLength, myTextType);
00222         rod0->clearTextBuffer();
00223         for (int i=0; i<myTextLength; i++) {
00224           cout << myTextBuffer[i];
00225           if (0==(i+1)%64) cout << endl;
00226         }
00227         cout << endl; 
00228       }
00229     } while (returnPState != PRIM_EXECUTING); 
00230     do {
00231       try {
00232         returnPState = rod0->primHandler();
00233       }
00234       catch (RodException &r) {
00235         cout << r;
00236         return 1;
00237       }
00238       catch (VmeException &v) {
00239         cout << "VmeException in second primHandler call." << endl;
00240         cout << "ErrorClass = " << v.getErrorClass() << endl;
00241         cout << "ErrorCode = " << v.getErrorCode() << endl; 
00242 
00243         return 1;
00244       }
00245       catch (BaseException &r) {
00246         cout << r;
00247         return 1;
00248       }
00249     } while (returnPState != PRIM_WAITING && returnPState != PRIM_IDLE); 
00250   
00251 // Retrieve output buffer
00252     RodOutList* outList = rod0->getOutList();
00253 
00254     if(outList) {
00255 // Print results (User processing of outList)
00256       UINT32 outListLength = UINT32(outList->getLength());
00257       unsigned long* outBody = outList->getBody();
00258       UINT32 outLength = UINT32(outBody[0]);
00259 
00260       if(outLength != outListLength) {
00261         // These should be identical (they're read from the same address!)
00262         //  but descrepencies have been seen
00263         cout << " ** Mismatch between RodOutList length and first word of RodOutList body!";
00264         cout << "   outLength = 0x" << hex << outLength << " outListLength = 0x" << outListLength << dec << endl;
00265 
00266         // Should be able to continue with next iteration, as its a momentary glitch
00267         continue;
00268       }
00269 
00270       UINT32 outIndex = UINT32(outBody[1]);
00271       UINT32 outNumPrims = outBody[2];
00272       UINT32 outPrimVersion = outBody[3];
00273       if(reps == 0) {
00274         cout << "outLength = " << outLength << ", outIndex = " << outIndex << 
00275                 ", outNumPrims = " << outNumPrims << ", outPrimVersion = " << 
00276                 outPrimVersion <<'\n';
00277       }
00278       int outPtr = 4;
00279       for (UINT32 j=0; j<outNumPrims; j++) {
00280         UINT32 primLength = outBody[outPtr++];
00281         UINT32 primIndex = outBody[outPtr++];
00282         UINT32 primId = outBody[outPtr++];
00283         UINT32 primVersion = outBody[outPtr++];
00284 
00285         if(reps>=1) {
00286           for(UINT32 i=0; i<primLength-4; i++) {
00287             if(outBody[i+outPtr] != echoData[i]) {
00288               cout << "Echo Check failed in word " << i << " of repetition " << reps << "\n";
00289               cout << "0x" << hex << outBody[i+outPtr] << " != 0x" << echoData[i] << dec << endl;
00290               return 1;
00291             }
00292           }
00293         } else {
00294           cout << "primLength = " << primLength << ", primIndex = " << primIndex << 
00295                   ", primId = " << primId << ", primVersion = " << primVersion << '\n';
00296           cout << "ECHO data: " << endl;
00297           hex(cout);
00298 
00299           for (UINT32 i=0; i<primLength-4; i++) {
00300             cout.width(8);
00301             cout << outBody[i+outPtr] <<" ";
00302             if (0 == (i+1)%8) cout << endl; 
00303           };
00304           if (0 != (primLength-4)%8) cout << endl;
00305           dec(cout);
00306         }
00307       };
00308 
00309 // Copy to local buffer
00310       unsigned long* myOutBody;
00311       try {
00312         myOutBody= new unsigned long[outLength];
00313       }
00314       catch (std::bad_alloc) {
00315         cout << "Unable to allocate myOutBody in main." << endl;
00316 
00317         return 1;
00318       }
00319       for (UINT32 i=0; i<outLength; i++) {
00320         myOutBody[i] = outBody[i];
00321       };
00322 
00323       delete [] myOutBody;
00324       rod0->deleteOutList();
00325     } else {
00326       cout << "No response from echo primitive!\n";
00327     }
00328   }
00329 
00330   if(repetitions > 1)
00331     cerr << endl;
00332 
00333 // Test ostream output
00334   cout << "Rod Status:" << endl << *rod0 << endl;
00335 
00336   cout << "Check text buffers before exiting:\n";
00337 
00338   try {
00339     returnTState = rod0->textHandler();
00340   }
00341   catch (VmeException &v) {
00342     cout << "VmeException in first textHandler call." << endl;
00343     cout << "ErrorClass = " << v.getErrorClass() << endl;
00344     cout << "ErrorCode = " << v.getErrorCode() << endl; 
00345 
00346     return 1;
00347   }
00348   if (returnTState == TEXT_RQ_SET) {
00349     do {
00350       try {
00351         returnTState = rod0->textHandler();
00352       }
00353       catch (VmeException &v) {
00354         cout << "VmeException in second textHandler call." << endl;
00355         cout << "ErrorClass = " << v.getErrorClass() << endl;
00356         cout << "ErrorCode = " << v.getErrorCode() << endl; 
00357 
00358         return 1;
00359       }
00360     } while (returnTState != TEXT_READOUT);
00361     rod0->getTextBuffer(myTextBuffer, myTextLength, myTextType);
00362     rod0->clearTextBuffer();
00363     for (int i=0; i<myTextLength; i++) {
00364       cout << myTextBuffer[i];
00365       if (0==(i+1)%64) cout << endl;
00366     }
00367     cout << endl; 
00368   }
00369 
00370 
00371   cout << "Text buffers checked\n";
00372   
00373 // Test reset of master DSP 
00374 /*  try { 
00375     rod0->resetMasterDsp();
00376   }
00377   catch (VmeException &v) {
00378     cout << "VmeException in resetMasterDsp()." << endl;
00379     cout << "ErrorClass = " << v.getErrorClass() << endl;
00380     cout << "ErrorCode = " << v.getErrorCode() << endl; 
00381 
00382     return 1;
00383   };
00384   try {
00385     rod0->resetAllDsps();
00386   }
00387   catch (VmeException &v) {
00388     cout << "VmeException in resetAllDsps()." << endl;
00389     cout << "ErrorClass = " << v.getErrorClass() << endl;
00390     cout << "ErrorCode = " << v.getErrorCode() << endl; 
00391 
00392     return 1;
00393   };
00394 */
00395 
00396 // Clean up: clear primList, delete primitive, delete the outList
00397   primList.clear();
00398   delete [] echoData;
00399   delete echo;
00400   delete [] myTextBuffer;
00401   rod0->deleteOutList();
00402   
00403 // Delete the ROD and VME objects before exiting
00404   delete rod0;
00405   delete vme1;
00406 
00407   return 0;  
00408 }
00409 
00410 

Generated on Fri Sep 16 18:01:50 2005 for SCT DAQ/DCS Software - C++ by doxygen 1.3.5