1 | // $URL: http://subversion:8080/svn/gsc/trunk/drivers/LINUX/18AI32SSC1M/savedata/savedata.c $ |
---|
2 | // $Rev: 35161 $ |
---|
3 | // $Date: 2016-06-06 16:28:07 -0500 (Mon, 06 Jun 2016) $ |
---|
4 | |
---|
5 | #include <ctype.h> |
---|
6 | #include <stdio.h> |
---|
7 | #include <stdlib.h> |
---|
8 | #include <string.h> |
---|
9 | #include <time.h> |
---|
10 | #include <unistd.h> |
---|
11 | |
---|
12 | #include "main.h" |
---|
13 | |
---|
14 | #define MAX_INFO_FILE_LENGTH 500 |
---|
15 | |
---|
16 | |
---|
17 | // #defines ******************************************************************* |
---|
18 | |
---|
19 | #define _1M (1024L * 1024L) |
---|
20 | |
---|
21 | |
---|
22 | |
---|
23 | // variables ******************************************************************* |
---|
24 | |
---|
25 | static u32 _buffer[_1M]; |
---|
26 | |
---|
27 | |
---|
28 | |
---|
29 | //***************************************************************************** |
---|
30 | static int _channels(int fd, s32* chans) |
---|
31 | { |
---|
32 | s32 active = -1; |
---|
33 | char buf[64]; |
---|
34 | int errs = 0; |
---|
35 | s32 first = -1; |
---|
36 | s32 last = -1; |
---|
37 | s32 single = -1; |
---|
38 | |
---|
39 | gsc_label("Input Channels"); |
---|
40 | |
---|
41 | active = -1; |
---|
42 | errs += ai32ssc1m_dsl_ioctl(fd, AI32SSC1M_IOCTL_CHAN_ACTIVE, &active ); |
---|
43 | errs += ai32ssc1m_dsl_ioctl(fd, AI32SSC1M_IOCTL_CHAN_FIRST, &first ); |
---|
44 | errs += ai32ssc1m_dsl_ioctl(fd, AI32SSC1M_IOCTL_CHAN_LAST, &last ); |
---|
45 | errs += ai32ssc1m_dsl_ioctl(fd, AI32SSC1M_IOCTL_CHAN_SINGLE, &single ); |
---|
46 | |
---|
47 | if (errs == 0) |
---|
48 | { |
---|
49 | switch (active) |
---|
50 | { |
---|
51 | default: |
---|
52 | |
---|
53 | errs++; |
---|
54 | printf("FAIL <--- (invalid ACTIVE selection: %ld)\n", (long) active); |
---|
55 | break; |
---|
56 | |
---|
57 | case AI32SSC1M_CHAN_ACTIVE_SINGLE: |
---|
58 | |
---|
59 | chans[0] = 1; |
---|
60 | sprintf(buf, "# %ld", (long) single); |
---|
61 | break; |
---|
62 | |
---|
63 | case AI32SSC1M_CHAN_ACTIVE_0_1: |
---|
64 | |
---|
65 | chans[0] = 2; |
---|
66 | strcpy(buf, "#s 0-1"); |
---|
67 | break; |
---|
68 | |
---|
69 | case AI32SSC1M_CHAN_ACTIVE_0_3: |
---|
70 | |
---|
71 | chans[0] = 4; |
---|
72 | strcpy(buf, "#s 0-3"); |
---|
73 | break; |
---|
74 | |
---|
75 | case AI32SSC1M_CHAN_ACTIVE_0_7: |
---|
76 | |
---|
77 | chans[0] = 8; |
---|
78 | strcpy(buf, "#s 0-7"); |
---|
79 | break; |
---|
80 | |
---|
81 | case AI32SSC1M_CHAN_ACTIVE_0_15: |
---|
82 | |
---|
83 | chans[0] = 16; |
---|
84 | strcpy(buf, "#s 0-15"); |
---|
85 | break; |
---|
86 | |
---|
87 | case AI32SSC1M_CHAN_ACTIVE_0_31: |
---|
88 | |
---|
89 | chans[0] = 32; |
---|
90 | strcpy(buf, "#s 0-31"); |
---|
91 | break; |
---|
92 | |
---|
93 | case AI32SSC1M_CHAN_ACTIVE_RANGE: |
---|
94 | |
---|
95 | chans[0] = last - first + 1; |
---|
96 | |
---|
97 | if (first == last) |
---|
98 | sprintf(buf, "# %ld", (long) first); |
---|
99 | else |
---|
100 | sprintf(buf, "#s %ld-%ld", (long) first, (long) last); |
---|
101 | |
---|
102 | break; |
---|
103 | } |
---|
104 | |
---|
105 | if (errs == 0) |
---|
106 | { |
---|
107 | printf( "%ld Channel%s (%s)\n", |
---|
108 | (long) chans[0], |
---|
109 | (chans[0] == 1) ? "" : "s", |
---|
110 | buf); |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | return(errs); |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | |
---|
119 | //***************************************************************************** |
---|
120 | static int _read_data(int fd) |
---|
121 | { |
---|
122 | int errs; |
---|
123 | long get = sizeof(_buffer) / 4; |
---|
124 | int got; |
---|
125 | |
---|
126 | gsc_label("Reading"); |
---|
127 | got = ai32ssc1m_dsl_read(fd, _buffer, get); |
---|
128 | |
---|
129 | if (got < 0) |
---|
130 | { |
---|
131 | errs = 1; |
---|
132 | } |
---|
133 | else if (got != get) |
---|
134 | { |
---|
135 | errs = 1; |
---|
136 | printf( "FAIL <--- (got %ld samples, requested %ld)\n", |
---|
137 | (long) got, |
---|
138 | (long) get); |
---|
139 | } |
---|
140 | else |
---|
141 | { |
---|
142 | errs = 0; |
---|
143 | printf( "PASS (%ld samples)\n", |
---|
144 | (long) get); |
---|
145 | } |
---|
146 | |
---|
147 | return(errs); |
---|
148 | } |
---|
149 | |
---|
150 | |
---|
151 | |
---|
152 | |
---|
153 | //***************************************************************************** |
---|
154 | static int _save_data_with_datename(int fd, int chan, int acquisition_rate, int errs) |
---|
155 | { |
---|
156 | // char name_hex[50]; |
---|
157 | // char name_dec[50]; |
---|
158 | char name[50]; |
---|
159 | |
---|
160 | s32 first_channel=-1; |
---|
161 | s32 last_channel=-1; |
---|
162 | s32 amplitude=-1; |
---|
163 | FILE* file; |
---|
164 | int i; |
---|
165 | long l; |
---|
166 | // const char* name = "data.txt"; |
---|
167 | long samples = sizeof(_buffer) / 4; |
---|
168 | |
---|
169 | char fileposition[]="/home/etalon-admin/acquisition_data/motor_position.txt"; |
---|
170 | FILE* fid; |
---|
171 | char line[MAX_INFO_FILE_LENGTH]; |
---|
172 | |
---|
173 | time_t t = time(NULL); |
---|
174 | struct tm tm = *localtime(&t); |
---|
175 | |
---|
176 | sprintf(name,"data_CLIO_%d-%02d-%02d_%02d-%02d-%02d.txt", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); |
---|
177 | |
---|
178 | errs += ai32ssc1m_chan_first (fd, -1, -1, &first_channel); |
---|
179 | errs += ai32ssc1m_chan_last (fd, -1, -1, &last_channel); |
---|
180 | errs += ai32ssc1m_ain_range(fd, -1, -1, &litude); |
---|
181 | gsc_label("Filename"); |
---|
182 | printf("%s \n",name); |
---|
183 | |
---|
184 | gsc_label("Saving"); |
---|
185 | |
---|
186 | for (;;) |
---|
187 | { |
---|
188 | if (errs) |
---|
189 | { |
---|
190 | printf("SKIPPED (errors)\n"); |
---|
191 | errs = 0; |
---|
192 | break; |
---|
193 | } |
---|
194 | |
---|
195 | file = fopen(name, "w+b"); |
---|
196 | |
---|
197 | if (file == NULL) |
---|
198 | { |
---|
199 | printf("FAIL <--- (unable to create %s)\n", name); |
---|
200 | errs = 1; |
---|
201 | break; |
---|
202 | } |
---|
203 | |
---|
204 | fprintf(file,"#date= %d-%02d-%02d_%02d-%02d-%02d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); |
---|
205 | fprintf(file,"#first= %d\n", first_channel); |
---|
206 | fprintf(file,"#last= %d\n", last_channel); |
---|
207 | fprintf(file,"#acquisition_rate= %d\n", acquisition_rate); |
---|
208 | fprintf(file,"#amplitude= %d\n", amplitude); |
---|
209 | |
---|
210 | fid = fopen(fileposition, "r"); |
---|
211 | int itry=0; |
---|
212 | while ((fid == NULL)&&(itry<1)) { |
---|
213 | printf("Trying to read motor position (%d)\n",itry); |
---|
214 | sleep(1); |
---|
215 | fid = fopen(fileposition, "r"); |
---|
216 | itry++; |
---|
217 | } |
---|
218 | if (fid == NULL) { |
---|
219 | printf("FAIL <--- (unable to read %s)\n", fileposition); |
---|
220 | } else { |
---|
221 | printf("Reading motor position\n"); |
---|
222 | while(fgets(line,MAX_INFO_FILE_LENGTH,fid)){ |
---|
223 | //while(getline(&line,&line_length,fid)){ |
---|
224 | //fread(line,sizeof(char),MAX_INFO_FILE_LENGTH,fid); |
---|
225 | printf("Line read: %s\n",line); |
---|
226 | fprintf(file,"#%s",line); |
---|
227 | } //while reading line |
---|
228 | }//fid not null |
---|
229 | fclose(fid); |
---|
230 | |
---|
231 | printf("SAMPLES %08lX \n",samples); |
---|
232 | for (l = 0; l < samples; l++) |
---|
233 | { |
---|
234 | i = fprintf(file, "%08lX ", (long) _buffer[l]); |
---|
235 | |
---|
236 | if (i != 10) |
---|
237 | { |
---|
238 | printf("FAIL <--- (fprintf() failure to %s)\n", name); |
---|
239 | errs = 1; |
---|
240 | break; |
---|
241 | } |
---|
242 | // if ((l) && (_buffer[l] & 0x80000000)) |
---|
243 | if (l%32==31) |
---|
244 | { |
---|
245 | i = fprintf(file, "\r\n"); |
---|
246 | |
---|
247 | if (i != 2) |
---|
248 | { |
---|
249 | printf("FAIL <--- (fprintf() failure to %s)\n", name);; |
---|
250 | errs = 1; |
---|
251 | break; |
---|
252 | } |
---|
253 | } |
---|
254 | |
---|
255 | } |
---|
256 | |
---|
257 | fprintf(file,"\r\n"); |
---|
258 | fclose(file); |
---|
259 | |
---|
260 | if (errs == 0) |
---|
261 | printf("PASS (%s)\n", name); |
---|
262 | |
---|
263 | break; |
---|
264 | } |
---|
265 | |
---|
266 | return(errs); |
---|
267 | |
---|
268 | } // _save_data_with_name |
---|
269 | |
---|
270 | //***************************************************************************** |
---|
271 | static int _save_data(int fd, int chan, int errs) |
---|
272 | { |
---|
273 | FILE* file; |
---|
274 | int i; |
---|
275 | long l; |
---|
276 | const char* name = "data.txt"; |
---|
277 | long samples = sizeof(_buffer) / 4; |
---|
278 | |
---|
279 | gsc_label("Saving"); |
---|
280 | |
---|
281 | for (;;) |
---|
282 | { |
---|
283 | if (errs) |
---|
284 | { |
---|
285 | printf("SKIPPED (errors)\n"); |
---|
286 | errs = 0; |
---|
287 | break; |
---|
288 | } |
---|
289 | |
---|
290 | file = fopen(name, "w+b"); |
---|
291 | |
---|
292 | if (file == NULL) |
---|
293 | { |
---|
294 | printf("FAIL <--- (unable to create %s)\n", name); |
---|
295 | errs = 1; |
---|
296 | break; |
---|
297 | } |
---|
298 | |
---|
299 | for (l = 0; l < samples; l++) |
---|
300 | { |
---|
301 | if ((l) && (_buffer[l] & 0x80000000)) |
---|
302 | { |
---|
303 | i = fprintf(file, "\r\n"); |
---|
304 | |
---|
305 | if (i != 2) |
---|
306 | { |
---|
307 | printf("FAIL <--- (fprintf() failure to %s)\n", name);; |
---|
308 | errs = 1; |
---|
309 | break; |
---|
310 | } |
---|
311 | } |
---|
312 | |
---|
313 | i = fprintf(file, " %08lX", (long) _buffer[l]); |
---|
314 | |
---|
315 | if (i != 10) |
---|
316 | { |
---|
317 | printf("FAIL <--- (fprintf() failure to %s)\n", name); |
---|
318 | errs = 1; |
---|
319 | break; |
---|
320 | } |
---|
321 | } |
---|
322 | |
---|
323 | fclose(file); |
---|
324 | |
---|
325 | if (errs == 0) |
---|
326 | printf("PASS (%s)\n", name); |
---|
327 | |
---|
328 | break; |
---|
329 | } |
---|
330 | |
---|
331 | return(errs); |
---|
332 | } |
---|
333 | |
---|
334 | |
---|
335 | |
---|
336 | //***************************************************************************** |
---|
337 | int save_data(int fd, s32 io_mode) |
---|
338 | { |
---|
339 | s32 chans = AI32SSC1M_CHAN_ACTIVE_0_31; |
---|
340 | s32 nchannels=32; |
---|
341 | // s32 chans = AI32SSC1M_CHAN_ACTIVE_0_15; |
---|
342 | int errs = 0; |
---|
343 | s32 timeout = 20; |
---|
344 | s32 acquisition_rate = 36; // 36=1Ms/s min: 549s/s |
---|
345 | s32 previous_acquisition_rate=0; |
---|
346 | // s32 burst_size_per_channel=10240; |
---|
347 | s32 burst_size_per_channel=10240; |
---|
348 | s32 burst_size=burst_size_per_channel*nchannels; |
---|
349 | int triggered=1; |
---|
350 | gsc_label("Capture & Save"); |
---|
351 | printf("\n"); |
---|
352 | gsc_label_level_inc(); |
---|
353 | |
---|
354 | //Acquisition rate |
---|
355 | errs += ai32ssc1m_config_ai(fd, -1, acquisition_rate); |
---|
356 | previous_acquisition_rate=acquisition_rate; |
---|
357 | // errs += ai32ssc1m_config_ai(fd, -1, 1440); |
---|
358 | |
---|
359 | //Trigger: |
---|
360 | //AI32SSC1M_BURST_SYNC_EXT => external trigger |
---|
361 | //AI32SSC1M_BURST_SYNC_DISABLE => Internal trigger |
---|
362 | if (triggered==1) { |
---|
363 | gsc_label("Triggered"); |
---|
364 | printf("\n"); |
---|
365 | errs += ai32ssc1m_burst_sync(fd, -1, AI32SSC1M_BURST_SYNC_EXT, NULL); // External trigger |
---|
366 | } else { |
---|
367 | gsc_label("Internal Trig"); |
---|
368 | printf("\n"); |
---|
369 | errs += ai32ssc1m_burst_sync(fd, -1, AI32SSC1M_BURST_SYNC_DISABLE, NULL); // Internal trigger |
---|
370 | } |
---|
371 | //errs += ai32ssc1m_burst_sync(fd, -1, AI32SSC1M_BURST_SYNC_RBG, NULL); // External trigger |
---|
372 | |
---|
373 | // errs += ai32ssc1m_aux_clk_mode(fd,-1,AI32SSC1M_AUX_CLK_MODE_DISABLE,NULL); |
---|
374 | // errs += ai32ssc1m_aux_sync_mode(fd,-1,AI32SSC1M_AUX_SYNC_MODE_INPUT,NULL); |
---|
375 | //errs += ai32ssc1m_aux_sync_mode(fd,-1,AI32SSC1M_AUX_SYNC_MODE_DISABLE,NULL); |
---|
376 | errs += ai32ssc1m_aux_clk_mode(fd,-1,AI32SSC1M_AUX_CLK_MODE_DISABLE,NULL); |
---|
377 | errs += ai32ssc1m_burst_size(fd, -1, burst_size, NULL); |
---|
378 | |
---|
379 | // errs += ai32ssc1m_burst_size(fd, -1, 16, NULL); // Burst size |
---|
380 | |
---|
381 | //Timeout (duration set above) |
---|
382 | errs += ai32ssc1m_rx_io_timeout(fd,-1, -1, timeout, NULL); |
---|
383 | |
---|
384 | errs += ai32ssc1m_rx_io_mode(fd, -1, io_mode, NULL); |
---|
385 | errs += _channels(fd, &chans); |
---|
386 | |
---|
387 | //Channels range |
---|
388 | errs += ai32ssc1m_chan_active (fd, -1, AI32SSC1M_CHAN_ACTIVE_RANGE, NULL); |
---|
389 | errs += ai32ssc1m_chan_first (fd, -1, 0, NULL); |
---|
390 | errs += ai32ssc1m_chan_last (fd, -1, 31, NULL); |
---|
391 | errs += ai32ssc1m_ain_range(fd, -1, AI32SSC1M_AIN_RANGE_5V, NULL); |
---|
392 | // errs += ai32ssc1m_ain_mode(fd, -1, -1, &timeout); |
---|
393 | errs += ai32ssc1m_ain_mode(fd, -1, AI32SSC1M_AIN_MODE_DIFF, NULL); |
---|
394 | // errs += ai32ssc1m_ain_mode(fd, -1, AI32SSC1M_AIN_MODE_VREF, NULL); |
---|
395 | gsc_label("1 second before data taking..."); |
---|
396 | sleep(1); |
---|
397 | printf("Now\n"); |
---|
398 | gsc_label_level_inc(); |
---|
399 | for(;;) { |
---|
400 | if (triggered==1) { |
---|
401 | gsc_label("Triggered"); |
---|
402 | printf("\n"); |
---|
403 | errs += ai32ssc1m_burst_sync(fd, -1, AI32SSC1M_BURST_SYNC_EXT, NULL); // External trigger |
---|
404 | } else { |
---|
405 | gsc_label("Internal Trig"); |
---|
406 | printf("\n"); |
---|
407 | errs += ai32ssc1m_burst_sync(fd, -1, AI32SSC1M_BURST_SYNC_DISABLE, NULL); // Internal trigger |
---|
408 | } |
---|
409 | errs += ai32ssc1m_burst_size(fd, -1, burst_size, NULL); |
---|
410 | errs += ai32ssc1m_aux_clk_mode(fd,-1,AI32SSC1M_AUX_CLK_MODE_DISABLE,NULL); |
---|
411 | gsc_label("Acquiring"); |
---|
412 | errs += ai32ssc1m_ain_buf_clear(fd, -1); |
---|
413 | errs += _read_data(fd); |
---|
414 | errs += ai32ssc1m_ain_buf_overflow(fd, -1, -1, NULL); |
---|
415 | errs += ai32ssc1m_ain_buf_underflow(fd, -1, -1, NULL); |
---|
416 | // errs += ai32ssc1m_ain_buf_thr_lvl(fd, -1, -1, &timeout); |
---|
417 | //printf("thr lvl %d \n",timeout); |
---|
418 | errs += _save_data_with_datename(fd, chans, acquisition_rate, errs); |
---|
419 | gsc_label("Done"); |
---|
420 | |
---|
421 | char fileinfo[]="/home/etalon-admin/acquisition_data/acquisition_info.txt"; |
---|
422 | FILE* fid; |
---|
423 | char line[MAX_INFO_FILE_LENGTH]; |
---|
424 | int pause_between_acquisition_milliseconds=1; |
---|
425 | fid = fopen(fileinfo, "r"); |
---|
426 | if (fid == NULL) { |
---|
427 | printf("FAIL <--- (unable to read %s)\n", fileinfo); |
---|
428 | } else { |
---|
429 | fread(line,sizeof(char),MAX_INFO_FILE_LENGTH,fid); |
---|
430 | //gsc_label("Line read: %s\n",line); |
---|
431 | int iline=0; |
---|
432 | while(line[iline]!='\0'){ |
---|
433 | int char_found=0; |
---|
434 | if (strncmp("PAUSE",&(line[iline]),5)==0) { |
---|
435 | iline+=6; |
---|
436 | pause_between_acquisition_milliseconds=0; |
---|
437 | while(((line[iline]-'0')>=0)&&((line[iline]-'0')<='9')) { |
---|
438 | pause_between_acquisition_milliseconds*=10; |
---|
439 | pause_between_acquisition_milliseconds+=(line[iline]-'0'); |
---|
440 | iline++; |
---|
441 | } |
---|
442 | gsc_label("PAUSE (ms) "); |
---|
443 | printf("%d\n",pause_between_acquisition_milliseconds); |
---|
444 | char_found=1; |
---|
445 | } //line is pause |
---|
446 | if (strncmp("RATE",&(line[iline]),4)==0) { |
---|
447 | acquisition_rate=0; |
---|
448 | iline+=5; |
---|
449 | while(((line[iline]-'0')>=0)&&((line[iline]-'0')<='9')) { |
---|
450 | // printf("val= %d %c %d %d\n",iline, line[iline], line[iline]-'0', acquisition_rate); |
---|
451 | acquisition_rate*=10; |
---|
452 | acquisition_rate+=(line[iline]-'0'); |
---|
453 | iline++; |
---|
454 | } |
---|
455 | if (acquisition_rate!=previous_acquisition_rate) { |
---|
456 | errs += ai32ssc1m_config_ai(fd, -1, acquisition_rate); |
---|
457 | previous_acquisition_rate=acquisition_rate; |
---|
458 | gsc_label("Acquisition rate changed to "); |
---|
459 | printf("%d\n",acquisition_rate); |
---|
460 | } |
---|
461 | char_found=1; |
---|
462 | } |
---|
463 | if ((line[iline]=='\f')||(line[iline]=='\n')){ |
---|
464 | iline++; |
---|
465 | char_found=1; |
---|
466 | } //special caracters |
---|
467 | //printf("Reading... %d %d %d %s\n", iline, line[iline],char_found,&(line[iline])); |
---|
468 | if(char_found==0){ |
---|
469 | iline++; |
---|
470 | } |
---|
471 | } //while reading line |
---|
472 | fclose(fid); |
---|
473 | } |
---|
474 | gsc_label("Sleep "); |
---|
475 | printf("%d ms\n",pause_between_acquisition_milliseconds); |
---|
476 | sleep((float)pause_between_acquisition_milliseconds/1000.); |
---|
477 | |
---|
478 | char filename[]="/home/etalon-admin/acquisition_data/pause_acquisition"; |
---|
479 | int isleep=0; |
---|
480 | while( access( filename, F_OK ) != -1 ) { |
---|
481 | // file exists |
---|
482 | if (isleep==0) { |
---|
483 | printf("File %s exists\nPause...\n",filename); |
---|
484 | isleep=1; |
---|
485 | } |
---|
486 | printf("."); |
---|
487 | sleep(10); |
---|
488 | } |
---|
489 | char filename_stop[]="/home/etalon-admin/acquisition_data/stop_acquisition"; |
---|
490 | if( access( filename_stop, F_OK ) != -1 ) { |
---|
491 | printf("File %s is present, exiting...\n",filename_stop); |
---|
492 | // file exists |
---|
493 | break; //we leave the for loop |
---|
494 | } |
---|
495 | } |
---|
496 | gsc_label_level_dec(); |
---|
497 | gsc_label_level_dec(); |
---|
498 | return(errs); |
---|
499 | } |
---|
500 | |
---|
501 | |
---|
502 | |
---|