V172X_Daq_Helpers.cc

00001 #include "V172X_Daq.hh"
00002 #include "CAENVMElib.h"
00003 #include "Message.hh"
00004 //#include "exstream.hh"
00005 #include <exception>
00006 #include <stdexcept>
00007 
00008 //change this if CAENVME_MultiWrite/Read starts working...
00009 //DO NOT CHANGE since now several handles are used one for each board
00010 const bool stupid = true;
00011 
00012 CVAddressModifier add_mod[20] = { cvA32_U_DATA,cvA32_U_DATA,cvA32_U_DATA,
00013                                   cvA32_U_DATA,cvA32_U_DATA,cvA32_U_DATA,
00014                                   cvA32_U_DATA,cvA32_U_DATA,cvA32_U_DATA,
00015                                   cvA32_U_DATA,cvA32_U_DATA,cvA32_U_DATA,
00016                                   cvA32_U_DATA,cvA32_U_DATA,cvA32_U_DATA,
00017                                   cvA32_U_DATA,cvA32_U_DATA,cvA32_U_DATA,
00018                                   cvA32_U_DATA, cvA32_U_DATA };
00019 CVDataWidth data_width[20] = { cvD32,cvD32,cvD32,cvD32,cvD32,cvD32,
00020                                cvD32,cvD32,cvD32,cvD32,cvD32,cvD32,
00021                                cvD32,cvD32,cvD32,cvD32,cvD32,cvD32,
00022                                cvD32,cvD32, };
00023 
00024 void V172X_Daq::WriteVMERegister(uint32_t address, 
00025                                  uint32_t write_me, int32_t handle) throw(std::runtime_error)
00026 {
00027   CVErrorCodes err = CAENVME_WriteCycle(handle,
00028                                         address,
00029                                         &write_me, add_mod[0], data_width[0]);
00030 //  Message(INFO) << "writing on handle " <<  handle << " addreess " << std::hex << address << std::endl;
00031   if(err != cvSuccess){
00032     switch(err){
00033     case cvBusError:
00034       _status = BUS_ERROR;
00035       break;
00036     case cvCommError:
00037       _status = COMM_ERROR;
00038       break;
00039     default:
00040       _status = GENERIC_ERROR;
00041     }
00042     Message e(EXCEPTION);
00043     e <<" Error writing to VME Register "
00044       <<std::hex<<std::showbase<<address
00045       <<std::dec<<std::noshowbase<<": "
00046       <<CAENVME_DecodeError(err)<<std::endl;
00047     throw std::runtime_error(e.str());
00048   }
00049   
00050 }
00051   
00052 uint32_t V172X_Daq::ReadVMERegister(uint32_t address, int32_t handle) throw(std::runtime_error)
00053 {
00054   uint32_t data=0;
00055   CVErrorCodes err = CAENVME_ReadCycle(handle,
00056                                        address,
00057                                        &data, add_mod[0], data_width[0]);
00058   
00059 //  Message(INFO) << "reading on handle " <<  handle << " addreess " << std::hex << address << std::endl;
00060   if(err != cvSuccess){
00061     switch(err){
00062     case cvBusError:
00063       _status = BUS_ERROR;
00064       break;
00065     case cvCommError:
00066       _status = COMM_ERROR;
00067       break;
00068     default:
00069       _status = GENERIC_ERROR;
00070     }
00071     Message e(EXCEPTION);
00072     e <<" Error reading from VME Register "
00073       <<std::hex<<std::showbase<<address<<": "
00074       <<std::dec<<std::noshowbase<<CAENVME_DecodeError(err)
00075       <<std::endl;
00076     throw std::runtime_error(e.str());
00077   }
00078   return data;
00079 }
00080 
00081 void V172X_Daq::WriteVMERegisters(uint32_t address, uint32_t write_me) throw(std::runtime_error)
00082 {
00083   const int n = _params.enabled_boards;
00084   uint32_t full_address[V172X_Params::nboards];
00085   uint32_t data[V172X_Params::nboards];
00086   CVErrorCodes ecodes[V172X_Params::nboards];
00087   uint32_t handles[V172X_Params::nboards];
00088   int board = 0;
00089   for(int i=0; i< _params.nboards; i++){
00090     if(_params.board[i].enabled){
00091       full_address[board] = _params.board[i].address+address;
00092       data[board] = write_me;
00093       ecodes[board]=cvSuccess;
00094       handles[board]=_handle_board[i];
00095       board++;
00096     }
00097   }
00098   CVErrorCodes err;
00099   if(!stupid){
00100     //This doesn't seem to work...
00101     err = CAENVME_MultiWrite(_handle_vme_bridge, full_address, data, n, 
00102                              add_mod, data_width, ecodes);
00103   }
00104   else{
00105     err = cvSuccess;
00106     for(int i=0; i<n; i++) {
00107        ecodes[i] = CAENVME_WriteCycle(handles[i],full_address[i],data + i,
00108                                add_mod[i],data_width[i]);
00109       if (ecodes[i] != cvSuccess) err = ecodes[i];
00110     }
00111   }
00112   if(err != cvSuccess){
00113     switch(err){
00114     case cvBusError:
00115       _status = BUS_ERROR;
00116       break;
00117     case cvCommError:
00118       _status = COMM_ERROR;
00119       break;
00120     default:
00121       _status = GENERIC_ERROR;
00122     }
00123     Message e(EXCEPTION);
00124     e <<" Errors writing to VME Registers: "<<CAENVME_DecodeError(err)<<"\n";
00125     for(int i=0; i<n; i++){
00126       e<<"\t"<<std::hex<<std::showbase<<full_address[i]<<" "<<data[i]<<": "
00127        <<"\t"<<std::dec<<std::noshowbase<<CAENVME_DecodeError(ecodes[i])
00128        <<"\t"<<std::endl;
00129     }
00130     throw std::runtime_error(e.str());
00131   }
00132 }
00133 
00134 void V172X_Daq::WriteVMERegisters(uint32_t address, uint32_t* write_me) throw(std::runtime_error)
00135 {
00136   const int n = _params.enabled_boards;
00137   uint32_t full_address[V172X_Params::nboards];
00138   CVErrorCodes ecodes[V172X_Params::nboards];
00139   uint32_t handles[V172X_Params::nboards];
00140   int board = 0;
00141   for(int i=0; i< _params.nboards; i++){
00142     if(_params.board[i].enabled){
00143       full_address[board] = _params.board[i].address+address;
00144       ecodes[board] = cvSuccess;
00145       handles[board]=_handle_board[i];
00146       board++;
00147     }
00148   }
00149   CVErrorCodes err;
00150   if(!stupid){
00151     //This doesn't seem to work...
00152     err = CAENVME_MultiWrite(_handle_vme_bridge, full_address, write_me, n, 
00153                              add_mod, data_width, ecodes);
00154   }
00155   else{
00156     err = cvSuccess;
00157     for(int i=0; i<n; i++) {
00158       ecodes[i] = CAENVME_WriteCycle(handles[i],full_address[i],write_me + i,
00159                                add_mod[i],data_width[i]);
00160   //    Message(INFO) << "writing on handle " <<  handles[i] << " addreess " << std::hex << full_address[i] << std::endl;
00161       if (ecodes[i] != cvSuccess) err = ecodes[i];
00162     }
00163   }
00164   
00165   if(err != cvSuccess){
00166     switch(err){
00167     case cvBusError:
00168       _status = BUS_ERROR;
00169       break;
00170     case cvCommError:
00171       _status = COMM_ERROR;
00172       break;
00173     default:
00174       _status = GENERIC_ERROR;
00175     }
00176     Message e(EXCEPTION);
00177     e <<" Errors writing to VME Registers:\n";
00178     for(int i=0; i<n; i++){
00179       e<<"\t"<<std::hex<<std::showbase<<full_address[i]<<" "<<write_me[i]<<": "
00180        <<"\t"<<std::dec<<std::noshowbase<<CAENVME_DecodeError(ecodes[i])
00181        <<"\t"<<std::endl;
00182     }
00183     throw std::runtime_error(e.str());
00184   }
00185 }
00186 
00187 void V172X_Daq::ReadVMERegisters(uint32_t address, 
00188                                  uint32_t *data) throw(std::runtime_error)
00189 {
00190   const int n = _params.enabled_boards;
00191   uint32_t* luint_data = data;
00192   uint32_t full_address[n];
00193   CVErrorCodes ecodes[n];
00194   uint32_t handles[V172X_Params::nboards];
00195   int board = 0;
00196   for(int i=0; i< _params.nboards; i++){
00197     if(_params.board[i].enabled){
00198       full_address[board] = _params.board[i].address+address;
00199       handles[board]=_handle_board[i];
00200       board++;
00201     }
00202   }
00203   CVErrorCodes err;
00204   if(!stupid){
00205     //This doesn't seem to work...
00206     err = CAENVME_MultiRead(_handle_vme_bridge, full_address, luint_data, n, 
00207                              add_mod, data_width, ecodes);
00208   }
00209   else{
00210     err = cvSuccess;
00211     for(int i=0; i<n; i++) {
00212       ecodes[i] = CAENVME_ReadCycle(handles[i],full_address[i],luint_data + i,
00213                                add_mod[i],data_width[i]);
00214       if (ecodes[i] != cvSuccess) err = ecodes[i];
00215     }
00216   }
00217   
00218    if(err != cvSuccess){
00219     switch(err){
00220     case cvBusError:
00221       _status = BUS_ERROR;
00222       break;
00223     case cvCommError:
00224       _status = COMM_ERROR;
00225       break;
00226     default:
00227       _status = GENERIC_ERROR;
00228     }
00229     Message e(EXCEPTION);
00230     e <<" Errors reading from VME Registers:\n";
00231     for(int i=0; i<n; i++){
00232       e<<"\t"<<std::hex<<std::showbase<<full_address[i]<<": "
00233        <<"\t"<<std::dec<<std::noshowbase<<CAENVME_DecodeError(ecodes[i])
00234        <<"\t"<<std::endl;
00235     }
00236     throw std::runtime_error(e.str());
00237   }
00238 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines

Generated on 20 Jun 2014 for daqman by  doxygen 1.6.1