00001 #include "otsdaq-components/DAQHardware/FSSRFirmwareBase.h"
00002
00003 #include "otsdaq-core/MessageFacility/MessageFacility.h"
00004 #include "otsdaq-core/Macros/CoutMacros.h"
00005
00006 #include "otsdaq-components/DAQHardware/FSSRFirmwareDefinitions.h"
00007 #include "otsdaq-core/BitManipulator/BitManipulator.h"
00008 #include "otsdaq-components/DetectorHardware/FSSRROCDefinitions.h"
00009
00010 #include <sys/socket.h>
00011 #include <netinet/in.h>
00012 #include <arpa/inet.h>
00013
00014 #include <iostream>
00015 #include <cstdlib>
00016
00017 #include "otsdaq-components/DAQHardware/FrontEndFirmwareBase.h"
00018 #include "otsdaq-components/DAQHardware/PurdueFirmwareCore.h"
00019 #include "otsdaq-components/DAQHardware/OtsUDPFirmwareCore.h"
00020
00021 using namespace ots;
00022
00023 const std::string FSSRFirmwareBase::PURDUE_FIRMWARE_NAME = "PurdueFSSRFirmware";
00024 const std::string FSSRFirmwareBase::OTS_FIRMWARE_NAME = "OtsUDPFSSRFirmware";
00025
00026
00027
00028 FSSRFirmwareBase::FSSRFirmwareBase(
00029 const std::string& communicationFirmwareType,
00030 unsigned int communicationFirmwareVersion,
00031 unsigned int version)
00032 : stripCSRRegisterValue_(0)
00033 , communicationFirmwareType_(communicationFirmwareType)
00034 {
00035
00036 if(communicationFirmwareType == FSSRFirmwareBase::PURDUE_FIRMWARE_NAME)
00037 communicationFirmwareInstance_ = new PurdueFirmwareCore(communicationFirmwareVersion);
00038 else if(communicationFirmwareType == FSSRFirmwareBase::OTS_FIRMWARE_NAME)
00039 communicationFirmwareInstance_ = new OtsUDPFirmwareCore(communicationFirmwareVersion);
00040 else
00041 {
00042 __SS__ << "Unknown communication firmware type choice: " <<
00043 communicationFirmwareType << std::endl;
00044 __COUT_ERR__ << ss.str();
00045 throw std::runtime_error(ss.str());
00046 }
00047
00048
00049
00050
00051
00052
00053 }
00054
00055
00056 FSSRFirmwareBase::~FSSRFirmwareBase(void)
00057 {
00058 delete communicationFirmwareInstance_;
00059 communicationFirmwareInstance_= NULL;
00060 }
00061
00062
00063 void FSSRFirmwareBase::init(void)
00064 {}
00065
00066
00067
00068 std::string FSSRFirmwareBase::universalRead(char* address)
00069 {
00070 __COUT__ << "universalRead communicationFirmwareType_ " << communicationFirmwareType_ << std::endl;
00071 return communicationFirmwareInstance_->read(address);
00072 }
00073
00074
00075 std::string FSSRFirmwareBase::universalWrite(char* address, char* data)
00076 {
00077 __COUT__ << "universalWrite communicationFirmwareType_ " << communicationFirmwareType_ << std::endl;
00078 return communicationFirmwareInstance_->write(address, data);
00079 }
00080
00081
00082 uint32_t FSSRFirmwareBase::createRegisterFromValue (std::string& readBuffer, std::string& receivedValue)
00083 {
00084 return communicationFirmwareInstance_->createRegisterFromValue(readBuffer,receivedValue);
00085 }
00086
00087
00088 std::string FSSRFirmwareBase::configureClocks(std::string source, double frequency)
00089 {
00090 std::cout << __COUT_HDR_FL__ << "Writing Clock configuration!" << std::endl;
00091
00092 std::string buffer;
00093
00094
00095 setPacketSizeStripCSR(6);
00096 setExternalBCOClockSourceStripCSR(source);
00097 communicationFirmwareInstance_->write(buffer, STRIP_CSR, stripCSRRegisterValue_);
00098
00099 resetDCM(buffer);
00100 alignReadOut(buffer,0x3000);
00101
00102 setFrequencyFromClockState(buffer, frequency);
00103 communicationFirmwareInstance_->write(buffer, STRIP_CSR, stripCSRRegisterValue_,false);
00104
00105 resetDCM(buffer);
00106 std::cout << __COUT_HDR_FL__ << "stripCSRRegisterValue :" << std::hex << stripCSRRegisterValue_ << std::dec << std::endl;
00107
00108 return buffer;
00109 }
00110
00111
00112 void FSSRFirmwareBase::resetDCM(std::string& buffer)
00113 {
00114 __COUT__ << communicationFirmwareInstance_ << std::endl;
00115 resetDCMStripCSR(true);
00116 __COUT__ << communicationFirmwareInstance_ << std::endl;
00117
00118 communicationFirmwareInstance_->write(buffer, STRIP_CSR, stripCSRRegisterValue_,false);
00119
00120 __COUT__ << communicationFirmwareInstance_ << std::endl;
00121
00122 resetDCMStripCSR(false);
00123 communicationFirmwareInstance_->write(buffer, STRIP_CSR, stripCSRRegisterValue_,false);
00124
00125 communicationFirmwareInstance_->waitClear(buffer, STRIP_CSR, waitDCMResetStripCSR(),false);
00126 }
00127
00128
00129
00130 void FSSRFirmwareBase::alignReadOut(std::string& buffer, uint32_t value)
00131 {
00132 communicationFirmwareInstance_->write(buffer, STRIP_TRIM_CSR, value);
00133 }
00134
00135 std::string FSSRFirmwareBase::resetDetector(int channel)
00136 {
00137 std::cout << __COUT_HDR_FL__ << "Resetting detector!" << std::endl;
00138 std::string buffer;
00139 if (channel == -1)
00140 {
00141
00142 communicationFirmwareInstance_->write(buffer, STRIP_RESET, 0xf000003f);
00143 communicationFirmwareInstance_->waitClear(buffer, STRIP_RESET, 0xf0000000);
00144 } else
00145 {
00146 communicationFirmwareInstance_->write(buffer, STRIP_RESET, 0xf000003f);
00147 communicationFirmwareInstance_->waitClear(buffer, STRIP_RESET, 0xf0000000);
00148 }
00149
00150 return buffer;
00151 }
00152
00153
00154 std::string FSSRFirmwareBase::enableTrigger(void)
00155 {
00156 std::string buffer;
00157 std::cout << __COUT_HDR_FL__ << "Enabling Trigger!!!" << std::endl;
00158 std::cout << __COUT_HDR_FL__ << "stripCSRRegisterValue in :" << std::hex << stripCSRRegisterValue_ << std::dec << std::endl;
00159
00160
00161 sendTriggerDataStripCSR(true);
00162 sendTriggerNumberStripCSR(true);
00163 sendBCOStripCSR(true);
00164 communicationFirmwareInstance_->write(buffer, STRIP_CSR, stripCSRRegisterValue_, false);
00165
00166 stripTriggerCSRRegisterValue_ = 0;
00167 BCOOffset(4);
00168 communicationFirmwareInstance_->write(buffer, STRIP_TRIG_CSR, stripTriggerCSRRegisterValue_, false);
00169
00170
00171
00172
00173 configureStripTriggerUnbiased(buffer);
00174
00175 configureTriggerInputs(buffer);
00176
00177
00178
00179 if (1 || communicationFirmwareInstance_->getVersion() == 1)
00180 communicationFirmwareInstance_->write(buffer, STRIP_TRIG_INPUT_3, 0x20401000, false);
00181 else if (communicationFirmwareInstance_->getVersion() == 2)
00182 communicationFirmwareInstance_->write(buffer, STRIP_TRIG_INPUT_3, 0x20301000, false);
00183 else
00184 {
00185 __SS__ << "what version is this?" << communicationFirmwareInstance_->getVersion() << std::endl;
00186 __COUT__ << ss.str();
00187 throw std::runtime_error(ss.str());
00188 }
00189 std::cout << __COUT_HDR_FL__ << "stripCSRRegisterValue out:" << std::hex << stripCSRRegisterValue_ << std::dec << std::endl;
00190 std::cout << __COUT_HDR_FL__ << "Done enabling Trigger!!!" << std::endl;
00191
00192 return buffer;
00193 }
00194
00196
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219 std::string FSSRFirmwareBase::resetBCO(void)
00220 {
00221 std::cout << __COUT_HDR_PL__ << "Reset BCO!!!" << std::endl;
00222 std::cout << __COUT_HDR_FL__ << "stripCSRRegisterValue in :" << std::hex << stripCSRRegisterValue_ << std::dec << std::endl;
00223 std::string buffer;
00224
00225 resetTriggerCounterStripCSR(buffer);
00226
00227 std::cout << __COUT_HDR_PL__ << "stripCSRRegisterValue :" << std::hex << stripCSRRegisterValue_ << std::dec << std::endl;
00228
00229
00230 communicationFirmwareInstance_->write(buffer,STRIP_SC_CSR,0x903f0b95, false);
00231
00232
00233
00234
00235 std::cout << __COUT_HDR_PL__ << "stripCSRRegisterValue out:" << std::hex << stripCSRRegisterValue_ << std::dec << std::endl;
00236 std::cout << __COUT_HDR_FL__ << "Done reset BCO!!!" << std::endl;
00237
00238 return buffer;
00239 }
00240
00241
00242 std::string FSSRFirmwareBase::armBCOReset(void)
00243 {
00244 std::cout << __COUT_HDR_FL__ << std::endl;
00245 std::cout << __COUT_HDR_PL__ << "stripCSRRegisterValue in :" << std::hex << stripCSRRegisterValue_ << std::dec << std::endl;
00246 std::string buffer;
00247
00248 enableBCOStripCSR(true);
00249 communicationFirmwareInstance_->write(buffer, STRIP_CSR, stripCSRRegisterValue_);
00250 std::cout << __COUT_HDR_PL__ << "stripCSRRegisterValue out:" << std::hex << stripCSRRegisterValue_ << std::dec << std::endl;
00251 std::cout << __COUT_HDR_FL__ << "Done!" << std::endl;
00252
00253 return buffer;
00254 }
00255
00256
00257 std::string FSSRFirmwareBase::startStream(bool channel0, bool channel1, bool channel2, bool channel3, bool channel4, bool channel5)
00258 {
00259 std::cout << __COUT_HDR_FL__ << "Start Stream!" << std::endl;
00260 std::cout << __COUT_HDR_PL__ << "stripCSRRegisterValue in:" << std::hex << stripCSRRegisterValue_ << std::dec << std::endl;
00261 std::string buffer;
00262
00263 std::cout << __COUT_HDR_PL__ << " channel0 " << channel0 << " channel1 " << channel1 << " channel2 " << channel2 << " channel3 " << channel3 << " channel4 " << channel4 << " channel5 " << channel5 << std::endl;
00264
00265 enableChannelsStripCSR(channel0, channel1, channel2, channel3, channel4, channel5);
00266
00267
00268
00269
00270
00271 enableStreamStripCSR(true);
00272 communicationFirmwareInstance_->write(buffer, STRIP_CSR, stripCSRRegisterValue_);
00273
00274 std::cout << __COUT_HDR_PL__ << "stripCSRRegisterValue out:" << std::hex << stripCSRRegisterValue_ << std::dec << std::endl;
00275 std::cout << __COUT_HDR_PL__ << "Done start Stream!" << std::endl;
00276
00277 return buffer;
00278 }
00279
00280
00281 std::string FSSRFirmwareBase::stopStream(void)
00282 {
00283 std::string buffer;
00284 enableChannelsStripCSR(false, false, false, false, false, false);
00285 enableStreamStripCSR(false);
00286 communicationFirmwareInstance_->write(buffer, STRIP_CSR, stripCSRRegisterValue_);
00287 return buffer;
00288 }
00289
00290
00291 void FSSRFirmwareBase::makeDACSequence(FirmwareSequence<uint64_t>& sequence, unsigned int channel, const ROCStream& rocStream)
00292 {
00293 const ROCDACs& rocDACs = rocStream.getROCDACs();
00294 for (DACList::const_iterator it = rocDACs.getDACList().begin(); it != rocDACs.getDACList().end(); it++)
00295 {
00296
00297 uint64_t data = FSSRROCDefinitions::makeDACWriteCommand(
00298 rocStream.getFEWROCAddress(), it->first, it->second.second);
00299 sequence.pushBack(ChannelFIFOAddress[channel], data);
00300 sequence.pushBack(ChannelFIFOAddress[channel],
00301 BitManipulator::insertBits(data, (uint64_t) 0x48, 56, 8));
00302
00303 sequence.pushBack(ChannelFIFOAddress[channel],
00304 BitManipulator::insertBits(data, (uint64_t) 1, 60, 1));
00305
00306 sequence.pushBack(ChannelFIFOAddress[channel],
00307 BitManipulator::insertBits(data, (uint64_t) 0, 60, 1));
00308
00309 sequence.pushBack(ChannelFIFOAddress[channel],
00310 BitManipulator::insertBits(data, (uint64_t) 0, 62, 1));
00311 sequence.pushBack(ChannelFIFOAddress[channel],
00312 BitManipulator::insertBits(data, (uint64_t) 0x40, 56, 8));
00313
00314 }
00315 }
00316
00317
00318 void FSSRFirmwareBase::makeDACSequence(FirmwareSequence<uint32_t>& sequence,
00319 unsigned int channel, const ROCStream& rocStream)
00320 {
00321 const ROCDACs& rocDACs = rocStream.getROCDACs();
00322 for (DACList::const_iterator it = rocDACs.getDACList().begin(); it
00323 != rocDACs.getDACList().end(); it++)
00324 {
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340 uint32_t data = FSSRROCDefinitions::makeDACWriteHeader(
00341 rocStream.getFEWROCAddress(), it->first);
00342
00343 BitManipulator::insertBits(data, 1, 16 + channel, 1);
00344 sequence.pushBack(ChannelFIFOAddress[channel], it->second.second);
00345 std::cout << __COUT_HDR_FL__ << "Register: " << it->first << " value: "
00346 << it->second.second << std::hex << " -> Data: " << data << std::dec
00347 << std::endl;
00348 sequence.pushBack(STRIP_SC_CSR, data);
00349 }
00350 }
00351
00352
00353 void FSSRFirmwareBase::makeDACBuffer(std::string& buffer, unsigned int channel, const ROCStream& rocStream)
00354 {
00355 std::cout << __COUT_HDR_FL__ << "Channel: " << channel << std::endl;
00356 std::cout << __COUT_HDR_FL__ << "BufferINsize: " << buffer.size() << std::endl;
00357 const ROCDACs& rocDACs = rocStream.getROCDACs();
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403 for (DACList::const_iterator it = rocDACs.getDACList().begin(); it != rocDACs.getDACList().end(); it++)
00404 {
00405 std::string bufferElement;
00406 communicationFirmwareInstance_->waitClear(bufferElement, STRIP_SC_CSR, 0x80000000, false);
00407 uint32_t registerHeader = 0;
00408
00409 if (it->first != "RejectHits" && it->first != "SendData")
00410 {
00411 communicationFirmwareInstance_->write(bufferElement, ChannelFIFOAddress[channel], it->second.second, false);
00412 registerHeader = FSSRROCDefinitions::makeDACWriteHeader(rocStream.getFEWROCAddress(), it->first);
00413
00414 BitManipulator::insertBits(registerHeader, 1, 16 + channel, 1);
00415 }
00416 else
00417 {
00418 if (it->second.second == 1 || it->second.second == 2)
00419 {
00420 registerHeader = FSSRROCDefinitions::makeDACSetHeader(rocStream.getFEWROCAddress(), it->first);
00421
00422 BitManipulator::insertBits(registerHeader, 1, 16 + channel, 1);
00423 }
00424 else if (it->second.second == 0 || it->second.second == 5)
00425 {
00426 registerHeader = FSSRROCDefinitions::makeDACResetHeader(rocStream.getFEWROCAddress(), it->first);
00427
00428 BitManipulator::insertBits(registerHeader, 1, 16 + channel, 1);
00429 }
00430 else
00431 std::cout << __COUT_HDR_FL__ << "Register value for : " << it->first
00432 << " doesn't have a value I expect -> value = "
00433 << it->second.second << std::endl;
00434
00435 }
00436
00437
00438 communicationFirmwareInstance_->write(bufferElement, STRIP_SC_CSR, registerHeader, false);
00439 communicationFirmwareInstance_->waitClear(bufferElement, STRIP_SC_CSR, 0x80000000, false);
00440
00441
00442 buffer += bufferElement;
00443
00444 }
00445 std::cout << __COUT_HDR_FL__ << "BufferOUTsize: " << buffer.size() << std::endl;
00446 }
00447
00448
00449 void FSSRFirmwareBase::makeDACBuffer(std::vector<std::string>& buffer, unsigned int channel, const ROCStream& rocStream)
00450 {
00451
00452 std::cout << __COUT_HDR_FL__ << "\tMaking DAC Buffer" << std::endl;
00453
00454 int limitCount = 0;
00455 unsigned int singleVectorCount = 0;
00456
00457 std::string alternateBuffer;
00458
00459 std::cout << __COUT_HDR_FL__ << "Channel: " << channel << std::endl;
00460 const ROCDACs& rocDACs = rocStream.getROCDACs();
00461
00462 std::string bufferElement;
00463
00464
00465 for (const std::pair<std::string, std::pair<unsigned int, unsigned int> >& it: rocDACs.getDACList())
00466
00467 {
00468
00469
00470
00471 communicationFirmwareInstance_->waitClear(bufferElement, STRIP_SC_CSR, 0x80000000, false);
00472 uint32_t registerHeader = 0;
00473
00474
00475 if (it.first != "RejectHits" && it.first != "SendData")
00476 {
00477
00478 communicationFirmwareInstance_->write(bufferElement, ChannelFIFOAddress[channel], it.second.second, false);
00479 registerHeader = FSSRROCDefinitions::makeDACWriteHeader(rocStream.getFEWROCAddress(), it.first);
00480
00481 BitManipulator::insertBits(registerHeader, 1, 16 + channel, 1);
00482 }
00483 else
00484 {
00485
00486 if (it.second.second == 1 || it.second.second == 2)
00487 {
00488 registerHeader = FSSRROCDefinitions::makeDACSetHeader(rocStream.getFEWROCAddress(), it.first);
00489
00490 BitManipulator::insertBits(registerHeader, 1, 16 + channel, 1);
00491 }
00492 else if (it.second.second == 0 || it.second.second == 5)
00493 {
00494 registerHeader = FSSRROCDefinitions::makeDACResetHeader(rocStream.getFEWROCAddress(), it.first);
00495
00496 BitManipulator::insertBits(registerHeader, 1, 16 + channel, 1);
00497 }
00498 else
00499 std::cout << __COUT_HDR_FL__ << "Register value for : " << it.first
00500 << " doesn't have a value I expect -> value = "
00501 << it.second.second << std::endl;
00502
00503 }
00504
00505 communicationFirmwareInstance_->write(bufferElement, STRIP_SC_CSR, registerHeader, false);
00506 communicationFirmwareInstance_->waitClear(bufferElement, STRIP_SC_CSR, 0x80000000, false);
00507
00508
00509
00510 limitCount++;
00511 singleVectorCount++;
00512
00513 if (limitCount == STIB_DAC_WRITE_MAX)
00514 {
00515 std::cout << __COUT_HDR_FL__ << "\tBuffer length:" << bufferElement.size() << std::endl;
00516 buffer.push_back(bufferElement);
00517 limitCount = 0;
00518 bufferElement.clear();
00519 }
00520 else if (singleVectorCount == rocDACs.getDACList().size())
00521 {
00522 buffer.push_back(bufferElement);
00523 }
00524
00525
00526
00527 }
00528 std::cout << __COUT_HDR_FL__ << "\tDone making DAC Buffer" << std::endl;
00529
00530 }
00531
00532
00533 void FSSRFirmwareBase::makeMaskBuffer(std::string& buffer, unsigned int channel,
00534 const ROCStream& rocStream)
00535 {
00536 std::cout << __COUT_HDR_FL__ << "Making mask! " << std::endl;
00537 makeMaskBuffer(buffer, channel, rocStream, "Kill");
00538
00539 }
00540
00541
00542 void FSSRFirmwareBase::makeMaskBuffer(std::string& buffer, unsigned int channel, const ROCStream& rocStream, const std::string& registerName)
00543 {
00544 int chipId = rocStream.getFEWROCAddress();
00545 const std::string& mask = rocStream.getROCMask();
00546 std::cout << __COUT_HDR_FL__ << "\tMaking mask! Length = " << mask.length() << std::endl;
00547
00548
00549
00550 unsigned int data[4] = { 0, 0, 0, 0 };
00551
00552 char val;
00553
00554 for (unsigned int d = 0; d < 4; d++)
00555 {
00556
00557 for (unsigned int m = 0; m < 8 * 4; m++)
00558 {
00559 val = mask[(8 * 4 * d) + m];
00560
00561
00562 data[d] |= (unsigned int) atoi(&val) << (8 * 4 - 1 - m);
00563
00564
00565
00566 }
00567
00568 }
00569
00570 int i;
00571 unsigned int w;
00572 unsigned char len = 4;
00573 unsigned char addr = 17;
00574 unsigned char bitMask = 1 << channel;
00575 unsigned char inst = WRITE;
00576
00577 communicationFirmwareInstance_->waitClear(buffer, STRIP_SC_CSR, 0x80000000, false);
00578
00579 for (i = 0; i < 4; i++)
00580
00581 communicationFirmwareInstance_->write(buffer, STRIP_SCI + 4 * (4 - i - 1), data[i], false);
00582
00583 w = 0x80000000 | (len << 24) | (bitMask << 16) | (inst << 10) | (addr << 5) | chipId;
00584
00585 communicationFirmwareInstance_->write(buffer, STRIP_SC_CSR, w, false);
00586
00587 communicationFirmwareInstance_->waitClear(buffer, STRIP_SC_CSR, 0x80000000, false);
00588 }
00589
00590
00591 void FSSRFirmwareBase::makeMaskSequence(FirmwareSequence<uint64_t>& sequence,
00592 unsigned int channel, const ROCStream& rocStream)
00593 {
00594 std::cout << __COUT_HDR_FL__ << "Making mask! " << std::endl;
00595 makeMaskSequence(sequence, channel, rocStream, "Kill");
00596
00597 }
00598
00599
00600 void FSSRFirmwareBase::makeMaskSequence(FirmwareSequence<uint32_t>& sequence,
00601 unsigned int channel, const ROCStream& rocStream)
00602 {
00603 std::cout << __COUT_HDR_FL__ << "Making mask! " << std::endl;
00604 makeMaskSequence(sequence, channel, rocStream, "Kill");
00605
00606 }
00607
00608
00609 void FSSRFirmwareBase::makeMaskSequence(FirmwareSequence<uint64_t>& sequence,
00610 unsigned int channel, const ROCStream& rocStream,
00611 const std::string& registerName)
00612 {
00613 int chipId = rocStream.getFEWROCAddress();
00614 std::string mask = rocStream.getROCMask();
00615 std::cout << __COUT_HDR_FL__ << "Mask length: " << mask.length() << std::endl;
00616
00617 uint64_t uInt64Data = 0;
00618 std::string stringData = "";
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636 BitManipulator::insertBits(uInt64Data, 0x1, 56, 8);
00637 sequence.pushBack(ChannelFIFOAddress[channel], uInt64Data);
00638
00639
00640 BitManipulator::insertBits(uInt64Data, 0x2, 56, 8);
00641 sequence.pushBack(ChannelFIFOAddress[channel], uInt64Data);
00642
00643
00644
00645
00646
00647 BitManipulator::insertBits(uInt64Data, 0x40, 16, 8);
00648 sequence.pushBack(ChannelFIFOAddress[channel], uInt64Data);
00649 BitManipulator::insertBits(uInt64Data, 0xc0, 16, 8);
00650 sequence.pushBack(ChannelFIFOAddress[channel], uInt64Data);
00651
00652 stringData = FSSRROCDefinitions::makeMaskWriteCommand(chipId,
00653 registerName, mask);
00654
00655 uint8_t data;
00656 for (unsigned int s = 0; s < stringData.length(); s++)
00657 for (int b = 8 - 1; b >= 0 && (s * 8 + 8 - b < 13 + 128); b--)
00658 {
00659
00660 data = 0x40 | (((stringData.data()[s] >> b) & 1) << 5);
00661 BitManipulator::insertBits(uInt64Data, (uint64_t) data, 16, 8);
00662 sequence.pushBack(ChannelFIFOAddress[channel], uInt64Data);
00663 data |= 0x80;
00664 BitManipulator::insertBits(uInt64Data, (uint64_t) data, 16, 8);
00665 sequence.pushBack(ChannelFIFOAddress[channel], uInt64Data);
00666 }
00667
00668
00669 BitManipulator::insertBits(uInt64Data, 0x0, 16, 8);
00670 sequence.pushBack(ChannelFIFOAddress[channel], uInt64Data);
00671 BitManipulator::insertBits(uInt64Data, 0x80, 16, 8);
00672 sequence.pushBack(ChannelFIFOAddress[channel], uInt64Data);
00673
00674
00675 BitManipulator::insertBits(uInt64Data, 0x0, 56, 8);
00676 sequence.pushBack(ChannelFIFOAddress[channel], uInt64Data);
00677
00678
00679 BitManipulator::insertBits(uInt64Data, 0x80, 56, 8);
00680 sequence.pushBack(ChannelFIFOAddress[channel], uInt64Data);
00681
00682 }
00683
00684
00685 void FSSRFirmwareBase::makeMaskSequence(FirmwareSequence<uint32_t>& sequence,
00686 unsigned int channel, const ROCStream& rocStream,
00687 const std::string& registerName)
00688 {}
00689
00690
00691 std::string FSSRFirmwareBase::readCSRRegister()
00692 {
00693 std::string buffer;
00694 std::cout << __COUT_HDR_FL__ << "FSSR readCSRRegister" << std::endl;
00695 communicationFirmwareInstance_->read(buffer,STRIP_CSR);
00696 return buffer;
00697 }
00698
00699
00700 std::string FSSRFirmwareBase::readSCCSRRegister()
00701 {
00702 std::string buffer;
00703 std::cout << __COUT_HDR_FL__ << "FSSR readCSRRegister" << std::endl;
00704 communicationFirmwareInstance_->read(buffer,STRIP_SC_CSR);
00705 return buffer;
00706 }
00707
00708
00709 void FSSRFirmwareBase::setFrequencyFromClockState(std::string& buffer, double frequency)
00710 {
00711 std::cout << __COUT_HDR_FL__ << "Setting up clock frequency!!!" << std::endl;
00712
00713 int quotient;
00714 int numerator;
00715 int denominator;
00716 double realClockFrequency;
00717
00718 if (BitManipulator::readBits(stripCSRRegisterValue_, 17, 1) == 1)
00719 quotient = 4;
00720 else
00721 quotient = 8;
00722
00723 if (isClockStateExternal())
00724 {
00725 realClockFrequency = EXTERNAL_CLOCK_FREQUENCY / quotient;
00726 }
00727 else
00728 {
00729 realClockFrequency = INTERNAL_CLOCK_FREQUENCY / quotient;
00730 }
00731
00732 double factor = frequency / realClockFrequency;
00733
00734
00735
00736
00737
00738 numerator = factor * 100;
00739 denominator = 100;
00740
00741 do
00742 {
00743
00744 int gcd = numerator;
00745 int rest = denominator;
00746 int tmp;
00747
00748 while (rest != 0)
00749 {
00750 tmp = rest;
00751 rest = gcd % tmp;
00752 gcd = tmp;
00753 }
00754
00755
00756 if (gcd == 1)
00757 {
00758 numerator /= 2;
00759 denominator /= 2;
00760 }
00761 else
00762 {
00763 numerator /= gcd;
00764 denominator /= gcd;
00765 }
00766
00767 }
00768 while (denominator >= 32 || numerator >= 32);
00769
00770
00771
00772 std::cout << __COUT_HDR_FL__ << "Numerator: " << numerator << std::endl;
00773 std::cout << __COUT_HDR_FL__ << "Denominator: " << denominator << std::endl;
00774 setFrequencyRatio(buffer, numerator, denominator);
00775 std::cout << __COUT_HDR_FL__ << "Done with clock frequency setup!!!" << std::endl;
00776 }
00777
00778 bool FSSRFirmwareBase::isClockStateExternal()
00779 {
00780 if (BitManipulator::readBits(stripCSRRegisterValue_, 16, 1) == 1)
00781 return true;
00782 else
00783 return false;
00784 }
00785
00786
00787 void FSSRFirmwareBase::setCSRRegister(uint32_t total)
00788 {
00789 stripCSRRegisterValue_ = total;
00790 }
00791
00792
00793 void FSSRFirmwareBase::setPacketSizeStripCSR(uint32_t size)
00794 {
00795 if (size > 31)
00796 std::cout << __COUT_HDR_FL__
00797 << "ERROR: Maximum packet size is 31 while here you are trying to set "
00798 << size << " packets!" << std::endl;
00799 BitManipulator::insertBits(stripCSRRegisterValue_, size, 3, 5);
00800
00801 }
00802
00803
00804 void FSSRFirmwareBase::enableChannelsStripCSR(bool channel0, bool channel1,
00805 bool channel2, bool channel3, bool channel4, bool channel5)
00806 {
00807 BitManipulator::insertBits(stripCSRRegisterValue_, ((uint32_t) channel0)
00808 + ((uint32_t) channel1 << 1) + ((uint32_t) channel2 << 2)
00809 + ((uint32_t) channel3 << 3) + ((uint32_t) channel4 << 4)
00810 + ((uint32_t) channel5 << 5), 8, 6);
00811 }
00812
00813
00814 void FSSRFirmwareBase::setExternalBCOClockSourceStripCSR(std::string clockSource)
00815 {
00816 if (clockSource == "External")
00817 BitManipulator::insertBits(stripCSRRegisterValue_, 1, 16, 1);
00818 else if (clockSource == "Internal")
00819 BitManipulator::insertBits(stripCSRRegisterValue_, 0, 16, 1);
00820 }
00821
00822
00823 void FSSRFirmwareBase::setHaltStripCSR(bool set)
00824 {
00825 BitManipulator::insertBits(stripCSRRegisterValue_, (uint32_t) set, 17, 1);
00826 }
00827
00828
00829 void FSSRFirmwareBase::enableBCOStripCSR(bool enable)
00830 {
00831 BitManipulator::insertBits(stripCSRRegisterValue_, (uint32_t) enable, 19, 1);
00832 }
00833
00834
00835 void FSSRFirmwareBase::flushBuffersStripCSR(void)
00836 {
00837 BitManipulator::insertBits(stripCSRRegisterValue_, 1, 20, 1);
00838 }
00839
00840
00841 void FSSRFirmwareBase::resetTriggerCounterStripCSR(std::string& buffer)
00842 {
00843
00844 BitManipulator::insertBits(stripCSRRegisterValue_, 1, 21, 1);
00845 communicationFirmwareInstance_->write(buffer, STRIP_CSR, stripCSRRegisterValue_, false);
00846
00847
00848
00849 }
00850
00851
00852 void FSSRFirmwareBase::resetBCOCounterStripCSR(void)
00853 {
00854 BitManipulator::insertBits(stripCSRRegisterValue_, 1, 22, 1);
00855 }
00856
00857
00858 void FSSRFirmwareBase::enableTriggerStripCSR(bool enable)
00859 {
00860 BitManipulator::insertBits(stripCSRRegisterValue_, (uint32_t) enable, 23, 1);
00861 }
00862
00863
00864 void FSSRFirmwareBase::sendTriggerDataStripCSR(bool send)
00865 {
00866 BitManipulator::insertBits(stripCSRRegisterValue_, (uint32_t) send, 24, 1);
00867 }
00868
00869
00870 void FSSRFirmwareBase::sendTriggerNumberStripCSR(bool send)
00871 {
00872 BitManipulator::insertBits(stripCSRRegisterValue_, (uint32_t) send, 25, 1);
00873 }
00874
00875
00876 void FSSRFirmwareBase::sendBCOStripCSR(bool send)
00877 {
00878 BitManipulator::insertBits(stripCSRRegisterValue_, (uint32_t) send, 26, 1);
00879 }
00880
00881
00882 void FSSRFirmwareBase::enableStreamStripCSR(bool enable)
00883 {
00884 BitManipulator::insertBits(stripCSRRegisterValue_, (uint32_t) enable, 27, 1);
00885 }
00886
00887
00888 void FSSRFirmwareBase::resetDCMStripCSR(bool clear)
00889 {
00890 BitManipulator::insertBits(stripCSRRegisterValue_, (uint32_t) clear, 31, 1);
00891 }
00892
00893
00894 uint32_t FSSRFirmwareBase::waitDCMResetStripCSR(void)
00895 {
00896 uint32_t bitToCheck = 0;
00897 BitManipulator::insertBits(bitToCheck, 1, 31, 2);
00898 return bitToCheck;
00899 }
00900
00901
00902 void FSSRFirmwareBase::resetDAC(void)
00903 {
00904 BitManipulator::insertBits(stripResetRegisterValue_, 1, 27, 1);
00905 }
00906
00907
00908 void FSSRFirmwareBase::resetLink(bool channel0, bool channel1, bool channel2,
00909 bool channel3, bool channel4, bool channel5)
00910 {
00911 stripResetRegisterValue_ = 0;
00912 BitManipulator::insertBits(stripResetRegisterValue_,
00913 ((uint32_t) channel0) + ((uint32_t) channel1 << 1)
00914 + ((uint32_t) channel2 << 2) + ((uint32_t) channel3 << 3)
00915 + ((uint32_t) channel4 << 4) + ((uint32_t) channel5 << 5),
00916 0, 6);
00917 BitManipulator::insertBits(stripResetRegisterValue_, 1, 29, 1);
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927
00928 }
00929
00930
00931 void FSSRFirmwareBase::clearErrors(bool channel0, bool channel1, bool channel2,
00932 bool channel3, bool channel4, bool channel5)
00933 {
00934 stripResetRegisterValue_ = 0;
00935 BitManipulator::insertBits(stripResetRegisterValue_,
00936 ((uint32_t) channel0) + ((uint32_t) channel1 << 1)
00937 + ((uint32_t) channel2 << 2) + ((uint32_t) channel3 << 3)
00938 + ((uint32_t) channel4 << 4) + ((uint32_t) channel5 << 5),
00939 0, 6);
00940 BitManipulator::insertBits(stripResetRegisterValue_, 1, 29, 1);
00941 }
00942
00943
00944 void FSSRFirmwareBase::clearFIFO(bool channel0, bool channel1, bool channel2,
00945 bool channel3, bool channel4, bool channel5)
00946 {
00947 stripResetRegisterValue_ = 0;
00948 BitManipulator::insertBits(stripResetRegisterValue_,
00949 ((uint32_t) channel0) + ((uint32_t) channel1 << 1)
00950 + ((uint32_t) channel2 << 2) + ((uint32_t) channel3 << 3)
00951 + ((uint32_t) channel4 << 4) + ((uint32_t) channel5 << 5),
00952 0, 6);
00953 BitManipulator::insertBits(stripResetRegisterValue_, 1, 30, 1);
00954 }
00955
00956
00957 void FSSRFirmwareBase::resetChip(bool channel0, bool channel1, bool channel2,
00958 bool channel3, bool channel4, bool channel5)
00959 {
00960 stripResetRegisterValue_ = 0;
00961 BitManipulator::insertBits(stripResetRegisterValue_,
00962 ((uint32_t) channel0) + ((uint32_t) channel1 << 1)
00963 + ((uint32_t) channel2 << 2) + ((uint32_t) channel3 << 3)
00964 + ((uint32_t) channel4 << 4) + ((uint32_t) channel5 << 5),
00965 0, 6);
00966 BitManipulator::insertBits(stripResetRegisterValue_, 1, 31, 1);
00967 }
00968
00969
00970 void FSSRFirmwareBase::setFrequencyRatio(std::string& buffer, int numerator, int denominator)
00971 {
00972
00973 communicationFirmwareInstance_->write(buffer, STRIP_BCO_DCM, 0x80500000 + (numerator - 1));
00974 communicationFirmwareInstance_->waitClear(buffer, STRIP_BCO_DCM, 0xf0000000);
00975
00976 communicationFirmwareInstance_->write(buffer, STRIP_BCO_DCM, 0x80520000 + (denominator - 1));
00977 communicationFirmwareInstance_->waitClear(buffer, STRIP_BCO_DCM, 0xf0000000);
00978 }
00979
00980
00981 void FSSRFirmwareBase::BCOOffset(uint32_t offset)
00982 {
00983 BitManipulator::insertBits(stripTriggerCSRRegisterValue_, offset, 0, 4);
00984 }
00985
00986
00987 void FSSRFirmwareBase::selectSpyFIFO(uint32_t input)
00988 {
00989 BitManipulator::insertBits(stripTriggerCSRRegisterValue_, input, 4, 3);
00990 }
00991
00992
00993 void FSSRFirmwareBase::halt(bool halt)
00994 {
00995 BitManipulator::insertBits(stripTriggerCSRRegisterValue_, (uint32_t) halt, 7, 1);
00996 }
00997
00998
00999 void FSSRFirmwareBase::configureStripTriggerUnbiased(std::string& buffer)
01000 {
01001 communicationFirmwareInstance_->write(buffer, STRIP_TRIG_UNBIASED, 0x002805c, false);
01002 }
01003
01004
01005 void FSSRFirmwareBase::configureTriggerInputs(std::string& buffer)
01006 {
01007
01008
01009 communicationFirmwareInstance_->write(buffer, STRIP_TRIG_INPUT_0, 0x0, false);
01010 communicationFirmwareInstance_->write(buffer, STRIP_TRIG_INPUT_1, 0x0, false);
01011 communicationFirmwareInstance_->write(buffer, STRIP_TRIG_INPUT_2, 0x0, false);
01012 }
01013
01014
01015 std::string FSSRFirmwareBase::resetSlaveBCO(void)
01016 {
01017 std::string buffer;
01018
01019 communicationFirmwareInstance_->write(buffer, (uint32_t)0xc5000000, (uint32_t)0x00000008);
01020 return buffer;
01021 }
01022
01023
01024
01025
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047
01048
01049
01050
01051
01052
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068
01069
01070
01071
01072
01073
01074
01075
01076
01077
01078
01079
01080
01081
01082
01083
01084
01085
01086
01087
01088
01089
01090
01091
01092
01093
01094
01095
01096
01097
01098
01099
01100
01101
01102
01103
01104
01105
01106
01107
01108
01109
01110
01111
01112
01113
01114
01115
01116
01117
01118
01119
01120
01121
01122
01123
01124
01125
01126
01127
01128
01129
01130