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

#include <ProtocolAxi4Lite.hpp>

Public Member Functions

 MappedFile (const std::string &aPath, size_t aLength, int aProtFlags=PROT_WRITE)
 ~MappedFile ()
const std::string & getPath () const
void setPath (const std::string &aPath)
void setLength (size_t)
void open ()
 Open bus file and map it to memory.
void close ()
 Unmap and close file.
void createBuffer (const size_t aNrBytes)
 Create a local buffer.
void read (const uint32_t aAddr, const uint32_t aNrWords, std::vector< uint32_t > &aValues)
void write (const uint32_t aAddr, const std::vector< uint32_t > &aValues)
void write (const uint32_t aAddr, const uint8_t *const aPtr, const size_t aNrBytes)
void write (const uint32_t aAddr, const std::vector< std::pair< const uint8_t *, size_t > > &aData)
bool haveLock () const
void lock ()
void unlock ()

Private Attributes

std::string mPath
int mFd
uint32_t * mBar
size_t mLength
int mProtFlags
bool mLocked
size_t mBufferSize
char * mBuffer

Detailed Description

Definition at line 95 of file ProtocolAxi4Lite.hpp.

Constructor & Destructor Documentation

◆ MappedFile()

uhallibs::Axi4Lite::MappedFile::MappedFile ( const std::string & aPath,
size_t aLength,
int aProtFlags = PROT_WRITE )

◆ ~MappedFile()

uhallibs::Axi4Lite::MappedFile::~MappedFile ( )

Definition at line 34 of file ProtocolAxi4Lite.cpp.

35{
36 if (mBuffer != nullptr)
37 free(mBuffer);
38 close();
39}
void close()
Unmap and close file.

Member Function Documentation

◆ close()

void uhallibs::Axi4Lite::MappedFile::close ( )

Unmap and close file.

Definition at line 74 of file ProtocolAxi4Lite.cpp.

75{
76 if (mBar != nullptr)
77 munmap(mBar, mLength);
78 mBar = nullptr;
79
80 if (mFd != -1) {
81 if (haveLock())
82 unlock();
83 int rc = ::close(mFd);
84 mFd = -1;
85 if (rc == -1)
86 log(uhal::Error(),
87 "Failed to close file ",
88 uhal::Quote(mPath),
89 "; errno=",
90 uhal::Integer(errno),
91 ", meaning ",
92 uhal::Quote(strerror(errno)));
93 }
94}
const std::string strerror(int error)
Convert C error number to string.
Definition kernel.cpp:111

◆ createBuffer()

void uhallibs::Axi4Lite::MappedFile::createBuffer ( const size_t aNrBytes)

Create a local buffer.

Definition at line 97 of file ProtocolAxi4Lite.cpp.

98{
99 if (mBuffer != NULL) {
100 if (mBufferSize >= aNrBytes)
101 return;
102 else {
103 free(mBuffer);
104 mBuffer = NULL;
105 mBufferSize = 0;
106 }
107 }
108
109 posix_memalign((void**)&mBuffer, 4096 /*alignment*/, aNrBytes + 4096);
110 if (mBuffer == NULL) {
111 exception::Axi4LiteCommunicationError lExc;
112 log(lExc, "Failed to allocate ", Integer(aNrBytes + 4096), " bytes in Axi4Lite::MappedFile::createBuffer");
113 throw lExc;
114 }
115
116 mBufferSize = aNrBytes + 4096;
117}

◆ getPath()

const std::string & uhallibs::Axi4Lite::MappedFile::getPath ( ) const

Definition at line 42 of file ProtocolAxi4Lite.cpp.

43{
44 return mPath;
45}

◆ haveLock()

bool uhallibs::Axi4Lite::MappedFile::haveLock ( ) const

Definition at line 183 of file ProtocolAxi4Lite.cpp.

184{
185 return mLocked;
186}

◆ lock()

void uhallibs::Axi4Lite::MappedFile::lock ( )

Definition at line 189 of file ProtocolAxi4Lite.cpp.

190{
191 if (flock(mFd, LOCK_EX) == -1) {
192 ipc::exception::MutexError lExc;
193 log(lExc,
194 "Failed to lock device file ",
195 uhal::Quote(mPath),
196 "; errno=",
197 Integer(errno),
198 ", meaning ",
199 uhal::Quote(strerror(errno)));
200 throw lExc;
201 }
202 mLocked = true;
203}

◆ open()

void uhallibs::Axi4Lite::MappedFile::open ( )

Open bus file and map it to memory.

Definition at line 59 of file ProtocolAxi4Lite.cpp.

60{
61 if (mBar != nullptr)
62 return;
63
64 mFd = ::open(mPath.c_str(), (mProtFlags & PROT_WRITE) ? O_RDWR : O_RDONLY);
65 if (mFd < 0) {
66 return;
67 }
68
69 void* lBar = mmap(nullptr, 4 * mLength, mProtFlags, MAP_SHARED, mFd, 0);
70 mBar = (lBar == MAP_FAILED ? nullptr : (uint32_t*)lBar);
71}
void open()
Open bus file and map it to memory.

