otsdaq_components  v2_04_01
VIPICROCDefinitions.cc
1 /*
2  * VIPICROCDefinitions.cc
3  *
4  * Created on: Jun 12, 2013
5  * Author: Luigi Vigani
6  */
7 
8 #include "otsdaq-components/DetectorHardware/VIPICROCDefinitions.h"
9 #include "otsdaq-core/BitManipulator/BitManipulator.h"
10 //#include "otsdaq-core/MessageFacility/MessageFacility.h"
11 #include <cassert>
12 #include <cstdlib>
13 #include <iostream>
14 #include "otsdaq-core/Macros/CoutMacros.h"
15 
16 using namespace ots;
17 
18 //========================================================================================================================
19 VIPICROCDefinitions::VIPICROCDefinitions() {}
20 
21 //========================================================================================================================
22 VIPICROCDefinitions::~VIPICROCDefinitions(void) {}
23 
24 //========================================================================================================================
25 unsigned int VIPICROCDefinitions::getDACRegisterAddress(const std::string& registerName)
26 {
27  if(registerName == "PulserData")
28  return 1;
29  else if(registerName == "PulserControl")
30  return 2;
31  else if(registerName == "IntegratorVbn")
32  return 3;
33  else if(registerName == "ShaperVbp2")
34  return 4;
35  else if(registerName == "ShaperVbp1")
36  return 5;
37  else if(registerName == "BLRestorer")
38  return 6;
39  else if(registerName == "VTn")
40  return 7;
41  else if(registerName == "VTp0")
42  return 8;
43  else if(registerName == "VTp1")
44  return 9;
45  else if(registerName == "VTp2")
46  return 10;
47  else if(registerName == "VTp3")
48  return 11;
49  else if(registerName == "VTp4")
50  return 12;
51  else if(registerName == "VTp5")
52  return 13;
53  else if(registerName == "VTp6")
54  return 14;
55  else if(registerName == "VTp7")
56  return 15;
57  else if(registerName == "ActiveLines")
58  return 16;
59  else if(registerName == "SendData")
60  return 19;
61  else if(registerName == "RejectHits")
62  return 20;
63  else if(registerName == "DigContrReg")
64  return 27;
65  else if(registerName == "AqBCO")
66  return 30;
67  // Kill and Inject are GENERAL names that will be used for all ROCs
68  else if(registerName == "Kill")
69  return 17;
70  else if(registerName == "Inject")
71  return 18;
72  else
73  {
74  std::cout << __COUT_HDR_FL__ << " Register Name not recognized!" << std::endl;
75  assert(0);
76  }
77  return 0;
78 }
79 
80 //========================================================================================================================
81 uint64_t VIPICROCDefinitions::makeDACWriteHeader(int chipId,
82  const std::string& registerName)
83 {
84  return makeDACHeader(chipId, registerName, write);
85 }
86 
87 //========================================================================================================================
88 uint64_t VIPICROCDefinitions::makeDACSetHeader(int chipId,
89  const std::string& registerName)
90 {
91  return makeDACHeader(chipId, registerName, set);
92 }
93 
94 //========================================================================================================================
95 uint64_t VIPICROCDefinitions::makeDACResetHeader(int chipId,
96  const std::string& registerName)
97 {
98  return makeDACHeader(chipId, registerName, reset);
99 }
100 
101 //========================================================================================================================
102 uint64_t VIPICROCDefinitions::makeDACReadHeader(int chipId,
103  const std::string& registerName)
104 {
105  return makeDACHeader(chipId, registerName, read);
106 }
107 
108 //========================================================================================================================
109 uint64_t VIPICROCDefinitions::makeDACHeader(int chipId,
110  const std::string& registerName,
111  unsigned int instruction)
112 {
113  /*Ryan
114  uint64_t command = 0;
115  //Insert Chip ID
116  BitManipulator::insertBits(command,BitManipulator::reverseBits(chipId,0,5),0,5);
117  //Insert Register Address
118  BitManipulator::insertBits(command,BitManipulator::reverseBits(getDACRegisterAddress(registerName),0,5),5,5);
119  //Insert instruction
120  BitManipulator::insertBits(command,BitManipulator::reverseBits(instruction,0,3),10,3);
121 */
122  uint32_t command = 0x80000000;
123  uint32_t registerAddress = getDACRegisterAddress(registerName);
124  // Insert Chip ID
125  BitManipulator::insertBits(command, chipId, 0, 5);
126  // Insert Register Address
127  BitManipulator::insertBits(command, registerAddress, 5, 5);
128  // Insert instruction
129  BitManipulator::insertBits(command, instruction, 10, 3);
130 
131  // Insert mask
132  // BitManipulator::insertBits(command,0x1,16,8);
133  unsigned int length = 3;
134  if(registerAddress < 16 || registerAddress < 27 || registerAddress < 30)
135  length = 3;
136  else if(registerAddress == 17 || registerAddress == 18)
137  length = 4;
138  else if(registerAddress == 16)
139  length = 2;
140  else if(registerAddress == 19 || registerAddress == 20)
141  length = 1;
142  // Insert command length
143  BitManipulator::insertBits(command, length, 24, 7);
144 
145  return command;
146 }
147 
148 //========================================================================================================================
149 uint64_t VIPICROCDefinitions::makeDACWriteCommand(int chipId,
150  const std::string& registerName,
151  unsigned int valueToWrite)
152 {
153  std::cout << __COUT_HDR_FL__ << "Working on Register " << registerName
154  << " Address: " << getDACRegisterAddress(registerName)
155  << " Value: " << valueToWrite << std::endl;
156  uint64_t command = 0;
157  if(registerName != "SendData" && registerName != "RejectHits")
158  {
159  command = makeDACWriteHeader(chipId, registerName);
160  BitManipulator::insertBits(
161  command, BitManipulator::reverseBits(valueToWrite, 0, 8), 13, 8);
162  }
163  else if(valueToWrite == 1)
164  command = makeDACSetHeader(chipId, registerName);
165  else
166  command = makeDACResetHeader(chipId, registerName);
167  // BitManipulator::insertBits(command,0x0,13,8);It doesn't matter the value you put in
168  // if is set or reset!
169 
170  return command;
171 }
172 
173 //========================================================================================================================
174 uint64_t VIPICROCDefinitions::makeDACReadCommand(int chipId,
175  const std::string& registerName)
176 {
177  return makeDACHeader(chipId, registerName, read);
178 }
179 
180 //========================================================================================================================
181 std::string VIPICROCDefinitions::makeMaskWriteCommand(int chipId,
182  const std::string& registerName,
183  std::string valueToWrite)
184 {
185  std::string command = "0000000000000000000";
186 
187  /*Ryan's
188  //Insert Chip ID
189  //BitManipulator::insertBits(command,BitManipulator::reverseBits(chipId,0,5),0,5);
190  BitManipulator::insertBits(command,chipId,0,5);
191  //Insert Register Address
192  //BitManipulator::insertBits(command,BitManipulator::reverseBits(getDACRegisterAddress(registerName),0,5),5,5);
193  BitManipulator::insertBits(command,getDACRegisterAddress(registerName),5,5);
194  //Insert instruction
195  //BitManipulator::insertBits(command,writeReversed,10,3);
196  BitManipulator::insertBits(command,write,10,3);
197 
198  UInt8 revField = (UInt8)0;
199  for (unsigned int c=0; c<valueToWrite.length(); c++)
200  {
201  if(registerName == "Inject")
202  revField = !(UInt8)valueToWrite.substr(c,1).data()[0];
203  else
204  revField = (UInt8)valueToWrite.substr(c,1).data()[0];
205  //revField = 0x40;
206  BitManipulator::insertBits(command,(uint64_t)revField,13+c,1);
207  }
208 
209  std::cout << __COUT_HDR_FL__ << " std::string made: " << std::endl;
210  for (int i=0; i<19; i++)
211  {
212  std::cout << __COUT_HDR_FL__ << hex << (uint64_t)command.substr(i, 1).data()[0] <<
213  "-" << std::endl;
214  }
215  std::cout << __COUT_HDR_FL__ << "\n" << std::endl;
216 */
217  return command;
218 }
219 
220 //========================================================================================================================
221 uint64_t VIPICROCDefinitions::makeMaskReadCommand(int chipId,
222  const std::string& registerName)
223 {
224  return 0;
225 }
226 
227 //========================================================================================================================
228 uint64_t VIPICROCDefinitions::setSendData(int chipId)
229 {
230  return makeDACWriteCommand(chipId, "SendData", 1);
231 }
232 
233 //========================================================================================================================
234 uint64_t VIPICROCDefinitions::resetSendData(int chipId)
235 {
236  return makeDACWriteCommand(chipId, "SendData", 0);
237 }
238 
239 //========================================================================================================================
240 uint64_t VIPICROCDefinitions::setRejectHits(int chipId)
241 {
242  return makeDACWriteCommand(chipId, "RejectHits", 1);
243 }
244 
245 //========================================================================================================================
246 uint64_t VIPICROCDefinitions::resetRejectHits(int chipId)
247 {
248  return makeDACWriteCommand(chipId, "RejectHits", 0);
249 }