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