pit_driver_opticalLink.cpp

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <fstream>
00003 #include <sstream>
00004 #include <exception>
00005 
00006 
00007 #include "pit_driver_opticalLink.h"
00008 #include "pit_driver_optinBoard.h"
00009 #include "pit_driver_foChannel.h"
00010 #include "pit_configuration.h"
00011 
00012 
00013 const int maxSrvNameLength=256;
00014 
00015 pit_driver_opticalLink::pit_driver_opticalLink(unsigned int p_linkNumber, pit_comm *comm_engine, pit_driver_optinBoard *OptinBoard)
00016 {
00017 unsigned int i;
00018     
00019     linkNumber = p_linkNumber;
00020     motherOptinBoard = OptinBoard;
00021     commFEE = comm_engine;
00022       
00023     logDriverOpticalLink = &(log4cpp::Category::getInstance("pitLog.driver")); 
00024     //logDriverOpticalLink->info("Constructor of pit_driver_opticalLink %d, OPTIN board %d",linkNumber,motherOptinBoard->getBoardNumber());
00025 
00026 
00027 
00028     // Create 10 instances of pit_driver_foChannel for the 10 pixel chips of this link
00029         for(i = 0; i < NUMBER_FO_CHANNELS; i++) {
00030               // create 10 instances of pit_driver_foChannels to model the 10 fast-or channels on each optical links
00031             foChannel[i] = new pit_driver_foChannel(i, commFEE, this);
00032         }/* end for */
00033         
00034                         // refreshes the detector coordinates to generate the services with the right name
00035         this->refreshDetectorCoordinates();
00036                         
00037         
00038     //***************** will initialize the dim services ********************************************************
00039         // uses the string stream class to format the values inside the srings
00040         char charSide;
00041         if (side == A) charSide = 'A';
00042         else charSide='C';
00043         
00044     stringstream halfStaveName;
00045     halfStaveName << "PIT/SIDE_"<< charSide<<"/SECTOR_"<<sector<<"/HALF_STAVE_"<<half_stave<<"/";
00046     
00047         // initializes these strings already with the halfstave name in them
00048 
00049     const string linkRequiredName(halfStaveName.str()+ "REQUIRED");
00050     const string linkLockedName(halfStaveName.str()+"LOCKED");
00051     const string linkActiveName( halfStaveName.str()+"ACTIVE");
00052     
00053          
00054                 // starts the services here
00055                 // we will see if this casting from bool to int & reference will work
00056         // also writes an info in the log stating the name of the services
00057     logDriverOpticalLink->info("creating service %s",linkLockedName.c_str());
00058     serviceLinkLocked = new DimService(linkLockedName.c_str(), linkLocked);     
00059     logDriverOpticalLink->info("creating service %s",linkActiveName.c_str());
00060     serviceLinkActive = new DimService(linkActiveName.c_str(), linkActive);
00061     logDriverOpticalLink->info("creating service %s",linkRequiredName.c_str());
00062     serviceLinkRequired = new DimService(linkRequiredName.c_str(), linkRequired);
00063 
00064     if (motherOptinBoard->IsBoardPlugged()){
00065                 // reads the link status to update the services
00066         this->readLinkStatus();
00067         this->refreshLinkSetting();
00068         
00069     }
00070     else{
00071                 // if the optin board is un plugged marks the link as not active and as not locked
00072         this->linkLocked=0;
00073         this->linkActive=0;
00074     }
00075  
00076         // checks if the link is required
00077     this->refreshLinkRequired();
00078         // updates all services connected with this link
00079     this->refreshStatusServices();
00080     
00081 }// end  pit_driver_opticalLink
00082 
00083 
00084 pit_driver_opticalLink::~pit_driver_opticalLink()
00085 {
00086 
00087  
00088   for(unsigned int i = 0; i <  NUMBER_FO_CHANNELS; i++) delete foChannel[i];
00089   
00090   // deletes the dim services also
00091   delete serviceLinkActive;
00092   delete serviceLinkLocked;
00093   delete serviceLinkRequired;
00094   
00095 }// end  pit_driver_opticalLink
00096 
00097 
00104 pit_driver_foChannel& pit_driver_opticalLink::getFoChannel(unsigned int channelNum) {
00105 
00106      if (channelNum >= NUMBER_FO_CHANNELS ){
00107        logDriverOpticalLink->crit("(getFoChannel), channelNum %d out of range, opticalLink %d, board %d", channelNum, linkNumber, getBoardNumber());
00108        throw out_of_range("Exception from pit_driver_opticalLink::getFoChannel, channelNum out of range");
00109      }
00110      else{
00111        return *foChannel[channelNum];
00112      }
00113 
00114 }
00115 
00116 
00117 int pit_driver_opticalLink::refresh(){
00118         this->refreshLinkRequired();
00119         this->refreshLinkStatus();
00120         this->refreshLinkSetting();
00121         
00122         // now call the rfresh method of all children
00123         for (int foCh = 0 ; foCh < NUMBER_FO_CHANNELS; foCh ++){
00124                                 // it does the argument as false so to not force the parent to refresh the link setting and status 10 times
00125                 this->getFoChannel(foCh).refresh(false);
00126         }
00127         return 0;
00128 }
00129 
00131 void pit_driver_opticalLink::refreshDetectorCoordinates(){
00132                 // gets the reference of the only instance
00133         pit_configuration &pitConf = pit_configuration::getInstance();
00134         
00135                 // gets the board number from the parent
00136         unsigned int boardNumber = motherOptinBoard->getBoardNumber();
00137         sector =pitConf.getSector(boardNumber, linkNumber);
00138         side = pitConf.getSide(boardNumber, linkNumber);
00139         half_stave=pitConf.getHalfStave(boardNumber, linkNumber);
00140         
00141 }
00142 
00144 bool pit_driver_opticalLink::refreshLinkRequired(){
00145         pit_configuration &pitConf = pit_configuration::getInstance();
00146         this->linkRequired = pitConf.getLinkRequired(this->getBoardNumber(), this->getLinkNumber());
00147                 // updates service
00148         this->serviceLinkRequired->updateService();
00149         return this->linkRequired;
00150 }
00152 
00153 void pit_driver_opticalLink::decodeLinkSetting(UInt32 linkSetting){
00154          
00155         logDriverOpticalLink->log(log4cpp::Priority::DEBUG, "(pit_driver_opticalLink::decodeLinkSetting): Decoding link setting register value 0x%x", linkSetting);
00156                         
00157         foMask = extractFromUInt32(0,10,linkSetting);
00158         udfFoPattern=extractFromUInt32(10,10,linkSetting);
00159         //selfMaskingEnable=extractFromUInt32(20,10,linkSetting);
00160         udfEnable = extractFromUInt32(20,1,linkSetting );
00161         linkDelay=extractFromUInt32(30,2,linkSetting);
00162         
00163         
00164 }
00165 
00166 /************************************************************
00167  * function to write the link setting register
00168  * used by writeFoMask, writeLinkDelay, writeUdfFoPattern
00169  * uses pit_comm->PITWriteRegister
00170 ***********************************************************/
00171 int pit_driver_opticalLink::writeLinkSetting(UInt32 p_linkSetting)throw(std::exception){
00172         
00173                 // address of the settings register
00174         UInt28 address = motherOptinBoard->getBaseAddress() + LINK_SETTING_OFFSET + linkNumber;
00175         
00176         commFEE->PITWriteWord(address,p_linkSetting);
00177         //if everything went fine then decodes this valus updating the internal data members 
00178         this->decodeLinkSetting(p_linkSetting);
00179                 
00180 
00181         return 0;
00182 }
00183 
00185 
00187 UInt32 pit_driver_opticalLink::readLinkStatus()throw (std::exception){
00188         UInt32 status;
00189         
00190         UInt28 address= motherOptinBoard->getBaseAddress() + LINK_STATUS_OFFSET +linkNumber;
00191         commFEE->PITReadWord( address,&status);
00192         return status;
00193 }
00194 
00195 //decodes a status register into the internal data members
00196 void pit_driver_opticalLink::decodeLinkStatus(UInt32 status){
00197         
00198         linkLocked= extractFromUInt32(0,1,status)?true:false;
00199         linkActive= extractFromUInt32(1,1,status)?true:false;
00200         
00201                 // there is no internal method for link error, gianluca forgot or its not needed?
00202         //linkError== extractFromUInt32(2,1,status)?true:false; 
00203         
00204         linkPhase=extractFromUInt32(3,2,status);
00205         
00206         
00207 
00208 }
00209 
00210 // updates link status related services
00211 // for the moment only two
00212 void pit_driver_opticalLink::refreshStatusServices(void){
00213                 // updates services
00214         serviceLinkActive->updateService();
00215         serviceLinkLocked->updateService();
00216 
00217                         // refreshes also the link required service
00218         this->refreshLinkRequired();
00219         
00220 }
00221 //updates the services connected with the status register
00222 UInt32 pit_driver_opticalLink::refreshLinkStatus(void){
00223         
00224         UInt32 linkStatus=this->readLinkStatus();
00225         this->decodeLinkStatus(linkStatus);
00226         this->refreshStatusServices();
00227         return linkStatus;
00228         
00229 
00230 }
00231 
00233 
00234 UInt32 pit_driver_opticalLink::readLinkSetting()throw (std::exception){
00235         
00236         UInt32 linkSetting;
00237         
00238                 // address of the settings register
00239         UInt28 address = motherOptinBoard->getBaseAddress() + LINK_SETTING_OFFSET + linkNumber;
00240         
00241         commFEE->PITReadWord(address,&linkSetting);
00242                 
00243         return linkSetting;     
00244 }
00245 
00246 UInt32 pit_driver_opticalLink::refreshLinkSetting(){
00247         
00248         UInt32 linkSetting = this->readLinkSetting();
00249         this->decodeLinkSetting(linkSetting);
00250         return linkSetting;
00251 }
00253 bool pit_driver_opticalLink::readLinkLocked()throw(std::exception){
00254 
00255         this->refreshLinkStatus();
00256         return linkLocked;
00257 
00258 }
00259         //function to get the link active
00260 bool pit_driver_opticalLink::readLinkActive() throw(std::exception){
00261         this->refreshLinkStatus();
00262         return linkActive;
00263 }
00264 
00265 //function to get the link measured phase
00266 int pit_driver_opticalLink::readLinkPhase() throw(std::exception){
00267 this->refreshLinkStatus();
00268 return linkPhase;
00269 }
00270 
00271 
00273 int pit_driver_opticalLink::readFoMask()throw(std::exception){
00274         
00275         this->refreshLinkSetting();
00276         return foMask;
00277 }
00278 
00279 //int pit_driver_opticalLink::readSelfMaskingEnable() throw(std::exception){
00280 //      
00281 //      this->refreshLinkSetting();
00282 //      return selfMaskingEnable;
00283 //}
00284 
00285 int pit_driver_opticalLink::readUdfFoPattern()throw(std::exception){
00286         this->refreshLinkSetting();
00287         return udfFoPattern;
00288 }
00289 
00291 int pit_driver_opticalLink::readLinkDelay()throw(std::exception){
00292 
00293 this->refreshLinkSetting();
00294 return linkDelay;
00295 }
00296 
00297 
00299 
00300 int pit_driver_opticalLink::writeFoMask(int p_foMask)throw(std::exception){
00301                 // read the old setting register
00302         UInt32 oldSetting = this->readLinkSetting();
00303         UInt32 newSetting = insertIntoUInt32(0,10,p_foMask, oldSetting); 
00304         
00305         this->writeLinkSetting(newSetting);
00306         return 0;
00307 }
00309 
00310 int pit_driver_opticalLink::writeUdfFoPattern(int p_udfFoPattern)throw(std::exception){
00311                 // read the old setting register
00312         UInt32 oldSetting = this->readLinkSetting();
00313         UInt32 newSetting = insertIntoUInt32(10,10,p_udfFoPattern, oldSetting); 
00314         
00315         this->writeLinkSetting(newSetting);
00316         return 0;
00317 }
00318 
00320 
00321 int pit_driver_opticalLink::writeSelfMaskingEnable(int p_selfMaskingEnable)throw(std::exception){
00322         UInt32 oldSetting =this->readLinkSetting();
00323         UInt32 newSetting = insertIntoUInt32(20,10,p_selfMaskingEnable,oldSetting);
00324         
00325         this->writeLinkSetting(newSetting);
00326         return 0;
00327 }
00328 
00330 
00331 int pit_driver_opticalLink::writeLinkDelay(int p_linkDelay)throw(std::exception){
00332         UInt32 oldSetting =this->readLinkSetting();
00333         UInt32 newSetting = insertIntoUInt32(30,2, p_linkDelay, oldSetting);
00334         logDriverOpticalLink->log(log4cpp::Priority::DEBUG, "(pit_driver_opticalLink::writeLinkDelay): p_linkDelay = 0x%x, oldSetting = 0x%x, newSetting = 0x%x", p_linkDelay, oldSetting, newSetting);
00335                                         
00336         this->writeLinkSetting(newSetting);
00337                 
00338         return 0;
00339 }
00340 
00341 int pit_driver_opticalLink::readTimeStamp(int timeStampNumber)throw(std::exception){
00342         // address of the timeStamp register
00343         UInt28 address = motherOptinBoard->getBaseAddress() + LINK_TIMESTAMP_OFFSET +linkNumber*NumberTimeStamps;
00344         
00345         UInt32 timeStamp;
00346         
00347         commFEE->PITReadWord(address,&timeStamp);
00348         return timeStamp;
00349 }
00350 
00351 int pit_driver_opticalLink::readErrorCounter()throw(std::exception){
00352         UInt28 address = motherOptinBoard->getBaseAddress() + LINK_ERROR_COUNTER_OFFSET +linkNumber;
00353                 
00354         UInt32 errorCounter;
00355                 
00356         commFEE->PITReadWord(address,&errorCounter);
00357         return errorCounter;
00358 }
00359 
00360 bool pit_driver_opticalLink::isInnerLayer()const{
00361         
00362         if (half_stave < NUMBER_HSTAVE_INNER) { return true;}
00363         else{ return false;     }
00364         
00365 }
00366 
00367 unsigned int pit_driver_opticalLink::getBoardNumber()const {return motherOptinBoard->getBoardNumber();}
00368 UInt28 pit_driver_opticalLink::getBoardBaseAdress()const {return motherOptinBoard->getBaseAddress();}
00369 
00370 int pit_driver_opticalLink::writeEnUdFastors(bool enable){
00371         
00372         UInt32 linksetting = readLinkSetting();
00373         UInt32 newLinkSetting = insertIntoUInt32(20,1,enable,linksetting);
00374         writeLinkSetting(newLinkSetting);
00375         //decodeLinkSetting(newLinkSetting);
00376         return 0;
00377 }
00378 
00379 bool pit_driver_opticalLink::readEnUdFastors(){
00380         refreshLinkSetting();
00381         return udfEnable;
00382 }

Generated on Sat Mar 29 22:55:55 2008 for pixelTrigger by  doxygen 1.5.0