forked from psi46/psi46test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_ana.cpp
1244 lines (989 loc) · 26 KB
/
test_ana.cpp
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 "psi46test.h"
#include "chipdatabase.h"
#include "profiler.h"
#include <iostream>
#include <iomanip>
#include <sstream>
#include "datastream.h"
#define VCAL_TEST 35 // 35 (high range) pixel alive test
// schwelle @ 40MHz = 17
#define VCAL_DCOL_TEST 65 // 30 ... 120
#define VCAL_LEVEL 140 // 135 vcal for level test without sensor
#define VCAL_LEVEL_SENSOR 45 // 45 vcal for level test with sensor
#define VCAL_LEVEL_EXT 150 // 150 vcal for external calibrate
#define VANA0 145 // default vana value
#define VDIG0 4
#define VSH0 150 // 225
namespace TestRocAna
{
// =======================================================================
// initialization
// =======================================================================
int tct_wbc = 0;
void WriteSettings()
{
Log.section("SETTINGS");
Log.printf("f=40\n");
}
void InitDAC(bool reset)
{ PROFILING
if (reset)
{
g_chipdata.InitVana = VANA0;
}
tb.roc_SetDAC( 1, VDIG0); // Vdig
tb.roc_SetDAC( 2, g_chipdata.InitVana);
tb.roc_SetDAC( 3, 30); // Vsf
tb.roc_SetDAC( 4, 12); // Vcomp
tb.roc_SetDAC( 5, 0); // Vleak_comp
tb.roc_SetDAC( 6, 0); // VrgPr
tb.roc_SetDAC( 7, 35); // VwllPr
tb.roc_SetDAC( 8, 0); // VrgSh
tb.roc_SetDAC( 9, 35); // VwllSh
tb.roc_SetDAC( 10, 160); // VhldDel
tb.roc_SetDAC( 11, 7); // Vtrim
tb.roc_SetDAC( 12, 70); // VthrComp
tb.roc_SetDAC( 13, 30); // VIBias_Bus
tb.roc_SetDAC( 14, 10); // Vbias_sf
tb.roc_SetDAC( 15, 50); // VoffsetOp
tb.roc_SetDAC( 16, 115); // VIbiasOp
tb.roc_SetDAC( 17, 120); // VoffsetRO
tb.roc_SetDAC( 18, 115); // VIon
tb.roc_SetDAC( 19, 220); // VIbias_PH
tb.roc_SetDAC( 20, 153); // VIbias_DAC
tb.roc_SetDAC( 21, 220); // VIbias_roc
tb.roc_SetDAC( 22, 99); // VIColOr
tb.roc_SetDAC( 23, 0); // Vnpix
tb.roc_SetDAC( 24, 0); // VsumCol
tb.roc_SetDAC( 25, 2); // Vcal
tb.roc_SetDAC( 26, 40); // 30 CalDel
tb.roc_SetDAC( 27, 0); // RangeTemp
tb.roc_SetDAC( 0xfe, 20); // WBC
tb.roc_SetDAC( 0xfd, 4); // CtrlReg
tb.Flush();
}
void InitChip()
{ PROFILING
InitDAC(true);
tb.roc_ClrCal();
tb.roc_Chip_Mask();
tb.Flush();
}
void SetMHz(int MHz = 0)
{ PROFILING
tb.Sig_SetDelay(SIG_CLK, settings.adc_clkDelay );
tb.Sig_SetDelay(SIG_SDA, settings.adc_clkDelay+15);
tb.Sig_SetDelay(SIG_CTR, settings.adc_clkDelay);
tb.Sig_SetDelay(SIG_TIN, settings.adc_clkDelay+5);
tb.Flush();
tct_wbc = 5;
}
// =======================================================================
// chip startup
// =======================================================================
#define ERROR_IMAX 1
#define ERROR_IMIN 2
// ROCDIGTEST: GetVD_CAP GetVDAC_CAP GetTOUT_COM GetAOUT_COM not implemented
int test_startup(bool probecard)
{ PROFILING
double Idig = 0.0, Iana = 0.0;
// power on, supply current limits
Log.section("PON", false);
Log.printf(" %i\n", nEntry);
g_chipdata.nEntry = nEntry;
tb.Pon();
tb.mDelay(400);
g_chipdata.IdigOn = Idig = tb.GetID()*1000.0;
Log.printf("Idig=%6.2lf mA\n", Idig);
g_chipdata.IanaOn = Iana = tb.GetIA()*1000;
Log.printf("Iana=%6.2lf mA\n", Iana);
if (Idig >120.0 || Iana > 120.0 ) return ERROR_IMAX;
// initialize, supply currents
Log.section("INIT");
InitChip();
tb.mDelay(300);
g_chipdata.IdigInit = Idig = tb.GetID()*1000.0;
Log.printf("Idig=%6.2lf mA\n", Idig);
g_chipdata.IanaInit = Iana = tb.GetIA()*1000.0;
Log.printf("Iana=%6.2lf mA\n", Iana);
if (Idig >100.0 || Iana >100.0) return ERROR_IMAX;
if (Idig < 10.0 || Iana < 1.0) return ERROR_IMIN;
g_chipdata.probecard.isValid = probecard;
if (probecard)
{
// check return voltages
tb.GetVD_Cap(); tb.GetVD_Cap(); tb.GetVD_Cap(); tb.GetVD_Cap();
g_chipdata.probecard.vd_cap =tb.GetVD_Cap();
Log.section("VDCAP", false);
Log.printf("%5.3f\n", g_chipdata.probecard.vd_cap);
tb.GetVD_Reg(); tb.GetVD_Reg(); tb.GetVD_Reg(); tb.GetVD_Reg();
g_chipdata.probecard.vd_reg = tb.GetVD_Reg();
Log.section("VDREG", false);
Log.printf("%5.3f\n", g_chipdata.probecard.vd_reg);
tb.GetVDAC_Reg(); tb.GetVDAC_Reg(); tb.GetVDAC_Reg(); tb.GetVDAC_Reg();
g_chipdata.probecard.v_dac = tb.GetVDAC_Reg();
Log.section("VDAC", false);
Log.printf("%5.3f\n", g_chipdata.probecard.v_dac);
g_chipdata.probecard.v_tout = 0; // tb.GetTOUT_COM();
Log.section("VTOUT", false);
Log.printf("%5.3f\n", g_chipdata.probecard.v_tout);
g_chipdata.probecard.v_aout = 0; // tb.GetAOUT_COM();
Log.section("VAOUT", false);
Log.printf("%5.3f\n", g_chipdata.probecard.v_aout);
}
return 0;
}
// =======================================================================
// token out check
// =======================================================================
#define ERROR_TOKEN_MISSING 3
#define ERROR_TOKEN_TIME 4
int test_tout()
{ PROFILING
Log.section("TOKEN", false);
InitDAC();
tb.roc_SetDAC( CtrlReg, 0x04);
tb.Pg_SetCmd(0, PG_RESR + 20);
tb.Pg_SetCmd(1, PG_TOK);
unsigned int cnt;
CDtbSource src;
CDataRecordScannerROC raw;
CSink<CDataRecord*> data;
src >> raw >> data;
src.OpenRocAna(tb, 14, 10, 300, false, 5000);
try
{
src.Enable();
tb.Pg_Single();
tb.uDelay(4000);
src.Disable();
cnt = data.Get()->GetSize();
cnt = cnt >= 3 ? (cnt-3)/6 : 0;
if (cnt > 255) cnt = 255;
} catch (DataPipeException e) { cnt = 255; }
src.Close();
tb.Flush();
g_chipdata.token = cnt;
Log.printf(" %i\n", cnt);
if (cnt == 255) return ERROR_TOKEN_MISSING;
if (cnt != 0) return ERROR_TOKEN_TIME; // no empty readout
return 0;
}
// =======================================================================
// I2C address scan
// =======================================================================
#define ERROR_I2C 5
#define ERROR_I2C0 6 // address 0 works
void check_20_40(int addr_i2c)
{
tb.roc_I2cAddr(addr_i2c);
tb.roc_SetDAC(CtrlReg, 0x05);
tb.cDelay(10);
tb.Pg_Single();
tb.cDelay(50);
tb.roc_SetDAC(CtrlReg, 0x04);
tb.cDelay(5);
}
int test_i2c()
{ PROFILING
// --- init
Log.section("I2C");
tb.Pg_SetCmd(0, PG_RESR+20);
tb.Pg_SetCmd(1, PG_TOK);
int i, k;
unsigned int mask;
CDtbSource src;
CDataRecordScannerROC raw;
CSink<CDataRecord*> data;
src >> raw >> data;
src.OpenRocAna(tb, 14, 10, 300, false, 1000);
src.Enable();
// scan adresses
for (i=0; i<16; i++)
{
tb.SetRocAddress(i);
tb.cDelay(16000);
for (k=0; k<16; k++) check_20_40(k);
}
tb.SetRocAddress(0);
tb.roc_I2cAddr(0);
tb.cDelay(10000);
src.Disable();
tb.Flush();
unsigned int res[16];
for (i=0; i<16; i++) res[i] = 0;
std::stringstream sslog;
sslog << " ";
for (i=0; i<16; i++) sslog << std::hex << std::setw(2) << i;
sslog << std::endl;
for (i=0; i<16; i++)
{
sslog << std::setw(2) << std::hex << i << ": ";
try
{
for (k=0, mask=1; k<16; k++, mask<<=1)
{
CDataRecord *px = data.Get();
if (px->GetSize() > 3)
{
res[i] |= mask;
sslog << "1 ";
}
else sslog << ". ";
}
} catch (DataPipeException e) {}
sslog << std::endl;
}
Log.puts(sslog.str());
// check results
for (i=0, mask=1; i<16; i++, mask<<=1)
{
if (mask != res[i])
{ // i2c error
for (k=0; k<16; k++) Log.printf("%04X\n", res[k]);
return (res[0]&0x0001) ? ERROR_I2C : ERROR_I2C0;
}
}
g_chipdata.i2c = 1;
return 0;
}
// =======================================================================
// Va supply current characteristic
// =======================================================================
#define VANASTEPS 5
double getIana(int dac, bool prot = false)
{ PROFILING
double Iana;
tb.Pg_SetCmd(0, PG_RESR);
tb.Pg_Single();
tb.Flush();
tb.uDelay(100);
tb.roc_SetDAC(Vana, dac);
// tb.Flush();
tb.mDelay(200);
Iana = tb.GetIA()*1000.0;
if (prot)
{
Log.section("IANA",false);
Log.printf("%3i %6.2lf mA\n", dac, Iana);
}
return Iana;
}
void test_current()
{ PROFILING
int xmin = 30;
tb.roc_SetDAC(VwllPr, 0);
tb.roc_SetDAC(VwllSh, 0);
tb.Flush();
// Iana @ Vana
double ia = 0.0;
const int dac[VANASTEPS] = { 64, 96, 128, 160, 192 };
Log.section("VANA");
for (int i=0; i<VANASTEPS; i++)
{
ia = getIana(dac[i]);
g_chipdata.Iana[i] = ia;
Log.printf("%3i %6.2lf mA\n", dac[i], ia);
if (ia<24.0) xmin = dac[i];
}
// set Iana to 24+/-2 mA
if (xmin==0) return;
int xmax = xmin+30, x=0;
for (int n=0; n<6; n++)
{
x = (xmin+xmax)/2;
ia = getIana(x);
if (ia < 0.0) break;
if (ia > 24.0) xmax = x; else xmin = x;
}
g_chipdata.InitVana = (ia>=0.0)? x : -1;
g_chipdata.InitIana = ia;
Log.section("ITRIM", false);
Log.printf("%i %1.2lf mA\n", g_chipdata.InitVana, g_chipdata.InitIana);
InitDAC();
}
// =======================================================================
// calibrate analog decoding
// =======================================================================
int ub_level;
int b_level;
void test_calibrate_decoding()
{ PROFILING
// load settings
Log.section("CALREADOUT", false);
InitDAC();
tb.roc_Chip_Mask();
tb.Pg_SetCmd(0, PG_RESR + 25);
tb.Pg_SetCmd(1, PG_TOK);
tb.uDelay(100);
tb.Flush();
CDtbSource src;
CDataRecordScannerROC raw;
// CRocRawDataPrinter debug("debug.txt", true);
CSink<CDataRecord*> data;
// src >> raw >> debug >> data;
src >> raw >> data;
src.OpenRocAna(tb, 14, 10, 100, false, 1000);
src.Enable();
// --- scan all pixel ------------------------------------------------------
int i;
for (i=0; i<30; i++)
{
tb.Pg_Single();
tb.uDelay(10);
}
src.Disable();
int ub = 0, b = 0, n = 0;
try
{
for (i=0; i<30; i++)
{
CDataRecord *ev = data.Get();
if (ev->GetSize() >= 2)
{
ub += CAnalogLevelDecoder::ExpandSign((*ev)[0]);
b += CAnalogLevelDecoder::ExpandSign((*ev)[1]);
n++;
}
}
}
catch (DataPipeException e)
{
printf("\nERROR test_calibrate_decoding: %s\n", e.what());
}
if (n>0)
{
ub_level = ub/n;
b_level = b/n;
}
else
{
ub_level = -350;
b_level = 0;
}
Log.printf(" %i %i\n", ub_level, b_level);
}
// =======================================================================
// pixel alive test
// =======================================================================
void test_pixel()
{ PROFILING
// load settings
InitDAC();
tb.roc_Chip_Mask();
tb.roc_SetDAC(Vcal, VCAL_TEST);
tb.roc_SetDAC(CtrlReg,0x04); // 0x04
tb.Pg_SetCmd(0, PG_RESR + 25);
tb.Pg_SetCmd(1, PG_CAL + 20 + tct_wbc);
tb.Pg_SetCmd(2, PG_TRG + 16);
tb.Pg_SetCmd(3, PG_TOK);
tb.uDelay(100);
tb.Flush();
CDtbSource src;
CDataRecordScannerROC raw;
// CLevelHistogram hist;
CRocAnaDecoder dec;
dec.Calibrate(ub_level, b_level);
CSink<CEvent*> data;
// src >> raw >> hist >> dec >> data;
src >> raw >> dec >> data;
src.OpenRocAna(tb, 14, 10, 100, false, 100000);
src.Enable();
// --- scan all pixel ------------------------------------------------------
unsigned char col, row;
for (col=0; col<ROC_NUMCOLS; col++)
{
tb.roc_Col_Enable(col, true);
tb.uDelay(10);
for (row=0; row<ROC_NUMROWS; row++)
{
tb.roc_Pix_Cal (col, row, false);
tb.uDelay(20);
tb.Pg_Single();
tb.uDelay(10);
tb.roc_Pix_Trim(col, row, 15);
tb.uDelay(5);
tb.Pg_Single();
tb.uDelay(10);
tb.roc_Pix_Mask(col, row);
tb.roc_ClrCal();
}
tb.roc_Col_Enable(col, false);
tb.uDelay(10);
}
src.Disable();
// --- analyze data --------------------------------------------------------
// for each col, for each row, (masked pixel, unmasked pixel)
try
{
for (col=0; col<ROC_NUMCOLS; col++)
{
for (row=0; row<ROC_NUMROWS; row++)
{
// must be empty readout
CEvent *ev = data.Get();
g_chipdata.pixmap.SetMaskedCount(col, row, ev->roc[0].pixel.size());
// must be single pixel hit
ev = data.Get();
g_chipdata.pixmap.SetUnmaskedCount(col, row, ev->roc[0].pixel.size());
if (ev->roc[0].pixel.size() > 0)
{
g_chipdata.pixmap.SetDefectColCode(col, row, ev->roc[0].pixel[0].x != col);
g_chipdata.pixmap.SetDefectRowCode(col, row, ev->roc[0].pixel[0].y != row);
g_chipdata.pixmap.SetPulseHeight(col, row, ev->roc[0].pixel[0].ph);
}
}
}
} catch (DataPipeException e) { printf("\nERROR TestPixel: %s\n", e.what()); }
// hist.Report(Log);
src.Close();
tb.roc_SetDAC(CtrlReg,0);
}
// =======================================================================
// pulse height
// =======================================================================
#define PULSE_VCAL1 40 // High Range
#define PULSE_VCAL2 60 // High Range
void test_pulse_height1()
{ PROFILING
InitDAC();
// load individual settings
tb.roc_SetDAC(CtrlReg, 0);
tb.roc_SetDAC(Vcal, PULSE_VCAL1);
tb.roc_SetDAC(CtrlReg, 0x04);
tb.Pg_SetCmd(0, PG_RESR + 25);
tb.Pg_SetCmd(1, PG_CAL + 15 + tct_wbc);
tb.Pg_SetCmd(2, PG_TRG + 16);
tb.Pg_SetCmd(3, PG_TOK);
tb.uDelay(100);
tb.Flush();
CDtbSource src;
CDataRecordScannerROC raw;
CRocDigDecoder dec;
CSink<CEvent*> data;
src >> raw >> dec >> data;
src.OpenRocDig(tb, settings.deser160_tinDelay, false, 100000);
src.Enable();
int col, row;
// scan data
for (col=0; col<ROC_NUMCOLS; col++)
{
tb.roc_Col_Enable(col, true);
tb.uDelay(10);
for (row=0; row<ROC_NUMROWS; row++)
{
tb.roc_Pix_Cal (col, row, false);
tb.roc_Pix_Trim(col, row, 15);
tb.uDelay(5);
tb.Pg_Single();
tb.uDelay(10);
tb.roc_Pix_Mask(col, row);
tb.roc_ClrCal();
}
tb.roc_Col_Enable(col, false);
tb.uDelay(10);
}
src.Disable();
// analyze data
try
{
for (col=0; col<ROC_NUMCOLS; col++) for (row=0; row<ROC_NUMROWS; row++)
{
CEvent *ev = data.Get();
if (ev->roc[0].pixel.size() != 0) g_chipdata.pixmap.SetPulseHeight1(col,row, ev->roc[0].pixel[0].ph);
}
} catch (DataPipeException e) { printf("\nERROR Test Pulse Height 1: %s\n", e.what()); return; }
g_chipdata.pixmap.pulseHeight1Exist = true;
}
void test_pulse_height2()
{ PROFILING
InitDAC();
// load individual settings
tb.roc_SetDAC(CtrlReg, 0);
tb.roc_SetDAC(Vcal, PULSE_VCAL2);
tb.roc_SetDAC(CtrlReg, 0x04);
tb.Pg_SetCmd(0, PG_RESR + 25);
tb.Pg_SetCmd(1, PG_CAL + 15 + tct_wbc);
tb.Pg_SetCmd(2, PG_TRG + 16);
tb.Pg_SetCmd(3, PG_TOK);
tb.uDelay(100);
tb.Flush();
CDtbSource src;
CDataRecordScannerROC raw;
CRocDigDecoder dec;
CSink<CEvent*> data;
src >> raw >> dec >> data;
src.OpenRocDig(tb, settings.deser160_tinDelay, false, 100000);
src.Enable();
int col, row;
// scan data
for (col=0; col<ROC_NUMCOLS; col++)
{
tb.roc_Col_Enable(col, true);
tb.uDelay(10);
for (row=0; row<ROC_NUMROWS; row++)
{
tb.roc_Pix_Cal (col, row, false);
tb.roc_Pix_Trim(col, row, 15);
tb.uDelay(5);
tb.Pg_Single();
tb.uDelay(10);
tb.roc_Pix_Mask(col, row);
tb.roc_ClrCal();
}
tb.roc_Col_Enable(col, false);
tb.uDelay(10);
}
src.Disable();
// analyze data
try
{
for (col=0; col<ROC_NUMCOLS; col++) for (row=0; row<ROC_NUMROWS; row++)
{
CEvent *ev = data.Get();
if (ev->roc[0].pixel.size() != 0) g_chipdata.pixmap.SetPulseHeight2(col,row, ev->roc[0].pixel[0].ph);
}
} catch (DataPipeException e) { printf("\nERROR Test Pulse Height 2: %s\n", e.what()); return; }
g_chipdata.pixmap.pulseHeight2Exist = true;
}
// =======================================================================
// test pixel threshold
// =======================================================================
bool GetPixel(CDtbSource &src, CSink<CEvent*> &data, unsigned int x)
{ PROFILING
const unsigned int count = 9;
unsigned int i;
unsigned int n = 0;
tb.roc_SetDAC(VthrComp, x);
tb.uDelay(30);
src.Enable();
for (i=0; i<count; i++)
{
tb.Pg_Single();
tb.uDelay(5);
}
src.Disable();
for (i=0; i<count; i++)
{
CEvent *ev = data.Get();
if (ev->roc[0].pixel.size() > 0) n++;
}
return n > count/2;
}
unsigned char FindLevel(CDtbSource &src, CSink<CEvent*> &data)
{ PROFILING
static unsigned char x = 20; // first estimation
if (x>80) x = 80; else if (x<1) x=1;
try
{
if (GetPixel(src, data, x))
{
do x--; while (GetPixel(src, data, x) && x>0);
x++;
}
else
{
do x++; while (!GetPixel(src, data, x) && x<100);
if (x>100) x=100;
}
} catch (DataPipeException e) { x = 20; throw; }
return x;
}
unsigned char test_PUC(CDtbSource &src, CSink<CEvent*> &data, unsigned char col, unsigned char row, unsigned char trim)
{ PROFILING
tb.roc_Pix_Trim(col, row, trim);
tb.roc_Pix_Cal (col, row, 0);
unsigned char res = FindLevel(src, data);
tb.roc_ClrCal();
tb.roc_Pix_Mask(col,row);
return res;
}
void testColPixel(CDtbSource &src, CSink<CEvent*> &data, unsigned char col, unsigned char trimbit, unsigned char res[])
{ PROFILING
unsigned char row;
tb.roc_Col_Enable(col, 1);
for(row=0; row<ROC_NUMROWS; row++)
{
res[row] = test_PUC(src, data, col,row,trimbit);
}
tb.roc_Col_Enable(col, 0);
}
void testAllPixel(CDtbSource &src, CSink<CEvent*> &data, int vtrim, unsigned int trimbit=4 /* reference */ )
{ PROFILING
unsigned char res[ROC_NUMROWS];
tb.roc_SetDAC(Vtrim, vtrim);
tb.uDelay(100);
unsigned int trimvalue = (trimbit<4) ? (~(0x01<<trimbit)&15) : 15;
int col, row;
for (col=0; col<ROC_NUMCOLS; col++)
{
testColPixel(src, data, col,trimvalue,res);
for(row=0; row<ROC_NUMROWS; row++)
{
if (trimbit>3) g_chipdata.pixmap.SetRefLevel(col,row,res[row]);
else g_chipdata.pixmap.SetLevel(col,row,trimbit,res[row]);
}
}
}
int test_PUCs(bool forceDefTest = false)
{ PROFILING
tb.Pg_SetCmd(0, PG_RESR + 25);
tb.Pg_SetCmd(1, PG_CAL + 15 + tct_wbc);
tb.Pg_SetCmd(2, PG_TRG + 16);
tb.Pg_SetCmd(3, PG_TOK);
tb.uDelay(100);
tb.Flush();
CDtbSource src;
CDataRecordScannerROC raw;
CRocDigDecoder dec;
CSink<CEvent*> data;
src >> raw >> dec >> data;
src.OpenRocDig(tb, settings.deser160_tinDelay, false, 10000);
InitDAC();
tb.roc_SetDAC(Vcal, settings.sensor ? VCAL_LEVEL_SENSOR : VCAL_LEVEL);
try
{
testAllPixel(src, data, 80);
testAllPixel(src, data, 80, 3);
testAllPixel(src, data, 110, 2);
testAllPixel(src, data, 150, 1);
testAllPixel(src, data, 255, 0); // 200
g_chipdata.pixmap.UpdateTrimDefects();
} catch (DataPipeException e) { printf("\nERROR test_PUCs: %s\n", e.what()); }
InitDAC();
return 0;
}
// =======================================================================
// test pixel threshold
// =======================================================================
int PixelFired(vector<uint16_t> x, int &pos)
{ PROFILING
// check header
if (pos >= int(x.size())) return -1; // missing data
if ((x[pos] & 0x8ffc) != 0x87f8) return -2; // wrong header
pos++;
if (pos >= int(x.size()) || (x[pos] & 0x8000)) return 0; // empty data readout
// read additional noisy pixel
while (!(pos >= int(x.size()) || (x[pos] & 0x8000))) { pos++; }
return 1;
}
int GetPixelC(unsigned int x)
{ PROFILING
const unsigned int count = 20;
unsigned int i;
unsigned int n = 0;
tb.roc_SetDAC(Vcal, x);
tb.uDelay(30);
tb.Daq_Start();
for (i=0; i<count; i++)
{
tb.Pg_Single();
tb.uDelay(5);
}
tb.Daq_Stop();
vector<uint16_t> data;
tb.Daq_Read(data, 10000);
int pos = 0;
for (i=0; i<count; i++)
{
int res = PixelFired(data, pos);
if (res > 0) n++;
else if (res < 0) return res;
}
return (n > count/2) ? 1 : 0;
}
int FindLevelC()
{ PROFILING
static unsigned char x = 9; // first estimation
if (x>253) x = 200; else if (x<1) x=1;
int res = GetPixelC(x);
if (res < 0) return res;
if (res > 0)
{
do
{
x--;
res = GetPixelC(x);
if (res < 0) return res;
} while ((res > 0) && x>0);
x++;
}
else
{
do
{
x++;
res = GetPixelC(x);
if (res < 0) return res;
} while (!(res > 0) && x<253);
}
return x;
}
int test_PUCC(unsigned char col, unsigned char row, unsigned char trim)
{ PROFILING
tb.roc_Pix_Trim(col, row, trim);
tb.roc_Pix_Cal (col, row, 0);
int res = FindLevelC();
tb.roc_ClrCal();
tb.roc_Pix_Mask(col,row);
return res;
}
bool testColPixelC(uint8_t col, uint8_t trimbit, vectorR<uint8_t> &res)
{ PROFILING
unsigned char row;
tb.roc_Col_Enable(col, 1);
res.clear();
res.reserve(ROC_NUMROWS);
for(row=0; row<ROC_NUMROWS; row++)
{
int r = test_PUCC(col,row,trimbit);
if (r < 0) return false;
res.push_back(r);
}
tb.roc_Col_Enable(col, 0);
return true;
}
bool testAllPixelC(int vtrim, unsigned int trimbit=4 /* reference */ )
{ PROFILING
vector<uint8_t> res;
tb.roc_SetDAC(Vtrim, vtrim);
tb.uDelay(100);
unsigned int trimvalue = (trimbit<4) ? (~(0x01<<trimbit)&15) : 15;
int col, row;
for (col=0; col<ROC_NUMCOLS; col++)
{
if (!tb.TestColPixel(col,trimvalue,false,res)) return false;
for(row=0; row<ROC_NUMROWS; row++)
{
if (trimbit>3) g_chipdata.pixmap.SetRefLevel(col,row,res[row]);
else g_chipdata.pixmap.SetLevel(col,row,trimbit,res[row]);
}
}
return true;
}
int test_PUCsC(bool forceDefTest = false)
{ PROFILING
tb.Pg_SetCmd(0, PG_RESR + 25);
tb.Pg_SetCmd(1, PG_CAL + 15 + tct_wbc);
tb.Pg_SetCmd(2, PG_TRG + 16);
tb.Pg_SetCmd(3, PG_TOK);
tb.uDelay(100);
tb.Flush();
CDtbSource src;
src.OpenRocDig(tb, settings.deser160_tinDelay, false, 100000);
InitDAC();
tb.roc_SetDAC(VthrComp, 40); // 20
tb.roc_SetDAC(CtrlReg,0x00); // 0x04
testAllPixelC( 0);
testAllPixelC(100, 3);
testAllPixelC(255, 2); // 110
// testAllPixelC(255, 1); // 150
// testAllPixelC(255, 0); // 200
g_chipdata.pixmap.UpdateTrimDefects();
InitDAC();
return 0;
}
// =======================================================================
//
// digital ROC test
//
// =======================================================================
void test_cleanup(int bin)
{ PROFILING
tb.Init();
tb.Flush();
g_chipdata.bin = bin;
g_chipdata.Calculate();
Log.section("CLASS", false);
Log.printf(" %i\n", g_chipdata.chipClass);
Log.section("POFF", false);
Log.printf(" %i\n", bin);
}
int test_roc(bool &repeat)