-
Notifications
You must be signed in to change notification settings - Fork 2
/
read_regs.cc
421 lines (307 loc) · 8.32 KB
/
read_regs.cc
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
using namespace std;
#include <iostream>
#include <iomanip>
#include <parseargument.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#define MY_IP "10.0.0.3"
#define SRS_IP "10.0.0.2"
#define RECPORT 6007
typedef struct {
unsigned int RequestId;
unsigned int SubAddress;
unsigned int CmdField1;
unsigned int CmdField2;
unsigned int data[8896];
} requeststructure;
struct valuepair {
unsigned int errorcode;
unsigned int value;
};
typedef struct {
unsigned int RequestId;
unsigned int SubAddress;
unsigned int CmdField1;
unsigned int CmdField2;
struct valuepair vp[512];
} returnstructure;
int read_params ( const int regport, const int regcount, returnstructure *returnblock);
int set_value( const unsigned int adr, const int reqport, const unsigned int val);
void exithelp( const int ret =1)
{
cout << std::endl;
cout << " srs_control usage " << std::endl;
cout << " srs_control DAQ_IP <ip> set ip of DAQ stream" << std::endl;
cout << " srs_control BLCK_MODE val trigger mode" << std::endl;
cout << " srs_control BLCK_TRGBURST val number of time slots (<=9)" << std::endl;
cout << " srs_control BLCK_FREQ val dead time or frequency" << std::endl;
cout << " srs_control EVBLD_CHMASK val channel mask for hybrids" << std::endl;
cout << " srs_control RO_ENABLE start acquisition" << std::endl;
cout << " srs_control RO_DISABLE stop acquisition" << std::endl;
cout << " srs_control readfpga read all fpga parameters" << std::endl;
cout << " srs_control readapv read all APV parameters" << std::endl;
cout << " srs_control readadccard read all ADC card parameters" << std::endl;
cout << " " << std::endl;
exit(ret);
}
int main(int argc, char *argv[])
{
int reqport = 0;
int regcount =0;
if ( argc==1)
{
cout << "usage: " << argv[0] << " action " << endl;
exithelp();
}
returnstructure returnblock;
unsigned int val = 0;
if ( argc > 2 )
{
int i;
if ( strcmp( argv[1], "DAQ_IP") == 0)
{
val = get_uvalue( argv[2]);
i = set_value( 10, 6007, val);
}
else if ( strcmp( argv[1], "BLCK_MODE") == 0)
{
val = get_uvalue( argv[2]);
i = set_value( 0, 6039, val);
}
else if ( strcmp( argv[1], "BLCK_TRGBURST") == 0)
{
val = get_uvalue( argv[2]);
i = set_value( 1, 6039, val);
}
else if ( strcmp( argv[1], "BLCK_FREQ") == 0)
{
val = get_uvalue( argv[2]);
i = set_value( 2, 6039, val);
}
else if ( strcmp( argv[1], "EVBLD_CHMASK") == 0)
{
val = get_uvalue( argv[2]);
val &=0xffff;
i = set_value( 8, 6039, val);
}
}
else
{
int i;
if ( strcmp( argv[1], "help") == 0)
{
exithelp(0);
}
else if ( strcmp( argv[1], "readfpga") == 0)
{
reqport = 6007;
regcount = 11;
}
else if ( strcmp( argv[1], "readapv") == 0)
{
reqport = 6039;
regcount = 21;
}
else if ( strcmp( argv[1], "readadccard") == 0)
{
reqport = 6519;
regcount = 6;
}
else if ( strcmp( argv[1], "RO_ENABLE") == 0)
{
val = 1;
i = set_value( 0xF, 6039, val);
}
else if ( strcmp( argv[1], "RO_DISABLE") == 0)
{
val = 0;
set_value( 0xF, 6039, val);
}
if ( reqport && regcount )
{
int r = read_params ( reqport, regcount, &returnblock);
cout << "received: " << r << " bytes" << endl;
if ( r>0)
{
cout << " Requestid: " << hex << ntohl(returnblock.RequestId) << dec << endl;
cout << " SubAdress: " << hex << ntohl(returnblock.SubAddress) << dec << endl;
cout << " CmdField1: " << hex << ntohl(returnblock.CmdField1) << dec << endl;
cout << " CmdField2: " << hex << ntohl(returnblock.CmdField2) << dec << endl;
int x = (r/4 -4)/2;
for ( int i = 0; i < x; i++)
{
cout << setw(3) << i << " " << setw(10) << hex << ntohl(returnblock.vp[i].value) <<dec << " ";
if ( returnblock.vp[i].errorcode)
{
cout << setw(10) << hex << ntohl(returnblock.vp[i].errorcode) <<dec;
}
else
{
cout << " ok";
}
cout << endl;
}
}
}
}
return 0;
}
int read_params ( const int regport, const int regcount, returnstructure *returnblock)
{
struct sockaddr_in _server;
int _sockfd;
in_addr _srsip;
in_addr _myip;
if (inet_aton(SRS_IP, &_srsip)==0)
{
cerr << "invalid IP address " << SRS_IP << endl;
return 1;
}
if (inet_aton(MY_IP, &_myip)==0)
{
cerr << "invalid IP address " << SRS_IP << endl;
return 1;
}
socklen_t slen=sizeof(_server);
if ( ( _sockfd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP) ) ==-1)
{
perror("socket " );
return -1;
}
struct sockaddr_in si_return;
socklen_t sl = sizeof(si_return );
memset((char *) &si_return, 0, sizeof(si_return));
si_return.sin_family = AF_INET;
si_return.sin_port = htons(RECPORT);
si_return.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(_sockfd, (sockaddr *)&si_return, sizeof(si_return))==-1)
{
perror("bind");
return -6;
}
memset ( (char *) &_server, 0, slen);
_server.sin_family = AF_INET;
_server.sin_addr = _srsip;
_server.sin_port = htons(regport);
requeststructure request;
request.RequestId = ntohl(0x87654000);
request.SubAddress =ntohl(0x00000000);
request.CmdField1 = ntohl(0xBBAAFFFF);
request.CmdField2 = 0;
for ( int i = 0; i < regcount; i++)
{
request.data[i] = htonl(i);
}
if (sendto(_sockfd, &request, 4*4+regcount*4, 0, (sockaddr *) &_server, slen)==-1)
{
perror("sendto " );
return -2;
}
int r;
fd_set read_flag;
FD_ZERO(&read_flag);
FD_SET(_sockfd, &read_flag);
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 300000;
int retval = select(_sockfd+1, &read_flag, NULL, NULL, &tv);
if ( retval >0)
{
if ( ( r = recvfrom(_sockfd, returnblock, sizeof(*returnblock), 0, (sockaddr *) &si_return, &sl) ) ==-1)
{
perror("recfrom");
return -5;
}
close (_sockfd);
return r;
}
else
{
cout << "timeout " << endl;
close ( _sockfd);
return -1;
}
return 0;
}
int set_value( const unsigned int adr, const int reqport, const unsigned int val)
{
struct sockaddr_in _server;
int _sockfd;
in_addr _srsip;
in_addr _myip;
if (inet_aton(SRS_IP, &_srsip)==0)
{
cerr << "invalid IP address " << SRS_IP << endl;
return 1;
}
if (inet_aton(MY_IP, &_myip)==0)
{
cerr << "invalid IP address " << SRS_IP << endl;
return 1;
}
socklen_t slen=sizeof(_server);
if ( ( _sockfd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP) ) ==-1)
{
perror("socket " );
return -1;
}
struct sockaddr_in si_return;
socklen_t sl = sizeof(si_return );
memset((char *) &si_return, 0, sizeof(si_return));
si_return.sin_family = AF_INET;
si_return.sin_port = htons(RECPORT);
si_return.sin_addr = _myip;
if (bind(_sockfd, (sockaddr *)&si_return, sizeof(si_return))==-1)
{
perror("bind");
return -6;
}
memset ( (char *) &_server, 0, slen);
_server.sin_family = AF_INET;
_server.sin_addr = _srsip;
_server.sin_port = htons(reqport);
requeststructure request;
request.RequestId = htonl(0x87654000);
request.SubAddress =htonl(0x00000000);
request.CmdField1 = htonl(0xAAAAFFFF);
request.CmdField2 = 0;
request.data[0] = htonl(adr);
request.data[1] = htonl(val);
if (sendto(_sockfd, &request, 4*4 + 2*4, 0, (sockaddr *) &_server, slen)==-1)
{
perror("sendto " );
return -2;
}
int r;
fd_set read_flag;
FD_ZERO(&read_flag);
FD_SET(_sockfd, &read_flag);
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 300000;
returnstructure returnblock;
int retval = select(_sockfd+1, &read_flag, NULL, NULL, &tv);
if ( retval >0)
{
if ( ( r = recvfrom(_sockfd, &returnblock, sizeof(returnblock), 0, (sockaddr *) &si_return, &sl) ) ==-1)
{
perror("recfrom");
return -5;
}
close (_sockfd);
return r;
}
else
{
cout << "timeout " << endl;
close ( _sockfd);
return -1;
}
return 0;
}