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

LedTest.cxx

00001 #include <iostream>
00002 using namespace std;
00003 
00004 #include <ctype.h>
00005 
00006 #include <sys/time.h>
00007 
00008 #include "RodModule.h"
00009 
00010 #include "primParams.h"
00011 #include "RCCVmeInterface.h"
00012 #include "parameters.h"
00013 
00014 int main(int argc, char *argv[]) {
00015 
00016 using namespace SctPixelRod;
00017 
00018   RodPrimList primList(1);                      // Primitive List
00019   long myTextLength;                            // Actual length of text message
00020   TEXT_BUFFER_TYPE myTextType;                  // Buffer type for latest message
00021   PrimState returnPState;
00022   TextBuffState returnTState;
00023   std::string ipramFile("../Dsp/Binary/slaveRun_IPRAM_exp.bin");
00024   std::string idramFile("../Dsp/Binary/slaveRun_IDRAM_exp.bin");
00025   std::string extFile("../Dsp/Binary/slaveRun_xcode_exp.bin");
00026 
00027   int slaveNumber=-1;
00028   std::string fileName(""), option;
00029   bool initRod = true;
00030   int slot = -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 'n': {
00040           initRod = false;
00041           break;
00042         }
00043         case 's': {
00044           slot = atoi(option.substr(2).c_str());
00045           break;
00046         }
00047         case 'v': {
00048           slaveNumber = atoi(option.substr(2).c_str());
00049           break;
00050         }
00051         default: {
00052           break;
00053         }
00054       }
00055     }
00056   }
00057 // Prompt for slot number
00058   if (slot < 0 ) {
00059     cout << "Enter slot number (decimal):"; 
00060     cin >> slot;
00061     while ((slot < 1) || (slot > 21)) {
00062       cout << "Slot number out or range [1:21], re-enter: ";
00063       cin >> slot;
00064     }
00065   }
00066   baseAddress = slot << 24;
00067 
00068 // Create VME interface
00069   RCCVmeInterface *vme1 = new RCCVmeInterface();
00070   
00071 // Create RodModule and initialize it
00072   RodModule* rod0 = new RodModule(baseAddress, mapSize, *vme1, numSlaves);
00073   try{
00074     rod0->initialize();
00075   }
00076   catch (HpiException &h) {
00077     hex(cout);
00078     cout << h.getDescriptor() << '\n';
00079     cout << "calcAddr: " << h.getCalcAddr() << ", readAddr: " << 
00080             h.getReadAddr() << '\n';
00081     dec(cout);
00082   }
00083   catch (VmeException &v) {
00084     cout << "VmeException creating RodModule." << endl;
00085     cout << "ErrorClass = " << v.getErrorClass() << endl;
00086     cout << "ErrorCode = " << v.getErrorCode() << endl; 
00087   }
00088   catch (RodException &r) {
00089   cout << r.getDescriptor() <<", " << r.getData1() << ", " << r.getData2()
00090           << '\n';
00091   };
00092 
00093 // Initialize slave
00094   if (slaveNumber < 0 ) {
00095     cout << "Enter slave number (0-3):"; 
00096     cin >> slaveNumber;
00097     while ((slaveNumber < 0) || (slaveNumber > 3)) {
00098       cout << "Slave number out or range [0:3], re-enter: ";
00099       cin >> slaveNumber;
00100     }
00101   }
00102 
00103   try {
00104     rod0->initSlaveDsp(ipramFile, idramFile, extFile, slaveNumber, 'v');
00105   }
00106   catch (RodException & r) {
00107     cout << r;
00108     return 0;
00109   }
00110   catch (NoImageFile& i) {
00111     cout << i;
00112     return 0;
00113   }
00114   catch (VmeException &v) {
00115     cout << "VmeException in SendPrimList." << endl;
00116     cout << "ErrorClass = " << v.getErrorClass() << endl;
00117     cout << "ErrorCode = " << v.getErrorCode() << endl; 
00118     return 0;
00119   };
00120 
00121 // Create a buffer for text messages
00122   char * myTextBuffer;
00123   try {
00124     myTextBuffer= new char[TEXT_BUFF_SIZE];
00125   }
00126   catch (std::bad_alloc) {
00127     cout << "Unable to allocate text buffer in main.";
00128   }
00129   
00130 // Create and Send a FLASH_LED primitive to a slave DSP
00131   FLASH_LED_IN ledData;
00132   ledData.ledNum = YELLOW_LED;
00133   ledData.period = 1000;
00134   ledData.numTimes = 10;
00135 //   long ledData[3] = {0,1000,10};           //State and toggle flag
00136   RodPrimitive* led0;
00137   try {
00138     led0 = new RodPrimitive(4+3, 0, FLASH_LED, R_FLASH_LED, (long*)&ledData);
00139   }
00140   catch (std::bad_alloc) {
00141     cout << "Unable to allocate led0 primitive in main." << endl;
00142   }
00143   primList.insert(primList.begin(), *led0);
00144   try {
00145     primList.bufferBuild();
00146   }
00147   catch (PrimListException &p) {
00148     cout << p.getDescriptor() << " ";
00149     cout << p.getData1() << ", " << p.getData2() << "\n";
00150   };
00151   
00152 // Get a copy of buffer and use SEND_SLAVE_LIST/START_SLAVE_LIST primitives
00153 // to have MDSP send it to the slave
00154   long buffLength = primList.getBufferLength();
00155   unsigned long* slavePrimList;
00156   try {
00157     slavePrimList = new unsigned long[buffLength];
00158   }
00159   catch (bad_alloc) {
00160     cout << "Unable to allocate slavePrimList in main." << endl;
00161   };
00162   
00163   unsigned long* bufferStart = primList.getBuffer();
00164   for (int i=0; i< buffLength; i++) {
00165     slavePrimList[i] = bufferStart[i];
00166   }
00167   primList.clear();
00168   delete led0;
00169   
00170 // Create Slave List primitives
00171   long slaveData[4+buffLength]; 
00172   slaveData[0] = slaveNumber;
00173   slaveData[1] = buffLength;
00174   slaveData[2] = DEFAULT;
00175   slaveData[3] = DEFAULT;
00176   for (int i=0; i< buffLength; i++) {
00177     slaveData[i+4] = slavePrimList[i];
00178   }
00179   RodPrimitive* send0;
00180   try {
00181     send0 = new RodPrimitive(8+buffLength, 0, SEND_SLAVE_LIST, 
00182                 R_SEND_SLAVE_LIST, slaveData);
00183   }
00184   catch (bad_alloc) {
00185     cout << "Unable to allocate send0 primitive in main." << endl;
00186   };
00187   
00188   primList.insert(primList.begin(), *send0);
00189   long startData[3] = {slaveNumber, 1, 0};
00190   RodPrimitive* start0;
00191   try {
00192     start0 = new RodPrimitive(7, 0, START_SLAVE_LIST, 
00193                 R_START_SLAVE_LIST, startData);
00194   }
00195   catch (bad_alloc) {
00196     cout << "Unable to allocate start0 primitive in main." << endl;
00197   };
00198   primList.insert(primList.end(), *start0);
00199   try {
00200     primList.bufferBuild();
00201   }
00202   catch (PrimListException *p) {
00203     cout << p->getDescriptor() << " ";
00204     cout << p->getData1() << ", " << p->getData2() << "\n";
00205   };
00206 
00207   struct timeval startTime;
00208   gettimeofday(&startTime, NULL);
00209 
00210   try {
00211     rod0->sendPrimList(&primList);
00212   }
00213   catch (HpiException *h) {
00214     hex(cout);
00215     cout << h->getDescriptor() << '\n';
00216     cout << "calcAddr: " << h->getCalcAddr() << ", readAddr: " << 
00217             h->getReadAddr() << '\n';
00218     dec(cout);
00219   }
00220   catch (VmeException &v) {
00221     cout << "VmeException in SendPrimList." << endl;
00222     cout << "ErrorClass = " << v.getErrorClass() << endl;
00223     cout << "ErrorCode = " << v.getErrorCode() << endl; 
00224   };
00225 
00226 // Wait for ROD to begin executing and then wait for it to finish executing
00227 // Check for error messages in text buffer and read them out if they exist.
00228 // Note: this is NOT how primHandler and textHandler will be used once we
00229 // go to threads.  This is for debugging the code only.
00230   do {
00231     try {
00232       returnPState = rod0->primHandler();
00233     }
00234     catch (VmeException &v) {
00235       cout << "VmeException in first primHandler call." << endl;
00236       cout << "ErrorClass = " << v.getErrorClass() << endl;
00237       cout << "ErrorCode = " << v.getErrorCode() << endl; 
00238     }
00239     try {
00240       returnTState = rod0->textHandler();
00241     }
00242     catch (VmeException &v) {
00243       cout << "VmeException in first textHandler call." << endl;
00244       cout << "ErrorClass = " << v.getErrorClass() << endl;
00245       cout << "ErrorCode = " << v.getErrorCode() << endl; 
00246     }
00247     if (returnTState == TEXT_RQ_SET) {
00248       do {
00249         try {
00250           returnTState = rod0->textHandler();
00251           }
00252         catch (VmeException &v) {
00253           cout << "VmeException in second textHandler call." << endl;
00254           cout << "ErrorClass = " << v.getErrorClass() << endl;
00255           cout << "ErrorCode = " << v.getErrorCode() << endl; 
00256         }
00257       } while (returnTState != TEXT_READOUT);
00258       rod0->getTextBuffer(myTextBuffer, myTextLength, myTextType);
00259       rod0->clearTextBuffer();
00260       for (int i=0; i<myTextLength; i++) {
00261         cout << myTextBuffer[i];
00262         if (0==(i+1)%64) cout << endl;
00263         }
00264       cout << endl; 
00265     }
00266   } while (returnPState != PRIM_EXECUTING); 
00267   do {
00268     try {
00269       returnPState = rod0->primHandler();
00270     }
00271     catch (RodException *r) {
00272     cout << r->getDescriptor() <<", " << r->getData1() << ", " << r->getData2()
00273             << '\n';
00274     }
00275     catch (VmeException &v) {
00276       cout << "VmeException in second primHandler call." << endl;
00277       cout << "ErrorClass = " << v.getErrorClass() << endl;
00278       cout << "ErrorCode = " << v.getErrorCode() << endl; 
00279     }
00280   } while ((returnPState != PRIM_WAITING)&&(returnPState != PRIM_IDLE)); 
00281     
00282   struct timeval endTime;
00283   gettimeofday(&endTime, NULL);
00284 
00285   long diff = (endTime.tv_sec - startTime.tv_sec) * 1000000;
00286   diff += (endTime.tv_usec - startTime.tv_usec);
00287 
00288   cout << "LED flashing took " << ((float(diff))/1e6) << " seconds\n";
00289 
00290 // Clean up: clear primList, delete primitive, delete the outList
00291   primList.clear();
00292   delete send0;
00293   delete start0;
00294   delete [] slavePrimList;
00295   delete [] myTextBuffer;
00296   rod0->deleteOutList();
00297   
00298 // Delete the ROD and VME objects before exiting
00299   delete rod0;
00300   delete vme1;
00301 
00302   return 0;  
00303 }
00304 
00305 

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