11#include " V1724.hh"
22
3- V1724 ::V1724 (){
3+ V1724 ::V1724 (MongoLog *log ){
44 fBoardHandle =fLink =fCrate =fBID =-1 ;
55 fBaseAddress =0 ;
6+ fLog = log;
67}
78V1724 ::~V1724 (){
89 End ();
@@ -20,15 +21,89 @@ int V1724::Init(int link, int crate, int bid, unsigned int address=0){
2021 fBID = bid;
2122 fBaseAddress =address;
2223 cout<<" Successfully initialized board at " <<fBoardHandle <<endl;
24+ clock_counter = 0 ;
25+ last_time = 0 ;
26+ seen_over_15 = false ;
27+ seen_under_5 = true ; // starts run as true
2328 return 0 ;
2429}
2530
31+ int V1724::GetClockCounter (u_int32_t timestamp){
32+ // The V1724 has a 31-bit on board clock counter that counts 10ns samples.
33+ // So it will reset every 21 seconds. We need to count the resets or we
34+ // can't run longer than that. But it's not as simple as incementing a
35+ // counter every time a timestamp is less than the previous one because
36+ // we're multi-threaded and channels are quasi-independent. So we need
37+ // this fancy logic here.
38+
39+ // Seen under 5, true first time you see something under 5. False first time you
40+ // see something under 15 but >5
41+ // Seen over 15, true first time you se something >15 if under 5=false. False first
42+ // time you see something under 5
43+
44+ // First, is this number greater than the previous?
45+ if (timestamp > last_time){
46+
47+ // Case 1. This is over 15s but seen_under_5 is true. Give 1 back
48+ if (timestamp >= 15e8 && seen_under_5)
49+ return clock_counter-1 ;
50+
51+ // Case 2. This is over 5s and seen_under_5 is true.
52+ else if (timestamp >= 5e8 && timestamp < 15e8 && seen_under_5){
53+ seen_under_5 = false ;
54+ last_time = timestamp;
55+ return clock_counter;
56+ }
57+
58+ // Case 3. This is over 15s and seen_under_5 is false
59+ else if (timestamp >= 15e8 && !seen_under_5){
60+ seen_over_15 = true ;
61+ last_time = timestamp;
62+ return clock_counter;
63+ }
64+
65+ // Case 5. Anything else where the clock is progressing correctly
66+ else {
67+ last_time = timestamp;
68+ return clock_counter;
69+ }
70+ }
71+
72+ // Second, is this number less than the previous?
73+ else if (timestamp < last_time){
74+
75+ // Case 1. Genuine clock reset. under 5s is false and over 15s is true
76+ if (timestamp < 5e8 && !seen_under_5 && seen_over_15){
77+ seen_under_5 = true ;
78+ seen_over_15 = false ;
79+ last_time = timestamp;
80+ clock_counter++;
81+ return clock_counter;
82+ }
83+
84+ // Case 2: Any other jitter within the 21 seconds, just return
85+ else {
86+ return clock_counter;
87+ }
88+ }
89+ else {
90+ std::stringstream err;
91+ err<<" Something odd in your clock counters. t_new: " <<timestamp<<
92+ " last time: " <<last_time<<" over 15: " <<seen_over_15<<
93+ " under 5: " <<seen_under_5;
94+ fLog ->Entry (err.str (), MongoLog::Warning);
95+ return clock_counter;
96+ }
97+ }
98+
2699int V1724::WriteRegister (unsigned int reg, unsigned int value){
27100 std::cout<<" Writing reg:val: " <<hex<<reg<<" :" <<value<<dec<<std::endl;
28101 if (CAENVME_WriteCycle (fBoardHandle ,fBaseAddress +reg,
29102 &value,cvA32_U_DATA,cvD32) != cvSuccess){
30- cout<<" Failed to write register 0x" <<hex<<reg<<dec<<" to board " <<fBID <<
103+ std::stringstream err;
104+ err<<" Failed to write register 0x" <<hex<<reg<<dec<<" to board " <<fBID <<
31105 " with value " <<hex<<value<<dec<<" board handle " <<fBoardHandle <<endl;
106+ fLog ->Entry (err.str (), MongoLog::Warning);
32107 return -1 ;
33108 }
34109 return 0 ;
@@ -38,7 +113,9 @@ unsigned int V1724::ReadRegister(unsigned int reg){
38113 unsigned int temp;
39114 if (CAENVME_ReadCycle (fBoardHandle , fBaseAddress +reg, &temp,
40115 cvA32_U_DATA, cvD32) != cvSuccess){
41- cout<<" Failed to read register 0x" <<hex<<reg<<dec<<" on board " <<fBID <<endl;
116+ std::stringstream err;
117+ err<<" Failed to read register 0x" <<hex<<reg<<dec<<" on board " <<fBID <<endl;
118+ fLog ->Entry (err.str (), MongoLog::Warning);
42119 return -1 ;
43120 }
44121 return temp;
@@ -57,7 +134,9 @@ u_int32_t V1724::ReadMBLT(unsigned int *&buffer){
57134 ((unsigned char *)tempBuffer)+blt_bytes,
58135 BLT_SIZE , cvA32_U_BLT, cvD32, &nb);
59136 if ( (ret != cvSuccess) && (ret != cvBusError) ){
60- cout<<" Read error in board " <<fBID <<" after " <<count<<" reads." <<endl;
137+ stringstream err;
138+ err<<" Read error in board " <<fBID <<" after " <<count<<" reads." ;
139+ fLog ->Entry (err.str (), MongoLog::Error);
61140 delete[] tempBuffer;
62141 return 0 ;
63142 }
@@ -66,8 +145,11 @@ u_int32_t V1724::ReadMBLT(unsigned int *&buffer){
66145 blt_bytes+=nb;
67146
68147 if (blt_bytes>BLT_SIZE ){
69- cout<<" You managed to transfer more data than fits on board." <<endl;
70- cout<<" Transferred: " <<blt_bytes<<" bytes, Buffer: " <<BLT_SIZE <<" bytes." <<endl;
148+ stringstream err;
149+ err<<" You managed to transfer more data than fits on board." <<
150+ " Transferred: " <<blt_bytes<<" bytes, Buffer: " <<BLT_SIZE <<" bytes." ;
151+ fLog ->Entry (err.str (), MongoLog::Error);
152+
71153 delete[] tempBuffer;
72154 return 0 ;
73155 }
0 commit comments