LCOV - code coverage report
Current view: top level - mu2e-pcie-utils/cfoInterfaceLib - util_main.cc (source / functions) Coverage Total Hit
Test: mu2edaq.info.cleaned Lines: 0.0 % 155 0
Test Date: 2026-07-30 01:56:44 Functions: 0.0 % 9 0

            Line data    Source code
       1              : // This file (util_main.cc) was created by Ron Rechenmacher <ron@fnal.gov> on
       2              : // May 13, 2015. "TERMS AND CONDITIONS" governing this file are in the README
       3              : // or COPYING file. If you do not have such a file, one can be obtained by
       4              : // contacting Ron or Fermi Lab in Batavia IL, 60510, phone: 630-840-3000.
       5              : // $RCSfile: .emacs.gnu,v $
       6              : // rev="$Revision: 1.23 $$Date: 2012/01/23 15:32:40 $";
       7              : 
       8              : #include <unistd.h>  // usleep
       9              : #include <chrono>
      10              : #include <cmath>
      11              : #include <cstdio>   // printf
      12              : #include <cstdlib>  // strtoul
      13              : #include <fstream>
      14              : #include <iomanip>
      15              : #include <iostream>
      16              : #include <set>
      17              : #include <string>
      18              : 
      19              : #include "TRACE/tracemf.h"
      20              : 
      21              : #include "cfoInterfaceLib/CFO_Compiler.hh"
      22              : #include "cfoInterfaceLib/CFO_Registers.h"
      23              : 
      24              : using namespace CFOLib;
      25              : 
      26              : // bool rawOutput = false;
      27              : std::string rawOutputFile = "/tmp/cfoUtil.raw";
      28              : std::string inputFile = "/tmp/cfoUtil.raw";
      29              : bool compileInputFile = false;
      30              : std::ofstream outputStream;
      31              : unsigned targetFrequency = 166666667;
      32              : unsigned clockSpeed = 40000000;
      33              : 
      34              : int dtc = -1;
      35              : std::string op = "";
      36              : 
      37            0 : unsigned getOptionValue(int* index, char** argv[])
      38              : {
      39            0 :         auto arg = (*argv)[*index];
      40            0 :         if (arg[2] == '\0')
      41              :         {
      42            0 :                 (*index)++;
      43            0 :                 unsigned ret = strtoul((*argv)[*index], nullptr, 0);
      44            0 :                 if (ret == 0 && (*argv)[*index][0] != '0')  // No option given
      45              :                 {
      46            0 :                         (*index)--;
      47              :                 }
      48            0 :                 return ret;
      49              :         }
      50            0 :         auto offset = 2;
      51            0 :         if (arg[2] == '=')
      52              :         {
      53            0 :                 offset = 3;
      54              :         }
      55              : 
      56            0 :         return strtoul(&arg[offset], nullptr, 0);
      57              : }
      58            0 : unsigned long long getOptionValueLong(int* index, char** argv[])
      59              : {
      60            0 :         auto arg = (*argv)[*index];
      61            0 :         if (arg[2] == '\0')
      62              :         {
      63            0 :                 (*index)++;
      64            0 :                 unsigned long long ret = strtoull((*argv)[*index], nullptr, 0);
      65            0 :                 if (ret == 0 && (*argv)[*index][0] != '0')  // No option given
      66              :                 {
      67            0 :                         (*index)--;
      68              :                 }
      69            0 :                 return ret;
      70              :         }
      71            0 :         auto offset = 2;
      72            0 :         if (arg[2] == '=')
      73              :         {
      74            0 :                 offset = 3;
      75              :         }
      76              : 
      77            0 :         return strtoull(&arg[offset], nullptr, 0);
      78              : }
      79              : 
      80            0 : std::string getOptionString(int* index, char** argv[])
      81              : {
      82            0 :         auto arg = (*argv)[*index];
      83            0 :         if (arg[2] == '\0')
      84              :         {
      85            0 :                 (*index)++;
      86            0 :                 return std::string((*argv)[*index]);
      87              :         }
      88            0 :         auto offset = 2;
      89            0 :         if (arg[2] == '=')
      90              :         {
      91            0 :                 offset = 3;
      92              :         }
      93              : 
      94            0 :         return std::string(&arg[offset]);
      95              : }
      96              : 
      97            0 : unsigned getLongOptionValue(int* index, char** argv[])
      98              : {
      99            0 :         auto arg = std::string((*argv)[*index]);
     100            0 :         auto pos = arg.find('=');
     101              : 
     102            0 :         if (pos == std::string::npos)
     103              :         {
     104            0 :                 (*index)++;
     105            0 :                 unsigned ret = strtoul((*argv)[*index], nullptr, 0);
     106            0 :                 if (ret == 0 && (*argv)[*index][0] != '0')  // No option given
     107              :                 {
     108            0 :                         (*index)--;
     109              :                 }
     110            0 :                 return ret;
     111              :         }
     112              : 
     113            0 :         return strtoul(&arg[++pos], nullptr, 0);
     114            0 : }
     115            0 : unsigned long long getLongOptionValueLong(int* index, char** argv[])
     116              : {
     117            0 :         auto arg = std::string((*argv)[*index]);
     118            0 :         auto pos = arg.find('=');
     119              : 
     120            0 :         if (pos == std::string::npos)
     121              :         {
     122            0 :                 (*index)++;
     123            0 :                 unsigned long long ret = strtoull((*argv)[*index], nullptr, 0);
     124            0 :                 if (ret == 0 && (*argv)[*index][0] != '0')  // No option given
     125              :                 {
     126            0 :                         (*index)--;
     127              :                 }
     128            0 :                 return ret;
     129              :         }
     130              : 
     131            0 :         return strtoull(&arg[++pos], nullptr, 0);
     132            0 : }
     133              : 
     134            0 : std::string getLongOptionOption(int* index, char** argv[])
     135              : {
     136            0 :         auto arg = std::string((*argv)[*index]);
     137            0 :         auto pos = arg.find('=');
     138              : 
     139            0 :         if (pos == std::string::npos)
     140              :         {
     141            0 :                 return arg;
     142              :         }
     143              :         else
     144              :         {
     145            0 :                 return arg.substr(0, pos - 1);
     146              :         }
     147            0 : }
     148              : 
     149            0 : std::string getLongOptionString(int* index, char** argv[])
     150              : {
     151            0 :         auto arg = std::string((*argv)[*index]);
     152              : 
     153            0 :         if (arg.find('=') == std::string::npos)
     154              :         {
     155            0 :                 return std::string((*argv)[++(*index)]);
     156              :         }
     157              :         else
     158              :         {
     159            0 :                 return arg.substr(arg.find('='));
     160              :         }
     161            0 : }
     162              : 
     163            0 : void printHelpMsg()
     164              : {
     165            0 :         std::cout << "Usage: cfoUtil [options] [write_program,program_clock,dma_info]" << std::endl;
     166              :         std::cout
     167            0 :                 << "Options are:" << std::endl
     168            0 :                 << "    -h, --help: This message." << std::endl
     169            0 :                 << "    -f: RAW Output file path" << std::endl
     170            0 :                 << "    -F: Frequency to program (in Hz, sorry...Default 166666667 Hz)" << std::endl
     171            0 :                 << "    -p: CFO program to write (Default: /tmp/cfoUtil.raw)" << std::endl
     172              :                 << "    -C: CFO program needs to be compiled (i.e. not bitfile). If specified with -f, will not write compiled "
     173            0 :                    "program to CFO"
     174            0 :                 << std::endl
     175            0 :                 << "    -c: Clock speed for CFO (default 40000000)" << std::endl
     176            0 :                 << "    --cfo: Use cfo <num> (Defaults to DTCLIB_DTC if set, 0 otherwise, see ls /dev/mu2e* for available CFOs)"
     177            0 :                 << std::endl;
     178            0 :         exit(0);
     179              : }
     180              : 
     181            0 : int main(int argc, char* argv[])
     182              : {
     183            0 :         for (auto optind = 1; optind < argc; ++optind)
     184              :         {
     185            0 :                 if (argv[optind][0] == '-')
     186              :                 {
     187            0 :                         switch (argv[optind][1])
     188              :                         {
     189            0 :                                 case 'f':
     190              :                                         // rawOutput = true;
     191            0 :                                         rawOutputFile = getOptionString(&optind, &argv);
     192            0 :                                         break;
     193            0 :                                 case 'F':
     194            0 :                                         targetFrequency = getOptionValue(&optind, &argv);
     195            0 :                                         break;
     196            0 :                                 case 'c':
     197            0 :                                         clockSpeed = getOptionValue(&optind, &argv);
     198            0 :                                         break;
     199            0 :                                 case 'C':
     200            0 :                                         compileInputFile = true;
     201            0 :                                         break;
     202            0 :                                 case 'p':
     203            0 :                                         inputFile = getOptionString(&optind, &argv);
     204            0 :                                         break;
     205            0 :                                 case '-':  // Long option
     206              :                                 {
     207            0 :                                         auto option = getLongOptionOption(&optind, &argv);
     208            0 :                                         if (option == "--cfo")
     209              :                                         {
     210            0 :                                                 dtc = getLongOptionValue(&optind, &argv);
     211              :                                         }
     212            0 :                                         else if (option == "--help")
     213              :                                         {
     214            0 :                                                 printHelpMsg();
     215              :                                         }
     216            0 :                                         break;
     217            0 :                                 }
     218            0 :                                 default:
     219            0 :                                         std::cout << "Unknown option: " << argv[optind] << std::endl;
     220            0 :                                         printHelpMsg();
     221            0 :                                         break;
     222            0 :                                 case 'h':
     223            0 :                                         printHelpMsg();
     224            0 :                                         break;
     225              :                         }
     226              :                 }
     227              :                 else
     228              :                 {
     229            0 :                         op = std::string(argv[optind]);
     230              :                 }
     231              :         }
     232              : 
     233            0 :         std::cout.setf(std::ios_base::boolalpha);
     234              :         std::cout << "Options are: "
     235            0 :                           << "Operation: " << std::string(op) << ", CFO: " << dtc << std::endl;
     236              : 
     237            0 :         if (op == "write_program")
     238              :         {
     239            0 :                 if (compileInputFile)
     240              :                 {
     241            0 :                         std::cout << "Raw output file: " << rawOutputFile << std::endl;
     242              : 
     243            0 :                         CFO_Compiler compiler;
     244            0 :                         compiler.processFile(inputFile, rawOutputFile);
     245            0 :                         return 0;
     246            0 :                 }
     247              :                 else
     248              :                 {
     249              :                         mu2e_databuff_t inputData;
     250            0 :                         std::ifstream file(inputFile, std::ios::binary | std::ios::ate);
     251            0 :                         if (file.eof())
     252              :                         {
     253            0 :                                 std::cout << "Input file " << inputFile << " does not exist!" << std::endl;
     254              :                         }
     255            0 :                         auto inputSize = file.tellg();
     256            0 :                         uint64_t dmaSize = static_cast<uint64_t>(inputSize) + 8;
     257            0 :                         file.seekg(0, std::ios::beg);
     258              :                         //*reinterpret_cast<uint64_t*>(inputData) = input.size();
     259            0 :                         memcpy(&inputData[0], &dmaSize, sizeof(uint64_t));
     260            0 :                         file.read(reinterpret_cast<char*>(&inputData[8]), inputSize);
     261            0 :                         file.close();
     262              : 
     263            0 :                         auto thisCFO = new CFO_Registers(DTC_SimMode_NoCFO, dtc);
     264            0 :                         thisCFO->GetDevice()->write_data(DTC_DMA_Engine_DAQ, inputData, sizeof(inputData));
     265            0 :                         delete thisCFO;
     266            0 :                 }
     267              :         }
     268            0 :         else if (op == "program_clock")
     269              :         {
     270            0 :                 auto thisDTC = new CFO_Registers(DTC_SimMode_NoCFO, dtc);
     271            0 :                 thisDTC->SetNewOscillatorFrequency(targetFrequency);
     272            0 :                 delete thisDTC;
     273              :         }
     274            0 :         else if (op == "dma_info")
     275              :         {
     276            0 :                 if (dtc == -1)
     277              :                 {
     278            0 :                         auto dtcE = getenv("DTCLIB_DTC");
     279            0 :                         if (dtcE != nullptr)
     280              :                         {
     281            0 :                                 dtc = atoi(dtcE);
     282              :                         }
     283              :                         else
     284            0 :                                 dtc = 0;
     285              :                 }
     286              : 
     287            0 :                 mu2edev device;
     288            0 :                 device.init(DTCLib::DTC_SimMode_Disabled, dtc);
     289            0 :                 device.meta_dump();
     290            0 :         }
     291              :         else
     292              :         {
     293            0 :                 std::cout << "Unrecognized operation: " << op << std::endl;
     294            0 :                 printHelpMsg();
     295              :         }
     296              : 
     297              :         // if (rawOutput) outputStream.close();
     298            0 :         return 0;
     299              : }  // main
        

Generated by: LCOV version 2.0-1