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 97 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 33 of file ProtocolAxi4Lite.cpp.

33 {
34 if (mBuffer != nullptr) free(mBuffer);
35 close();
36}
void close()
Unmap and close file.

Member Function Documentation

◆ close()

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

Unmap and close file.

Definition at line 55 of file ProtocolAxi4Lite.cpp.

55 {
56 if (mBar != nullptr) munmap(mBar, mLength);
57 mBar = nullptr;
58
59 if (mFd != -1) {
60 if (haveLock())
61 unlock();
62 int rc = ::close(mFd);
63 mFd = -1;
64 if (rc == -1)
65 log (uhal::Error(), "Failed to close file ", uhal::Quote(mPath), "; errno=", uhal::Integer(errno), ", meaning ", uhal::Quote (strerror(errno)));
66 }
67}
const std::string strerror(int error)
Convert C error number to string.
Definition kernel.cpp:114

◆ createBuffer()

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

Create a local buffer.

Definition at line 70 of file ProtocolAxi4Lite.cpp.

70 {
71 if (mBuffer != NULL) {
72 if (mBufferSize >= aNrBytes)
73 return;
74 else {
75 free(mBuffer);
76 mBuffer = NULL;
77 mBufferSize = 0;
78 }
79 }
80
81 posix_memalign((void**)&mBuffer, 4096 /*alignment*/, aNrBytes + 4096);
82 if (mBuffer == NULL) {
83 exception::Axi4LiteCommunicationError lExc;
84 log(lExc, "Failed to allocate ", Integer(aNrBytes + 4096),
85 " bytes in Axi4Lite::MappedFile::createBuffer");
86 throw lExc;
87 }
88
89 mBufferSize = aNrBytes + 4096;
90}

◆ getPath()

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

Definition at line 38 of file ProtocolAxi4Lite.cpp.

38{ return mPath; }

◆ haveLock()

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

Definition at line 148 of file ProtocolAxi4Lite.cpp.

148{ return mLocked; }

◆ lock()

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

Definition at line 150 of file ProtocolAxi4Lite.cpp.

150 {
151 if (flock(mFd, LOCK_EX) == -1) {
152 ipc::exception::MutexError lExc;
153 log(lExc, "Failed to lock device file ", uhal::Quote(mPath),
154 "; errno=", Integer(errno), ", meaning ", uhal::Quote(strerror(errno)));
155 throw lExc;
156 }
157 mLocked = true;
158}

◆ open()

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

Open bus file and map it to memory.

Definition at line 43 of file ProtocolAxi4Lite.cpp.

43 {
44 if (mBar != nullptr) return;
45
46 mFd = ::open(mPath.c_str(), (mProtFlags & PROT_WRITE) ? O_RDWR : O_RDONLY);
47 if (mFd < 0) {
48 return;
49 }
50
51 void* lBar = mmap(nullptr, 4*mLength, mProtFlags, MAP_SHARED, mFd, 0);
52 mBar = (lBar == MAP_FAILED ? nullptr : (uint32_t*)lBar);
53}
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 92 of file ProtocolAxi4Lite.cpp.

93 {
94 if (mBar == nullptr) open();
95
96 for (size_t i(0); i < aNrWords; ++i) {
97 aValues.push_back(le32toh(mBar[aAddr + i]));
98 }
99}

◆ setLength()

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

Definition at line 41 of file ProtocolAxi4Lite.cpp.

41{ mLength = aLength; }

◆ setPath()

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

Definition at line 40 of file ProtocolAxi4Lite.cpp.

40{ mPath = aPath; }

◆ unlock()

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

Definition at line 160 of file ProtocolAxi4Lite.cpp.

160 {
161 if (flock(mFd, LOCK_UN) == -1) {
162 log(uhal::Warning(), "Failed to unlock device file ", uhal::Quote(mPath),
163 "; errno=", Integer(errno), ", meaning ", uhal::Quote(strerror(errno)));
164 } else
165 mLocked = false;
166}

◆ 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 119 of file ProtocolAxi4Lite.cpp.

120 {
121 if (mBar == nullptr) open();
122
123 size_t lNrBytes = 0;
124 for (size_t i = 0; i < aData.size(); i++) lNrBytes += aData.at(i).second;
125
126 assert((lNrBytes % 4) == 0);
127 size_t lNrWords = lNrBytes/4;
128
129 createBuffer(lNrBytes);
130
131 size_t k(0);
132 for (size_t i = 0; i < aData.size(); ++i) {
133 for (size_t j = 0; j < aData.at(i).second; ++j) {
134 mBuffer[k] = aData.at(i).first[j];
135 ++k;
136 }
137 }
138
139 auto mBuffer32b = reinterpret_cast<const uint32_t*>(mBuffer);
140
141 for (size_t i(0); i<lNrWords; ++i) {
142 mBar[aAddr + i] = htole32(mBuffer32b[i]);
143 }
144 // std::memcpy(mBar + aAddr, mBuffer, lNrBytes);
145
146}
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 101 of file ProtocolAxi4Lite.cpp.

101 {
102 write(aAddr, reinterpret_cast<const uint8_t*>(aValues.data()),
103 4 * aValues.size());
104}
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 106 of file ProtocolAxi4Lite.cpp.

107 {
108 if (mBar == nullptr) open();
109
110 assert((aNrBytes % 4) == 0);
111 uint32_t lNrWordsData = aNrBytes / 4;
112
113 auto lPtr32 = reinterpret_cast<const uint32_t*>(aPtr);
114 for (size_t i(0); i < lNrWordsData; ++i) {
115 mBar[aAddr + i] = lPtr32[i];
116 }
117}

Member Data Documentation

◆ mBar

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

Definition at line 137 of file ProtocolAxi4Lite.hpp.

◆ mBuffer

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

Definition at line 142 of file ProtocolAxi4Lite.hpp.

◆ mBufferSize

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

Definition at line 141 of file ProtocolAxi4Lite.hpp.

◆ mFd

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

Definition at line 136 of file ProtocolAxi4Lite.hpp.

◆ mLength

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

Definition at line 138 of file ProtocolAxi4Lite.hpp.

◆ mLocked

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

Definition at line 140 of file ProtocolAxi4Lite.hpp.

◆ mPath

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

Definition at line 135 of file ProtocolAxi4Lite.hpp.

◆ mProtFlags

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

Definition at line 139 of file ProtocolAxi4Lite.hpp.


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