DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
uhallibs::Flx::Card Class Reference

Public Member Functions

 Card (const std::string &aPath, u_int aLockMask)
 ~Card ()
const std::string & getPath () const
int getDeviceId () const
void open ()
void close ()
void read (const uint32_t aAddr, const uint32_t aNrWords, std::vector< uint32_t > &aValues)
void write (const uint32_t aAddr, const std::vector< std::pair< const uint8_t *, size_t > > &aData)
bool haveLock () const
void lock ()
void unlock ()

Static Private Member Functions

static regmap_register_t * find_reg (const std::string &aName)

Private Attributes

std::string mPath
int mDeviceId
u_int mLockMask
FlxCard mFlxCard
bool mIsOpen
int mFd
bool mLocked

Detailed Description

Definition at line 100 of file ProtocolFlx.hpp.

Constructor & Destructor Documentation

◆ Card()

uhallibs::Flx::Card::Card ( const std::string & aPath,
u_int aLockMask )

Definition at line 93 of file ProtocolFlx.cpp.

94 : mPath(aDevicePath)
95 , mLockMask(aLockMask)
96 , mFlxCard()
97 , mIsOpen(false)
98{
99
100 const std::string prefix("/dev/flx");
101 if (aDevicePath.rfind(prefix, 0) != 0) {
102
103 exception::FlxInvalidDevice lExc;
104 log(lExc, "Invalid device path ", uhal::Quote(mPath));
105 throw lExc;
106 }
107
108 std::string device_id_str = aDevicePath.substr(prefix.size()).data();
109 try {
110 mDeviceId = std::stoi(device_id_str);
111 } catch (std::invalid_argument const& ex) {
112
113 exception::FlxInvalidDevice lExc;
114 log(lExc, "Invalid device id ", uhal::Quote(mPath));
115 throw lExc;
116 }
117}

◆ ~Card()

uhallibs::Flx::Card::~Card ( )

Definition at line 120 of file ProtocolFlx.cpp.

120{}

Member Function Documentation

◆ close()

void uhallibs::Flx::Card::close ( )

Definition at line 154 of file ProtocolFlx.cpp.

155{
156
157 if (mIsOpen)
158 mFlxCard.card_close();
159
160 if (mFd != -1) {
161 if (haveLock())
162 unlock();
163 int rc = ::close(mFd);
164 mFd = -1;
165 if (rc == -1)
166 log(uhal::Error(),
167 "Failed to close file ",
168 uhal::Quote(mPath),
169 "; errno=",
170 uhal::Integer(errno),
171 ", meaning ",
172 uhal::Quote(strerror(errno)));
173 }
174}
bool haveLock() const
const std::string strerror(int error)
Convert C error number to string.
Definition kernel.cpp:111

◆ find_reg()

regmap_register_t * uhallibs::Flx::Card::find_reg ( const std::string & aName)
staticprivate

Definition at line 79 of file ProtocolFlx.cpp.

80{
81
82 regmap_register_t* reg;
83 for (reg = regmap_registers; reg->name != NULL; reg++) {
84 if (aName == reg->name) {
85 return reg;
86 }
87 }
88
89 return nullptr;
90}

◆ getDeviceId()

int uhallibs::Flx::Card::getDeviceId ( ) const

Definition at line 185 of file ProtocolFlx.cpp.

186{
187 return mDeviceId;
188}

◆ getPath()

const std::string & uhallibs::Flx::Card::getPath ( ) const

Definition at line 178 of file ProtocolFlx.cpp.

179{
180 return mPath;
181}

◆ haveLock()

bool uhallibs::Flx::Card::haveLock ( ) const

Definition at line 271 of file ProtocolFlx.cpp.

272{
273 return mLocked;
274}

◆ lock()

void uhallibs::Flx::Card::lock ( )

Definition at line 277 of file ProtocolFlx.cpp.

278{
279 if (flock(mFd, LOCK_EX) == -1) {
280 ipc::exception::MutexError lExc;
281 log(lExc,
282 "Failed to lock device file ",
283 uhal::Quote(mPath),
284 "; errno=",
285 uhal::Integer(errno),
286 ", meaning ",
287 uhal::Quote(strerror(errno)));
288 throw lExc;
289 }
290 mLocked = true;
291}

◆ open()

void uhallibs::Flx::Card::open ( )

Definition at line 124 of file ProtocolFlx.cpp.

125{
126
127 log(uhal::Debug(), "Flx::Card client Opening felix endpoint ", mDeviceId, " on ", mPath);
128
129 if (access(mPath.c_str(), F_OK) == -1) {
130
131 exception::FlxInitialisationError lExc;
132 log(lExc,
133 "Failed to open device file ",
134 uhal::Quote(mPath),
135 "; errno=",
136 uhal::Integer(errno),
137 ", meaning ",
138 uhal::Quote(strerror(errno)));
139 throw lExc;
140 }
141
142 mFd = ::open(mPath.c_str(), O_RDWR);
143 if (mFd < 0) {
144 return;
145 }
146
147 mFlxCard.card_open(mDeviceId, mLockMask);
148
149 mIsOpen = true;
150}

