LCOV - code coverage report
Current view: top level - wibmod/src/WIB1 - WIB_SI5342.cpp (source / functions) Coverage Total Hit
Test: code.result Lines: 0.0 % 88 0
Test Date: 2025-12-21 13:07:08 Functions: 0.0 % 8 0

            Line data    Source code
       1              : #include "wibmod/WIB1/WIB.hh"
       2              : #include "wibmod/WIB1/WIBException.hh"
       3              : #include <fstream>
       4              : #include <unistd.h> //usleep
       5              : 
       6              : #define SI5342_CONFIG_FILENAME "FELIX_SI5342.txt"
       7              : 
       8            0 : void WIB::WriteDAQ_SI5342(uint16_t address,uint32_t value,uint8_t byte_count){
       9            0 :   WriteI2C("DAQ.SI5342.I2C",address,value,byte_count);
      10            0 : }
      11            0 : uint32_t WIB::ReadDAQ_SI5342(uint16_t address,uint8_t byte_count){
      12            0 :   return ReadI2C("DAQ.SI5342.I2C",address,byte_count);
      13              : }
      14              : 
      15              : 
      16            0 : void WIB::ResetSi5342(){
      17            0 :   Write("DAQ.SI5342.RESET",0x1);
      18            0 :   Write("DAQ.SI5342.RESET",0x0);
      19            0 :   usleep(100000);
      20            0 : }
      21              : 
      22            0 : void WIB::SetDAQ_SI5342Page(uint8_t page){
      23            0 :   WriteDAQ_SI5342(0x1,page,1);
      24            0 : }
      25              : 
      26            0 : uint8_t WIB::GetDAQ_SI5342Page(){
      27            0 :   return uint8_t(ReadDAQ_SI5342(0x1,1)&0xFF);
      28              : }
      29              : 
      30            0 : uint8_t WIB::GetDAQ_SI5342AddressPage(uint16_t address){
      31            0 :   return uint8_t((address >> 8)&0xFF); 
      32              : }
      33              : 
      34            0 : void WIB::LoadConfigDAQ_SI5342(std::string const & fileName){
      35            0 :   std::ifstream confFile(fileName.c_str());
      36            0 :   BUException::WIB_BAD_ARGS badFile;
      37              : 
      38            0 :   if(confFile.fail()){
      39              :     //Failed to topen filename, add it to the exception
      40            0 :     badFile.Append("Bad SI5342 config file name:");
      41            0 :     badFile.Append(fileName.c_str());
      42              : 
      43              :     //Try the default
      44            0 :     if(getenv("WIBMOD_SHARE") != NULL){      
      45            0 :       std::string envBasedFileName=getenv("WIBMOD_SHARE");
      46            0 :       envBasedFileName+="/config/WIB1/config/";
      47            0 :       envBasedFileName+=SI5342_CONFIG_FILENAME;
      48            0 :       confFile.open(envBasedFileName.c_str());
      49            0 :       if(confFile.fail()){
      50            0 :         badFile.Append("Bad env based filename:");
      51            0 :         badFile.Append(envBasedFileName.c_str());
      52              :       }
      53            0 :     }
      54              :   }
      55              :   
      56            0 :   if(confFile.fail()){
      57              :     //We are still failing to open our file
      58            0 :     throw badFile;
      59              :   }
      60              : 
      61              :   //Make sure the chip isn't in reset
      62            0 :   if(Read("DAQ.SI5342.RESET") != 0){
      63            0 :     Write("DAQ.SI5342.RESET",0x0);
      64            0 :     usleep(50000);
      65              :   }
      66              : 
      67              :   //Reset the I2C firmware
      68            0 :   Write("DAQ.SI5342.I2C.RESET",1);
      69              : 
      70            0 :   std::vector<std::pair<uint16_t,uint8_t> > writes;
      71            0 :   while(!confFile.eof()){
      72            0 :     std::string line;
      73            0 :     std::getline(confFile,line);
      74            0 :     if(line.size() == 0){
      75            0 :       continue;
      76            0 :     }else if(line[0] == '#'){
      77            0 :       continue;
      78            0 :     }else if(line[0] == 'A'){
      79            0 :       continue;
      80              :     }else{
      81            0 :       if( line.find(',') == std::string::npos ){
      82            0 :         printf("Skipping bad line: \"%s\"\n",line.c_str());
      83            0 :         continue;
      84              :       }
      85            0 :       uint16_t address = strtoul(line.substr(0,line.find(',')).c_str(),NULL,16);
      86            0 :       uint8_t  data    = strtoul(line.substr(line.find(',')+1).c_str(),NULL,16);
      87            0 :       writes.push_back(std::pair<uint16_t,uint8_t>(address,data));
      88              :     }
      89            0 :   }
      90              : 
      91              :   //Disable the SI5342 output
      92            0 :   Write("DAQ.SI5342.ENABLE",0x0);
      93              : 
      94            0 :   uint8_t page = GetDAQ_SI5342Page();
      95            0 :   unsigned int percentDone = 0;
      96              : 
      97              : 
      98            0 :   printf("\n[==================================================]\n");
      99            0 :   fprintf(stderr," ");
     100            0 :   for(size_t iWrite = 0; iWrite < writes.size();iWrite++){
     101              : 
     102            0 :     if(page != GetDAQ_SI5342AddressPage(writes[iWrite].first)){
     103            0 :       page = GetDAQ_SI5342AddressPage(writes[iWrite].first);
     104            0 :       SetDAQ_SI5342Page(page);
     105            0 :       usleep(100000);
     106              :     }
     107              : 
     108              :     
     109            0 :     if(iWrite == 3){
     110            0 :       usleep(300000);
     111              :     }
     112              :     
     113              : 
     114            0 :     uint8_t  address = writes[iWrite].first & 0xFF;
     115            0 :     uint32_t data = (writes[iWrite].second) & 0xFF;
     116            0 :     uint8_t  iData = 1;
     117              : 
     118            0 :     for(size_t iTries = 10; iTries > 0;iTries--){
     119            0 :       try{
     120            0 :         WriteDAQ_SI5342(address ,data,iData);
     121            0 :       }catch (BUException::WIB_ERROR & e){  
     122              :         //Reset the I2C firmware
     123            0 :         Write("DAQ.SI5342.I2C.RESET",1);
     124            0 :         if(iTries == 1){
     125            0 :           e.Append("\nTried 3 times\n");
     126            0 :           throw;
     127              :         }
     128            0 :       }
     129              :     }
     130            0 :     if((100*iWrite)/writes.size() > percentDone){
     131            0 :       fprintf(stderr,"#");
     132            0 :       percentDone+=2;
     133              :     }
     134              :   }
     135            0 :   printf("\n");
     136              : 
     137            0 : }
     138              : 
     139            0 : void WIB::SelectSI5342(uint64_t input,bool enable){
     140            0 :   Write("DAQ.SI5342.INPUT_SELECT", input); 
     141            0 :   Write("DAQ.SI5342.ENABLE", uint64_t(enable)); 
     142            0 : }
        

Generated by: LCOV version 2.0-1