//------------------------------------------------------------------------------ // Communication system master -- // (C) Piero Giubilato 2008-2010, Berkeley Lab -- //------------------------------------------------------------------------------ //______________________________________________________________________________ // {Trace} // [File name] "comm_Master.h" // [Author] "Piero Giubilato" // [Version] "1.2" // [Modified by] "Piero Giubilato" // [Last revision] "08 Apr 2009" // [Language] "C++" // [Compiler] "Visual C++ 9.x" // [Member of] "Cool SEAL" // [Project] "SEAL" // [Description] "Manages the parallel buffering system by glueing the USB" // "level buffering with the data set. Also applies the geometry" // [Key documentation] // "Root user's guide, writing a graphical interface" // "Visual C++ Reference Help" // {Trace} //______________________________________________________________________________ // Overloading check #ifndef comm_Master_H #define comm_Master_H // Standard libs #include #include #include // Root components #include // Application components #include "data_Burst.h" // Adds specific constants to the comm_Const namespace namespace comm_Const { // Device properties const int vendor_ID = 2830; const int device_ID = 3338; // Firmware constants const UInt_t frw_mdl_test_ID = 1; const UInt_t frw_mdl_reg_ID = 2; const UInt_t frw_pck_bld_ID = 3; const UInt_t frw_acq_mst_ID = 4; // Data packet properties const UInt_t pck_head_Count = 7; const UInt_t pck_foot_Count = 3; // Burst read error conditions //const Int_t brs_err_No = 1; //const Int_t brs_err_Mismatch = 2; //const Int_t brs_err_Timeout = 3; //const Int_t brs_err_Ask = 16; } //______________________________________________________________________________ class comm_Master { private: // Singleton implementation static comm_Master* inst_Handle; static int inst_Count; // Device interface static int comm_ON; // Communication is ON static int dev_ID; // In use device // Multi-threading data static data_Burst* burst_read_Ref; // data_Burst references static Int_t burst_read_Flag; // burst_Read status static Int_t burst_read_Last; // burst_Read last status static HANDLE burst_read_Hnd; // Read thread handle static UInt_t burst_read_ID; // Read thread ID static UInt_t burst_read_TOut; // Timeout before quitting the reading attempt static clock_t burst_read_TRef; // Last successful reading time reference // Double chunk buffering data static UShort_t* cnk_Buf[2]; // Double buffer pointers static UInt_t cnk_Pos[2]; // Read buffer position static UInt_t cnk_Len[2]; // Read buffer real lenght static Int_t cnk_WR; // Double buffer switch static Int_t cnk_RD; // Double buffer switch // Chunk reading data, data_Burst structure static UInt_t brs_frm_Count; // Expected frame count from the data burst static UInt_t brs_lyr_Count; // Expected layer count from the data burst static UInt_t brs_chn_Count; // Expected channel count from the data burst static UInt_t brs_word_Count; // Expected word count for a whole burst static UInt_t brs_word_Left; // Left words to read to complete the burst static UInt_t frm_word_Count; // Expected word count for a whole frame static UInt_t* brs_gmt_Layer; // Pointers to the channel target layer static UInt_t** brs_gmt_Front; // Pointers to the layer(s) geometry array static UShort_t** brs_lyr_Front; // Pointers to the layer(s) data storage static UInt_t lyr_dp_Idx; // Per layer dp, indipendent from the layer // USB communication buffering static int usb_buf_Len; // USB inner buffer lenght static int usb_buf_tOut; // USB inner buffer timeout // Chunk reading data, chunk structure static UInt_t cnk_buf_Len; // Chunk size static UShort_t* cnk_rd_Buf; // Chunk reference to read buffer static UInt_t cnk_rd_Idx; // Chunk reading position static UInt_t cnk_rd_Len; // Chunk reading len (real chunk lenght) static UInt_t cnk_rd_Stop; // Chunk read stop for bulk read // Chunk reading data, packet structure static UInt_t pck_Idx; // Current packet index static UInt_t pck_tStamp; // Current packet timestamp static UInt_t pck_Count; // How many packets to complete a data set transmission static UInt_t pck_dp_Idx; // What dp inside the current packet static UInt_t pck_dp_Count; // How many dp inside this packet static UInt_t pck_chn_Idx; // Current packet channel index static UInt_t pck_chn_Count; // How many channels inside the packet static UInt_t pck_chn_dp_Idx; // What dp inside the current channel static UInt_t pck_chn_dp_Count; // How many dp inside a channel static UInt_t pck_head_Idx; // On header position static UInt_t pck_head_Count; // Header lenght static UInt_t pck_foot_Idx; // On footer position static UInt_t pck_foot_Count; // Footer lenght static UInt_t pck_Timestamp; // Packet timestamp static UInt_t pck_chk_Flag; // Check flag from the packet builder static UInt_t pck_chk_Count; // Check dp count from packet builder // Multi-thread burst/frame reading function system static unsigned __stdcall burst_read_MT(void* Args); static void burst_read_Setup(); static void burst_read_Chunk(); static void burst_read_Header(); static void burst_read_Check(); static void burst_read_Bulk(); static void burst_read_Transition(); static void burst_read_Footer(); static void burst_read_Exit(); protected: // Force a singleton comm_Master(); ~comm_Master(); comm_Master(const comm_Master&) {}; comm_Master& operator=(comm_Master&) {}; public: // burst read status flags enum k_burst_Read {kbr_Free, kbr_Startup, kbr_Busy, kbr_Completed, kbr_Missed, kbr_Timeout, kbr_Error}; // Singleton access static comm_Master* instance_Load(); static void instance_Destroy(); // Comm mastering static int Open(); static int Test(); static bool isOpen(); static int Close(); static int Reset(); static int Purge(); static int Clear(); // Low level functions static int raw_Read(UShort_t* buffer, Int_t* len, bool wait = true); static int raw_Write(UShort_t* buffer, Int_t* len); // Mid level functions static int reg_Read(UShort_t regID, UShort_t* buffer, UShort_t len); static int reg_Read(UShort_t regID, UShort_t word_ID, UShort_t* word_Val); static int reg_Write(UShort_t regID, UShort_t* buffer, UShort_t len); static int reg_Write(UShort_t regID, UShort_t word_ID, UShort_t* word_Val); // High level functions static int burst_Read(data_Burst* burst_Ref, bool wait = false, unsigned int timeout = 1000); static int burst_read_Status() {return burst_read_Flag;}; static int burst_read_Exited() {return burst_read_Last;}; }; // Overloading check #endif