Subversion Repositories f9daq

Rev

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
 
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
 
12
 
13
 
14
//------------------------------------------------------------------------------------------------------ 
15
int easiroc_Init(const char* SiTCP_MASTER_IP, unsigned int daq_mode)
16
{
17
        // Start WinSock
18
        printf("easiroc_Init --> Start.\n");
19
        WORD wVersionRequested;
20
    WSADATA wsaData;
21
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
22
        printf("WSAStartup failed with error!\n");
23
    }
24
 
25
        //Initialize -------------------------------------------------------------
26
 
27
        SOCKET sock = INVALID_SOCKET;
28
        struct sockaddr_in SiTCP_ADDR;
29
        const char*  IP;
30
        unsigned int port;
31
 
32
        sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
33
        if(sock  == INVALID_SOCKET) {  
34
                printf("easiroc_Init::sock = %d\n", sock);
35
                return -1;
36
        }
37
        IP                         = SiTCP_MASTER_IP;
38
        port                       = SiTCP_PORT;
39
        SiTCP_ADDR.sin_family      = AF_INET;
40
        SiTCP_ADDR.sin_port        = htons(port);
41
        SiTCP_ADDR.sin_addr.s_addr = inet_addr(IP);
42
 
43
        struct timeval tv;
44
        tv.tv_sec  = 3;
45
        tv.tv_usec = 0;
46
        setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char*)&tv, sizeof(tv));
47
 
48
        int flag = 1;
49
        setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char*)&flag, sizeof(flag));
50
 
51
        //Connection -------------------------------------------------------------
52
        int con = connect(sock, (struct sockaddr*)&SiTCP_ADDR, sizeof(SiTCP_ADDR));
53
        printf("easiroc_Init::connect = %d\n", con);
54
        if(0 > con){
55
                printf("SiTCP Master :: Connection fail\n");
56
                closesocket(sock);
57
                return -1;
58
        }
59
 
60
        printf("SiTCP Master :: Connection Done\n\n");
61
 
62
        // Auto Initialize -------------------------------------------------------
63
        PrepareFPGA();
64
        DebugFPGA(sock);
65
        printf("\nASIS Initialize : Done\n\n");
66
//      Sleep(10000);
67
 
68
        WriteData(sock, 37888);
69
        PrepareSC(1);
70
        TransmitSC(sock);
71
        printf("\nSlow Control chip1 : Done\n\n");
72
        PrepareReadSC(1);
73
        TransmitReadSC(sock);
74
        printf("\nRead Slow Control chip1 : Done\n\n");  
75
 
76
        WriteData(sock, 21504);
77
        PrepareSC(2);
78
        TransmitSC(sock);
79
        printf("\nSlow Control chip2 : Done\n\n");
80
        PrepareReadSC(2);
81
        TransmitReadSC(sock);
82
        printf("\nRead Slow Control chip2: Done\n\n");
83
 
84
        WriteData(sock, 5120);
85
        {
86
                unsigned int signal = 31;
87
                signal = signal << 16;
88
                unsigned int data = daq_mode;
89
                data = data << 8;
90
                signal += data;
91
                if(-1 == WriteData(sock, signal)){
92
                        return -1;
93
                }
94
                printf("\n#D : DAQ mode is %d \n", daq_mode);
95
        }
96
 
97
 
98
        return 0;
