DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
WIBBase.cpp
Go to the documentation of this file.
4
5WIBBase::WIBBase(std::string const& address, std::string const& WIBAddressTable, std::string const& FEMBAddressTable)
6 : wib(NULL)
9{
10 // Make sure all pointers and NULL before any allocation
11
12 for (size_t iFEMB = 0; iFEMB < FEMB_COUNT; iFEMB++) {
13 FEMB[iFEMB] = NULL;
14 }
15
16 // Create the wib address table interface
17 wib = new AddressTable(WIBAddressTable, address, 0);
18 for (size_t iFEMB = 0; iFEMB < FEMB_COUNT; iFEMB++) {
19 FEMB[iFEMB] = new AddressTable(FEMBAddressTable, address, (iFEMB + 1) * 0x10);
20 }
21}
22
24{
25 if (wib != NULL) {
26 delete wib;
27 wib = NULL;
28 }
29 for (size_t iFEMB = 0; iFEMB < FEMB_COUNT; iFEMB++) {
30 if (FEMB[iFEMB] != NULL) {
31 delete FEMB[iFEMB];
32 FEMB[iFEMB] = NULL;
33 }
34 }
35}
36
37std::string
39{
40 return wib->GetRemoteAddress();
41}
42
43uint32_t
44WIBBase::ReadI2C(std::string const& base_address, uint16_t address, uint8_t byte_count)
45{
46 // This is an incredibly inefficient version of this function since it does a dozen or so UDP transactions.
47 // This is done to be generic, but it could be hard-coded by assuming address offsets and bit maps and done in one
48 // write and one read.
49
50 // Set type of read
51 WriteWithRetry(base_address + ".RW", 1);
52 // Set address
53 WriteWithRetry(base_address + ".ADDR", address);
54 // Set read size
55 WriteWithRetry(base_address + ".BYTE_COUNT", byte_count);
56 // Wait for the last transaction to be done
57 while (ReadWithRetry(base_address + ".DONE") == 0x0) {
58 usleep(1000);
59 }
60 // Run transaction
61 WriteWithRetry(base_address + ".RUN", 0x1);
62 // Wait for the last transaction to be done
63 while (ReadWithRetry(base_address + ".DONE") == 0x0) {
64 usleep(1000);
65 }
66 if (ReadWithRetry(base_address + ".ERROR")) {
67 printf("%s 0x%08X\n", (base_address + ".ERROR").c_str(), ReadWithRetry(base_address + ".ERROR"));
68 // Reset the I2C firmware
69 WriteWithRetry(base_address + ".RESET", 1);
70 char trans_info[] = "rd @ 0xFFFF";
71 sprintf(trans_info, "rd @ 0x%04X", address & 0xFFFF);
72 BUException::WIB_ERROR e;
73 e.Append("I2C Error on ");
74 e.Append(base_address.c_str());
75 e.Append(trans_info);
76 throw e;
77 }
78 return ReadWithRetry(base_address + ".RD_DATA");
79}
80void
81WIBBase::WriteI2C(std::string const& base_address,
82 uint16_t address,
83 uint32_t data,
84 uint8_t byte_count,
85 bool ignore_error)
86{
87 // This is an incredibly inefficient version of this function since it does a dozen or so UDP transactions.
88 // This is done to be generic, but it could be hard-coded by assuming address offsets and bit maps and done in one
89 // write and one read.
90
91 // Set type of read
92 WriteWithRetry(base_address + ".RW", 0);
93 // Set address
94 WriteWithRetry(base_address + ".ADDR", address);
95 // Set read size
96 WriteWithRetry(base_address + ".BYTE_COUNT", byte_count);
97 // Send data to write
98 WriteWithRetry(base_address + ".WR_DATA", data);
99 // Wait for the last transaction to be done
100 while (ReadWithRetry(base_address + ".DONE") == 0x0) {
101 usleep(1000);
102 }
103 // Run transaction
104 WriteWithRetry(base_address + ".RUN", 0x1);
105
106 // Wait for the last transaction to be done
107 while (ReadWithRetry(base_address + ".DONE") == 0x0) {
108 usleep(1000);
109 }
110
111 if (!ignore_error && ReadWithRetry(base_address + ".ERROR")) {
112 // Reset the I2C firmware
113 WriteWithRetry(base_address + ".RESET", 1);
114 char trans_info[] = "wr 0xFFFFFFFF @ 0xFFFF";
115 sprintf(trans_info, "wr 0x%08X @ 0x%04X", data, address & 0xFFFF);
116 BUException::WIB_ERROR e;
117 e.Append("I2C Error on ");
118 e.Append(base_address.c_str());
119 e.Append(trans_info);
120 throw e;
121 }
122}
123
124Item const*
125WIBBase::GetItem(std::string const& str)
126{
127 return wib->GetItem(str);
128}
129
130Item const*
131WIBBase::GetFEMBItem(int iFEMB, std::string const& str)
132{
133 if ((iFEMB > 4) || (iFEMB < 1)) {
134 BUException::WIB_INDEX_OUT_OF_RANGE e;
135 e.Append("In WIBBase::ReadFEMB\n");
136 throw e;
137 }
138 return FEMB[iFEMB - 1]->GetItem(str);
139}
140
141uint32_t
142WIBBase::ReadWithRetry(uint16_t address)
143{
144 return wib->ReadWithRetry(address);
145}
146uint32_t
147WIBBase::Read(uint16_t address)
148{
149 return wib->Read(address);
150}
151uint32_t
152WIBBase::ReadWithRetry(std::string const& address)
153{
154 return wib->ReadWithRetry(address);
155}
156uint32_t
157WIBBase::Read(std::string const& address)
158{
159 return wib->Read(address);
160}
161
162void
163WIBBase::WriteWithRetry(uint16_t address, uint32_t value)
164{
165 wib->WriteWithRetry(address, value);
166}
167void
168WIBBase::Write(uint16_t address, uint32_t value)
169{
170 wib->Write(address, value);
171}
172void
173WIBBase::WriteWithRetry(std::string const& address, uint32_t value)
174{
175 wib->WriteWithRetry(address, value);
176}
177void
178WIBBase::Write(std::string const& address, uint32_t value)
179{
180 wib->Write(address, value);
181}
182void
183WIBBase::Write(uint16_t address, std::vector<uint32_t> const& values)
184{
185 wib->Write(address, values);
186}
187void
188WIBBase::Write(std::string const& address, std::vector<uint32_t> const& values)
189{
190 wib->Write(address, values);
191}
192void
193WIBBase::Write(uint16_t address, uint32_t const* values, size_t word_count)
194{
195 wib->Write(address, values, word_count);
196}
197void
198WIBBase::Write(std::string const& address, uint32_t const* values, size_t word_count)
199{
200 wib->Write(address, values, word_count);
201}
202
203uint32_t
204WIBBase::ReadFEMB(int iFEMB, uint16_t address)
205{
206 if ((iFEMB > 4) || (iFEMB < 1)) {
207 BUException::WIB_INDEX_OUT_OF_RANGE e;
208 e.Append("In WIBBase::ReadFEMB\n");
209 throw e;
210 }
211 return FEMB[iFEMB - 1]->Read(address);
212 usleep((useconds_t)FEMBReadSleepTime * 1e6);
213}
214uint32_t
215WIBBase::ReadFEMB(int iFEMB, std::string const& address)
216{
217 if ((iFEMB > 4) || (iFEMB < 1)) {
218 BUException::WIB_INDEX_OUT_OF_RANGE e;
219 e.Append("In WIBBase::ReadFEMB\n");
220 throw e;
221 }
222 return FEMB[iFEMB - 1]->Read(address);
223 usleep((useconds_t)FEMBReadSleepTime * 1e6);
224}
225
226void
227WIBBase::WriteFEMB(int iFEMB, uint16_t address, uint32_t value)
228{
229 if ((iFEMB > 4) || (iFEMB < 1)) {
230 BUException::WIB_INDEX_OUT_OF_RANGE e;
231 e.Append("In WIBBase::WriteFEMB\n");
232 throw e;
233 }
234 FEMB[iFEMB - 1]->WriteWithRetry(address, value);
235 usleep((useconds_t)FEMBWriteSleepTime * 1e6);
236}
237void
238WIBBase::WriteFEMB(int iFEMB, std::string const& address, uint32_t value)
239{
240 if ((iFEMB > 4) || (iFEMB < 1)) {
241 BUException::WIB_INDEX_OUT_OF_RANGE e;
242 e.Append("In WIBBase::WriteFEMB\n");
243 throw e;
244 }
245 FEMB[iFEMB - 1]->WriteWithRetry(address, value);
246 usleep((useconds_t)FEMBWriteSleepTime * 1e6);
247}
248
249void
250WIBBase::WriteFEMBBits(int iFEMB, uint16_t address, uint32_t pos, uint32_t mask, uint32_t value)
251{
252 if ((iFEMB > 4) || (iFEMB < 1)) {
253 BUException::WIB_INDEX_OUT_OF_RANGE e;
254 e.Append("In WIBBase::WriteFEMB\n");
255 throw e;
256 }
257
258 uint32_t shiftVal = value & mask;
259 uint32_t regMask = (mask << pos);
260 uint32_t initVal = FEMB[iFEMB - 1]->Read(address);
261 uint32_t newVal = ((initVal & ~(regMask)) | (shiftVal << pos));
262
263 FEMB[iFEMB - 1]->WriteWithRetry(address, newVal);
264 usleep((useconds_t)FEMBWriteSleepTime * 1e6);
265}
266
267void
268WIBBase::EnableADC(uint64_t iFEMB, uint64_t enable)
269{
270 if (iFEMB > 4 || iFEMB < 1) {
271 BUException::WIB_INDEX_OUT_OF_RANGE e;
272 e.Append("In WIBBase::Enable_ADC");
273 throw e;
274 }
275 if (bool(enable))
276 FEMB[iFEMB - 1]->Write(0x03, 0x00);
277 else
278 FEMB[iFEMB - 1]->Write(0x03, 0xFF);
279 usleep((useconds_t)FEMBWriteSleepTime * 1e6);
280}
#define FEMB_COUNT
Definition WIBBase.hh:9
void Write(uint16_t, uint32_t)
Item const * GetItem(std::string const &)
void WriteWithRetry(uint16_t, uint32_t)
uint32_t Read(uint16_t)
~WIBBase()
Definition WIBBase.cpp:23
std::string GetAddress()
Definition WIBBase.cpp:38
AddressTable * FEMB[FEMB_COUNT]
Definition WIBBase.hh:78
void WriteWithRetry(uint16_t address, uint32_t value)
Definition WIBBase.cpp:163
void WriteFEMBBits(int iFEMB, uint16_t address, uint32_t pos, uint32_t mask, uint32_t value)
Definition WIBBase.cpp:250
uint32_t ReadWithRetry(uint16_t address)
Definition WIBBase.cpp:142
Item const * GetItem(std::string const &)
Definition WIBBase.cpp:125
void WriteI2C(std::string const &base_address, uint16_t I2C_address, uint32_t data, uint8_t byte_count=4, bool ignore_error=false)
Definition WIBBase.cpp:81
const float FEMBWriteSleepTime
Definition WIBBase.hh:81
void WriteFEMB(int iFEMB, uint16_t address, uint32_t value)
Definition WIBBase.cpp:227
Item const * GetFEMBItem(int iFEMB, std::string const &)
Definition WIBBase.cpp:131
uint32_t Read(uint16_t address)
Definition WIBBase.cpp:147
uint32_t ReadI2C(std::string const &base_address, uint16_t I2C_aaddress, uint8_t byte_count=4)
Definition WIBBase.cpp:44
void Write(uint16_t address, uint32_t value)
Definition WIBBase.cpp:168
uint32_t ReadFEMB(int iFEMB, uint16_t address)
Definition WIBBase.cpp:204
void EnableADC(uint64_t iFEMB, uint64_t enable)
Definition WIBBase.cpp:268
const float FEMBReadSleepTime
Definition WIBBase.hh:80
Definition wib.pb.cc:23