00001
00008 #ifndef SCTROD_SCTAPI_H
00009 #define SCTROD_SCTAPI_H
00010
00011
00012 #include "config.h"
00013
00014 #include <list>
00015 #include <map>
00016 #include <string>
00017 #include <vector>
00018
00019 #include <boost/thread.hpp>
00020 #include <boost/shared_ptr.hpp>
00021 #include <boost/shared_array.hpp>
00022
00023 #include <mrs/message.h>
00024 #if USE_IS
00025 #include <is/isinfotmpl.h>
00026 #endif
00027
00028 class SCTDCSCommand;
00029 #include "log.h"
00030
00031 #include "SctApiConfigCache.h"
00032 #include "Sct/Addressing.h"
00033
00034
00035 #include <CommonWithDsp/sctStructure.h>
00036
00037 #include <CommonWithDsp/processor.h>
00038
00039 struct scan_result_ptrs;
00040
00041 namespace SctConfiguration {
00042 class Configuration;
00043 struct BOCChannelConfig;
00044 struct BOCGlobalConfig;
00045 }
00046
00047 #include "SctApiFwd.h"
00048
00049
00050 #include "ModuleList.h"
00051
00052
00053 namespace SctApi {
00054 class Crate;
00055 class PrimListWrapper;
00056 class ScanEx;
00057 class DCSAccess;
00058 class SctApiDDC;
00059 };
00060
00061
00062 namespace SctApi {
00063
00121 class SctApi {
00122 SctApi(const SctApi &);
00123 SctApi &operator=(const SctApi&);
00124 public:
00125
00127 static const unsigned int BAD_MODULE;
00128
00132 SctApi(const Idiosyncrasy &);
00133
00137 SctApi(const Idiosyncrasy &, boost::shared_ptr<SctConfiguration::Configuration> newConf);
00138
00139 virtual ~SctApi();
00140
00141 private:
00147 void setup();
00148
00152 void dumpPrimList(boost::shared_ptr<PrimListWrapper> prim);
00153
00155 void dumpRawEvent(unsigned int rod, int units, unsigned long *bufferA, unsigned long *bufferB);
00156
00157 public:
00161 const Idiosyncrasy & idiosyncrasy();
00162
00166 void setMrsStream(MRSStream *stream);
00167
00168 #if USE_IS
00169
00172 void setIsDictionary();
00173 #endif
00174
00175
00176
00178
00191 void initialiseAll(int run);
00192
00194
00201 void shutdownAll();
00202
00204
00207 void setRunNumber(UINT32 newRun);
00208
00210
00213 void setScanNumber(UINT32 newScan);
00214
00216
00219 UINT32 getRunNumber();
00220
00222
00225 UINT32 getScanNumber();
00226
00228
00232 bool isRODPresent(unsigned int rod);
00233
00235
00242 void startupModules();
00243
00245 ModuleList getModuleList();
00246
00247
00248
00250
00254 void loadConfiguration();
00255
00257
00261 void loadConfiguration(std::string filename);
00262
00264
00267 int loadModuleConfigurations();
00268
00270
00274 void storeModuleConfigurations();
00275
00277
00281 void storeBOCConfigurations();
00282
00284
00288 void configureBOC(unsigned int rod);
00289
00291
00295 std::vector<double> getBOCMonitorArray(unsigned int rod);
00296
00298
00302 int awaitResponse(unsigned int rod, int timeout);
00303
00305
00309 int awaitResponseAll(int timeout);
00310
00312
00317 unsigned long *getResponse(unsigned int rod,
00318 unsigned long &length);
00319
00325 bool getRodMessage(unsigned int rod,
00326 char *buffer, unsigned long &length);
00327
00328
00329
00331
00341 void flashLED(unsigned int rod,
00342 long slaveNumber, long period = 1000, long flashes = 10);
00343
00345
00349 void echo(unsigned int rod,
00350 unsigned int length, const unsigned long *data);
00351
00353
00357 void echoAll(unsigned int length, const unsigned long *data);
00358
00360
00364 void echoSlave(unsigned int rod,
00365 unsigned int slave, unsigned int length, const unsigned long *data);
00366
00367
00368
00369
00370
00372
00381 int dspBlockDump(unsigned int rod,
00382 long dspStart, long numWords, long dspNumber, bool usePrim = true);
00383
00385
00395 int dspBlockDumpFile(unsigned int rod,
00396 long dspStart, long numWords, long dspNumber, std::string filename, bool usePrim = true);
00397
00399
00404 void standardRegisterDump(RodLabel rl);
00405
00407
00410 void standardRegisterDump(unsigned int rod);
00411
00413
00417 void standardRegisterDumpAll();
00418
00420
00426 unsigned long *dspBlockRead(unsigned int rod,
00427 long dspStart, long numWords, long dspNumber, unsigned long &length, bool usePrim = true);
00428
00430
00436 int dspBlockWrite(unsigned int rod,
00437 unsigned long *buffer, unsigned long dspAddress, long numWords, long dspNumber, bool usePrim = true);
00438
00440
00443 unsigned long dspSingleBlockRead(unsigned int rod,
00444 long dspStart, long dspNumber);
00445
00447
00450 void dspSingleBlockWrite(unsigned int rod,
00451 unsigned long dspAddress, unsigned long val, long dspNumber);
00452
00454
00462 unsigned long *primReadSlaveDsp(unsigned int rod,
00463 int s, int add, int words);
00464
00466
00472 void primWriteSlaveDsp(unsigned int rod,
00473 int s, int add, int numWords, unsigned long *data);
00474
00475 void writeSlaveFile(unsigned int rod, unsigned int slave,
00476 std::string fileName, unsigned long address, bool usePrim = true);
00477
00479 void startSlave(unsigned int rod, unsigned int slave);
00480
00482 boost::shared_array<unsigned long> loadFile(std::string filename, unsigned long & length);
00483
00485
00489 unsigned long readRodStatusReg(unsigned int rod,
00490 long regNumber);
00491
00493
00497 unsigned long readRodCommandReg(unsigned int rod,
00498 long regNumber);
00499
00501
00505 unsigned long dspSingleRead(unsigned int rod,
00506 const unsigned long dspAddr, long dspNumber);
00507
00509
00513 void dspSingleWrite(unsigned int rod,
00514 unsigned long dspAddr, unsigned long value, long dspNumber);
00515
00517
00523 ABCDModule *retrieveModule(UINT32 mid);
00524
00526
00530 void createDebugPrimList();
00531
00533
00537 void addDebugPrimList(unsigned long length, long index, long id, long version,
00538 unsigned long * body);
00539
00541
00545 void sendDebugPrimList(unsigned int rod);
00546
00548
00552 void sendDebugPrimListAll();
00553
00555
00563 void sendDebugSlavePrimList(unsigned int rod,
00564 unsigned int slave, bool await, bool response);
00565
00567
00570 void debugPrimListFromFile(std::string fileName);
00571
00573 void dumpDebugPrimList();
00574
00576 const std::list<RodLabel> listRods() const;
00577
00579
00580
00581
00582
00584
00591 UINT32 findModule(std::string sn);
00592
00594
00598 std::string convertToString(UINT32 mid);
00599
00601
00605 UINT32 findModule(INT32 mur, INT32 module);
00606
00608
00612 std::pair<INT32, INT32> convertToMUR(UINT32 mid);
00613
00615
00619 UINT32 findBarrelModule(INT32 barrel, INT32 row, INT32 number);
00620
00622
00626 void convertToBarrelModule(UINT32 mid, UINT32 &barrel, UINT32 &row, int &number);
00627
00629
00633 UINT32 findEndcapModule(INT32 disk, INT32 quadrant, INT32 number);
00634
00636
00640 void convertToEndcapModule(UINT32 mid, INT32 &disk, UINT32 &quadrant, UINT32 &number);
00641
00643
00648 void getABCDModules(BankType bank);
00649
00651
00656 virtual void getABCDModule(UINT32 mid, BankType bank);
00657
00659
00664 virtual boost::shared_ptr<ABCDModule> getABCDModuleRaw(unsigned int rod,
00665 UINT32 slot, BankType bank);
00666
00668
00673 virtual void setABCDModules(std::list<BankType> bank);
00674
00676
00681 virtual void setABCDModule(UINT32 mid, std::list<BankType> banks);
00682
00686 virtual void copyABCDModule(UINT32 mid, BankType from, std::list<BankType> to);
00687
00691 virtual void copyABCDModules(BankType from, std::list<BankType> to);
00692
00694
00704 virtual void sendAllABCDModules(BankType bank,
00705 ConfigType type = SCTAPI_CONFIG_ALL,
00706 const bool enableDataMode=true );
00707
00709
00719 virtual void sendABCDModule(UINT32 mid,
00720 BankType bank,
00721 ConfigType type = SCTAPI_CONFIG_ALL,
00722 const bool enableDataMode=true );
00723
00725
00731 void modifyABCDMask(UINT32 mid, UINT32* mask);
00732
00736
00737
00744 void modifyABCDTrims(UINT32 mid, UINT8* trims);
00745
00749
00750
00752
00756 void modifyABCDVar(UINT32 typ, FLOAT32 var);
00757
00759
00763 void modifyABCDVar(UINT32 mid, UINT32 typ, FLOAT32 var);
00764
00766
00767
00769
00773 void modifyABCDVar(UINT32 mid, UINT32 chip, UINT32 typ, FLOAT32 var);
00774
00776
00780 void modifyABCDVarROD(UINT32 mid, UINT32 chip, UINT32 typ, FLOAT32 var, std::list<BankType> banks);
00781
00783
00787 void modifyABCDVarROD(UINT32 mid, UINT32 typ, FLOAT32 var, std::list<BankType> banks);
00788
00790
00794 void modifyABCDVarROD(UINT32 type, FLOAT32 value, std::list<BankType> banks);
00795
00797
00798
00800
00804 void modifyABCDRC(UINT32 mid, UINT32 chip, UINT16 function, FLOAT32 p0, FLOAT32 p1, FLOAT32 p2);
00805
00807
00811 void modifyBOCParam(unsigned int rod,
00812 unsigned int channel, unsigned int type, unsigned int val);
00813
00815
00819 void modifyBOCParam(unsigned int type, unsigned int val);
00820
00822
00826 void printBOCSetup(unsigned int rod);
00827
00829
00833 std::vector<SctConfiguration::BOCChannelConfig> currentBOCSetup(unsigned int rod);
00834
00836
00840 void printBOCRegisters(unsigned int rod);
00841
00843
00847 SctConfiguration::BOCGlobalConfig currentBOCRegisters(unsigned int rod);
00848
00850 void saveBOCSetup(unsigned int rod, BankType bank);
00851
00853 void saveBOCRegisters(unsigned int rod, BankType bank);
00854
00856 void restoreBOCSetup(unsigned int rod, BankType bank);
00857
00859 void restoreBOCRegisters(unsigned int rod, BankType bank);
00860
00862
00866 void lasersOff();
00867
00868
00869
00871
00878 void timSetFrequency(double trigFreq, double rstFreq);
00879
00881
00886 void freeTriggers();
00887
00889
00893 void stopTriggers();
00894
00896
00900 void timL1A();
00901
00903
00907 void timCalL1A(int delay);
00908
00910
00914 void timSoftReset();
00915
00917
00921 void timBCReset();
00922
00924 void sendTimBurst(unsigned int count);
00925
00927 void timVerbose();
00928
00930
00933 void timWriteRegister(int reg, UINT16 val);
00934
00936
00939 UINT16 timReadRegister(int reg);
00940
00942
00946 void status();
00947
00949
00953 void defaultScan(int type);
00954
00956
00962 void doScan(boost::shared_ptr<Scan> scan);
00963
00964 bool hasFinishedScanning();
00965
00966 const std::string & nameOfScanStatusObject();
00967
00971 void awaitScan();
00972
00982 void abortScan();
00983
00985
00989 void doRawScan(boost::shared_ptr<Scan> scan, int delay, int width,
00990 bool configureModules = false, bool clkBy2 = false);
00991
00993
00997 void sendTimTrigger(const Trigger *trig);
00998
01000
01004 void sendTrigger(unsigned int rod, const Trigger *trig);
01005
01007
01013 void printABCDModule(int mid);
01014
01016
01023 void printABCDRodModule(int mid, BankType bank);
01024
01026
01030 void decodeEvent(unsigned int rod,
01031 int sl, int index, bool extFlag = false, bool errorType = false);
01032
01034
01038 void scanEvents(unsigned int rod, int sl, bool extFlag, bool errorType);
01039
01043 void reportEventErrors();
01044
01048 void decodeConfig(unsigned int rod,
01049 bool skipTrim = false, bool bypass = false);
01050
01052
01056 void rawData(unsigned int rod, int delay, int units, bool setMask = true, const Trigger *trig = 0);
01057
01059
01065 void sendL1A(unsigned int rod, bool capture = false);
01066
01068
01085 std::vector<char> probe(unsigned int rod,
01086 signed int harness = -1);
01087
01089
01106 std::vector<char> probeWithTrigger(unsigned int rod,
01107 const Trigger *trigger, signed int harness = -1);
01108
01110 std::vector<std::vector<char> > SctApi::probeScan(unsigned int rod,
01111 boost::shared_ptr<Scan> scan, signed int harness = -1);
01112
01119 bool checkAllModulesProbe(std::string value);
01120
01126 void autoConfigure();
01127
01131 void bocHistogram(unsigned int rod,
01132 unsigned int samples, unsigned int numLoops);
01133
01134
01135 SctApiDDC * getSctApiDDC() const;
01136
01147 void tidyHistogramming();
01148
01152 long getRodSlot(unsigned int rod);
01153
01157 int getRodRevision(unsigned int rod);
01158
01162 int getRodRevision(const RodLabel &label);
01163
01167 void testLinkOutSelect(unsigned int rod,
01168 unsigned int link);
01169
01173 unsigned int testLinkOutRetrieve(unsigned int rod);
01174
01182 void unsetDebugOption(std::string opt);
01183
01191 void setDebugOption(std::string opt);
01192
01200 std::list<std::string> listEnabledDebugOptions();
01201
01209 std::vector<std::string> listDebugOptions();
01210
01214 void debugStepHistogram();
01215
01221 void debugContinueHistogram();
01222
01229 void debugAbortHistogram();
01230
01236 void eventTrapSetup(int slaveMask, int trapMatch, int trapMod, int trapRemain, bool tim, bool error,
01237 boost::shared_ptr<PrimListWrapper> primList);
01238
01240 void startEventTrap(int slave, boost::shared_ptr<PrimListWrapper> primList);
01241
01243 void stopEventTrap(int slave, boost::shared_ptr<PrimListWrapper> primList);
01244
01246 void rodMode(unsigned int rod,
01247 int mode, int flag, int fifoSetup, int nBins, int delay, int message);
01248
01250 void setupModuleMask(unsigned int rod,
01251 int port, int slvs);
01252
01254 void setupModuleMask(int port, int slvs, boost::shared_ptr<PrimListWrapper> list);
01255
01257 std::pair<UINT32, UINT32> sendData(unsigned int rod, int type, int dsp);
01258
01262 void resumePolling();
01263
01267 void stopPolling();
01268
01270 void print_calib(unsigned int rod);
01271
01273
01278 int synchSendPrimListAllCrates(boost::shared_ptr<PrimListWrapper> primList, int timeout = 10);
01279
01281 void stopHistogramming(const ScanEx &ex);
01282
01284 void setSlaveBlock(unsigned int rod,
01285 int s, long sAdd, int words, long value = 0, bool usePrim = true);
01286
01288 void writeRODRegister(unsigned int rod,
01289 int reg, int off, int width, int value);
01290
01292 unsigned int readRODRegister(unsigned int rod, int r);
01293
01295 int pollRegister(unsigned int rod, int r, int off, int width, int val, int timeout = 1000000);
01296
01297 virtual unsigned long *readFifo(unsigned int rod, int id, int bank, int elems);
01298
01300 void startHistogramTask(const Scan &scan, const ScanEx &ex, const RodLabel,
01301 unsigned int startBin, unsigned int nBins, unsigned int nTrigs,
01302 boost::shared_ptr<PrimListWrapper> list);
01303
01305
01310 void sendPrimList(unsigned int rod,
01311 boost::shared_ptr<PrimListWrapper> prim);
01312
01313 MRSStream *mrs;
01314 #if USE_IS
01315 boost::shared_ptr<ISInfoDictionary> m_isDict;
01316 #endif
01317 protected:
01318
01323 virtual void really_setABCDModule(UINT32 mid, std::list<BankType> banks);
01324
01329 virtual void really_getABCDModule(UINT32 mid, BankType bank);
01330
01331
01332
01333
01335
01340 virtual int initialiseRod(unsigned int rod);
01341
01343
01349 virtual int initialiseModule(std::string module);
01350
01352 ABCDModule *lookupConfig(UINT32 mid);
01353
01355
01359 virtual bool preScanHardwareCheck(Scan& scan, ScanEx& extra);
01360
01361 virtual void doHistogramSetup(const Scan &scan, const ScanEx &extra);
01362
01367 UINT32 *readEventBuffer(unsigned int rod,
01368 int sl, bool extFlag, bool errorType);
01369
01373 int getLastEventFrame(unsigned int rod, int slave);
01374
01376
01385 virtual int pollHistogramming(boost::shared_ptr<ScanControl> controller, int timeout = 5);
01386
01387 void scanLoop();
01388
01389 void scanPollingThread();
01390
01391 void addRodToList(RodLabel newRod);
01392
01393 RodInfo &SctApi::getRodInfo(const RodLabel &l);
01394
01396 std::list<RodLabel> rodList;
01397
01399 std::map<RodLabel, RodInfo> rodInfoList;
01400
01402 std::map<UINT32, ABCDModule> moduleMap;
01403
01404
01405 boost::shared_ptr<ScanControl> scanController;
01406
01407
01408 boost::shared_ptr<ScanControl> lastScanController;
01409
01410 private:
01411
01412
01413
01414
01415
01416
01417 const Sct::UCID & ucid() const;
01418 unsigned int prt() const;
01419 unsigned int crt() const;
01420 Sct::URID localRod(const Sct::RodIdentifier rod) const;
01421
01423
01425 void doRawScanThreadInnards(boost::shared_ptr<Scan> scan, int delay, int width,
01426 bool configureModules, bool clkBy2);
01428
01430 void doRawScanThread(boost::shared_ptr<Scan> scan, int delay, int width,
01431 bool configureModules, bool clkBy2);
01432
01433
01435
01441 void sendSlavePrimList(unsigned int rod,
01442 boost::shared_ptr<PrimListWrapper> prim, unsigned int slave, bool await, bool response);
01443
01445
01450 void sendPrimListAll(boost::shared_ptr<PrimListWrapper> prim);
01451
01453 void setupEventTrapping(const Scan &scan, const ScanEx &ex, const RodLabel, boost::shared_ptr<PrimListWrapper> list);
01454
01456 void startEventTrapping(const Scan &scan, const ScanEx &ex, const RodLabel, boost::shared_ptr<PrimListWrapper> primList);
01457
01459 void setupEventDump(const Scan &scan, const ScanEx &ex, const RodLabel, boost::shared_ptr<PrimListWrapper> list);
01460
01462 void setupHistogramming(const Scan &scan, const ScanEx &ex, const RodLabel, bool tim,
01463 boost::shared_ptr<PrimListWrapper> list);
01464
01466 void calib_init();
01467
01469 void readRawData(unsigned int rod,
01470 int delay, bool setMask);
01471
01473 virtual int setupRawData(unsigned int rod,
01474 int delay, int units, bool setMask, const Trigger *trig = 0);
01475
01477
01481 Crate *getCrate() const;
01482
01484 void cacheModuleConfig(UINT32 mid);
01485
01487
01490 void getrpcrc(UINT32 mid, unsigned int &rpartition, unsigned int &rcrate, unsigned int &rrod, unsigned &rchannel);
01491
01493 void setupScanMasks(ScanEx &extra, int distSlave, bool dual);
01494
01496 bool checkModuleListsForScan();
01497
01499
01502 void preScanModuleSetup(Scan &scan);
01503
01504 bool checkDebugOption(std::string opt);
01505
01506 bool checkDebugOption(int opt);
01507
01508
01509
01511 boost::shared_ptr<Idiosyncrasy> m_id;
01512
01514 boost::shared_ptr<SctConfiguration::Configuration> config;
01515
01516 boost::shared_ptr<DCSAccess> m_dcsAccess;
01517 boost::shared_ptr<SctApiDDC> m_sctApiDDC;
01518
01520 boost::shared_ptr<PrimListWrapper> debugPrimList;
01521
01523
01524 boost::shared_ptr<Crate> crateObject;
01525
01527 boost::shared_ptr<Log> m_log;
01528 Log & log() { return *m_log; };
01529
01531 boost::shared_ptr<const Scan> lastDebugScan;
01532 boost::shared_ptr<const ScanEx> lastDebugScanEx;
01533
01535 UINT32 scanNumber;
01536
01538 UINT32 runNumber;
01539
01540 #if USE_SCAN_THREAD
01541 boost::condition scanQueue_notEmpty;
01542 boost::mutex scanQueue_mutex;
01543
01544
01545 std::auto_ptr<boost::thread> scanPollThread;
01546
01547 bool m_stopPolling;
01548 bool m_inScanLoop;
01549 bool m_inRawScanLoop;
01550 #endif
01551
01553 SctApiConfigCache module_cache;
01554
01555
01556 };
01557 }
01558 #endif