DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
ProtocolAxi4Lite.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
41
42#ifndef _DUNEDAQ_UHALLIBS_PROTOCOLAXI4LITE_HPP_
43#define _DUNEDAQ_UHALLIBS_PROTOCOLAXI4LITE_HPP_
44
45#include <stddef.h> // for size_t
46#include <stdint.h> // for uint32_t, uint8_t
47
48#include <chrono>
49#include <deque> // for deque
50#include <memory>
51#include <mutex>
52#include <string> // for string
53#include <utility> // for pair
54#include <vector> // for vector
55
56#include "uhal/ClientInterface.hpp"
57#include "uhal/ProtocolIPbus.hpp"
58#include "uhal/log/exception.hpp"
62
63namespace uhal {
64struct URI;
65class Buffers;
66}
67
68namespace uhallibs {
69
70namespace exception {
74 uhal::exception::ClientTimeout,
75 "Exception class to handle the case in "
76 "which the Axi4Lite connection timed out.")
79UHAL_DEFINE_DERIVED_EXCEPTION_CLASS(Axi4LiteInitialisationError,
80 uhal::exception::TransportLayerError,
81 "Exception class to handle a failure to read from the specified device "
82 "files during initialisation.")
85UHAL_DEFINE_DERIVED_EXCEPTION_CLASS(Axi4LiteCommunicationError,
86 uhal::exception::TransportLayerError,
87 "Exception class to handle a low-level seek/read/write error after "
88 "initialisation.")
89} // namespace exception
90
92class Axi4Lite : public uhal::IPbus<2, 0>
93{
94public:
96 {
97 public:
98 MappedFile(const std::string& aPath, size_t aLength, int aProtFlags = PROT_WRITE);
100
101 const std::string& getPath() const;
102
103 void setPath(const std::string& aPath);
104
105 void setLength(size_t);
106
108 void open();
110 void close();
111
113 void createBuffer(const size_t aNrBytes);
114
115 void read(const uint32_t aAddr, const uint32_t aNrWords, std::vector<uint32_t>& aValues);
116
117 void write(const uint32_t aAddr, const std::vector<uint32_t>& aValues);
118
119 void write(const uint32_t aAddr, const uint8_t* const aPtr, const size_t aNrBytes);
120
121 void write(const uint32_t aAddr, const std::vector<std::pair<const uint8_t*, size_t>>& aData);
122
123 bool haveLock() const;
124
125 void lock();
126
127 void unlock();
128
129 private:
130 std::string mPath;
131 int mFd;
132 uint32_t* mBar;
133 size_t mLength;
137 char* mBuffer;
138 };
139
145 Axi4Lite(const std::string& aId, const uhal::URI& aUri);
146
147 Axi4Lite(const Axi4Lite&) = delete;
148 Axi4Lite& operator=(const Axi4Lite&) = delete;
149
151 virtual ~Axi4Lite();
152
153private:
155 typedef std::unique_lock<IPCMutex_t> IPCScopedLock_t;
156
157 typedef IPbus<2, 0> InnerProtocol;
158 typedef std::chrono::steady_clock SteadyClock_t;
159
166 void implementDispatch(std::shared_ptr<uhal::Buffers> aBuffers);
167
170 virtual void Flush();
171
173 virtual void dispatchExceptionHandler();
174
175 static std::string getDevicePath(const uhal::URI& aUri);
176
177 static std::string getSharedMemName(const std::string&);
178
183 uint32_t getMaxSendSize();
184
189 uint32_t getMaxReplySize();
191 void connect();
192
195
197 void disconnect();
198
200 void write(const std::shared_ptr<uhal::Buffers>& aBuffers);
201
203 void read();
204
206
208
212
213 std::chrono::microseconds mSleepDuration;
214
217
219 std::deque<std::shared_ptr<uhal::Buffers>> mReplyQueue;
220};
221
222} // namespace uhal
223
224#endif /* _DUNEDAQ_UHALLIBS_PROTOCOLAXI4LITE_HPP_ */
void close()
Unmap and close file.
MappedFile(const std::string &aPath, size_t aLength, int aProtFlags=PROT_WRITE)
void open()
Open bus file and map it to memory.
void setPath(const std::string &aPath)
void write(const uint32_t aAddr, const std::vector< uint32_t > &aValues)
void createBuffer(const size_t aNrBytes)
Create a local buffer.
const std::string & getPath() const
void read(const uint32_t aAddr, const uint32_t aNrWords, std::vector< uint32_t > &aValues)
std::deque< std::shared_ptr< uhal::Buffers > > mReplyQueue
The list of buffers still awaiting a reply.
std::chrono::microseconds mSleepDuration
static std::string getSharedMemName(const std::string &)
ipc::SharedMemObject< IPCMutex_t > mIPCMutex
void connect()
Set up the connection to the device.
ipc::RobustMutex IPCMutex_t
static std::string getDevicePath(const uhal::URI &aUri)
IPbus< 2, 0 > InnerProtocol
void disconnect()
Close the connection to the device.
virtual void dispatchExceptionHandler()
Function which tidies up this protocol layer in the event of an exception.
Axi4Lite(const std::string &aId, const uhal::URI &aUri)
void write(const std::shared_ptr< uhal::Buffers > &aBuffers)
Write request packet to next page in host-to-FPGA device file.
Axi4Lite(const Axi4Lite &)=delete
std::chrono::steady_clock SteadyClock_t
Axi4Lite & operator=(const Axi4Lite &)=delete
void read()
Read next pending reply packet from appropriate page of FPGA-to-host device file, and validate conten...
std::unique_lock< IPCMutex_t > IPCScopedLock_t
void implementDispatch(std::shared_ptr< uhal::Buffers > aBuffers)
UHAL_DEFINE_DERIVED_EXCEPTION_CLASS(Axi4LiteTimeout, uhal::exception::ClientTimeout, "Exception class to handle the case in " "which the Axi4Lite connection timed out.") UHAL_DEFINE_DERIVED_EXCEPTION_CLASS(Axi4LiteInitialisationError