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

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

Generated on Thu Feb 10 02:40:15 2005 for SCT DAQ/DCS Software - C++ by doxygen 1.3.5