-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathPixelPh1FECInterface.cc
More file actions
3051 lines (2410 loc) · 114 KB
/
PixelPh1FECInterface.cc
File metadata and controls
3051 lines (2410 loc) · 114 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iomanip>
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <sstream>
#include <cstdlib>
#include "PixelFECInterface/include/PixelPh1FECInterface.h"
#include "PixelFECInterface/include/TBMReadException.h"
using namespace std;
using namespace pos;
namespace {
bool buffermodenever = false;
bool mfecbusyprint = true;
bool PRINT = false;
bool PRINT_old = PRINT;
void PRINT_ON() {
PRINT_old = PRINT;
PRINT = true;
}
void PRINT_RESTORE() {
PRINT = PRINT_old;
}
void hexprintword(uint32_t w) {
cout << hex
<< setw(2) << setfill('0') << ((w&0xFF000000)>>24) << " "
<< setw(2) << ((w&0x00FF0000)>>16) << " "
<< setw(2) << ((w&0x0000FF00)>> 8) << " "
<< setw(2) << ((w&0x000000FF) )
<< setfill(' ') << dec;
}
}
PixelPh1FECInterface::PixelPh1FECInterface(RegManager * const RegManagerPtr,
const char* boardid)
: pRegManager(RegManagerPtr),
board_id(boardid)
{
//Constructor stuff
if (PRINT) cout << "PixelPh1FECInterface: " << "PixelPh1FECInterface Constructor" << endl;
cout << "PixelPh1FECInterface::maxbuffersize set to " << maxbuffersize << "!!! change this in future\n";
assert(qbufsize > maxbuffersize + 100);
fecdebug = 0;
for (int tfecchannel=1;tfecchannel<=2;tfecchannel++) {
for (int tmfec=0;tmfec<9;tmfec++) {
qbufnsend[tmfec][tfecchannel]=0;
qbufnerr[tmfec][tfecchannel]=0;
qbufn[tmfec][tfecchannel]=0;
qbufn_old[tmfec][tfecchannel]=0;
memset(qbuf[tmfec][tfecchannel], 0, qbufsize);
}
}
pRegManager->WriteReg("ctrl.ttc_xpoint_A_out3", 0);
usleep(100);
resetttc();
usleep(100);
if (!hasclock() || clocklost()) {
std::cerr << "hits go in wrong bx after clock lost until you reload firmware / power cycle\n";
// assert(0);
}
d25_trimloadtest.resize(52*80);
for (size_t i = 0; i < 52*80; ++i)
d25_trimloadtest[i] = (unsigned char)(i % 256);
}
bool PixelPh1FECInterface::hasclock() {
return pRegManager->ReadReg("Board0.TTCatLHC");
}
bool PixelPh1FECInterface::clocklost() {
return pRegManager->ReadReg("Board0.TTCLostLHC");
}
void PixelPh1FECInterface::resetttc() {
pRegManager->WriteReg("Board0.TTCrst", 1);
usleep(1000);
pRegManager->WriteReg("Board0.TTCrst", 0);
}
void PixelPh1FECInterface::resetclocklost() {
pRegManager->WriteReg("Board0.RstTTCLostLHC", 1);
usleep(1000);
pRegManager->WriteReg("Board0.RstTTCLostLHC", 0);
}
//-----------------------------------------------------------------------
void PixelPh1FECInterface::resetpll(){
pRegManager->WriteReg("Board0.PLLrst", 1);
usleep(1000);
pRegManager->WriteReg("Board0.PLLrst", 0);
}
//-----------------------------------------------------------------------
void PixelPh1FECInterface::ttcdelayinc(){
pRegManager->WriteReg("Board0.IODelayInc", 3);
resetttc();
readdelay();
}
//-----------------------------------------------------------------------
void PixelPh1FECInterface::ttcdelaydec(){
pRegManager->WriteReg("Board0.IODelayDec", 1);
resetttc();
readdelay();
}
//-----------------------------------------------------------------------
void PixelPh1FECInterface::readdelay(){
valword value;
value = pRegManager->ReadReg("Board0.IODelay");
cout <<" The current io ttc delay setting is: " <<value.value()<<endl;
}
//-------------------------------------------------------------------------
/* There are two versions of getversion() for both hal and CAEN (4 total)
The form getversion(unsigned long *data) returns the integer 0..255
version number of the mfec firmware that is loaded into an mfec, but
assumes an mfec installed into position 1. The
getversion(const int mfec, unsigned long *data) form queries a particular
mfec for the version (all mfecs on a VME FEC board will have the same
mfec version sice it is loaded from a single eeprom.) */
unsigned PixelPh1FECInterface::getGeneral() {
valword value = pRegManager->ReadReg("GenReg");
if (PRINT) cout << "PixelPh1FECInterface: Get FEC general register: 0x" << hex << value.value() << dec << endl;
return value.value();
}
int PixelPh1FECInterface::getversion(unsigned long *data) {
valword value;
value = pRegManager->ReadReg("GenRegM1.VERSION");
*data = value.value();
if (PRINT) cout << "PixelPh1FECInterface: " <<"Get FEC version finds firmware version: "<<value.value()<<endl;
return value;
}
//-----------------------------------------------------------------------
// Get the STATUS word of the FEC. Includes the QPLL/TTCrx ready bits
int PixelPh1FECInterface::getStatus(void) {
valword value;
value = pRegManager->ReadReg("STATUS");
if (PRINT) cout << "PixelPh1FECInterface: " <<"Get FEC status "<<value.value()<<endl;
return value;
}
//--------------------------------------------------------------------------
int PixelPh1FECInterface::getversion(const int mfec, unsigned long *data) {
const string names[4] = {
"GenRegM1.VERSION",
"GenRegM2.VERSION",
"GenRegM3.VERSION",
"GenRegM4.VERSION"
};
valword value;
value = pRegManager->ReadReg(names[mfec-1]);
*data = value.value();
//*data = (value & 0xFF000000) >> 24;
if (PRINT) cout << "PixelPh1FECInterface: " <<"Get FEC version finds firmware version: "<<*data<<endl;
return 0;
}
//----------------------------------------------------------------------------
//
int PixelPh1FECInterface::writeCSregister(int mfec, int fecchannel, int cscommand) {
if(mfec < 1 || mfec > 4){
cerr << "PixelPh1FECInterface::writeCSregister -> User provided a wrong mfec number" <<endl;
return -1;
}
// expect the lower 8 bits of the CS register (shift for other channel)
uint32_t value;
uint32_t value2;
int cscommand1, cscommand2;
value = mfecCSregister[mfec];
if (PRINT) {
cout << "writeCSregister first reads: "<<hex<< value <<dec<<endl;
if (value & 0x00000100) cout << "CH1 Receive Timeout"<<endl;
if (value & 0x00000200) cout << "CH1 Receive Complete"<<endl;
if (value & 0x00000400) cout << "CH1 Hub Address Error Timeout"<<endl;
if (value & 0x00000800) cout << "CH1 Send Started"<<endl;
if (value & 0x00001000) cout << "CH1 Receive Count Error"<<endl;
if (value & 0x00002000) cout << "CH1 Receive Failure"<<endl;
if (value & 0x01000000) cout << "CH2 Receive Timeout"<<endl;
if (value & 0x02000000) cout << "CH2 Receive Complete"<<endl;
if (value & 0x04000000) cout << "CH2 Hub Address Error Timeout"<<endl;
if (value & 0x08000000) cout << "CH2 Send Started"<<endl;
if (value & 0x10000000) cout << "CH2 Receive Count Error"<<endl;
if (value & 0x20000000) cout << "CH2 Receive Failure"<<endl;
}
if (fecchannel == 1) {
value = value & 0x00FF0000; // only preserve fec ch2 mode bits
cscommand2 = cscommand;
value = value | cscommand2;
} else if (fecchannel == 2) {
value = value & 0x000000FF; // only preserve fec ch1 mode bits
cscommand1 = cscommand << 16;
value = value | cscommand1;
} else {
// value = value & 0x00000000; // toss the mode bits from both channels
cscommand1 = cscommand << 16;
value = cscommand | cscommand1;
}
value2 = value;
switch (mfec) {
case 1:
pRegManager->WriteReg("CSRegM1.CSREG", value2);
break;
case 2:
pRegManager->WriteReg("CSRegM2.CSREG", value2);
break;
case 3:
pRegManager->WriteReg("CSRegM3.CSREG", value2);
break;
case 4:
pRegManager->WriteReg("CSRegM4.CSREG", value2);
break;
}
if (PRINT) {
cout << "writeCSregister puts : "<<hex<< value2 <<dec<<endl;
}
return value;
}
//-------------------------------------------------------------------------------
// mfecbusy reads the csreg and waits if the send_started is still
// in effect. This should change state on it's own. Timeout used in
// case it doesnt change.
void PixelPh1FECInterface::mfecbusy(int mfec, int fecchannel,
unsigned int *ch1, unsigned int *ch2) {
if(mfec < 1 || mfec > 4){
cerr << "PixelPh1FECInterface::mfecbusy -> User provided a wrong mfec number" <<endl;
return ;
}
valword value;
int timeout;
// CSTIMEOUT of 300 is large enough for a mfec to shift out an entirepRegManager
// rocinit (typical wait for small column wise op is 20)
const int CSTIMEOUT = 300;
switch (mfec) {
case 1:
value = pRegManager->ReadReg("CSRegM1.CSREG");
break;
case 2:
value = pRegManager->ReadReg("CSRegM2.CSREG");
break;
case 3:
value = pRegManager->ReadReg("CSRegM3.CSREG");
break;
case 4:
value = pRegManager->ReadReg("CSRegM4.CSREG");
break;
}
*ch1 = (value.value() >> 8) & 0x7F;
*ch2 = (value.value() >> 24) & 0x7F;
timeout = 0;
if ((fecchannel == 1)&&((*ch1 & 0x8)==0x8)&&(*ch1!=0)) { //send started -
//wait until complete or timeout
// if (PRINT) {
//cout << "<WAITING FOR READY (1)"<<hex<<value<<" "<<timeout<<dec<<" > ";
// }
timeout = 0;
do {
switch (mfec) {
case 1:
value = pRegManager->ReadReg("CSRegM1.CSREG");
break;
case 2:
value = pRegManager->ReadReg("CSRegM2.CSREG");
break;
case 3:
value = pRegManager->ReadReg("CSRegM3.CSREG");
break;
case 4:
value = pRegManager->ReadReg("CSRegM4.CSREG");
break;
}
*ch1 = (int) (value.value() >> 8) & 0x7F;
timeout++;
// if (PRINT) {
//cout << "<WAITING FOR READY "<<hex<<value<<" "<<timeout<<dec<< " > ";
// }
} while (((*ch1 & 0x8) == 0x8) && (timeout < CSTIMEOUT));
if (PRINT) cout << "PixelPh1FECInterface: " << "CH1 timeout=" << dec << timeout <<endl;
if (timeout>=CSTIMEOUT && mfecbusyprint) { cout << "ERROR mfecbusy channel 1"<<endl; }
}
if ((fecchannel == 1) && (fecdebug > 0)) {
if (((*ch1 & 0x02) != 0x02) && (*ch1 != 0)) {
analyzeFecCSRChan(mfec, 1, *ch1);
if (fecdebug == 2) writeCSregister(mfec, fecchannel, 0x08);
}
}
if ((fecchannel == 2)&&((*ch2 & 0x8)==0x8)&&(*ch2!=0)) { //send started -
//wait until complete or timeout
// if (PRINT) {
//cout << "<WAITING FOR READY "<<hex<<value<<" "<<timeout<<dec<< " > ";
// }
timeout = 0;
do {
switch (mfec) {
case 1:
value = pRegManager->ReadReg("CSRegM1.CSREG");
break;
case 2:
value = pRegManager->ReadReg("CSRegM2.CSREG");
break;
case 3:
value = pRegManager->ReadReg("CSRegM3.CSREG");
break;
case 4:
value = pRegManager->ReadReg("CSRegM4.CSREG");
break;
}
*ch2 = (int) (value.value() >> 24) & 0x7F;
timeout++;
// if (PRINT) {
//cout << "<WAITING FOR READY "<<hex<<value<<" "<<timeout<<dec<< " > ";
// }
} while (((*ch2 & 0x8) == 0x8) && (timeout < CSTIMEOUT));
if (PRINT) cout << "PixelPh1FECInterface: " << "CH2 timeout=" << dec << timeout <<endl;
if (timeout>=CSTIMEOUT && mfecbusyprint) { cout << "ERROR mfecbusy channel 2"<<endl; }
}
if ((fecchannel == 2) && (fecdebug > 0)) {
if (((*ch2 & 0x02) != 0x02) && (*ch2 != 0)) {
analyzeFecCSRChan(mfec, 2, *ch2);
if (fecdebug == 2) writeCSregister(mfec, fecchannel, 0x08);
}
}
mfecCSregister[mfec] = value.value();
}
//--------------------------------------------------------------------------------
// Finds out if *previous* transmission was received okay.
// Reads the mfec combined control/status register which returns and sets
// functions of ch1 and ch2 independently
int PixelPh1FECInterface::getfecctrlstatus(const int mfec, unsigned long *data) {
if(mfec < 1 || mfec > 4){
cerr << "PixelPh1FECInterface::getfecctrlstatus -> User provided a wrong mfec number" <<endl;
return -1 ;
}
valword value;
switch (mfec) {
case 1:
value = pRegManager->ReadReg("CSRegM1.CSREG");
break;
case 2:
value = pRegManager->ReadReg("CSRegM2.CSREG");
break;
case 3:
value = pRegManager->ReadReg("CSRegM3.CSREG");
break;
case 4:
value = pRegManager->ReadReg("CSRegM4.CSREG");
break;
}
*data = value.value();
if (PRINT) cout << "PixelPh1FECInterface: " << "Getting FEC cntrstatus register: 0x" << hex << *data << dec <<endl;
return 0;
}
//----------------------------------------------------------------------------------
//// output one word in HAL single mode
void PixelPh1FECInterface::outputwordhal(const char *halname, unsigned int data) {
pRegManager->WriteReg(halname, data);
if (PRINT) {
valword value = pRegManager->ReadReg(halname);
cout <<"PixelPh1FECInterface: outputwordhal " << halname << " 0x" << hex << data << " readback 0x" << value;
if (value.value() != data)
cout << "NOT OK";
cout << endl;
}
}
void PixelPh1FECInterface::outputblock(const int mfec, const int fecchannel, std::vector<uint32_t> wordcont) {
if(mfec < 1 || mfec > 4){
cerr << "PixelPh1FECInterface::outputblock -> User provided a wrong mfec number" <<endl;
return ;
}
const string names[2][4] = {
{"BOUT_BUF1M1","BOUT_BUF1M2","BOUT_BUF1M3","BOUT_BUF1M4"},
{"BOUT_BUF2M1","BOUT_BUF2M2","BOUT_BUF2M3","BOUT_BUF2M4"}
};
// implement throw exception for the cases when mfec >/< bla and channel >/< bla
//std::cout << "name " << names[fecchannel-1][mfec-1] << " size of the word vector " << wordcont.size()<< std::endl;
pRegManager->WriteBlockReg( names[fecchannel-1][mfec-1], wordcont);
}
int PixelPh1FECInterface::resetdoh(const int mfec, const int fecchannel) {
if (PRINT) cout << "PixelPh1FECInterface: " << "resetdoh(" << mfec << ", " << fecchannel << ")" << endl;
// fecchannel determines if value will be shifted on CS register word
writeCSregister(mfec, fecchannel, 0x8000);
return 0;
}
int PixelPh1FECInterface::injectrstroc(const int mfec, const int bitstate) {
if(mfec < 1 || mfec > 4){
cerr << "PixelPh1FECInterface::injectrstroc -> User provided a wrong mfec number" <<endl;
return -1;
}
disableexttrigger(mfec, 1); // JMTBAD to be taken out if FW changes
if (PRINT) cout << "PixelPh1FECInterface: " << "injectrstroc(" << mfec << ", " << bitstate << ")" << endl;
switch (mfec)
{
case 1: outputwordhal("GenRegM1.INRSTROC", bitstate); break;
case 2: outputwordhal("GenRegM2.INRSTROC", bitstate); break;
case 3: outputwordhal("GenRegM3.INRSTROC", bitstate); break;
case 4: outputwordhal("GenRegM4.INRSTROC", bitstate); break;
}
disableexttrigger(mfec, 0);
return 0;
}
int PixelPh1FECInterface::injecttrigger(const int mfec, const int bitstate) {
if(mfec < 1 || mfec > 4){
cerr << "PixelPh1FECInterface::injecttrigger -> User provided a wrong mfec number" <<endl;
return -1;
}
if (PRINT) cout << "PixelPh1FECInterface: " << "injecttrigger(" << mfec << ", " << bitstate << ")" << endl;
switch (mfec)
{
case 1: outputwordhal("GenRegM1.INTRIG", bitstate); break;
case 2: outputwordhal("GenRegM2.INTRIG", bitstate); break;
case 3: outputwordhal("GenRegM3.INTRIG", bitstate); break;
case 4: outputwordhal("GenRegM4.INTRIG", bitstate); break;
}
return 0;
}
int PixelPh1FECInterface::injectrsttbm(const int mfec, const int bitstate) {
if(mfec < 1 || mfec > 4){
cerr << "PixelPh1FECInterface::injectrsttbm -> User provided a wrong mfec number" <<endl;
return -1;
}
disableexttrigger(mfec, 1); // JMTBAD to be taken out if FW chang
if (PRINT) cout << "PixelPh1FECInterface: " << "injectrsttbm(" << mfec << ", " << bitstate << ")" << endl;
switch (mfec)
{
case 1: outputwordhal("GenRegM1.INRSTTBM", bitstate); break;
case 2: outputwordhal("GenRegM2.INRSTTBM", bitstate); break;
case 3: outputwordhal("GenRegM3.INRSTTBM", bitstate); break;
case 4: outputwordhal("GenRegM4.INRSTTBM", bitstate); break;
}
disableexttrigger(mfec, 0);
return 0;
}
int PixelPh1FECInterface::injectrstcsr(const int mfec, const int bitstate) {
if(mfec < 1 || mfec > 4){
cerr << "PixelPh1FECInterface::injectrstcsr -> User provided a wrong mfec number" <<endl;
return -1;
}
disableexttrigger(mfec, 1); // JMTBAD to be taken out if FW chang
if (PRINT) cout << "PixelPh1FECInterface: " << "injectrstcsr(" << mfec << ", " << bitstate << ")" << endl;
switch (mfec)
{
case 1: outputwordhal("GenRegM1.INRSTCSR", bitstate); break;
case 2: outputwordhal("GenRegM2.INRSTCSR", bitstate); break;
case 3: outputwordhal("GenRegM3.INRSTCSR", bitstate); break;
case 4: outputwordhal("GenRegM4.INRSTCSR", bitstate); break;
}
disableexttrigger(mfec, 0); // JMTBAD to be taken out if FW chang
return 0;
}
int PixelPh1FECInterface::enablecallatency(const int mfec, const int bitstate) {
if(mfec < 1 || mfec > 4){
cerr << "PixelPh1FECInterface::enablecallatency -> User provided a wrong mfec number" <<endl;
return -1;
}
if (PRINT) cout << "PixelPh1FECInterface: " << "enablecallatency(" << mfec << ", " << bitstate << ")" << endl;
switch (mfec)
{
case 1: outputwordhal("GenRegM1.ENCALLATENCY", bitstate); break;
case 2: outputwordhal("GenRegM2.ENCALLATENCY", bitstate); break;
case 3: outputwordhal("GenRegM3.ENCALLATENCY", bitstate); break;
case 4: outputwordhal("GenRegM4.ENCALLATENCY", bitstate); break;
}
return 0;
}
int PixelPh1FECInterface::disableexttrigger(const int mfec, const int bitstate) {
if(mfec < 1 || mfec > 4){
cerr << "PixelPh1FECInterface::disableexttrigger -> User provided a wrong mfec number" <<endl;
return -1;
}
if (PRINT) cout << "PixelPh1FECInterface: " << "disableexttrigger(" << mfec << ", " << bitstate << ")" << endl;
switch (mfec)
{
case 1: outputwordhal("GenRegM1.DISEXTTRIG", bitstate); break;
case 2: outputwordhal("GenRegM2.DISEXTTRIG", bitstate); break;
case 3: outputwordhal("GenRegM3.DISEXTTRIG", bitstate); break;
case 4: outputwordhal("GenRegM4.DISEXTTRIG", bitstate); break;
}
return 0;
}
int PixelPh1FECInterface::loopnormtrigger(const int mfec, const int bitstate) {
if(mfec < 1 || mfec > 4){
cerr << "PixelPh1FECInterface::loopnormtrigger -> User provided a wrong mfec number" <<endl;
return -1;
}
if (PRINT) cout << "PixelPh1FECInterface: " << "loopnormtrigger(" << mfec << ", " << bitstate << ")" << endl;
switch (mfec)
{
case 1: outputwordhal("GenRegM1.LOOPNORMTRIG", bitstate); break;
case 2: outputwordhal("GenRegM2.LOOPNORMTRIG", bitstate); break;
case 3: outputwordhal("GenRegM3.LOOPNORMTRIG", bitstate); break;
case 4: outputwordhal("GenRegM4.LOOPNORMTRIG", bitstate); break;
}
return 0;
}
int PixelPh1FECInterface::loopcaltrigger(const int mfec, const int bitstate) {
if(mfec < 1 || mfec > 4){
cerr << "PixelPh1FECInterface::loopcaltrigger -> User provided a wrong mfec number" <<endl;
return -1;
}
if (PRINT) cout << "PixelPh1FECInterface: " << "loopcaltrigger(" << mfec << ", " << bitstate << ")" << endl;
switch (mfec)
{
case 1: outputwordhal("GenRegM1.LOOPCALTRIG", bitstate); break;
case 2: outputwordhal("GenRegM2.LOOPCALTRIG", bitstate); break;
case 3: outputwordhal("GenRegM3.LOOPCALTRIG", bitstate); break;
case 4: outputwordhal("GenRegM4.LOOPCALTRIG", bitstate); break;
}
return 0;
}
int PixelPh1FECInterface::callatencycount(const int mfec, const int latency) {
if(mfec < 1 || mfec > 4){
cerr << "PixelPh1FECInterface::callatencycount -> User provided a wrong mfec number" <<endl;
return -1;
}
if (PRINT) cout << "PixelPh1FECInterface: " << "callatencycount(" << mfec << ", " << latency << ")" << endl;
switch (mfec)
{
case 1: outputwordhal("GenRegM1.CALLATCNT", latency); break;
case 2: outputwordhal("GenRegM2.CALLATCNT", latency); break;
case 3: outputwordhal("GenRegM3.CALLATCNT", latency); break;
case 4: outputwordhal("GenRegM4.CALLATCNT", latency); break;
}
return 0;
}
int PixelPh1FECInterface::FullBufRDaDisable(const int mfec, const int disable) {
if(mfec < 1 || mfec > 4){
cerr << "PixelPh1FECInterface::FullBufRDaDisable -> User provided a wrong mfec number" <<endl;
return -1;
}
if (PRINT) cout << "PixelPh1FECInterface: " << "FullBufRDaDisable(" << mfec << ", " << disable << ")" << endl;
switch (mfec)
{
case 1: outputwordhal("GenRegM1.DISRDACHECK", disable); break;
case 2: outputwordhal("GenRegM2.DISRDACHECK", disable); break;
case 3: outputwordhal("GenRegM3.DISRDACHECK", disable); break;
case 4: outputwordhal("GenRegM4.DISRDACHECK", disable); break;
}
return 0;
}
int PixelPh1FECInterface::AllRDaDisable(const int mfec, const int disable) {
if(mfec < 1 || mfec > 4){
cerr << "PixelPh1FECInterface::AllRDaDisable -> User provided a wrong mfec number" <<endl;
return -1;
}
if (PRINT) cout << "PixelPh1FECInterface: " << "AllRDaDisable(" << mfec << ", " << disable << ")" << endl;
switch (mfec)
{
case 1: outputwordhal("GenRegM1.DISRDA", disable); break;
case 2: outputwordhal("GenRegM2.DISRDA", disable); break;
case 3: outputwordhal("GenRegM3.DISRDA", disable); break;
case 4: outputwordhal("GenRegM4.DISRDA", disable); break;
}
return 0;
}
int PixelPh1FECInterface::testFiberEnable(const int mfec, const int enable) {
if(mfec < 1 || mfec > 4){
cerr << "PixelPh1FECInterface::testFiberEnable -> User provided a wrong mfec number" <<endl;
return -1;
}
if (PRINT) cout << "PixelPh1FECInterface: " << "testFiberEnable(" << mfec << ", " << enable << ")" << endl;
switch (mfec)
{
case 1: outputwordhal("GenRegM1.TESTFIBER", enable); break;
case 2: outputwordhal("GenRegM2.TESTFIBER", enable); break;
case 3: outputwordhal("GenRegM3.TESTFIBER", enable); break;
case 4: outputwordhal("GenRegM4.TESTFIBER", enable); break;
}
return 0;
}
//-------------------------------------------------------------------------------------
// To read back the information from the mFEC input FIFOs
// Return the word from the FIFO
int PixelPh1FECInterface::readback(const int mfec, const int channel) {
return readreturn(mfec, channel, 1)[0];
}
std::vector<uint32_t> PixelPh1FECInterface::readreturn(const int mfec, const int channel, uint32_t size) {
if(mfec<1 || mfec>4) {
cerr<<" PixelPh1FECInterface: Wrong mfec number "<<mfec<<endl;
assert(0);
}
if(channel<1 || channel>2) {
cout<<" PixelPh1FECInterface: Wrong mfec channel number "<<channel<<endl;
assert(0);
}
const string names[2][4] = {
{"INP_BUF1M1","INP_BUF1M2","INP_BUF1M3","INP_BUF1M4"},
{"INP_BUF2M1","INP_BUF2M2","INP_BUF2M3","INP_BUF2M4"}
};
return pRegManager->ReadBlockRegValue(names[channel-1][mfec-1], size);
}
//---------------------------------------------------------------------------------
// To read the mfec word with the HUB and byte COUNT
// Read data is returned in *data. Return same data..
// byte defines the offset to access the corrvecect information
// byte = 0 - transmitted HUB address
// byte = 1 - received HUB address
// byte = 2 - transmitted byte COUNT
// byte = 3 - received byte COUNT
// byte = 4 - return the full word
int PixelPh1FECInterface::getByteHubCount(const int mfec,
const int channel,
const int byte,
int * data) {
valword value;
int ret = 0;
if (PRINT) cout << "PixelPh1FECInterface: byte + hub count:\n";
if(mfec<1 || mfec>8) {
cout<<" PixelPh1FECInterface: Wrong mfec number "<<mfec<<endl;
return 3;
}
if(channel<1 || channel>2) {
cout<<" PixelPh1FECInterface: Wrong mfec channel number "<<channel<<endl;
return 2;
}
const string names[2][4] = {
{"STAT_M1.StatCh1","STAT_M2.StatCh1","STAT_M3.StatCh1","STAT_M4.StatCh1"},
{"STAT_M1.StatCh2","STAT_M2.StatCh2","STAT_M3.StatCh2","STAT_M4.StatCh2"}
};
value = pRegManager->ReadReg(names[channel-1][mfec-1]);
if (PRINT) cout << hex << "0x" << value << dec << endl;
if(byte<0||byte>4) {*data=0; ret=1;} // signal out of range, return 0
else if(byte==4) {*data = value.value();} // for 4 return the whole register
else {*data = (int) (value.value() >> (8*byte)) & 0xFF;} // for 0-3 return a byte
return ret;
}
// Set the debug flag
void PixelPh1FECInterface::fecDebug(int newstate) {
if ((fecdebug == 0) && (newstate > 0))
cout << "\nSetting FECDEBUG to ON (" << newstate << ")" << endl;
if ((fecdebug > 0) && (newstate == 0))
cout << "\nSetting FECDEBUG to OFF (" << newstate << ")" << endl;
fecdebug = newstate;
}
//------------------------------------------------------------------------------
//
unsigned char PixelPh1FECInterface::cinttogray(unsigned int igray) {
// cintogray
if (PRINT) cout << "PixelPh1FECInterface: " <<"CINTTOGRAY "<<igray<<" -> "<<(igray^(igray>>1))<<endl;;
return (igray^(igray>>1));
}
//------------------------------------------------------------------------------
// Analyze the CSR register, print errors.
void PixelPh1FECInterface::analyzeFecCSRChan(int mFEC, int ichan, unsigned int ival) {
if (fecdebug == 1) cout << "Previous fec command FAILED ";
else if (fecdebug == 2) cout << "Current fec command FAILED ";
cout << "CSR Register:0x" << hex <<ival<<dec<<endl;
if ((ival & 0x01) == 0x01) cout <<"board="<<board_id<<" mFEC="<<mFEC<<" CH="<<ichan<<" Receive timeout"<<endl;
if ((ival & 0x02) == 0x02) cout <<"board="<<board_id<<" mFEC="<<mFEC<<" CH="<<ichan<<" Receive complete"<<endl;
if ((ival & 0x04) == 0x04) cout <<"board="<<board_id<<" mFEC="<<mFEC<<" CH="<<ichan<<" HubAddress error"<<endl;
if ((ival & 0x08) == 0x08) cout <<"board="<<board_id<<" mFEC="<<mFEC<<" CH="<<ichan<<" Send started but not finished"<<endl;
if ((ival & 0x10) == 0x10) cout <<"board="<<board_id<<" mFEC="<<mFEC<<" CH="<<ichan<<" Receive count error"<<endl;
if ((ival & 0x20) == 0x20) cout <<"board="<<board_id<<" mFEC="<<mFEC<<" CH="<<ichan<<" Receive error"<<endl;
if ((ival & 0x40) == 0x40) cout <<"board="<<board_id<<" mFEC="<<mFEC<<" CH="<<ichan<<" Stop Failure"<<endl;
}
//-----------------------------------------------------------------------------
// Send buffered data for all channels
int PixelPh1FECInterface::qbufsend(void) {
int tmfec, tfecchannel;
for (tfecchannel=1;tfecchannel<=2;tfecchannel++) {
for (tmfec=0;tmfec<9;tmfec++) {
if (qbufn[tmfec][tfecchannel] > 0) qbufsend(tmfec, tfecchannel);
}
}
return 0;
}
//--------------------------------------------------------------------------------
// Send buffered data for one channel
int PixelPh1FECInterface::qbufsend(int mfec, int fecchannel) {
if (buffermodenever) {
std::cerr << "shouldn't have gotten to qbufsend(mfec,fecchannel) with buffermodenever" << std::endl;
return 0;
}
unsigned int ch1stat, ch2stat;
// append a final FF to data
qbuf[mfec][fecchannel][qbufn[mfec][fecchannel]++] = 0xff;
// cout << "\nqbufsend writing "<<qbufn[mfec][fecchannel]<<" bytes\n";
qbufnsend[mfec][fecchannel]++;
//PRINT_ON();
if (PRINT) cout << "PixelPh1FECInterface: " << "qbufsend(" << mfec << ", " << fecchannel << "), ndata = " << qbufn[mfec][fecchannel] << ", ncalls = " << qbufnsend[mfec][fecchannel] << endl;
mfecbusy(mfec, fecchannel, &ch1stat, &ch2stat); //must make sure channel is not busy, wait if necessary!!!!!!
// FIXME could be more efficient - e.g. go to another channel
if (fecdebug==1) {
if (((fecchannel == 1)&&((ch1stat & 0x02) != 0x02) && (ch1stat != 0))||
((fecchannel == 2)&&((ch2stat & 0x02) != 0x02) && (ch2stat != 0))){
qbufnerr[mfec][fecchannel]++;
static unsigned int count=0;
if (count < 50000) {
static ofstream dmp("feccmd.dmp");
count++;
cout << "%Found channel with error: board="<<board_id<<" "<<mfec<<" "
<< fecchannel<<" sent "<<qbufnsend[mfec][fecchannel]
<< " commands and had "<<qbufnerr[mfec][fecchannel]
<< " errors."<<endl;
dmp << (int)qbufn_old[mfec][fecchannel]-1;
for(int i=0;i<qbufn_old[mfec][fecchannel]-1;i++){
dmp << " " << (int) qbuf_old[mfec][fecchannel][i];
}
dmp << endl;
cout << mfec << " " << fecchannel << " " << (int)qbufn_old[mfec][fecchannel];
for(int i=0;i<qbufn_old[mfec][fecchannel];i++){
cout << " " << (int) qbuf_old[mfec][fecchannel][i];
}
cout << endl;
}
}
}
qbufn_old[mfec][fecchannel]=qbufn[mfec][fecchannel];
for(int i=0;i<qbufn[mfec][fecchannel];i++){
qbuf_old[mfec][fecchannel][i]=qbuf[mfec][fecchannel][i];
}
// Now data is ready. Ready to initiate tx. Reset and send go.
// Reset the appropriate mfec channel
// writeCSregister(mfec, fecchannel, 0x08);
// enable arm auto send reset mFEC
writeCSregister(mfec, fecchannel, 0x88);
//cout << "after 88: " << hex << pRegManager->ReadReg("CSReg.CSREGM1") << dec << endl;
std::vector<uint32_t> wordvec;
unsigned int *iword;
int i;
// Now load the data to the word container
if (PRINT) cout<<"PixelPh1FECInterface: Final FEC data (ndata: " << qbufn[mfec][fecchannel] << ")\n";
for (i=0;i<qbufn[mfec][fecchannel];i+=4) {
iword = (unsigned int*) &qbuf[mfec][fecchannel][i];
wordvec.push_back( *iword );
if (PRINT) {
cout<<"PixelPh1FECInterface: ("<<setw(5)<<i<<"): ";
hexprintword(*iword);
cout << endl;
}
}
outputblock(mfec, fecchannel, wordvec);
// Now data is in txdata. Ready to initiate tx. Reset and send go.
writeCSregister(mfec, fecchannel, 0x87);
//cout << "after 87: 0x" << hex << pRegManager->ReadReg("CSReg.CSREGM1") << dec << endl;
//mfecbusy(mfec, fecchannel, &ch1stat, &ch2stat); //must make sure channel is not busy, wait if necessary!!!!!!
//unsigned long csreg;
//getfecctrlstatus(mfec,&csreg);
//cout << "csreg direct read 0x" << hex << csreg << " from mfecbusy 0x" << mfecCSregister[mfec] << dec << endl;
if (1 && PRINT) {
usleep(100000);
std::vector<uint32_t> rb = readreturn(mfec, fecchannel, 16000);
const size_t nrb = rb.size();
const size_t qbn = qbufn[mfec][fecchannel];
std::cout << "PixelPh1FECInterface: JMT readback size = " << nrb << " we sent " << qbn << std::endl;
for (size_t i = 0; i < nrb; ++i) {
cout<<"PixelPh1FECInterface: ("<<setw(5)<<i<<"): ";
hexprintword(rb[i]);
if (i < qbn) {
std::cout << " qbuf: ";
hexprintword(qbuf[mfec][fecchannel][i]);
}
cout << endl;
}
}
qbufn[mfec][fecchannel] = 0;
memset(qbuf[mfec][fecchannel], 0, qbufsize); // JMTBAD for good measure to be sure new FEC doesn't pick up stuff after the 0xFF...
//PRINT_RESTORE();
return 0;
}
//-----------------------------------------------------------------------------------------
// Set the WBC + reset
int PixelPh1FECInterface::rocsetwbc(int mfec, int mfecchannel, int tbmchannel,
int hubaddress, int portaddress, int rocid,
int wbcvalue) {
if (PRINT) cout << "PixelPh1FECInterface: " << std::dec << "rocsetwbc("
<< "mfec=" << mfec << ", "
<< "mfecchannel=" << mfecchannel << ", "
<< "tbmchannel=" << tbmchannel << ", "
<< "hubaddress=" << hubaddress << ", "
<< "portaddress=" << portaddress << ", "
<< "rocid=" << rocid << ", "
<< "wbcvalue=" << wbcvalue << std::endl;
// Clear the buffer if not in buffer mode)
if (qbufn[mfec][mfecchannel] > 0) {
qbufsend(mfec,mfecchannel);
cout << "mfec " << mfec <<":"<<mfecchannel<<" leftover from buffer mode "
<<qbufn[mfec][mfecchannel]<<endl;
}
progdac(mfec, mfecchannel, hubaddress, portaddress, rocid, 254, wbcvalue);
rocreset(mfec, mfecchannel, tbmchannel, hubaddress); // need rocreset here
return 0;
}
//-----------------------------------------------------------------------------
// Set the control register
int PixelPh1FECInterface::rocsetchipcontrolregister(int mfec, int mfecchannel,
int hubaddress,
int portaddress,
int rocid,
int calhighrange,
int chipdisable,
int halfspeed,
const bool buffermode) {
if (PRINT) cout << "PixelPh1FECInterface: " << std::dec << "rocsetchipcontrolregister("
<< "mfec=" << mfec << ", "
<< "mfecchannel=" << mfecchannel << ", "
<< "hubaddress=" << hubaddress << ", "
<< "portaddress=" << portaddress << ", "
<< "rocid=" << rocid << ", "
<< "calhighrange=" << calhighrange << ", "
<< "chipdisable=" << chipdisable << ", "
<< "halfspeed=" << halfspeed << ", "
<< "buffermode=" << buffermode
<< ")" << std::endl;
if (halfspeed)
printf("JMT PixelPh1FECInterface::rocsetchipcontrolregister halfspeed is %i and it's not used any more\n", halfspeed);
unsigned char mydata;
mydata = 0;
if (halfspeed > 0) mydata |= 0x01;
if (chipdisable > 0) mydata |= 0x02;
if (calhighrange > 0) mydata |= 0x04;
// Clear the buffer if not in buffer mode)
if(!buffermode) {
if (qbufn[mfec][mfecchannel] > 0) {
qbufsend(mfec,mfecchannel);
cout << "mfec " << mfec <<":"<<mfecchannel<<" leftover from buffer mode "
<<qbufn[mfec][mfecchannel]<<endl;
}
}
progdac(mfec, mfecchannel, hubaddress, portaddress, rocid, 253, mydata, buffermode);
return 0;
}
//-----------------------------------------------------------------------------------
// Reset TBM
int PixelPh1FECInterface::tbmreset(int mfec, int fecchannel, int tbmchannel,
int hubaddress) {
if (PRINT) cout << "PixelPh1FECInterface: " << "tbmreset: ";
return tbmcmd(mfec, fecchannel, tbmchannel, hubaddress, 4, 2, 16, 0);
}
// Reset ROC
int PixelPh1FECInterface::rocreset(int mfec, int fecchannel, int tbmchannel,
int hubaddress) {
if (PRINT) cout << "PixelPh1FECInterface: " << "rocreset: ";
return tbmcmd(mfec, fecchannel, tbmchannel, hubaddress, 4, 2, 4, 0);
}
//
//--------------------------------------------------------------------------------------
// Clear calibrate, works for the whole ROC
// Single of buffered command.
int PixelPh1FECInterface::clrcal(int mfec, int fecchannel,