Generic hardware access library
|
00001 #include "VMEDevice.hh" 00002 #include "VMEAddressTable.hh" 00003 #include "VMEAddressTableASCIIReader.hh" 00004 00005 // adjujst this to your needs (choose the busadapter) 00006 //#include "SBS620x86LinuxBusAdapter.hh" 00007 #include "VMEDummyBusAdapter.hh" 00008 // #include "MXI2x86LinuxBusAdapter.hh" 00009 // #include "VMEVxWorksMv2304BusAdapter.hh" 00010 00011 #include "CommandSequencer.hh" 00012 #include "CommandSequenceASCIIReader.hh" 00013 00014 #include <iostream> 00015 #include <sstream> 00016 #include <fstream> 00017 #include <iomanip> 00018 00019 // These are veeryyyy ugly hardcoded constand to be modified by 00020 // the user... 00021 #define DEVICE_BASEADDRESS 0x900800 00022 #define DEVICE_ADDRESSTABLE "TTCviAddressMap.dat" 00023 #define SEQUENCESETTINGS "Sequences.conf" 00024 00025 00029 void loadSequencer( HAL::CommandSequencer& sequencer, HAL::AddressTableInterface& addressTable ) { 00030 const int MaxLength=1024; 00031 char buffer[MaxLength]; 00032 std::ifstream input( SEQUENCESETTINGS ); 00033 if ( ! input ) { 00034 std::string text = "Cannot open file : " + std::string(SEQUENCESETTINGS); 00035 // throw (HAL::NoSuchFileException( text, __FILE__, __LINE__, __FUNCTION__ )); 00036 return; 00037 } 00038 std::cout << "trying to load " << SEQUENCESETTINGS << std::endl; 00039 while ( ! input.eof() ) { 00040 input.getline(buffer, MaxLength); 00041 std::istringstream Line(&buffer[0]); 00042 std::string word; 00043 Line >> word; 00044 if (word != "") { 00045 std::cout << "looking for " << word << std::endl; 00046 try { 00047 std::cout << "build reader" << std::endl; 00048 HAL::CommandSequenceASCIIReader* rd_ptr = new HAL::CommandSequenceASCIIReader( word); 00049 HAL::CommandSequence* seq_ptr = 00050 new HAL::CommandSequence(word, 00051 *rd_ptr, 00052 addressTable); 00053 sequencer.registerSequence( *seq_ptr ); 00054 } catch ( HAL::HardwareAccessException& e ) { 00055 std::cout << e.what() << std::endl; 00056 } 00057 } 00058 } 00059 input.close(); 00060 } 00061 00062 00063 00067 int main() { 00068 00069 try { 00070 // decide with which crate controler you want to wrok: 00071 // here the sbs busadapter is chosen: 00072 // HAL::SBS620x86LinuxBusAdapter busAdapter(0); 00073 HAL::VMEDummyBusAdapter busAdapter( HAL::VMEDummyBusAdapter::VERBOSE_ON, 00074 HAL::VMEDummyBusAdapter:: MEMORY_MAP_ON ); // verbose with memory mapping 00075 // HAL::MXI2x86LinuxBusAdapter busAdapter(0); 00076 // VMEVxWorksMv2304BusAdapter busAdapter; 00077 00078 HAL::VMEAddressTableASCIIReader addressTableReader( DEVICE_ADDRESSTABLE ); 00079 00080 HAL::VMEAddressTable addressTable( "Test address table", addressTableReader ); 00081 00082 // just to have some output: 00083 addressTable.print(); 00084 00085 HAL::VMEDevice device(addressTable, busAdapter, DEVICE_BASEADDRESS); 00086 00087 // we want to work with sequences (scripts) of the HAL 00088 HAL::CommandSequencer sequencer; 00089 loadSequencer( sequencer, addressTable ); 00090 00091 // at this point we have done everything to use the module 00092 // with all features of the HAL (sequences). 00093 00094 char *buffer; 00095 bool loop = true; 00096 std::string item; 00097 uint32_t option; 00098 uint32_t ic, ic2; 00099 uint32_t value, offset, size, dumpOffset, dumpSize, data; 00100 char *dataPtr; 00101 char *limit; 00102 char *maxLimit; 00103 std::vector<std::string> names; 00104 std::ofstream* output; 00105 HAL::CommandSequence* seq_ptr; 00106 HAL::CommandSequenceReader* seqrd_ptr; 00107 00108 // this is a primitive interactive menu loop: 00109 // here you hack in whatever you want to debug your module: 00110 while ( loop ) { 00111 std::cout << std::endl; 00112 std::cout << "1) write to item" << std::endl; 00113 std::cout << "2) read from item" << std::endl; 00114 std::cout << "3) register a new sequence" << std::endl; 00115 std::cout << "4) Excecute a sequence (after rescan)" << std::endl; 00116 std::cout << "5) Save registered sequences" << std::endl; 00117 std::cout << "6) try a block read" << std::endl; 00118 std::cout << "7) write a test block" << std::endl; 00119 std::cout << "8) write a test block with verify option" << std::endl; 00120 std::cout << "9) read a test block (give startitem)" << std::endl; 00121 std::cout << std::endl; 00122 std::cout << "0) end program" << std::endl; 00123 std::cout << std::endl; 00124 std::cout << "Enter option : "; 00125 00126 std::cin >> option; 00127 00128 switch (option) { 00129 case 0: 00130 loop = false; 00131 break; 00132 case 1: 00133 std::cout << "Enter item : "; 00134 std::cin >> item; 00135 std::cout << "Enter value (hex) : "; 00136 std::cin >> std::hex >> value; 00137 device.write( item, value ); 00138 break; 00139 case 2: 00140 std::cout << "Enter item : "; 00141 std::cin >> item; 00142 device.read( item, &value ); 00143 std::cout << "result : " << std::hex << std::setfill('0') << std::setw(8) << value << std::endl; 00144 break; 00145 case 3: 00146 std::cout << "Register new sequence : "; 00147 std::cin >> item; 00148 try { 00149 seqrd_ptr = new HAL::CommandSequenceASCIIReader( item ); 00150 seq_ptr = 00151 new HAL::CommandSequence(item, 00152 *seqrd_ptr, 00153 addressTable); 00154 sequencer.registerSequence( *seq_ptr ); 00155 } catch ( HAL::HardwareAccessException& e ) { 00156 std::cout << e.what() << std::endl; 00157 } 00158 break; 00159 case 4: 00160 names.clear(); 00161 names = sequencer.getNameVector(); 00162 std::cout << std::endl; 00163 if (names.size() == 0) { 00164 std::cout << "\nthere are no registered sequneces\n" << std::endl; 00165 } else { 00166 for( ic=0; ic < names.size(); ic++ ) { 00167 std::cout << " " << (ic+1) << ") " << names[ic] << std::endl; 00168 } 00169 std::cout << "Enter Number (0: do nothing): "; 00170 std::cin >> option; 00171 if ((option < 0) || (option > names.size())) { 00172 std::cout << "invalid number ! " << std::endl; 00173 } else if( option != 0 ) { 00174 sequencer.rescan( names[option-1] ); 00175 sequencer.run( names[option-1], device); 00176 } 00177 std::cout << std::endl; 00178 } 00179 break; 00180 case 5: 00181 output = new std::ofstream( SEQUENCESETTINGS ); 00182 names.clear(); 00183 names = sequencer.getNameVector(); 00184 for( ic=0; ic < names.size(); ic++ ) { 00185 *output << names[ic] << std::endl; 00186 } 00187 output->close(); 00188 delete(output); 00189 break; 00190 00191 case 6: 00192 std::cout << "Enter offset from where to start (hex) : "; 00193 std::cin >> std::hex >> offset; 00194 std::cout << "Enter number of bytes of the transfer (hex) : "; 00195 std::cin >> std::hex >> size; 00196 buffer = new char[(size+0x10)]; 00197 std::cout << "size : " << std::hex << size << std::endl; 00198 std::cout << "buffer : " << std::hex << buffer << std::endl; 00199 std::cout << "offset : " << std::hex << offset << std::endl; 00200 00201 device.readBlock( "RAMLow", size, buffer, HAL::HAL_DO_INCREMENT, offset ); 00202 00203 std::cout << "Enter offset for dumping (hex) :"; 00204 std::cin >> std::hex >> dumpOffset; 00205 std::cout << "How many bytes do you want to dump (hex) : "; 00206 std::cin >> std::hex >> dumpSize; 00207 dataPtr = buffer + dumpOffset; 00208 limit = buffer + dumpOffset + dumpSize; 00209 maxLimit = (&(buffer[size])); 00210 ic2 = 0; 00211 while (dataPtr < limit) { 00212 if (dataPtr >= maxLimit){ 00213 std::cout << " out of range" << std::endl; 00214 } else { 00215 std::cout << std::hex << std::setw(8) << std::setfill('0') << dataPtr << " , " 00216 << std::setw(8) << std::setfill('0') << ic2 << " : "; 00217 for (char* ic1 = dataPtr; ic1 < (dataPtr+0x10); ic1+=1) { 00218 data = *(ic1); 00219 std::cout << std::hex << std::setw(2) << std::setfill('0') << data << " "; 00220 } 00221 std::cout << std::endl; 00222 } 00223 dataPtr += 0x10; 00224 ic2 += 0x10; 00225 } 00226 delete (buffer); 00227 break; 00228 00229 case 7: 00230 std::cout << "Enter offset from where to start (hex) : "; 00231 std::cin >> std::hex >> offset; 00232 std::cout << "Enter number of bytes of the transfer (hex) : "; 00233 std::cin >> std::hex >> size; 00234 buffer = new char[(size+0x10)]; 00235 std::cout << "size : " << std::hex << size << std::endl; 00236 std::cout << "buffer : " << std::hex << buffer << std::endl; 00237 std::cout << "offset : " << std::hex << offset << std::endl; 00238 for (ic = 0; ic<size; ic+=4) { 00239 *((uint32_t*)(&buffer[ic])) = ic; 00240 } 00241 00242 device.writeBlock( "RAMLow", size, buffer, HAL::HAL_NO_VERIFY, HAL::HAL_DO_INCREMENT, offset ); 00243 00244 delete (buffer); 00245 00246 break; 00247 00248 case 8: 00249 std::cout << "Enter offset from where to start (hex) : "; 00250 std::cin >> std::hex >> offset; 00251 std::cout << "Enter number of bytes of the transfer (hex) : "; 00252 std::cin >> std::hex >> size; 00253 buffer = new char[(size+0x10)]; 00254 std::cout << "size : " << std::hex << size << std::endl; 00255 std::cout << "buffer : " << std::hex << buffer << std::endl; 00256 std::cout << "offset : " << std::hex << offset << std::endl; 00257 for (ic = 0; ic<size; ic+=4) { 00258 *((uint32_t*)(&buffer[ic])) = ic; 00259 } 00260 00261 device.writeBlock( "RAMLow", size, buffer, HAL::HAL_DO_VERIFY, HAL::HAL_DO_INCREMENT, offset ); 00262 00263 delete (buffer); 00264 break; 00265 case 9: 00266 std::cout << "Enter item where block starts : "; 00267 std::cin >> item; 00268 std::cout << "Enter length of block to read (hex) :"; 00269 std::cin >> std::hex >> size; 00270 buffer = new char[(size+0x10)]; 00271 device.readBlock( item, size, buffer, HAL::HAL_DO_INCREMENT, 0); 00272 for (ic = 0; ic<size; ic+=4) { 00273 std::cout << std::hex << *((uint32_t*)(&buffer[ic])) << std::endl; 00274 } 00275 00276 delete (buffer); 00277 break; 00278 } 00279 } 00280 sequencer.deleteSequences(); 00281 00282 // if somehting goes wrong in the HAL an excpetion will 00283 // bw thrown : here we get it and print out some info: 00284 // (look at the HAL-API to find out more about the exceptions 00285 // thrown) 00286 } catch (HAL::HardwareAccessException& e ) { 00287 std::cout << "exceptional exception : " << std::endl; 00288 std::cout << e.what() << std::endl; 00289 } catch ( ... ) { 00290 std::cout << "another exception" << std::endl; 00291 } 00292 std::cout << "This is the end, my friend..." << std::endl; 00293 return 0; 00294 } 00295