00001
00002
00003 #ifndef SCTROD_SCTAPI_H
00004 #define SCTROD_SCTAPI_H
00005
00006 #include <list>
00007 #include <map>
00008 #include <string>
00009 #include <vector>
00010
00011 #include <boost/thread.hpp>
00012 #include <boost/shared_ptr.hpp>
00013
00014 #include <mrs/message.h>
00015 #if USE_IS
00016 #include <is/isinfotmpl.h>
00017 #endif
00018
00019 #include "sctddc/SCTDCSCommand.hxx"
00020 #include "log.h"
00021
00022
00023 #include <CommonWithDsp/sctStructure.h>
00024
00025 #include <CommonWithDsp/processor.h>
00026
00027 #warning "Should some of these return smart pointers?"
00028
00029 struct scan_result_ptrs;
00030
00031 namespace SctConfiguration {
00032 class Configuration;
00033 }
00034
00035 namespace SctApi {
00036 class Crate;
00037
00038 class PrimListWrapper;
00039
00041 class Trigger {
00042 public:
00043 virtual ~Trigger();
00044
00046 typedef std::vector<std::pair<UINT16, UINT32> > RODTriggers;
00047
00049 virtual void singleL1A() = 0;
00051 virtual void doubleL1A(unsigned short delay) = 0;
00053 virtual void calL1A(unsigned short delay) = 0;
00054 virtual void pulseL1A(unsigned short delay) = 0;
00055 virtual void softL1A(unsigned short delay) = 0;
00056 virtual void softCalL1A(unsigned short delay, unsigned short delay2) = 0;
00057 virtual void softPulseL1A(unsigned short delay, unsigned short delay2) = 0;
00058 virtual void bcL1A(unsigned short delay) = 0;
00059 virtual void bcCalL1A(unsigned short delay, unsigned short delay2) = 0;
00060 virtual void bcPulseL1A(unsigned short delay, unsigned short delay2) = 0;
00061
00062 virtual void setCommIncr(unsigned short command, unsigned short incr) = 0;
00063 virtual void getCommIncr(unsigned short &command, unsigned short &incr) const = 0;
00064
00066 virtual const RODTriggers getRODTriggers() const = 0;
00067
00068 virtual void print() const = 0;
00069 };
00070
00079 class Scan {
00080 public:
00081 virtual ~Scan();
00082
00084 typedef std::vector<FLOAT32> ScanPoints;
00085
00087 enum ScanOptions {FORMAT, FULL, BITS32, LOOPCALLINE, DISTSLAVE, DEBUG};
00088
00089 virtual void configure(unsigned short type, float start, float stop, float step) = 0;
00090 virtual void configure2(unsigned short type, float start, float stop, float step) = 0;
00091
00092 virtual const Trigger *getTrigger1() const = 0;
00093 virtual const Trigger *getTrigger2() const = 0;
00094
00095 virtual Trigger *getTrigger1() = 0;
00096 virtual Trigger *getTrigger2() = 0;
00097
00098 virtual void setTrigger1(Trigger *trigger) = 0;
00099 virtual void setTrigger2(Trigger *trigger) = 0;
00100
00101 virtual void setScanVariable1(unsigned short var) = 0;
00102 virtual void setScanVariable2(unsigned short var) = 0;
00103
00104 virtual unsigned short getScanVariable1() const = 0;
00105 virtual unsigned short getScanVariable2() const = 0;
00106
00107 virtual void setNTrigs(unsigned long nTrigs) = 0;
00108 virtual unsigned short getNTrigs() const = 0;
00109
00110 virtual const ScanPoints getScanPoints1() const = 0;
00111 virtual const ScanPoints getScanPoints2() const = 0;
00112 virtual void setScanPoints1(const ScanPoints &scans) = 0;
00113 virtual void setScanPoints2(const ScanPoints &scans) = 0;
00114
00115 virtual int getOption(enum ScanOptions) const = 0;
00116 virtual void setOption(enum ScanOptions, int option) = 0;
00117
00119 virtual void print() const = 0;
00120
00121 virtual void setScanNumber(unsigned int scan) = 0;
00122 virtual unsigned int getScanNumber() const = 0;
00123 virtual void setRunNumber(unsigned int scan) = 0;
00124 virtual unsigned int getRunNumber() const = 0;
00125
00126 virtual std::list<std::string> getModuleList(unsigned int group) const = 0;
00127 virtual void setModuleList(unsigned int group, std::list<std::string> newList) = 0;
00128 virtual unsigned int getNGroups() const = 0;
00129 virtual void setNGroups(unsigned int val) = 0;
00130 };
00131
00133
00136 class ScanEx {
00137 public:
00138
00139 int partition;
00140 int crate;
00141 int rod;
00142 UINT32 channels[2];
00143
00144 UINT32 dspChannels[4][2];
00145
00147 int slaves;
00148
00150 char bitFieldDSP;
00151
00153 unsigned int diagnosticReg;
00154
00156 int trimScan;
00157
00158
00159
00161 int timTrigs;
00162 };
00163
00215 class SctApi {
00216 SctApi(const SctApi &);
00217 SctApi &operator=(const SctApi&);
00218 public:
00219
00221 static const unsigned int BAD_MODULE;
00222
00226 SctApi();
00227
00231 SctApi(boost::shared_ptr<SctConfiguration::Configuration> newConf);
00232
00236 SctApi(int initDebug);
00237
00238 ~SctApi();
00239
00240 private:
00246 void setup();
00247
00248 public:
00252 void setMrsStream(MRSStream *stream);
00253
00254 #if USE_IS
00255
00258 void setIsDictionary(ISInfoDictionary *dict);
00259 #endif
00260
00261
00262
00264
00277 void initialiseAll(int run);
00278
00280
00287 void shutdownAll();
00288
00290
00293 void setRunNumber(UINT32 newRun);
00294
00296
00299 void setScanNumber(UINT32 newScan);
00300
00302
00309 void configureAllModules();
00310
00311
00312
00314
00318 void loadConfiguration();
00319
00321
00325 void loadConfiguration(std::string filename);
00326
00328
00332 void storeModuleConfigurations();
00333
00335
00339 void configureBOC(unsigned int partition, unsigned int crate, unsigned int rod);
00340
00342
00346 void awaitResponse(unsigned int partition, unsigned int crate, unsigned int rod, int timeout);
00347
00349
00353 void awaitResponseAll(unsigned int partition, unsigned int crate, int timeout);
00354
00356
00361 unsigned long *getResponse(unsigned int partition, unsigned int crate, unsigned int rod,
00362 unsigned long &length);
00363
00369 bool getRodMessage(unsigned int partition, unsigned int crate, unsigned int rod,
00370 char *buffer, int &length);
00371
00372
00373
00375
00384 void flashLED(unsigned int partition, unsigned int crate, unsigned int rod,
00385 unsigned long slaveNumber, long period = 1000, long flashes = 10);
00386
00388
00392 void echo(unsigned int partition, unsigned int crate, unsigned int rod,
00393 unsigned int length, const long *data);
00394
00395
00396
00397
00398
00400
00407 int dspBlockDump(unsigned int partition, unsigned int crate, unsigned int rod,
00408 long dspStart, long numWords, long dspNumber);
00409
00410 int dspBlockDumpFile(unsigned int partition, unsigned int crate, unsigned int rod,
00411 long dspStart, long numWords, long dspNumber, string filename);
00412
00414 void standardRegisterDump(unsigned int partition, unsigned int crate, unsigned int rod);
00415
00417
00423 unsigned long *dspBlockRead(unsigned int partition, unsigned int crate, unsigned int rod,
00424 long dspStart, long numWords, long dspNumber, long &length);
00425
00427
00433 int dspBlockWrite(unsigned int partition, unsigned int crate, unsigned int rod,
00434 unsigned long *buffer, unsigned long dspAddress, long numWords, long dspNumber);
00435
00437
00445 unsigned long *readSlaveDsp(unsigned int partition, unsigned int crate, unsigned int rod,
00446 int s, int add, int words);
00447
00449
00453 unsigned long readRodStatusReg(unsigned int partition, unsigned int crate, unsigned int rod,
00454 long regNumber);
00455
00457
00461 unsigned long readRodCommandReg(unsigned int partition, unsigned int crate, unsigned int rod,
00462 long regNumber);
00463
00465
00469 unsigned long dspSingleRead(unsigned int partition, unsigned int crate, unsigned int rod,
00470 const unsigned long dspAddr, long dspNumber);
00471
00473
00477 void dspSingleWrite(unsigned int partition, unsigned int crate, unsigned int rod,
00478 unsigned long dspAddr, unsigned long buffer, long dspNumber);
00479
00481
00487 ABCDModule *retrieveModule(UINT32 mid);
00488
00490
00494 void createDebugPrimList();
00496
00500 void addDebugPrimList(unsigned long length, long index, long id, long version,
00501 unsigned long * body);
00503
00507 void sendDebugPrimList(unsigned int partition, unsigned int crate, unsigned int rod);
00508
00510
00518 void sendDebugSlavePrimList(unsigned int partition, unsigned int crate, unsigned int rod,
00519 unsigned int slave, bool await, bool response);
00520
00521
00522
00523
00525
00532 UINT32 findModule(std::string sn);
00533
00535
00539 std::string convertToString(UINT32 mid);
00540
00542
00546 UINT32 findModule(INT32 mur, INT32 module);
00547
00549
00553 std::pair<INT32, INT32> convertToMUR(UINT32 mid);
00554
00556
00560 UINT32 findBarrelModule(INT32 barrel, INT32 row, INT32 number);
00561
00563
00567 void convertToBarrelModule(UINT32 mid, UINT32 &barrel, UINT32 &row, int &number);
00568
00570
00574 UINT32 findEndcapModule(INT32 disk, INT32 quadrant, INT32 number);
00575
00577
00581 void convertToEndcapModule(UINT32 mid, UINT32 &disk, UINT32 &quadrant, UINT32 &number);
00582
00584
00588 void getABCDModule(UINT32 bank);
00589
00591
00595 void getABCDModule(UINT32 mid, UINT32 bank);
00596
00598
00602 void setABCDModule(UINT32 mid, UINT32 bank);
00603
00605
00614 void sendAllABCDModules(UINT32 bank, UINT32 type = 2);
00615
00617
00627 void sendABCDModule(UINT32 mid, UINT32 bank, UINT32 type=2);
00628
00630
00636 void modifyABCDMask(UINT32 mid, UINT32* mask);
00637
00641
00642
00649 void modifyABCDTrims(UINT32 mid, UINT8* trims);
00650
00654
00655
00657
00661 void modifyABCDVar(UINT32 typ, FLOAT32 var);
00662
00664
00668 void modifyABCDVar(UINT32 mid, UINT32 typ, FLOAT32 var);
00669
00671
00672
00674
00678 void modifyABCDVar(UINT32 mid, UINT32 chip, UINT32 typ, FLOAT32 var);
00679
00681
00682
00684
00688 void modifyABCDRC(UINT32 mid, UINT32 chip, UINT16 function, FLOAT32 p0, FLOAT32 p1, FLOAT32 p2);
00689
00691
00695 void modifyBOCParam(unsigned int partition, unsigned int crate, unsigned int rod,
00696 unsigned int channel, unsigned int type, unsigned int val);
00697
00699
00703 void printBOCSetup(unsigned int partition, unsigned int crate, unsigned int rod);
00704
00706
00710 void printBOCRegisters(unsigned int partition, unsigned int crate, unsigned int rod);
00711
00713
00717 void lasersOff();
00718
00719
00720
00722
00728 void freeTriggers(unsigned int partition, unsigned int crate,
00729 float freq);
00730
00732
00736 void stopTriggers(unsigned int partition, unsigned int crate);
00737
00739
00743 void timL1A(unsigned int partition, unsigned int crate);
00744
00746
00750 void timCalL1A(unsigned int partition, unsigned int crate, int delay);
00751
00752
00754
00758 void status();
00759
00761
00765 void defaultScan(int type);
00766
00768
00774 void doScan(Scan &scan);
00775
00783 void abortScan();
00784
00786
00790 void doRawScan(Scan &scan, int delay, bool configure = false, bool clkBy2 = false);
00791
00793
00797 void sendTriggerAll(const Trigger *trig);
00798
00800
00804 void sendTrigger(unsigned int partition, unsigned int crate, unsigned int rod, const Trigger *trig);
00805
00807
00813 void printABCDModule(int mid);
00814
00816
00823 void printABCDRodModule(int mid, int bank);
00824
00826
00830 void decodeEvent(unsigned int partition, unsigned int crate, unsigned int rod,
00831 int sl, int index, bool extFlag = false, bool errorType = false);
00832
00833
00834 void printHex(UINT32 num, int width);
00835
00837
00841 void rawData(unsigned int partition, unsigned int crate, unsigned int rod, int delay, int units, bool setMask = true, const Trigger *trig = 0);
00842
00844
00850 void sendL1A(unsigned int partition, unsigned int crate, unsigned int rod, bool capture = false);
00851
00853
00869 vector<char> probe(unsigned int partition, unsigned int crate, unsigned int rod, signed int harness = -1);
00870
00872
00888 vector<char> probeWithTrigger(unsigned int partition, unsigned int crate, unsigned int rod, const Trigger *trigger, signed int harness = -1);
00889
00893 void bocHistogram(unsigned int partition, unsigned int crate, unsigned int rod,
00894 unsigned int samples, unsigned int numLoops);
00895
00896
00897
00905 void requestHardResetAll();
00906
00914 void requestHardReset(UINT32 mid);
00915
00923 void requestIVCurve(UINT32 mid, FLOAT32 start, FLOAT32 stop, FLOAT32 step, UINT16 delay, FLOAT32 currentLim);
00924
00932 void requestIVCurveAll(FLOAT32 start, FLOAT32 stop, FLOAT32 step, UINT16 delay, FLOAT32 currentLim);
00933
00935 void ddcGetChannelParameter(int crate, int channel, std::string dpeName,
00936 int& dpeValue, int timeout);
00937
00948
00949 void setSelect(UINT32 mid, bool value);
00950
00960 void changeRunState(int state);
00961
00972 void tidyHistogramming();
00973
00981 void setDebugLevel(unsigned int level);
00982
00990 unsigned int getDebugLevel();
00991
00992 void debugStepHistogram();
00993
00994 void debugContinueHistogram();
00995
00996 void debugAbortHistogram();
00997
00999 void rodMode(unsigned int partition, unsigned int crate, unsigned int rod,
01000 int mode, int flag, int fifoSetup, int nBins, int delay, int message);
01001
01003 void setupModuleMask(unsigned int partition, unsigned int crate, unsigned int rod,
01004 int port, int slvs);
01005
01007 void setupModuleMask(int port, int slvs, boost::shared_ptr<PrimListWrapper> list);
01008
01009 void sendData(unsigned int partition, unsigned int crate, unsigned int rod);
01010 private:
01011
01012
01014
01019 int initialiseRod(unsigned int partition, unsigned int crate, unsigned int rod);
01020
01022
01027 int initialiseTim(unsigned int partition, unsigned int crate);
01028
01030
01035 int initialiseBOC(unsigned int partition, unsigned int crate, unsigned int rod);
01036
01038 void writeSlaveBlock(unsigned int partition, unsigned int crate, unsigned int rod, int s, long sAdd, int words, long *block);
01039
01041 void setSlaveBlock(unsigned int partition, unsigned int crate, unsigned int rod, int s, long sAdd, int words, long value = 0);
01042
01044 void writeRODRegister(unsigned int partition, unsigned int crate, unsigned int rod,
01045 int reg, int off, int width, int value);
01046
01048 void writeRODRegister(int reg, int off, int width, int value, boost::shared_ptr<PrimListWrapper> list);
01049
01051 unsigned int readRODRegister(unsigned int partition, unsigned int crate, unsigned int rod, int r);
01052
01054 int pollRegister(unsigned int partition, unsigned int crate, unsigned int rod, int r, int off, int width, int val, int timeout = 1000000);
01055
01056 unsigned long *readFifo(unsigned int partition, unsigned int crate, unsigned int rod, int id, int bank, int elems);
01057
01059
01065 int initialiseModule(std::string module);
01066
01067
01068
01069
01070
01071
01073
01079 void sendSlavePrimList(unsigned int partition, unsigned int crate, unsigned int rod,
01080 boost::shared_ptr<PrimListWrapper> prim, unsigned int slave, bool await, bool response);
01081
01083
01087 void buildSlavePrimList(boost::shared_ptr<PrimListWrapper> primList, boost::shared_ptr<PrimListWrapper> prim,
01088 unsigned int slaveNumber, bool await, bool response);
01089
01091
01096 void sendPrimList(unsigned int partition, unsigned int crate, unsigned int rod,
01097 boost::shared_ptr<PrimListWrapper> prim);
01098
01100
01105 void sendPrimListAll(unsigned int partition, unsigned int crate,
01106 boost::shared_ptr<PrimListWrapper> prim);
01107
01109 void startHistogramTask(const Scan &scan, const ScanEx &ex, boost::shared_ptr<PrimListWrapper> list);
01110
01112 void setupEventTrapping(const Scan &scan, const ScanEx &ex, boost::shared_ptr<PrimListWrapper> list);
01113
01115 void startEventTrapping(const Scan &scan, const ScanEx &ex, boost::shared_ptr<PrimListWrapper> primList);
01116
01118 void setupHistogramming(const Scan &scan, const ScanEx &ex, boost::shared_ptr<PrimListWrapper> list);
01119
01121 void stopHistogramming(ScanEx &ex);
01122
01124 void calib_init();
01125
01127 void print_calib(unsigned int partition, unsigned int crate, unsigned int rod);
01128
01130
01138 void pollHistogramming(ScanEx &ex, int slave, int bins, int events, int timeout = 5);
01139
01141 scan_result_ptrs readHistogramData(const Scan &scan, const ScanEx &ex, UINT32 mid, int index);
01142
01144 void readHistogramsToFile(const Scan &scan, const ScanEx &ex);
01145
01147 void saveHistogramToFile(scan_result_ptrs histo, std::string filename);
01148
01149 #ifdef USE_IS
01150
01151 void readHistogramsToIS(const Scan &scan, const ScanEx &ex);
01152 #endif
01153
01155 void readRawData(unsigned int partition, unsigned int crate, unsigned int rod,
01156 int delay, bool setMask);
01157
01159 int setupRawData(unsigned int partition, unsigned int crate, unsigned int rod,
01160 int delay, int units, bool setMask, const Trigger *trig = 0);
01161
01163
01168 void printOutList(unsigned long *outlist, unsigned long length, bool in = false, int level = 0);
01169
01171
01175 Crate *getCrate(unsigned int partition, unsigned int crate) const;
01176
01178 ABCDModule *lookupConfig(UINT32 mid);
01179
01181 void cacheModuleConfig(UINT32 mid);
01182
01184
01187 void getrpcrc(UINT32 mid, unsigned int &rpartition, unsigned int &rcrate, unsigned int &rrod, unsigned &rchannel);
01188
01190 void initDDC();
01191
01192
01193
01195 boost::shared_ptr<SctConfiguration::Configuration> config;
01196
01197 MRSStream *mrs;
01198 #if USE_IS
01199 ISInfoDictionary *m_isDict;
01200 #endif
01201
01202 IPCPartition *ddcPartition;
01203 string *ddcCtrls[2];
01204 SCTDCSCommand *ddcCmd;
01205
01207 boost::shared_ptr<PrimListWrapper> debugPrimList;
01208
01210 std::map<std::pair<unsigned int, unsigned int>, Crate* > crateMap;
01211
01213 std::map<UINT32, ABCDModule> moduleMap;
01214
01216 Log log;
01217
01219 int debugLevel;
01220
01222 Scan *lastDebugScan;
01223 ScanEx lastDebugScanEx;
01224
01226 int responseCode;
01227
01229 UINT32 scanNumber;
01230
01232 UINT32 runNumber;
01233
01234
01235 boost::mutex logMutex;
01236 };
01237 }
01238 #endif
01239