◆ read()

void uhallibs::Flx::Card::read ( const uint32_t aAddr,
const uint32_t aNrWords,
std::vector< uint32_t > & aValues )

Definition at line 192 of file ProtocolFlx.cpp.

193{
194
195 if (!mIsOpen)
196 open();
197
198 flxcard_bar2_regs_t* bar2 = (flxcard_bar2_regs_t*)mFlxCard.openBackDoor(2);
199
200 // +1 is ceiling rounding in integers
201 uint32_t lNrReads64b = (aNrWords + 1) / 2;
202 uint32_t lAddr = aAddr / 2;
203
204 for (uint32_t i(0); i < lNrReads64b; i++) {
205 // *lReadAddrPtr = lAddr+i;
206 bar2->IPBUS_READ_ADDRESS = lAddr + i;
207 // uint64_t lDataWord = *lReadDataPtr;
208 uint64_t lDataWord = bar2->IPBUS_READ_DATA;
209 // Split the 64b word in 32b chunks
210 aValues.push_back(lDataWord & 0xffffffff);
211 if (2 * i + 1 < aNrWords)
212 aValues.push_back(lDataWord >> 32);
213 }
214
215 log(uhal::Debug(), "Flx::Card::read, ", aNrWords, " requested, ", aValues.size(), " read");
216}

◆ unlock()

void uhallibs::Flx::Card::unlock ( )

Definition at line 294 of file ProtocolFlx.cpp.

295{
296 if (flock(mFd, LOCK_UN) == -1) {
297 log(uhal::Warning(),
298 "Failed to unlock device file ",
299 uhal::Quote(mPath),
300 "; errno=",
301 uhal::Integer(errno),
302 ", meaning ",
303 uhal::Quote(strerror(errno)));
304 } else
305 mLocked = false;
306}

◆ write()

void uhallibs::Flx::Card::write ( const uint32_t aAddr,
const std::vector< std::pair< const uint8_t *, size_t > > & aData )

Definition at line 220 of file ProtocolFlx.cpp.

221{
222
223 if (!mIsOpen)
224 open();
225
226 flxcard_bar2_regs_t* bar2 = (flxcard_bar2_regs_t*)mFlxCard.openBackDoor(2);
227
228 size_t lNrBytes = 0;
229 for (size_t i = 0; i < aData.size(); i++)
230 lNrBytes += aData.at(i).second;
231
232 assert((lNrBytes % 4) == 0);
233
234 char* allocated = NULL;
235 posix_memalign((void**)&allocated, 4096 /*alignment*/, lNrBytes + 4096);
236 if (allocated == NULL) {
237 exception::FlxCommunicationError lExc;
238 log(lExc, "Failed to allocate ", uhal::Integer(lNrBytes + 4096), " bytes in File::write/2 function");
239 throw lExc;
240 }
241
242 // data to write to register address
243 char* buffer = allocated;
244 size_t lNrBytesCopied = 0;
245 for (size_t i = 0; i < aData.size(); i++) {
246 memcpy(buffer + lNrBytesCopied, aData.at(i).first, aData.at(i).second);
247 lNrBytesCopied += aData.at(i).second;
248 }
249
250 lNrBytesCopied = 0;
251 uint32_t lAddr = aAddr / 2;
252
253 while (lNrBytesCopied < lNrBytes) {
254 bar2->IPBUS_WRITE_ADDRESS = lAddr;
255 char* lSrcPtr = buffer + lNrBytesCopied;
256 if ((lNrBytes - lNrBytesCopied) >= 8) {
257 bar2->IPBUS_WRITE_DATA.DATA = *(uint64_t*)lSrcPtr;
258 lNrBytesCopied += 8;
259 } else if ((lNrBytes - lNrBytesCopied) >= 4) {
260 bar2->IPBUS_WRITE_DATA.DATA = uint64_t(*(uint32_t*)lSrcPtr);
261 lNrBytesCopied += 4;
262 }
263
264 ++lAddr;
265 }
266
267 free(allocated);
268}

Member Data Documentation

◆ mDeviceId

int uhallibs::Flx::Card::mDeviceId
private

Definition at line 128 of file ProtocolFlx.hpp.

◆ mFd

int uhallibs::Flx::Card::mFd
private

Definition at line 134 of file ProtocolFlx.hpp.

◆ mFlxCard

FlxCard uhallibs::Flx::Card::mFlxCard
private

Definition at line 131 of file ProtocolFlx.hpp.

◆ mIsOpen

bool uhallibs::Flx::Card::mIsOpen
private

Definition at line 132 of file ProtocolFlx.hpp.

◆ mLocked

bool uhallibs::Flx::Card::mLocked
private

Definition at line 135 of file ProtocolFlx.hpp.

◆ mLockMask

u_int uhallibs::Flx::Card::mLockMask
private

Definition at line 129 of file ProtocolFlx.hpp.

◆ mPath

std::string uhallibs::Flx::Card::mPath
private

Definition at line 127 of file ProtocolFlx.hpp.


The documentation for this class was generated from the following files: