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

MdspFlashLoad_JCH1.cxx

00001 //------------------------------MdspFlashLoad------------------------------ 
00002 
00014 #include <iostream>
00015 using namespace std;
00016 
00017 #include <ctype.h>
00018 #include <string>
00019 #include <fstream>
00020 
00021 #include "RodModule.h"
00022 #include "RCCVmeInterface.h"
00023 
00024 int main(int argc, char *argv[]) {
00025 
00026 using namespace SctPixelRod;
00027 
00028   const unsigned long mapSize=0xc00040;         // VME map size 
00029   const long numSlaves=4;                       // Number of slaves
00030   std::string binFileName;                      // Name of binary file to load
00031   ifstream binFile;                             // Pointer to binary frile
00032   int fileSize;                                 // Size of binary file in bytes
00033   
00034   const unsigned long prmStart=0x01400000;
00035   std::string fileName(""), option;
00036   int safeMode = false;
00037   int slot = -1;
00038   unsigned long baseAddress;
00039 
00040   if (argc > 1) {
00041     for (int i=1; i<argc; i++) {
00042       option = argv[i];
00043       if (option[0] != '-') break;
00044       switch (option[1]) {
00045         case 'f': {
00046           safeMode = true;
00047           break;
00048         }
00049         case 's': {
00050           slot = atoi(option.substr(2).c_str());
00051           break;
00052         }
00053         default: {
00054           break;
00055         }
00056       }
00057     }
00058   }
00059 
00060 // Prompt for slot number
00061   if (slot < 0 ) {
00062     cout << "Enter slot number (decimal):"; 
00063     cin >> slot;
00064     while ((slot < 1) || (slot > 21)) {
00065       cout << "Slot number out or range [1:21], re-enter: ";
00066       cin >> slot;
00067     }
00068   }
00069   baseAddress = slot << 24;
00070   
00071 // Create VME interface
00072   RCCVmeInterface *vme1 = new RCCVmeInterface();
00073   
00074 // Create RodModule and initialize it
00075   RodModule* rod0 = new RodModule(baseAddress, mapSize, *vme1, numSlaves);
00076 
00077   if(!safeMode) {
00078     try{
00079       rod0->initialize();
00080     }
00081     catch (HpiException &h) {
00082       cout << "HPI exception initialising ROD\n";
00083       hex(cout);
00084       cout << h.getDescriptor() << '\n';
00085       cout << "calcAddr: " << h.getCalcAddr() << ", readAddr: " << 
00086         h.getReadAddr() << '\n';
00087       dec(cout);
00088 
00089       cout << "Try the \"safe\" mode (-f)\n";
00090       return 1;
00091     }
00092     catch (BaseException &b) {
00093       cout << "Other exception initialising ROD\n";
00094       cout << b << endl;
00095 
00096       cout << "Try the \"safe\" mode (-f)\n";
00097       return 1;
00098     };
00099   } else {   // Safe mode (doesn't try to talk to MDSP (for when the MDSP is corrupt))
00100     try {
00101       unsigned long int fpgaHold = 0x40;
00102       unsigned long int mdspReset = 0x2;
00103 
00104       //--JCH Extra
00105       unsigned long int configureOverride = 0x40;
00106       //--End JCH extra
00107       
00108       cerr << "Put FPGAs on reset hold\n";
00109       // Put FPGAs on hold
00110       rod0->hpiLoad(FPGA_CONTROL_REG_REL_ADDR[1], fpgaHold);
00111       rod0->sleep(1000);
00112  
00113       //--JCH Extra
00114       cout << "Set Configure Override bit\n";
00115       //Configure override bit allows user complete access to all flashes,
00116       //with no protection at all!!
00117       rod0->hpiLoad(FPGA_CONTROL_REG_REL_ADDR[0], configureOverride);
00118       rod0->sleep(1000);
00119       //--End JCH extra
00120 
00121       // Reset MDSP 
00122       rod0->hpiLoad(FPGA_CONTROL_REG_REL_ADDR[2], mdspReset);
00123 
00124       rod0->sleep(1000);
00125 
00126       unsigned long hpicValue = 0x00010001;
00127       rod0->hpiLoad(HPIC, hpicValue);
00128 
00129       rod0->sleep(1000);
00130       // mdspSingleRead/Write use HPID not HPID++ (not allowed)
00131 
00132       cerr << "Read from EMIF (1)\n";
00133       for(unsigned long addr = 0x01800000; addr < 0x0180001c; addr+=4) {
00134         unsigned long val = rod0->mdspSingleRead(addr);
00135         cerr << "0x" << hex << addr << ": 0x" << val << dec << endl;
00136       } 
00137 
00138       rod0->sleep(100);
00139 
00140       cerr << "Write CE space setup\n";
00141       rod0->mdspSingleWrite(0x01800004, 0xffff3f03);
00142       rod0->mdspSingleWrite(0x01800008, 0x00000040);
00143       rod0->mdspSingleWrite(0x01800010, 0xffff3f33);
00144       rod0->mdspSingleWrite(0x01800014, 0xffff3f33);
00145 
00146       rod0->sleep(500);
00147 
00148       cerr << "Read from EMIF (2)\n";
00149 
00150       // mdspSingleRead/Write use HPID not HPID++ (not allowed)
00151 
00152       for(unsigned long addr = 0x01800000; addr < 0x0180001c; addr+=4) {
00153         unsigned long val = rod0->mdspSingleRead(addr);
00154         cerr << "0x" << hex << addr << ": 0x" << val << dec << endl;
00155       } 
00156     }
00157     catch (BaseException &b) {
00158       cout << "Exception \"initing\" ROD:\n" << b << endl;
00159       exit(0);
00160     }
00161 
00162     rod0->sleep(3000);
00163   }
00164 
00165   cout << "Enter binary file name, including extension (""q"" to quit): ";
00166   cin >>  binFileName;
00167 
00168   if (binFileName.c_str() == "q") exit(0);
00169   binFile.open(binFileName.c_str(), ios::binary);
00170   if (!binFile.is_open()) {
00171     cout << "Unable to open binary file." << endl;
00172     exit(1);
00173   }
00174   
00175 // Get size of file
00176   binFile.seekg(0, ios::end);           // go to end of file
00177   fileSize = binFile.tellg();          // file size given by current location
00178   binFile.seekg(0, ios::beg);          // go back to beginning of file
00179     
00180 // Create a buffer and read file into itprmSize = fileSize
00181   
00182   UINT8 * buffer;
00183   try {
00184     buffer = new UINT8[fileSize];
00185   }
00186   catch (std::bad_alloc & ba) {
00187     cout << "Unable to allocate buffer for binary file." << endl;
00188     exit(2);
00189   }
00190   binFile.read((char *)buffer, fileSize);
00191 
00192   cout << "Read file of size " << fileSize << endl;
00193   
00194 // write buffer to MDSP flash memory
00195   try {
00196     rod0->writeBlockToFlashHpi(prmStart, buffer, fileSize);
00197 
00198     cout << fileSize << " bytes written to the MDSP flash memory"<< endl;
00199   } catch (VmeException &v) {
00200     cout << "VmeException creating Writing flash." << endl;
00201     cout << "ErrorClass = " << v.getErrorClass() << endl;
00202     cout << "ErrorCode = " << v.getErrorCode() << endl; 
00203   } catch (RodException &r) {
00204     cout << r.getDescriptor() <<", " << r.getData1() << ", " << r.getData2()
00205      << '\n';
00206   }
00207         
00208 // Clean up before exiting
00209   delete [] buffer;
00210   delete rod0;
00211   delete vme1;
00212 
00213   return 0;  
00214 }
00215 
00216 

Generated on Thu Dec 22 20:17:04 2005 for SCT DAQ/DCS Software - C++ by doxygen 1.3.5