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