DUNE-DAQ
DUNE Trigger and Data Acquisition software
Toggle main menu visibility
Loading...
Searching...
No Matches
dunedaq
sourcecode
wibmod
src
WIB1
WIBBase.cpp
Go to the documentation of this file.
1
#include "
wibmod/WIB1/WIBBase.hh
"
2
#include "
wibmod/WIB1/BNL_UDP_Exception.hh
"
3
#include "
wibmod/WIB1/WIBException.hh
"
4
5
WIBBase::WIBBase
(std::string
const
& address, std::string
const
& WIBAddressTable, std::string
const
& FEMBAddressTable)
6
:
wib
(NULL)
7
,
FEMBReadSleepTime
(0.01)
8
,
FEMBWriteSleepTime
(0.01)
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
23
WIBBase::~WIBBase
()
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
37
std::string
38
WIBBase::GetAddress
()
39
{
40
return
wib
->GetRemoteAddress();
41
}
42
43
uint32_t
44
WIBBase::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
}
80
void
81
WIBBase::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
124
Item
const
*
125
WIBBase::GetItem
(std::string
const
& str)
126
{
127
return
wib
->GetItem(str);
128
}
129
130
Item
const
*
131
WIBBase::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
141
uint32_t
142
WIBBase::ReadWithRetry
(uint16_t address)
143
{
144
return
wib
->ReadWithRetry(address);
145
}
146
uint32_t
147
WIBBase::Read
(uint16_t address)
148
{
149
return
wib
->Read(address);
150
}
151
uint32_t
152
WIBBase::ReadWithRetry
(std::string
const
& address)
153
{
154
return
wib
->ReadWithRetry(address);
155
}
156
uint32_t
157
WIBBase::Read
(std::string
const
& address)
158
{
159
return
wib
->Read(address);
160
}
161
162
void
163
WIBBase::WriteWithRetry
(uint16_t address, uint32_t value)
164
{
165
wib
->WriteWithRetry(address, value);
166
}
167
void
168
WIBBase::Write
(uint16_t address, uint32_t value)
169
{
170
wib
->Write(address, value);
171
}
172
void
173
WIBBase::WriteWithRetry
(std::string
const
& address, uint32_t value)
174
{
175
wib
->WriteWithRetry(address, value);
176
}
177
void
178
WIBBase::Write
(std::string
const
& address, uint32_t value)
179
{
180
wib
->Write(address, value);
181
}
182
void
183
WIBBase::Write
(uint16_t address, std::vector<uint32_t>
const
& values)
184
{
185
wib
->Write(address, values);
186
}
187
void
188
WIBBase::Write
(std::string
const
& address, std::vector<uint32_t>
const
& values)
189
{
190
wib
->Write(address, values);
191
}
192
void
193
WIBBase::Write
(uint16_t address, uint32_t
const
* values,
size_t
word_count)
194
{
195
wib
->Write(address, values, word_count);
196
}
197
void
198
WIBBase::Write
(std::string
const
& address, uint32_t
const
* values,
size_t
word_count)
199
{
200
wib
->Write(address, values, word_count);
201
}
202
203
uint32_t
204
WIBBase::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
}
214
uint32_t
215
WIBBase::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
226
void
227
WIBBase::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
}
237
void
238
WIBBase::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
249
void
250
WIBBase::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
267
void
268
WIBBase::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
}
BNL_UDP_Exception.hh
WIBBase.hh
FEMB_COUNT
#define FEMB_COUNT
Definition
WIBBase.hh:9
WIBException.hh
AddressTable::Write
void Write(uint16_t, uint32_t)
Definition
AddressTable_IO.cpp:21
AddressTable::GetItem
Item const * GetItem(std::string const &)
Definition
AddressTable_item.cpp:12
AddressTable::AddressTable
AddressTable()
AddressTable::WriteWithRetry
void WriteWithRetry(uint16_t, uint32_t)
Definition
AddressTable_IO.cpp:26
AddressTable::Read
uint32_t Read(uint16_t)
Definition
AddressTable_IO.cpp:10
Item
Definition
AddressTable.hh:14
WIBBase::~WIBBase
~WIBBase()
Definition
WIBBase.cpp:23
WIBBase::GetAddress
std::string GetAddress()
Definition
WIBBase.cpp:38
WIBBase::FEMB
AddressTable * FEMB[FEMB_COUNT]
Definition
WIBBase.hh:78
WIBBase::WriteWithRetry
void WriteWithRetry(uint16_t address, uint32_t value)
Definition
WIBBase.cpp:163
WIBBase::WriteFEMBBits
void WriteFEMBBits(int iFEMB, uint16_t address, uint32_t pos, uint32_t mask, uint32_t value)
Definition
WIBBase.cpp:250
WIBBase::ReadWithRetry
uint32_t ReadWithRetry(uint16_t address)
Definition
WIBBase.cpp:142
WIBBase::GetItem
Item const * GetItem(std::string const &)
Definition
WIBBase.cpp:125
WIBBase::WriteI2C
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
WIBBase::FEMBWriteSleepTime
const float FEMBWriteSleepTime
Definition
WIBBase.hh:81
WIBBase::WriteFEMB
void WriteFEMB(int iFEMB, uint16_t address, uint32_t value)
Definition
WIBBase.cpp:227
WIBBase::GetFEMBItem
Item const * GetFEMBItem(int iFEMB, std::string const &)
Definition
WIBBase.cpp:131
WIBBase::Read
uint32_t Read(uint16_t address)
Definition
WIBBase.cpp:147
WIBBase::ReadI2C
uint32_t ReadI2C(std::string const &base_address, uint16_t I2C_aaddress, uint8_t byte_count=4)
Definition
WIBBase.cpp:44
WIBBase::Write
void Write(uint16_t address, uint32_t value)
Definition
WIBBase.cpp:168
WIBBase::ReadFEMB
uint32_t ReadFEMB(int iFEMB, uint16_t address)
Definition
WIBBase.cpp:204
WIBBase::WIBBase
WIBBase()
WIBBase::EnableADC
void EnableADC(uint64_t iFEMB, uint64_t enable)
Definition
WIBBase.cpp:268
WIBBase::FEMBReadSleepTime
const float FEMBReadSleepTime
Definition
WIBBase.hh:80
wib
Definition
wib.pb.cc:23
Generated on
for DUNE-DAQ by
1.18.0