otsdaq_components  v2_04_02
FSSRFirmwareBase.cc
1 #include "otsdaq-components/DAQHardware/FSSRFirmwareBase.h"
2 
3 #include "otsdaq/Macros/CoutMacros.h"
4 #include "otsdaq/MessageFacility/MessageFacility.h"
5 
6 #include "otsdaq-components/DAQHardware/FSSRFirmwareDefinitions.h"
7 #include "otsdaq-components/DetectorHardware/FSSRROCDefinitions.h"
8 #include "otsdaq/BitManipulator/BitManipulator.h"
9 
10 #include <arpa/inet.h>
11 #include <netinet/in.h>
12 #include <sys/socket.h>
13 
14 #include <cstdlib>
15 #include <iostream>
16 
17 #include "otsdaq-components/DAQHardware/FrontEndFirmwareBase.h"
18 #include "otsdaq-components/DAQHardware/OtsUDPFirmwareCore.h"
19 #include "otsdaq-components/DAQHardware/PurdueFirmwareCore.h"
20 
21 using namespace ots;
22 
23 const std::string FSSRFirmwareBase::PURDUE_FIRMWARE_NAME = "PurdueFSSRFirmware";
24 const std::string FSSRFirmwareBase::OTS_FIRMWARE_NAME = "OtsUDPFSSRFirmware";
25 
26 //========================================================================================================================
27 FSSRFirmwareBase::FSSRFirmwareBase(const std::string& communicationFirmwareType,
28  unsigned int communicationFirmwareVersion,
29  unsigned int version)
30  : stripCSRRegisterValue_(0), communicationFirmwareType_(communicationFirmwareType)
31 {
32  // choose: OtsUDPFirmwareCore or PurdueFirmwareCore
33  if(communicationFirmwareType ==
34  FSSRFirmwareBase::PURDUE_FIRMWARE_NAME) // could also use string and: if(choice ==
35  // "purdue") etc.
36  communicationFirmwareInstance_ =
37  new PurdueFirmwareCore(communicationFirmwareVersion);
38  else if(communicationFirmwareType ==
39  FSSRFirmwareBase::OTS_FIRMWARE_NAME) // AUG-17-2017 RAR dissociated because
40  // function calls are entirely
41  // independent from PURDUE firmware
42  // calls
43  // //
44  communicationFirmwareInstance_ =
45  new OtsUDPFirmwareCore(communicationFirmwareVersion);
46  else
47  {
48  __SS__ << "Unknown communication firmware type choice: "
49  << communicationFirmwareType << std::endl;
50  __COUT_ERR__ << ss.str();
51  __SS_THROW__;
52  }
53 
54  // now we can call write/read etc with
55  // communicationFirmwareInstance_->write,
56  // communicationFirmwareInstance_->read, etc
57 }
58 
59 //========================================================================================================================
60 FSSRFirmwareBase::~FSSRFirmwareBase(void)
61 {
62  delete communicationFirmwareInstance_;
63  communicationFirmwareInstance_ = NULL;
64 }
65 
66 //========================================================================================================================
67 void FSSRFirmwareBase::init(void) {}
68 
69 //========================================================================================================================
70 std::string FSSRFirmwareBase::universalRead(char* address)
71 {
72  __COUT__ << "universalRead communicationFirmwareType_ " << communicationFirmwareType_
73  << std::endl;
74  return communicationFirmwareInstance_->read(address);
75 }
76 
77 //========================================================================================================================
78 std::string FSSRFirmwareBase::universalWrite(char* address, char* data)
79 {
80  __COUT__ << "universalWrite communicationFirmwareType_ " << communicationFirmwareType_
81  << std::endl;
82  return communicationFirmwareInstance_->write(address, data);
83 }
84 
85 //========================================================================================================================
86 uint32_t FSSRFirmwareBase::createRegisterFromValue(std::string& readBuffer,
87  std::string& receivedValue)
88 {
89  return communicationFirmwareInstance_->createRegisterFromValue(readBuffer,
90  receivedValue);
91 }
92 
93 //========================================================================================================================
94 std::string FSSRFirmwareBase::configureClocks(std::string source, double frequency)
95 {
96  // __COUT__ << "Writing Clock configuration!" << std::endl;
97 
98  std::string buffer;
99  // NoNeedNowwrite(buffer, ETHIO_DESTINATION_PORT, 0x0000b798); // Listen port for
100  // ethio stuff
101 
102  setPacketSizeStripCSR(6);
103  setExternalBCOClockSourceStripCSR(source); //(source)
104  communicationFirmwareInstance_->write(
105  buffer, STRIP_CSR, stripCSRRegisterValue_); // Reset CSR - reset trigger
106  // counter, external 27 MHz clock
107 
108  resetDCM(buffer);
109  // alignReadOut(buffer,0x3000);//0x3000
110 
111  setFrequencyFromClockState(buffer, frequency);
112  communicationFirmwareInstance_->write(
113  buffer, STRIP_CSR, stripCSRRegisterValue_, false /*clearBuffer*/);
114 
115  resetDCM(buffer);
116  // __COUT__ << "stripCSRRegisterValue :" << std::hex <<
117  // stripCSRRegisterValue_ << std::dec << std::endl;
118 
119  return buffer;
120 }
121 
122 //========================================================================================================================
123 void FSSRFirmwareBase::resetDCM(std::string& buffer)
124 {
125  // 31 DCM_RESET
126  resetDCMStripCSR(true); // Set bit 31
127  communicationFirmwareInstance_->write(buffer,
128  STRIP_CSR,
129  stripCSRRegisterValue_,
130  false /*clearBuffer*/); // Set reset to DCM
131 
132  resetDCMStripCSR(false); // unset bit 31
133  communicationFirmwareInstance_->write(buffer,
134  STRIP_CSR,
135  stripCSRRegisterValue_,
136  false /*clearBuffer*/); // Clear reset to DCM
137 
138  // 2018-10-24 Lorenzo in OtsUDPFirmware is doing nothing so I am commenting it out
139  // communicationFirmwareInstance_->waitClear(buffer, STRIP_CSR,
140  // waitDCMResetStripCSR(),false/*clearBuffer*/); // Wait for DCM to lock
141 }
142 
143 //========================================================================================================================
144 void FSSRFirmwareBase::alignReadOut(std::string& buffer,
145  unsigned int sensor,
146  unsigned int chip)
147 {
148  //(2:0) sensor
149  //(7:3) chip
150  // 28 up/down (-> here we always move up)
151  // 31 must be set 1
152  // It shifts the readout every time is called by few picosends (likely) if bit 31 is
153  // set
154  communicationFirmwareInstance_->write(
155  buffer,
156  STRIP_TRIM_CSR,
157  (0x9 << 28) + ((chip & 0xf) << 3) +
158  (sensor & 0x7)); // MCLKB edge for channel 5 // was 0x00002000
159 }
160 //========================================================================================================================
161 std::string FSSRFirmwareBase::resetDetector(int channel)
162 {
163  // __COUT__ << "Resetting detector!" << std::endl;
164  // Byte 4-------------------
165  // 31 CHIP_RESET
166  // 30 CLEAR_FIFO
167  // 29 CLEAR_ERRORS
168  // 28 LINK_RESET
170  // 27 DAC_RESET
171  // 26 unused
172  // 25 unused
173  // 24 unused
174  // Byte 3-------------------
175  // 23-16 unused
176  // Byte 2-------------------
177  // 15-8 unused
178  // Byte 1-------------------
179  // 7-0 CHANNEL_MASK ->Reset signals are sent to all channels with bits set in
180  // CHANNEL_MASK field
181 
182  std::string buffer;
183  if(channel == -1) // reset all channels
184  {
185  // write(buffer,STRIP_RESET,0xd000003f); // Issue reset
186  communicationFirmwareInstance_->write(
187  buffer,
188  STRIP_RESET,
189  0xf000003f); // Issue reset // was 0xf000003f
190  // 2018-10-24 Lorenzo in OtsUDPFirmware is doing nothing so I am
191  // commenting it out
192  // communicationFirmwareInstance_->waitClear(buffer,
193  // STRIP_RESET, 0xf0000000); // Wait for reset to complete //
194  // was 0xf0000000
195  }
196  else
197  {
198  communicationFirmwareInstance_->write(
199  buffer,
200  STRIP_RESET,
201  0xf000003f); // Issue reset
202  // 2018-10-24 Lorenzo in OtsUDPFirmware is doing nothing so I am
203  // commenting it out
204  // communicationFirmwareInstance_->waitClear(buffer,
205  // STRIP_RESET, 0xf0000000); // Wait for reset to complete
206  }
207 
208  return buffer;
209 }
210 
211 //========================================================================================================================
212 std::string FSSRFirmwareBase::enableTrigger(void)
213 {
214  std::string buffer;
215  // __COUT__ << "Enabling Trigger!!!" << std::endl;
216  // __COUT__ << "stripCSRRegisterValue in :" << std::hex <<
217  // stripCSRRegisterValue_ << std::dec << std::endl;
218 
219  // setHaltStripCSR(1);//WARNING THIS IS CLEARED BY THE MASTER BUT IF THERE IS NO
220  // MASTER NOTHING WORKS UNLESS THE BIT IS UNSET
221  sendTriggerDataStripCSR(false); // STRIP_CSR -> Bit 24 -> SEND_TRIG
222  communicationFirmwareInstance_->write(
223  buffer, STRIP_CSR, stripCSRRegisterValue_, false /*clearBuffer*/);
224  resetTriggerCounterStripCSR(buffer);
225  sendTriggerDataStripCSR(true); // STRIP_CSR -> Bit 24 -> SEND_TRIG
226  sendTriggerNumberStripCSR(true); // STRIP_CSR -> Bit 25 -> SEND_TRIGNUM
227  sendBCOStripCSR(true); // STRIP_CSR -> Bit 26 -> SEND_BCO
228  communicationFirmwareInstance_->write(
229  buffer, STRIP_CSR, stripCSRRegisterValue_, false /*clearBuffer*/);
230 
231  stripTriggerCSRRegisterValue_ = 0;
232  BCOOffset(4);
233  communicationFirmwareInstance_->write(
234  buffer,
235  STRIP_TRIG_CSR,
236  stripTriggerCSRRegisterValue_,
237  false /*clearBuffer*/); // BCO offset // was 0x00000004
238 
239  // write(buffer,STRIP_TRIG_INPUT_0,0x1f060040); // FSSR GOTHIT trigger input -
240  // timed in for the 27 MHz external clock
241  // write(buffer,STRIP_TRIG_INPUT_3,0x3f874040); // Unbiased trigger input +
242  // external trigger
243 
244  configureStripTriggerUnbiased(buffer);
245 
246  configureTriggerInputs(buffer);
247 
248  // FIXME for IP .36 the number to set is 0x20401000
249 
250  // if (1 || communicationFirmwareInstance_->getVersion() == 1)
251  communicationFirmwareInstance_->write(
252  buffer, STRIP_TRIG_INPUT_3, 0x20401000, false /*clearBuffer*/);
253  // else if (communicationFirmwareInstance_->getVersion() == 2)
254  // communicationFirmwareInstance_->write(buffer, STRIP_TRIG_INPUT_3, 0x20301000,
255  // false/*clearBuffer*/); else
256  //{
257  // __SS__ << "what version is this?" << communicationFirmwareInstance_->getVersion()
258  //<< std::endl;
259  // __COUT__ << ss.str();
260  // __SS_THROW__;
261  //}
262  // __COUT__ << "stripCSRRegisterValue out:" << std::hex <<
263  // stripCSRRegisterValue_ << std::dec << std::endl; __COUT__ <<
264  // "Done enabling Trigger!!!" << std::endl;
265 
266  return buffer;
267 }
268 //
270 // void FSSRFirmwareBase::setDataDestination(std::string& buffer, const std::string& ip,
271 // const uint16_t port)
273 //{
274 // __COUT__ << "Set data destination!" << std::endl;
275 //
276 // struct sockaddr_in socketAddress;
277 // inet_pton(AF_INET, ip.c_str(), &(socketAddress.sin_addr));
278 // __COUT__ << "ADDRESS: " << std::hex <<
279 // ntohl(socketAddress.sin_addr.s_addr) << std::dec << std::endl;
280 // communicationFirmwareInstance_->write(buffer, DATA_DESTINATION_IP,
281 // ntohl(socketAddress.sin_addr.s_addr)); // Set data destination IP 192.168.133.1
282 // __COUT__ << "PORT: " << std::hex << port << std::dec <<
283 // std::endl; communicationFirmwareInstance_->write(buffer,
284 // DATA_SOURCE_DESTINATION_PORT, port); // Set data destination port std::cout <<
285 // __COUT_HDR_FL__ << "THIS IS THE BUFFER: " << buffer << std::endl;
286 //
287 // for(uint32_t i=0; i<buffer.size(); i++)
288 // printf("%2.2X-",(uint8_t)buffer[i]);
289 // std::cout << std::dec << std::endl;
290 //
291 //}
292 
293 //========================================================================================================================
294 std::string FSSRFirmwareBase::resetBCO(void)
295 {
296  __COUT__ << "Reset BCO!!!" << std::endl;
297  __COUT__ << "stripCSRRegisterValue in :" << std::hex << stripCSRRegisterValue_
298  << std::dec << std::endl;
299  std::string buffer;
300 
301  // resetTriggerCounterStripCSR(buffer);
302  // write(buffer, STRIP_CSR, stripCSRRegisterValue_);//the write is done in the reset
303  // __COUT__ << "stripCSRRegisterValue :" << std::hex <<
304  // stripCSRRegisterValue_ << std::dec << std::endl;
305 
306  // msg->Write(STRIP_SC_CSR,0x90000b95|(chmask<<16));
307 
308  // Byte 4-------------------
309  // 31 SC_BUSY
310  // 30 RAW
311  // 29 BCO_SYNC
312  // 28 BCO_ZERO
314  // 27 unused
315  // 26-24 LENGTH
316  // Byte 3-------------------
317  // 23-16 CHANNEL_MASK
318  // Byte 2-------------------
319  // 15-13 READ_SELECT
320  // 12-10 INSTRUCTION
321  // 9-5 ADDRESS
322  // Byte 1-------------------
323  // 9-5 ADDRESS
324  // 4-0 CHIP_ID
325  // 0x 9 0 3 f 0 b 9 5
326  // 1001-0000-0011-1111-0000-1011-1001-0101
327  // CHIP_ID 1-0101 -> 21 wild chip id ->all chips will get the command
328  // ADDRESS 11-100 -> 28
329  // INSTRUCTION 010 -> 2
330  // READ_SELECT 000 -> 0
331  // CHANNEL_MASK 0011-1111 -> all Channels
332  // LENGTH 000 -> 0
333  // unused
334  // BCO_SYNC 1
335  // BCO_ZERO 0
336  // RAW 0
337  // SC_BUSY 1
338  // When BCO SYNC is set the transaction is delayed until the internal BCO counter has
339  // a value of 240. This allow the AqBCO command to be synchronized
340  communicationFirmwareInstance_->write(
341  buffer, STRIP_SC_CSR, 0x903f0b95, false /*clearBuffer*/); // This is the
342  // <SCR,set> command
343  // with the bit set to
344  // sync SHIFT with
345  // BCO=0.
346 
347  // enableBCOStripCSR(true);
348  // communicationFirmwareInstance_->write(buffer, STRIP_CSR, stripCSRRegisterValue_);
349 
350  __COUT__ << "stripCSRRegisterValue out:" << std::hex << stripCSRRegisterValue_
351  << std::dec << std::endl;
352  __COUT__ << "Done reset BCO!!!" << std::endl;
353 
354  return buffer;
355 }
356 
357 //========================================================================================================================
358 std::string FSSRFirmwareBase::armBCOReset(void)
359 {
360  std::string buffer;
361  armBCOResetCSR();
362  communicationFirmwareInstance_->write(buffer, STRIP_CSR, stripCSRRegisterValue_);
363  return buffer;
364 }
365 
366 //========================================================================================================================
367 std::string FSSRFirmwareBase::startStream(bool channel0,
368  bool channel1,
369  bool channel2,
370  bool channel3,
371  bool channel4,
372  bool channel5)
373 {
374  // __COUT__ << "Start Stream!" << std::endl;
375  // __COUT__ << "stripCSRRegisterValue in:" << std::hex <<
376  // stripCSRRegisterValue_ << std::dec << std::endl;
377  std::string buffer;
378 
379  // __COUT__ << " channel0 " << channel0 << " channel1 " <<
380  // channel1 << " channel2 " << channel2 << " channel3 " << channel3 << " channel4 " <<
381  // channel4 << " channel5 " << channel5 << std::endl;
382 
383  enableChannelsStripCSR(channel0, channel1, channel2, channel3, channel4, channel5);
384  // if (version_ == 1)
385  // enableChannelsStripCSR(true, true, true, true, false, false);
386  // else if (version_ == 2)
387  // enableChannelsStripCSR(true, true, true, true, true, true);
388 
389  enableStreamStripCSR(
390  true); // Turn on streaming hits along with BCO data // was 0x0f000f30
391  communicationFirmwareInstance_->write(buffer, STRIP_CSR, stripCSRRegisterValue_);
392 
393  // __COUT__ << "stripCSRRegisterValue out:" << std::hex <<
394  // stripCSRRegisterValue_ << std::dec << std::endl; __COUT__
395  // << "Done start Stream!" << std::endl;
396 
397  return buffer;
398 }
399 
400 //========================================================================================================================
401 std::string FSSRFirmwareBase::stopStream(void)
402 {
403  std::string buffer;
404  enableStreamStripCSR(false);
405  enableChannelsStripCSR(false, false, false, false, false, false);
406  communicationFirmwareInstance_->write(buffer, STRIP_CSR, stripCSRRegisterValue_);
407  return buffer;
408 }
409 
410 //========================================================================================================================
411 void FSSRFirmwareBase::makeDACSequence(FirmwareSequence<uint64_t>& sequence,
412  unsigned int channel,
413  const ROCStream& rocStream)
414 {
415  const ROCDACs& rocDACs = rocStream.getROCDACs();
416  for(DACList::const_iterator it = rocDACs.getDACList().begin();
417  it != rocDACs.getDACList().end();
418  it++)
419  {
420  // if(it->first != "SendData" && it->first != "RejectHits") continue;
421  uint64_t data = FSSRROCDefinitions::makeDACWriteCommand(
422  rocStream.getFEWROCAddress(), it->first, it->second.second);
423  sequence.pushBack(ChannelFIFOAddress[channel], data);
424  sequence.pushBack(ChannelFIFOAddress[channel],
425  BitManipulator::insertBits(data, (uint64_t)0x48, 56, 8));
426  // set WRITE
427  sequence.pushBack(ChannelFIFOAddress[channel],
428  BitManipulator::insertBits(data, (uint64_t)1, 60, 1));
429  // clr WRITE
430  sequence.pushBack(ChannelFIFOAddress[channel],
431  BitManipulator::insertBits(data, (uint64_t)0, 60, 1));
432  // clr TALK
433  sequence.pushBack(ChannelFIFOAddress[channel],
434  BitManipulator::insertBits(data, (uint64_t)0, 62, 1));
435  sequence.pushBack(ChannelFIFOAddress[channel],
436  BitManipulator::insertBits(data, (uint64_t)0x40, 56, 8));
437  // break;
438  }
439 }
440 
441 //========================================================================================================================
442 void FSSRFirmwareBase::makeDACSequence(FirmwareSequence<uint32_t>& sequence,
443  unsigned int channel,
444  const ROCStream& rocStream)
445 {
446  const ROCDACs& rocDACs = rocStream.getROCDACs();
447  for(DACList::const_iterator it = rocDACs.getDACList().begin();
448  it != rocDACs.getDACList().end();
449  it++)
450  {
451  /*RYAN
452  //if(it->first != "SendData" && it->first != "RejectHits") continue;
453  uint64_t data =
454  FSSRROCDefinitions::makeDACWriteCommand(rocStream.getFEWROCAddress(), it->first,
455  it->second.second); sequence.pushBack(ChannelFIFOAddress[channel], data);
456  sequence.pushBack(ChannelFIFOAddress[channel],
457  BitManipulator::insertBits(data,(uint32_t)0x48,56,8));
458  //set WRITE
459  sequence.pushBack(ChannelFIFOAddress[channel],
460  BitManipulator::insertBits(data,(uint32_t)1,60,1));
461  //clr WRITE
462  sequence.pushBack(ChannelFIFOAddress[channel],
463  BitManipulator::insertBits(data,(uint32_t)0,60,1));
464  //clr TALK
465  sequence.pushBack(ChannelFIFOAddress[channel],
466  BitManipulator::insertBits(data,(uint32_t)0,62,1));
467  sequence.pushBack(ChannelFIFOAddress[channel],
468  BitManipulator::insertBits(data,(uint32_t)0x40,56,8));
469  */
470 
471  // if(it->first != "SendData" && it->first != "RejectHits") continue;
472  uint32_t data = FSSRROCDefinitions::makeDACWriteHeader(
473  rocStream.getFEWROCAddress(), it->first);
474  // Insert channel
475  BitManipulator::insertBits(data, 1, 16 + channel, 1);
476  sequence.pushBack(ChannelFIFOAddress[channel], it->second.second);
477  __COUT__ << "Register: " << it->first << " value: " << it->second.second
478  << std::hex << " -> Data: " << data << std::dec << std::endl;
479  sequence.pushBack(STRIP_SC_CSR, data);
480  }
481 }
482 
483 //========================================================================================================================
484 void FSSRFirmwareBase::makeDACBuffer(std::string& buffer,
485  unsigned int channel,
486  const ROCStream& rocStream)
487 {
488  __COUT__ << "Channel: " << channel << std::endl;
489  __COUT__ << "BufferINsize: " << buffer.size() << std::endl;
490  const ROCDACs& rocDACs = rocStream.getROCDACs();
491  // for (DACList::const_iterator it = rocDACs.getDACList().begin(); it !=
492  // rocDACs.getDACList().end(); it++)
493  // {
494  // std::string bufferElement;
495  // communicationFirmwareInstance_->waitClear(bufferElement, STRIP_SC_CSR,
496  // 0x80000000); uint32_t registerHeader = 0;
497  // //FIXME This must go in the FSSRROCDefinitions stuff
498  // //if (it->first != "RejectHits" && it->first != "SendData")
499  // if (it->first == "VTp0")
500  // {
501  // //communicationFirmwareInstance_->write(bufferElement,
502  // ChannelFIFOAddress[channel], it->second.second); registerHeader =
503  // FSSRROCDefinitions::makeDACReadHeader(rocStream.getFEWROCAddress(), it->first);
504  // //Insert channel
505  // BitManipulator::insertBits(registerHeader, 1, 16 + channel, 1);
525  //
526  // __COUT__ << "Register: " << it->first << " value: " <<
527  // it->second.second << std::hex << " -> Data: " << registerHeader << std::dec <<
528  // std::endl; communicationFirmwareInstance_->write(bufferElement, STRIP_SC_CSR,
529  // registerHeader); communicationFirmwareInstance_->waitClear(bufferElement,
530  // STRIP_SC_CSR, 0x80000000);
531  //
532  // //buffer.push_back(bufferElement);
533  // buffer += bufferElement;
534  // }
535  // //break;
536  // }
537  for(DACList::const_iterator it = rocDACs.getDACList().begin();
538  it != rocDACs.getDACList().end();
539  it++)
540  {
541  std::string bufferElement;
542  communicationFirmwareInstance_->waitClear(
543  bufferElement, STRIP_SC_CSR, 0x80000000, false);
544  uint32_t registerHeader = 0;
545  // FIXME This must go in the FSSRROCDefinitions stuff
546  if(it->first != "RejectHits" && it->first != "SendData")
547  {
548  communicationFirmwareInstance_->write(
549  bufferElement, ChannelFIFOAddress[channel], it->second.second, false);
550  registerHeader = FSSRROCDefinitions::makeDACWriteHeader(
551  rocStream.getFEWROCAddress(), it->first);
552  // Insert channel
553  BitManipulator::insertBits(registerHeader, 1, 16 + channel, 1);
554  }
555  else
556  {
557  if(it->second.second == 1 || it->second.second == 2)
558  {
559  registerHeader = FSSRROCDefinitions::makeDACSetHeader(
560  rocStream.getFEWROCAddress(), it->first);
561  // Insert channel
562  BitManipulator::insertBits(registerHeader, 1, 16 + channel, 1);
563  }
564  else if(it->second.second == 0 || it->second.second == 5)
565  {
566  registerHeader = FSSRROCDefinitions::makeDACResetHeader(
567  rocStream.getFEWROCAddress(), it->first);
568  // Insert channel
569  BitManipulator::insertBits(registerHeader, 1, 16 + channel, 1);
570  }
571  else
572  __COUT__ << "Register value for : " << it->first
573  << " doesn't have a value I expect -> value = "
574  << it->second.second << std::endl;
575  }
576 
577  // __COUT__ << "Register: " << it->first << " value: " <<
578  // it->second.second << std::hex << " -> Data: " << registerHeader << std::dec <<
579  // std::endl;
580  communicationFirmwareInstance_->write(
581  bufferElement, STRIP_SC_CSR, registerHeader, false);
582  communicationFirmwareInstance_->waitClear(
583  bufferElement, STRIP_SC_CSR, 0x80000000, false);
584 
585  // buffer.push_back(bufferElement);
586  buffer += bufferElement;
587  // break;
588  }
589  __COUT__ << "BufferOUTsize: " << buffer.size() << std::endl;
590 }
591 
592 //========================================================================================================================
593 void FSSRFirmwareBase::makeDACBuffer(std::vector<std::string>& buffer,
594  unsigned int channel,
595  const ROCStream& rocStream)
596 {
597  // __COUT__ << "\tMaking DAC Buffer" << std::endl;
598 
599  int limitCount = 0;
600  unsigned int singleVectorCount = 0;
601 
602  std::string alternateBuffer;
603 
604  // __COUT__ << "Channel: " << channel << std::endl;
605  const ROCDACs& rocDACs = rocStream.getROCDACs();
606  // __COUT__ << "Number of DACs: " << rocDACs.getDACList().size()
607  // << std::endl;
608  std::string bufferElement;
609  // FIXME My
610  // for (const auto it: rocDACs.getDACList())
611  for(const std::pair<std::string, std::pair<unsigned int, unsigned int> >& it :
612  rocDACs.getDACList())
613  // for (std::map<std::string, std::pair<unsigned int, unsigned int> >::const_iterator
614  // it = rocDACs.getDACList().begin(); it != rocDACs.getDACList().end(); it++)
615  {
616  // __COUT__ << "Register?" << std::endl;
617  // if (it.first != "ActiveLines") continue;
618  // __COUT__ << "Register-1: " << it->first << " val: " <<
619  // it->second.first << " = " << it->second.second << std::endl;
620  communicationFirmwareInstance_->waitClear(
621  bufferElement, STRIP_SC_CSR, 0x80000000, false);
622  uint32_t registerHeader = 0;
623  // FIXME This must go in the FSSRROCDefinitions stuff
624  // __COUT__ << "Register0: " << it.first << std::endl;
625  if(it.first != "RejectHits" && it.first != "SendData")
626  {
627  // __COUT__ << "Register1: " << it.first << "Channel: " <<
628  // channel << " fifo address: " << ChannelFIFOAddress[channel] << std::endl;
629  communicationFirmwareInstance_->write(
630  bufferElement, ChannelFIFOAddress[channel], it.second.second, false);
631  registerHeader = FSSRROCDefinitions::makeDACWriteHeader(
632  rocStream.getFEWROCAddress(), it.first);
633  // Insert channel
634  BitManipulator::insertBits(registerHeader, 1, 16 + channel, 1);
635  }
636  else
637  {
638  // __COUT__ << "Register2: " << it.first << std::endl;
639  if(it.second.second == 1 || it.second.second == 2)
640  {
641  registerHeader = FSSRROCDefinitions::makeDACSetHeader(
642  rocStream.getFEWROCAddress(), it.first);
643  // Insert channel
644  BitManipulator::insertBits(registerHeader, 1, 16 + channel, 1);
645  }
646  else if(it.second.second == 0 || it.second.second == 5)
647  {
648  registerHeader = FSSRROCDefinitions::makeDACResetHeader(
649  rocStream.getFEWROCAddress(), it.first);
650  // Insert channel
651  BitManipulator::insertBits(registerHeader, 1, 16 + channel, 1);
652  }
653  else
654  __COUT__ << "Register value for : " << it.first
655  << " doesn't have a value I expect -> value = "
656  << it.second.second << std::endl;
657  }
658  // __COUT__ << "Register: " << it.first << " value: " <<
659  // it.second.second << std::hex << " -> Data: " << registerHeader << std::dec <<
660  // std::endl;
661  communicationFirmwareInstance_->write(
662  bufferElement, STRIP_SC_CSR, registerHeader, false);
663  communicationFirmwareInstance_->waitClear(
664  bufferElement, STRIP_SC_CSR, 0x80000000, false);
665 
666  // __COUT__ << "Register3: " << it.first << std::endl;
667  // alternateBuffer += bufferElement;
668  limitCount++;
669  singleVectorCount++;
670 
671  if(limitCount == STIB_DAC_WRITE_MAX)
672  {
673  __COUT__ << "\tBuffer length:" << bufferElement.size() << std::endl;
674  buffer.push_back(bufferElement);
675  limitCount = 0;
676  bufferElement.clear();
677  }
678  else if(singleVectorCount ==
679  rocDACs.getDACList().size()) // case for incomplete packet
680  {
681  buffer.push_back(bufferElement);
682  }
683 
684  // buffer.push_back(bufferElement);
685  // break;
686  }
687  // __COUT__ << "\tDone making DAC Buffer" << std::endl;
688 }
689 
690 //========================================================================================================================
691 void FSSRFirmwareBase::makeMaskBuffer(std::string& buffer,
692  unsigned int channel,
693  const ROCStream& rocStream)
694 {
695  // __COUT__ << "Making mask! " << std::endl;
696  makeMaskBuffer(buffer, channel, rocStream, "Kill");
697  // makeMaskSequence(buffer, channel, rocStream, "Inject");
698 }
699 
700 //========================================================================================================================
701 void FSSRFirmwareBase::makeMaskBuffer(std::string& buffer,
702  unsigned int channel,
703  const ROCStream& rocStream,
704  const std::string& registerName)
705 {
706  int chipId = rocStream.getFEWROCAddress();
707  const std::string& mask = rocStream.getROCMask();
708  // __COUT__ << "\tMaking mask! Length = " << mask.length() <<
709  // std::endl; __COUT__ << "\tMask length: " << mask.length() <<
710  // std::endl; __COUT__ << "\tMask: " << mask << std::endl;
711 
712  unsigned int data[4] = {0, 0, 0, 0};
713 
714  char val;
715  // int j = 0;
716  for(unsigned int d = 0; d < 4; d++)
717  { // d goes from 0 to 4. 4 bytes
718  // __COUT__ << "---------------------" << d <<
719  // "-------------------" << std::endl;
720  for(unsigned int m = 0; m < 8 * 4; m++)
721  { // m goes from 0 to 31, since each byte has 8 bits, there are 32 bits
722  val = mask[(8 * 4 * d) + m]; // assigns to val the value of the corresponding
723  // bit. 0-31, 32-63, 64-95, 96-127. it goes
724  // through each of the 128 bits
725  // __COUT__ << "---------------------" << j++ <<
726  // std::endl; __COUT__ << "data[" << d << "] before: " <<
727  // std::hex << data[d] << std::dec << std::endl;
728  data[d] |= (unsigned int)atoi(&val) << (8 * 4 - 1 - m);
729  // __COUT__ << "(unsigned int) atoi(&val): " << (unsigned
730  // int) atoi(&val) << std::endl; __COUT__ << "data[" << d
731  // << "] after: " << std::hex << data[d] << std::dec << std::endl; std::cout
732  // <<
733  // __COUT_HDR_FL__ << std::hex << "D: " << data[d] << " Val: " << (unsigned
734  // int)atoi(&val) << " index: " << std::dec << (8*4-1-m) << " bit: " <<
735  // mask[(8*4*d)+m] << std::dec << std::endl;
736  }
737  //
738  }
739 
740  int i;
741  unsigned int w;
742  unsigned char len = 4;
743  unsigned char addr = 17; // Kill
744  unsigned char bitMask = 1 << channel;
745  unsigned char inst = WRITE;
746 
747  communicationFirmwareInstance_->waitClear(buffer, STRIP_SC_CSR, 0x80000000, false);
748 
749  for(i = 0; i < 4; i++)
750  // write(buffer, STRIP_SCI + 4 * i, data[i]);
751  communicationFirmwareInstance_->write(
752  buffer, STRIP_SCI + 4 * (4 - i - 1), data[i], false);
753 
754  w = 0x80000000 | (len << 24) | (bitMask << 16) | (inst << 10) | (addr << 5) | chipId;
755 
756  communicationFirmwareInstance_->write(buffer, STRIP_SC_CSR, w, false);
757 
758  communicationFirmwareInstance_->waitClear(buffer, STRIP_SC_CSR, 0x80000000, false);
759 }
760 
761 //========================================================================================================================
762 void FSSRFirmwareBase::makeMaskSequence(FirmwareSequence<uint64_t>& sequence,
763  unsigned int channel,
764  const ROCStream& rocStream)
765 {
766  __COUT__ << "Making mask! " << std::endl;
767  makeMaskSequence(sequence, channel, rocStream, "Kill");
768  // makeMaskSequence(sequence, channel, rocStream, "Inject");
769 }
770 
771 //========================================================================================================================
772 void FSSRFirmwareBase::makeMaskSequence(FirmwareSequence<uint32_t>& sequence,
773  unsigned int channel,
774  const ROCStream& rocStream)
775 {
776  __COUT__ << "Making mask! " << std::endl;
777  makeMaskSequence(sequence, channel, rocStream, "Kill");
778  // makeMaskSequence(channel,rocStream,sequence,"Inject");
779 }
780 
781 //========================================================================================================================
782 void FSSRFirmwareBase::makeMaskSequence(FirmwareSequence<uint64_t>& sequence,
783  unsigned int channel,
784  const ROCStream& rocStream,
785  const std::string& registerName)
786 {
787  int chipId = rocStream.getFEWROCAddress(); // 9, 14 or 21 bcast
788  std::string mask = rocStream.getROCMask();
789  __COUT__ << "Mask length: " << mask.length() << std::endl;
790 
791  uint64_t uInt64Data = 0;
792  std::string stringData = "";
793 
794  // have to manually set every bit for mask writes.
795  // reset fifo
796  // download every bit (shift_in and shift_ctrl) to fifo (setup muxes and control
797  // lines) configure muxes for readout commence readout
798 
799  // FIFO Controls - byte 5 (7:0)
800  // 7 - FIFO Clock
801  // 6 - Shift Ctrl bit
802  // MASK_CELL_H - Shift In bit
803  // MUX Controls - byte 0 (7:0)
804  // 7 - Read En/Output MUX sel (1 for masks)
805  // 1 - Write En for mux
806  // 0 - Reset Fifo
807 
808  // reset fifo
809  BitManipulator::insertBits(uInt64Data, 0x1, 56, 8);
810  sequence.pushBack(ChannelFIFOAddress[channel], uInt64Data);
811 
812  // configure fifo for write
813  BitManipulator::insertBits(uInt64Data, 0x2, 56, 8);
814  sequence.pushBack(ChannelFIFOAddress[channel], uInt64Data);
815 
816  // Download every bit (shift_in and shift_control) to fifo (setup muxes and control
817  // lines) Bit 7 of data is FIFO clock, bit 6 is shift_control, bit 5 is shift_in
818  // start bits (ctrl 0, data 0 => ctrl 1, data 0)
819 
820  BitManipulator::insertBits(uInt64Data, 0x40, 16, 8); //(0<<7) | (1<<6) | (0<<5)
821  sequence.pushBack(ChannelFIFOAddress[channel], uInt64Data);
822  BitManipulator::insertBits(
823  uInt64Data,
824  0xc0,
825  16,
826  8); //(1<<7) | (1<<6) | (0<<5) -> clock the data in the fifo
827  sequence.pushBack(ChannelFIFOAddress[channel], uInt64Data);
828 
829  stringData = FSSRROCDefinitions::makeMaskWriteCommand(chipId, registerName, mask);
830 
831  uint8_t data;
832  for(unsigned int s = 0; s < stringData.length(); s++)
833  for(int b = 8 - 1; b >= 0 && (s * 8 + 8 - b < 13 + 128); b--)
834  {
835  // __COUT__ << "S: " << s << " val: " <<
836  // stringData.data()[s] << " b: " << b << " bit: " <<
837  // ((stringData.data()[s]>>b)&1) << std::endl;
838  data = 0x40 | (((stringData.data()[s] >> b) & 1) << 5);
839  BitManipulator::insertBits(uInt64Data, (uint64_t)data, 16, 8);
840  sequence.pushBack(ChannelFIFOAddress[channel], uInt64Data);
841  data |= 0x80;
842  BitManipulator::insertBits(uInt64Data, (uint64_t)data, 16, 8);
843  sequence.pushBack(ChannelFIFOAddress[channel], uInt64Data);
844  }
845 
846  // reset Shift Control
847  BitManipulator::insertBits(uInt64Data, 0x0, 16, 8);
848  sequence.pushBack(ChannelFIFOAddress[channel], uInt64Data);
849  BitManipulator::insertBits(uInt64Data, 0x80, 16, 8);
850  sequence.pushBack(ChannelFIFOAddress[channel], uInt64Data);
851 
852  // configure muxes for readout
853  BitManipulator::insertBits(uInt64Data, 0x0, 56, 8);
854  sequence.pushBack(ChannelFIFOAddress[channel], uInt64Data);
855 
856  // commence readout
857  BitManipulator::insertBits(uInt64Data, 0x80, 56, 8);
858  sequence.pushBack(ChannelFIFOAddress[channel], uInt64Data);
859 }
860 
861 //========================================================================================================================
862 void FSSRFirmwareBase::makeMaskSequence(FirmwareSequence<uint32_t>& sequence,
863  unsigned int channel,
864  const ROCStream& rocStream,
865  const std::string& registerName)
866 {
867 }
868 
869 //========================================================================================================================
870 std::string FSSRFirmwareBase::readCSRRegister()
871 {
872  std::string buffer;
873  // __COUT__ << "FSSR readCSRRegister" << std::endl;
874  communicationFirmwareInstance_->read(buffer, STRIP_CSR);
875  return buffer;
876 }
877 
878 //========================================================================================================================
879 std::string FSSRFirmwareBase::readSCCSRRegister()
880 {
881  std::string buffer;
882  __COUT__ << "FSSR readCSRRegister" << std::endl;
883  communicationFirmwareInstance_->read(buffer, STRIP_SC_CSR);
884  return buffer;
885 }
886 
887 //========================================================================================================================
888 void FSSRFirmwareBase::setFrequencyFromClockState(std::string& buffer, double frequency)
889 {
890  // __COUT__ << "Setting up clock frequency!!!" << std::endl;
891 
892  int quotient;
893  int numerator;
894  int denominator;
895  double realClockFrequency;
896 
897  if(BitManipulator::readBits(stripCSRRegisterValue_, 17, 1) ==
898  1) // if fastBCO is enabled
899  quotient = 4;
900  else // normal cases
901  quotient = 8;
902 
903  if(isClockStateExternal()) // base freq: 54MHz
904  {
905  realClockFrequency =
906  EXTERNAL_CLOCK_FREQUENCY / quotient; // this is the REAL frequency being used
907  }
908  else // base freq: 66.667MHz
909  {
910  realClockFrequency =
911  INTERNAL_CLOCK_FREQUENCY / quotient; // this is the REAL frequency being used
912  }
913 
914  double factor = frequency / realClockFrequency;
915 
916  // The device needs the denominator and the denominator to be load into a 5 bit
917  // register It will take two initial numerator and denominator bigger than necessary
918  // (to do not loose precision) and divide them for their gcd. If they still do not
919  // fit in 5 bit, they are trunked (divided by 2) untill they are less than 32
920  numerator =
921  factor * 100; // we will work with 2 digits precision after the decimal point
922  denominator = 100;
923 
924  do
925  {
926  // We will need the GCD at some point in order to simplify fractions //taken from
927  // other sources
928  int gcd = numerator;
929  int rest = denominator;
930  int tmp;
931 
932  while(rest != 0)
933  {
934  tmp = rest;
935  rest = gcd % tmp;
936  gcd = tmp;
937  }
938  // Done finding the GCD
939 
940  if(gcd == 1) // if there's no GCD, just divide by 2 to find the nearest
941  // approximation with less bits
942  {
943  numerator /= 2;
944  denominator /= 2;
945  }
946  else
947  {
948  numerator /= gcd;
949  denominator /= gcd;
950  }
951 
952  } while(denominator >= 32 || numerator >= 32);
953 
954  // numerator = 2;
955  // denominator = 1;
956  // __COUT__ << "Numerator: " << numerator << std::endl;
957  // __COUT__ << "Denominator: " << denominator << std::endl;
958  setFrequencyRatio(buffer, numerator, denominator);
959  // __COUT__ << "Done with clock frequency setup!!!" << std::endl;
960 }
961 //========================================================================================================================
962 bool FSSRFirmwareBase::isClockStateExternal() // returns true if the clock state is
963  // External
964 {
965  if(BitManipulator::readBits(stripCSRRegisterValue_, 16, 1) == 1)
966  return true;
967  else
968  return false;
969 }
970 
971 //========================================================================================================================
972 void FSSRFirmwareBase::setCSRRegister(uint32_t total) { stripCSRRegisterValue_ = total; }
973 
974 //========================================================================================================================
975 void FSSRFirmwareBase::setPacketSizeStripCSR(uint32_t size)
976 {
977  if(size > 31)
978  __COUT__ << "ERROR: Maximum packet size is 31 while here you are trying to set "
979  << size << " packets!" << std::endl;
980  BitManipulator::insertBits(stripCSRRegisterValue_, size, 3, 5);
981  // write(buffer,STRIP_CSR, stripSCRRegisterValue_);
982 }
983 
984 //========================================================================================================================
985 void FSSRFirmwareBase::enableChannelsStripCSR(bool channel0,
986  bool channel1,
987  bool channel2,
988  bool channel3,
989  bool channel4,
990  bool channel5)
991 {
992  BitManipulator::insertBits(stripCSRRegisterValue_,
993  ((uint32_t)channel0) + ((uint32_t)channel1 << 1) +
994  ((uint32_t)channel2 << 2) + ((uint32_t)channel3 << 3) +
995  ((uint32_t)channel4 << 4) + ((uint32_t)channel5 << 5),
996  8,
997  6);
998 }
999 
1000 //========================================================================================================================
1001 void FSSRFirmwareBase::setExternalBCOClockSourceStripCSR(std::string clockSource)
1002 {
1003  if(clockSource == "External")
1004  BitManipulator::insertBits(stripCSRRegisterValue_, 1, 16, 1);
1005  else if(clockSource == "Internal")
1006  BitManipulator::insertBits(stripCSRRegisterValue_, 0, 16, 1);
1007 }
1008 
1009 //========================================================================================================================
1010 // void FSSRFirmwareBase::setHaltStripCSR(bool set)
1011 //{
1012 // BitManipulator::insertBits(stripCSRRegisterValue_, (uint32_t) set, 17, 1);
1013 //}
1014 
1015 //========================================================================================================================
1016 void FSSRFirmwareBase::armBCOResetCSR(void)
1017 {
1018  BitManipulator::insertBits(stripCSRRegisterValue_, 1, 19, 1);
1019 }
1020 
1021 //========================================================================================================================
1022 void FSSRFirmwareBase::flushBuffersStripCSR(void)
1023 {
1024  BitManipulator::insertBits(stripCSRRegisterValue_, 1, 20, 1);
1025 }
1026 
1027 //========================================================================================================================
1028 void FSSRFirmwareBase::resetTriggerCounterStripCSR(std::string& buffer)
1029 {
1030  // Ryan's firmware is too fast so I need to make sure he understand the 1!!!!
1031  BitManipulator::insertBits(stripCSRRegisterValue_, 1, 21, 1);
1032  communicationFirmwareInstance_->write(
1033  buffer, STRIP_CSR, stripCSRRegisterValue_, false /*clearBuffer*/);
1034  BitManipulator::insertBits(stripCSRRegisterValue_, 1, 21, 1);
1035  communicationFirmwareInstance_->write(
1036  buffer, STRIP_CSR, stripCSRRegisterValue_, false /*clearBuffer*/);
1037  BitManipulator::insertBits(stripCSRRegisterValue_, 1, 21, 1);
1038  communicationFirmwareInstance_->write(
1039  buffer, STRIP_CSR, stripCSRRegisterValue_, false /*clearBuffer*/);
1040  BitManipulator::insertBits(stripCSRRegisterValue_, 1, 21, 1);
1041  communicationFirmwareInstance_->write(
1042  buffer, STRIP_CSR, stripCSRRegisterValue_, false /*clearBuffer*/);
1043  BitManipulator::insertBits(stripCSRRegisterValue_, 1, 21, 1);
1044  communicationFirmwareInstance_->write(
1045  buffer, STRIP_CSR, stripCSRRegisterValue_, false /*clearBuffer*/);
1046 
1047  BitManipulator::insertBits(stripCSRRegisterValue_, 0, 21, 1);
1048  communicationFirmwareInstance_->write(
1049  buffer, STRIP_CSR, stripCSRRegisterValue_, false /*clearBuffer*/);
1050 }
1051 
1052 //========================================================================================================================
1053 void FSSRFirmwareBase::resetBCOCounterStripCSR(void)
1054 {
1055  BitManipulator::insertBits(stripCSRRegisterValue_, 1, 22, 1);
1056 }
1057 
1058 //========================================================================================================================
1059 void FSSRFirmwareBase::enableTriggerStripCSR(bool enable)
1060 {
1061  BitManipulator::insertBits(stripCSRRegisterValue_, (uint32_t)enable, 23, 1);
1062 }
1063 
1064 //========================================================================================================================
1065 void FSSRFirmwareBase::sendTriggerDataStripCSR(bool send)
1066 {
1067  BitManipulator::insertBits(stripCSRRegisterValue_, (uint32_t)send, 24, 1);
1068 }
1069 
1070 //========================================================================================================================
1071 void FSSRFirmwareBase::sendTriggerNumberStripCSR(bool send)
1072 {
1073  BitManipulator::insertBits(stripCSRRegisterValue_, (uint32_t)send, 25, 1);
1074 }
1075 
1076 //========================================================================================================================
1077 void FSSRFirmwareBase::sendBCOStripCSR(bool send)
1078 {
1079  BitManipulator::insertBits(stripCSRRegisterValue_, (uint32_t)send, 26, 1);
1080 }
1081 
1082 //========================================================================================================================
1083 void FSSRFirmwareBase::enableStreamStripCSR(bool enable)
1084 {
1085  BitManipulator::insertBits(stripCSRRegisterValue_, (uint32_t)enable, 27, 1);
1086 }
1087 
1088 //========================================================================================================================
1089 void FSSRFirmwareBase::resetDCMStripCSR(bool clear)
1090 {
1091  BitManipulator::insertBits(stripCSRRegisterValue_, (uint32_t)clear, 31, 1);
1092 }
1093 
1094 //========================================================================================================================
1095 uint32_t FSSRFirmwareBase::waitDCMResetStripCSR(void)
1096 {
1097  uint32_t bitToCheck = 0;
1098  BitManipulator::insertBits(bitToCheck, 1, 31, 2);
1099  return bitToCheck;
1100 }
1101 
1102 //========================================================================================================================
1103 void FSSRFirmwareBase::resetDAC(void)
1104 {
1105  BitManipulator::insertBits(stripResetRegisterValue_, 1, 27, 1);
1106 }
1107 
1108 //========================================================================================================================
1109 void FSSRFirmwareBase::resetLink(bool channel0,
1110  bool channel1,
1111  bool channel2,
1112  bool channel3,
1113  bool channel4,
1114  bool channel5)
1115 {
1116  stripResetRegisterValue_ = 0;
1117  BitManipulator::insertBits(stripResetRegisterValue_,
1118  ((uint32_t)channel0) + ((uint32_t)channel1 << 1) +
1119  ((uint32_t)channel2 << 2) + ((uint32_t)channel3 << 3) +
1120  ((uint32_t)channel4 << 4) + ((uint32_t)channel5 << 5),
1121  0,
1122  6);
1123  BitManipulator::insertBits(stripResetRegisterValue_, 1, 29, 1);
1124  /*
1125  write(buffer, STRIP_RESET, (1<<28) +
1126  ((uint32_t)channel0) +
1127  ((uint32_t)channel1<<1) +
1128  ((uint32_t)channel2<<2) +
1129  ((uint32_t)channel3<<3) +
1130  ((uint32_t)channel4<<4) +
1131  ((uint32_t)channel5<<5)
1132  );
1133  */
1134 }
1135 
1136 //========================================================================================================================
1137 void FSSRFirmwareBase::clearErrors(bool channel0,
1138  bool channel1,
1139  bool channel2,
1140  bool channel3,
1141  bool channel4,
1142  bool channel5)
1143 {
1144  stripResetRegisterValue_ = 0;
1145  BitManipulator::insertBits(stripResetRegisterValue_,
1146  ((uint32_t)channel0) + ((uint32_t)channel1 << 1) +
1147  ((uint32_t)channel2 << 2) + ((uint32_t)channel3 << 3) +
1148  ((uint32_t)channel4 << 4) + ((uint32_t)channel5 << 5),
1149  0,
1150  6);
1151  BitManipulator::insertBits(stripResetRegisterValue_, 1, 29, 1);
1152 }
1153 
1154 //========================================================================================================================
1155 void FSSRFirmwareBase::clearFIFO(bool channel0,
1156  bool channel1,
1157  bool channel2,
1158  bool channel3,
1159  bool channel4,
1160  bool channel5)
1161 {
1162  stripResetRegisterValue_ = 0;
1163  BitManipulator::insertBits(stripResetRegisterValue_,
1164  ((uint32_t)channel0) + ((uint32_t)channel1 << 1) +
1165  ((uint32_t)channel2 << 2) + ((uint32_t)channel3 << 3) +
1166  ((uint32_t)channel4 << 4) + ((uint32_t)channel5 << 5),
1167  0,
1168  6);
1169  BitManipulator::insertBits(stripResetRegisterValue_, 1, 30, 1);
1170 }
1171 
1172 //========================================================================================================================
1173 void FSSRFirmwareBase::resetChip(bool channel0,
1174  bool channel1,
1175  bool channel2,
1176  bool channel3,
1177  bool channel4,
1178  bool channel5)
1179 {
1180  stripResetRegisterValue_ = 0;
1181  BitManipulator::insertBits(stripResetRegisterValue_,
1182  ((uint32_t)channel0) + ((uint32_t)channel1 << 1) +
1183  ((uint32_t)channel2 << 2) + ((uint32_t)channel3 << 3) +
1184  ((uint32_t)channel4 << 4) + ((uint32_t)channel5 << 5),
1185  0,
1186  6);
1187  BitManipulator::insertBits(stripResetRegisterValue_, 1, 31, 1);
1188 }
1189 
1190 //========================================================================================================================
1191 void FSSRFirmwareBase::setFrequencyRatio(std::string& buffer,
1192  int numerator,
1193  int denominator)
1194 {
1195  // The device need to load numerator minus one and denominator minus one, with an
1196  // internal address of 0x50 and 052 respectively
1197  communicationFirmwareInstance_->write(
1198  buffer,
1199  STRIP_BCO_DCM,
1200  0x80500000 + (numerator - 1)); // Set BCOCLK numerator // was 0x80500003
1201  communicationFirmwareInstance_->waitClear(
1202  buffer,
1203  STRIP_BCO_DCM,
1204  0xf0000000); // Wait DCM write to finish // was 0x80000000
1205 
1206  communicationFirmwareInstance_->write(
1207  buffer,
1208  STRIP_BCO_DCM,
1209  0x80520000 + (denominator - 1)); // Set BCOCLK denominator // was 0x80520001
1210  communicationFirmwareInstance_->waitClear(
1211  buffer, STRIP_BCO_DCM, 0xf0000000); // Wait DCM write to finish - BCO frequency
1212  // should be 13.513 MHz // was 0x80000000
1213 }
1214 
1215 //========================================================================================================================
1216 void FSSRFirmwareBase::BCOOffset(uint32_t offset)
1217 {
1218  BitManipulator::insertBits(stripTriggerCSRRegisterValue_, offset, 0, 4);
1219 }
1220 
1221 //========================================================================================================================
1222 void FSSRFirmwareBase::selectSpyFIFO(uint32_t input)
1223 {
1224  BitManipulator::insertBits(stripTriggerCSRRegisterValue_, input, 4, 3);
1225 }
1226 
1227 //========================================================================================================================
1228 void FSSRFirmwareBase::halt(bool halt)
1229 {
1230  BitManipulator::insertBits(stripTriggerCSRRegisterValue_, (uint32_t)halt, 7, 1);
1231 }
1232 
1233 //========================================================================================================================
1234 void FSSRFirmwareBase::configureStripTriggerUnbiased(std::string& buffer)
1235 {
1236  communicationFirmwareInstance_->write(
1237  buffer,
1238  STRIP_TRIG_UNBIASED,
1239  0x002805c,
1240  false /*clearBuffer*/); // Configure unbiased trigger
1241 }
1242 
1243 //========================================================================================================================
1244 void FSSRFirmwareBase::configureTriggerInputs(std::string& buffer)
1245 {
1246  // communicationFirmwareInstance_->write(buffer, STRIP_TRIG_INPUT_0, 0x3f440000); //
1247  // FSSR GOTHIT trigger input channel 0,1
1248  // communicationFirmwareInstance_->write(buffer, STRIP_TRIG_INPUT_1, 0x3f440000); //
1249  // FSSR GOTHIT trigger input channel 2,3
1250  communicationFirmwareInstance_->write(
1251  buffer,
1252  STRIP_TRIG_INPUT_0,
1253  0x0,
1254  false /*clearBuffer*/); // FSSR GOTHIT trigger input channel 0,1
1255  communicationFirmwareInstance_->write(
1256  buffer,
1257  STRIP_TRIG_INPUT_1,
1258  0x0,
1259  false /*clearBuffer*/); // FSSR GOTHIT trigger input channel 2,3
1260  communicationFirmwareInstance_->write(
1261  buffer,
1262  STRIP_TRIG_INPUT_2,
1263  0x0,
1264  false /*clearBuffer*/); // FSSR GOTHIT trigger input channel 4,5
1265 }
1266 
1267 //========================================================================================================================
1268 std::string FSSRFirmwareBase::resetSlaveBCO(void)
1269 {
1270  std::string buffer;
1271  /*TODO:make unambiguous by casting to uint32_t*/
1272  communicationFirmwareInstance_->write(
1273  buffer, (uint32_t)0xc5000000, (uint32_t)0x00000008);
1274  return buffer;
1275 }
1276 
1277 /*
1278  //========================================================================================================================
1279  void FSSRFirmwareBase::chipID(uint32_t size)
1280  {
1281  BitManipulator::insertBits(stripSCRegisterValue_, size, 0, 5);
1282  }
1283 
1284  //========================================================================================================================
1285  void FSSRFirmwareBase::addressSlowControls(uint32_t size)
1286  {
1287  BitManipulator::insertBits(stripSCRegisterValue_, size, 5, 5);
1288  }
1289 
1290  //========================================================================================================================
1291  void FSSRFirmwareBase::instructionSlowControls(uint32_t size)
1292  {
1293  BitManipulator::insertBits(stripSCRegisterValue_, size, 10, 3);
1294  }
1295 
1296  //========================================================================================================================
1297  void FSSRFirmwareBase::channelreadSelect(uint32_t size)
1298  {
1299  BitManipulator::insertBits(stripSCRegisterValue_, size, 13, 3);
1300  }
1301 
1302  //========================================================================================================================
1303  void FSSRFirmwareBase::channelMask(uint32_t size)
1304  {
1305  BitManipulator::insertBits(stripSCRegisterValue_, size, 16, 8);
1306  }
1307 
1308  //========================================================================================================================
1309  void FSSRFirmwareBase::bitsLength(uint32_t length)
1310  {
1311  BitManipulator::insertBits(stripSCRegisterValue_, length, 26, 3);
1312  }
1313 
1314 
1315  //========================================================================================================================
1316  void FSSRFirmwareBase::syncFallingBCO(bool sync)
1317  {
1318  BitManipulator::insertBits(stripSCRegisterValue_, (uint32_t)sync, 28, 1);
1319  }
1320 
1321  //========================================================================================================================
1322  void FSSRFirmwareBase::syncRisingBCO(bool sync)
1323  {
1324  BitManipulator::insertBits(stripSCRegisterValue_, (uint32_t)sync, 29, 1);
1325  }
1326 
1327  //========================================================================================================================
1328  void FSSRFirmwareBase::setRaw(bool set)
1329  {
1330  BitManipulator::insertBits(stripSCRegisterValue_, (uint32_t)set, 30, 1);
1331  }
1332 
1333  //========================================================================================================================
1334  void FSSRFirmwareBase::initSlowControls(bool init)
1335  {
1336  BitManipulator::insertBits(stripSCRegisterValue_, (uint32_t)init, 31, 1);
1337  }
1338 
1339  //========================================================================================================================
1340  void FSSRFirmwareBase::resetCount(bool reset)
1341  {
1342  BitManipulator::insertBits(stripAnalysisCSRRegisterValue_, (uint32_t)reset, 30, 1);
1343  }
1344 
1345  //========================================================================================================================
1346  void FSSRFirmwareBase::setBCO_0(uint32_t void FSSRFirmwareBase::BCOOffset(uint32_t
1347  offset)
1348  {
1349  BitManipulator::insertBits(stripTrigCSRRegisterValue_, offset, 0, 4);
1350  }input)
1351  {
1352  BitManipulator::insertBits(trigInputRegisterValue_, input, 0, 8);
1353  }
1354 
1355  //========================================================================================================================
1356  void FSSRFirmwareBase::setBCO_1(uint32_t input)
1357  {
1358  BitManipulator::insertBits(trigInputRegisterValue_, input, 8, 8);
1359  }
1360 
1361  //========================================================================================================================
1362  void FSSRFirmwareBase::trimFracBCO_0(uint32_t input)
1363  {
1364  BitManipulator::insertBits(trigInputRegisterValue_, input, 16, 4);
1365  }
1366 
1367  //========================================================================================================================
1368  void FSSRFirmwareBase::trimFracBCO_1(uint32_t input)
1369  {
1370  BitManipulator::insertBits(trigInputRegisterValue_, input, 20, 4);
1371  }
1372 
1373  //========================================================================================================================
1374  void FSSRFirmwareBase::enable_0(bool enable)
1375  {
1376  BitManipulator::insertBits(trigInputRegisterValue_, (uint32_t)enable, 28, 1);
1377  }
1378 
1379  //========================================================================================================================
1380  void FSSRFirmwareBase::enable_1(bool enable)
1381  {
1382  BitManipulator::insertBits(trigInputRegisterValue_, (uint32_t)enable, 29, 1);
1383  }
1384 
1385  */
void makeDACBuffer(std::string &buffer, unsigned int channel, const ROCStream &rocStream)