00001
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;
00031 const long numSlaves=4;
00032 std::string binFileName;
00033 ifstream binFile;
00034 unsigned long selectAddr;
00035 unsigned long flashAddr;
00036 int fileSize;
00037 int selectSize;
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
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
00081 RCCVmeInterface *vme1 = new RCCVmeInterface();
00082
00083
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/F)\n";
00118 return 1;
00119 }
00120
00121
00122
00123 if((rod0->getRevision() == 0xB)||(rod0->getRevision() == 0xC)) {
00124 cout << "Loading flash to Rev B/C\n";
00125 selectAddr = flashStart[iselect-1];
00126 selectSize = flashSize[iselect-1];
00127 } else {
00128 cout << "Loading flash to Rev E/F\n";
00129 selectAddr = flashStartE[iselect-1];
00130 selectSize = flashSizeE[iselect-1];
00131 }
00132
00133 cout << "Enter binary file name, including extension (""q"" to quit): ";
00134 cin >> binFileName;
00135
00136 if (binFileName.c_str() == "q") exit(0);
00137 binFile.open(binFileName.c_str(), ios::binary);
00138 if (!binFile.is_open()) {
00139 cout << "Unable to open binary file." << endl;
00140 exit(1);
00141 }
00142
00143
00144 binFile.seekg(0, ios::end);
00145 fileSize = binFile.tellg();
00146 binFile.seekg(0, ios::beg);
00147 if (fileSize != selectSize) {
00148 cout << "File size is incorrect. Expected: " << dec << selectSize << " Found: "
00149 << dec << fileSize << endl;
00150 exit(3);
00151 }
00152
00153
00154 UINT8 * buffer;
00155 try {
00156 buffer = new UINT8[fileSize];
00157 }
00158 catch (std::bad_alloc & ba) {
00159 cout << "Unable to allocate buffer for binary file." << endl;
00160 exit(2);
00161 }
00162 binFile.read((char *)buffer, fileSize);
00163
00164
00165 flashAddr = selectAddr;
00166
00167 try {
00168 rod0->writeBlockToFlash(flashAddr, buffer, fileSize);
00169
00170 cout << dec << selectSize << " bytes written to the "<< flashName[iselect-1]
00171 << " flash memory" << endl;
00172 } catch(BaseException &b) {
00173 cout << "******* Exception during FLASH upload!********\n";
00174 cout << b << endl;
00175 }
00176
00177
00178 delete [] buffer;
00179 delete rod0;
00180 delete vme1;
00181
00182 return 0;
00183 }
00184
00185