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

MdspFlashLoad.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 mdspBootMode = 0x1;
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 Mdsp boot mode to HPI\n";
00115       //This sets the MDSP to boot from the host port rather than ROM
00116       rod0->hpiLoad(FPGA_CONTROL_REG_REL_ADDR[2], mdspBootMode);
00117       rod0->sleep(1000);
00118       //--End JCH extra
00119 
00120       // Reset MDSP 
00121       rod0->hpiLoad(FPGA_CONTROL_REG_REL_ADDR[2], mdspReset+mdspBootMode);
00122 
00123       rod0->sleep(1000);
00124 
00125       unsigned long hpicValue = 0x00010001;
00126       rod0->hpiLoad(HPIC, hpicValue);
00127 
00128       rod0->sleep(1000);
00129       // mdspSingleRead/Write use HPID not HPID++ (not allowed)
00130 
00131       cerr << "Read from EMIF (1)\n";
00132       for(unsigned long addr = 0x01800000; addr < 0x0180001c; addr+=4) {
00133         unsigned long val = rod0->mdspSingleRead(addr);
00134         cerr << "0x" << hex << addr << ": 0x" << val << dec << endl;
00135       } 
00136 
00137       rod0->sleep(100);
00138 
00139       cerr << "Write CE space setup\n";
00140       rod0->mdspSingleWrite(0x01800004, 0xffff3f03);
00141       rod0->mdspSingleWrite(0x01800008, 0x00000040);
00142       rod0->mdspSingleWrite(0x01800010, 0xffff3f33);
00143       rod0->mdspSingleWrite(0x01800014, 0xffff3f33);
00144 
00145       rod0->sleep(500);
00146 
00147       cerr << "Read from EMIF (2)\n";
00148 
00149       // mdspSingleRead/Write use HPID not HPID++ (not allowed)
00150 
00151       for(unsigned long addr = 0x01800000; addr < 0x0180001c; addr+=4) {
00152         unsigned long val = rod0->mdspSingleRead(addr);
00153         cerr << "0x" << hex << addr << ": 0x" << val << dec << endl;
00154       } 
00155     }
00156     catch (BaseException &b) {
00157       cout << "Exception \"initing\" ROD:\n" << b << endl;
00158       exit(0);
00159     }
00160 
00161     rod0->sleep(3000);
00162   }
00163 
00164   cout << "Enter binary file name, including extension (""q"" to quit): ";
00165   cin >>  binFileName;
00166 
00167   if (binFileName.c_str() == "q") exit(0);
00168   binFile.open(binFileName.c_str(), ios::binary);
00169   if (!binFile.is_open()) {
00170     cout << "Unable to open binary file." << endl;
00171     exit(1);
00172   }
00173   
00174 // Get size of file
00175   binFile.seekg(0, ios::end);           // go to end of file
00176   fileSize = binFile.tellg();          // file size given by current location
00177   binFile.seekg(0, ios::beg);          // go back to beginning of file
00178     
00179 // Create a buffer and read file into itprmSize = fileSize
00180   
00181   UINT8 * buffer;
00182   try {
00183     buffer = new UINT8[fileSize];
00184   }
00185   catch (std::bad_alloc & ba) {
00186     cout << "Unable to allocate buffer for binary file." << endl;
00187     exit(2);
00188   }
00189   binFile.read((char *)buffer, fileSize);
00190 
00191   cout << "Read file of size " << fileSize << endl;
00192   
00193 // write buffer to MDSP flash memory
00194   try {
00195     rod0->writeBlockToFlashHpi(prmStart, buffer, fileSize);
00196 
00197     cout << fileSize << " bytes written to the MDSP flash memory"<< endl;
00198   } catch (VmeException &v) {
00199     cout << "VmeException creating Writing flash." << endl;
00200     cout << "ErrorClass = " << v.getErrorClass() << endl;
00201     cout << "ErrorCode = " << v.getErrorCode() << endl; 
00202   } catch (RodException &r) {
00203     cout << r.getDescriptor() <<", " << r.getData1() << ", " << r.getData2()
00204      << '\n';
00205   }
00206         
00207 // Clean up before exiting
00208   delete [] buffer;
00209   delete rod0;
00210   delete vme1;
00211 
00212   return 0;  
00213 }
00214 
00215 

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