SctApiLow.cxx

00001 // Low level ROD access, no access to modules...
00002 #include <iostream>
00003 
00004 #include "SctApi.h"
00005 #include "SctApiDebug.h"
00006 #include "primListWrapper.h"
00007 #include "PrimBuilder.h"
00008 
00009 #include "RodCrate/RodPrimitive.h"
00010 
00011 #include "CommonWithDsp/primParams.h"
00012 #include "SctApiException.h"
00013 
00014 using namespace std;
00015 using namespace SctPixelRod;
00016 
00017 namespace SctApi {
00018 
00019 void SctApi::writeRODRegister(unsigned int rod,
00020                               int reg, int off, int width, int value) {
00021   {
00022     boost::mutex::scoped_lock lock(log().mutex());
00023     log() << "Write ROD register 0x" << hex << reg << dec << endl;
00024   }
00025 
00026   boost::shared_ptr<PrimListWrapper> regList(new PrimListWrapper(2));
00027 
00028   PrimBuilder::instance().writeRegister(regList, reg, off, width, value);
00029 
00030   sendPrimList(rod, regList);
00031 
00032   int responseCode = awaitResponse(rod, 1);
00033 
00034   if(responseCode != 0) {
00035     cout << "Register write 0x" << hex << reg << dec << " failed!\n";
00036   }
00037 }
00038 
00039 unsigned int SctApi::readRODRegister(unsigned int rod, int r) {
00040   {
00041     boost::mutex::scoped_lock lock(log().mutex());
00042     log() << "Read ROD reg 0x" << hex << r << dec << "\n";
00043   }
00044 
00045   boost::shared_ptr<PrimListWrapper> readList(new PrimListWrapper(2));
00046 
00047   PrimBuilder::instance().readRegister(readList, r);
00048  
00049   sendPrimList(rod, readList);
00050 
00051   int responseCode = awaitResponse(rod, 2);
00052 
00053   if(responseCode != 0) {
00054     cout << "Read ROD reg (0x" << hex << r << dec << ") primitive failed!\n";
00055   }
00056 
00057   unsigned long length;
00058   unsigned long *result = getResponse(rod, length);
00059   unsigned int val = 0;
00060   if(result) {
00061     val = result[8];
00062 
00063     delete [] result;
00064   } else {
00065     cout << "ReadRegister 0x" << hex << r << dec << " failed (no result)\n";
00066   }
00067 
00068   return val;
00069 }
00070 
00071 int SctApi::pollRegister(unsigned int rod, 
00072                          int r, int off, int width, int val, int timeout) {
00073   {
00074     boost::mutex::scoped_lock lock(log().mutex());
00075     log() << "Poll ROD register\n";
00076   }
00077 
00078   boost::shared_ptr<PrimListWrapper> pollList(new PrimListWrapper(2));
00079  
00080   PrimBuilder::instance().pollRegister(pollList, r, off, width, val, timeout);
00081 
00082   sendPrimList(rod, pollList);
00083 
00084   int responseCode = awaitResponse(rod, 5);
00085 
00086   if(responseCode != 0) {
00087     cout << "POLL ROD reg 0x" << hex << r << dec << " failed!\n";
00088     return 0;
00089   }
00090 
00091   unsigned long length;
00092 
00093   unsigned long *response = getResponse(rod, length);
00094 
00095   int result = 0;
00096 
00097   if(response) {
00098     if(response[8] == 1) {
00099       cout << "Success\n";
00100       result = 1;
00101     } else {
00102       cout << "Failure (timeout)\n";
00103       result = 0;
00104     }
00105 
00106     delete [] response;
00107   }
00108 
00109   return result;
00110 }
00111 
00112 unsigned long *SctApi::readFifo(unsigned int rod, int id, int bank, int elems) {
00113   if (checkDebugOption(DEBUG_DIAG)){
00114     boost::mutex::scoped_lock lock(log().mutex()); 
00115     log() << "readFIFO\n";
00116   }
00117 
00118   boost::shared_ptr<PrimListWrapper> fifoList(new PrimListWrapper(2));
00119 
00120   PrimBuilder::instance().readFifo(fifoList, id, bank, elems);
00121 
00122   sendPrimList(rod, fifoList);
00123 
00124   int responseCode = awaitResponse(rod, 2);
00125 
00126   if(responseCode != 0) {
00127     cout << "Read FIFO failed!\n";
00128   }
00129 
00130   unsigned long length;
00131   unsigned long *response = getResponse(rod, length);
00132 
00133   unsigned long *result;
00134   if(response) {
00135     unsigned long myLength = length-9-2;
00136 
00137     result = new unsigned long[myLength];
00138 
00139     for(unsigned int i=0; i<myLength; i++) {
00140       result[i] = response[i+9];
00141     }
00142   } else {
00143     result = 0;
00144   }
00145 
00146   return result;
00147 }
00148 
00149 void SctApi::echo(unsigned int rod,
00150                   unsigned int length, const unsigned long *data) {
00151   {
00152     boost::mutex::scoped_lock lock(log().mutex());
00153     log() << "Echo primitive\n";
00154   }
00155 
00156   boost::shared_ptr<PrimListWrapper> primList(new PrimListWrapper(1));
00157 
00158   // Create and Send a simple ECHO primitive
00159 #if (R_ECHO != 100) 
00160 #error "ECHO revision changed!"
00161 #endif
00162 
00163   long *myData = new long[length];
00164   copy(data, data + length, myData);
00165   primList->addPrimitive(RodPrimitive(4+length, 1, ECHO, R_ECHO, myData),
00166                          myData);
00167 
00168   sendPrimList(rod, primList);
00169 
00170   if(checkDebugOption(DEBUG_DIAG)) {
00171     cout << "Echo primList sent\n";
00172   }
00173 }
00174 
00175 void SctApi::echoAll(unsigned int length, const unsigned long *data) {
00176   {
00177     boost::mutex::scoped_lock lock(log().mutex());
00178     log() << "Echo primitive\n";
00179   }
00180 
00181   boost::shared_ptr<PrimListWrapper> primList(new PrimListWrapper(1));
00182 
00183   // Create and Send a simple ECHO primitive
00184 #if (R_ECHO != 100) 
00185 #error "ECHO revision changed!"
00186 #endif
00187 
00188   long *myData = new long[length];
00189   copy(data, data + length, myData);
00190   primList->addPrimitive(RodPrimitive(4+length, 1, ECHO, R_ECHO, myData),
00191                          myData);
00192 
00193   sendPrimListAll(primList);
00194 
00195   if(checkDebugOption(DEBUG_DIAG)) {
00196     cout << "Echo primList sent\n";
00197   }
00198 }
00199 
00200 void SctApi::echoSlave(unsigned int rod,
00201                        unsigned int slave, unsigned int length, const unsigned long *data) {
00202   {
00203     boost::mutex::scoped_lock lock(log().mutex());
00204     log() << "Send Echo primitive to slave\n";
00205   }
00206 
00207   boost::shared_ptr<PrimListWrapper> primList(new PrimListWrapper(1));
00208 
00209   // Create and Send a simple ECHO primitive
00210 #if (R_ECHO != 100) 
00211 #error "ECHO revision changed!"
00212 #endif
00213 
00214   long *myData = new long[length];
00215   copy(data, data + length, myData);
00216   primList->addPrimitive(RodPrimitive(4+length, 1, ECHO, R_ECHO, myData),
00217                          myData);
00218 
00219   sendSlavePrimList(rod, primList, slave, 1, 1);
00220 
00221   if(checkDebugOption(DEBUG_DIAG)) {
00222     cout << "Echo primList sent\n";
00223   }
00224 }
00225 
00226 /*
00227     Flash the LED associated with a Slave DSP.
00228     This creates its own primitive list and sends it to the ROD
00229 */
00230 void SctApi::flashLED(unsigned int rod,
00231                       long slaveNumber, long period, long flashes) {
00232   {
00233     boost::mutex::scoped_lock lock(log().mutex());
00234     log() << "FlashLED\n" << flush;
00235   }
00236 
00237   boost::shared_ptr<PrimListWrapper> primList(new PrimListWrapper(1));
00238 
00239   // Create a FLASH_LED primitive to be sent to a slave DSP
00240   long *ledData = new long[sizeof(FLASH_LED_IN)/4];
00241 
00242   FLASH_LED_IN &ledParam = *(FLASH_LED_IN *)ledData;
00243 
00244 #if (R_FLASH_LED == 103) 
00245   ledParam.ledNum = YELLOW_LED;
00246   ledParam.period = period;
00247   ledParam.numTimes = flashes;
00248 #else 
00249 #error "FLASH_LED not compiled (revision change)"
00250 #endif
00251 
00252   primList->addPrimitive(sizeof(FLASH_LED_IN)/4, 0, FLASH_LED, R_FLASH_LED, ledData);
00253 
00254   if(slaveNumber == -1) {
00255     // Send to master
00256     sendPrimList(rod, primList);
00257   } else {
00258     sendSlavePrimList(rod, primList, slaveNumber, true, false);
00259   }
00260 
00261   return;
00262 }
00263 
00264 pair<UINT32, UINT32> SctApi::sendData(unsigned int rod, 
00265                                       int type, int dsp) {
00266   // type was HISTOGRAM_DATA
00267   SEND_DATA_IN primData;
00268   primData.dataType = type;
00269   primData.auxVal = 0;
00270   primData.repBufferFlag = 0;
00271   primData.timeout = 0x20000;
00272 
00273   boost::shared_ptr<PrimListWrapper> primList(new PrimListWrapper(1));
00274 
00275   primList->addPrimitive(sizeof(primData)/4, 0, SEND_DATA, R_SEND_DATA, (const long *)&primData);
00276 
00277   if(dsp == -1) {
00278     sendPrimList(rod, primList);
00279   } else {
00280     sendSlavePrimList(rod, primList, dsp, true, true);
00281   }
00282 
00283   pair<UINT32, UINT32> result;
00284 
00285   int responseCode = awaitResponse(rod, 5);
00286   if(responseCode != 0) {
00287     cout << "Send_data failed!\n";
00288 
00289     throw SctApiException("SEND DATA didn't respond");
00290   } else {
00291     unsigned long dataLength;
00292     unsigned long *dataBody = getResponse(rod, dataLength);
00293 
00294     if(dataBody) {
00295       if(dsp == -1) {
00296         cout << "SEND DATA says ptr 0x" << hex << dataBody[8] << " length 0x" << dataBody[9] << dec << endl;
00297 
00298         result.first = dataBody[8];
00299         result.second = dataBody[9];
00300       } else {
00301         cout << "SEND DATA from dsp " << dsp 
00302              << " says ptr 0x" << hex << dataBody[16] << " length 0x" << dataBody[17] << dec << endl;
00303 
00304         result.first = dataBody[16];
00305         result.second = dataBody[17];
00306       }
00307     } else {
00308       cout << "SEND DATA returned nothing!\n";
00309       throw SctApiException("SEND DATA returned no data");
00310     }
00311   }
00312 
00313   return result;
00314 }
00315 
00316 }  // Close namespace

Generated on Mon Feb 6 14:01:29 2006 for SCT DAQ/DCS Software - C++ by  doxygen 1.4.6