Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
291 | f9daq | 1 | #include "easiroc.h" |
2 | |||
3 | static const int SiTCP_PORT = 24; |
||
4 | SOCKET sock; |
||
5 | |||
6 | unsigned int slowdata[sizeByte_SC]; |
||
7 | unsigned int ReadSCdata[sizeByte_RSC]; |
||
8 | unsigned int Probedata[sizeByte_PSC]; |
||
9 | unsigned int Pindata[sizeByte_PIN]; |
||
10 | |||
11 | unsigned int Probedata[sizeByte_PSC]; |
||
12 | |||
13 | int ForceStop = 0; |
||
14 | int EndADC = 0; |
||
15 | |||
16 | #include <windows.h> |
||
17 | |||
18 | void usleep(__int64 usec) |
||
19 | { |
||
20 | HANDLE timer; |
||
21 | LARGE_INTEGER ft; |
||
22 | |||
23 | ft.QuadPart = -(10*usec); // Convert to 100 nanosecond interval, negative value indicates relative time |
||
24 | |||
25 | timer = CreateWaitableTimer(NULL, TRUE, NULL); |
||
26 | SetWaitableTimer(timer, &ft, 0, NULL, NULL, 0); |
||
27 | WaitForSingleObject(timer, INFINITE); |
||
28 | CloseHandle(timer); |
||
29 | } |
||
30 | |||
31 | //------------------------------------------------------------------------------------------------------ |
||
32 | int easiroc_Init(const char* SiTCP_MASTER_IP, unsigned int daq_mode) |
||
33 | { |
||
34 | // Start WinSock |
||
35 | printf("easiroc_Init --> Start.\n"); |
||
36 | WORD wVersionRequested; |
||
37 | WSADATA wsaData; |
||
38 | if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { |
||
39 | printf("WSAStartup failed with error!\n"); |
||
40 | } |
||
41 | |||
42 | //Initialize ------------------------------------------------------------- |
||
43 | |||
44 | sock = INVALID_SOCKET; |
||
45 | struct sockaddr_in SiTCP_ADDR; |
||
46 | const char* IP; |
||
47 | unsigned int port; |
||
48 | |||
49 | sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
||
50 | if(sock == INVALID_SOCKET) { |
||
51 | printf("Error: easiroc_Init::sock = %d\n", sock); |
||
52 | return -1; |
||
53 | } |
||
54 | IP = SiTCP_MASTER_IP; |
||
55 | port = SiTCP_PORT; |
||
56 | SiTCP_ADDR.sin_family = AF_INET; |
||
57 | SiTCP_ADDR.sin_port = htons(port); |
||
58 | SiTCP_ADDR.sin_addr.s_addr = inet_addr(IP); |
||
59 | |||
60 | struct timeval tv; |
||
61 | tv.tv_sec = 3; |
||
62 | tv.tv_usec = 0; |
||
63 | setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char*)&tv, sizeof(tv)); |
||
64 | |||
65 | int flag = 1; |
||
66 | setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char*)&flag, sizeof(flag)); |
||
67 | |||
68 | //Connection ------------------------------------------------------------- |
||
69 | int con = connect(sock, (struct sockaddr*)&SiTCP_ADDR, sizeof(SiTCP_ADDR)); |
||
70 | printf("easiroc_Init::connect = %d\n", con); |
||
71 | if(0 > con){ |
||
72 | printf("SiTCP Master :: Connection fail\n"); |
||
73 | closesocket(sock); |
||
74 | return -1; |
||
75 | } |
||
76 | |||
77 | printf("SiTCP Master :: Connection Done\n\n"); |
||
78 | |||
79 | // Auto Initialize ------------------------------------------------------- |
||
80 | PrepareFPGA(); |
||
81 | DebugFPGA(sock); |
||
82 | printf("\nASIS Initialize : Done\n\n"); |
||
83 | //Sleep(1000); |
||
84 | |||
85 | WriteData(sock, 37888); |
||
86 | PrepareSC(1); |
||
87 | TransmitSC(sock); |
||
88 | printf("\nSlow Control chip1 : Done\n\n"); |
||
89 | PrepareReadSC(1); |
||
90 | TransmitReadSC(sock); |
||
91 | printf("\nRead Slow Control chip1 : Done\n\n"); |
||
92 | |||
93 | WriteData(sock, 21504); |
||
94 | PrepareSC(2); |
||
95 | TransmitSC(sock); |
||
96 | printf("\nSlow Control chip2 : Done\n\n"); |
||
97 | PrepareReadSC(2); |
||
98 | TransmitReadSC(sock); |
||
99 | printf("\nRead Slow Control chip2: Done\n\n"); |
||
100 | |||
101 | WriteData(sock, 5120); |
||
102 | { |
||
103 | unsigned int signal = 31; |
||
104 | signal = signal << 16; |
||
105 | unsigned int data = daq_mode; |
||
106 | data = data << 8; |
||
107 | signal += data; |
||
108 | if(-1 == WriteData(sock, signal)){ |
||
109 | return -1; |
||
110 | } |
||
111 | printf("\n#D : DAQ mode is %d \n", daq_mode); |
||
112 | } |
||
113 | |||
114 | |||
115 | return 0; |
||
116 | } |
||
117 | //------------------------------------------------------------------------------------------------------ |
||
118 | void easiroc_Close() |
||
119 | { |
||
120 | closesocket(sock); |
||
121 | WSACleanup(); |
||
122 | printf("easiroc_Close -> Done.\n"); |
||
123 | } |
||
124 | //------------------------------------------------------------------------------------------------------ |
||
125 | int WriteData(SOCKET sock, unsigned int data) |
||
126 | { |
||
127 | int esrcdbg = 0; |
||
128 | |||
129 | data += 128 << 24;; |
||
130 | |||
131 | if(esrcdbg) printf("0x%X\n", data); |
||
132 | send(sock, (char*)&data, sizeof(int), 0); |
||
133 | // this sleep is needed! |
||
134 | Sleep(WRITEDATA_DELAY); |
||
135 | |||
136 | unsigned int buf = 0; |
||
137 | unsigned int length = 4; |
||
138 | int ret = recv(sock, (char*)&buf, length, 0); |
||
139 | |||
140 | if ( ret > 0 ) { |
||
141 | if(esrcdbg) printf("Bytes received: %d\n", ret); |
||
142 | else printf("."); |
||
143 | } else if ( ret == 0 ) { |
||
144 | printf("Connection closed\n"); |
||
145 | return -1; |
||
146 | } else { |
||
147 | printf("recv failed: %d\n", WSAGetLastError()); |
||
148 | return -1; |
||
149 | } |
||
150 | |||
151 | return 0; |
||
152 | } |
||
153 | //------------------------------------------------------------------------------------------------------ |
||
154 | void PrepareFPGA() |
||
155 | { |
||
156 | Pindata[0] = 20; |
||
157 | Pindata[1] = 65776; |
||
158 | } |
||
159 | //------------------------------------------------------------------------------------------------------ |
||
160 | void PrepareSC(int chipNo) |
||
161 | { |
||
162 | if(chipNo == 1) { |
||
163 | slowdata[0] = 192; |
||
164 | slowdata[1] = 1720319; |
||
165 | slowdata[2] = 4253040640; |
||
166 | slowdata[3] = 413; |
||
167 | slowdata[4] = 0; |
||
168 | slowdata[5] = 3739423232; |
||
169 | slowdata[6] = 4294967295; |
||
170 | slowdata[7] = 4294967295; |
||
171 | slowdata[8] = 4294967295; |
||
172 | slowdata[9] = 4294967295; |
||
173 | slowdata[10] = 4294967295; |
||
174 | slowdata[11] = 4294967295; |
||
175 | slowdata[12] = 4294967295; |
||
176 | slowdata[13] = 4294967295; |
||
177 | slowdata[14] = 4294967295; |
||
178 | } else { |
||
179 | slowdata[0] = 192; |
||
180 | slowdata[1] = 4292874239; |
||
181 | slowdata[2] = 4257218559; |
||
182 | slowdata[3] = 478; |
||
183 | slowdata[4] = 0; |
||
184 | slowdata[5] = 3740340736; |
||
185 | slowdata[6] = 1175693740; |
||
186 | slowdata[7] = 2815957775; |
||
187 | slowdata[8] = 766945147; |
||
188 | slowdata[9] = 1504433946; |
||
189 | slowdata[10] = 2729016485; |
||
190 | slowdata[11] = 3412554245; |
||
191 | slowdata[12] = 2725848728; |
||
192 | slowdata[13] = 2267165112; |
||
193 | slowdata[14] = 4291041547; |
||
194 | } |
||
195 | } |
||
196 | //------------------------------------------------------------------------------------------------------ |
||
197 | void PrepareReadSC(int chipNo) |
||
198 | { |
||
199 | if(chipNo == 1) { |
||
200 | ReadSCdata[0] = 0; |
||
201 | } else { |
||
202 | ReadSCdata[0] = 1; |
||
203 | } |
||
204 | } |
||
205 | //------------------------------------------------------------------------------------------------------ |
||
206 | int DebugFPGA(SOCKET socket) |
||
207 | { |
||
208 | unsigned int buffer = 0; |
||
209 | buffer += 0 << 16; |
||
210 | buffer += (Pindata[0] & 255) << 8; |
||
211 | if(-1 == WriteData(socket, buffer)){ |
||
212 | return -1; |
||
213 | } |
||
214 | |||
215 | for(int i = 1 ; i<5; ++i){ |
||
216 | buffer = 0; |
||
217 | if(i == 4){ |
||
218 | buffer += 5 << 16; |
||
219 | }else{ |
||
220 | buffer += i << 16; |
||
221 | } |
||
222 | buffer += ((Pindata[1] >> (i-1)*8) & 255) << 8; |
||
223 | |||
224 | if(-1 == WriteData(socket, buffer)){ |
||
225 | return -1; |
||
226 | } |
||
227 | Sleep(1); |
||
228 | } |
||
229 | |||
230 | return 0; |
||
231 | } |
||
232 | //------------------------------------------------------------------------------------------------------ |
||
233 | int TransmitSC(SOCKET socket) |
||
234 | { |
||
235 | unsigned int data = 0; |
||
236 | //Set SC mode ----------------------------------------------------------- |
||
237 | data = 0; |
||
238 | data += 1 << 16; |
||
239 | data += 240 << 8; |
||
240 | if(-1 == WriteData(socket, data)){ |
||
241 | return -1; |
||
242 | } |
||
243 | |||
244 | //SC start ------------------------------------------------------------- |
||
245 | data = 0; |
||
246 | data += 10 << 16; |
||
247 | data += (slowdata[0] & 255) << 8; |
||
248 | if(-1 == WriteData(socket, data)){ |
||
249 | return -1; |
||
250 | } |
||
251 | |||
252 | for(int i = 1; i<15; ++i){ |
||
253 | for(int shift = 0; shift<4; ++shift){ |
||
254 | data = 0; |
||
255 | data += 10 << 16; |
||
256 | data += ((slowdata[i] >> 8*shift) & 255) << 8; |
||
257 | if(-1 == WriteData(socket, data)){ |
||
258 | return -1; |
||
259 | } |
||
260 | //std::cout<<"test"<<std::endl; |
||
261 | Sleep(1); |
||
262 | } |
||
263 | } |
||
264 | |||
265 | // Sleep(50000); |
||
266 | |||
267 | //StartCycle ----------------------------------------------------------- |
||
268 | data = 0; |
||
269 | data += 1 << 16; |
||
270 | data += 242 << 8; |
||
271 | if(-1 == WriteData(socket, data)){ |
||
272 | return -1; |
||
273 | } |
||
274 | |||
275 | data = 0; |
||
276 | data += 1 << 16; |
||
277 | data += 240 << 8; |
||
278 | if(-1 == WriteData(socket, data)){ |
||
279 | return -1; |
||
280 | } |
||
281 | |||
282 | // Sleep(50000); |
||
283 | |||
284 | //Load SC -------------------------------------------------------------- |
||
285 | data = 0; |
||
286 | data += 1 << 16; |
||
287 | data += 241 << 8; |
||
288 | if(-1 == WriteData(socket, data)){ |
||
289 | return -1; |
||
290 | } |
||
291 | |||
292 | data = 0; |
||
293 | data += 1 << 16; |
||
294 | data += 240 << 8; |
||
295 | if(-1 == WriteData(socket, data)){ |
||
296 | return -1; |
||
297 | } |
||
298 | |||
299 | return 0; |
||
300 | } |
||
301 | //------------------------------------------------------------------------------------------------------ |
||
302 | int TransmitReadSC(SOCKET socket) |
||
303 | { |
||
304 | //SCA read --------------------------------------------------------------- |
||
305 | unsigned int data = 0; |
||
306 | |||
307 | for(int i = 0; i<4; ++i){ |
||
308 | data = 0; |
||
309 | data += 12 << 16; |
||
310 | data += ((ReadSCdata[0] >> i*8) & 255) << 8; |
||
311 | if(-1 == WriteData(socket, data)){ |
||
312 | return -1; |
||
313 | } |
||
314 | Sleep(1); |
||
315 | } |
||
316 | |||
317 | //StartCycle ------------------------------------------------------------ |
||
318 | data = 0; |
||
319 | data += 1 << 16; |
||
320 | data += 242 << 8; |
||
321 | if(-1 == WriteData(socket, data)){ |
||
322 | return -1; |
||
323 | } |
||
324 | |||
325 | data = 0; |
||
326 | data += 1 << 16; |
||
327 | data += 240 << 8; |
||
328 | if(-1 == WriteData(socket, data)){ |
||
329 | return -1; |
||
330 | } |
||
331 | |||
332 | return 0; |
||
333 | } |
||
334 | //------------------------------------------------------------------------------------------------------ |
||
335 | int PreparePSC(int CurrentCh, int CurrentProbeType) |
||
336 | { |
||
337 | enum Asign{fs_all, ssh_16, ssh_0, pa_16, pa_0, sizeAsign}; |
||
338 | enum DataName{fs, ssh_hg, ssh_lg, pa_hg, pa_lg, sizeDataName}; |
||
339 | enum DataName CurrentProbe; |
||
340 | CurrentProbe = fs; |
||
341 | |||
342 | static unsigned int ProbeBuffer = 0; |
||
343 | int flag_rst = 0; |
||
344 | int flag_input = 1; |
||
345 | for(int i = 0; i<sizeByte_PSC; ++i){ |
||
346 | Probedata[i] = 0; |
||
347 | } |
||
348 | |||
349 | switch(CurrentProbeType){ |
||
350 | case '1': |
||
351 | CurrentProbe = pa_hg; |
||
352 | break; |
||
353 | |||
354 | case '2': |
||
355 | CurrentProbe = pa_lg; |
||
356 | break; |
||
357 | |||
358 | case '3': |
||
359 | CurrentProbe = ssh_hg; |
||
360 | break; |
||
361 | |||
362 | case '4': |
||
363 | CurrentProbe = ssh_lg; |
||
364 | break; |
||
365 | |||
366 | case '5': |
||
367 | CurrentProbe = fs; |
||
368 | break; |
||
369 | |||
370 | case '6': |
||
371 | |||
372 | break; |
||
373 | |||
374 | case '7': |
||
375 | |||
376 | break; |
||
377 | |||
378 | case '8': |
||
379 | flag_rst = 1; |
||
380 | CurrentCh = 0; |
||
381 | ProbeBuffer = 0; |
||
382 | CurrentProbe = fs; |
||
383 | break; |
||
384 | |||
385 | default: |
||
386 | break; |
||
387 | }; |
||
388 | |||
389 | if(flag_rst) { |
||
390 | return 0; |
||
391 | } |
||
392 | |||
393 | if(flag_input) { |
||
394 | ProbeBuffer = 1 << (31 - CurrentCh); |
||
395 | } |
||
396 | |||
397 | unsigned int buffer_16 = ProbeBuffer*ProbeBuffer; |
||
398 | unsigned int buffer_0 = (ProbeBuffer >> 16)*(ProbeBuffer >> 16); |
||
399 | if(CurrentProbe == ssh_hg || CurrentProbe == pa_hg) { |
||
400 | buffer_16 = buffer_16 << 1; |
||
401 | buffer_0 = buffer_0 << 1; |
||
402 | } |
||
403 | if(CurrentProbe == fs) { |
||
404 | Probedata[fs_all] = ProbeBuffer; |
||
405 | } else if(CurrentProbe == ssh_hg || CurrentProbe == ssh_lg) { |
||
406 | if(CurrentCh > 15) { |
||
407 | Probedata[ssh_16] = buffer_16; |
||
408 | } else { |
||
409 | Probedata[ssh_0] = buffer_0; |
||
410 | } |
||
411 | } else if(CurrentProbe == pa_hg || CurrentProbe == pa_lg) { |
||
412 | if(CurrentCh > 15){ |
||
413 | Probedata[pa_16] = buffer_16; |
||
414 | }else{ |
||
415 | Probedata[pa_0] = buffer_0; |
||
416 | } |
||
417 | } else { |
||
418 | printf("bug desu\n"); |
||
419 | } |
||
420 | |||
421 | for(int shift = 0; shift<32; ++shift) { |
||
422 | if(((Probedata[fs_all]) >> shift) & 1) { |
||
423 | printf("!"); |
||
424 | }else{ |
||
425 | printf("."); |
||
426 | } |
||
427 | } |
||
428 | printf("\n"); |
||
429 | |||
430 | for(int index = ssh_16; index <= ssh_0; ++index) { |
||
431 | for(int shift = 0; shift<32; ++shift) { |
||
432 | if(((Probedata[index]) >> shift) & 1) { |
||
433 | printf("!"); |
||
434 | } else { |
||
435 | printf("."); |
||
436 | } |
||
437 | } |
||
438 | } |
||
439 | printf("\n"); |
||
440 | |||
441 | for(int index = pa_16; index <= pa_0; ++index) { |
||
442 | for(int shift = 0; shift<32; ++shift) { |
||
443 | if(((Probedata[index]) >> shift) & 1) { |
||
444 | printf("!"); |
||
445 | }else{ |
||
446 | printf("."); |
||
447 | } |
||
448 | } |
||
449 | } |
||
450 | printf("\n"); |
||
451 | |||
452 | return 0; |
||
453 | } |
||
454 | //------------------------------------------------------------------------------------------------------ |
||
455 | int TransmitProbe(SOCKET socket) |
||
456 | { |
||
457 | unsigned int data = 0; |
||
458 | //Set Probe mode -------------------------------------------------------- |
||
459 | data += 1 << 16; |
||
460 | data += 208 << 8; |
||
461 | if(-1 == WriteData(socket, data)) { |
||
462 | return -1; |
||
463 | } |
||
464 | |||
465 | //Probe start ---------------------------------------------------------- |
||
466 | for(int i = 0; i<sizeByte_PSC; ++i) { |
||
467 | for(int shift = 0; shift<4; ++shift) { |
||
468 | data = 0; |
||
469 | data += 10 << 16; |
||
470 | data += ((Probedata[i] >> 8*shift) & 255) << 8; |
||
471 | if(-1 == WriteData(socket, data)) { |
||
472 | return -1; |
||
473 | } |
||
474 | } |
||
475 | } |
||
476 | |||
477 | //StartCycle ------------------------------------------------------------ |
||
478 | data = 0; |
||
479 | data += 1 << 16; |
||
480 | data += 210 << 8; |
||
481 | if(-1 == WriteData(socket, data)) { |
||
482 | return -1; |
||
483 | } |
||
484 | |||
485 | data = 0; |
||
486 | data += 1 << 16; |
||
487 | data += 208 << 8; |
||
488 | if(-1 == WriteData(socket, data)) { |
||
489 | return -1; |
||
490 | } |
||
491 | |||
492 | return 0; |
||
493 | } |
||
494 | //------------------------------------------------------------------------------------------------------ |
||
495 | int easiroc_fTransmitSC() |
||
496 | { |
||
497 | if(WriteData(sock, 37888) != 0) return -1; |
||
498 | PrepareSC(1); |
||
499 | if(TransmitSC(sock) != 0) return -1; |
||
500 | printf("\nSlow Control chip1 : Done\n\n"); |
||
501 | |||
502 | if(WriteData(sock, 21504) != 0) return -1; |
||
503 | PrepareSC(2); |
||
504 | if(TransmitSC(sock) != 0) return -1; |
||
505 | printf("\nSlow Control chip2 : Done\n\n"); |
||
506 | |||
507 | if(WriteData(sock, 5120) != 0) return -1; |
||
508 | |||
509 | return 0; |
||
510 | } |
||
511 | //------------------------------------------------------------------------------------------------------ |
||
512 | int easiroc_fTransmitReadSC() |
||
513 | { |
||
514 | if(WriteData(sock, 37888) != 0) return -1; |
||
515 | PrepareReadSC(1); |
||
516 | if(TransmitReadSC(sock) != 0) return -1; |
||
517 | printf("\nRead Slow Control chip1 : Done\n\n"); |
||
518 | |||
519 | if(WriteData(sock, 21504) != 0) return -1; |
||
520 | PrepareReadSC(2); |
||
521 | if(TransmitReadSC(sock) != 0) return -1; |
||
522 | printf("\nRead Slow Control chip2 : Done\n\n"); |
||
523 | |||
524 | if(WriteData(sock, 5120) != 0) return -1; |
||
525 | |||
526 | return 0; |
||
527 | } |
||
528 | //------------------------------------------------------------------------------------------------------ |
||
529 | int easiroc_fAsicInitialize() |
||
530 | { |
||
531 | PrepareFPGA(); |
||
532 | DebugFPGA(sock); |
||
533 | printf("\nASIS Initialize : Done\n\n"); |
||
534 | |||
535 | return 0; |
||
536 | } |
||
537 | //------------------------------------------------------------------------------------------------------ |
||
538 | int easiroc_fTransmitProbe(int CurrentCh, int CurrentProbe) |
||
539 | { |
||
540 | //printf("\neasiroc_fTransmitProbe: CurrentCh : %d | CurrentProbe: %d\n\n", CurrentCh, CurrentProbe); |
||
541 | |||
542 | if(CurrentCh < 32) { |
||
543 | if(WriteData(sock, 37888)!= 0) return -1; |
||
544 | } else if(CurrentCh > 31) { |
||
545 | if(WriteData(sock, 21504)!= 0) return -1; |
||
546 | CurrentCh = CurrentCh - 32; |
||
547 | } |
||
548 | |||
549 | //printf("\neasiroc_fTransmitProbe: Preparing PSC\n\n"); |
||
550 | if(PreparePSC(CurrentCh, CurrentProbe)!= 0) { |
||
551 | printf("\neasiroc_fTransmitProbe: PreparingPSC ERROR !!!!!!!!!!!!!!!!!!!\n\n"); |
||
552 | return -1; |
||
553 | } |
||
554 | //printf("\neasiroc_fTransmitProbe: TransmitProbe\n\n"); |
||
555 | if(TransmitProbe(sock)!= 0) { |
||
556 | printf("\neasiroc_fTransmitProbe: TransmitProbe ERROR !!!!!!!!!!!!!!!!!!!\n\n"); |
||
557 | return -1; |
||
558 | } |
||
559 | //WriteData(3, 5120); |
||
560 | //printf("\nTransmitProbe: Success!\n\n"); |
||
561 | |||
562 | return 0; |
||
563 | } |
||
564 | //------------------------------------------------------------------------------------------------------ |
||
565 | int ADCOneCycle_wHeader_ver2(SOCKET socket, FILE* file) |
||
566 | { |
||
567 | static const int NofHeader = 3; |
||
568 | |||
569 | unsigned int DataBuffer[1000]; |
||
570 | memset(DataBuffer, 0, sizeof(DataBuffer)); |
||
571 | unsigned int *ptr_DataBuf = DataBuffer; |
||
572 | unsigned int TotalRecvByte = 0; |
||
573 | unsigned int TotalRecvEvent = 0; |
||
574 | |||
575 | unsigned int sizeHeader = NofHeader*sizeof(int); |
||
576 | |||
577 | unsigned int Header[] = {1, 1, 1}; |
||
578 | int ret = 0; |
||
579 | |||
580 | ret = recv(sock, (char*)Header, sizeHeader, 0); |
||
581 | if(ret <= 0 && EndADC == 1) { |
||
582 | if(EndADC != 0) printf("Fatal ADC ERROR : recv\n"); |
||
583 | return -1; |
||
584 | } |
||
585 | |||
586 | // sleep necessary! ERRORS happen if trigger rate lower than ~ sleep frequency! |
||
587 | usleep(200); |
||
588 | //Sleep(1); |
||
589 | |||
590 | //printf("Header1 (0xFFFFEA0C) : 0x%x | Header2 (0x00000100) : 0x%x\n", Header[0], Header[1]); |
||
591 | if(Header[0] != 0xFFFFEA0C) { |
||
592 | printf("Fatal ADC ERROR : HEADER\n"); |
||
593 | printf("Header1 : 0x%x | Header2 : 0x%x | Header3 : 0x%x\n", Header[0], Header[1], Header[2]); |
||
594 | return -1; |
||
595 | } else { |
||
596 | unsigned int sizeData = (sizeof(int)*Header[1] & 0xffff); |
||
597 | unsigned int NofWord = (Header[1] & 0xffff); |
||
598 | unsigned int OneData[NofWord]; |
||
599 | |||
600 | recv(socket, (char*)OneData, sizeData, 0); |
||
601 | |||
602 | // for(int i = 0; i<NofWord; ++i){ std::cout << std::hex << OneData[i] << std::endl; } |
||
603 | |||
604 | memcpy(ptr_DataBuf, Header, sizeHeader); |
||
605 | ptr_DataBuf += NofHeader; |
||
606 | |||
607 | memcpy(ptr_DataBuf, OneData, sizeData); |
||
608 | ptr_DataBuf += sizeData/sizeof(int); |
||
609 | |||
610 | TotalRecvByte += sizeHeader + sizeData; |
||
611 | ++TotalRecvEvent; |
||
612 | } |
||
613 | |||
614 | if(EndADC != 1){ |
||
615 | for(unsigned int i = 0; i<TotalRecvByte/sizeof(int); ++i) { |
||
616 | unsigned int buffer = DataBuffer[i]; |
||
617 | fwrite((char*)&buffer, sizeof(int), 1, file); |
||
618 | } |
||
619 | } |
||
620 | |||
621 | return 0; |
||
622 | } |
||
623 | //------------------------------------------------------------------------------------------------------ |
||
624 | void ADCStop(SOCKET socket) |
||
625 | { |
||
626 | unsigned int signal = 0; |
||
627 | printf("ADC exit process\n"); |
||
628 | signal += 16 << 24; |
||
629 | signal += 100 << 16; |
||
630 | send(socket, (char*)&signal, sizeof(int), 0); |
||
631 | Sleep(1); |
||
632 | |||
633 | signal = 0; |
||
634 | signal += 100 << 16; |
||
635 | send(socket, (char*)&signal, sizeof(int), 0); |
||
636 | |||
637 | Sleep(10); |
||
638 | |||
639 | return; |
||
640 | } |
||
641 | //------------------------------------------------------------------------------------------------------ |
||
642 | int ContinuousADC_ver2(SOCKET socket, char *file_name, int MaxCycleNum) |
||
643 | { |
||
644 | |||
645 | unsigned int signal = 0; |
||
646 | int EventNum = 0; |
||
647 | |||
648 | FILE *file = NULL; |
||
649 | file = fopen(file_name, "wb"); |
||
650 | if(file==NULL) { |
||
651 | printf("ContinuousADC_ver2: ERROR opneing file %s\n", file_name); |
||
652 | return -1; |
||
653 | } |
||
654 | |||
655 | signal += 32 << 24; |
||
656 | signal += 100 << 16; |
||
657 | send(socket, (char*)&signal, sizeof(int), 0); |
||
658 | Sleep(100); |
||
659 | |||
660 | while(EventNum < MaxCycleNum){ |
||
661 | //Sleep(10); |
||
662 | ADCOneCycle_wHeader_ver2(socket, file); |
||
663 | ++EventNum; |
||
664 | if(0 == EventNum%1000){ |
||
665 | printf("Event # %d\n", EventNum); |
||
666 | } |
||
667 | |||
668 | if(0 || EventNum == MaxCycleNum|| ForceStop == 1) { |
||
669 | ADCStop(socket); |
||
670 | |||
671 | EndADC = 1; |
||
672 | int abort = 0; |
||
673 | while(0 == ADCOneCycle_wHeader_ver2(socket, file)) { |
||
674 | Sleep(1); |
||
675 | if(abort == 50) { |
||
676 | ADCStop(socket); |
||
677 | abort = 0; |
||
678 | } |
||
679 | //printf("dummy data\n"); |
||
680 | printf("."); |
||
681 | ++abort; |
||
682 | } |
||
683 | |||
684 | EndADC = 0; |
||
685 | ForceStop = 0; |
||
686 | break; |
||
687 | } |
||
688 | } |
||
689 | |||
690 | printf("\nEnd ADC\n"); |
||
691 | fclose(file); |
||
692 | |||
693 | return 0; |
||
694 | } |
||
695 | //------------------------------------------------------------------------------------------------------ |
||
696 | int easiroc_fDAQ(char *file_name, int MaxCycleNum) |
||
697 | { |
||
698 | printf("easiroc_fDAQ: Starting DAQ with file_name = %s | MaxCycleNum = %d\n", file_name, MaxCycleNum); |
||
699 | if(ContinuousADC_ver2(sock, file_name, MaxCycleNum) != 0) return -1; |
||
700 | |||
701 | return 0; |
||
702 | } |
||
703 | |||
704 | |||
705 |