99
}
100
//------------------------------------------------------------------------------------------------------ 
101
void easiroc_Close()
102
{
103
        WSACleanup();
104
        printf("easiroc_Close -> Done.\n");
105
}
106
//------------------------------------------------------------------------------------------------------ 
107
int  WriteData(SOCKET sock, unsigned int data)
108
{
109
        int esrcdbg = 0;
110
 
111
        Sleep(SOCK_DELAY);
112
 
113
        data += 128 << 24;;
114
 
115
        if(esrcdbg) printf("0x%X\n", data);
116
        send(sock, (char*)&data, sizeof(int), 0);
117
 
118
        Sleep(SOCK_DELAY);
119
 
120
        unsigned int buf = 0;
121
        unsigned int length = 4;
122
        int ret = recv(sock, (char*)&buf, length, 0);
123
 
124
        if ( ret > 0 ) {
125
                if(esrcdbg) printf("Bytes received: %d\n", ret);
126
                else printf(".");
127
        } else if ( ret == 0 ) {
128
                printf("Connection closed\n");
129
                return -1;
130
        } else {
131
                printf("recv failed: %d\n", WSAGetLastError());
132
                return -1;
133
        }
134
 
135
        return 0;
136
}
137
//------------------------------------------------------------------------------------------------------
138
void PrepareFPGA()
139
{
140
        Pindata[0] = 20;
141
        Pindata[1] = 65776;    
142
}
143
//------------------------------------------------------------------------------------------------------
144
void PrepareSC(int chipNo)
145
{
146
        if(chipNo == 1) {
147
                slowdata[0] = 192;
148
                slowdata[1] = 1720319;
149
                slowdata[2] = 4253040640;
150
                slowdata[3] = 413;
151
                slowdata[4] = 0;
152
                slowdata[5] = 3739423232;
153
                slowdata[6] = 4294967295;
154
                slowdata[7] = 4294967295;
155
                slowdata[8] = 4294967295;
156
                slowdata[9] = 4294967295;
157
                slowdata[10] = 4294967295;
158
                slowdata[11] = 4294967295;
159
                slowdata[12] = 4294967295;
160
                slowdata[13] = 4294967295;
161
                slowdata[14] = 4294967295;
162
        } else {
163
                slowdata[0] = 192;
164
                slowdata[1] = 4292874239;
165
                slowdata[2] = 4257218559;
166
                slowdata[3] = 478;
167
                slowdata[4] = 0;
168
                slowdata[5] = 3740340736;
169
                slowdata[6] = 1175693740;
170
                slowdata[7] = 2815957775;
171
                slowdata[8] = 766945147;
172
                slowdata[9] = 1504433946;
173
                slowdata[10] = 2729016485;
174
                slowdata[11] = 3412554245;
175
                slowdata[12] = 2725848728;
176
                slowdata[13] = 2267165112;
177
                slowdata[14] = 4291041547;
178
        }
179
}
180
//------------------------------------------------------------------------------------------------------ 
181
void PrepareReadSC(int chipNo)
182
{
183
        if(chipNo == 1) {      
184
                ReadSCdata[0] = 0;     
185
        } else {
186
                ReadSCdata[0] = 1;
187
        }
188
}
189
//------------------------------------------------------------------------------------------------------ 
190
int DebugFPGA(SOCKET socket)
191
{
192
        unsigned int buffer = 0;
193
        buffer += 0 << 16;
194
        buffer += (Pindata[0] & 255) << 8;
195
        if(-1 == WriteData(socket, buffer)){
196
                return -1;
197
        }
198
 
199
        for(int i = 1 ; i<5; ++i){
200
                buffer = 0;
201
                if(i == 4){
202
                        buffer += 5 << 16;      
203
                }else{
204
                        buffer += i << 16;
205
                }
206
                buffer += ((Pindata[1] >> (i-1)*8) & 255) << 8;
207
 
208
                if(-1 == WriteData(socket, buffer)){
209
                        return -1;
210
                }
211
                Sleep(1);
212
        }
213
 
214
        return 0;
215
}
216
//------------------------------------------------------------------------------------------------------ 
217
int TransmitSC(SOCKET socket){
218
  unsigned int data = 0;
219
  //Set SC mode -----------------------------------------------------------
220
  data = 0;
221
  data += 1 << 16;
222
  data += 240 << 8;
223
  if(-1 == WriteData(socket, data)){
224
    return -1;
225
  }
226
 
227
  //SC start -------------------------------------------------------------
228
  data = 0;
229
  data += 10 << 16;
230
  data += (slowdata[0] & 255) << 8;
231
  if(-1 == WriteData(socket, data)){
232
    return -1;    
233
  }
234
 
235
  for(int i = 1; i<15; ++i){
236
    for(int shift = 0; shift<4; ++shift){
237
      data = 0;
238
      data += 10 << 16;
239
      data += ((slowdata[i] >> 8*shift) & 255) << 8;
240
      if(-1 == WriteData(socket, data)){
241
                return -1;    
242
      }
243
      //std::cout<<"test"<<std::endl;
244
      Sleep(1);
245
    }
246
  }
247
 
248
  // Sleep(50000);
249
 
250
  //StartCycle -----------------------------------------------------------
251
  data = 0;
252
  data += 1 << 16;
253
  data += 242 << 8;
254
  if(-1 == WriteData(socket, data)){
255
    return -1;    
256
  }
257
 
258
  data = 0;
259
  data += 1 << 16;
260
  data += 240 << 8;
261
  if(-1 == WriteData(socket, data)){
262
    return -1;    
263
  }
264
 
265
  // Sleep(50000);
266
 
267
  //Load SC --------------------------------------------------------------
268
  data = 0;
269
  data += 1 << 16;
270
  data += 241 << 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
  return 0;
283
}
284
//------------------------------------------------------------------------------------------------------ 
285
int TransmitReadSC(SOCKET socket)
286
{
287
  //SCA read ---------------------------------------------------------------
288
  unsigned int data = 0;
289
 
290
  for(int i = 0; i<4; ++i){
291
    data = 0;
292
    data += 12 << 16;
293
    data += ((ReadSCdata[0] >> i*8) & 255) << 8;
294
    if(-1 == WriteData(socket, data)){
295
      return -1;
296
    }
297
    Sleep(1);
298
  }
299
 
300
  //StartCycle ------------------------------------------------------------
301
  data = 0;
302
  data += 1 << 16;
303
  data += 242 << 8;
304
  if(-1 == WriteData(socket, data)){
305
    return -1;
306
  }
307
 
308
  data = 0;
309
  data += 1 << 16;
310
  data += 240 << 8;
311
  if(-1 == WriteData(socket, data)){
312
    return -1;
313
  }
314
 
315
  return 0;
316
}
317
 
318
//------------------------------------------------------------------------------------------------------ 
319
 
