μHAL (v2.6.5)
Part of the IPbus software repository
ProtocolPCIe.hpp
Go to the documentation of this file.
1 /*
2 ---------------------------------------------------------------------------
3 
4  This file is part of uHAL.
5 
6  uHAL is a hardware access library and programming framework
7  originally developed for upgrades of the Level-1 trigger of the CMS
8  experiment at CERN.
9 
10  uHAL is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  uHAL is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with uHAL. If not, see <http://www.gnu.org/licenses/>.
22 
23 
24  Andrew Rose, Imperial College, London
25  email: awr01 <AT> imperial.ac.uk
26 
27  Marc Magrans de Abril, CERN
28  email: marc.magrans.de.abril <AT> cern.ch
29 
30  Tom Williams, Rutherford Appleton Laboratory, Oxfordshire
31  email: tom.williams <AT> cern.ch
32 
33 ---------------------------------------------------------------------------
34 */
35 
42 #ifndef _uhal_ProtocolPCIe_hpp_
43 #define _uhal_ProtocolPCIe_hpp_
44 
45 #include <deque> // for deque
46 #include <stddef.h> // for size_t
47 #include <stdint.h> // for uint32_t, uint8_t
48 #include <string> // for string
49 #include <utility> // for pair
50 #include <vector> // for vector
51 
52 #include <boost/chrono/system_clocks.hpp> // for steady_clock
53 #include <boost/function.hpp> // for function
54 
55 #include "uhal/ClientInterface.hpp"
56 #include "uhal/log/exception.hpp"
57 #include "uhal/ProtocolIPbus.hpp"
58 
59 
60 namespace boost
61 {
62  template <class Y> class shared_ptr;
63 }
64 
65 namespace uhal
66 {
67  class Buffers;
68  struct URI;
69 
70  namespace exception
71  {
73  UHAL_DEFINE_DERIVED_EXCEPTION_CLASS ( PCIeTimeout , ClientTimeout , "Exception class to handle the case in which the PCIe connection timed out." )
75  UHAL_DEFINE_DERIVED_EXCEPTION_CLASS ( PCIeInitialisationError , TransportLayerError , "Exception class to handle a failure to read from the specified device files during initialisation." )
77  UHAL_DEFINE_DERIVED_EXCEPTION_CLASS ( PCIeCommunicationError , TransportLayerError , "Exception class to handle a low-level seek/read/write error after initialisation." )
78  }
79 
81  class PCIe : public IPbus< 2 , 0 >
82  {
83  public:
84  class PacketFmt {
85  public:
86  PacketFmt(const uint8_t* const, const size_t);
87  PacketFmt(const std::vector< std::pair<const uint8_t*, size_t> >& aData);
88  ~PacketFmt();
89 
90  const std::vector< std::pair<const uint8_t*, size_t> > mData;
91  };
92 
93  // private:
94  class File {
95  public:
96  File(const std::string& aPath, int aFlags);
97  ~File();
98 
99  const std::string& getPath() const;
100  void setPath(const std::string& aPath);
101 
102  void open();
103  void close();
104 
105  void createBuffer(const size_t aNrBytes);
106 
107  void read(const uint32_t aAddr, const uint32_t aNrWords, std::vector<uint32_t>& aValues);
108 
109  void write(const uint32_t aAddr, const std::vector<uint32_t>& aValues);
110 
111  void write(const uint32_t aAddr, const uint8_t* const aPtr, const size_t aNrBytes);
112 
113  void write(const uint32_t aAddr, const std::vector<std::pair<const uint8_t*, size_t> >& aData);
114 
115  private:
116  std::string mPath;
117  int mFd;
118  int mFlags;
119  size_t mBufferSize;
120  char* mBuffer;
121  };
122 
123  PCIe ( const PCIe& aPCIe );
124 
125  PCIe& operator= ( const PCIe& aPCIe );
126 
127  public:
133  PCIe ( const std::string& aId, const URI& aUri );
134 
136  virtual ~PCIe();
137 
138  private:
139 
145  void implementDispatch ( boost::shared_ptr< Buffers > aBuffers );
146 
150  virtual void Flush( );
151 
152 
154  virtual void dispatchExceptionHandler();
155 
156 
158 
159  typedef boost::chrono::steady_clock SteadyClock_t;
160 
165  uint32_t getMaxSendSize();
166 
171  uint32_t getMaxReplySize();
172 
174  void connect();
175 
177  void disconnect();
178 
180  void write(const boost::shared_ptr<Buffers>& aBuffers);
181 
183  void read();
184 
186 
193 
195 
197 
198  boost::chrono::microseconds mSleepDuration;
199 
200  uint32_t mNumberOfPages, mMaxInFlight, mPageSize, mMaxPacketSize, mIndexNextPage, mPublishedReplyPageCount, mReadReplyPageCount;
201 
203  std::deque < boost::shared_ptr< Buffers > > mReplyQueue;
204 
210  };
211 
212  std::ostream& operator<<(std::ostream& aStream, const PCIe::PacketFmt& aPacket);
213 
214 }
215 
216 
217 #endif
A class which provides the version-specific functionality for IPbus.
std::string mPath
std::ostream & operator<<(std::ostream &aStream, const PCIe::PacketFmt &aPacket)
uint32_t mReadReplyPageCount
const std::vector< std::pair< const uint8_t *, size_t > > mData
#define UHAL_DEFINE_DERIVED_EXCEPTION_CLASS(ClassName, BaseClassName, ClassDescription)
Macro for simplifying the declaration and definition of derived exception types.
Definition: exception.hpp:54
File mDeviceFileFPGAToHost
FPGA-to-host device file.
An abstract base exception class providing an interface to a throw/ThrowAsDerivedType mechanism which...
Definition: exception.hpp:89
uhal::exception::exception * mAsynchronousException
A pointer to an exception object for passing exceptions from the worker thread to the main thread...
boost::chrono::steady_clock SteadyClock_t
bool mUseInterrupt
IPbus< 2, 0 > InnerProtocol
File mDeviceFileHostToFPGA
Host-to-FPGA device file.
bool mXdma7seriesWorkaround
File mDeviceFileFPGAEvent
FPGA-to-host interrupt (event) file.
Transport protocol to transfer an IPbus buffer via PCIe.
c write(addr, xx[0])
Struct to store a URI when parsed by boost spirit.
Definition: URI.hpp:49
boost::chrono::microseconds mSleepDuration
std::deque< boost::shared_ptr< Buffers > > mReplyQueue
The list of buffers still awaiting a reply.