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
00186 void initialiseAll(int run);
00187
00192 bool isInitialised();
00193
00195
00202 void shutdownAll();
00203
00205
00208 void setRunNumber(UINT32 newRun);
00209
00211
00214 void setScanNumber(UINT32 newScan);
00215
00217
00220 UINT32 getRunNumber();
00221
00223
00226 UINT32 getScanNumber();
00227
00229
00233 bool isRODPresent(unsigned int rod);
00234
00238 bool changeRunMode(enum RunType mode);
00239
00243 bool changeEventFormat(enum EventFormatType format);
00244
00246
00253 void startupModules();
00254
00256 ModuleList getModuleList();
00257
00258
00259
00261
00265 void loadConfiguration();
00266
00268
00272 void loadConfiguration(std::string filename);
00273
00275
00278 int loadModuleConfigurations();
00279
00281
00285 void storeModuleConfigurations(BankType bank);
00286
00288
00292 void storeBOCConfigurations();
00293
00295
00299 void configureBOC(unsigned int rod);
00300
00302
00306 std::vector<double> getBOCMonitorArray(unsigned int rod);
00307
00309
00313 int awaitResponse(unsigned int rod, int timeout);
00314
00316
00320 int awaitResponseAll(int timeout);
00321
00323
00328 unsigned long *getResponse(unsigned int rod,
00329 unsigned long &length);
00330
00336 bool getRodMessage(unsigned int rod,
00337 char *buffer, unsigned long &length);
00338
00339
00340
00342
00352 void flashLED(unsigned int rod,
00353 long slaveNumber, long period = 1000, long flashes = 10);
00354
00356
00360 void echo(unsigned int rod,
00361 unsigned int length, const unsigned long *data);
00362
00364
00368 void echoAll(unsigned int length, const unsigned long *data);
00369
00371
00375 void echoSlave(unsigned int rod,
00376 unsigned int slave, unsigned int length, const unsigned long *data);
00377
00378
00379
00380
00381
00383
00392 int dspBlockDump(unsigned int rod,
00393 long dspStart, long numWords, long dspNumber, bool usePrim = true);
00394
00396
00406 int dspBlockDumpFile(unsigned int rod,
00407 long dspStart, long numWords, long dspNumber, std::string filename, bool usePrim = true);
00408
00410
00415 void standardRegisterDump(RodLabel rl);
00416
00418
00421 void standardRegisterDump(unsigned int rod);
00422
00424
00428 void standardRegisterDumpAll();
00429
00431
00437 unsigned long *dspBlockRead(unsigned int rod,
00438 long dspStart, long numWords, long dspNumber, unsigned long &length, bool usePrim = true);
00439
00441
00447 int dspBlockWrite(unsigned int rod,
00448 unsigned long *buffer, unsigned long dspAddress, long numWords, long dspNumber, bool usePrim = true);
00449
00451
00454 unsigned long dspSingleBlockRead(unsigned int rod,
00455 long dspStart, long dspNumber);
00456
00458
00461 void dspSingleBlockWrite(unsigned int rod,
00462 unsigned long dspAddress, unsigned long val, long dspNumber);
00463
00465
00473 unsigned long *primReadSlaveDsp(unsigned int rod,
00474 int s, int add, int words);
00475
00477
00483 void primWriteSlaveDsp(unsigned int rod,
00484 int s, int add, int numWords, unsigned long *data);
00485
00486 void writeSlaveFile(unsigned int rod, unsigned int slave,
00487 std::string fileName, unsigned long address, bool usePrim = true);
00488
00490 void startSlave(unsigned int rod, unsigned int slave);
00491
00493 boost::shared_array<unsigned long> loadFile(std::string filename, unsigned long & length);
00494
00496
00500 unsigned long readRodStatusReg(unsigned int rod,
00501 long regNumber);
00502
00504
00508 unsigned long readRodCommandReg(unsigned int rod,
00509 long regNumber);
00510
00512
00516 unsigned long dspSingleRead(unsigned int rod,
00517 const unsigned long dspAddr, long dspNumber);
00518
00520
00524 void dspSingleWrite(unsigned int rod,
00525 unsigned long dspAddr, unsigned long value, long dspNumber);
00526
00528
00534 ABCDModule *retrieveModule(UINT32 mid);
00535
00537
00541 void createDebugPrimList();
00542
00544
00548 void addDebugPrimList(unsigned long length, long index, long id, long version,
00549 unsigned long * body);
00550
00552
00556 void sendDebugPrimList(unsigned int rod);
00557
00559
00563 void sendDebugPrimListAll();
00564
00566
00574 void sendDebugSlavePrimList(unsigned int rod,
00575 unsigned int slave, bool await, bool response);
00576
00578
00581 void debugPrimListFromFile(std::string fileName);
00582
00584 void dumpDebugPrimList();
00585
00587 const std::list<RodLabel> listRods() const;
00588
00590
00591
00592
00593
00595
00602 UINT32 findModule(std::string sn);
00603
00605
00609 std::string convertToString(UINT32 mid);
00610
00612
00616 UINT32 findModule(INT32 mur, INT32 module);
00617
00619
00623 std::pair<INT32, INT32> convertToMUR(UINT32 mid);
00624
00626
00630 UINT32 findBarrelModule(INT32 barrel, INT32 row, INT32 number);
00631
00633
00637 void convertToBarrelModule(UINT32 mid, UINT32 &barrel, UINT32 &row, int &number);
00638
00640
00644 UINT32 findEndcapModule(INT32 disk, INT32 quadrant, INT32 number);
00645
00647
00651 void convertToEndcapModule(UINT32 mid, INT32 &disk, UINT32 &quadrant, UINT32 &number);
00652
00654
00659 void getABCDModules(BankType bank);
00660
00662
00667 virtual void getABCDModule(UINT32 mid, BankType bank);
00668
00670
00675 virtual boost::shared_ptr<ABCDModule> getABCDModuleRaw(unsigned int rod,
00676 UINT32 slot, BankType bank);
00677
00679
00684 virtual void setABCDModules(std::list<BankType> bank);
00685
00687
00692 virtual void setABCDModule(UINT32 mid, std::list<BankType> banks);
00693
00697 virtual void copyABCDModule(UINT32 mid, BankType from, std::list<BankType> to);
00698
00702 virtual void copyABCDModules(BankType from, std::list<BankType> to);
00703
00705
00715 virtual void sendAllABCDModules(BankType bank,
00716 ConfigType type = SCTAPI_CONFIG_ALL,
00717 const bool enableDataMode=true );
00718
00720
00730 virtual void sendABCDModule(UINT32 mid,
00731 BankType bank,
00732 ConfigType type = SCTAPI_CONFIG_ALL,
00733 const bool enableDataMode=true );
00734
00736
00742 void modifyABCDMask(UINT32 mid, UINT32* mask);
00743
00747
00748
00755 void modifyABCDTrims(UINT32 mid, UINT8* trims);
00756
00760
00761
00765 void setAutoUpdateBanks(std::list<BankType> banks);
00766
00768
00772 void modifyABCDVar(UINT32 typ, FLOAT32 var);
00773
00775
00779 void modifyABCDVar(UINT32 mid, UINT32 typ, FLOAT32 var);
00780
00782
00783
00785
00789 void modifyABCDVar(UINT32 mid, UINT32 chip, UINT32 typ, FLOAT32 var);
00790
00794 void modifyABCDVarAllModulesOneChip(UINT32 chip, UINT32 typ, FLOAT32 var);
00795
00797
00801 void modifyABCDVarROD(UINT32 mid, UINT32 chip, UINT32 typ, FLOAT32 var, std::list<BankType> banks);
00802
00804
00808 void modifyABCDVarROD(UINT32 mid, UINT32 typ, FLOAT32 var, std::list<BankType> banks);
00809
00811
00815 void modifyABCDVarROD(UINT32 type, FLOAT32 value, std::list<BankType> banks);
00816
00818
00819
00821
00825 void modifyABCDRC(UINT32 mid, UINT32 chip, UINT16 function, FLOAT32 p0, FLOAT32 p1, FLOAT32 p2);
00826
00827
00831 void modifyTIMParam(unsigned int param, unsigned int value);
00832
00833
00835
00839 void modifyBOCParam(unsigned int rod,
00840 unsigned int channel, unsigned int type, unsigned int val);
00841
00843
00847 void modifyBOCParam(unsigned int type, unsigned int val);
00848
00850
00854 void printBOCSetup(unsigned int rod);
00855
00857
00861 std::vector<SctConfiguration::BOCChannelConfig> currentBOCSetup(unsigned int rod);
00862
00864
00868 void printBOCRegisters(unsigned int rod);
00869
00871
00875 SctConfiguration::BOCGlobalConfig currentBOCRegisters(unsigned int rod);
00876
00878 void saveBOCSetup(unsigned int rod, BankType bank);
00879
00881 void saveBOCRegisters(unsigned int rod, BankType bank);
00882
00884 void restoreBOCSetup(unsigned int rod, BankType bank);
00885
00887 void restoreBOCRegisters(unsigned int rod, BankType bank);
00888
00890
00894 void lasersOff();
00895
00897 void setErrorMasks();
00898
00899
00900
00902
00909 void timSetFrequency(double trigFreq, double rstFreq);
00910
00912 double timGetTriggerFrequency();
00913
00915 double timGetResetFrequency();
00916
00918
00923 void freeTriggers();
00924
00926
00930 void stopTriggers();
00931
00933
00937 void timL1A();
00938
00940
00944 void timCalL1A(int delay);
00945
00947
00951 void timSoftReset();
00952
00954
00958 void timBCReset();
00959
00961 void timLoadSequence(const std::vector<unsigned>& seq);
00962
00964 unsigned long timRunSequence(unsigned long ntrigs, int random, bool external=false);
00965
00967 unsigned long sendTimBurst(unsigned long count, int random, bool external=false);
00968
00970 void timVerbose();
00971
00973
00976 void timWriteRegister(int reg, UINT16 val);
00977
00979
00982 UINT16 timReadRegister(int reg);
00983
00985
00989 std::string status();
00990
00992
00996 void defaultScan(int type);
00997
00999
01005 void doScan(boost::shared_ptr<const Scan> scan);
01006
01008
01014 boost::shared_ptr<ScanMonitor> startAsyncScan(boost::shared_ptr<const Scan> scan);
01015
01017
01023 boost::shared_ptr<ScanControl> startScan(boost::shared_ptr<const Scan> scan, bool pollTriggers);
01024
01029 void SctApi::validateScan(boost::shared_ptr<const Scan> scan);
01030
01032 bool hasFinishedScanning();
01033
01034 const std::string & nameOfScanStatusObject();
01035
01039 void awaitScan();
01040
01050 void abortScan();
01051
01053
01057 void doRawScan(boost::shared_ptr<Scan> scan, int delay, int width,
01058 bool configureModules = false, bool clkBy2 = false);
01059
01061
01065 void sendRodTrigger(unsigned int rod, const Trigger *trig, long unsigned count, int scan_point=0);
01066
01071 unsigned long sendTriggers(const Trigger* trig, unsigned long count, int scan_point=0);
01072
01074
01080 void printABCDModule(int mid);
01081
01083
01090 void printABCDRodModule(int mid, BankType bank);
01091
01093
01097 void decodeEvent(unsigned int rod,
01098 int sl, int index, bool extFlag = false, bool errorType = false);
01099
01101
01105 void scanEvents(unsigned int rod, int sl, bool extFlag, bool errorType);
01106
01110 void reportEventErrors();
01111
01115 void decodeConfig(unsigned int rod,
01116 bool skipTrim = false, bool bypass = false);
01117
01119
01123 void rawData(unsigned int rod, int delay, int units, bool setMask = true, const Trigger *trig = 0);
01124
01126
01132 void sendL1A(unsigned int rod, bool capture = false);
01133
01135
01152 std::vector<char> probe(unsigned int rod,
01153 signed int harness = -1);
01154
01156
01174 std::vector<char> probeWithTrigger(unsigned int rod,
01175 const Trigger *trigger, signed int harness = -1);
01176
01178 std::vector<std::vector<char> > SctApi::probeScan(unsigned int rod,
01179 boost::shared_ptr<Scan> scan, signed int harness = -1);
01180
01187 bool checkAllModulesProbe(std::string value);
01188
01194 void autoConfigure();
01195
01199 void bocHistogram(unsigned int rod,
01200 unsigned int samples, unsigned int numLoops);
01201
01202
01203
01204 SctApiDDC * getSctApiDDC() const;
01205
01216 void tidyHistogramming();
01217
01221 long getRodSlot(unsigned int rod);
01222
01226 int getRodRevision(unsigned int rod);
01227
01231 int getRodRevision(const RodLabel &label);
01232
01236 void testLinkOutSelect(unsigned int rod,
01237 unsigned int link);
01238
01242 unsigned int testLinkOutRetrieve(unsigned int rod);
01243
01251 void unsetDebugOption(std::string opt);
01252
01260 void setDebugOption(std::string opt);
01261
01269 std::list<std::string> listEnabledDebugOptions() const;
01270
01278 std::vector<std::string> listDebugOptions() const;
01279
01283 #warning "Call this unset scan busy?"
01284 void unsetBusy();
01285
01289 void debugStepHistogram();
01290
01296 void debugContinueHistogram();
01297
01304 void debugAbortHistogram();
01305
01311 void eventTrapSetup(int slaveMask, int trapMatch, int trapMod, int trapRemain, bool tim, bool error,
01312 boost::shared_ptr<PrimListWrapper> primList);
01313
01315 void startEventTrap(int slave, boost::shared_ptr<PrimListWrapper> primList);
01316
01318 void stopEventTrap(int slave, boost::shared_ptr<PrimListWrapper> primList);
01319
01321 void rodMode(unsigned int rod,
01322 int mode, int flag, int fifoSetup, int nBins, int delay, int message);
01323
01325 void setupModuleMask(unsigned int rod,
01326 int port, int slvs);
01327
01329 void setupModuleMask(int port, int slvs, boost::shared_ptr<PrimListWrapper> list);
01330
01332 std::pair<UINT32, UINT32> sendData(unsigned int rod, int type, int dsp);
01333
01337 void resumePolling();
01338
01342 void stopPolling();
01343
01345 void print_calib(unsigned int rod);
01346
01348
01353 int synchSendPrimListAll(boost::shared_ptr<PrimListWrapper> primList, int timeout = 10);
01354
01356 void stopHistogramming(const ScanEx &ex);
01357
01359 void setSlaveBlock(unsigned int rod,
01360 int s, long sAdd, int words, long value = 0, bool usePrim = true);
01361
01363 void writeRODRegister(unsigned int rod,
01364 int reg, int off, int width, int value);
01365
01367 unsigned int readRODRegister(unsigned int rod, int r);
01368
01370 int pollRegister(unsigned int rod, int r, int off, int width, int val, int timeout = 1000000);
01371
01372 virtual unsigned long *readFifo(unsigned int rod, int id, int bank, int elems);
01373
01375 void startHistogramTask(const Scan &scan, const ScanEx &ex, const RodLabel,
01376 unsigned int startBin, unsigned int nBins, unsigned int nTrigs,
01377 boost::shared_ptr<PrimListWrapper> list);
01378
01380
01385 void sendPrimList(unsigned int rod,
01386 boost::shared_ptr<PrimListWrapper> prim);
01387
01389
01394 int synchSendPrimList(unsigned int rod, boost::shared_ptr<PrimListWrapper> primList, int timeout = 10);
01395
01396 MRSStream *mrs;
01397
01399 SctApiConfigCache& getModuleConfigCache();
01400 #if USE_IS
01401 boost::shared_ptr<ISInfoDictionary> m_isDict;
01402 #endif
01403
01404 unsigned preloadModuleConfigurations();
01405 volatile bool m_preloadModuleConfigurationsStop;
01406 protected:
01411 virtual void really_setABCDModule(UINT32 mid, std::list<BankType> banks, UINT32 api_cfg_bits);
01412
01417 virtual void really_getABCDModule(UINT32 mid, BankType bank);
01418
01419
01420
01421
01423
01428 virtual int initialiseRod(unsigned int rod);
01429
01431
01437 virtual int initialiseModule(std::string module, bool toRod=true);
01438
01440 ABCDModule *lookupConfig(UINT32 mid);
01441
01443
01447 virtual bool preScanHardwareCheck(const Scan& scan, ScanEx& extra);
01448
01449 virtual void doHistogramSetup(const Scan &scan, const ScanEx &extra);
01450
01455 UINT32 *readEventBuffer(unsigned int rod,
01456 int sl, bool extFlag, bool errorType);
01457
01461 int getLastEventFrame(unsigned int rod, int slave);
01462
01464
01473 virtual int pollHistogramming(boost::shared_ptr<ScanControl> controller, int timeout = 5);
01474
01475 void scanLoop();
01476
01477 void scanPollingThread();
01478
01479 void initialisationThread();
01480
01481 void addRodToList(RodLabel newRod);
01482
01483 RodInfo &SctApi::getRodInfo(const RodLabel &l);
01484
01486 std::list<RodLabel> rodList;
01487
01489 std::map<RodLabel, RodInfo> rodInfoList;
01490 boost::recursive_mutex rodInfoListAccess;
01491
01493 std::map<UINT32, ABCDModule> moduleMap;
01494
01496 boost::mutex moduleMapAccess;
01497
01498
01499 boost::shared_ptr<ScanControl> scanController;
01500
01501
01502 boost::shared_ptr<ScanControl> lastScanController;
01503
01504 private:
01505
01506
01507
01508
01509
01510
01511 unsigned int prt() const;
01512 unsigned int crt() const;
01513 Sct::URID localRod(const Sct::RodIdentifier rod) const;
01514
01516
01518 void doRawScanThreadInnards(boost::shared_ptr<Scan> scan, int delay, int width,
01519 bool configureModules, bool clkBy2);
01521
01523 void doRawScanThread(boost::shared_ptr<Scan> scan, int delay, int width,
01524 bool configureModules, bool clkBy2);
01525
01526
01528
01534 void sendSlavePrimList(unsigned int rod,
01535 boost::shared_ptr<PrimListWrapper> prim, unsigned int slave, bool await, bool response);
01536
01538
01543 void sendPrimListAll(boost::shared_ptr<PrimListWrapper> prim);
01544
01546 void setupEventTrapping(const Scan &scan, const ScanEx &ex, const RodLabel, boost::shared_ptr<PrimListWrapper> list);
01547
01549 void startEventTrapping(const Scan &scan, const ScanEx &ex, const RodLabel, boost::shared_ptr<PrimListWrapper> primList);
01550
01552 void setupEventDump(const Scan &scan, const ScanEx &ex, const RodLabel, boost::shared_ptr<PrimListWrapper> list);
01553
01555 void setupHistogramming(const Scan &scan, const ScanEx &ex, const RodLabel, bool tim,
01556 boost::shared_ptr<PrimListWrapper> list);
01557
01559 void calib_init();
01560
01562 void readRawData(unsigned int rod,
01563 int delay, bool setMask);
01564
01566 virtual int setupRawData(unsigned int rod,
01567 int delay, int units, bool setMask, const Trigger *trig = 0);
01568
01570
01574 Crate *getCrate() const;
01575
01577 void cacheModuleConfig(UINT32 mid, std::string name="");
01578
01580
01583 void getrpcrc(UINT32 mid, unsigned int &rpartition, unsigned int &rcrate, unsigned int &rrod, unsigned &rchannel);
01584
01586 void setupScanMasks(ScanEx &extra, int distSlave, bool dual);
01587
01589 bool checkModuleListsForScan();
01590
01592
01595 void preScanModuleSetup(const Scan &scan);
01596
01600 void insertISStats();
01601
01605 void updateISStats();
01606
01607
01608 public:
01609 bool checkDebugOption(std::string opt) const;
01610
01611 bool checkDebugOption(int opt) const;
01612 private:
01613
01614
01615
01617 boost::shared_ptr<Idiosyncrasy> m_id;
01618
01620 boost::shared_ptr<SctConfiguration::Configuration> config;
01621
01622 boost::shared_ptr<DCSAccess> m_dcsAccess;
01623 boost::shared_ptr<SctApiDDC> m_sctApiDDC;
01624
01626 boost::shared_ptr<PrimListWrapper> debugPrimList;
01627
01629
01630 boost::shared_ptr<Crate> crateObject;
01631
01633 boost::shared_ptr<Log> m_log;
01634 Log & log() { return *m_log; };
01635
01637 boost::shared_ptr<const Scan> lastDebugScan;
01638 boost::shared_ptr<const ScanEx> lastDebugScanEx;
01639
01641 UINT32 scanNumber;
01642
01644 UINT32 runNumber;
01645
01647 RunType currentRunMode;
01648
01650 std::list<BankType> m_autoUpdateBanks;
01651
01652 #if USE_SCAN_THREAD
01653 boost::condition scanQueue_notEmpty;
01654 boost::mutex scanQueue_mutex;
01655
01656
01657 std::auto_ptr<boost::thread> scanPollThread;
01658
01659 bool m_stopPolling;
01660 bool m_inScanLoop;
01661 bool m_inRawScanLoop;
01662
01666 std::auto_ptr<boost::thread> m_initialisationThread;
01667 bool m_is_initialised;
01668
01669 #endif
01670
01672 SctApiConfigCache module_cache;
01673
01674
01675 };
01676 }
01677 #endif