320
/*
321
 
322
#include"src/easiroc.h"
323
#include"src/SC.hh"
324
 
325
#include<fstream>
326
#include<ios>
327
 
328
static const int  SiTCP_PORT       = 24;
329
 
330
extern unsigned int slowdata[sizeByte_SC];
331
extern unsigned int ReadSCdata[sizeByte_RSC];
332
extern unsigned int Probedata[sizeByte_PSC];
333
extern unsigned int Pindata[sizeByte_PIN];
334
 
335
int ForceStop = 0;
336
int EndADC = 0;
337
 
338
void ForceStop_FromCtrl_C(int signal);
339
int  receive(int sock, char* data_buf, unsigned int *ReadLength);
340
int  WriteData(int socket, unsigned int data);
341
int  ReadData(int socket, unsigned int signal, unsigned int data);
342
void ADCStop(int socket);
343
 
344
int DebugFPGA(int socket);
345
int TransmitSC(int socket);
346
int TransmitReadSC(int socket);
347
int TransmitProbe(int socket);
348
int ContinuousADC(int socket);
349
int ContinuousADC_ver2(int socket);
350
void ADCOneCycle(int socket, std::ofstream& file);
351
void ADCOneCycle_wHeader(int socket, std::ofstream& file);
352
int ADCOneCycle_wHeader_ver2(int socket, std::ofstream& file);
353
void MHTDC_debug(int socket);
354
int Debugging(int socket);
355
int chipselect();
356
 
357
int main(int argc, char* argv[]){
358
  if(1 == argc){
359
    std::cout << "Usage\n";
360
    std::cout << "./easiroc [IP Address of SOY] [DAQ mode]" << std::e\
361
ndl;
362
    std::cout << "   DAQ mode : 1 MHTDC\n";
363
    std::cout << "            : 2 ADC\n";
364
    std::cout << "            : 3 ADC & MHTDC" << std::endl;
365
    exit(-1);
366
  }
367
 
368
  unsigned int daq_mode = (unsigned int)atoi(argv[2]);
369
 
370
  const char* SiTCP_MASTER_IP = argv[1];
371
 
372
  int sock = 0;
373
  struct sockaddr_in SiTCP_ADDR;
374
  const char*  IP;
375
  unsigned int port;
376
 
377
  //Initialize -------------------------------------------------------------
378
  ForceStop  = 0;
379
 (void) signal(SIGINT, ForceStop_FromCtrl_C);
380
 
381
  sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
382
  IP                         = SiTCP_MASTER_IP;
383
  port                       = SiTCP_PORT;
384
  SiTCP_ADDR.sin_family      = AF_INET;
385
  SiTCP_ADDR.sin_port        = htons(port);
386
  SiTCP_ADDR.sin_addr.s_addr = inet_addr(IP);
387
 
388
  struct timeval tv;
389
  tv.tv_sec  = 3;
390
  tv.tv_usec = 0;
391
  setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char*)&tv, sizeof(tv));
392
 
393
  int flag = 1;
394
  setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char*)&flag, sizeof(flag));
395
 
396
  //Connection -------------------------------------------------------------
397
  if(0 > connect(sock, (struct sockaddr*)&SiTCP_ADDR, sizeof(SiTCP_ADDR))){
398
    std::cerr << "SiTCP Master :: Connection fail" << std::endl;
399
    close(sock);
400
    return -1;
401
  }
402
 
403
  std::cout << "SiTCP Master :: Connection Done" << std::endl;
404
  std::cout << std::endl;
405
 
406
  // Auto Initialize -------------------------------------------------------
407
  PrepareFPGA();
408
  DebugFPGA(sock);
409
  std::cout << "ASIS Initialize : Done" << std::endl;
410
  std::cout << std::endl;
411
  usleep(10000);
412
 
413
  WriteData(3, 37888);
414
  PrepareSC(1);
415
  TransmitSC(sock);
416
  std::cout << "Slow Control chip1 : Done" << std::endl;
417
  std::cout << std::endl;
418
  PrepareReadSC(1);
419
  TransmitReadSC(sock);
420
  std::cout << "Read Slow Control chip1 : Done" << std::endl;
421
  std::cout << std::endl;
422
 
423
  WriteData(3, 21504);
424
  PrepareSC(2);
425
  TransmitSC(sock);
426
  std::cout << "Slow Control chip2 : Done" << std::endl;
427
  std::cout << std::endl;
428
  PrepareReadSC(2);
429
  TransmitReadSC(sock);
430
  std::cout << "Read Slow Control chip2: Done" << std::endl;
431
  std::cout << std::endl;
432
 
433
  WriteData(3, 5120);
434
  {
435
    unsigned int signal = 31;
436
    signal = signal << 16;
437
    unsigned int data = daq_mode;
438
    data = data << 8;
439
    signal += data;
440
    if(-1 == WriteData(sock, signal)){
441
      exit(-1);
442
    }
443
    std::cout << "#D : DAQ mode is " << daq_mode << std::endl;
444
  }
445
 
446
 
447
  // User area -----------------------------------------------------------
448
  bool DispMenu = true;
449
  int CurrentCh = 0;
450
  while(DispMenu){
451
    for(int i = 0; i<3; ++i){
452
      std::cout << std::endl;
453
    }
454
 
455
    std::cout << "I'm : " << SiTCP_MASTER_IP  << std::endl;
456
    std::cout << "Please select" << std::endl;
457
    std::cout << " 1. Transmit SC" << std::endl;
458
    std::cout << " 2. Transmit Read SC" << std::endl;
459
    std::cout << " 3. ASIC Initialize" << std::endl;
460
    std::cout << " 4. Transmit Probe " << std::endl;
461
    std::cout << " 5. Connection Close" << std::endl;
462
    std::cout << " 6. Start DAQ" << std::endl;
463
    std::cout << " 7. Debug" << std::endl;
464
    std::cout << "input # ====> " << std::flush;
465
    char buf[10];
466
    fgets(buf, sizeof(buf),stdin);
467
    char num;
468
    sscanf(buf,"%c",&num);
469
 
470
    switch(num){
471
    case '1':
472
      WriteData(3, 37888);
473
      PrepareSC(1);
474
      TransmitSC(sock);
475
      WriteData(3, 21504);
476
      PrepareSC(2);
477
      TransmitSC(sock);
478
      WriteData(3, 5120);
479
      break;
480
 
481
    case '2':
482
      WriteData(3, 37888);
483
      PrepareReadSC(1);
484
      TransmitReadSC(sock);
485
      WriteData(3, 21504);
486
      PrepareReadSC(2);
487
      TransmitReadSC(sock);
488
      WriteData(3, 5120);
489
      break;
490
 
491
    case '3':
492
      PrepareFPGA();
493
      DebugFPGA(sock);
494
      break;
495
 
496
    case '4':
497
      std::cout << "Input channel number ====> " << std::endl;
498
      std::cin >> CurrentCh;
499
      fgets(&buf[0],sizeof(buf),stdin);           //Enter buffer clear
500
      if(CurrentCh < 32){
501
        WriteData(3, 37888);
502
      }      
503
      else if(CurrentCh > 31){
504
        WriteData(3, 21504);
505
        CurrentCh = CurrentCh - 32;
506
      }
507
      std::cout<<CurrentCh<<std::endl;
508
      PreparePSC(CurrentCh);
509
      TransmitProbe(sock);
510
      //WriteData(3, 5120);
511
      break;
512
 
513
    case '5':
514
      DispMenu = false;
515
      break;
516
 
517
    case '6':
518
      ContinuousADC_ver2(sock);
519
      break;
520
 
521
    case '7':
522
      Debugging(sock);
523
      break;
524
 
525
    case '8':
526
      MHTDC_debug(sock);
527
      break;
528
 
529
    case '9':
530
      chipselect();
531
      break;
532
 
533
    default:
534
      break;
535
    };
536
  }
537
 
538
  //End Connection ---------------------------------------------------------
539
  close(sock);
540
  std::cout << "All :: Connection Close" << std::endl;
541
 
542
  return 0;
543
}
544
 
545
void ForceStop_FromCtrl_C(int signal){
546
  std::cout << "Stop request from user" << std::endl;
547
  ForceStop = 1;
548
  EndADC = 1;
549
}
550
 
551
int receive(int sock, char* data_buf, unsigned int *ReadLength){
552
  unsigned int revd_size     = 0;
553
  int tmp_returnVal = 0;
554
 
555
  while(revd_size < *ReadLength){
556
    tmp_returnVal = recv(sock, data_buf +revd_size, *ReadLength -revd_size, 0);
557
    if(tmp_returnVal < 0){
558
      int errbuf = errno;
559
      perror("TCP receive");
560
      if(errbuf == EAGAIN){
561
        if(EndADC == 1){
562
          std::cout << "No data" << std::endl;
563
          break;
564
        }
565
        continue;
566
      }else if(errbuf == EINTR){
567
        continue;
568
      }else{
569
        exit(-1);
570
      }
571
    }
572
    revd_size += tmp_returnVal;
573
  }
574
 
575
    //    std::cout << "ret is " << std::dec << revd_size << std::endl;
576
  return revd_size;
577
}
578
 
579
int WriteData(int sock, unsigned int data){
580
  data += 128 << 24;
581
//   for(int i = 31; i>-1; --i){
582
//     unsigned int hoge = (data >> i) & 1;
583
//     if(hoge){
584
//       fprintf(stdout, "!");
585
//     }else{
586
//       fprintf(stdout, ".");
587
//     }
588
//     if(i%8 == 0){
589
//       fprintf(stdout, "\n");
590
//     }
591
//   }
592
//   fprintf(stdout, "\n");
593
 
594
//  std::cout << std::hex << data << std::endl;
595
  send(sock, (char*)&data, sizeof(int), 0);
596
  unsigned int buf = 0;
597
  unsigned int length = 4;
598
  receive(sock, (char*)&buf, &length);
599
  if(data - buf){
600
    std::cerr << "### Data Transmit Error ###" << std::endl;
601
    std::cerr << "Transmited data is " << std::hex << data << std::endl;
602
    std::cout << "Returned data is   " << std::hex << buf << std::endl;
603
    return -1;
604
  }
605
 
606
  return 0;
607
}
608
 
609
int ReadData(int sock, unsigned int signal, unsigned int *data){
610
  signal += 64 << 24;
611
  send(sock, (char*)&signal, sizeof(int), 0);
612
  unsigned int length = 4;
613
  receive(sock, (char*)data, &length);
614
 
615
//    for(int i = 31; i>-1; --i){
616
//      unsigned int hoge = (*data >> i) & 1;
617
//      if(hoge){
618
//        fprintf(stdout, "!");
619
//      }else{
620
//        fprintf(stdout, ".");
621
//      }
622
//      if(i%8 == 0){
623
//        fprintf(stdout, "\n");
624
//      }
625
//    }
626
//    fprintf(stdout, "\n");
627
 
628
  return 0;
629
}
630
 
631
int DebugFPGA(int socket){
632
  unsigned int buffer = 0;
633
  buffer += 0 << 16;
634
  buffer += (Pindata[0] & 255) << 8;
635
  if(-1 == WriteData(socket, buffer)){
636
    exit(-1);
637
  }
638
 
639
  for(int i = 1 ; i<5; ++i){
640
    buffer = 0;
641
    if(i == 4){
642
      buffer += 5 << 16;      
643
    }else{
644
      buffer += i << 16;
645
    }
646
    buffer += ((Pindata[1] >> (i-1)*8) & 255) << 8;
647
 
648
    if(-1 == WriteData(socket, buffer)){
649
      exit(-1);
650
    }
651
    usleep(1);
652
  }
653
 
654
  return 0;
655
}
656
 
657
int TransmitSC(int socket){
658
  unsigned int data = 0;
659
  //Set SC mode -----------------------------------------------------------
660
  data = 0;
661
  data += 1 << 16;
662
  data += 240 << 8;
663
  if(-1 == WriteData(socket, data)){
664
    exit(-1);
665
  }
666
 
667
  //SC start -------------------------------------------------------------
668
  data = 0;
669
  data += 10 << 16;
670
  data += (slowdata[0] & 255) << 8;
671
  if(-1 == WriteData(socket, data)){
672
    exit(-1);
673
  }
674
 
675
  for(int i = 1; i<15; ++i){
676
    for(int shift = 0; shift<4; ++shift){
677
      data = 0;
678
      data += 10 << 16;
679
      data += ((slowdata[i] >> 8*shift) & 255) << 8;
680
      if(-1 == WriteData(socket, data)){
681
        exit(-1);
682
      }
683
      //std::cout<<"test"<<std::endl;
684
      usleep(1);
685
    }
686
  }
687
 
688
  // usleep(50000);
689
 
690
  //StartCycle -----------------------------------------------------------
691
  data = 0;
692
  data += 1 << 16;
693
  data += 242 << 8;
694
  if(-1 == WriteData(socket, data)){
695
    exit(-1);
696
  }
697
 
698
  data = 0;
699
  data += 1 << 16;
700
  data += 240 << 8;
701
  if(-1 == WriteData(socket, data)){
702
    exit(-1);
703
  }
704
 
705
  // usleep(50000);
706
 
707
  //Load SC --------------------------------------------------------------
708
  data = 0;
709
  data += 1 << 16;
710
  data += 241 << 8;
711
  if(-1 == WriteData(socket, data)){
712
    exit(-1);
713
  }
714
 
715
  data = 0;
716
  data += 1 << 16;
717
  data += 240 << 8;
718
  if(-1 == WriteData(socket, data)){
719
    exit(-1);
720
  }
721
 
722
  return 0;
723
}
724
 
725
int TransmitReadSC(int socket){
726
  //SCA read ---------------------------------------------------------------
727
  unsigned int data = 0;
728
 
729
  for(int i = 0; i<4; ++i){
730
    data = 0;
731
    data += 12 << 16;
732
    data += ((ReadSCdata[0] >> i*8) & 255) << 8;
733
    if(-1 == WriteData(socket, data)){
734
      exit(-1);
735
    }
736
    usleep(1);
737
  }
738
 
739
  //StartCycle ------------------------------------------------------------
740
  data = 0;
741
  data += 1 << 16;
742
  data += 242 << 8;
743
  if(-1 == WriteData(socket, data)){
744
    exit(-1);
745
  }
746
 
747
  data = 0;
748
  data += 1 << 16;
749
  data += 240 << 8;
750
  if(-1 == WriteData(socket, data)){
751
    exit(-1);
752
  }
753
 
754
  return 0;
755
}
756
 
757
int TransmitProbe(int socket){
758
  unsigned int data = 0;
759
  //Set Probe mode --------------------------------------------------------
760
  data += 1 << 16;
761
  data += 208 << 8;
762
  if(-1 == WriteData(socket, data)){
763
    exit(-1);
764
  }
765
 
766
  //Probe start ----------------------------------------------------------
767
  for(int i = 0; i<sizeByte_PSC; ++i){
768
    for(int shift = 0; shift<4; ++shift){
769
      data = 0;
770
      data += 10 << 16;
771
      data += ((Probedata[i] >> 8*shift) & 255) << 8;
772
      if(-1 == WriteData(socket, data)){
773
        exit(-1);
774
      }
775
      usleep(1);
776
    }
777
  }
778
 
779
  usleep(50000);
780
 
781
  //StartCycle ------------------------------------------------------------
782
  data = 0;
783
  data += 1 << 16;
784
  data += 210 << 8;
785
  if(-1 == WriteData(socket, data)){
786
    exit(-1);
787
  }
788
 
789
  data = 0;
790
  data += 1 << 16;
791
  data += 208 << 8;
792
  if(-1 == WriteData(socket, data)){
793
    exit(-1);
794
  }
795
 
796
  return 0;
797
}
798
 
799
int ContinuousADC(int socket){
800
  std::string file_num;
801
  std::cout << "Input output data file name : " << std::flush;
802
  std::cin >> file_num;
803
  std::string basename = "after_break_ext_";
804
  std::string file_name = basename + file_num + ".dat";
805
  std::ofstream file(file_name.c_str(),std::ios::binary);
806
 
807
  int MaxCycleNum = 0;
808
  std::cout << "Input total # of events" << std::endl;
809
  //  std::cout << "If you type 'N', preset # of events will be N*1000" << std::endl;
810
  std::cout << "======>" << std::flush;
811
  std::cin >> MaxCycleNum;
812
  MaxCycleNum = MaxCycleNum/1000;
813
  std::cout << "Preset value : " << MaxCycleNum*1000 << std::endl;
814
 
815
  for(int i = 0; i<MaxCycleNum; ++i){
816
    //    ADCOneCycle(socket, file);
817
    ADCOneCycle_wHeader(socket, file);
818
    std::cout << "Event # " << 1000*(i+1) << std::endl;
819
  }
820
 
821
  std::cout << "End ADC" << std::endl;
822
  file.close();
823
 
824
  return 0;
825
}
826
 
827
int ContinuousADC_ver2(int socket){
828
  std::string file_name;
829
  std::cout << "Input output data file name : " << std::flush;
830
  std::cin >> file_name;
831
  std::ofstream file(file_name.c_str(),std::ios::binary);
832
 
833
  int MaxCycleNum = 0;
834
  std::cout << "Input total # of events" << std::endl;
835
  std::cout << "======>" << std::flush;
836
  std::cin >> MaxCycleNum;
837
  std::cout << "Preset value : " << std::dec << MaxCycleNum << std::endl;
838
 
839
  unsigned int signal = 0;
840
  int EventNum = 0;
841
 
842
  signal += 32 << 24;
843
  signal += 100 << 16;
844
  send(socket, (char*)&signal, sizeof(int), 0);
845
 
846
  while(EventNum < MaxCycleNum){
847
    ADCOneCycle_wHeader_ver2(socket, file);
848
    ++EventNum;
849
    if(0 == EventNum%1000){
850
      std::cout << "Event # " << std::dec << EventNum << std::endl;
851
    }
852
 
853
    if(false
854
       || EventNum  == MaxCycleNum
855
       || ForceStop == 1
856
       ){
857
      ADCStop(socket);
858
 
859
      EndADC = 1;
860
      int abort = 0;
861
      while(0 == ADCOneCycle_wHeader_ver2(socket, file)){
862
        usleep(10000);
863
        if(abort == 50){
864
          ADCStop(socket);
865
          abort = 0;
866
        }
867
        std::cout << "dummy data" << std::endl;
868
        ++abort;
869
      }
870
 
871
      EndADC    = 0;
872
      ForceStop = 0;
873
      break;
874
    }
875
  }
876
 
877
  std::cout << "End ADC" << std::endl;
878
  file.close();
879
 
880
  return 0;
881
}
882
 
883
void ADCOneCycle(int socket, std::ofstream& file){
884
  static const int TotalEvent = 1000;
885
  unsigned int signal = 0;
886
 
887
  signal += 16 << 24;
888
  signal += 100 << 16;
889
  send(socket, (char*)&signal, sizeof(int), 0);
890
 
891
  unsigned int ADCData[16*TotalEvent];
892
  for(int i = 0; i<16*TotalEvent; ++i){
893
    ADCData[i] = 0;
894
  }
895
  static unsigned int ADCLength  = sizeof(ADCData);
896
  receive(socket, (char*)ADCData, &ADCLength);
897
 
898
 
899
  for(int Event = 0; Event<TotalEvent; ++Event ){
900
    int event = 0xffff;
901
    file.write((char*)&event, sizeof(short));
902
    //    fprintf(stdout, "Event # %d\n",Event);
903
 
904
    for(int i = 0; i<16; ++i){
905
      unsigned int buffer = ADCData[i+16*Event] & 4095;
906
      //      fprintf(stdout, "%d\n",buffer & 15);
907
      file.write((char*)&buffer, sizeof(short));
908
      buffer = (ADCData[i+16*Event] >> 16) & 4095;
909
      //      fprintf(stdout, "%d\n",buffer & 15);
910
      file.write((char*)&buffer, sizeof(short));
911
    }
912
    //    std::cout << std::endl;
913
    //    std::cout << std::endl;
914
  }
915
 
916
  signal = 0;
917
  signal += 100 << 16;
918
  if(-1 == WriteData(socket, signal)){
919
    exit(-1);
920
  }
921
 
922
}
923
 
924
void ADCOneCycle_wHeader(int socket, std::ofstream& file){
925
  static const int TotalEvent = 1000;
926
  unsigned int signal = 0;
927
 
928
  unsigned int DataBuffer[100*TotalEvent];
929
  memset(DataBuffer, 0, sizeof(DataBuffer));
930
  unsigned int *ptr_DataBuf = DataBuffer;
931
  unsigned int TotalRecvByte  = 0;
932
  unsigned int TotalRecvEvent = 0;
933
 
934
  static unsigned int sizeHeader = 2*sizeof(int);
935
 
936
  signal += 16 << 24;
937
  signal += 100 << 16;
938
  send(socket, (char*)&signal, sizeof(int), 0);
939
 
940
  for(int i = 0; i<TotalEvent; ++i){
941
    unsigned int Header[2] = {1, 1};
942
    receive(socket, (char*)Header, &sizeHeader);
943
    //    Header[0] = 0xFFFFEA0C;
944
    //    Header[1] = 0x00000100;
945
    if(Header[0] != 0xFFFFEA0C){
946
      std::cerr << "Fatal ADC ERROR : HEADER" << std::endl;
947
      std::cerr << "Header1 : " << std::hex << Header[0] << " Header2 : " << std::hex << Header[1] << std::endl;
948
      exit(-1);
949
    }else{
950
      //      std::cerr << "Header1 : " << std::hex << Header[0] << " Header2 : " << std::hex << Header[1] << std::endl;
951
      //      std::cout << "Num " << i << std::endl;
952
      //      std::cerr << "Header1 : " << std::hex << Header[0] << " Header2 : " << std::hex << Header[1] << std::endl;
953
      unsigned int sizeData = Header[1] & 0xffff;
954
      unsigned int OneData[sizeData/sizeof(int)];
955
//       for(unsigned int i = 0; i<sizeData/sizeof(int); ++i){
956
//      OneData[i] = (900 +2*i) << 16;
957
//      OneData[i] += 900 +2*i +1;
958
//       }
959
      receive(socket, (char*)OneData, &sizeData);
960
      //      std::cout << OneData[0] << std::endl;
961
 
962
      memcpy(ptr_DataBuf, Header, sizeHeader);
963
      ptr_DataBuf += 2;
964
 
965
      memcpy(ptr_DataBuf, OneData, sizeData);
966
      ptr_DataBuf += sizeData/sizeof(int);
967
 
968
      TotalRecvByte += sizeHeader + sizeData;
969
      ++TotalRecvEvent;
970
    }
971
  }
972
 
973
 
974
  for(unsigned int i = 0; i<TotalRecvByte/sizeof(int); ++i){
975
//    unsigned  int buffer = (DataBuffer[i] >> 16) & 0xffff;
976
//     //      fprintf(stdout, "%d\n",buffer & 15);
977
//     file.write((char*)&buffer, sizeof(short));
978
//     buffer = DataBuffer[i] & 0xffff;
979
//     //      fprintf(stdout, "%d\n",buffer & 15);
980
//     file.write((char*)&buffer, sizeof(short));
981
    unsigned  int buffer = DataBuffer[i];
982
    file.write((char*)&buffer, sizeof(int));
983
  }
984
 
985
  signal = 0;
986
  signal += 100 << 16;
987
  if(-1 == WriteData(socket, signal)){
988
    exit(-1);
989
  }
990
 
991
  return;
992
}
993
 
994
int ADCOneCycle_wHeader_ver2(int socket, std::ofstream& file){
995
  static const int NofHeader = 3;
996
 
997
  unsigned int DataBuffer[1000];
998
  memset(DataBuffer, 0, sizeof(DataBuffer));
999
  unsigned int *ptr_DataBuf = DataBuffer;
1000
  unsigned int TotalRecvByte  = 0;
1001
  unsigned int TotalRecvEvent = 0;
1002
 
1003
  static unsigned int sizeHeader = NofHeader*sizeof(int);
1004
 
1005
  unsigned int Header[NofHeader] = {1, 1, 1};
1006
  int ret = 0;
1007
  ret = receive(socket, (char*)Header, &sizeHeader);
1008
  if(ret <= 0 && EndADC == 1){
1009
    return -1;
1010
  }
1011
 
1012
  //  std::cout << "Header1 : " << std::hex << Header[0] << " Header2 : " << std::hex << Header[1] << std::endl;
1013
  //    Header[0] = 0xFFFFEA0C;
1014
  //    Header[1] = 0x00000100;
1015
  if(Header[0] != 0xFFFFEA0C){
1016
    std::cerr << "Fatal ADC ERROR : HEADER" << std::endl;
1017
    std::cerr << "Header1 : " << std::hex << Header[0]
1018
              << " Header2 : " << std::hex << Header[1]
1019
              << " Header3 : " << std::hex << Header[2] << std::endl;
1020
    exit(-1);
1021
  }else{
1022
    unsigned int sizeData = (sizeof(int)*Header[1] & 0xffff);
1023
    unsigned int NofWord = (Header[1] & 0xffff);
1024
    unsigned int OneData[NofWord];
1025
    receive(socket, (char*)OneData, &sizeData);
1026
 
1027
//     for(int i = 0; i<NofWord; ++i){
1028
//       std::cout << std::hex << OneData[i] << std::endl;
1029
//     }
1030
 
1031
    memcpy(ptr_DataBuf, Header, sizeHeader);
1032
    ptr_DataBuf += NofHeader;
1033
 
1034
    memcpy(ptr_DataBuf, OneData, sizeData);
1035
    ptr_DataBuf += sizeData/sizeof(int);
1036
 
1037
    TotalRecvByte += sizeHeader + sizeData;
1038
    ++TotalRecvEvent;
1039
  }
1040
 
1041
  if(EndADC != 1){
1042
    for(unsigned int i = 0; i<TotalRecvByte/sizeof(int); ++i){
1043
      unsigned int buffer = DataBuffer[i];
1044
      file.write((char*)&buffer, sizeof(int));
1045
    }
1046
  }
1047
 
1048
  return 0;
1049
}
1050
 
1051
// Debug ---------------------------------------------------------------------
1052
int Debugging(int socket){
1053
  enum debug{read, write, quit, sizedebug};
1054
  bool flag_[sizedebug];
1055
  for(int i = 0; i<sizedebug; ++i){
1056
    flag_[i] = false;
1057
  }
1058
 
1059
  std::cout << "Read/Write" << std::endl;
1060
  std::cout << "1. Read" << std::endl;
1061
  std::cout << "2. Write" << std::endl;
1062
  std::cout << "3. Return" << std::endl;
1063
  std::cout << "input # =====> " << std::flush;
1064
  char buf[10];
1065
  fgets(buf, sizeof(buf), stdin);
1066
  char num;
1067
  sscanf(buf, "%c", &num);
1068
 
1069
  switch(num){
1070
  case '1':
1071
    flag_[read] = true;
1072
    break;
1073
 
1074
  case '2':
1075
    flag_[write] = true;
1076
    break;
1077
 
1078
  default:
1079
 
1080
    break;
1081
  };
1082
 
1083
  if(flag_[read]){
1084
    std::cout << "Debug Read" << std::endl;
1085
    std::cout << "Input subaddress : " << std::flush;
1086
    std::string strbuf;
1087
    std::cin >> strbuf;
1088
    std::istringstream valbuf(strbuf);
1089
    unsigned int saddr = 0;
1090
    valbuf >> saddr;
1091
    saddr = saddr << 16;
1092
 
1093
    unsigned int data = 0;
1094
    if(0 == ReadData(socket, saddr, &data)){
1095
      std::cout << std::hex << data << std::endl;
1096
      std::cout << "bit31 <<                    >> bit0" << std::endl;
1097
      for(int i = 1; i<33; ++i){
1098
        if(data >> (32 -i) & 1){
1099
          std::cout << "!" << std::flush;
1100
        }else{
1101
          std::cout << "." << std::flush;
1102
        }
1103
 
1104
        if(0 == i%8){
1105
          std::cout <<  " " << std::endl;
1106
        }
1107
      }
1108
      std::cout << std::endl;
1109
    }else{
1110
      std::cerr << "Read error" << std::endl;
1111
      exit(-1);
1112
    }
1113
  }
1114
 
1115
  if(flag_[write]){
1116
    std::cout << "Debug Write" << std::endl;
1117
    std::cout << "Input subaddress : " << std::flush;
1118
    std::string strbuf;
1119
    std::cin >> strbuf;
1120
    std::istringstream valbuf(strbuf);
1121
    unsigned int signal = 0;
1122
    valbuf >> signal;
1123
    signal = signal << 16;
1124
 
1125
    std::cout << "Input signal : " << std::flush;
1126
    std::string strbuf2;
1127
    std::cin >> strbuf2;
1128
    std::istringstream databuf(strbuf2);
1129
    unsigned int data = 0;
1130
    databuf >> data;
1131
    data = data << 8;
1132
    signal += data;
1133
 
1134
    std::cout << std::hex << signal << std::flush;
1135
    std::cout << std::dec << std::endl;
1136
    std::cout << signal << std::endl;
1137
    if(-1 == WriteData(socket, signal)){
1138
      exit(-1);
1139
    }
1140
  }
1141
 
1142
  return 0;
1143
}
1144
 
1145
void ADCStop(int socket){
1146
  unsigned int signal = 0;
1147
  std::cout << "ADC exit process" << std::endl;
1148
  signal += 16 << 24;
1149
  signal += 100 << 16;
1150
  send(socket, (char*)&signal, sizeof(int), 0);
1151
  sleep(1);
1152
 
1153
  signal = 0;
1154
  signal += 100 << 16;
1155
  send(socket, (char*)&signal, sizeof(int), 0);
1156
 
1157
  usleep(10000);
1158
 
1159
  return;
1160
}
1161
 
1162
void MHTDC_debug(int sock){
1163
 
1164
  std::ofstream m_file("mhtdc.dat",std::ios::binary);
1165
 
1166
  unsigned int saddr = 50;
1167
  unsigned int data = 0;
1168
  saddr = saddr << 16;
1169
  unsigned int signal = 4;
1170
  signal = signal << 8;
1171
  saddr += signal;
1172
  WriteData(sock, saddr);
1173
 
1174
  saddr = 50;
1175
  saddr = saddr << 16;
1176
  WriteData(sock, saddr);
1177
  usleep(100000);
1178
 
1179
  while(ForceStop == 0){
1180
    //  for(int k = 0; k<3; ++k){
1181
    saddr = 41;
1182
    data = 0;
1183
    saddr = saddr << 16;
1184
 
1185
    usleep(100000);
1186
    if(0 == ReadData(sock, saddr, &data)){
1187
      int size = (data >> 8) & 0xff;
1188
      //      std::cout << "size : " << size << std::endl;
1189
      std::cout << std::hex << "status : " << data << std::endl;
1190
 
1191
      int valid = data & 0x1;
1192
 
1193
      if(valid == 1){
1194
        unsigned int f_size = 0xffff;
1195
        f_size = (f_size << 16) + size;
1196
        m_file.write((char*)&f_size, sizeof(unsigned int));
1197
        for(int i = 0; i<size; ++i){
1198
          //      usleep(100);
1199
          saddr = 42;
1200
          saddr = saddr << 16;
1201
          unsigned int head =0;
1202
          if(0 == ReadData(sock, saddr, &data)){
1203
            std::cout << std::hex << "header" << i << " : " << data << std::endl;
1204
            head = data & 0xffff;
1205
          }else{
1206
            std::cerr << "Read error" << std::endl;
1207
            exit(-1);
1208
          }
1209
 
1210
          // Leading
1211
          saddr = 40;
1212
          saddr = saddr << 16;
1213
          if(0 == ReadData(sock, saddr, &data)){
1214
            unsigned int time = (data & 0xffff);
1215
            unsigned int one_data = (head << 16) + time;
1216
            m_file.write((char*)&one_data, sizeof(unsigned int));
1217
            std::cout << std::dec << "time L" << i << " : " << time << " ns" << std::endl;
1218
          }else{
1219
            std::cerr << "Read error" << std::endl;
1220
            exit(-1);
1221
          }
1222
 
1223
          // Trailing
1224
          saddr = 43;
1225
          saddr = saddr << 16;
1226
          if(0 == ReadData(sock, saddr, &data)){
1227
            unsigned int time = (data & 0xffff);
1228
            time = (1 << 15) + time;
1229
            unsigned int one_data = (head << 16) + time;
1230
            m_file.write((char*)&one_data, sizeof(unsigned int));
1231
            std::cout << std::dec << "time T" << i << " : " << (time & 0xfff) << " ns" << std::endl;
1232
          }else{
1233
            std::cerr << "Read error" << std::endl;
1234
            exit(-1);
1235
          }
1236
        }
1237
        saddr = 50;
1238
        saddr = saddr << 16;
1239
        signal = 4;
1240
        signal = signal << 8;
1241
        saddr += signal;
1242
        WriteData(sock, saddr);
1243
 
1244
        saddr = 50;
1245
        saddr = saddr << 16;
1246
        WriteData(sock, saddr);
1247
 
1248
      }
1249
    }else{
1250
      std::cerr << "Read error" << std::endl;
1251
      exit(-1);
1252
    }
1253
  }
1254
 
1255
  return;
1256
}
1257
 
1258
int chipselect(){
1259
 
1260
  WriteData(3, 37888);
1261
    // if(-1 == WriteData(3, 5120)){
1262
    //   exit(-1);
1263
    // }
1264
  return 0;
1265
}
1266
          */