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 __SS_THROW__;
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
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
00101
00102 setFrequencyFromClockState(buffer, frequency);
00103 communicationFirmwareInstance_->write(buffer, STRIP_CSR, stripCSRRegisterValue_,false);
00104
00105 resetDCM(buffer);
00106
00107
00108 return buffer;
00109 }
00110
00111
00112 void FSSRFirmwareBase::resetDCM(std::string& buffer)
00113 {
00114
00115 resetDCMStripCSR(true);
00116 communicationFirmwareInstance_->write(buffer, STRIP_CSR, stripCSRRegisterValue_,false);
00117
00118 resetDCMStripCSR(false);
00119 communicationFirmwareInstance_->write(buffer, STRIP_CSR, stripCSRRegisterValue_,false);
00120
00121
00122
00123 }
00124
00125
00126
00127 void FSSRFirmwareBase::alignReadOut(std::string& buffer, unsigned int sensor, unsigned int chip)
00128 {
00129
00130
00131
00132
00133
00134 communicationFirmwareInstance_->write(buffer, STRIP_TRIM_CSR, (0x9<<28)+((chip&0xf)<<3)+(sensor&0x7));
00135 }
00136
00137 std::string FSSRFirmwareBase::resetDetector(int channel)
00138 {
00139
00140
00141
00142
00143
00144
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158 std::string buffer;
00159 if (channel == -1)
00160 {
00161
00162 communicationFirmwareInstance_->write(buffer, STRIP_RESET, 0xf000003f);
00163
00164
00165 } else
00166 {
00167 communicationFirmwareInstance_->write(buffer, STRIP_RESET, 0xf000003f);
00168
00169
00170 }
00171
00172 return buffer;
00173 }
00174
00175
00176 std::string FSSRFirmwareBase::enableTrigger(void)
00177 {
00178 std::string buffer;
00179
00180
00181
00182
00183 sendTriggerDataStripCSR (false);
00184 communicationFirmwareInstance_->write(buffer, STRIP_CSR, stripCSRRegisterValue_, false);
00185 resetTriggerCounterStripCSR(buffer);
00186 sendTriggerDataStripCSR (true);
00187 sendTriggerNumberStripCSR(true);
00188 sendBCOStripCSR (true);
00189 communicationFirmwareInstance_->write(buffer, STRIP_CSR, stripCSRRegisterValue_, false);
00190
00191 stripTriggerCSRRegisterValue_ = 0;
00192 BCOOffset(4);
00193 communicationFirmwareInstance_->write(buffer, STRIP_TRIG_CSR, stripTriggerCSRRegisterValue_, false);
00194
00195
00196
00197
00198 configureStripTriggerUnbiased(buffer);
00199
00200 configureTriggerInputs(buffer);
00201
00202
00203
00204
00205 communicationFirmwareInstance_->write(buffer, STRIP_TRIG_INPUT_3, 0x20401000, false);
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217 return buffer;
00218 }
00219
00221
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244 std::string FSSRFirmwareBase::resetBCO(void)
00245 {
00246 std::cout << __COUT_HDR_PL__ << "Reset BCO!!!" << std::endl;
00247 std::cout << __COUT_HDR_FL__ << "stripCSRRegisterValue in :" << std::hex << stripCSRRegisterValue_ << std::dec << std::endl;
00248 std::string buffer;
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287 communicationFirmwareInstance_->write(buffer, STRIP_SC_CSR, 0x903f0b95, false);
00288
00289
00290
00291
00292 std::cout << __COUT_HDR_PL__ << "stripCSRRegisterValue out:" << std::hex << stripCSRRegisterValue_ << std::dec << std::endl;
00293 std::cout << __COUT_HDR_FL__ << "Done reset BCO!!!" << std::endl;
00294
00295 return buffer;
00296 }
00297
00298
00299 std::string FSSRFirmwareBase::armBCOReset(void)
00300 {
00301 std::string buffer;
00302 armBCOResetCSR();
00303 communicationFirmwareInstance_->write(buffer, STRIP_CSR, stripCSRRegisterValue_);
00304 return buffer;
00305 }
00306
00307
00308 std::string FSSRFirmwareBase::startStream(bool channel0, bool channel1, bool channel2, bool channel3, bool channel4, bool channel5)
00309 {
00310
00311
00312 std::string buffer;
00313
00314
00315
00316 enableChannelsStripCSR(channel0, channel1, channel2, channel3, channel4, channel5);
00317
00318
00319
00320
00321
00322 enableStreamStripCSR(true);
00323 communicationFirmwareInstance_->write(buffer, STRIP_CSR, stripCSRRegisterValue_);
00324
00325
00326
00327
00328 return buffer;
00329 }
00330
00331
00332 std::string FSSRFirmwareBase::stopStream(void)
00333 {
00334 std::string buffer;
00335 enableStreamStripCSR(false);
00336 enableChannelsStripCSR(false, false, false, false, false, false);
00337 communicationFirmwareInstance_->write(buffer, STRIP_CSR, stripCSRRegisterValue_);
00338 return buffer;
00339 }
00340
00341
00342 void FSSRFirmwareBase::makeDACSequence(FirmwareSequence<uint64_t>& sequence, unsigned int channel, const ROCStream& rocStream)
00343 {
00344 const ROCDACs& rocDACs = rocStream.getROCDACs();
00345 for (DACList::const_iterator it = rocDACs.getDACList().begin(); it != rocDACs.getDACList().end(); it++)
00346 {
00347
00348 uint64_t data = FSSRROCDefinitions::makeDACWriteCommand(
00349 rocStream.getFEWROCAddress(), it->first, it->second.second);
00350 sequence.pushBack(ChannelFIFOAddress[channel], data);
00351 sequence.pushBack(ChannelFIFOAddress[channel],
00352 BitManipulator::insertBits(data, (uint64_t) 0x48, 56, 8));
00353
00354 sequence.pushBack(ChannelFIFOAddress[channel],
00355 BitManipulator::insertBits(data, (uint64_t) 1, 60, 1));
00356
00357 sequence.pushBack(ChannelFIFOAddress[channel],
00358 BitManipulator::insertBits(data, (uint64_t) 0, 60, 1));
00359
00360 sequence.pushBack(ChannelFIFOAddress[channel],
00361 BitManipulator::insertBits(data, (uint64_t) 0, 62, 1));
00362 sequence.pushBack(ChannelFIFOAddress[channel],
00363 BitManipulator::insertBits(data, (uint64_t) 0x40, 56, 8));
00364
00365 }
00366 }
00367
00368
00369 void FSSRFirmwareBase::makeDACSequence(FirmwareSequence<uint32_t>& sequence,
00370 unsigned int channel, const ROCStream& rocStream)
00371 {
00372 const ROCDACs& rocDACs = rocStream.getROCDACs();
00373 for (DACList::const_iterator it = rocDACs.getDACList().begin(); it
00374 != rocDACs.getDACList().end(); it++)
00375 {
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391 uint32_t data = FSSRROCDefinitions::makeDACWriteHeader(
00392 rocStream.getFEWROCAddress(), it->first);
00393
00394 BitManipulator::insertBits(data, 1, 16 + channel, 1);
00395 sequence.pushBack(ChannelFIFOAddress[channel], it->second.second);
00396 std::cout << __COUT_HDR_FL__ << "Register: " << it->first << " value: "
00397 << it->second.second << std::hex << " -> Data: " << data << std::dec
00398 << std::endl;
00399 sequence.pushBack(STRIP_SC_CSR, data);
00400 }
00401 }
00402
00403
00404 void FSSRFirmwareBase::makeDACBuffer(std::string& buffer, unsigned int channel, const ROCStream& rocStream)
00405 {
00406 std::cout << __COUT_HDR_FL__ << "Channel: " << channel << std::endl;
00407 std::cout << __COUT_HDR_FL__ << "BufferINsize: " << buffer.size() << std::endl;
00408 const ROCDACs& rocDACs = rocStream.getROCDACs();
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454 for (DACList::const_iterator it = rocDACs.getDACList().begin(); it != rocDACs.getDACList().end(); it++)
00455 {
00456 std::string bufferElement;
00457 communicationFirmwareInstance_->waitClear(bufferElement, STRIP_SC_CSR, 0x80000000, false);
00458 uint32_t registerHeader = 0;
00459
00460 if (it->first != "RejectHits" && it->first != "SendData")
00461 {
00462 communicationFirmwareInstance_->write(bufferElement, ChannelFIFOAddress[channel], it->second.second, false);
00463 registerHeader = FSSRROCDefinitions::makeDACWriteHeader(rocStream.getFEWROCAddress(), it->first);
00464
00465 BitManipulator::insertBits(registerHeader, 1, 16 + channel, 1);
00466 }
00467 else
00468 {
00469 if (it->second.second == 1 || it->second.second == 2)
00470 {
00471 registerHeader = FSSRROCDefinitions::makeDACSetHeader(rocStream.getFEWROCAddress(), it->first);
00472
00473 BitManipulator::insertBits(registerHeader, 1, 16 + channel, 1);
00474 }
00475 else if (it->second.second == 0 || it->second.second == 5)
00476 {
00477 registerHeader = FSSRROCDefinitions::makeDACResetHeader(rocStream.getFEWROCAddress(), it->first);
00478
00479 BitManipulator::insertBits(registerHeader, 1, 16 + channel, 1);
00480 }
00481 else
00482 std::cout << __COUT_HDR_FL__ << "Register value for : " << it->first
00483 << " doesn't have a value I expect -> value = "
00484 << it->second.second << std::endl;
00485
00486 }
00487
00488
00489 communicationFirmwareInstance_->write(bufferElement, STRIP_SC_CSR, registerHeader, false);
00490 communicationFirmwareInstance_->waitClear(bufferElement, STRIP_SC_CSR, 0x80000000, false);
00491
00492
00493 buffer += bufferElement;
00494
00495 }
00496 std::cout << __COUT_HDR_FL__ << "BufferOUTsize: " << buffer.size() << std::endl;
00497 }
00498
00499
00500 void FSSRFirmwareBase::makeDACBuffer(std::vector<std::string>& buffer, unsigned int channel, const ROCStream& rocStream)
00501 {
00502
00503
00504
00505 int limitCount = 0;
00506 unsigned int singleVectorCount = 0;
00507
00508 std::string alternateBuffer;
00509
00510
00511 const ROCDACs& rocDACs = rocStream.getROCDACs();
00512
00513 std::string bufferElement;
00514
00515
00516 for (const std::pair<std::string, std::pair<unsigned int, unsigned int> >& it: rocDACs.getDACList())
00517
00518 {
00519
00520
00521
00522 communicationFirmwareInstance_->waitClear(bufferElement, STRIP_SC_CSR, 0x80000000, false);
00523 uint32_t registerHeader = 0;
00524
00525
00526 if (it.first != "RejectHits" && it.first != "SendData")
00527 {
00528
00529 communicationFirmwareInstance_->write(bufferElement, ChannelFIFOAddress[channel], it.second.second, false);
00530 registerHeader = FSSRROCDefinitions::makeDACWriteHeader(rocStream.getFEWROCAddress(), it.first);
00531
00532 BitManipulator::insertBits(registerHeader, 1, 16 + channel, 1);
00533 }
00534 else
00535 {
00536
00537 if (it.second.second == 1 || it.second.second == 2)
00538 {
00539 registerHeader = FSSRROCDefinitions::makeDACSetHeader(rocStream.getFEWROCAddress(), it.first);
00540
00541 BitManipulator::insertBits(registerHeader, 1, 16 + channel, 1);
00542 }
00543 else if (it.second.second == 0 || it.second.second == 5)
00544 {
00545 registerHeader = FSSRROCDefinitions::makeDACResetHeader(rocStream.getFEWROCAddress(), it.first);
00546
00547 BitManipulator::insertBits(registerHeader, 1, 16 + channel, 1);
00548 }
00549 else
00550 std::cout << __COUT_HDR_FL__ << "Register value for : " << it.first
00551 << " doesn't have a value I expect -> value = "
00552 << it.second.second << std::endl;
00553
00554 }
00555
00556 communicationFirmwareInstance_->write(bufferElement, STRIP_SC_CSR, registerHeader, false);
00557 communicationFirmwareInstance_->waitClear(bufferElement, STRIP_SC_CSR, 0x80000000, false);
00558
00559
00560
00561 limitCount++;
00562 singleVectorCount++;
00563
00564 if (limitCount == STIB_DAC_WRITE_MAX)
00565 {
00566 std::cout << __COUT_HDR_FL__ << "\tBuffer length:" << bufferElement.size() << std::endl;
00567 buffer.push_back(bufferElement);
00568 limitCount = 0;
00569 bufferElement.clear();
00570 }
00571 else if (singleVectorCount == rocDACs.getDACList().size())
00572 {
00573 buffer.push_back(bufferElement);
00574 }
00575
00576
00577
00578 }
00579
00580
00581 }
00582
00583
00584 void FSSRFirmwareBase::makeMaskBuffer(std::string& buffer, unsigned int channel,
00585 const ROCStream& rocStream)
00586 {
00587
00588 makeMaskBuffer(buffer, channel, rocStream, "Kill");
00589
00590 }
00591
00592
00593 void FSSRFirmwareBase::makeMaskBuffer(std::string& buffer, unsigned int channel, const ROCStream& rocStream, const std::string& registerName)
00594 {
00595 int chipId = rocStream.getFEWROCAddress();
00596 const std::string& mask = rocStream.getROCMask();
00597
00598
00599
00600
00601 unsigned int data[4] = { 0, 0, 0, 0 };
00602
00603 char val;
00604
00605 for (unsigned int d = 0; d < 4; d++)
00606 {
00607
00608 for (unsigned int m = 0; m < 8 * 4; m++)
00609 {
00610 val = mask[(8 * 4 * d) + m];
00611
00612
00613 data[d] |= (unsigned int) atoi(&val) << (8 * 4 - 1 - m);
00614
00615
00616
00617 }
00618
00619 }
00620
00621 int i;
00622 unsigned int w;
00623 unsigned char len = 4;
00624 unsigned char addr = 17;
00625 unsigned char bitMask = 1 << channel;
00626 unsigned char inst = WRITE;
00627
00628 communicationFirmwareInstance_->waitClear(buffer, STRIP_SC_CSR, 0x80000000, false);
00629
00630 for (i = 0; i < 4; i++)
00631
00632 communicationFirmwareInstance_->write(buffer, STRIP_SCI + 4 * (4 - i - 1), data[i], false);
00633
00634 w = 0x80000000 | (len << 24) | (bitMask << 16) | (inst << 10) | (addr << 5) | chipId;
00635
00636 communicationFirmwareInstance_->write(buffer, STRIP_SC_CSR, w, false);
00637
00638 communicationFirmwareInstance_->waitClear(buffer, STRIP_SC_CSR, 0x80000000, false);
00639 }
00640
00641
00642 void FSSRFirmwareBase::makeMaskSequence(FirmwareSequence<uint64_t>& sequence,
00643 unsigned int channel, const ROCStream& rocStream)
00644 {
00645 std::cout << __COUT_HDR_FL__ << "Making mask! " << std::endl;
00646 makeMaskSequence(sequence, channel, rocStream, "Kill");
00647
00648 }
00649
00650
00651 void FSSRFirmwareBase::makeMaskSequence(FirmwareSequence<uint32_t>& sequence,
00652 unsigned int channel, const ROCStream& rocStream)
00653 {
00654 std::cout << __COUT_HDR_FL__ << "Making mask! " << std::endl;
00655 makeMaskSequence(sequence, channel, rocStream, "Kill");
00656
00657 }
00658
00659
00660 void FSSRFirmwareBase::makeMaskSequence(FirmwareSequence<uint64_t>& sequence,
00661 unsigned int channel, const ROCStream& rocStream,
00662 const std::string& registerName)
00663 {
00664 int chipId = rocStream.getFEWROCAddress();
00665 std::string mask = rocStream.getROCMask();
00666 std::cout << __COUT_HDR_FL__ << "Mask length: " << mask.length() << std::endl;
00667
00668 uint64_t uInt64Data = 0;
00669 std::string stringData = "";
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687 BitManipulator::insertBits(uInt64Data, 0x1, 56, 8);
00688 sequence.pushBack(ChannelFIFOAddress[channel], uInt64Data);
00689
00690
00691 BitManipulator::insertBits(uInt64Data, 0x2, 56, 8);
00692 sequence.pushBack(ChannelFIFOAddress[channel], uInt64Data);
00693
00694
00695
00696
00697
00698 BitManipulator::insertBits(uInt64Data, 0x40, 16, 8);
00699 sequence.pushBack(ChannelFIFOAddress[channel], uInt64Data);
00700 BitManipulator::insertBits(uInt64Data, 0xc0, 16, 8);
00701 sequence.pushBack(ChannelFIFOAddress[channel], uInt64Data);
00702
00703 stringData = FSSRROCDefinitions::makeMaskWriteCommand(chipId,
00704 registerName, mask);
00705
00706 uint8_t data;
00707 for (unsigned int s = 0; s < stringData.length(); s++)
00708 for (int b = 8 - 1; b >= 0 && (s * 8 + 8 - b < 13 + 128); b--)
00709 {
00710
00711 data = 0x40 | (((stringData.data()[s] >> b) & 1) << 5);
00712 BitManipulator::insertBits(uInt64Data, (uint64_t) data, 16, 8);
00713 sequence.pushBack(ChannelFIFOAddress[channel], uInt64Data);
00714 data |= 0x80;
00715 BitManipulator::insertBits(uInt64Data, (uint64_t) data, 16, 8);
00716 sequence.pushBack(ChannelFIFOAddress[channel], uInt64Data);
00717 }
00718
00719
00720 BitManipulator::insertBits(uInt64Data, 0x0, 16, 8);
00721 sequence.pushBack(ChannelFIFOAddress[channel], uInt64Data);
00722 BitManipulator::insertBits(uInt64Data, 0x80, 16, 8);
00723 sequence.pushBack(ChannelFIFOAddress[channel], uInt64Data);
00724
00725
00726 BitManipulator::insertBits(uInt64Data, 0x0, 56, 8);
00727 sequence.pushBack(ChannelFIFOAddress[channel], uInt64Data);
00728
00729
00730 BitManipulator::insertBits(uInt64Data, 0x80, 56, 8);
00731 sequence.pushBack(ChannelFIFOAddress[channel], uInt64Data);
00732
00733 }
00734
00735
00736 void FSSRFirmwareBase::makeMaskSequence(FirmwareSequence<uint32_t>& sequence,
00737 unsigned int channel, const ROCStream& rocStream,
00738 const std::string& registerName)
00739 {}
00740
00741
00742 std::string FSSRFirmwareBase::readCSRRegister()
00743 {
00744 std::string buffer;
00745
00746 communicationFirmwareInstance_->read(buffer,STRIP_CSR);
00747 return buffer;
00748 }
00749
00750
00751 std::string FSSRFirmwareBase::readSCCSRRegister()
00752 {
00753 std::string buffer;
00754 std::cout << __COUT_HDR_FL__ << "FSSR readCSRRegister" << std::endl;
00755 communicationFirmwareInstance_->read(buffer,STRIP_SC_CSR);
00756 return buffer;
00757 }
00758
00759
00760 void FSSRFirmwareBase::setFrequencyFromClockState(std::string& buffer, double frequency)
00761 {
00762
00763
00764 int quotient;
00765 int numerator;
00766 int denominator;
00767 double realClockFrequency;
00768
00769 if (BitManipulator::readBits(stripCSRRegisterValue_, 17, 1) == 1)
00770 quotient = 4;
00771 else
00772 quotient = 8;
00773
00774 if (isClockStateExternal())
00775 {
00776 realClockFrequency = EXTERNAL_CLOCK_FREQUENCY / quotient;
00777 }
00778 else
00779 {
00780 realClockFrequency = INTERNAL_CLOCK_FREQUENCY / quotient;
00781 }
00782
00783 double factor = frequency / realClockFrequency;
00784
00785
00786
00787
00788
00789 numerator = factor * 100;
00790 denominator = 100;
00791
00792 do
00793 {
00794
00795 int gcd = numerator;
00796 int rest = denominator;
00797 int tmp;
00798
00799 while (rest != 0)
00800 {
00801 tmp = rest;
00802 rest = gcd % tmp;
00803 gcd = tmp;
00804 }
00805
00806
00807 if (gcd == 1)
00808 {
00809 numerator /= 2;
00810 denominator /= 2;
00811 }
00812 else
00813 {
00814 numerator /= gcd;
00815 denominator /= gcd;
00816 }
00817
00818 }
00819 while (denominator >= 32 || numerator >= 32);
00820
00821
00822
00823
00824
00825 setFrequencyRatio(buffer, numerator, denominator);
00826
00827 }
00828
00829 bool FSSRFirmwareBase::isClockStateExternal()
00830 {
00831 if (BitManipulator::readBits(stripCSRRegisterValue_, 16, 1) == 1)
00832 return true;
00833 else
00834 return false;
00835 }
00836
00837
00838 void FSSRFirmwareBase::setCSRRegister(uint32_t total)
00839 {
00840 stripCSRRegisterValue_ = total;
00841 }
00842
00843
00844 void FSSRFirmwareBase::setPacketSizeStripCSR(uint32_t size)
00845 {
00846 if (size > 31)
00847 std::cout << __COUT_HDR_FL__
00848 << "ERROR: Maximum packet size is 31 while here you are trying to set "
00849 << size << " packets!" << std::endl;
00850 BitManipulator::insertBits(stripCSRRegisterValue_, size, 3, 5);
00851
00852 }
00853
00854
00855 void FSSRFirmwareBase::enableChannelsStripCSR(bool channel0, bool channel1,
00856 bool channel2, bool channel3, bool channel4, bool channel5)
00857 {
00858 BitManipulator::insertBits(stripCSRRegisterValue_, ((uint32_t) channel0)
00859 + ((uint32_t) channel1 << 1) + ((uint32_t) channel2 << 2)
00860 + ((uint32_t) channel3 << 3) + ((uint32_t) channel4 << 4)
00861 + ((uint32_t) channel5 << 5), 8, 6);
00862 }
00863
00864
00865 void FSSRFirmwareBase::setExternalBCOClockSourceStripCSR(std::string clockSource)
00866 {
00867 if (clockSource == "External")
00868 BitManipulator::insertBits(stripCSRRegisterValue_, 1, 16, 1);
00869 else if (clockSource == "Internal")
00870 BitManipulator::insertBits(stripCSRRegisterValue_, 0, 16, 1);
00871 }
00872
00873
00874
00875
00876
00877
00878
00879
00880 void FSSRFirmwareBase::armBCOResetCSR(void)
00881 {
00882 BitManipulator::insertBits(stripCSRRegisterValue_, 1, 19, 1);
00883 }
00884
00885
00886 void FSSRFirmwareBase::flushBuffersStripCSR(void)
00887 {
00888 BitManipulator::insertBits(stripCSRRegisterValue_, 1, 20, 1);
00889 }
00890
00891
00892 void FSSRFirmwareBase::resetTriggerCounterStripCSR(std::string& buffer)
00893 {
00894
00895 BitManipulator::insertBits(stripCSRRegisterValue_, 1, 21, 1);
00896 communicationFirmwareInstance_->write(buffer, STRIP_CSR, stripCSRRegisterValue_, false);
00897 BitManipulator::insertBits(stripCSRRegisterValue_, 1, 21, 1);
00898 communicationFirmwareInstance_->write(buffer, STRIP_CSR, stripCSRRegisterValue_, false);
00899 BitManipulator::insertBits(stripCSRRegisterValue_, 1, 21, 1);
00900 communicationFirmwareInstance_->write(buffer, STRIP_CSR, stripCSRRegisterValue_, false);
00901 BitManipulator::insertBits(stripCSRRegisterValue_, 1, 21, 1);
00902 communicationFirmwareInstance_->write(buffer, STRIP_CSR, stripCSRRegisterValue_, false);
00903 BitManipulator::insertBits(stripCSRRegisterValue_, 1, 21, 1);
00904 communicationFirmwareInstance_->write(buffer, STRIP_CSR, stripCSRRegisterValue_, false);
00905
00906 BitManipulator::insertBits(stripCSRRegisterValue_, 0, 21, 1);
00907 communicationFirmwareInstance_->write(buffer, STRIP_CSR, stripCSRRegisterValue_, false);
00908 }
00909
00910
00911 void FSSRFirmwareBase::resetBCOCounterStripCSR(void)
00912 {
00913 BitManipulator::insertBits(stripCSRRegisterValue_, 1, 22, 1);
00914 }
00915
00916
00917 void FSSRFirmwareBase::enableTriggerStripCSR(bool enable)
00918 {
00919 BitManipulator::insertBits(stripCSRRegisterValue_, (uint32_t) enable, 23, 1);
00920 }
00921
00922
00923 void FSSRFirmwareBase::sendTriggerDataStripCSR(bool send)
00924 {
00925 BitManipulator::insertBits(stripCSRRegisterValue_, (uint32_t) send, 24, 1);
00926 }
00927
00928
00929 void FSSRFirmwareBase::sendTriggerNumberStripCSR(bool send)
00930 {
00931 BitManipulator::insertBits(stripCSRRegisterValue_, (uint32_t) send, 25, 1);
00932 }
00933
00934
00935 void FSSRFirmwareBase::sendBCOStripCSR(bool send)
00936 {
00937 BitManipulator::insertBits(stripCSRRegisterValue_, (uint32_t) send, 26, 1);
00938 }
00939
00940
00941 void FSSRFirmwareBase::enableStreamStripCSR(bool enable)
00942 {
00943 BitManipulator::insertBits(stripCSRRegisterValue_, (uint32_t) enable, 27, 1);
00944 }
00945
00946
00947 void FSSRFirmwareBase::resetDCMStripCSR(bool clear)
00948 {
00949 BitManipulator::insertBits(stripCSRRegisterValue_, (uint32_t) clear, 31, 1);
00950 }
00951
00952
00953 uint32_t FSSRFirmwareBase::waitDCMResetStripCSR(void)
00954 {
00955 uint32_t bitToCheck = 0;
00956 BitManipulator::insertBits(bitToCheck, 1, 31, 2);
00957 return bitToCheck;
00958 }
00959
00960
00961 void FSSRFirmwareBase::resetDAC(void)
00962 {
00963 BitManipulator::insertBits(stripResetRegisterValue_, 1, 27, 1);
00964 }
00965
00966
00967 void FSSRFirmwareBase::resetLink(bool channel0, bool channel1, bool channel2,
00968 bool channel3, bool channel4, bool channel5)
00969 {
00970 stripResetRegisterValue_ = 0;
00971 BitManipulator::insertBits(stripResetRegisterValue_,
00972 ((uint32_t) channel0) + ((uint32_t) channel1 << 1)
00973 + ((uint32_t) channel2 << 2) + ((uint32_t) channel3 << 3)
00974 + ((uint32_t) channel4 << 4) + ((uint32_t) channel5 << 5),
00975 0, 6);
00976 BitManipulator::insertBits(stripResetRegisterValue_, 1, 29, 1);
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987 }
00988
00989
00990 void FSSRFirmwareBase::clearErrors(bool channel0, bool channel1, bool channel2,
00991 bool channel3, bool channel4, bool channel5)
00992 {
00993 stripResetRegisterValue_ = 0;
00994 BitManipulator::insertBits(stripResetRegisterValue_,
00995 ((uint32_t) channel0) + ((uint32_t) channel1 << 1)
00996 + ((uint32_t) channel2 << 2) + ((uint32_t) channel3 << 3)
00997 + ((uint32_t) channel4 << 4) + ((uint32_t) channel5 << 5),
00998 0, 6);
00999 BitManipulator::insertBits(stripResetRegisterValue_, 1, 29, 1);
01000 }
01001
01002
01003 void FSSRFirmwareBase::clearFIFO(bool channel0, bool channel1, bool channel2,
01004 bool channel3, bool channel4, bool channel5)
01005 {
01006 stripResetRegisterValue_ = 0;
01007 BitManipulator::insertBits(stripResetRegisterValue_,
01008 ((uint32_t) channel0) + ((uint32_t) channel1 << 1)
01009 + ((uint32_t) channel2 << 2) + ((uint32_t) channel3 << 3)
01010 + ((uint32_t) channel4 << 4) + ((uint32_t) channel5 << 5),
01011 0, 6);
01012 BitManipulator::insertBits(stripResetRegisterValue_, 1, 30, 1);
01013 }
01014
01015
01016 void FSSRFirmwareBase::resetChip(bool channel0, bool channel1, bool channel2,
01017 bool channel3, bool channel4, bool channel5)
01018 {
01019 stripResetRegisterValue_ = 0;
01020 BitManipulator::insertBits(stripResetRegisterValue_,
01021 ((uint32_t) channel0) + ((uint32_t) channel1 << 1)
01022 + ((uint32_t) channel2 << 2) + ((uint32_t) channel3 << 3)
01023 + ((uint32_t) channel4 << 4) + ((uint32_t) channel5 << 5),
01024 0, 6);
01025 BitManipulator::insertBits(stripResetRegisterValue_, 1, 31, 1);
01026 }
01027
01028
01029 void FSSRFirmwareBase::setFrequencyRatio(std::string& buffer, int numerator, int denominator)
01030 {
01031
01032 communicationFirmwareInstance_->write(buffer, STRIP_BCO_DCM, 0x80500000 + (numerator - 1));
01033 communicationFirmwareInstance_->waitClear(buffer, STRIP_BCO_DCM, 0xf0000000);
01034
01035 communicationFirmwareInstance_->write(buffer, STRIP_BCO_DCM, 0x80520000 + (denominator - 1));
01036 communicationFirmwareInstance_->waitClear(buffer, STRIP_BCO_DCM, 0xf0000000);
01037 }
01038
01039
01040 void FSSRFirmwareBase::BCOOffset(uint32_t offset)
01041 {
01042 BitManipulator::insertBits(stripTriggerCSRRegisterValue_, offset, 0, 4);
01043 }
01044
01045
01046 void FSSRFirmwareBase::selectSpyFIFO(uint32_t input)
01047 {
01048 BitManipulator::insertBits(stripTriggerCSRRegisterValue_, input, 4, 3);
01049 }
01050
01051
01052 void FSSRFirmwareBase::halt(bool halt)
01053 {
01054 BitManipulator::insertBits(stripTriggerCSRRegisterValue_, (uint32_t) halt, 7, 1);
01055 }
01056
01057
01058 void FSSRFirmwareBase::configureStripTriggerUnbiased(std::string& buffer)
01059 {
01060 communicationFirmwareInstance_->write(buffer, STRIP_TRIG_UNBIASED, 0x002805c, false);
01061 }
01062
01063
01064 void FSSRFirmwareBase::configureTriggerInputs(std::string& buffer)
01065 {
01066
01067
01068 communicationFirmwareInstance_->write(buffer, STRIP_TRIG_INPUT_0, 0x0, false);
01069 communicationFirmwareInstance_->write(buffer, STRIP_TRIG_INPUT_1, 0x0, false);
01070 communicationFirmwareInstance_->write(buffer, STRIP_TRIG_INPUT_2, 0x0, false);
01071 }
01072
01073
01074 std::string FSSRFirmwareBase::resetSlaveBCO(void)
01075 {
01076 std::string buffer;
01077
01078 communicationFirmwareInstance_->write(buffer, (uint32_t)0xc5000000, (uint32_t)0x00000008);
01079 return buffer;
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
01131
01132
01133
01134
01135
01136
01137
01138
01139
01140
01141
01142
01143
01144
01145
01146
01147
01148
01149
01150
01151
01152
01153
01154
01155
01156
01157
01158
01159
01160
01161
01162
01163
01164
01165
01166
01167
01168
01169
01170
01171
01172
01173
01174
01175
01176
01177
01178
01179
01180
01181
01182
01183
01184
01185
01186
01187
01188
01189