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

FlashLoad.cxx

00001 //------------------------------FlashLoad------------------------------ 
00002 
00016 #include <iostream>
00017 using namespace std;
00018 
00019 #include <ctype.h>
00020 #include <string>
00021 #include <fstream>
00022 
00023 #include "RodModule.h"
00024 #include "RCCVmeInterface.h"
00025 
00026 int main(int argc, char *argv[]) {
00027 
00028 using namespace SctPixelRod;
00029 
00030   const unsigned long mapSize=0xc00040;         // Map size
00031   const long numSlaves=4;                       // Number of slaves
00032   std::string binFileName;                      // Name of binary file to load
00033   ifstream binFile;                             // Pointer to binary frile
00034   unsigned long selectAddr;                     // Start address of seleced flash
00035   unsigned long flashAddr;                      // Location in target flash
00036   int fileSize;                                 // Size of binary file in bytes
00037   int selectSize;                               // Size of selected flash
00038   int iselect;
00039   
00040   const unsigned long flashStart[5]={0xe00000, 0xe01000, 0xe80000, 0xed3000, 0xf26000};
00041   const long flashSize[5] = {24, 495204, 336688, 336680, 234456};
00042 
00043   const unsigned long flashStartE[5]={0xe00000, 0xe00000, 0xe80000, 0xf00000, 0xf80000};
00044   const long flashSizeE[5] = {0, 495204, 495212, 336680, 336680};
00045 
00046   std::string flashName[5] = {"Location", "ROD Controller", "Formatter", 
00047        "Event Fragment Builder", "Router"};
00048   
00049   std::string fileName(""), option;
00050   int slot = -1;
00051   unsigned long baseAddress;
00052 
00053   if (argc > 1) {
00054     for (int i=1; i<argc; i++) {
00055       option = argv[i];
00056       if (option[0] != '-') break;
00057       switch (option[1]) {
00058         case 's': {
00059           slot = atoi(option.substr(2).c_str());
00060           break;
00061         }
00062         default: {
00063           break;
00064         }
00065       }
00066     }
00067   }
00068 
00069 // Prompt for slot number
00070   if (slot < 0 ) {
00071     cout << "Enter slot number (decimal):"; 
00072     cin >> slot;
00073     while ((slot < 1) || (slot > 21)) {
00074       cout << "Slot number out or range [1:21], re-enter: ";
00075       cin >> slot;
00076     }
00077   }
00078   baseAddress = slot << 24;
00079   
00080 // Create VME interface
00081   RCCVmeInterface *vme1 = new RCCVmeInterface();
00082   
00083 // Create RodModule and initialize it
00084   RodModule* rod0 = new RodModule(baseAddress, mapSize, *vme1, numSlaves);
00085   try{
00086     rod0->initialize();
00087   }
00088   catch (HpiException *h) {
00089     hex(cout);
00090     cout << h->getDescriptor() << '\n';
00091     cout << "calcAddr: " << h->getCalcAddr() << ", readAddr: " << 
00092             h->getReadAddr() << '\n';
00093     dec(cout);
00094   };
00095 
00096 
00097   cout << "Enter Flash to program:" << endl;
00098   cout << "    1 = Location file" << endl;
00099   cout << "    2 = ROD Controller" << endl;
00100   cout << "    3 = Formatter" << endl;
00101   cout << "    4 = Event Fragment Builder" << endl;
00102   cout << "    5 = Router" << endl;
00103   cout << "Your choice (1-5)? ";
00104   cin >> dec >> iselect;
00105 
00106   if(iselect > 5) {
00107     cout << "Invalid selection (too high)\n";
00108     return 1;
00109   }
00110 
00111   if(iselect < 1) {
00112     cout << "Invalid selection (too low)\n";
00113     return 1;
00114   }
00115 
00116   if(rod0->getRevision() == 0xE && iselect == 1) {
00117     cout << "Invalid selection (no location file on Rev E)\n";
00118     return 1;
00119   }
00120 
00121   if(rod0->getRevision() == 0xE) {
00122     cout << "Loading flash to Rev E\n";
00123     selectAddr = flashStartE[iselect-1];
00124     selectSize = flashSizeE[iselect-1];
00125   } else {
00126     cout << "Loading flash to Rev B/C\n";
00127     selectAddr = flashStart[iselect-1];
00128     selectSize = flashSize[iselect-1];
00129   }
00130 
00131   cout << "Enter binary file name, including extension (""q"" to quit): ";
00132   cin >>  binFileName;
00133 
00134   if (binFileName.c_str() == "q") exit(0);
00135   binFile.open(binFileName.c_str(), ios::binary);
00136   if (!binFile.is_open()) {
00137     cout << "Unable to open binary file." << endl;
00138     exit(1);
00139   }
00140   
00141 // Get size of file
00142   binFile.seekg(0, ios::end);           // go to end of file
00143   fileSize = binFile.tellg();          // file size given by current location
00144   binFile.seekg(0, ios::beg);          // go back to beginning of file
00145   if (fileSize != selectSize) {
00146     cout << "File size is incorrect. Expected: " << dec << selectSize << " Found: " 
00147     << dec << fileSize << endl;
00148     exit(3);
00149   }
00150     
00151 // Create a buffer and read file into it
00152   UINT8 * buffer;
00153   try {
00154     buffer = new UINT8[fileSize];
00155   }
00156   catch (std::bad_alloc & ba) {
00157     cout << "Unable to allocate buffer for binary file." << endl;
00158     exit(2);
00159   }
00160   binFile.read((char *)buffer, fileSize);
00161   
00162 // write buffer to flash
00163   flashAddr = selectAddr;
00164 
00165   try {
00166     rod0->writeBlockToFlash(flashAddr, buffer, fileSize);
00167 
00168     cout << dec << selectSize << " bytes written to the "<< flashName[iselect-1]
00169          << " flash memory" << endl;
00170   } catch(BaseException &b) {
00171     cout << "******* Exception during FLASH upload!********\n";
00172     cout << b << endl;
00173   }
00174         
00175 // Clean up before exiting
00176   delete [] buffer;
00177   delete rod0;
00178   delete vme1;
00179 
00180   return 0;  
00181 }
00182 
00183 

Generated on Thu Feb 3 17:37:36 2005 for SCT DAQ/DCS Software - C++ by doxygen 1.3.5