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