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