00001
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;
00029 const long numSlaves=4;
00030 std::string binFileName;
00031 ifstream binFile;
00032 int fileSize;
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
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
00072 RCCVmeInterface *vme1 = new RCCVmeInterface();
00073
00074
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 {
00100 try {
00101 unsigned long int fpgaHold = 0x40;
00102 unsigned long int mdspReset = 0x2;
00103
00104 cerr << "Put FPGAs on reset hold\n";
00105
00106 rod0->hpiLoad(FPGA_CONTROL_REG_REL_ADDR[1], fpgaHold);
00107 rod0->sleep(1000);
00108
00109 rod0->hpiLoad(FPGA_CONTROL_REG_REL_ADDR[2], mdspReset);
00110
00111 rod0->sleep(1000);
00112
00113 unsigned long hpicValue = 0x00010001;
00114 rod0->hpiLoad(HPIC, hpicValue);
00115
00116 rod0->sleep(1000);
00117
00118
00119 cerr << "Read from EMIF (1)\n";
00120 for(unsigned long addr = 0x01800000; addr < 0x0180001c; addr+=4) {
00121 unsigned long val = rod0->mdspSingleRead(addr);
00122 cerr << "0x" << hex << addr << ": 0x" << val << dec << endl;
00123 }
00124
00125 rod0->sleep(100);
00126
00127 cerr << "Write CE space setup\n";
00128 rod0->mdspSingleWrite(0x01800004, 0xffff3f03);
00129 rod0->mdspSingleWrite(0x01800008, 0x00000040);
00130 rod0->mdspSingleWrite(0x01800010, 0xffff3f33);
00131 rod0->mdspSingleWrite(0x01800014, 0xffff3f33);
00132
00133 rod0->sleep(500);
00134
00135 cerr << "Read from EMIF (2)\n";
00136
00137
00138
00139 for(unsigned long addr = 0x01800000; addr < 0x0180001c; addr+=4) {
00140 unsigned long val = rod0->mdspSingleRead(addr);
00141 cerr << "0x" << hex << addr << ": 0x" << val << dec << endl;
00142 }
00143 }
00144 catch (BaseException &b) {
00145 cout << "Exception \"initing\" ROD:\n" << b << endl;
00146 exit(0);
00147 }
00148
00149 rod0->sleep(3000);
00150 }
00151
00152 cout << "Enter binary file name, including extension (""q"" to quit): ";
00153 cin >> binFileName;
00154
00155 if (binFileName.c_str() == "q") exit(0);
00156 binFile.open(binFileName.c_str(), ios::binary);
00157 if (!binFile.is_open()) {
00158 cout << "Unable to open binary file." << endl;
00159 exit(1);
00160 }
00161
00162
00163 binFile.seekg(0, ios::end);
00164 fileSize = binFile.tellg();
00165 binFile.seekg(0, ios::beg);
00166
00167
00168
00169 UINT8 * buffer;
00170 try {
00171 buffer = new UINT8[fileSize];
00172 }
00173 catch (std::bad_alloc & ba) {
00174 cout << "Unable to allocate buffer for binary file." << endl;
00175 exit(2);
00176 }
00177 binFile.read((char *)buffer, fileSize);
00178
00179 cout << "Read file of size " << fileSize << endl;
00180
00181
00182 try {
00183 rod0->writeBlockToFlashHpi(prmStart, buffer, fileSize);
00184
00185 cout << fileSize << " bytes written to the MDSP flash memory"<< endl;
00186 } catch (VmeException &v) {
00187 cout << "VmeException creating Writing flash." << endl;
00188 cout << "ErrorClass = " << v.getErrorClass() << endl;
00189 cout << "ErrorCode = " << v.getErrorCode() << endl;
00190 } catch (RodException &r) {
00191 cout << r.getDescriptor() <<", " << r.getData1() << ", " << r.getData2()
00192 << '\n';
00193 }
00194
00195
00196 delete [] buffer;
00197 delete rod0;
00198 delete vme1;
00199
00200 return 0;
00201 }
00202
00203