◆ read()

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

Definition at line 120 of file ProtocolAxi4Lite.cpp.

121{
122 if (mBar == nullptr)
123 open();
124
125 for (size_t i(0); i < aNrWords; ++i) {
126 aValues.push_back(le32toh(mBar[aAddr + i]));
127 }
128}

◆ setLength()

void uhallibs::Axi4Lite::MappedFile::setLength ( size_t aLength)

Definition at line 53 of file ProtocolAxi4Lite.cpp.

54{
55 mLength = aLength;
56}

◆ setPath()

void uhallibs::Axi4Lite::MappedFile::setPath ( const std::string & aPath)

Definition at line 48 of file ProtocolAxi4Lite.cpp.

49{
50 mPath = aPath;
51}

◆ unlock()

void uhallibs::Axi4Lite::MappedFile::unlock ( )

Definition at line 206 of file ProtocolAxi4Lite.cpp.

207{
208 if (flock(mFd, LOCK_UN) == -1) {
209 log(uhal::Warning(),
210 "Failed to unlock device file ",
211 uhal::Quote(mPath),
212 "; errno=",
213 Integer(errno),
214 ", meaning ",
215 uhal::Quote(strerror(errno)));
216 } else
217 mLocked = false;
218}

◆ write() [1/3]

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

Definition at line 152 of file ProtocolAxi4Lite.cpp.

153{
154 if (mBar == nullptr)
155 open();
156
157 size_t lNrBytes = 0;
158 for (size_t i = 0; i < aData.size(); i++)
159 lNrBytes += aData.at(i).second;
160
161 assert((lNrBytes % 4) == 0);
162 size_t lNrWords = lNrBytes / 4;
163
164 createBuffer(lNrBytes);
165
166 size_t k(0);
167 for (size_t i = 0; i < aData.size(); ++i) {
168 for (size_t j = 0; j < aData.at(i).second; ++j) {
169 mBuffer[k] = aData.at(i).first[j];
170 ++k;
171 }
172 }
173
174 auto mBuffer32b = reinterpret_cast<const uint32_t*>(mBuffer);
175
176 for (size_t i(0); i < lNrWords; ++i) {
177 mBar[aAddr + i] = htole32(mBuffer32b[i]);
178 }
179 // std::memcpy(mBar + aAddr, mBuffer, lNrBytes);
180}
void createBuffer(const size_t aNrBytes)
Create a local buffer.

◆ write() [2/3]

void uhallibs::Axi4Lite::MappedFile::write ( const uint32_t aAddr,
const std::vector< uint32_t > & aValues )

Definition at line 131 of file ProtocolAxi4Lite.cpp.

132{
133 write(aAddr, reinterpret_cast<const uint8_t*>(aValues.data()), 4 * aValues.size());
134}
void write(const uint32_t aAddr, const std::vector< uint32_t > &aValues)

◆ write() [3/3]

void uhallibs::Axi4Lite::MappedFile::write ( const uint32_t aAddr,
const uint8_t *const aPtr,
const size_t aNrBytes )

Definition at line 137 of file ProtocolAxi4Lite.cpp.

138{
139 if (mBar == nullptr)
140 open();
141
142 assert((aNrBytes % 4) == 0);
143 uint32_t lNrWordsData = aNrBytes / 4;
144
145 auto lPtr32 = reinterpret_cast<const uint32_t*>(aPtr);
146 for (size_t i(0); i < lNrWordsData; ++i) {
147 mBar[aAddr + i] = lPtr32[i];
148 }
149}

Member Data Documentation

◆ mBar

uint32_t* uhallibs::Axi4Lite::MappedFile::mBar
private

Definition at line 132 of file ProtocolAxi4Lite.hpp.

◆ mBuffer

char* uhallibs::Axi4Lite::MappedFile::mBuffer
private

Definition at line 137 of file ProtocolAxi4Lite.hpp.

◆ mBufferSize

size_t uhallibs::Axi4Lite::MappedFile::mBufferSize
private

Definition at line 136 of file ProtocolAxi4Lite.hpp.

◆ mFd

int uhallibs::Axi4Lite::MappedFile::mFd
private

Definition at line 131 of file ProtocolAxi4Lite.hpp.

◆ mLength

size_t uhallibs::Axi4Lite::MappedFile::mLength
private

Definition at line 133 of file ProtocolAxi4Lite.hpp.

◆ mLocked

bool uhallibs::Axi4Lite::MappedFile::mLocked
private

Definition at line 135 of file ProtocolAxi4Lite.hpp.

◆ mPath

std::string uhallibs::Axi4Lite::MappedFile::mPath
private

Definition at line 130 of file ProtocolAxi4Lite.hpp.

◆ mProtFlags

int uhallibs::Axi4Lite::MappedFile::mProtFlags
private

Definition at line 134 of file ProtocolAxi4Lite.hpp.


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