-
Notifications
You must be signed in to change notification settings - Fork 8
/
test_PROC600.cpp
1555 lines (1220 loc) · 33.6 KB
/
test_PROC600.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 20 // 20 50 60 (high range) pixel alive test
// schwelle @ 40MHz = 17
#define VCAL_DCOL_TEST 65 // 30 ... 120
#define VCAL_LEVEL 40 // 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 100 // default vana value
#define VDIG0 4
#define VSH0 150 // 225
#define MAX_TIME_BUFFER 56 // number of timestamp buffers
namespace TestPROC600
{
// =======================================================================
// 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;
g_chipdata.InitCalDel = 68;
}
tb.roc_SetDAC( 1, VDIG0); // Vdig
tb.roc_SetDAC( 2, g_chipdata.InitVana);
tb.roc_SetDAC( 3, 8); // *new* Iph (new 4 bit, previously Vsf)
tb.roc_SetDAC( 4, 12); // Vcomp
tb.roc_SetDAC( 7, 150); // VwllPr
tb.roc_SetDAC( 9, 150); // VwllSh
tb.roc_SetDAC( 11, 40); // Vtrim
tb.roc_SetDAC( 12, 80); // VthrComp
tb.roc_SetDAC( 13, 100); // *new* VColor (previously VIBias_Bus)
tb.roc_SetDAC( 17, 125); // *new* VoffsetRO (previously default: 170)
tb.roc_SetDAC( 19, 50); // Vcomp_ADC
tb.roc_SetDAC( 20, 90); // VIref_ADC
tb.roc_SetDAC( 25, 2); // Vcal
tb.roc_SetDAC( 26, g_chipdata.InitCalDel); // CalDel
tb.roc_SetDAC( 0xfe, 14); // WBC
tb.roc_SetDAC( 0xfd, 0x0c); // *new* CtrlReg (trigger output control)
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.deser160_clkDelay);
tb.Sig_SetDelay(SIG_SDA, settings.deser160_clkDelay+15);
tb.Sig_SetDelay(SIG_CTR, settings.deser160_clkDelay);
tb.Sig_SetDelay(SIG_TIN, settings.deser160_clkDelay+5);
tb.Flush();
tct_wbc = 4;
}
// =======================================================================
// 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
// 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.OpenRocDig(tb, settings.deser160_tinDelay, false, 1000);
try
{
src.Enable();
tb.Pg_Single();
tb.uDelay(4000);
src.Disable();
cnt = data.Get()->GetSize();
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 != 1) return ERROR_TOKEN_TIME; // no empty readout
return 0;
}
// =======================================================================
// caldel scan
// =======================================================================
bool CalDelScan(int col, int row)
{ PROFILING
const int max_caldel = 200;
int x, k;
InitDAC(false);
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 + 15 + tct_wbc);
tb.Pg_SetCmd(2, PG_TRG + 16);
tb.Pg_SetCmd(3, PG_TOK);
for (int i=0; i<ROC_NUMCOLS; i++) tb.roc_Col_Enable(i, true);
tb.roc_Pix_Trim(col, row, 15);
tb.roc_Pix_Cal(col, row);
// --- take data
CDtbSource src;
CDataRecordScannerROC raw;
CSink<CDataRecord*> data;
src >> raw >> data;
src.OpenRocDig(tb, settings.deser160_tinDelay, false, 50000);
src.Enable();
for (x = 0; x<=max_caldel; x++)
{
tb.roc_SetDAC(CalDel, x);
tb.uDelay(100);
for (k=0; k<10; k++)
{
tb.Pg_Single();
tb.uDelay(5);
}
}
src.Disable();
tb.roc_Pix_Mask(col, row);
tb.roc_ClrCal();
// --- analyze data
int pos = 0, count;
string s;
s.reserve(max_caldel+1);
try
{
for (x = 0; x<=max_caldel; x++)
{
count = 0;
for (k=0; k<10; k++) if (data.Get()->GetSize() > 1) count++;
if (count == 0) s.push_back('.');
else if (count >= 10) s.push_back('*');
else s.push_back('0' + count);
}
} catch (DataPipeException e) { printf("\nERROR CalDelScan\n"); return false; }
src.Close();
unsigned int x1, x2, xdiff, xmean;
x1 = s.find("********");
if (x1 == string::npos) return false;
x2 = x1+1;
while (x2 < (s.size()-1) && s[x2] == '*') x2++;
xdiff = x2 - x1;
xmean = (x1 + x2)/2;
Log.section("CALDELSCAN", false);
Log.printf("%i %i %u\n%s\n", col, row, xmean, s.c_str());
g_chipdata.InitCalDel = xmean;
return true;
}
void CalDelScan()
{ PROFILING
int i = 0;
while (!CalDelScan(24+2*i,40+i) && i < 4) i++;
}
// =======================================================================
// I2C address scan
// =======================================================================
#define ERROR_I2C 5
#define ERROR_I2C0 6 // address 0 works
/*
0000 I2C data
0001 I2C address
0010 I2C pixel column
0011 I2C pixel row
1000 VD unreg
1001 VA unreg
1010 VA reg
1011 V bandgap
1100 IA
{ rocaddr[3:0], sana, s[2:0], data[7:0] }
*/
int GetReadback()
{ PROFILING
int i;
// --- take data
CDtbSource src;
CDataRecordScannerROC raw;
CReadBack rdb;
CSink<CDataRecord*> pump;
// CRocRawDataPrinter debug("debug.txt", false);
src >> raw /* >> debug */ >> rdb >> pump;
src.OpenRocDig(tb, settings.deser160_tinDelay, false, 10000);
src.Enable();
for (i=0; i<32; i++)
{
tb.Pg_Single();
tb.uDelay(10);
}
src.Disable();
// read out data
try { pump.GetAll(); } catch (DataPipeException) {}
src.Close();
return rdb.IsUpdated() ? rdb.GetRdbData() : 0;
}
bool Check_Prog(int addr_i2c)
{ PROFILING
tb.roc_I2cAddr(addr_i2c);
tb.roc_SetDAC(Vcal, 0x0);
if ((GetReadback() & 0xff) != 0x00) return false;
tb.roc_SetDAC(Vcal, 0x5);
if ((GetReadback() & 0xff) != 0x05) return false;
return true;
}
int test_i2c()
{ PROFILING
// --- init
Log.section("I2C");
tb.Pg_SetCmd(0, PG_TOK);
tb.roc_SetDAC(255, 0);
int i, k;
unsigned int mask;
unsigned int res[16];
int error = 0;
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 << ": ";
tb.SetRocAddress(i);
tb.uDelay(400);
for (k=0, mask=1; k<16; k++, mask<<=1)
if (Check_Prog(k)) { res[i] |= mask; sslog << "1 "; } else { sslog << ". "; }
sslog << std::endl;
}
tb.SetRocAddress(0);
tb.roc_I2cAddr(0);
tb.cDelay(10000);
tb.Flush();
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;
}
// =======================================================================
// readback test
// =======================================================================
void test_readback()
{ PROFILING
tb.Pg_SetCmd(0, PG_TOK);
tb.roc_SetDAC(0xff, 8);
int vdig_u = GetReadback() & 0xff;
vdig_u = GetReadback() & 0xff;
tb.roc_SetDAC(0xff, 9);
int vana_u = GetReadback() & 0xff;
tb.roc_SetDAC(0xff, 10);
int vana_r = GetReadback() & 0xff;
tb.roc_SetDAC(0xff, 11);
int vbg = GetReadback() & 0xff;
tb.roc_SetDAC(0xff, 12);
int iana = GetReadback() & 0xff;
Log.section("READBACK");
double vd = tb.GetVD();
double va = tb.GetVA();
double ia = tb.GetIA()*1000.0;
if(vdig_u == 0) return;
double cal = vd/vdig_u;
double vdig_u_V = cal*vdig_u;
Log.printf("Vdig_u %3i %5.2lf %5.2lf \n", vdig_u, vdig_u_V, vd);
double vana_u_V = cal*vana_u;
Log.printf("Vana_u %3i %5.2lf %5.2lf\n", vana_u, vana_u_V, va);
double vana_r_V = cal/2*vana_r;
Log.printf("Vana_r %3i %5.2lf \n", vana_r, vana_r_V);
double vbg_V = cal/2*vbg;
Log.printf("Vbg %3i %5.2lf \n", vbg, vbg_V);
double iana_mA = cal*15.0*iana;
Log.printf("Iana %3i %5.1lf %5.1lf\n", iana, iana_mA, ia);
}
// =======================================================================
// 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;
int xmax = 140;
tb.roc_SetDAC(VwllPr, 0);
tb.roc_SetDAC(VwllSh, 0);
tb.Pg_SetCmd(0, PG_TOK);
tb.Flush();
// Iana @ Vana
double ia = 0.0;
const int dac[VANASTEPS] = { 20, 60, 100, 140, 180 };
Log.section("VANA");
for (int i=0; i<VANASTEPS; i++)
{
ia = getIana(dac[i]);
g_chipdata.Iana[i] = ia;
// readback current and voltage
tb.Pg_SetCmd(0, PG_TOK);
tb.roc_SetDAC(0xff, 9);
int vana_u = GetReadback() & 0xff;
tb.roc_SetDAC(0xff, 10);
int vana_r = GetReadback() & 0xff;
tb.roc_SetDAC(0xff, 12);
int iana = GetReadback() & 0xff;
Log.printf("%3i %6.2lf mA %3i %3i %3i \n", dac[i], ia, vana_u, vana_r, iana);
if (ia<24.0) xmin = dac[i];
if ((ia>24.0)&&(ia<30)) xmax=dac[i];
}
// set Iana to 24+/-2 mA
if (xmin==0) return;
int 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();
}
// =======================================================================
// 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 + 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;
CRocDigLinearDecoder dec;
CSink<CEvent*> data;
src >> raw >> dec >> data;
src.OpenRocDig(tb, settings.deser160_tinDelay, 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()); }
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, 13);
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;
CRocDigLinearDecoder 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;
CRocDigLinearDecoder 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;
}
// =======================================================================
// pulse height scan
// =======================================================================
void test_pulseheight()
{ PROFILING
int col = 10, row = 10;
InitDAC();
tb.roc_SetDAC(Vcal, VCAL_TEST);
tb.roc_SetDAC(CtrlReg,0x0c); // 0x04
if (g_chipdata.pixmap.GetUnmaskedCount(col, row) == 0) col += 2;
if (g_chipdata.pixmap.GetUnmaskedCount(col, row) == 0) col += 2;
if (g_chipdata.pixmap.GetUnmaskedCount(col, row) == 0) col += 2;
Log.section("PHSCAN");
const int vcalmin = 0;
const int vcalmax = 140;
tb.Pg_Stop();
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);
CDtbSource src;
CDataRecordScannerROC raw;
// CRocRawDataPrinter rawPrint("raw.txt");
CRocDigDecoder dec;
// CEventPrinter evPrint("event.txt");
CSink<CEvent*> data;
src >> raw /* >> rawPrint */ >> dec /* >> evPrint */ >> data;
src.OpenRocDig(tb, settings.deser160_tinDelay, false, 100000);
src.Enable();
tb.uDelay(100);
// --- scan vcal
tb.roc_Col_Enable(col, true);
tb.roc_Pix_Trim(col, row, 15);
tb.roc_Pix_Cal (col, row, false);
for (int cal = vcalmin; cal < vcalmax; cal++)
{
tb.roc_SetDAC(Vcal, cal);
tb.uDelay(100);
for (int k=0; k<5; k++)
{
tb.Pg_Single();
tb.uDelay(20);
}
}
tb.roc_Pix_Mask(col, row);
tb.roc_Col_Enable(col, false);
tb.roc_ClrCal();
src.Disable();
// --- plot data
try
{
for (int cal = vcalmin; cal < vcalmax; cal++)
{
int cnt = 0;
double yi = 0.0;
for (int k=0; k<5; k++)
{
CEvent *ev = data.Get();
if (ev->roc[0].pixel.size() > 0) { yi += ev->roc[0].pixel[0].ph; cnt++; }
}
if (cnt > 0)
Log.printf("%3i %5.1f\n", cal, yi/cnt);
else
Log.printf("%3i\n", cal);
}
} catch (DataPipeException e) { printf("\nERROR test_pulseheight: %s\n", e.what()); }
Log.flush();
}
// =======================================================================
// 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;
CRocDigLinearDecoder 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