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 SctPixelRod {
00032 class RodPrimList;
00033 }
00034
00035 using namespace SctPixelRod;
00036
00037 namespace SctConfiguration {
00038 class Configuration;
00039 }
00040
00041 namespace SctApi {
00042 class Crate;
00043
00045 class Trigger {
00046 public:
00047 virtual ~Trigger();
00048
00050 typedef std::vector<std::pair<UINT16, UINT32> > RODTriggers;
00051
00053 virtual void singleL1A() = 0;
00055 virtual void calL1A(unsigned short delay) = 0;
00056 virtual void pulseL1A(unsigned short delay) = 0;
00057 virtual void softL1A(unsigned short delay) = 0;
00058 virtual void softCalL1A(unsigned short delay, unsigned short delay2) = 0;
00059 virtual void softPulseL1A(unsigned short delay, unsigned short delay2) = 0;
00060 virtual void bcL1A(unsigned short delay) = 0;
00061 virtual void bcCalL1A(unsigned short delay, unsigned short delay2) = 0;
00062 virtual void bcPulseL1A(unsigned short delay, unsigned short delay2) = 0;
00063
00064 virtual void setCommIncr(unsigned short command, unsigned short incr) = 0;
00065 virtual void getCommIncr(unsigned short &command, unsigned short &incr) const = 0;
00066
00068 virtual const RODTriggers getRODTriggers() const = 0;
00069
00070 virtual void print() const = 0;
00071 };
00072
00081 class Scan {
00082 public:
00083 virtual ~Scan();
00084
00086 typedef std::vector<FLOAT32> ScanPoints;
00087
00089 enum ScanOptions {FORMAT, FULL, BITS32, LOOPCALLINE, DISTSLAVE, DEBUG};
00090
00091 virtual void configure(unsigned short type, float start, float stop, float step) = 0;
00092 virtual void configure2(unsigned short type, float start, float stop, float step) = 0;
00093
00094 virtual const Trigger *getTrigger1() const = 0;
00095 virtual const Trigger *getTrigger2() const = 0;
00096
00097 virtual Trigger *getTrigger1() = 0;
00098 virtual Trigger *getTrigger2() = 0;
00099
00100 virtual void setTrigger1(Trigger *trigger) = 0;
00101 virtual void setTrigger2(Trigger *trigger) = 0;
00102
00103 virtual void setScanVariable1(unsigned short var) = 0;
00104 virtual void setScanVariable2(unsigned short var) = 0;
00105
00106 virtual unsigned short getScanVariable1() const = 0;
00107 virtual unsigned short getScanVariable2() const = 0;
00108
00109 virtual void setNTrigs(unsigned long nTrigs) = 0;
00110 virtual unsigned short getNTrigs() const = 0;
00111
00112 virtual const ScanPoints getScanPoints1() const = 0;
00113 virtual const ScanPoints getScanPoints2() const = 0;
00114 virtual void setScanPoints1(const ScanPoints &scans) = 0;
00115 virtual void setScanPoints2(const ScanPoints &scans) = 0;
00116
00117 virtual int getOption(enum ScanOptions) const = 0;
00118 virtual void setOption(enum ScanOptions, int option) = 0;
00119
00121 virtual void print() const = 0;
00122
00123 virtual void setScanNumber(unsigned int scan) = 0;
00124 virtual unsigned int getScanNumber() const = 0;
00125 virtual void setRunNumber(unsigned int scan) = 0;
00126 virtual unsigned int getRunNumber() const = 0;
00127
00128 virtual std::list<std::string> getModuleList(unsigned int group) const = 0;
00129 virtual void setModuleList(unsigned int group, std::list<std::string> newList) = 0;
00130 virtual unsigned int getNGroups() const = 0;
00131 virtual void setNGroups(unsigned int val) = 0;
00132 };
00133
00135
00138 class ScanEx {
00139 public:
00140
00141 int partition;
00142 int crate;
00143 int rod;
00144 UINT32 channels[2];
00145
00146 UINT32 dspChannels[4][2];
00147
00149 int slaves;
00150
00152 char bitFieldDSP;
00153
00155 int trimScan;
00156
00157
00158
00160 int timTrigs;
00161 };
00162
00214 class SctApi {
00215 SctApi(const SctApi &);
00216 SctApi &operator=(const SctApi&);
00217 public:
00218
00220 static const unsigned int BAD_MODULE;
00221
00225 SctApi();
00226
00230 SctApi(boost::shared_ptr<SctConfiguration::Configuration> newConf);
00231
00235 SctApi(int initDebug);
00236
00237 ~SctApi();
00238
00239 private:
00245 void setup();
00246
00247 public:
00251 void setMrsStream(MRSStream *stream);
00252
00253 #if USE_IS
00254
00257 void setIsDictionary(ISInfoDictionary *dict);
00258 #endif
00259
00260
00261
00263
00276 void initialiseAll(int run);
00277
00279
00286 void shutdownAll();
00287
00289
00292 void setRunNumber(UINT32 newRun);
00293
00295
00298 void setScanNumber(UINT32 newScan);
00299
00301
00308 void configureAllModules();
00309
00310
00311
00313
00317 void loadConfiguration();
00318
00320
00324 void loadConfiguration(std::string filename);
00325
00327
00331 void storeModuleConfigurations();
00332
00334
00338 void configureBOC(unsigned int partition, unsigned int crate, unsigned int rod);
00339
00341
00345 void awaitResponse(unsigned int partition, unsigned int crate, unsigned int rod, int timeout);
00346
00348
00352 void awaitResponseAll(unsigned int partition, unsigned int crate, int timeout);
00353
00355
00360 unsigned long *getResponse(unsigned int partition, unsigned int crate, unsigned int rod,
00361 unsigned long &length);
00362
00368 bool getRodMessage(unsigned int partition, unsigned int crate, unsigned int rod,
00369 char *buffer, int &length);
00370
00371
00372
00374
00383 void flashLED(unsigned int partition, unsigned int crate, unsigned int rod,
00384 unsigned long slaveNumber, long period = 1000, long flashes = 10);
00385
00387
00391 void echo(unsigned int partition, unsigned int crate, unsigned int rod,
00392 unsigned int length, long *data);
00393
00394
00395
00396
00397
00399
00406 int dspBlockDump(unsigned int partition, unsigned int crate, unsigned int rod,
00407 long dspStart, long numWords, long dspNumber);
00408
00409 int dspBlockDumpFile(unsigned int partition, unsigned int crate, unsigned int rod,
00410 long dspStart, long numWords, long dspNumber, string filename);
00411
00413
00419 unsigned long *dspBlockRead(unsigned int partition, unsigned int crate, unsigned int rod,
00420 long dspStart, long numWords, long dspNumber, long &length);
00421
00423
00429 int dspBlockWrite(unsigned int partition, unsigned int crate, unsigned int rod,
00430 unsigned long *buffer, unsigned long dspAddress, long numWords, long dspNumber);
00431
00433
00439 unsigned long *readSlaveDsp(unsigned int partition, unsigned int crate, unsigned int rod,
00440 int s, int add, int words);
00441
00443
00447 unsigned long readRodStatusReg(unsigned int partition, unsigned int crate, unsigned int rod,
00448 long regNumber);
00449
00451
00455 unsigned long readRodCommandReg(unsigned int partition, unsigned int crate, unsigned int rod,
00456 long regNumber);
00457
00459
00463 unsigned long dspSingleRead(unsigned int partition, unsigned int crate, unsigned int rod,
00464 const unsigned long dspAddr, long dspNumber);
00465
00467
00471 void dspSingleWrite(unsigned int partition, unsigned int crate, unsigned int rod,
00472 unsigned long dspAddr, unsigned long buffer, long dspNumber);
00473
00475
00481 ABCDModule *retrieveModule(UINT32 mid);
00482
00484
00488 void createDebugPrimList();
00490
00494 void addDebugPrimList(unsigned long length, long index, long id, long version,
00495 unsigned long * body);
00497
00501 void sendDebugPrimList(unsigned int partition, unsigned int crate, unsigned int rod);
00502
00504
00512 void sendDebugSlavePrimList(unsigned int partition, unsigned int crate, unsigned int rod,
00513 unsigned int slave, bool await, bool response);
00514
00515
00516
00517
00519
00526 UINT32 findModule(std::string sn);
00527
00529
00533 std::string convertToString(UINT32 mid);
00534
00536
00540 UINT32 findModule(INT32 mur, INT32 module);
00541
00543
00547 std::pair<INT32, INT32> convertToMUR(UINT32 mid);
00548
00550
00554 UINT32 findBarrelModule(INT32 barrel, INT32 row, INT32 number);
00555
00557
00561 void convertToBarrelModule(UINT32 mid, UINT32 &barrel, UINT32 &row, int &number);
00562
00564
00568 UINT32 findEndcapModule(INT32 disk, INT32 quadrant, INT32 number);
00569
00571
00575 void convertToEndcapModule(UINT32 mid, UINT32 &disk, UINT32 &quadrant, UINT32 &number);
00576
00578
00582 void getABCDModule(UINT32 bank);
00583
00585
00589 void getABCDModule(UINT32 mid, UINT32 bank);
00590
00592
00596 void setABCDModule(UINT32 mid, UINT32 bank);
00597
00599
00608 void sendAllABCDModules(UINT32 bank, UINT32 type = 2);
00609
00611
00621 void sendABCDModule(UINT32 mid, UINT32 bank, UINT32 type=2);
00622
00624
00630 void modifyABCDMask(UINT32 mid, UINT32* mask);
00631
00635
00636
00643 void modifyABCDTrims(UINT32 mid, UINT8* trims);
00644
00648
00649
00651
00655 void modifyABCDVar(UINT32 typ, FLOAT32 var);
00656
00658
00662 void modifyABCDVar(UINT32 mid, UINT32 typ, FLOAT32 var);
00663
00665
00666
00668
00672 void modifyABCDVar(UINT32 mid, UINT32 chip, UINT32 typ, FLOAT32 var);
00673
00675
00676
00678
00682 void modifyABCDRC(UINT32 mid, UINT32 chip, UINT16 function, FLOAT32 p0, FLOAT32 p1, FLOAT32 p2);
00683
00685
00689 void modifyBOCParam(unsigned int partition, unsigned int crate, unsigned int rod,
00690 unsigned int channel, unsigned int type, unsigned int val);
00691
00693
00697 void printBOCSetup(unsigned int partition, unsigned int crate, unsigned int rod);
00698
00700
00704 void printBOCRegisters(unsigned int partition, unsigned int crate, unsigned int rod);
00705
00707
00711 void lasersOff();
00712
00713
00714
00716
00722 void freeTriggers(unsigned int partition, unsigned int crate,
00723 float freq);
00724
00726
00730 void stopTriggers(unsigned int partition, unsigned int crate);
00731
00733
00737 void timL1A(unsigned int partition, unsigned int crate);
00738
00740
00744 void timCalL1A(unsigned int partition, unsigned int crate, int delay);
00745
00746
00748
00752 void status();
00753
00755
00759 void defaultScan(int type);
00760
00762
00768 void doScan(Scan &scan);
00769
00777 void abortScan();
00778
00780
00784 void doRawScan(Scan &scan, int delay, bool clkBy2 = false);
00785
00787
00791 void sendTriggerAll(const Trigger *trig);
00792
00794
00798 void sendTrigger(unsigned int partition, unsigned int crate, unsigned int rod, const Trigger *trig);
00799
00801
00807 void printABCDModule(int mid);
00808
00810
00817 void printABCDRodModule(int mid, int bank);
00818
00820
00824 void decodeEvent(unsigned int partition, unsigned int crate, unsigned int rod,
00825 int sl, int index, bool extFlag = false, bool errorType = false);
00826
00827
00828 void printHex(UINT32 num, int width);
00829
00831
00835 void rawData(unsigned int partition, unsigned int crate, unsigned int rod, int delay, int units, bool setMask = true);
00836
00838
00844 void sendL1A(unsigned int partition, unsigned int crate, unsigned int rod);
00845
00847
00863 vector<char> probe(unsigned int partition, unsigned int crate, unsigned int rod, signed int harness = -1);
00864
00865
00866
00874 void requestHardResetAll();
00875
00883 void requestHardReset(UINT32 mid);
00884
00892 void requestIVCurve(UINT32 mid, FLOAT32 start, FLOAT32 stop, FLOAT32 step, UINT16 delay, FLOAT32 currentLim);
00893
00901 void requestIVCurveAll(FLOAT32 start, FLOAT32 stop, FLOAT32 step, UINT16 delay, FLOAT32 currentLim);
00902
00904 void ddcGetChannelParameter(int crate, int channel, std::string dpeName,
00905 int& dpeValue, int timeout);
00906
00917
00918 void setSelect(UINT32 mid, bool value);
00919
00929 void changeRunState(int state);
00930
00941 void tidyHistogramming();
00942
00950 void setDebugLevel(unsigned int level);
00951
00959 unsigned int getDebugLevel();
00960
00961 private:
00962
00963
00965
00970 int initialiseRod(unsigned int partition, unsigned int crate, unsigned int rod);
00971
00973
00978 int initialiseTim(unsigned int partition, unsigned int crate);
00979
00981
00986 int initialiseBOC(unsigned int partition, unsigned int crate, unsigned int rod);
00987
00989 void writeSlaveBlock(unsigned int partition, unsigned int crate, unsigned int rod, int s, long sAdd, int words, long *block);
00990
00992 void setSlaveBlock(unsigned int partition, unsigned int crate, unsigned int rod, int s, long sAdd, int words, long value = 0);
00993
00995 void writeRODRegister(unsigned int partition, unsigned int crate, unsigned int rod,
00996 int reg, int off, int width, int value);
00997
00999 void writeRODRegister(int reg, int off, int width, int value, RodPrimList &list);
01000
01002 unsigned int readRODRegister(unsigned int partition, unsigned int crate, unsigned int rod, int r);
01003
01005 int pollRegister(unsigned int partition, unsigned int crate, unsigned int rod, int r, int off, int width, int val, int timeout = 1000000);
01006
01007 unsigned long *readFifo(unsigned int partition, unsigned int crate, unsigned int rod, int id, int bank, int elems);
01008
01010
01016 int initialiseModule(std::string module);
01017
01018
01019
01020
01021
01022
01024
01030 void sendSlavePrimList(unsigned int partition, unsigned int crate, unsigned int rod,
01031 SctPixelRod::RodPrimList *prim, unsigned int slave, bool await, bool response);
01032
01034
01038 void buildSlavePrimList(RodPrimList &primList, RodPrimList *prim, unsigned int slaveNumber, bool await, bool response);
01039
01041
01046 void sendPrimList(unsigned int partition, unsigned int crate, unsigned int rod,
01047 SctPixelRod::RodPrimList *prim);
01048
01050
01055 void sendPrimListAll(unsigned int partition, unsigned int crate,
01056 SctPixelRod::RodPrimList *prim);
01057
01059 void startHistogramTask(const Scan &scan, const ScanEx &ex, RodPrimList &list);
01060
01062 void setupEventTrapping(const Scan &scan, const ScanEx &ex, RodPrimList &list);
01063
01065 void startEventTrapping(const Scan &scan, const ScanEx &ex, RodPrimList &primList);
01066
01068 void setupHistogramming(const Scan &scan, const ScanEx &ex, RodPrimList &list);
01069
01071 void stopHistogramming(ScanEx &ex);
01072
01074 void calib_init();
01075
01077
01085 void pollHistogramming(ScanEx &ex, int slave, int bins, int events, int timeout = 5);
01086
01088 scan_result_ptrs readHistogramData(const Scan &scan, const ScanEx &ex, UINT32 mid, int index);
01089
01091 void readHistogramsToFile(const Scan &scan, const ScanEx &ex);
01092
01094 void saveHistogramToFile(scan_result_ptrs histo, std::string filename);
01095
01096 #ifdef USE_IS
01097
01098 void readHistogramsToIS(const Scan &scan, const ScanEx &ex);
01099 #endif
01100
01102 void readRawData(unsigned int partition, unsigned int crate, unsigned int rod,
01103 int delay, bool setMask);
01104
01106 void setupRawData(unsigned int partition, unsigned int crate, unsigned int rod,
01107 int delay, int units, bool setMask);
01108
01110
01115 void printOutList(unsigned long *outlist, unsigned long length, bool in = false, int level = 0);
01116
01118
01122 Crate *getCrate(unsigned int partition, unsigned int crate) const;
01123
01125 ABCDModule *lookupConfig(UINT32 mid);
01126
01128 void cacheModuleConfig(UINT32 mid);
01129
01131
01134 void getrpcrc(UINT32 mid, unsigned int &rpartition, unsigned int &rcrate, unsigned int &rrod, unsigned &rchannel);
01135
01137 void initDDC();
01138
01139
01140
01142 boost::shared_ptr<SctConfiguration::Configuration> config;
01143
01144 MRSStream *mrs;
01145 #if USE_IS
01146 ISInfoDictionary *m_isDict;
01147 #endif
01148
01149 IPCPartition *ddcPartition;
01150 string *ddcCtrls[2];
01151 SCTDCSCommand *ddcCmd;
01152
01154 RodPrimList *debugPrimList;
01155
01157 std::map<std::pair<unsigned int, unsigned int>, Crate* > crateMap;
01158
01160 std::map<UINT32, ABCDModule> moduleMap;
01161
01163 Log log;
01164
01166 int debugLevel;
01167
01169 int responseCode;
01170
01172 UINT32 scanNumber;
01173
01175 UINT32 runNumber;
01176
01177
01178 boost::mutex logMutex;
01179 };
01180 }
01181 #endif
01182