-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfec_util.c
1159 lines (1071 loc) · 39 KB
/
fec_util.c
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 <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "include/Record_Info.h"
#include "include/xl_regs.h"
#include "penn_daq.h"
#include "fec_util.h"
#include "net_util.h"
//#include "db.h"
//#include "pouch.h"
//#include "json.h"
/*
int get_cmos_total_count(int crate,int slot,uint32_t* total_count)
{
int errorbit;
int errors = 0;
int busstop;
int i;
int howmany;
uint32_t done_mask = 0x0;
uint32_t results[32];
XL3_Packet packet;
MultiFC *commands = (MultiFC *) packet.payload;
do{
howmany = 0;
for (i=0;i<32;i++){
if (((0x1<<i) & done_mask) == 0x0){
commands->cmd[howmany].address = CMOS_INTERN_TOTAL(i) + FEC_SEL*slot + READ_REG;
commands->cmd[howmany].data = 0x0;
SwapLongBlock(&(commands->cmd[howmany].address),1);
SwapLongBlock(&(commands->cmd[howmany].data),1);
howmany++;
}
}
commands->howmany = howmany;
SwapLongBlock(&(commands->howmany),1);
packet.cmdHeader.packet_type = FEC_CMD_ID;
do_xl3_cmd(&packet,crate);
receive_data(howmany,command_number-1,crate,&results);
howmany = 0;
for (i=0;i<32;i++){
if (((0x1<<i) & done_mask) == 0x0){
total_count[i] = results[howmany];
errorbit = (total_count[i] & 0x80000000) ? 1 : 0;
if (errorbit){
errors++;
printsend("there was a cmos total count error\n");
if (errors > 320)
return -1;
}else{
total_count[i] &= 0x7FFFFFFF;
done_mask |= (0x1<<i);
}
howmany++;
}
}
}while(done_mask != 0xFFFFFFFF);
return 0;
}
*/
int get_cmos_total_count(int crate,int slot,int channel,uint32_t* total_count)
{
int errorbit;
int busstop;
int errors = 0;
do{
busstop = xl3_rw(CMOS_INTERN_TOTAL(channel) + FEC_SEL*slot + READ_REG,0x0,total_count,crate);
errorbit = (*total_count & 0x80000000) ? 1 : 0;
if (errorbit | busstop){
errors++;
if (errors > 10)
return -1;
}
*total_count &= 0x7FFFFFFF;
}while(errorbit);
return 0;
}
int get_cmos_total_count2(int crate, int slot, uint32_t* total_count)
{
XL3_Packet packet;
uint32_t *p = (uint32_t *) packet.payload;
*p = slot;
SwapLongBlock(p,1);
packet.cmdHeader.packet_type = CHECK_TOTAL_COUNT_ID;
do_xl3_cmd(&packet,crate);
SwapLongBlock((p+1),32);
int i;
for (i=0;i<32;i++){
total_count[i] = *(p+1+i);
}
return 0;
}
int get_crate_from_jumpers(uint16_t jumpers)
{
int i;
uint16_t jumper_array[] = { 0x1A, 0x19, 0x18, 0x17, 0x16, 0x16, 0x14, 0x13, 0x12, 0x10,
0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08, 0x07, 0xFFFF};
i = 0;
jumpers = jumpers & 0x1F;
while((jumper_array[i] != jumpers) && (jumper_array[i] != 0xFFFF))
i++;
if (jumper_array[i] == jumpers)
return i;
else
return -1;
}
int zdisc(char *buffer)
{
char *words,*words2;
uint32_t crate_num=2, slot_mask=0x2000, offset;
float rate;
int i,j,result;
int update_db = 0;
int final_test = 0;
char ft_ids[16][50];
words = strtok(buffer, " ");
while (words != NULL){
if (words[0] == '-'){
if (words[1] == 'c'){
words2 = strtok(NULL, " ");
crate_num = atoi(words2);
}
if (words[1] == 's'){
words2 = strtok(NULL, " ");
slot_mask = strtoul(words2,(char**)NULL,16);
}
if (words[1] == 'o'){
words2 = strtok(NULL, " ");
offset = atoi(words2);
}
if (words[1] == 'r'){
words2 = strtok(NULL, " ");
rate = (float) strtod(words2,(char**)NULL);
}
if (words[1] == 'd'){
update_db = 1;
}
if (words[1] == '#'){
final_test = 1;
for (i=0;i<16;i++){
if ((0x1<<i) & slot_mask){
words2 = strtok(NULL, " ");
sprintf(ft_ids[i],"%s",words2);
}
}
}
if (words[1] == 'h'){
printsend("Usage: zdisc -c"
" [crate_num] -s [slot mask (hex)] -o [offset] -r [rate] -d (write results to db)\n");
return 0;
}
}
words = strtok(NULL, " ");
}
printsend("-------------------------------------\n\r");
printsend("Discriminator Zero Finder. \n\r");
printsend("Desired rate:\t% 5.1f\n", rate);
printsend("Offset :\t%hu\n", offset);
printsend("-------------------------------------\n\r");
XL3_Packet packet;
hware_vals_t hware_vals_found[16];
float *MaxRate,*UpperRate,*LowerRate;
uint8_t *MaxDacSetting,*ZeroDacSetting,*UpperDacSetting,*LowerDacSetting;
;
for (i=0;i<16;i++){
if ((0x1<<i) & slot_mask){
///////////////////////
// TELL XL3 TO ZDISC //
///////////////////////
uint32_t *p = (uint32_t *) packet.payload;
*p = i;
*(p+1) = offset;
float *q = (float *) (p+2);
*q = rate;
packet.cmdHeader.packet_type = ZERO_DISCRIMINATOR_ID;
SwapLongBlock(p,3);
do_xl3_cmd(&packet,crate_num);
result = *(uint32_t *) packet.payload;
SwapLongBlock((packet.payload+4),96);
MaxRate = (float *) (packet.payload+4);
UpperRate = (float *) (packet.payload+4+32*sizeof(float));
LowerRate = (float *) (packet.payload+4+64*sizeof(float));
MaxDacSetting = (uint8_t *) (packet.payload+4+96*sizeof(float));
ZeroDacSetting = (uint8_t *) (packet.payload+4+96*sizeof(float)+32*sizeof(uint8_t));
UpperDacSetting = (uint8_t *) (packet.payload+4+96*sizeof(float)+64*sizeof(uint8_t));
LowerDacSetting = (uint8_t *) (packet.payload+4+96*sizeof(float)+96*sizeof(uint8_t));
// printout stuff/////////
printsend("channel max rate, lower, upper\n\r");
printsend("------------------------------------------\n\r");
for (j=0;j<32;j++)
{
printsend("ch (%2d) %5.2f(MHz) %6.1f(KHz) %6.1f(KHz)\n\r",j,(float) MaxRate[j]/1E6,(float) LowerRate[j]/1E3,(float) UpperRate[j]/1E3);
}
printsend("Dac Settings\n\r");
printsend("channel Max Lower Upper U+L/2\n\r");
for (j=0;j<32;j++)
{
printsend("ch (%2i) %5hu %5hu %5hu %5hu\n", j, MaxDacSetting[j],LowerDacSetting[j],UpperDacSetting[j],ZeroDacSetting[j]);
if (LowerDacSetting[j] > MaxDacSetting[j])
{
printsend(" <- lower > max! (MaxRate(MHz):%5.2f, lowrate(KHz):%5.2f\n",(float) MaxRate[j]/1E6,(float) LowerRate[j]/1000.0);
}
}
// update the database
if (update_db){
printsend("updating the database\n");
char hextostr[50];
JsonNode *newdoc = json_mkobject();
json_append_member(newdoc,"type",json_mkstring("zdisc"));
JsonNode *maxratenode = json_mkarray();
JsonNode *lowerratenode = json_mkarray();
JsonNode *upperratenode = json_mkarray();
JsonNode *maxdacnode = json_mkarray();
JsonNode *lowerdacnode = json_mkarray();
JsonNode *upperdacnode = json_mkarray();
JsonNode *zerodacnode = json_mkarray();
JsonNode *errorsnode = json_mkarray();
for (j=0;j<32;j++){
json_append_element(maxratenode,json_mknumber(MaxRate[j]));
json_append_element(lowerratenode,json_mknumber(LowerRate[j]));
json_append_element(upperratenode,json_mknumber(UpperRate[j]));
json_append_element(maxdacnode,json_mknumber((double)MaxDacSetting[j]));
json_append_element(lowerdacnode,json_mknumber((double)LowerDacSetting[j]));
json_append_element(upperdacnode,json_mknumber((double)UpperDacSetting[j]));
json_append_element(zerodacnode,json_mknumber((double)ZeroDacSetting[j]));
json_append_element(errorsnode,json_mknumber((double)0));//FIXME
}
json_append_member(newdoc,"Max_rate",maxratenode);
json_append_member(newdoc,"Lower_rate",lowerratenode);
json_append_member(newdoc,"Upper_rate",upperratenode);
json_append_member(newdoc,"Max_Dac_setting",maxdacnode);
json_append_member(newdoc,"Lower_Dac_setting",lowerdacnode);
json_append_member(newdoc,"Upper_Dac_setting",upperdacnode);
json_append_member(newdoc,"Zero_Dac_setting",zerodacnode);
json_append_member(newdoc,"errors",errorsnode);
json_append_member(newdoc,"pass",json_mkstring("yes"));//FIXME
if (final_test)
json_append_member(newdoc,"final_test_id",json_mkstring(ft_ids[i]));
post_debug_doc(crate_num,i,newdoc);
json_delete(newdoc); // Only need to delete the head node);
}
} // end loop over slot mask
} // end loop over slots
printsend("zero discriminator complete.\n");
printsend("*******************************\n");
return 0;
}
int ramp_voltage(char *buffer)
{
char *words,*words2;
uint32_t pattern = 0xf;
int crate = 2;
words = strtok(buffer, " ");
while (words != NULL){
if (words[0] == '-'){
if (words[1] == 'c')
crate = atoi(strtok(NULL, " "));
if (words[1] == 'p'){
words2 = strtok(NULL, " ");
pattern = strtoul(words2,(char**)NULL,16);
}
if (words[1] == 'h'){
printsend("Usage: load_relays -c"
" [crate_num] -s [slot mask (hex)] -d [update debug db]\n");
return 0;
}
}
words = strtok(NULL, " ");
}
int i,j;
uint32_t result;
xl3_rw(XL_HV_SP_R + WRITE_REG, current_hv_level + pattern,&result,crate);
usleep(1000);
usleep(1000);
usleep(1000);
usleep(1000);
usleep(1000);
current_hv_level += pattern;
char hvreadbackcom[100];
sprintf(hvreadbackcom,"hv_readback -c %d -a -b",crate);
hv_readback(hvreadbackcom);
return 0;
}
int load_relays(char *buffer)
{
char *words,*words2;
uint32_t pattern = 0xf;
int crate = 2;
words = strtok(buffer, " ");
while (words != NULL){
if (words[0] == '-'){
if (words[1] == 'c')
crate = atoi(strtok(NULL, " "));
if (words[1] == 'p'){
words2 = strtok(NULL, " ");
pattern = strtoul(words2,(char**)NULL,16);
}
if (words[1] == 'h'){
printsend("Usage: load_relays -c"
" [crate_num] -s [slot mask (hex)] -d [update debug db]\n");
return 0;
}
}
words = strtok(NULL, " ");
}
int i,j;
uint32_t result;
for (i=0;i<16;i++){
for (j=0;j<4;j++){
if ((0x1<<j) & pattern){
xl3_rw(XL_RELAY_R + WRITE_REG, 0x2,&result,crate);
xl3_rw(XL_RELAY_R + WRITE_REG, 0xa,&result,crate);
xl3_rw(XL_RELAY_R + WRITE_REG, 0x2,&result,crate);
}else{
xl3_rw(XL_RELAY_R + WRITE_REG, 0x0,&result,crate);
xl3_rw(XL_RELAY_R + WRITE_REG, 0x8,&result,crate);
xl3_rw(XL_RELAY_R + WRITE_REG, 0x0,&result,crate);
}
}
}
usleep(1000);
xl3_rw(XL_RELAY_R + WRITE_REG, 0x4,&result,crate);
return 0;
}
int fec_test(char *buffer)
{
char *words,*words2;
uint32_t slot_mask = 0x2000;
int crate_num = 2;
int update_db = 0;
int i;
char ft_ids[16][50];
int final_test = 0;
words = strtok(buffer, " ");
while (words != NULL){
if (words[0] == '-'){
if (words[1] == 'c')
crate_num = atoi(strtok(NULL, " "));
if (words[1] == 's'){
words2 = strtok(NULL, " ");
slot_mask = strtoul(words2,(char**)NULL,16);
}
if (words[1] == 'd'){
update_db = 1;
}
if (words[1] == '#'){
final_test = 1;
for (i=0;i<16;i++){
if ((0x1<<i) & slot_mask){
words2 = strtok(NULL, " ");
sprintf(ft_ids[i],"%s",words2);
}
}
}
if (words[1] == 'h'){
printsend("Usage: fec_test -c"
" [crate_num] -s [slot mask (hex)] -d [update debug db]\n");
return 0;
}
}
words = strtok(NULL, " ");
}
XL3_Packet packet;
uint32_t *p = (uint32_t *) packet.payload;
*p = slot_mask;
packet.cmdHeader.packet_type = FEC_TEST_ID;
SwapLongBlock(p,1);
do_xl3_cmd(&packet,crate_num);
if (update_db){
printsend("updating the database\n");
uint32_t* results = (uint32_t*) packet.payload;
// results layout is : [error flags] [slot 0 discrete] [slot 1 discrete] ... [slot 0 cmos] [slot 1 cmos] ...
SwapLongBlock(results,65);
int slot;
;
for (slot=0;slot<16;slot++){
if ((0x1<<slot) & slot_mask){
printsend("updating slot %d\n",slot);
JsonNode *newdoc = json_mkobject();
json_append_member(newdoc,"type",json_mkstring("fec_test"));
if (results[1+slot] & 0x1)
json_append_member(newdoc,"pedestal",json_mknumber((double)1));
else
json_append_member(newdoc,"pedestal",json_mknumber((double)0));
if (results[1+slot] & 0x2)
json_append_member(newdoc,"chip_disable",json_mknumber((double)1));
else
json_append_member(newdoc,"chip_disable",json_mknumber((double)0));
if (results[1+slot] & 0x4)
json_append_member(newdoc,"lgi_select",json_mknumber((double)1));
else
json_append_member(newdoc,"lgi_select",json_mknumber((double)0));
if (results[1+slot] & 0x8)
json_append_member(newdoc,"cmos_prog_low",json_mknumber((double)1));
else
json_append_member(newdoc,"cmos_prog_low",json_mknumber((double)0));
if (results[1+slot] & 0x10)
json_append_member(newdoc,"cmos_prog_high",json_mknumber((double)1));
else
json_append_member(newdoc,"cmos_prog_high",json_mknumber((double)0));
JsonNode *cmos_test_array = json_mkarray();
for (i=0;i<32;i++){
if (results[17+slot] & (0x1<<i))
json_append_element(cmos_test_array,json_mknumber((double)1));
else
json_append_element(cmos_test_array,json_mknumber((double)0));
}
if (results[17+slot] == 0x0)
json_append_element(cmos_test_array,json_mknumber((double)0));
else
json_append_element(cmos_test_array,json_mknumber((double)1));
json_append_member(newdoc,"cmos_test_reg",cmos_test_array);
if ((results[1+slot] == 0x0) && (results[17+slot] == 0x0)){
json_append_member(newdoc,"pass",json_mkstring("yes"));
}else{
json_append_member(newdoc,"pass",json_mkstring("no"));
}
if (final_test)
json_append_member(newdoc,"final_test_id",json_mkstring(ft_ids[slot]));
post_debug_doc(crate_num,slot,newdoc);
json_delete(newdoc); // Only have to delete the head node
}
}
}
return 0;
}
int board_id(char *buffer)
{
char *words,*words2;
uint32_t slot_mask = 0x2000;
int crate_num = 2;
words = strtok(buffer, " ");
while (words != NULL){
if (words[0] == '-'){
if (words[1] == 'c')
crate_num = atoi(strtok(NULL, " "));
if (words[1] == 's'){
words2 = strtok(NULL, " ");
slot_mask = strtoul(words2,(char**)NULL,16);
}
if (words[1] == 'h'){
printsend("Usage: board_id -c"
" [crate_num] -s [slot mask (hex)]\n");
return 0;
}
}
words = strtok(NULL, " ");
}
uint32_t mb_id, dc_id[4],hv_id;
XL3_Packet packet;
packet.cmdHeader.packet_type = BOARD_ID_READ_ID;
uint32_t *result = (uint32_t *) packet.payload;
uint32_t *slot = (uint32_t *) packet.payload;
uint32_t *chip = (uint32_t *) (packet.payload+4);
uint32_t *reg = (uint32_t *) (packet.payload+8);
int i,j,k;
printsend( "SLOT ID: MB DB1 DB2 DB3 DB4 HVC\n");
for (i=0;i<16;i++){
if (slot_mask & (0x01<<i)){
*slot = i;
*chip = 1;
*reg = 15;
SwapLongBlock(slot,3);
do_xl3_cmd(&packet,crate_num);
SwapLongBlock(slot,1);
mb_id = *result;
k=0;
for (j=2;j<6;j++){
*slot = i;
*chip = j;
*reg = 15;
SwapLongBlock(slot,3);
do_xl3_cmd(&packet,crate_num);
SwapLongBlock(slot,1);
dc_id[k] = *result;
k++;
}
*slot = i;
*chip = 6;
*reg = 15;
SwapLongBlock(slot,3);
do_xl3_cmd(&packet,crate_num);
SwapLongBlock(slot,1);
hv_id = *result;
printsend( "%d 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x\n",
i,mb_id,dc_id[0],dc_id[1],dc_id[2],dc_id[3],hv_id);
}
}
printsend("*******************************\n");
deselect_fecs(crate_num);
return 0;
}
int mem_test(char *buffer)
{
char *words,*words2;
uint32_t slot_num = 14;
int crate_num = 2;
int update_db = 0;
words = strtok(buffer, " ");
while (words != NULL){
if (words[0] == '-'){
if (words[1] == 'c')
crate_num = atoi(strtok(NULL, " "));
if (words[1] == 's'){
words2 = strtok(NULL, " ");
slot_num = atoi(words2);
}
if (words[1] == 'd'){
update_db = 1;
}
if (words[1] == 'h'){
printsend("Usage: mem_test -c"
" [crate_num] -s [slot number (int)] -d (update debug database)\n");
return 0;
}
}
words = strtok(NULL, " ");
}
if (slot_num > 15 || slot_num < 0){
printsend("slot number not valid (its an integer!)\n");
return -1;
}
XL3_Packet packet;
uint32_t *p = (uint32_t *) packet.payload;
*p = slot_num;
packet.cmdHeader.packet_type = MEM_TEST_ID;
SwapLongBlock(p,1);
do_xl3_cmd(&packet,crate_num);
if (update_db){
printsend("updating the database\n");
uint32_t* results = (uint32_t*) packet.payload;
// results layout is : [error flags] [address test bit failures] [pattern test first error location] [expected] [read]
SwapLongBlock(results,65);
char hextostr[50];
;
JsonNode *newdoc = json_mkobject();
json_append_member(newdoc,"type",json_mkstring("mem_test"));
sprintf(hextostr,"%05x",results[1]);
json_append_member(newdoc,"address_test",json_mkstring(hextostr));
JsonNode* patterntest = json_mkarray();
sprintf(hextostr,"%08x",results[2]);
json_append_element(patterntest,json_mkstring(hextostr));
sprintf(hextostr,"%08x",results[3]);
json_append_element(patterntest,json_mkstring(hextostr));
sprintf(hextostr,"%08x",results[4]);
json_append_element(patterntest,json_mkstring(hextostr));
json_append_member(newdoc,"pattern_test",patterntest);
if ((results[1] == 0x0) && (results[2] == 0xFFFFFFFF)){
json_append_member(newdoc,"pass",json_mkstring("yes"));
}else{
json_append_member(newdoc,"pass",json_mkstring("no"));
}
post_debug_doc(crate_num,slot_num,newdoc);
json_delete(newdoc); // only delete the head node
}
return 0;
}
int vmon(char *buffer)
{
char *words,*words2;
char v_name[21][20] = {"neg_24","neg_15","Vee","neg_3_3","neg_2","pos_3_3","pos_4","Vcc","pos_6_5","pos_8","pos_15","pos_24","neg_2_ref","neg_1_ref","pos_0_8_ref","pos_1_ref","pos_4_ref","pos_5_ref","Temp","CalD","hvt"};
uint32_t slot_mask = 0x2000;
float voltages[16][21];
float voltages_min[21] = {-99.,-99.,-99.,-99.,-99.,-99.,-99.,-99.,-99.,-99.,-99.,-99.,-99.,-99.,-99.,-99.,-99.,-99.,-99.,-99.,-99};
float voltages_max[21] = {99.,99.,99.,99.,99.,99.,99.,99.,99.,99.,99.,99.,99.,99.,99.,99.,99.,99.,99.,99.,99};
int update_db = 0;
char ft_ids[16][50];
int final_test = 0;
int i,j;
int crate_num = 2;
words = strtok(buffer, " ");
while (words != NULL){
if (words[0] == '-'){
if (words[1] == 'c')
crate_num = atoi(strtok(NULL, " "));
if (words[1] == 's'){
words2 = strtok(NULL, " ");
slot_mask = strtoul(words2,(char**)NULL,16);
}
if (words[1] == 'd'){
update_db = 1;
}
if (words[1] == '#'){
final_test = 1;
for (i=0;i<16;i++){
if ((0x1<<i) & slot_mask){
words2 = strtok(NULL, " ");
sprintf(ft_ids[i],"%s",words2);
}
}
}
if (words[1] == 'h'){
printsend("Usage: vmon -c"
" [crate_num] -s [slot mask (hex)] -d (udpate debug db)\n");
return 0;
}
}
words = strtok(NULL, " ");
}
// initialize the variables
for (i=0;i<16;i++)
for(j=0;j<21;j++)
voltages[i][j] = 0;
// send packets to xl3s to do the reads
int slot_num;
for (slot_num = 0;slot_num<16;slot_num++){
if ((0x1<<slot_num) & slot_mask){
XL3_Packet packet;
uint32_t *p = (uint32_t *) packet.payload;
*p = slot_num;
packet.cmdHeader.packet_type = VMON_START_ID;
SwapLongBlock(p,1);
do_xl3_cmd(&packet,crate_num);
float *v = (float *) packet.payload;
SwapLongBlock(v,21);
for (i=0;i<21;i++){
voltages[slot_num][i] = *(v+i);
}
}
}
// now lets print out the results
int k;
for (k=0;k<2;k++){
printsend("slot %2d %2d %2d %2d %2d %2d %2d %2d\n",k*8,k*8+1,k*8+2,k*8+3,k*8+4,k*8+5,k*8+6,k*8+7);
for (i=0;i<21;i++){
printsend("%10s ",v_name[i]);
for (j=0;j<8;j++){
printsend("%6.2f ",voltages[j+k*8][i]);
}
printsend("\n");
}
printsend("\n");
}
// update the database
if (update_db){
printsend("updating the database\n");
char hextostr[50];
;
for (slot_num=0;slot_num<16;slot_num++){
if ((0x1<<slot_num) & slot_mask){
int fail_flag = 0;
JsonNode *newdoc = json_mkobject();
json_append_member(newdoc,"type",json_mkstring("vmon"));
for (j=0;j<21;j++){
json_append_member(newdoc,v_name[j],json_mknumber((double)voltages[slot_num][j]));
if ((voltages[slot_num][j] < voltages_min[j]) || (voltages[slot_num][j] > voltages_max[j]))
fail_flag = 1;
}
if (fail_flag == 0){
json_append_member(newdoc,"pass",json_mkstring("yes"));
}else{
json_append_member(newdoc,"pass",json_mkstring("no"));
}
if (final_test)
json_append_member(newdoc,"final_test_id",json_mkstring(ft_ids[slot_num]));
post_debug_doc(crate_num,slot_num,newdoc);
json_delete(newdoc);
}
}
}
return 0;
}
int fec_load_crateadd(int crate, uint32_t slot_mask)
{
int i;
int errors;
uint32_t csr_orig;
uint32_t select_reg;
uint32_t result;
errors = 0;
for (i=0;i<16;i++){
if ((0x1<<i) & slot_mask){
select_reg =FEC_SEL*i;
xl3_rw(GENERAL_CSR_R + select_reg + WRITE_REG, 0x0 | (crate << FEC_CSR_CRATE_OFFSET),&result,crate);
}
}
deselect_fecs(crate);
return errors;
/* HERE BE THE SIMPLER WAY */
/*
XL3_Packet packet;
packet.cmdHeader.cmdID = 0xD;
uint32_t *p;
p = (uint32_t *) packet.payload;
*p = slot_mask;
p++;
*p = crate;
do_xl3_cmd(&packet,crate_num);
return 0;
*/
}
int set_crate_pedestals(int crate, uint32_t slot_mask, uint32_t pattern)
{
/* int i;
uint32_t select_reg, result;
for (i=0;i<16;i++){
select_reg = FEC_SEL*i;
if ((0x1<<i) & slot_mask){
xl3_rw(PED_ENABLE_R + select_reg + WRITE_REG,pattern,&result,crate);
}else{
xl3_rw(PED_ENABLE_R + select_reg + WRITE_REG,0x0,&result,crate);
}
}
deselect_fecs(crate);
return 0;
*/
/* HERE BE THE SIMPLER WAY */
XL3_Packet packet;
packet.cmdHeader.packet_type = 0xE;
uint32_t *p;
p = (uint32_t *) packet.payload;
*p = slot_mask;
p++;
*p = pattern;
SwapLongBlock(packet.payload,2);
do_xl3_cmd(&packet,crate);
return 0;
}
int32_t read_pmt(int crate_number, int32_t slot, int32_t limit, uint32_t *pmt_buf)
{
XL3_Packet packet;
MultiFC *commands = (MultiFC *) packet.payload;
#ifdef FIRST_WORD_BUG
// this is a hack until PW fixes the sequencer
static first[16]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
#endif //FIRST_WORD_BUG
int count;
uint32_t *memory;
uint32_t diff;
uint32_t select_reg, result;
int i,j;
int error;
#ifdef BIG_MOMMA
#define BIGDIFF 200 //max diff btw GT's for big momma
#define CNTXT 2
int lastGT = 0;
int theGT;
#endif //BIG_MOMMA
error = 0;
select_reg = FEC_SEL*slot;
xl3_rw(FIFO_DIFF_PTR_R + select_reg + READ_REG,0x0,&diff,crate_number);
diff &= 0xFFFFF;
diff = diff - (diff%3);
if ((3 * limit) < diff){
if ((3*limit)*1.5 < diff){
printsend("Memory level much higher than expected (%d > %d), possible fifo overflow\n",diff,3*limit);
}else{
printsend("Memory level over expected (%d > %d)\n",diff,3*limit);
}
diff = 3*limit; // make sure we do not read out more than limit pmt bundles
}
#ifdef FIRST_WORD_BUG
if ((diff > 3) && first[slot])
{
printsend("This is a hack until the sequencer is fixed\n");
xl3_rw(select_reg + READ_MEM,0x0,pmt_buf,crate_number);
xl3_rw(select_reg + READ_MEM,0x0,pmt_buf,crate_number);
xl3_rw(select_reg + READ_MEM,0x0,pmt_buf,crate_number);
first[slot] = 0;
} // throw out the first word of data because it is currently garbage
#endif //FIRST_WORD_BUG
// lets use the new function!
// first we attempt to load up the xl3 with 'diff' # of reads to memory
printsend("attempting to read %d bundles\n",diff/3);
packet.cmdHeader.packet_type = READ_PEDESTALS_ID;
*(uint32_t *) packet.payload = slot;
int reads_left = diff;
int this_read;
while(reads_left != 0){
if (reads_left > MAX_FEC_COMMANDS-1000)
this_read = MAX_FEC_COMMANDS-1000;
else
this_read = reads_left;
packet.cmdHeader.packet_type = READ_PEDESTALS_ID;
*(uint32_t *) packet.payload = slot;
*(uint32_t *) (packet.payload+4) = this_read;;
SwapLongBlock(packet.payload,2);
do_xl3_cmd(&packet,crate_number);
receive_data(this_read,command_number-1,crate_number,pmt_buf+(diff-reads_left));
reads_left -= this_read;
}
#ifdef BIG_MOMMA
//make sure all other output is done
fflush(stdout);
//loop over data again
if (!error){
for (i=0;i<diff;i+=3){
theGT = (int) UNPK_FEC_GT_ID(pmt_buf+i);
if ((theGT > (lastGT + BIGDIFF)) ||
(theGT < (lastGT - BIGDIFF)) ){
if (i == 0){
lastGT = theGT;
continue;
}
printsend( "Big Momma GT ID! iterator= %i\nGTID = %d (0x%06lx), lastGT = %d(0x%06lx)\n",
i/3,theGT,theGT,lastGT,lastGT);
//dump it and two previous, and one following
printsend( "Dumping GT and context (%d previous, 2 following)\n",CNTXT);
//dump_pmt_verbose(CNTXT + 3,(pmt_buf+i) - CNTXT*3); //RJB not in yet
}
lastGT = theGT;
}
}
#endif //BIG_MOMMA
if (error){
printsend("Bus error reading memory\n");
error = 0;
}
count = diff / 3;
deselect_fecs(crate_number);
return count;
}
#define NUM_SLOTS 16
int update_crate_config(int crate, uint16_t slot_mask)
{
XL3_Packet packet;
packet.cmdHeader.packet_type = BUILD_CRATE_CONFIG_ID;
uint32_t *p = (uint32_t *) packet.payload;
*p = slot_mask;
SwapLongBlock(p,1);
do_xl3_cmd(&packet,crate);
int errors;
errors = *(uint32_t *) packet.payload;
int j;
for (j=0;j<16;j++){
if ((0x1<<j) & slot_mask){
(crate_config[crate][j]) = *(hware_vals_t *) (packet.payload+4+j*sizeof(hware_vals_t));
SwapShortBlock(&(crate_config[crate][j].mb_id),1);
SwapShortBlock(crate_config[crate][j].dc_id,4);
}
}
deselect_fecs(crate);
return 0;
}
int multiloadsDac(int num_dacs, uint32_t *theDacs, uint32_t *theDAC_Values, int crate_number, uint32_t select_reg)
{
XL3_Packet packet;
packet.cmdHeader.packet_type = MULTI_LOADSDAC_ID;
uint32_t *p;
p = (uint32_t *) packet.payload;
*p = num_dacs;
p++;
int i,j, errors = 0;
for (i=0;i<16;i++){
if ((i*FEC_SEL) == select_reg){
for (j=0;j<num_dacs;j++){
*p = i;
*(p+1) = *theDacs;
*(p+2) = *theDAC_Values;
p+=3;
theDacs++;
theDAC_Values++;
}
SwapLongBlock(packet.payload,num_dacs*3+1);
do_xl3_cmd(&packet,crate_number);
SwapLongBlock(packet.payload,1);
errors += *(uint32_t *) packet.payload;
}
}
return errors;
}
int loadsDac(unsigned long theDAC, unsigned long theDAC_Value, int crate_number, uint32_t select_reg)
{
XL3_Packet packet;
packet.cmdHeader.packet_type = LOADSDAC_ID;
uint32_t *p;
p = (uint32_t *) packet.payload;
int i, errors = 0;
for (i=0;i<16;i++){
if((i*FEC_SEL) == select_reg){
*p = i;
*(p+1) = theDAC;
*(p+2) = theDAC_Value;
SwapLongBlock(p,3);
do_xl3_cmd(&packet,crate_number);
SwapLongBlock(p,1);
errors += *p;
}
}
return errors;
}
int read_bundle(char *buffer)
{
uint32_t pmtword[3];
char *words,*words2;
int slot_num = 7;
int crate_num = 2;
int quiet = 0;
words = strtok(buffer, " ");
while (words != NULL){
if (words[0] == '-'){
if (words[1] == 'c')
crate_num = atoi(strtok(NULL, " "));
if (words[1] == 's')
slot_num = atoi(strtok(NULL, " "));
if (words[1] == 'q')
quiet = 1;
if (words[1] == 'h'){
printsend("Usage: read_bundle -c"
" [crate_num] -s [slot_num (int)] -q (enable quiet mode)\n");
return 0;
}
}
words = strtok(NULL, " ");
}
uint32_t crate,slot,chan,gt8,gt16,cmos_es16,cgt_es16,cgt_es8,nc_cc;
int cell;
double qlx,qhs,qhl,tac;
xl3_rw(READ_MEM+slot_num*FEC_SEL,0x0,pmtword,crate_num);
xl3_rw(READ_MEM+slot_num*FEC_SEL,0x0,pmtword+1,crate_num);
xl3_rw(READ_MEM+slot_num*FEC_SEL,0x0,pmtword+2,crate_num);
printsend("%08x %08x %08x\n",pmtword[0],pmtword[1],pmtword[2]);
if (quiet == 0){
crate = (uint32_t) UNPK_CRATE_ID(pmtword);
slot = (uint32_t) UNPK_BOARD_ID(pmtword);
chan = (uint32_t) UNPK_CHANNEL_ID(pmtword);
cell = (int) UNPK_CELL_ID(pmtword);
gt8 = (uint32_t) UNPK_FEC_GT8_ID(pmtword);
gt16 = (uint32_t) UNPK_FEC_GT16_ID(pmtword);
cmos_es16 = (uint32_t) UNPK_CMOS_ES_16(pmtword);
cgt_es16 = (uint32_t) UNPK_CGT_ES_16(pmtword);
cgt_es8 = (uint32_t) UNPK_CGT_ES_24(pmtword);
nc_cc = (uint32_t) UNPK_NC_CC(pmtword);