-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfcs_client.c
1429 lines (1271 loc) · 53.4 KB
/
fcs_client.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
// Simple FCS Client interfacing with the BSMP library
// for controlling the BPM FPGA
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <getopt.h>
#include <signal.h>
#include <time.h>
#include "fcs_client.h"
#include "transport/transport.h"
#include "transport/ethernet.h"
#include "transport/serial_rs232.h"
#include "revision.h"
#include "debug.h"
#define C "CLIENT: "
#define PORT "8080" // the FPGA port client will be connecting to
#define FE_PORT "6791" // the RFFE port client will be connecting to
enum dev_type_e {
ETHERNET_DEV = 0,
SERIAL_RS232_DEV
};
// Our FPGA transport
static struct transport_s transport_fpga;
// Our FE transport
static struct transport_s transport_fe;
#define PACKET_SIZE BSMP_MAX_MESSAGE
#define PACKET_HEADER BSMP_HEADER_SIZE
#define TRY(name, func)\
do {\
enum bsmp_err err = func;\
if(err) {\
fprintf(stderr, C "%s: %s\n", name, bsmp_error_str(err));\
exit(-1);\
}\
}while(0)
#define PRINTV(verbose, fmt, ...)\
do {\
if (verbose) {\
printf (fmt, ## __VA_ARGS__);\
}\
}while(0)
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#define MONIT_POLL_RATE 200000 //usec
#define TIMESTAMP_BUF_LEN 80
static char buffer[TIMESTAMP_BUF_LEN];
static char * timestamp_str()
{
struct timespec tsp;
int ret;
int len = TIMESTAMP_BUF_LEN;
clock_gettime(CLOCK_REALTIME, &tsp); //Call clock_gettime to fill tsp
ret = strftime (buffer, 80, "%Y-%m-%dT%H:%M:%S", localtime(&tsp.tv_sec));
len -= ret-1;
snprintf(&buffer[strlen(buffer)], len, ".%09ldZ", tsp.tv_nsec);
return buffer;
}
const char* program_name;
char *hostname = NULL;
int need_hostname = 0;
char *fe_hostname = NULL;
int need_fe_hostname = 0;
int monit_timestamp = 0;
sig_atomic_t _interrupted = 0;
// C^c signal handler
static void sigint_handler (int sig, siginfo_t *siginfo, void *context)
{
(void) sig;
(void) siginfo;
(void) context;
_interrupted = 1;
}
#define PLOT_BUFFER_LEN 1024 // in 32-bit words
#define NUM_CHANNELS 4
typedef struct _plot_values_monit_double_t {
double ch0[PLOT_BUFFER_LEN];
double ch1[PLOT_BUFFER_LEN];
double ch2[PLOT_BUFFER_LEN];
double ch3[PLOT_BUFFER_LEN];
} plot_values_monit_double_t;
typedef struct _plot_values_monit_uint32_t {
uint32_t ch0;
uint32_t ch1;
uint32_t ch2;
uint32_t ch3;
} plot_values_monit_uint32_t;
/* 4096 data of A, B, C or D */
plot_values_monit_uint32_t pval_monit_uint32[PLOT_BUFFER_LEN];
plot_values_monit_double_t pval_monit_double;
/* Our send/receive packet for the FPGA */
recv_pkt_t recv_pkt;
send_pkt_t send_pkt;
/* Our send/receive packet for the FE */
recv_pkt_t fe_recv_pkt;
send_pkt_t fe_send_pkt;
/***************************************************************/
/********************** Functions *******************/
/***************************************************************/
void print_packet (char* pre, uint8_t *data, uint32_t size)
{
#ifdef DEBUG
printf("%s: [", pre);
if(size < 32)
{
unsigned int i;
for(i = 0; i < size; ++i)
printf("%02X ", data[i]);
printf("]\n");
}
else
printf("%d bytes ]\n", size);
#else
(void) pre;
(void) data;
(void) size;
#endif
}
int __bpm_send(int (*send_f)(int, uint8_t *, uint32_t *), int fd, uint8_t *data, uint32_t *count)
{
uint8_t packet[BSMP_MAX_MESSAGE];
uint32_t packet_size = *count;
uint32_t len = *count;
memcpy (packet, data, *count);
print_packet("SEND()", packet, packet_size);
if (!send_f) {
fprintf(stderr, "recv function not implemented!\n");
return -1;
}
int ret = send_f(fd, packet, &len);
DEBUGP ("bpm_send(%d): %d bytes sent!\n", fd, len);
if(len != packet_size) {
if(ret < 0)
perror("send");
return -1;
}
return 0;
}
int __bpm_recv(int (*recv_f)(int, uint8_t *, uint32_t *), int fd, uint8_t *data, uint32_t *count)
{
uint8_t packet[PACKET_SIZE] = {0};
uint32_t packet_size;
uint32_t len = PACKET_HEADER;
if (!recv_f) {
fprintf(stderr, "recv function not implemented!\n");
return -1;
}
int ret = recv_f(fd, packet, &len);
if(len != PACKET_HEADER) {
if(ret < 0)
perror("recv");
return -1;
}
DEBUGP ("bpm_recv(%d): received %d bytes (header)!\n", fd, PACKET_HEADER);
//uint32_t remaining = (packet[2] << 8) + packet[3];
uint32_t remaining = (packet[1] << 8) + packet[2];
len = remaining;
DEBUGP ("bpm_recv(%d): %d bytes to recv!\n", fd, remaining);
ret = recv_f(fd, packet + PACKET_HEADER, &len);
if(len != remaining) {
if(ret < 0)
perror("recv");
return -1;
}
DEBUGP("bpm_recv(%d) received payload!\n", fd);
packet_size = PACKET_HEADER + remaining;
print_packet("RECV", packet, packet_size);
*count = packet_size;
memcpy(data, packet, *count);
return 0;
}
/***************************************************************/
/********************** Wrappers *******************/
/***************************************************************/
int bpm_init (enum dev_type_e dev_type, struct transport_s *transport)
{
//dev_type is:
//0 -> Ethernet
//1 -> Serial RS-232
switch (dev_type) {
case ETHERNET_DEV:
transport->ops = ðernet_ops;
break;
case SERIAL_RS232_DEV:
transport->ops = &serial_rs232_ops;
break;
// Ethernet is default
default:
transport->ops = ðernet_ops;
}
return 0;
}
int bpm_fpga_send(uint8_t *data, uint32_t *count)
{
return __bpm_send(transport_fpga.ops->bpm_send, transport_fpga.fd, data, count);
//return transport_fpga.ops->bpm_send(transport_fpga.fd, data, count); // fd is the FPGA socket
}
int bpm_fpga_recv(uint8_t *data, uint32_t *count)
{
return __bpm_recv(transport_fpga.ops->bpm_recv, transport_fpga.fd, data, count);
//return transport_fpga.ops->bpm_recv(transport_fpga.fd, data, count); // fd is the FPGA socket
}
int bpm_fe_send(uint8_t *data, uint32_t *count)
{
return __bpm_send(transport_fe.ops->bpm_send, transport_fe.fd, data, count);
//return transport_fe.ops->bpm_send(transport_fe.fd, data, count); // fd is the FE socket
}
int bpm_fe_recv(uint8_t *data, uint32_t *count)
{
return __bpm_recv(transport_fe.ops->bpm_recv, transport_fe.fd, data, count);
//return transport_fe.ops->bpm_recv(transport_fe.fd, data, count); // fd is the FE socket
}
// Command-line handling
void print_usage (FILE* stream, int exit_code)
{
fprintf (stream, "FCS Client program\n");
fprintf (stream, "Git commit ID: %s.\n", build_revision);
fprintf (stream, "Build date: %s.\n\n", build_date);
fprintf (stream, "Usage: %s options \n", program_name);
fprintf (stream,
" -h --help Display this usage information.\n"
" -v --verbose Print verbose messages.\n"
" -b --blink Blink board leds\n"
" -r --reset Reconfigure all options to its defaults\n"
" -o --setfpgahostname <host> Sets FPGA hostname to <host>\n"
" -w --setrffehostname <host> Sets RFFE hostname to <host>\n"
" -x --setkx <value>[nm] Sets parameter Kx to <value>\n"
" [in UFIX25_0 format]\n"
" -y --setky <value>[nm] Sets parameter Ky to <value>\n"
" [in UFIX25_0 format]\n"
" -s --setksum <value> Sets parameter Ksum to <value>\n"
" [in FIX25_24 format]\n"
" -j --setswon Sets FPGA deswitching on\n"
" -k --setswoff Sets FPGA deswitching off\n"
" -1 --setswclkenon Sets FPGA switching clock enable on\n"
" -2 --setswclkenoff Sets FPGA switching clock enable off\n"
" -g --setfeswon Sets RFFE switching on\n"
" -m --setfeswoff Sets RFFE switching off\n"
" -d --setdivclk <value> Sets FPGA switching divider clock to <value>\n"
" [in number of ADC clock cycles]\n"
" -p --setphaseclk <value> Sets FPGA switching phase clock to <value>\n"
" [in number of ADC clock cycles]\n"
" -u --setwdwon Sets FPGA windowing on\n"
" -e --setwdwoff Sets FPGA windowing off\n"
" -n --setwdwdly <value> Sets FPGA windowing delay\n"
" [<value> must be between 0 and 500 [ADC clk cycles]]\n"
" -q --setadcclk <value> Sets FPGA reference ADC clock to <value> [in Hertz]\n"
" -i --setddsfreq <value> Sets FPGA DDS Frequency to <value> [in Hertz]\n"
" -l --setsamples <number of samples>\n"
" Sets FPGA Acquisition parameters\n"
" [<number of samples> must be between 4 and\n"
" ??? (TBD) \n"
" -c --setchan <channel> \n"
" Sets FPGA Acquisition parameters\n"
" [<channel> must be one of the following:\n"
" 0 -> ADC; 1-> TBT Amp; 2 -> TBT Pos\n"
" 3 -> FOFB Amp; 4-> FOFB Pos]\n"
" -t --startacq Starts FPGA acquistion with the previous parameters\n"
" -a --setfeatt1 <value> Sets the RFFE Attenuator 1 to <value>\n"
" [<value> must be between 0 and 31.5 [dB]\n"
" with 0.5 step. Invalid attenuation values\n"
" will be rounded down to the nearest valid\n"
" value]\n"
" -z --setfeatt2 <value> Sets the RFFE Attenuator 2 to <value>\n"
" [<value> must be between 0 and 31.5 [dB]\n"
" with 0.5 step. Invalid attenuation values\n"
" will be rounded down to the nearest valid\n"
" value]\n"
" -R --getfmctemp1 Gets FPGA FMC temparature 1 (near ?)\n"
" [in degrees celsius (*C)]\n"
" -T --getfmctemp2 Gets FPGA FMC temperature 2 (near ?)\n"
" [in degrees celsius (*C)]\n"
" -X --getkx Gets parameter Kx [nm] in UFIX25_0 format\n"
" -Y --getky Gets parameter Ky [nm] in UFIX25_0 format\n"
" -S --getksum Gets parameter Ksum in FIX25_24 format\n"
" -J --getsw Gets FPGA deswitching state \n"
" [0x1 is no switching and 0x3 is switching]\n"
" -G --getfesw Gets RFFE switching state \n"
" [0x1 is no switching and 0x3 is switching]\n"
" -3 --getswclken Gets FPGA switching clock enable state \n"
" [1 is enabled and 0 is disabled]\n"
" -A --getfeatt1 Gets RFFE Atenuattor 1 value\n"
" [<value> is between 0 and 31.5 [dB]\n"
" -Z --getfeatt2 Gets RFFE Attenuator 2 value\n"
" [<value> is between 0 and 31.5 [dB]\n"
" -M --getfetemp1 Gets RFFE Temparature 1 (near AC channels)\n"
" [in degrees celsius (*C)]\n"
" -K --getfetemp2 Gets RFFE temperature 2 (near BD channels)\n"
" [in degrees celsius (*C)]\n"
" -D --getdivclk Gets FPGA switching divider clock value\n"
" [in number of ADC clock cycles]\n"
" -P --getphaseclk Gets FPGA switching phase clock\n"
" [in number of ADC clock cycles]\n"
" -E --getwdw Gets FPGA windowing state \n"
" -N --getwdwdly Gets FPGA windowing delay\n"
" [<value> will be between 0 and 500 [ADC clk cycles]]\n"
" -Q --getadcclk Gets FPGA reference ADC clock [in Hertz]\n"
" -I --getddsfreq Gets FPGA DDS Frequency [in Hertz]\n"
" -L --getsamples Gets FPGA number of samples of the next acquisition\n"
" -C --getchan Gets FPGA data channel of the next acquisition\n"
" -B --getcurve <channel> Gets FPGA curve data of channel <channel_number>\n"
" [<channel> must be one of the following:\n"
" 0 -> ADC; 1-> TBT Amp; 2 -> TBT Pos\n"
" 3 -> FOFB Amp; 4-> FOFB Pos]\n"
" -E --getmonitamp Gets FPGA Monitoring Ampltitude Sample\n"
" [This consists of the following:\n"
" Monit. Amp 0, Amp 1, Amp 2, Amp 3]\n"
" -F --getmonitpos Gets FPGA Monitoring Position Sample\n"
" [This consists of the following:\n"
" Monit. X, Y, Q, Sum]\n"
" -O --monittimestamp Outputs timestamp to be alongside\n"
" the actual Monitoring data (Amp. or Pos.)\n"
);
exit (exit_code);
}
static struct option long_options[] =
{
{"help", no_argument, NULL, 'h'},
{"verbose", no_argument, NULL, 'v'},
{"blink", no_argument, NULL, 'b'},
{"reset", no_argument, NULL, 'r'},
{"setfpgahostname", required_argument, NULL, 'o'},
{"setrffehostname", required_argument, NULL, 'w'},
{"setkx", required_argument, NULL, 'x'},
{"setky", required_argument, NULL, 'y'},
{"setksum", required_argument, NULL, 's'},
{"setswon", no_argument, NULL, 'j'},
{"setswoff", no_argument, NULL, 'k'},
{"setswclkenon", no_argument, NULL, '1'},
{"setswclkenoff", no_argument, NULL, '2'},
{"setfeswon", no_argument, NULL, 'g'},
{"setfeswoff", no_argument, NULL, 'm'},
{"setdivclk", required_argument, NULL, 'd'},
{"setphaseclk", required_argument, NULL, 'p'},
{"setwdwon", no_argument, NULL, 'u'},
{"setwdwoff", no_argument, NULL, 'e'},
{"setwdwdly", required_argument, NULL, 'n'},
{"setadcclk", required_argument, NULL, 'q'},
{"setddsfreq", required_argument, NULL, 'i'},
{"setsamples", required_argument, NULL, 'l'},
{"setchan", required_argument, NULL, 'c'},
{"startacq", no_argument, NULL, 't'},
{"setfeatt1", required_argument, NULL, 'a'},
{"setfeatt2", required_argument, NULL, 'z'},
{"getfmctemp1", no_argument, NULL, 'R'},
{"getfmctemp2", no_argument, NULL, 'T'},
{"getkx", no_argument, NULL, 'X'},
{"getky", no_argument, NULL, 'Y'},
{"getksum", no_argument, NULL, 'S'},
{"getsw", no_argument, NULL, 'J'},
{"getswclken", no_argument, NULL, '3'},
{"getfesw", no_argument, NULL, 'G'},
{"getfeatt1", no_argument, NULL, 'A'},
{"getfeatt2", no_argument, NULL, 'Z'},
{"getfetemp1", no_argument, NULL, 'M'},
{"getfetemp2", no_argument, NULL, 'K'},
{"getdivclk", no_argument, NULL, 'D'},
{"getphaseclk", no_argument, NULL, 'P'},
{"getwdw", no_argument, NULL, 'U'},
{"getwdwdly", no_argument, NULL, 'N'},
{"getadcclk", no_argument, NULL, 'Q'},
{"getddsfreq", no_argument, NULL, 'I'},
{"getsamples", no_argument, NULL, 'L'},
{"getchan", no_argument, NULL, 'C'},
{"getcurve", required_argument, NULL, 'B'},
{"getmonitamp", no_argument, NULL, 'E'},
{"getmonitpos", no_argument, NULL, 'F'},
{"monittimestamp", no_argument, NULL, 'O'},
{NULL, 0, NULL, 0}
};
typedef enum _var_type {
UINT8_T = 0,
UINT16_T,
UINT32_T,
UINT64_T,
FLOAT_T,
DOUBLE_T
} var_type;
typedef var_type func_type;
typedef struct _call_var_t {
const char *name;
int call;
int rw; // 1 is read and 0 is write
var_type type; // Just for printing the return value
uint8_t write_val[sizeof(uint32_t)*2]; // 2 32-bits variables
uint8_t read_val[sizeof(uint32_t)*2]; // 2 32-bits variable
} call_var_t;
typedef call_var_t call_func_t;
/***************************************************/
/*************** General Functions *****************/
/***************************************************/
#define BLINK_FUNC_ID 0
#define BLINK_FUNC_NAME "blink"
#define RESET_FUNC_ID 1
#define RESET_FUNC_NAME "reset"
#define GET_FMC_TEMP1_ID 2
#define GET_FMC_TEMP1_NAME "get_fmc_temp1"
#define GET_FMC_TEMP2_ID 3
#define GET_FMC_TEMP2_NAME "get_fmc_temp2"
#define SET_KX_ID 4
#define SET_KX_NAME "set_kx"
#define GET_KX_ID 5
#define GET_KX_NAME "get_kx"
#define SET_KY_ID 6
#define SET_KY_NAME "set_ky"
#define GET_KY_ID 7
#define GET_KY_NAME "set_ky"
#define SET_KSUM_ID 8
#define SET_KSUM_NAME "set_ksum"
#define GET_KSUM_ID 9
#define GET_KSUM_NAME "get_ksum"
#define SET_SW_ON_ID 10
#define SET_SW_ON_NAME "set_sw_on"
#define SET_SW_OFF_ID 11
#define SET_SW_OFF_NAME "set_sw_off"
#define GET_SW_ID 12
#define GET_SW_NAME "get_sw"
#define SET_SW_CLK_EN_ON_ID 13
#define SET_SW_CLK_EN_ON_NAME "set_sw_clk_en_on"
#define SET_SW_CLK_EN_OFF_ID 14
#define SET_SW_CLK_EN_OFF_NAME "set_sw_clk_en_off"
#define GET_SW_CLK_EN_ID 15
#define GET_SW_CLK_EN_NAME "get_sw_clk_en"
#define SET_SW_DIVCLK_ID 16
#define SET_SW_DIVCLK_NAME "set_sw_divclk"
#define GET_SW_DIVCLK_ID 17
#define GET_SW_DIVCLK_NAME "get_sw_divclk"
#define SET_SW_PHASECLK_ID 18
#define SET_SW_PHASECLK_NAME "set_sw_phaseclk"
#define GET_SW_PHASECLK_ID 19
#define GET_SW_PHASECLK_NAME "get_sw_phaseclk"
#define SET_WDW_ON_ID 20
#define SET_WDW_ON_NAME "set_wdw_on"
#define SET_WDW_OFF_ID 21
#define SET_WDW_OFF_NAME "set_wdw_off"
#define GET_WDW_ID 22
#define GET_WDW_NAME "get_wdw"
#define SET_WDW_DLY_ID 23
#define SET_WDW_DLY_NAME "set_wdw_dly"
#define GET_WDW_DLY_ID 24
#define GET_WDW_DLY_NAME "get_wdw_dly"
#define SET_ADCCLK_ID 25
#define SET_ADCCLK_NAME "set_adc_clk"
#define GET_ADCCLK_ID 26
#define GET_ADCCLK_NAME "get_adc_clk"
#define SET_DDSFREQ_ID 27
#define SET_DDSFREQ_NAME "set_dds_freq"
#define GET_DDSFREQ_ID 28
#define GET_DDSFREQ_NAME "get_dds_freq"
#define SET_ACQ_PARAM_ID 29
#define SET_ACQ_PARAM_NAME "set_acq_param"
#define GET_ACQ_SAMPLES_ID 30
#define GET_ACQ_SAMPLES_NAME "get_acq_samples"
#define GET_ACQ_CHAN_ID 31
#define GET_ACQ_CHAN_NAME "get_acq_chan"
#define SET_ACQ_START_ID 32
#define SET_ACQ_START_NAME "set_acq_start"
#define END_ID 33
static call_func_t call_func[END_ID] =
{
{BLINK_FUNC_NAME , 0, 0, UINT32_T, {0}, {0}},
{RESET_FUNC_NAME , 0, 0, UINT32_T, {0}, {0}},
{GET_FMC_TEMP1_NAME , 0, 1, DOUBLE_T, {0}, {0}},
{GET_FMC_TEMP2_NAME , 0, 1, DOUBLE_T, {0}, {0}},
{SET_KX_NAME , 0, 0, UINT32_T, {0}, {0}},
{GET_KX_NAME , 0, 1, UINT32_T, {0}, {0}},
{SET_KY_NAME , 0, 0, UINT32_T, {0}, {0}},
{GET_KY_NAME , 0, 1, UINT32_T, {0}, {0}},
{SET_KSUM_NAME , 0, 0, UINT32_T, {0}, {0}},
{GET_KSUM_NAME , 0, 1, UINT32_T, {0}, {0}},
{SET_SW_ON_NAME , 0, 0, UINT32_T, {0}, {0}},
{SET_SW_OFF_NAME , 0, 0, UINT32_T, {0}, {0}},
{GET_SW_NAME , 0, 1, UINT32_T, {0}, {0}},
{SET_SW_CLK_EN_ON_NAME , 0, 0, UINT32_T, {0}, {0}},
{SET_SW_CLK_EN_OFF_NAME , 0, 0, UINT32_T, {0}, {0}},
{GET_SW_CLK_EN_NAME , 0, 1, UINT32_T, {0}, {0}},
{SET_SW_DIVCLK_NAME , 0, 0, UINT32_T, {0}, {0}},
{GET_SW_DIVCLK_NAME , 0, 1, UINT32_T, {0}, {0}},
{SET_SW_PHASECLK_NAME , 0, 0, UINT32_T, {0}, {0}},
{GET_SW_PHASECLK_NAME , 0, 1, UINT32_T, {0}, {0}},
{SET_WDW_ON_NAME , 0, 0, UINT32_T, {0}, {0}},
{SET_WDW_OFF_NAME , 0, 0, UINT32_T, {0}, {0}},
{GET_WDW_NAME , 0, 1, UINT32_T, {0}, {0}},
{SET_WDW_DLY_NAME , 0, 0, UINT32_T, {0}, {0}},
{GET_WDW_DLY_NAME , 0, 1, UINT32_T, {0}, {0}},
{SET_ADCCLK_NAME , 0, 0, UINT32_T, {0}, {0}},
{GET_ADCCLK_NAME , 0, 1, UINT32_T, {0}, {0}},
{SET_DDSFREQ_NAME , 0, 0, UINT32_T, {0}, {0}},
{GET_DDSFREQ_NAME , 0, 1, UINT32_T, {0}, {0}},
{SET_ACQ_PARAM_NAME , 0, 0, UINT32_T, {0}, {0}},
{GET_ACQ_SAMPLES_NAME , 0, 1, UINT32_T, {0}, {0}},
{GET_ACQ_CHAN_NAME , 0, 1, UINT32_T, {0}, {0}},
{SET_ACQ_START_NAME , 0, 0, UINT32_T, {0}, {0}}
};
/***************************************************/
/*************** Streaming CURVES ******************/
/***************************************************/
#define CURVE_MONIT_AMP_ID 0
#define CURVE_MONIT_AMP_NAME "monit_amp"
#define CURVE_MONIT_POS_ID 1
#define CURVE_MONIT_POS_NAME "monit_pos"
#define END_MONIT_ID 2
static call_func_t call_curve_monit[END_MONIT_ID] =
{
{CURVE_MONIT_AMP_NAME , 0, 1, UINT32_T, {0}, {0}},
{CURVE_MONIT_POS_NAME , 0, 1, UINT32_T, {0}, {0}}
};
typedef struct _str_idx_t {
char *str_idx[4]; // four strings of four chars
} str_idx_t;
//static str_idx_t monit_str_idx[END_MONIT_ID] = {
// {{"MONIT_AMP_A", "MONIT_AMP_B", "MONIT_AMP_C", "MONIT_AMP_D"}},
// {{"MONIT_POS_X", "MONIT_POS_Y", "MONIT_POS_Q", "MONIT_POS_SUM"}}
//};
#define ANY_CURVE_TYPE_ID 0
#define ANY_CURVE_TYPE_NAME "any_type_curve"
#define END_CURVE_TYPE_ID 1
static call_func_t call_curve_type[END_CURVE_TYPE_ID] = {
{ANY_CURVE_TYPE_NAME , 0, 1, UINT32_T, {0}, {0}}
};
/***************************************************/
/*************** On-demand CURVES ******************/
/***************************************************/
// We have 5 curves declared in server:
// 0 -> ADC, 1-> TBTAMP, 2 -> TBTPOS,
// 3 -> FOFBAMP, 4 -> FOFBPOS
#define CURVE_ADC_ID 0
#define CURVE_ADC_NAME "adc_curve"
#define CURVE_TBTAMP_ID 1
#define CURVE_TBTAMP_NAME "tbtamp_curve"
#define CURVE_TBTPOS_ID 2
#define CURVE_TBTPOS_NAME "tbtpos_curve"
#define CURVE_FOFBAMP_ID 3
#define CURVE_FOFBAMP_NAME "fofbamp_curve"
#define CURVE_FOFMPOS_ID 4
#define CURVE_FOFBPOS_NAME "fofbpos_curve"
#define END_CURVE_ID 5
static call_func_t call_curve[END_CURVE_ID] = {
{CURVE_ADC_NAME , 0, 1, UINT32_T, {0}, {0}},
{CURVE_TBTAMP_NAME , 0, 1, UINT32_T, {0}, {0}},
{CURVE_TBTPOS_NAME , 0, 1, UINT32_T, {0}, {0}},
{CURVE_FOFBAMP_NAME , 0, 1, UINT32_T, {0}, {0}},
{CURVE_FOFBPOS_NAME , 0, 1, UINT32_T, {0}, {0}}
};
/***************************************************/
/*************** RFFE Functions * ******************/
/***************************************************/
////enum var_type {
//// UINT8_T = 0,
//// UINT16_T,
//// UINT32_T,
//// UINT64_T,
//// FLOAT_T,
//// DOUBLE_T
////};
////struct call_var_t {
//// const char *name;
//// int call;
//// int rw; // 1 is read and 0 is write
//// enum var_type type;
//// uint8_t write_val[sizeof(uint32_t)*2]; // 2 32-bits variables
//// uint8_t read_val[sizeof(uint32_t)*2]; // 2 32-bits variable
////};
#define SET_FE_SW_ON_ID 0
#define SET_FE_SW_ON_NAME "getset_fe_sw"
#define SET_FE_SW_OFF_ID (SET_FE_SW_ON_ID) // They are the same ID in FE server
#define SET_FE_SW_OFF_NAME SET_FE_SW_ON_NAME
#define GET_FE_SW_ID (SET_FE_SW_ON_ID) // The are the same ID in FE server
#define GET_FE_SW_NAME SET_FE_SW_ON_NAME
//#define GETSET_FE_SW_LVL_ID 1
//#define GETSET_FE_SW_LVL_NAME "getset_sw_lvl"
#define GETSET_FE_ATT1_ID 1
#define GETSET_FE_ATT1_NAME "getset_fe_att1"
#define GETSET_FE_ATT2_ID 2
#define GETSET_FE_ATT2_NAME "getset_fe_att2"
#define GET_FE_TEMP1_ID 3
#define GET_FE_TEMP1_NAME "get_fe_temp1"
#define GET_FE_TEMP2_ID 4
#define GET_FE_TEMP2_NAME "get_fe_temp2"
#define END_FE_ID 5
static call_var_t call_fe_var[END_FE_ID] = {
{SET_FE_SW_ON_NAME , 0, 0, UINT8_T, {0}, {0}}, // The set "sw off" and "get sw"
// are on the same ID in FE server
//{GETSET_FE_SW_LVL_NAME , 0, 0, UINT8_T, {0}, {0}}, // The set "sw lvl" and get "sw lvl"
// // are on the same ID in FE server
{GETSET_FE_ATT1_NAME , 0, 0, DOUBLE_T, {0}, {0}}, // The set "att1" and get "att1"
// are on the same ID in FE server
{GETSET_FE_ATT2_NAME , 0, 0, DOUBLE_T, {0}, {0}}, // The set "att2" and get "att2"
// are on the same ID in FE server
{GET_FE_TEMP1_NAME , 0, 0, DOUBLE_T, {0}, {0}},
{GET_FE_TEMP2_NAME , 0, 0, DOUBLE_T, {0}, {0}}
};
// Some FE variable values
#define FE_SW_OFF 0x1
#define FE_SW_ON 0x3
// Some FE corection factors
#define FE_SW_DIV_FACTOR 2
/***************************************************/
/************ Client Utility Functions *************/
/***************************************************/
#define NUM_CHANNELS 4
#define SIZE_16_BYTES sizeof(uint16_t)
#define SIZE_32_BYTES sizeof(uint32_t)
/* Print data composed of 16-bit signed data */
int print_curve_16 (uint8_t *curve_data, uint32_t len)
{
unsigned int i;
for (i = 0; i < len/(SIZE_16_BYTES*NUM_CHANNELS); ++i) {
printf ("%d %d %d %d\n",
*((int16_t *)curve_data + i*NUM_CHANNELS),
*((int16_t *)curve_data + i*NUM_CHANNELS+1),
*((int16_t *)curve_data + i*NUM_CHANNELS+2),
*((int16_t *)curve_data + i*NUM_CHANNELS+3));
}
return 0;
}
/* Print data composed of 32-bit signed data */
int print_curve_32 (uint8_t *curve_data, uint32_t len)
{
unsigned int i;
//for (i = 0; i < len/4; ++i) {
// printf ("%d\n", *((int32_t *)((uint8_t *)curve_data + i*4)));
//}
for (i = 0; i < len/(SIZE_32_BYTES*NUM_CHANNELS); ++i) {
printf ("%d %d %d %d\n",
*((int32_t *)curve_data + i*NUM_CHANNELS),
*((int32_t *)curve_data + i*NUM_CHANNELS+1),
*((int32_t *)curve_data + i*NUM_CHANNELS+2),
*((int32_t *)curve_data + i*NUM_CHANNELS+3));
}
return 0;
}
int print_stream_curve (int monit_timestamp,
plot_values_monit_uint32_t *pval_monit_uint32)
{
if (monit_timestamp) {
printf ("%s ", timestamp_str ());
}
printf ("%d %d %d %d\n",
pval_monit_uint32->ch0,
pval_monit_uint32->ch1,
pval_monit_uint32->ch2,
pval_monit_uint32->ch3);
fflush(stdout);
return 0;
}
int read_bsmp_val(call_var_t *fe_var)
{
// Find out with type of varible this is and printf
// the correct string specifier
switch (fe_var->type) {
case UINT8_T:
printf ("%" PRIu8 "\n", *((uint8_t *)fe_var->read_val));
break;
case UINT16_T:
printf ("%" PRIu16 "\n", *((uint16_t *)fe_var->read_val));
break;
case UINT32_T:
printf ("%" PRIu32 "\n", *((uint32_t *)fe_var->read_val));
break;
case UINT64_T:
printf ("%" PRIu64 "\n", *((uint64_t *)fe_var->read_val));
break;
case FLOAT_T:
printf ("%f\n", *((float *)fe_var->read_val));
break;
case DOUBLE_T:
printf ("%f\n", *((double *)fe_var->read_val));
break;
default:
printf ("%" PRIu8 "\n", *((uint8_t *)fe_var->read_val));
}
return 0;
}
int read_bsmp_val_v(int verbose, call_var_t *fe_var)
{
PRINTV(verbose, "%s: ", fe_var->name);
if (verbose) {
read_bsmp_val(fe_var);
}
return 0;
}
int read_bsmp_func(call_func_t *func)
{
return read_bsmp_val((call_var_t *)func);
}
int read_bsmp_func_v(int verbose, call_func_t *func)
{
return read_bsmp_val_v(verbose, (call_var_t *)func);
}
int main(int argc, char *argv[])
{
//struct addrinfo hints, *servinfo, *p;
//int rv;
//char s[INET6_ADDRSTRLEN];
//int yes = 1;
int verbose = 0;
int ch;
// Acquitision parameters check
int acq_samples_set = 0;
uint32_t acq_samples_val = 0;
int acq_chan_set = 0;
uint32_t acq_chan_val = 0;
uint32_t acq_curve_chan = 0;
program_name = argv[0];
// loop over all of the options
while ((ch = getopt_long(argc, argv, "hvbro:w:x:y:s:jk12d:p:uen:q:i:l:c:gmta:z:RTXYSJ3GDPNUQILCAZMKCB:EFO",
long_options, NULL)) != -1)
{
// check to see if a single character or long option came through
switch (ch)
{
case 'h':
print_usage(stderr, 0);
case 'v':
verbose = 1;
break;
// Blink leds
case 'b':
call_func[BLINK_FUNC_ID].call = 1;
need_hostname = 1;
break;
// Reset to default
case 'r':
call_func[RESET_FUNC_ID].call = 1;
need_hostname = 1;
break;
// Set FPGA Hostname
case 'o':
hostname = strdup(optarg);
break;
// Set RFFE Hostname
case 'w':
fe_hostname = strdup(optarg);
break;
// Set KX
case 'x':
call_func[SET_KX_ID].call = 1;
*((uint32_t *)call_func[SET_KX_ID].write_val) = (uint32_t) atoi(optarg);
need_hostname = 1;
break;
// Set KY
case 'y':
call_func[SET_KY_ID].call = 1;
*((uint32_t *)call_func[SET_KY_ID].write_val) = (uint32_t) atoi(optarg);
need_hostname = 1;
break;
// Set Ksum
case 's':
call_func[SET_KSUM_ID].call = 1;
*((uint32_t *)call_func[SET_KSUM_ID].write_val) = (uint32_t) atoi(optarg);
need_hostname = 1;
break;
// Set FPGA Deswitching On
case 'j':
call_func[SET_SW_ON_ID].call = 1;
need_hostname = 1;
break;
// Set FPGA Deswitching Off
case 'k':
call_func[SET_SW_OFF_ID].call = 1;
need_hostname = 1;
break;
// Set FPGA Switching clock enable on
case '1':
call_func[SET_SW_CLK_EN_ON_ID].call = 1;
need_hostname = 1;
break;
// Set FPGA Switching clock enable Off
case '2':
call_func[SET_SW_CLK_EN_OFF_ID].call = 1;
need_hostname = 1;
break;
// Set FE Switching On
case 'g':
call_fe_var[SET_FE_SW_ON_ID].call = 1;
call_fe_var[SET_FE_SW_ON_ID].rw = 0; // write to variable
*call_fe_var[SET_FE_SW_ON_ID].write_val = (uint8_t) FE_SW_ON;
need_fe_hostname = 1;
break;
// Set FE Switching Off
case 'm':
call_fe_var[SET_FE_SW_ON_ID].call = 1;
call_fe_var[SET_FE_SW_ON_ID].rw = 0;
// *call_fe_var[SET_FE_SW_ON_ID].write_val = (uint8_t) FE_SW_OFF;
*call_fe_var[SET_FE_SW_ON_ID].write_val = (uint8_t) 0x1;
need_fe_hostname = 1;
break;
// Set DIVCLK
// FIXME: This command is correctly implemented in the FPGA
// firmware, but on the RFFE controller we need 2x the clock
// in order to drive the switching clock, because it clocks a
// JK-FF in toggle mode.
//
// So, we just divide the clk div here by 2
case 'd':
call_func[SET_SW_DIVCLK_ID].call = 1;
*((uint32_t *)call_func[SET_SW_DIVCLK_ID].write_val) = (uint32_t) (atoi(optarg)/FE_SW_DIV_FACTOR);
need_hostname = 1;
break;
// Set PHASECLK
case 'p':
call_func[SET_SW_PHASECLK_ID].call = 1;
*((uint32_t *)call_func[SET_SW_PHASECLK_ID].write_val) = (uint32_t) atoi(optarg);
need_hostname = 1;
break;
// Set Windowing On
case 'u':
call_func[SET_WDW_ON_ID].call = 1;
need_hostname = 1;
break;
// Set Windowing Off
case 'e':
call_func[SET_WDW_OFF_ID].call = 1;
need_hostname = 1;
break;
// Set Windowing delay
case 'n':
call_func[SET_WDW_DLY_ID].call = 1;
*((uint32_t *)call_func[SET_WDW_DLY_ID].write_val) = (uint32_t) atoi(optarg);
need_hostname = 1;
break;
// Set ADCCLK
case 'q':
call_func[SET_ADCCLK_ID].call = 1;
*((uint32_t *)call_func[SET_ADCCLK_ID].write_val) = (uint32_t) atoi(optarg);
need_hostname = 1;
break;
// Set DDSFREQ
case 'i':
call_func[SET_DDSFREQ_ID].call = 1;
*((uint32_t *)call_func[SET_DDSFREQ_ID].write_val) = (uint32_t) atoi(optarg);
need_hostname = 1;
break;
// Set Acq Samples
case 'l':
//call_func[SET_ACQ_SAMPLES_ID].call = 1;
//*((uint32_t *)call_func[SET_ACQ_SAMPLES_ID].write_val) = (uint32_t) atoi(optarg);
acq_samples_set = 1;
acq_samples_val = (uint32_t) atoi(optarg);
need_hostname = 1;
break;
// Set Acq Chan
case 'c':
//call_func[SET_ACQ_CHAN_ID].call = 1;
//*((uint32_t *)call_func[SET_ACQ_CHAN_ID].write_val) = (uint32_t) atoi(optarg);
acq_chan_set = 1;
acq_chan_val = (uint32_t) atoi(optarg);
need_hostname = 1;
break;
// Set Acq Start
case 't':
call_func[SET_ACQ_START_ID].call = 1;
need_hostname = 1;
break;
// Set FE Att1
case 'a':
call_fe_var[GETSET_FE_ATT1_ID].call = 1;
call_fe_var[GETSET_FE_ATT1_ID].rw = 0; // Write value to variable
*((double *)call_fe_var[GETSET_FE_ATT1_ID].write_val) = (double) atof(optarg);
need_fe_hostname = 1;
break;
// Set FE Att2
case 'z':
call_fe_var[GETSET_FE_ATT2_ID].call = 1;
call_fe_var[GETSET_FE_ATT2_ID].rw = 0; // Write value to variable
*((double *)call_fe_var[GETSET_FE_ATT2_ID].write_val) = (double) atof(optarg);
need_fe_hostname = 1;
break;
// Get FMC temp1
case 'R':
call_func[GET_FMC_TEMP1_ID].call = 1;
need_hostname = 1;
break;
// Get FMC temp2
case 'T':
call_func[GET_FMC_TEMP2_ID].call = 1;
need_hostname = 1;
break;
// Get Kx
case 'X':
call_func[GET_KX_ID].call = 1;
need_hostname = 1;
break;
// Get Ky
case 'Y':
call_func[GET_KY_ID].call = 1;
need_hostname = 1;
break;
// Get Ksum
case 'S':
call_func[GET_KSUM_ID].call = 1;
need_hostname = 1;
break;
// Get FPGA Deswitching State
case 'J':
call_func[GET_SW_ID].call = 1;
need_hostname = 1;
break;