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
00026 #include <is/infodictionary.h>
00027 #endif
00028
00029 class SCTDCSCommand;
00030 #include "log.h"
00031
00032 #include "SctApiConfigCache.h"
00033 #include "Sct/Addressing.h"
00034
00035
00036 #include <Sct/AbcdModule.h>
00037
00038 #include <CommonWithDsp/processor.h>
00039
00040 struct scan_result_ptrs;
00041
00042 namespace SctConfiguration {
00043 class Configuration;
00044 struct BOCChannelConfig;
00045 struct BOCGlobalConfig;
00046 }
00047
00048 #include "SctApiFwd.h"
00049
00050
00051 #include "ModuleList.h"
00052
00053 namespace SctApi {
00054
00112 class SctApi {
00113 SctApi(const SctApi &);
00114 SctApi &operator=(const SctApi&);
00115 public:
00116
00118 static const unsigned int BAD_MODULE;
00119
00123 SctApi(const Idiosyncrasy &);
00124
00128 SctApi(const Idiosyncrasy &, boost::shared_ptr<SctConfiguration::Configuration> newConf);
00129
00130 virtual ~SctApi();
00131
00132 private:
00138 void setup();
00139
00143 void dumpPrimList(boost::shared_ptr<PrimListWrapper> prim);
00144
00146 void dumpRawEvent(unsigned int rod, int units, unsigned long *bufferA, unsigned long *bufferB);
00147
00148 public:
00152 const Idiosyncrasy & idiosyncrasy();
00153 const Sct::UCID & ucid() const;
00154
00155 const std::string getHostName() const;
00156
00160 void setMrsStream(MRSStream *stream);
00161
00162 #if USE_IS
00163
00166 void setIsDictionary();
00167 #endif
00168
00169
00170
00172
00185 void initialiseAll(int run);
00186
00188
00195 void shutdownAll();
00196
00198
00201 void setRunNumber(UINT32 newRun);
00202
00204
00207 void setScanNumber(UINT32 newScan);
00208
00210
00213 UINT32 getRunNumber();
00214
00216
00219 UINT32 getScanNumber();
00220
00222
00226 bool isRODPresent(unsigned int rod);
00227
00231 bool changeRunMode(enum RunType mode);
00232
00236 bool changeEventFormat(enum EventFormatType format);
00237
00239
00246 void startupModules();
00247
00249 ModuleList getModuleList();
00250
00251
00252
00254
00258 void loadConfiguration();
00259
00261
00265 void loadConfiguration(std::string filename);
00266
00268
00271 int loadModuleConfigurations();
00272
00274
00278 void storeModuleConfigurations(BankType bank);
00279
00281
00285 void storeBOCConfigurations();
00286
00288
00292 void configureBOC(unsigned int rod);
00293
00295
00299 std::vector<double> getBOCMonitorArray(unsigned int rod);
00300
00302
00306 int awaitResponse(unsigned int rod, int timeout);
00307
00309
00313 int awaitResponseAll(int timeout);
00314
00316
00321 unsigned long *getResponse(unsigned int rod,
00322 unsigned long &length);
00323
00329 bool getRodMessage(unsigned int rod,
00330 char *buffer, unsigned long &length);
00331
00332
00333
00335
00345 void flashLED(unsigned int rod,
00346 long slaveNumber, long period = 1000, long flashes = 10);
00347
00349
00353 void echo(unsigned int rod,
00354 unsigned int length, const unsigned long *data);
00355
00357
00361 void echoAll(unsigned int length, const unsigned long *data);
00362
00364
00368 void echoSlave(unsigned int rod,
00369 unsigned int slave, unsigned int length, const unsigned long *data);
00370
00371
00372
00373
00374
00376
00385 int dspBlockDump(unsigned int rod,
00386 long dspStart, long numWords, long dspNumber, bool usePrim = true);
00387
00389
00399 int dspBlockDumpFile(unsigned int rod,
00400 long dspStart, long numWords, long dspNumber, std::string filename, bool usePrim = true);
00401
00403
00408 void standardRegisterDump(RodLabel rl);
00409
00411
00414 void standardRegisterDump(unsigned int rod);
00415
00417
00421 void standardRegisterDumpAll();
00422
00424
00430 unsigned long *dspBlockRead(unsigned int rod,
00431 long dspStart, long numWords, long dspNumber, unsigned long &length, bool usePrim = true);
00432
00434
00440 int dspBlockWrite(unsigned int rod,
00441 unsigned long *buffer, unsigned long dspAddress, long numWords, long dspNumber, bool usePrim = true);
00442
00444
00447 unsigned long dspSingleBlockRead(unsigned int rod,
00448 long dspStart, long dspNumber);
00449
00451
00454 void dspSingleBlockWrite(unsigned int rod,
00455 unsigned long dspAddress, unsigned long val, long dspNumber);
00456
00458
00466 unsigned long *primReadSlaveDsp(unsigned int rod,
00467 int s, int add, int words);
00468
00470
00476 void primWriteSlaveDsp(unsigned int rod,
00477 int s, int add, int numWords, unsigned long *data);
00478
00479 void writeSlaveFile(unsigned int rod, unsigned int slave,
00480 std::string fileName, unsigned long address, bool usePrim = true);
00481
00483 void startSlave(unsigned int rod, unsigned int slave);
00484
00486 boost::shared_array<unsigned long> loadFile(std::string filename, unsigned long & length);
00487
00489
00493 unsigned long readRodStatusReg(unsigned int rod,
00494 long regNumber);
00495
00497
00501 unsigned long readRodCommandReg(unsigned int rod,
00502 long regNumber);
00503
00505
00509 unsigned long dspSingleRead(unsigned int rod,
00510 const unsigned long dspAddr, long dspNumber);
00511
00513
00517 void dspSingleWrite(unsigned int rod,
00518 unsigned long dspAddr, unsigned long value, long dspNumber);
00519
00521
00527 ABCDModule *retrieveModule(UINT32 mid);
00528
00530
00534 void createDebugPrimList();
00535
00537
00541 void addDebugPrimList(unsigned long length, long index, long id, long version,
00542 unsigned long * body);
00543
00545
00549 void sendDebugPrimList(unsigned int rod);
00550
00552
00556 void sendDebugPrimListAll();
00557
00559
00567 void sendDebugSlavePrimList(unsigned int rod,
00568 unsigned int slave, bool await, bool response);
00569
00571
00574 void debugPrimListFromFile(std::string fileName);
00575
00577 void dumpDebugPrimList();
00578
00580 const std::list<RodLabel> listRods() const;
00581
00583
00584
00585
00586
00588
00595 UINT32 findModule(std::string sn);
00596
00598
00602 std::string convertToString(UINT32 mid);
00603
00605
00609 UINT32 findModule(INT32 mur, INT32 module);
00610
00612
00616 std::pair<INT32, INT32> convertToMUR(UINT32 mid);
00617
00619
00623 UINT32 findBarrelModule(INT32 barrel, INT32 row, INT32 number);
00624
00626
00630 void convertToBarrelModule(UINT32 mid, UINT32 &barrel, UINT32 &row, int &number);
00631
00633
00637 UINT32 findEndcapModule(INT32 disk, INT32 quadrant, INT32 number);
00638
00640
00644 void convertToEndcapModule(UINT32 mid, INT32 &disk, UINT32 &quadrant, UINT32 &number);
00645
00647
00652 void getABCDModules(BankType bank);
00653
00655
00660 virtual void getABCDModule(UINT32 mid, BankType bank);
00661
00663
00668 virtual boost::shared_ptr<ABCDModule> getABCDModuleRaw(unsigned int rod,
00669 UINT32 slot, BankType bank);
00670
00672
00677 virtual void setABCDModules(std::list<BankType> bank);
00678
00680
00685 virtual void setABCDModule(UINT32 mid, std::list<BankType> banks);
00686
00690 virtual void copyABCDModule(UINT32 mid, BankType from, std::list<BankType> to);
00691
00695 virtual void copyABCDModules(BankType from, std::list<BankType> to);
00696
00698
00708 virtual void sendAllABCDModules(BankType bank,
00709 ConfigType type = SCTAPI_CONFIG_ALL,
00710 const bool enableDataMode=true );
00711
00713
00723 virtual void sendABCDModule(UINT32 mid,
00724 BankType bank,
00725 ConfigType type = SCTAPI_CONFIG_ALL,
00726 const bool enableDataMode=true );
00727
00729
00735 void modifyABCDMask(UINT32 mid, UINT32* mask);
00736
00740
00741
00748 void modifyABCDTrims(UINT32 mid, UINT8* trims);
00749
00753
00754
00758 void setAutoUpdateBanks(std::list<BankType> banks);
00759
00761
00765 void modifyABCDVar(UINT32 typ, FLOAT32 var);
00766
00768
00772 void modifyABCDVar(UINT32 mid, UINT32 typ, FLOAT32 var);
00773
00775
00776
00778
00782 void modifyABCDVar(UINT32 mid, UINT32 chip, UINT32 typ, FLOAT32 var);
00783
00787 void modifyABCDVarAllModulesOneChip(UINT32 chip, UINT32 typ, FLOAT32 var);
00788
00790
00794 void modifyABCDVarROD(UINT32 mid, UINT32 chip, UINT32 typ, FLOAT32 var, std::list<BankType> banks);
00795
00797
00801 void modifyABCDVarROD(UINT32 mid, UINT32 typ, FLOAT32 var, std::list<BankType> banks);
00802
00804
00808 void modifyABCDVarROD(UINT32 type, FLOAT32 value, std::list<BankType> banks);
00809
00811
00812
00814
00818 void modifyABCDRC(UINT32 mid, UINT32 chip, UINT16 function, FLOAT32 p0, FLOAT32 p1, FLOAT32 p2);
00819
00820
00824 void modifyTIMParam(unsigned int param, unsigned int value);
00825
00826
00828
00832 void modifyBOCParam(unsigned int rod,
00833 unsigned int channel, unsigned int type, unsigned int val);
00834
00836
00840 void modifyBOCParam(unsigned int type, unsigned int val);
00841
00843
00847 void printBOCSetup(unsigned int rod);
00848
00850
00854 std::vector<SctConfiguration::BOCChannelConfig> currentBOCSetup(unsigned int rod);
00855
00857
00861 void printBOCRegisters(unsigned int rod);
00862
00864
00868 SctConfiguration::BOCGlobalConfig currentBOCRegisters(unsigned int rod);
00869
00871 void saveBOCSetup(unsigned int rod, BankType bank);
00872
00874 void saveBOCRegisters(unsigned int rod, BankType bank);
00875
00877 void restoreBOCSetup(unsigned int rod, BankType bank);
00878
00880 void restoreBOCRegisters(unsigned int rod, BankType bank);
00881
00883
00887 void lasersOff();
00888
00890 void setErrorMasks();
00891
00892
00893
00895
00902 void timSetFrequency(double trigFreq, double rstFreq);
00903
00905 double timGetTriggerFrequency();
00906
00908 double timGetResetFrequency();
00909
00911
00916 void freeTriggers();
00917
00919
00923 void stopTriggers();
00924
00926
00930 void timL1A();
00931
00933
00937 void timCalL1A(int delay);
00938
00940
00944 void timSoftReset();
00945
00947
00951 void timBCReset();
00952
00954 void timLoadSequence(const std::vector<unsigned>& seq);
00955
00957 unsigned long timRunSequence(unsigned long ntrigs, int random, bool external=false);
00958
00960 unsigned long sendTimBurst(unsigned long count, int random, bool external=false);
00961
00963 void timVerbose();
00964
00966
00969 void timWriteRegister(int reg, UINT16 val);
00970
00972
00975 UINT16 timReadRegister(int reg);
00976
00978
00982 std::string status();
00983
00985
00989 void defaultScan(int type);
00990
00992
00998 void doScan(boost::shared_ptr<const Scan> scan);
00999
01001
01007 boost::shared_ptr<ScanMonitor> startAsyncScan(boost::shared_ptr<const Scan> scan);
01008
01010
01016 boost::shared_ptr<ScanControl> startScan(boost::shared_ptr<const Scan> scan, bool pollTriggers);
01017
01022 void SctApi::validateScan(boost::shared_ptr<const Scan> scan);
01023
01025 bool hasFinishedScanning();
01026
01027 const std::string & nameOfScanStatusObject();
01028
01032 void awaitScan();
01033
01043 void abortScan();
01044
01046
01050 void doRawScan(boost::shared_ptr<Scan> scan, int delay, int width,
01051 bool configureModules = false, bool clkBy2 = false);
01052
01054
01058 void sendRodTrigger(unsigned int rod, const Trigger *trig, long unsigned count, int scan_point=0);
01059
01064 unsigned long sendTriggers(const Trigger* trig, unsigned long count, int scan_point=0);
01065
01067
01073 void printABCDModule(int mid);
01074
01076
01083 void printABCDRodModule(int mid, BankType bank);
01084
01086
01090 void decodeEvent(unsigned int rod,
01091 int sl, int index, bool extFlag = false, bool errorType = false);
01092
01094
01098 void scanEvents(unsigned int rod, int sl, bool extFlag, bool errorType);
01099
01103 void reportEventErrors();
01104
01108 void decodeConfig(unsigned int rod,
01109 bool skipTrim = false, bool bypass = false);
01110
01112
01116 void rawData(unsigned int rod, int delay, int units, bool setMask = true, const Trigger *trig = 0);
01117
01119
01125 void sendL1A(unsigned int rod, bool capture = false);
01126
01128
01145 std::vector<char> probe(unsigned int rod,
01146 signed int harness = -1);
01147
01149
01167 std::vector<char> probeWithTrigger(unsigned int rod,
01168 const Trigger *trigger, signed int harness = -1);
01169
01171 std::vector<std::vector<char> > SctApi::probeScan(unsigned int rod,
01172 boost::shared_ptr<Scan> scan, signed int harness = -1);
01173
01180 bool checkAllModulesProbe(std::string value);
01181
01187 void autoConfigure();
01188
01192 void bocHistogram(unsigned int rod,
01193 unsigned int samples, unsigned int numLoops);
01194
01195
01196
01197 SctApiDDC * getSctApiDDC() const;
01198
01209 void tidyHistogramming();
01210
01214 long getRodSlot(unsigned int rod);
01215
01219 int getRodRevision(unsigned int rod);
01220
01224 int getRodRevision(const RodLabel &label);
01225
01229 void testLinkOutSelect(unsigned int rod,
01230 unsigned int link);
01231
01235 unsigned int testLinkOutRetrieve(unsigned int rod);
01236
01244 void unsetDebugOption(std::string opt);
01245
01253 void setDebugOption(std::string opt);
01254
01262 std::list<std::string> listEnabledDebugOptions() const;
01263
01271 std::vector<std::string> listDebugOptions() const;
01272
01276 #warning "Call this unset scan busy?"
01277 void unsetBusy();
01278
01282 void debugStepHistogram();
01283
01289 void debugContinueHistogram();
01290
01297 void debugAbortHistogram();
01298
01304 void eventTrapSetup(int slaveMask, int trapMatch, int trapMod, int trapRemain, bool tim, bool error,
01305 boost::shared_ptr<PrimListWrapper> primList);
01306
01308 void startEventTrap(int slave, boost::shared_ptr<PrimListWrapper> primList);
01309
01311 void stopEventTrap(int slave, boost::shared_ptr<PrimListWrapper> primList);
01312
01314 void rodMode(unsigned int rod,
01315 int mode, int flag, int fifoSetup, int nBins, int delay, int message);
01316
01318 void setupModuleMask(unsigned int rod,
01319 int port, int slvs);
01320
01322 void setupModuleMask(int port, int slvs, boost::shared_ptr<PrimListWrapper> list);
01323
01325 std::pair<UINT32, UINT32> sendData(unsigned int rod, int type, int dsp);
01326
01330 void resumePolling();
01331
01335 void stopPolling();
01336
01338 void print_calib(unsigned int rod);
01339
01341
01346 int synchSendPrimListAll(boost::shared_ptr<PrimListWrapper> primList, int timeout = 10);
01347
01349 void stopHistogramming(const ScanEx &ex);
01350
01352 void setSlaveBlock(unsigned int rod,
01353 int s, long sAdd, int words, long value = 0, bool usePrim = true);
01354
01356 void writeRODRegister(unsigned int rod,
01357 int reg, int off, int width, int value);
01358
01360 unsigned int readRODRegister(unsigned int rod, int r);
01361
01363 int pollRegister(unsigned int rod, int r, int off, int width, int val, int timeout = 1000000);
01364
01365 virtual unsigned long *readFifo(unsigned int rod, int id, int bank, int elems);
01366
01368 void startHistogramTask(const Scan &scan, const ScanEx &ex, const RodLabel,
01369 unsigned int startBin, unsigned int nBins, unsigned int nTrigs,
01370 boost::shared_ptr<PrimListWrapper> list);
01371
01373
01378 void sendPrimList(unsigned int rod,
01379 boost::shared_ptr<PrimListWrapper> prim);
01380
01382
01387 int synchSendPrimList(unsigned int rod, boost::shared_ptr<PrimListWrapper> primList, int timeout = 10);
01388
01389 MRSStream *mrs;
01390
01392 SctApiConfigCache& getModuleConfigCache();
01393 #if USE_IS
01394 boost::shared_ptr<ISInfoDictionary> m_isDict;
01395 #endif
01396
01397 unsigned preloadModuleConfigurations();
01398 volatile bool m_preloadModuleConfigurationsStop;
01399 protected:
01404 virtual void really_setABCDModule(UINT32 mid, std::list<BankType> banks, UINT32 api_cfg_bits);
01405
01410 virtual void really_getABCDModule(UINT32 mid, BankType bank);
01411
01412
01413
01414
01416
01421 virtual int initialiseRod(unsigned int rod);
01422
01424
01430 virtual int initialiseModule(std::string module, bool toRod=true);
01431
01433 ABCDModule *lookupConfig(UINT32 mid);
01434
01436
01440 virtual bool preScanHardwareCheck(const Scan& scan, ScanEx& extra);
01441
01442 virtual void doHistogramSetup(const Scan &scan, const ScanEx &extra);
01443
01448 UINT32 *readEventBuffer(unsigned int rod,
01449 int sl, bool extFlag, bool errorType);
01450
01454 int getLastEventFrame(unsigned int rod, int slave);
01455
01457
01466 virtual int pollHistogramming(boost::shared_ptr<ScanControl> controller, int timeout = 5);
01467
01468 void scanLoop();
01469
01470 void scanPollingThread();
01471
01472 void addRodToList(RodLabel newRod);
01473
01474 RodInfo &SctApi::getRodInfo(const RodLabel &l);
01475
01477 std::list<RodLabel> rodList;
01478
01480 std::map<RodLabel, RodInfo> rodInfoList;
01481 boost::recursive_mutex rodInfoListAccess;
01482
01484 std::map<UINT32, ABCDModule> moduleMap;
01485
01487 boost::mutex moduleMapAccess;
01488
01489
01490 boost::shared_ptr<ScanControl> scanController;
01491
01492
01493 boost::shared_ptr<ScanControl> lastScanController;
01494
01495 private:
01496
01497
01498
01499
01500
01501
01502 unsigned int prt() const;
01503 unsigned int crt() const;
01504 Sct::URID localRod(const Sct::RodIdentifier rod) const;
01505
01507
01509 void doRawScanThreadInnards(boost::shared_ptr<Scan> scan, int delay, int width,
01510 bool configureModules, bool clkBy2);
01512
01514 void doRawScanThread(boost::shared_ptr<Scan> scan, int delay, int width,
01515 bool configureModules, bool clkBy2);
01516
01517
01519
01525 void sendSlavePrimList(unsigned int rod,
01526 boost::shared_ptr<PrimListWrapper> prim, unsigned int slave, bool await, bool response);
01527
01529
01534 void sendPrimListAll(boost::shared_ptr<PrimListWrapper> prim);
01535
01537 void setupEventTrapping(const Scan &scan, const ScanEx &ex, const RodLabel, boost::shared_ptr<PrimListWrapper> list);
01538
01540 void startEventTrapping(const Scan &scan, const ScanEx &ex, const RodLabel, boost::shared_ptr<PrimListWrapper> primList);
01541
01543 void setupEventDump(const Scan &scan, const ScanEx &ex, const RodLabel, boost::shared_ptr<PrimListWrapper> list);
01544
01546 void setupHistogramming(const Scan &scan, const ScanEx &ex, const RodLabel, bool tim,
01547 boost::shared_ptr<PrimListWrapper> list);
01548
01550 void calib_init();
01551
01553 void readRawData(unsigned int rod,
01554 int delay, bool setMask);
01555
01557 virtual int setupRawData(unsigned int rod,
01558 int delay, int units, bool setMask, const Trigger *trig = 0);
01559
01561
01565 Crate *getCrate() const;
01566
01568 void cacheModuleConfig(UINT32 mid, std::string name="");
01569
01571
01574 void getrpcrc(UINT32 mid, unsigned int &rpartition, unsigned int &rcrate, unsigned int &rrod, unsigned &rchannel);
01575
01577 void setupScanMasks(ScanEx &extra, int distSlave, bool dual);
01578
01580 bool checkModuleListsForScan();
01581
01583
01586 void preScanModuleSetup(const Scan &scan);
01587
01591 void insertISStats();
01592
01596 void updateISStats();
01597
01598
01599 public:
01600 bool checkDebugOption(std::string opt) const;
01601
01602 bool checkDebugOption(int opt) const;
01603 private:
01604
01605
01606
01608 boost::shared_ptr<Idiosyncrasy> m_id;
01609
01611 boost::shared_ptr<SctConfiguration::Configuration> config;
01612
01613 boost::shared_ptr<DCSAccess> m_dcsAccess;
01614 boost::shared_ptr<SctApiDDC> m_sctApiDDC;
01615
01617 boost::shared_ptr<PrimListWrapper> debugPrimList;
01618
01620
01621 boost::shared_ptr<Crate> crateObject;
01622
01624 boost::shared_ptr<Log> m_log;
01625 Log & log() { return *m_log; };
01626
01628 boost::shared_ptr<const Scan> lastDebugScan;
01629 boost::shared_ptr<const ScanEx> lastDebugScanEx;
01630
01632 UINT32 scanNumber;
01633
01635 UINT32 runNumber;
01636
01638 RunType currentRunMode;
01639
01641 std::list<BankType> m_autoUpdateBanks;
01642
01643 #if USE_SCAN_THREAD
01644 boost::condition scanQueue_notEmpty;
01645 boost::mutex scanQueue_mutex;
01646
01647
01648 std::auto_ptr<boost::thread> scanPollThread;
01649
01650 bool m_stopPolling;
01651 bool m_inScanLoop;
01652 bool m_inRawScanLoop;
01653 #endif
01654
01656 SctApiConfigCache module_cache;
01657
01658
01659 };
01660 }
01661 #endif