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