tdaq-develop-2025-02-12
ROCCosmicRayVetoInterface_interface.cc
1 #include "otsdaq-mu2e-crv/FEInterfaces/ROCCosmicRayVetoInterface.h"
2 #include "otsdaq-mu2e-crv/FEInterfaces/ROC_Registers.h"
3 
4 #include "otsdaq/Macros/InterfacePluginMacros.h"
5 
6 using namespace ots;
7 
8 #undef __MF_SUBJECT__
9 #define __MF_SUBJECT__ "FE-ROCCosmicRayVetoInterface"
10 
11 //=========================================================================================
12 ROCCosmicRayVetoInterface::ROCCosmicRayVetoInterface(
13  const std::string& rocUID,
14  const ConfigurationTree& theXDAQContextConfigTree,
15  const std::string& theConfigurationPath)
16  : ROCCoreVInterface(rocUID, theXDAQContextConfigTree, theConfigurationPath)
17 {
18  INIT_MF("." /*directory used is USER_DATA/LOG/.*/);
19 
20  __COUT_INFO__ << "ROCCosmicRayVetoInterface instantiated with link: "
21  << (int)linkID_ << " and EventWindowDelayOffset = " << delay_ << __E__;
22 
23  registerFEMacroFunction(
24  "Do the CRV Dance",
25  static_cast<FEVInterface::frontEndMacroFunction_t>(
26  &ROCCosmicRayVetoInterface::DoTheCRV_Dance),
27  std::vector<std::string>{"Which Step"},
28  std::vector<std::string>{
29  "Random Result"},
30  1); // requiredUserPermissions
31 
32  registerFEMacroFunction("Get Firmware Version",
33  static_cast<FEVInterface::frontEndMacroFunction_t>(
34  &ROCCosmicRayVetoInterface::GetFirmwareVersion),
35  std::vector<std::string>{},
36  std::vector<std::string>{"version", "git hash"},
37  1); // requiredUserPermissions
38 
39  registerFEMacroFunction("Get Test Counter",
40  static_cast<FEVInterface::frontEndMacroFunction_t>(
41  &ROCCosmicRayVetoInterface::GetTestCounter),
42  std::vector<std::string>{},
43  std::vector<std::string>{"counter"},
44  1); // requiredUserPermissions
45 
46  registerFEMacroFunction("Set Test Counter",
47  static_cast<FEVInterface::frontEndMacroFunction_t>(
48  &ROCCosmicRayVetoInterface::SetTestCounter),
49  std::vector<std::string>{"Set Counter (Default: 0)"},
50  std::vector<std::string>{},
51  1); // requiredUserPermissions
52 
53  registerFEMacroFunction("Reset uC",
54  static_cast<FEVInterface::frontEndMacroFunction_t>(
55  &ROCCosmicRayVetoInterface::HardReset),
56  std::vector<std::string>{},
57  std::vector<std::string>{},
58  1); // requiredUserPermissions
59 
60  registerFEMacroFunction("Configure ROC",
61  static_cast<FEVInterface::frontEndMacroFunction_t>(
62  &ROCCosmicRayVetoInterface::RocConfigure),
63  std::vector<std::string>{"send GR packages (Default: true)"},
64  std::vector<std::string>{},
65  1); // requiredUserPermissions
66 
67  registerFEMacroFunction("Reset TX and Counters",
68  static_cast<FEVInterface::frontEndMacroFunction_t>(
69  &ROCCosmicRayVetoInterface::SoftReset),
70  std::vector<std::string>{},
71  std::vector<std::string>{},
72  1); // requiredUserPermissions
73 
74  registerFEMacroFunction("Get Status",
75  static_cast<FEVInterface::frontEndMacroFunction_t>(
76  &ROCCosmicRayVetoInterface::GetStatus),
77  std::vector<std::string>{},
78  std::vector<std::string>{
79  "version", "git hash",
80  "CR", "Send GR", "Loopback Mode", "PLL lock", "Active Ports", "Uptime", "Link Errors Loss", "Link Errors CRC",
81  "Test Cnt", "Marker Decoded Cnt", "Marker Delayed Cnt", "Heartbeat Rx Cnt", "Heartbeat Tx Cnt", "DR Cnt", "Injection Cnt", "Loopback Markers (fiber) Cnt",
82  "Last Event Length (12.5ns)", "Injection Length (12.5ns)", "Injection Timestamp"
83  },
84  1); // requiredUserPermissions
85 
86  registerFEMacroFunction("Read Fiber Rx",
87  static_cast<FEVInterface::frontEndMacroFunction_t>(
88  &ROCCosmicRayVetoInterface::FiberRx),
89  std::vector<std::string>{"number of packages (Default: 10)"},
90  std::vector<std::string>{"buffer"},
91  1); // requiredUserPermissions
92 
93  registerFEMacroFunction("Read Fiber Tx",
94  static_cast<FEVInterface::frontEndMacroFunction_t>(
95  &ROCCosmicRayVetoInterface::FiberTx),
96  std::vector<std::string>{"number of packages (Default: 10)"},
97  std::vector<std::string>{"buffer"},
98  1); // requiredUserPermissions
99 
100  registerFEMacroFunction("Set Loopback Mode",
101  static_cast<FEVInterface::frontEndMacroFunction_t>(
102  &ROCCosmicRayVetoInterface::SetLoopbackMode),
103  std::vector<std::string>{"loopback mode (Default: 0)"},
104  std::vector<std::string>{},
105  1); // requiredUserPermissions
106 
107 }
108 
109 //==========================================================================================
110 ROCCosmicRayVetoInterface::~ROCCosmicRayVetoInterface(void)
111 {
112  // NOTE:: be careful not to call __FE_COUT__ decoration because it uses the
113  // tree and it may already be destructed partially
114  __COUT__ << FEVInterface::interfaceUID_ << " Destructor" << __E__;
115 }
116 
118 //void ROCCosmicRayVetoInterface::writeROCRegister(uint16_t address, uint16_t data_to_write)
119 //{
120 // __FE_COUT__ << "Calling write ROC register: link number " << std::dec << (int)linkID_
121 // << ", address = " << address << ", write data = " << data_to_write
122 // << __E__;
123 //
124 // return;
125 //}
126 //
128 //uint16_t ROCCosmicRayVetoInterface::readROCRegister(uint16_t address)
129 //{
130 // __FE_COUT__ << "Calling read ROC register: link number " << std::dec << linkID_
131 // << ", address = " << address << __E__;
132 //
133 // return -1;
134 //}
135 //
136 //============================================================================================
137 void ROCCosmicRayVetoInterface::writeEmulatorRegister(uint16_t address,
138  uint16_t data_to_write)
139 {
140  __FE_COUT__ << "Calling write ROC Emulator register: link number " << std::dec
141  << (int)linkID_ << ", address = " << address
142  << ", write data = " << data_to_write << __E__;
143 
144  return;
145 }
146 
147 //==================================================================================================
148 uint16_t ROCCosmicRayVetoInterface::readEmulatorRegister(uint16_t address)
149 {
150  __FE_COUT__ << "Calling read ROC Emulator register: link number " << std::dec
151  << (int)linkID_ << ", address = " << address << __E__;
152 
153  return -1;
154 }
155 
157 //int ROCCosmicRayVetoInterface::readTimestamp() { return this->readRegister(12); }
158 
160 //void ROCCosmicRayVetoInterface::writeDelay(uint16_t delay)
161 //{
162 // this->writeRegister(21, delay);
163 // return;
164 //}
165 
167 //int ROCCosmicRayVetoInterface::readDelay() { return this->readRegister(7); }
168 
170 //int ROCCosmicRayVetoInterface::readDTCLinkLossCounter() { return this->readRegister(8); }
171 
173 //void ROCCosmicRayVetoInterface::resetDTCLinkLossCounter()
174 //{
175 // this->writeRegister(24, 0x1);
176 // return;
177 //}
178 
179 //==================================================================================================
180 void ROCCosmicRayVetoInterface::configure(void) try
181 {
182  __COUT_INFO__ << ".... do nothing for CRV ROC yet ... " << __E__;
183 
184  // __COUT_INFO__ << "......... Clear DCS FIFOs" << __E__;
185 }
186 catch(const std::runtime_error& e)
187 {
188  __FE_COUT__ << "Error caught: " << e.what() << __E__;
189  throw;
190 }
191 catch(...)
192 {
193  __FE_SS__ << "Unknown error caught. Check printouts!" << __E__;
194  __FE_COUT__ << ss.str();
195  __FE_SS_THROW__;
196 }
197 
198 //==============================================================================
199 void ROCCosmicRayVetoInterface::halt(void) {}
200 
201 //==============================================================================
202 void ROCCosmicRayVetoInterface::pause(void) {}
203 
204 //==============================================================================
205 void ROCCosmicRayVetoInterface::resume(void) {}
206 
207 //==============================================================================
208 void ROCCosmicRayVetoInterface::start(std::string) // runNumber)
209 {
210 }
211 
212 //==============================================================================
213 void ROCCosmicRayVetoInterface::stop(void) {}
214 
215 //==============================================================================
216 bool ROCCosmicRayVetoInterface::running(void) { return false; }
217 
218 //========================================================================
219 void ROCCosmicRayVetoInterface::DoTheCRV_Dance(__ARGS__)
220 {
221 // uint32_t address = __GET_ARG_IN__("Which Step", uint32_t);
222  __FE_COUT__ << "Hello" << __E__;
223  __SET_ARG_OUT__("Random Result",0xA4);
224 
225 } //end DoTheCRV_Dance()
226 
227 void ROCCosmicRayVetoInterface::GetFirmwareVersion(__ARGS__)
228 {
229  __SET_ARG_OUT__("version", this->readRegister(ROCLib::ROC_Register::Version));
230  __SET_ARG_OUT__("git hash",
231  (this->readRegister(ROCLib::ROC_Register::GitHashHigh) << 16) +
232  this->readRegister(ROCLib::ROC_Register::GitHashLow)
233  );
234 } //end GetFirmwareVersion()
235 
236 void ROCCosmicRayVetoInterface::GetTestCounter(__ARGS__)
237 {
238  __SET_ARG_OUT__("counter", this->readRegister(ROCLib::ROC_Register::TestCounter));
239 } //end GetTestCounter()
240 
241 void ROCCosmicRayVetoInterface::SetTestCounter(__ARGS__)
242 {
243  uint16_t value = __GET_ARG_IN__("Set Counter (Default: 0)", uint16_t, 0);
244  this->writeRegister(ROCLib::ROC_Register::TestCounter, value);
245 } //end SetTestCounter()
246 
247 void ROCCosmicRayVetoInterface::Reset() {
248  this->writeRegister(ROCLib::ROC_Register::Reset, 0x1);
249 }
250 void ROCCosmicRayVetoInterface::HardReset(__ARGS__) { Reset(); }
251 
252 void ROCCosmicRayVetoInterface::ResetTxBuffers() {
253  this->writeRegister(ROCLib::ROC_Register::GTP_CRC, 0x1);
254  this->writeRegister(ROCLib::ROC_Register::CRS, 0x300);
255 }
256 void ROCCosmicRayVetoInterface::SoftReset(__ARGS__)
257 {
258  ResetTxBuffers();
259 }
260 
261 void ROCCosmicRayVetoInterface::RocConfigure(bool gr) {
262  // Enable the onboard PLL
263  this->writeRegister(ROCLib::ROC_Register::PLLStat, 0x0);
264  // and configure PLL mux to read digital lock
265  this->writeRegister(ROCLib::ROC_Register::PLLMuxHigh, 0x12);
266  this->writeRegister(ROCLib::ROC_Register::PLLMuxHLow, 0x12);
267 
268  // enable package forwarding based on markers
269  this->writeRegister(ROCLib::ROC_Register::CR, 0x20);
270 
271  // Set CSR of data-FPGAs
272  this->writeRegister(ROCLib::ROC_Register::Data_Broadcast|ROCLib::ROC_Register::Data_CRC, 0xA8);
273 
274  // Reset input buffers
275  ResetTxBuffers();
276 
277  // Reset DDR on Data FPGAs
278  for (int i = 0; i < 3; ++i) {
279  this->writeRegister(ROCLib::ROC_Register_Data[i]|ROCLib::ROC_Register::Data_DDR_WriteHigh,0x0);
280  this->writeRegister(ROCLib::ROC_Register_Data[i]|ROCLib::ROC_Register::Data_DDR_WriteHigh,0x0);
281  this->writeRegister(ROCLib::ROC_Register_Data[i]|ROCLib::ROC_Register::Data_DDR_ReadHigh, 0x0);
282  this->writeRegister(ROCLib::ROC_Register_Data[i]|ROCLib::ROC_Register::Data_DDR_ReadLow, 0x0);
283  }
284 
285  // Set TRIG 1
286  this->writeRegister(ROCLib::ROC_Register::TRIG, 0x1);
287 
288  // Enable GR package return
289  if(gr) this->writeRegister(ROCLib::ROC_Register::sendGR, 0x1);
290  else this->writeRegister(ROCLib::ROC_Register::sendGR, 0x0);
291 
292 }
293 void ROCCosmicRayVetoInterface::RocConfigure(__ARGS__)
294 {
295  bool gr = __GET_ARG_IN__("send GR packages (Default: true)", bool, true);
296  RocConfigure(gr);
297 }
298 
299 void ROCCosmicRayVetoInterface::GetStatus(__ARGS__)
300 {
301  __SET_ARG_OUT__("version", this->readRegister(ROCLib::ROC_Register::Version));
302  __SET_ARG_OUT__("git hash",
303  ((this->readRegister(ROCLib::ROC_Register::GitHashHigh) << 16) +
304  this->readRegister(ROCLib::ROC_Register::GitHashLow)) & 0xffffffff
305  );
306  __SET_ARG_OUT__("CR", this->readRegister(ROCLib::ROC_Register::CR));
307  __SET_ARG_OUT__("Send GR", this->readRegister(ROCLib::ROC_Register::sendGR) & 0x1);
308  __SET_ARG_OUT__("Loopback Mode", this->readRegister(ROCLib::ROC_Register::LoopbackMode));
309  __SET_ARG_OUT__("PLL lock", ((this->readRegister(ROCLib::ROC_Register::PLLStat)) >> 4) & 0x1 );
310  __SET_ARG_OUT__("Active Ports",
311  (this->readRegister(ROCLib::ROC_Register::ActivePortsHigh) << 16) +
312  this->readRegister(ROCLib::ROC_Register::ActivePortsLow));
313  __SET_ARG_OUT__("Uptime",
314  (this->readRegister(ROCLib::ROC_Register::UpTimeHigh) << 16) +
315  this->readRegister(ROCLib::ROC_Register::UpTimeLow));
316  __SET_ARG_OUT__("Link Errors Loss", this->readRegister(ROCLib::ROC_Register::LinkErrors) & 0xff);
317  __SET_ARG_OUT__("Link Errors CRC", this->readRegister(ROCLib::ROC_Register::LinkErrors) >> 12);
318 
319  // Counters
320  __SET_ARG_OUT__("Test Cnt", this->readRegister(ROCLib::ROC_Register::TestCounter));
321  __SET_ARG_OUT__("Marker Decoded Cnt", this->readRegister(ROCLib::ROC_Register::MarkerCnt) & 0xff);
322  __SET_ARG_OUT__("Marker Delayed Cnt", (this->readRegister(ROCLib::ROC_Register::MarkerCnt) >> 8));
323  __SET_ARG_OUT__("Heartbeat Rx Cnt", this->readRegister(ROCLib::ROC_Register::HeartBeat) & 0xff);
324  __SET_ARG_OUT__("Heartbeat Tx Cnt", this->readRegister(ROCLib::ROC_Register::HeartBeat) >> 8);
325  __SET_ARG_OUT__("DR Cnt",
326  (this->readRegister(ROCLib::ROC_Register::DRCntHigh) << 16) +
327  this->readRegister(ROCLib::ROC_Register::DRCnLow));
328  __SET_ARG_OUT__("Injection Cnt", this->readRegister(ROCLib::ROC_Register::InjectionCnt));
329  __SET_ARG_OUT__("Loopback Markers (fiber) Cnt", this->readRegister(ROCLib::ROC_Register::LoopbackMarkerCnt));
330 
331  // Event Lengths
332  __SET_ARG_OUT__("Last Event Length (12.5ns)", this->readRegister(ROCLib::ROC_Register::LastEventLength));
333  __SET_ARG_OUT__("Injection Length (12.5ns)", this->readRegister(ROCLib::ROC_Register::InjectionLength));
334  __SET_ARG_OUT__("Injection Timestamp", this->readRegister(ROCLib::ROC_Register::InjectionTS));
335 
336 }
337 
338 void ROCCosmicRayVetoInterface::FiberRx(__ARGS__) {
339  int n = __GET_ARG_IN__("number of packages (Default: 10)", int, 10);
340 
341  std::stringstream o;
342  o << std::endl;
343  for(int i = 0; i < n; ++i) { // n packages
344  for(int k = 0; k < 10; ++k) { // 10 words per package
345  o << std::hex << std::setfill('0') << std::setw(4) << this->readRegister(ROCLib::ROC_Register::GTPRxRead) << " ";
346  }
347  o << std::endl;
348  }
349  __SET_ARG_OUT__("buffer", o.str());
350 }
351 
352 void ROCCosmicRayVetoInterface::FiberTx(__ARGS__) {
353  int n = __GET_ARG_IN__("number of packages (Default: 10)", int, 10);
354 
355  std::stringstream o;
356  o << std::endl;
357  for(int i = 0; i < n; ++i) { // n packages
358  for(int k = 0; k < 10; ++k) { // 10 words per package
359  o << std::hex << std::setfill('0') << std::setw(4) << this->readRegister(ROCLib::ROC_Register::GTPTxRead) << " ";
360  }
361  o << std::endl;
362  }
363  __SET_ARG_OUT__("buffer", o.str());
364 }
365 
366 void ROCCosmicRayVetoInterface::SetLoopbackMode(__ARGS__) {
367  int16_t mode = __GET_ARG_IN__("loopback mode (Default: 0)", int16_t, 0);
368  this->writeRegister(ROCLib::ROC_Register::LoopbackMode, mode);
369 }
370 
371 DEFINE_OTS_INTERFACE(ROCCosmicRayVetoInterface)