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 110 of file ProtocolFlx.hpp.

Constructor & Destructor Documentation

◆ Card()

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

Definition at line 94 of file ProtocolFlx.cpp.

94 :
95 mPath(aDevicePath),
96 mLockMask(aLockMask),
97 mFlxCard(),
98 mIsOpen(false) {
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
109 std::string device_id_str = aDevicePath.substr(prefix.size()).data();
110 try {
111 mDeviceId = std::stoi(device_id_str);
112 } catch (std::invalid_argument const& ex) {
113
114 exception::FlxInvalidDevice lExc;
115 log(lExc, "Invalid device id ", uhal::Quote(mPath));
116 throw lExc;
117
118 }
119}

◆ ~Card()

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

Definition at line 123 of file ProtocolFlx.cpp.

123 {
124}

Member Function Documentation

◆ close()

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

Definition at line 151 of file ProtocolFlx.cpp.

151 {
152
153 if (mIsOpen)
154 mFlxCard.card_close();
155
156 if (mFd != -1) {
157 if (haveLock())
158 unlock();
159 int rc = ::close(mFd);
160 mFd = -1;
161 if (rc == -1)
162 log (uhal::Error(), "Failed to close file ", uhal::Quote(mPath), "; errno=", uhal::Integer(errno), ", meaning ", uhal::Quote (strerror(errno)));
163 }
164}
bool haveLock() const
const std::string strerror(int error)
Convert C error number to string.
Definition kernel.cpp:114

◆ find_reg()

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

Definition at line 81 of file ProtocolFlx.cpp.

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

◆ getDeviceId()

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

Definition at line 173 of file ProtocolFlx.cpp.

173 {
174 return mDeviceId;
175}

◆ getPath()

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

Definition at line 168 of file ProtocolFlx.cpp.

168 {
169 return mPath;
170}

◆ haveLock()

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

Definition at line 257 of file ProtocolFlx.cpp.

257{ return mLocked; }

◆ lock()

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

Definition at line 260 of file ProtocolFlx.cpp.

260 {
261 if (flock(mFd, LOCK_EX) == -1) {
262 ipc::exception::MutexError lExc;
263 log(lExc, "Failed to lock device file ", uhal::Quote(mPath),
264 "; errno=", uhal::Integer(errno), ", meaning ", uhal::Quote(strerror(errno)));
265 throw lExc;
266 }
267 mLocked = true;
268}

◆ open()

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

Definition at line 128 of file ProtocolFlx.cpp.

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

◆ read()

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

Definition at line 178 of file ProtocolFlx.cpp.

178 {
179
180 if (!mIsOpen)
181 open();
182
183 flxcard_bar2_regs_t *bar2 = (flxcard_bar2_regs_t *) mFlxCard.openBackDoor( 2 );
184
185 // +1 is ceiling rounding in integers
186 uint32_t lNrReads64b = (aNrWords+1)/2;
187 uint32_t lAddr = aAddr/2;
188
189 for ( uint32_t i(0); i<lNrReads64b; i++) {
190 // *lReadAddrPtr = lAddr+i;
191 bar2->IPBUS_READ_ADDRESS = lAddr+i;
192 // uint64_t lDataWord = *lReadDataPtr;
193 uint64_t lDataWord = bar2->IPBUS_READ_DATA;
194 // Split the 64b word in 32b chunks
195 aValues.push_back(lDataWord & 0xffffffff);
196 if ( 2*i+1 < aNrWords )
197 aValues.push_back(lDataWord >> 32);
198 }
199
200 log(uhal::Debug(), "Flx::Card::read, ", aNrWords, " requested, ", aValues.size(), " read");
201
202}

◆ unlock()

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

Definition at line 270 of file ProtocolFlx.cpp.

270 {
271 if (flock(mFd, LOCK_UN) == -1) {
272 log(uhal::Warning(), "Failed to unlock device file ", uhal::Quote(mPath),
273 "; errno=", uhal::Integer(errno), ", meaning ", uhal::Quote(strerror(errno)));
274 } else
275 mLocked = false;
276}

◆ write()

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

Definition at line 206 of file ProtocolFlx.cpp.

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

Member Data Documentation

◆ mDeviceId

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

Definition at line 139 of file ProtocolFlx.hpp.

◆ mFd

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

Definition at line 145 of file ProtocolFlx.hpp.

◆ mFlxCard

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

Definition at line 142 of file ProtocolFlx.hpp.

◆ mIsOpen

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

Definition at line 143 of file ProtocolFlx.hpp.

◆ mLocked

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

Definition at line 146 of file ProtocolFlx.hpp.

◆ mLockMask

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

Definition at line 140 of file ProtocolFlx.hpp.

◆ mPath

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

Definition at line 138 of file ProtocolFlx.hpp.


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