00001 #ifndef SCTAPI_CRATE_IMPL
00002 #define SCTAPI_CRATE_IMPL
00003
00004
00005 #include "config.h"
00006
00007 #include <list>
00008 #include <map>
00009 #include <memory>
00010 #include <mrs/message.h>
00011
00012 #include <boost/thread.hpp>
00013 #include <boost/shared_ptr.hpp>
00014
00015 #include "crate.h"
00016
00017 #if USE_THREADS
00018 enum RodPrimState { MYPRIM_STATE_LOADED, MYPRIM_STATE_EXECUTING, MYPRIM_STATE_COMPLETE, MYPRIM_STATE_IDLE };
00019 #endif
00020
00021 namespace SctPixelRod {
00022 class RodOutList;
00023 class RodModule;
00024 class TimModule;
00025 class VmeInterface;
00026 }
00027
00028 namespace SctApi {
00029 class PrimListWrapper;
00030
00031 struct RodStatus {
00032 #if USE_THREADS
00033 std::list<boost::shared_ptr<PrimListWrapper> > listQueue;
00034 boost::mutex mutex;
00035 RodPrimState currState;
00036 #endif
00037 int slaves;
00038
00039 std::vector<SctConfiguration::BOCChannelConfig> savedBocSetup;
00040 std::auto_ptr<SctConfiguration::BOCGlobalConfig> savedBocRegisters;
00041 };
00042
00052 class CrateImpl : public Crate {
00053
00054 CrateImpl(const CrateImpl&);
00055 CrateImpl &operator=(const CrateImpl&);
00056 public:
00058
00062 CrateImpl(unsigned int partition, unsigned int crate,
00063 boost::shared_ptr<SctConfiguration::Configuration> newConf = boost::shared_ptr<SctConfiguration::Configuration>() );
00065
00068 ~CrateImpl();
00069
00070
00071
00073
00076 int initialiseCrate();
00077
00079
00082 int initialiseTim();
00083
00085
00089 int initialiseRod(unsigned int rod);
00090
00092
00095 int initialiseBOC(unsigned int rod);
00096
00098
00101 int configureBOC(unsigned int rod);
00102
00103
00104
00106
00109 int sendPrimList(unsigned int rod, boost::shared_ptr<PrimListWrapper> prim);
00110
00112
00115 int sendPrimListAll(boost::shared_ptr<PrimListWrapper> prim);
00116
00118
00124 int awaitResponse(unsigned int rod, int timeout);
00125
00127
00132 int awaitResponseAll(int timeout);
00133
00135
00138 boost::shared_ptr<SctPixelRod::RodOutList> getResponse(unsigned int rod);
00139
00140 void setMrsStream(MRSStream *stream);
00141
00142
00143
00145
00152 bool getRodMessage(unsigned int rod, char *buffer, unsigned long &length);
00153
00154
00155
00157
00160 int mdspBlockRead(unsigned int rod, long dspStart, unsigned long *buffer, unsigned long numWords);
00161
00163
00166 int slvBlockRead(unsigned int rod, long dspStart, unsigned long *buffer, unsigned long numWords, long slaveNumber);
00167
00169
00172 int slvBlockWrite(unsigned int rod, long dspStart, unsigned long* buffer, unsigned long numWords, long dspNumber);
00173
00175
00178 int mdspBlockWrite(unsigned int rod, long dspStart, unsigned long *buffer, unsigned long numWords);
00179
00181
00184 unsigned long readRodStatusReg(unsigned int rod, long regNumber);
00185
00186
00188
00191 unsigned long readRodCommandReg(unsigned int rod, long regNumber);
00192
00194
00197 unsigned long dspSingleRead(unsigned int rod, const unsigned long dspAddr, long dspNumber);
00198
00200
00203 int dspSingleWrite(unsigned int rod, unsigned long dspAddr, unsigned long buffer, long dspNumber);
00204
00206 void status();
00207
00209 void primListControl(unsigned int rod, PLControl function);
00210
00212 void printBOCSetup(unsigned int rod);
00213
00215 std::vector<SctConfiguration::BOCChannelConfig> currentBOCSetup(unsigned int rod);
00216
00218 void printBOCRegisters(unsigned int rod);
00219
00221 SctConfiguration::BOCGlobalConfig currentBOCRegisters(unsigned int rod);
00222
00224 void saveBOCSetup(unsigned int rod);
00225
00227 void saveBOCRegisters(unsigned int rod);
00228
00230 void restoreBOCSetup(unsigned int rod);
00231
00233 void restoreBOCRegisters(unsigned int rod);
00234
00236 bool checkBOCLasersOn(unsigned int rod);
00237
00239 void enterBOCClockBy2Mode(unsigned int rod);
00240
00242 void leaveBOCClockBy2Mode(unsigned int rod);
00243
00245
00248 void modifyBOCParam(unsigned int type, unsigned int val, bool raw);
00249
00251 void modifyBOCParam(unsigned int rod, unsigned int channel, unsigned int type, unsigned int val, bool raw);
00252
00254 void lasersOff();
00255
00256
00257
00259
00263 void timSetFrequency(double trigFreq, double rstFreq);
00264
00266 void freeTriggers();
00267
00269 void stopTriggers();
00270
00272 void timL1A();
00273
00275 void timCalL1A(int delay);
00276
00277 void timECR();
00278
00279 void timBCR();
00280
00281 void timFER();
00282
00284 void sendTimBurst(int count);
00285
00287 void timVerbose();
00288
00290 void timRegLoad(int reg, UINT16 val);
00291
00293 UINT16 timRegRead(int reg);
00294
00296 bool slavePresent(int rod, int index) const;
00297
00299 void slaveStarted(int rod, int slave);
00300
00302 bool RODPresent(int rod) const;
00303
00304 long getRodSlot(int rod) const;
00305
00307 int getRodRevision(int rod) const;
00308
00310 void stopPolling();
00311
00313 void resumePolling();
00314
00315 private:
00316 void rodInitialiseThread(int rod);
00317
00319 void setupScanTx(unsigned int rod, unsigned int channel);
00320
00322 void setupScanRawTx(unsigned int rod, unsigned int channel);
00323
00325
00328 SctPixelRod::RodModule *getRod(unsigned int rod) const;
00329
00330 const RodStatus &getRodData(unsigned int rod) const;
00331 RodStatus &getRodData(unsigned int rod);
00332
00334 void doTextBuffer(SctPixelRod::RodModule &rod0);
00335
00336 #if USE_THREADS
00337 void pollingLoop();
00338 #endif
00339
00341 typedef std::map<unsigned int, std::pair<RodStatus *, SctPixelRod::RodModule *> > RodMap;
00342
00344 RodMap rodMap;
00345
00347 SctPixelRod::TimModule *tim;
00348
00350 boost::shared_ptr<SctConfiguration::Configuration> config;
00351
00353 SctPixelRod::VmeInterface *vme;
00354
00356 const unsigned int partition;
00357
00359 const unsigned int crate;
00360
00361 MRSStream *mrs;
00362
00363
00364 bool m_stopPolling;
00365
00366
00367 bool m_enablePolling;
00368
00369 #if USE_THREADS
00370
00371 std::list<boost::shared_ptr<PrimListWrapper> > listQueueAll;
00372
00373
00374 boost::mutex vmeMutex;
00375
00376 boost::condition primQueue_notEmpty;
00377 boost::mutex primQueue_mutex;
00378
00379
00380 std::auto_ptr<boost::thread> pollThread;
00381
00382
00383 int rodCounter;
00384 #endif
00385 };
00386
00387 }
00388 #endif