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 | |
---|
11 | #include "main.h" |
---|
12 | |
---|
13 | |
---|
14 | |
---|
15 | // #defines ******************************************************************* |
---|
16 | |
---|
17 | #define _1M (1024L * 1024L) |
---|
18 | |
---|
19 | |
---|
20 | |
---|
21 | // variables ******************************************************************* |
---|
22 | |
---|
23 | static u32 _buffer[_1M]; |
---|
24 | |
---|
25 | |
---|
26 | |
---|
27 | //***************************************************************************** |
---|
28 | static int _channels(int fd, s32* chans) |
---|
29 | { |
---|
30 | s32 active = -1; |
---|
31 | char buf[64]; |
---|
32 | int errs = 0; |
---|
33 | s32 first = -1; |
---|
34 | s32 last = -1; |
---|
35 | s32 single = -1; |
---|
36 | |
---|
37 | gsc_label("Input Channels"); |
---|
38 | |
---|
39 | active = -1; |
---|
40 | errs += ai32ssc1m_dsl_ioctl(fd, AI32SSC1M_IOCTL_CHAN_ACTIVE, &active ); |
---|
41 | errs += ai32ssc1m_dsl_ioctl(fd, AI32SSC1M_IOCTL_CHAN_FIRST, &first ); |
---|
42 | errs += ai32ssc1m_dsl_ioctl(fd, AI32SSC1M_IOCTL_CHAN_LAST, &last ); |
---|
43 | errs += ai32ssc1m_dsl_ioctl(fd, AI32SSC1M_IOCTL_CHAN_SINGLE, &single ); |
---|
44 | |
---|
45 | if (errs == 0) |
---|
46 | { |
---|
47 | switch (active) |
---|
48 | { |
---|
49 | default: |
---|
50 | |
---|
51 | errs++; |
---|
52 | printf("FAIL <--- (invalid ACTIVE selection: %ld)\n", (long) active); |
---|
53 | break; |
---|
54 | |
---|
55 | case AI32SSC1M_CHAN_ACTIVE_SINGLE: |
---|
56 | |
---|
57 | chans[0] = 1; |
---|
58 | sprintf(buf, "# %ld", (long) single); |
---|
59 | break; |
---|
60 | |
---|
61 | case AI32SSC1M_CHAN_ACTIVE_0_1: |
---|
62 | |
---|
63 | chans[0] = 2; |
---|
64 | strcpy(buf, "#s 0-1"); |
---|
65 | break; |
---|
66 | |
---|
67 | case AI32SSC1M_CHAN_ACTIVE_0_3: |
---|
68 | |
---|
69 | chans[0] = 4; |
---|
70 | strcpy(buf, "#s 0-3"); |
---|
71 | break; |
---|
72 | |
---|
73 | case AI32SSC1M_CHAN_ACTIVE_0_7: |
---|
74 | |
---|
75 | chans[0] = 8; |
---|
76 | strcpy(buf, "#s 0-7"); |
---|
77 | break; |
---|
78 | |
---|
79 | case AI32SSC1M_CHAN_ACTIVE_0_15: |
---|
80 | |
---|
81 | chans[0] = 16; |
---|
82 | strcpy(buf, "#s 0-15"); |
---|
83 | break; |
---|
84 | |
---|
85 | case AI32SSC1M_CHAN_ACTIVE_0_31: |
---|
86 | |
---|
87 | chans[0] = 32; |
---|
88 | strcpy(buf, "#s 0-31"); |
---|
89 | break; |
---|
90 | |
---|
91 | case AI32SSC1M_CHAN_ACTIVE_RANGE: |
---|
92 | |
---|
93 | chans[0] = last - first + 1; |
---|
94 | |
---|
95 | if (first == last) |
---|
96 | sprintf(buf, "# %ld", (long) first); |
---|
97 | else |
---|
98 | sprintf(buf, "#s %ld-%ld", (long) first, (long) last); |
---|
99 | |
---|
100 | break; |
---|
101 | } |
---|
102 | |
---|
103 | if (errs == 0) |
---|
104 | { |
---|
105 | printf( "%ld Channel%s (%s)\n", |
---|
106 | (long) chans[0], |
---|
107 | (chans[0] == 1) ? "" : "s", |
---|
108 | buf); |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | return(errs); |
---|
113 | } |
---|
114 | |
---|
115 | |
---|
116 | |
---|
117 | //***************************************************************************** |
---|
118 | static int _read_data(int fd) |
---|
119 | { |
---|
120 | int errs; |
---|
121 | long get = sizeof(_buffer) / 4; |
---|
122 | int got; |
---|
123 | |
---|
124 | gsc_label("Reading"); |
---|
125 | got = ai32ssc1m_dsl_read(fd, _buffer, get); |
---|
126 | |
---|
127 | if (got < 0) |
---|
128 | { |
---|
129 | errs = 1; |
---|
130 | } |
---|
131 | else if (got != get) |
---|
132 | { |
---|
133 | errs = 1; |
---|
134 | printf( "FAIL <--- (got %ld samples, requested %ld)\n", |
---|
135 | (long) got, |
---|
136 | (long) get); |
---|
137 | } |
---|
138 | else |
---|
139 | { |
---|
140 | errs = 0; |
---|
141 | printf( "PASS (%ld samples)\n", |
---|
142 | (long) get); |
---|
143 | } |
---|
144 | |
---|
145 | return(errs); |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | |
---|
150 | |
---|
151 | //***************************************************************************** |
---|
152 | static int _save_data_with_datename(int fd, int chan, int acquisition_rate, int errs) |
---|
153 | { |
---|
154 | // char name_hex[50]; |
---|
155 | // char name_dec[50]; |
---|
156 | char name[50]; |
---|
157 | |
---|
158 | s32 first_channel=-1; |
---|
159 | s32 last_channel=-1; |
---|
160 | s32 amplitude=-1; |
---|
161 | FILE* file; |
---|
162 | int i; |
---|
163 | long l; |
---|
164 | // const char* name = "data.txt"; |
---|
165 | long samples = sizeof(_buffer) / 4; |
---|
166 | |
---|
167 | time_t t = time(NULL); |
---|
168 | struct tm tm = *localtime(&t); |
---|
169 | |
---|
170 | 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); |
---|
171 | |
---|
172 | errs += ai32ssc1m_chan_first (fd, -1, -1, &first_channel); |
---|
173 | errs += ai32ssc1m_chan_last (fd, -1, -1, &last_channel); |
---|
174 | errs += ai32ssc1m_ain_range(fd, -1, -1, &litude); |
---|
175 | gsc_label("Filename"); |
---|
176 | printf("%s \n",name); |
---|
177 | |
---|
178 | gsc_label("Saving"); |
---|
179 | |
---|
180 | for (;;) |
---|
181 | { |
---|
182 | if (errs) |
---|
183 | { |
---|
184 | printf("SKIPPED (errors)\n"); |
---|
185 | errs = 0; |
---|
186 | break; |
---|
187 | } |
---|
188 | |
---|
189 | file = fopen(name, "w+b"); |
---|
190 | |
---|
191 | if (file == NULL) |
---|
192 | { |
---|
193 | printf("FAIL <--- (unable to create %s)\n", name); |
---|
194 | errs = 1; |
---|
195 | break; |
---|
196 | } |
---|
197 | |
---|
198 | 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); |
---|
199 | fprintf(file,"#first= %d\n", first_channel); |
---|
200 | fprintf(file,"#last= %d\n", last_channel); |
---|
201 | fprintf(file,"#acquisition_rate= %d\n", acquisition_rate); |
---|
202 | fprintf(file,"#amplitude= %d\n", amplitude); |
---|
203 | |
---|
204 | |
---|
205 | // printf("SAMPLES %d \n",samples); |
---|
206 | for (l = 0; l < samples; l++) |
---|
207 | { |
---|
208 | i = fprintf(file, "%08lX ", (long) _buffer[l]); |
---|
209 | |
---|
210 | if (i != 10) |
---|
211 | { |
---|
212 | printf("FAIL <--- (fprintf() failure to %s)\n", name); |
---|
213 | errs = 1; |
---|
214 | break; |
---|
215 | } |
---|
216 | // if ((l) && (_buffer[l] & 0x80000000)) |
---|
217 | if (l%32==31) |
---|
218 | { |
---|
219 | i = fprintf(file, "\r\n"); |
---|
220 | |
---|
221 | if (i != 2) |
---|
222 | { |
---|
223 | printf("FAIL <--- (fprintf() failure to %s)\n", name);; |
---|
224 | errs = 1; |
---|
225 | break; |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | } |
---|
230 | |
---|
231 | fprintf(file,"\r\n"); |
---|
232 | fclose(file); |
---|
233 | |
---|
234 | if (errs == 0) |
---|
235 | printf("PASS (%s)\n", name); |
---|
236 | |
---|
237 | break; |
---|
238 | } |
---|
239 | |
---|
240 | return(errs); |
---|
241 | |
---|
242 | } // _save_data_with_name |
---|
243 | |
---|
244 | //***************************************************************************** |
---|
245 | static int _save_data(int fd, int chan, int errs) |
---|
246 | { |
---|
247 | FILE* file; |
---|
248 | int i; |
---|
249 | long l; |
---|
250 | const char* name = "data.txt"; |
---|
251 | long samples = sizeof(_buffer) / 4; |
---|
252 | |
---|
253 | gsc_label("Saving"); |
---|
254 | |
---|
255 | for (;;) |
---|
256 | { |
---|
257 | if (errs) |
---|
258 | { |
---|
259 | printf("SKIPPED (errors)\n"); |
---|
260 | errs = 0; |
---|
261 | break; |
---|
262 | } |
---|
263 | |
---|
264 | file = fopen(name, "w+b"); |
---|
265 | |
---|
266 | if (file == NULL) |
---|
267 | { |
---|
268 | printf("FAIL <--- (unable to create %s)\n", name); |
---|
269 | errs = 1; |
---|
270 | break; |
---|
271 | } |
---|
272 | |
---|
273 | for (l = 0; l < samples; l++) |
---|
274 | { |
---|
275 | if ((l) && (_buffer[l] & 0x80000000)) |
---|
276 | { |
---|
277 | i = fprintf(file, "\r\n"); |
---|
278 | |
---|
279 | if (i != 2) |
---|
280 | { |
---|
281 | printf("FAIL <--- (fprintf() failure to %s)\n", name);; |
---|
282 | errs = 1; |
---|
283 | break; |
---|
284 | } |
---|
285 | } |
---|
286 | |
---|
287 | i = fprintf(file, " %08lX", (long) _buffer[l]); |
---|
288 | |
---|
289 | if (i != 10) |
---|
290 | { |
---|
291 | printf("FAIL <--- (fprintf() failure to %s)\n", name); |
---|
292 | errs = 1; |
---|
293 | break; |
---|
294 | } |
---|
295 | } |
---|
296 | |
---|
297 | fclose(file); |
---|
298 | |
---|
299 | if (errs == 0) |
---|
300 | printf("PASS (%s)\n", name); |
---|
301 | |
---|
302 | break; |
---|
303 | } |
---|
304 | |
---|
305 | return(errs); |
---|
306 | } |
---|
307 | |
---|
308 | |
---|
309 | |
---|
310 | //***************************************************************************** |
---|
311 | int save_data(int fd, s32 io_mode) |
---|
312 | { |
---|
313 | s32 chans = AI32SSC1M_CHAN_ACTIVE_0_31; |
---|
314 | // s32 chans = AI32SSC1M_CHAN_ACTIVE_0_15; |
---|
315 | int errs = 0; |
---|
316 | s32 timeout = 60; |
---|
317 | s32 acquisition_rate = 360; // 36=1Ms/s min: 549s/s |
---|
318 | gsc_label("Capture & Save"); |
---|
319 | printf("\n"); |
---|
320 | gsc_label_level_inc(); |
---|
321 | |
---|
322 | //Acquisition rate |
---|
323 | errs += ai32ssc1m_config_ai(fd, -1, acquisition_rate); |
---|
324 | // errs += ai32ssc1m_config_ai(fd, -1, 1440); |
---|
325 | |
---|
326 | //Trigger: |
---|
327 | //AI32SSC1M_BURST_SYNC_EXT => external trigger |
---|
328 | //AI32SSC1M_BURST_SYNC_DISABLE => Internal trigger |
---|
329 | errs += ai32ssc1m_burst_sync(fd, -1, AI32SSC1M_BURST_SYNC_DISABLE, NULL); // Internal trigger |
---|
330 | // errs += ai32ssc1m_burst_sync(fd, -1, AI32SSC1M_BURST_SYNC_EXT, NULL); // External trigger |
---|
331 | //errs += ai32ssc1m_burst_sync(fd, -1, AI32SSC1M_BURST_SYNC_RBG, NULL); // External trigger |
---|
332 | |
---|
333 | // errs += ai32ssc1m_aux_clk_mode(fd,-1,AI32SSC1M_AUX_CLK_MODE_DISABLE,NULL); |
---|
334 | // errs += ai32ssc1m_aux_sync_mode(fd,-1,AI32SSC1M_AUX_SYNC_MODE_INPUT,NULL); |
---|
335 | //errs += ai32ssc1m_aux_sync_mode(fd,-1,AI32SSC1M_AUX_SYNC_MODE_DISABLE,NULL); |
---|
336 | errs += ai32ssc1m_aux_clk_mode(fd,-1,AI32SSC1M_AUX_CLK_MODE_DISABLE,NULL); |
---|
337 | errs += ai32ssc1m_burst_size(fd, -1, 1024, NULL); |
---|
338 | |
---|
339 | // errs += ai32ssc1m_burst_size(fd, -1, 16, NULL); // Burst size |
---|
340 | |
---|
341 | //Timeout (duration set above) |
---|
342 | errs += ai32ssc1m_rx_io_timeout(fd,-1, -1, timeout, NULL); |
---|
343 | |
---|
344 | errs += ai32ssc1m_rx_io_mode(fd, -1, io_mode, NULL); |
---|
345 | errs += _channels(fd, &chans); |
---|
346 | |
---|
347 | //Channels range |
---|
348 | errs += ai32ssc1m_chan_active (fd, -1, AI32SSC1M_CHAN_ACTIVE_RANGE, NULL); |
---|
349 | errs += ai32ssc1m_chan_first (fd, -1, 0, NULL); |
---|
350 | errs += ai32ssc1m_chan_last (fd, -1, 31, NULL); |
---|
351 | errs += ai32ssc1m_ain_range(fd, -1, AI32SSC1M_AIN_RANGE_5V, NULL); |
---|
352 | // errs += ai32ssc1m_ain_mode(fd, -1, -1, &timeout); |
---|
353 | errs += ai32ssc1m_ain_mode(fd, -1, AI32SSC1M_AIN_MODE_DIFF, NULL); |
---|
354 | // errs += ai32ssc1m_ain_mode(fd, -1, AI32SSC1M_AIN_MODE_VREF, NULL); |
---|
355 | sleep(4); |
---|
356 | gsc_label("1 second before data taking..."); |
---|
357 | sleep(1); |
---|
358 | printf("Now\n"); |
---|
359 | gsc_label_level_inc(); |
---|
360 | for(;;) { |
---|
361 | gsc_label("Acquiring"); |
---|
362 | errs += ai32ssc1m_ain_buf_clear(fd, -1); |
---|
363 | errs += _read_data(fd); |
---|
364 | errs += ai32ssc1m_ain_buf_overflow(fd, -1, -1, NULL); |
---|
365 | errs += ai32ssc1m_ain_buf_underflow(fd, -1, -1, NULL); |
---|
366 | // errs += ai32ssc1m_ain_buf_thr_lvl(fd, -1, -1, &timeout); |
---|
367 | //printf("thr lvl %d \n",timeout); |
---|
368 | errs += _save_data_with_datename(fd, chans, acquisition_rate, errs); |
---|
369 | printf("Done\n"); |
---|
370 | { |
---|
371 | filename="/home/etalon-admin/acquisition_data"; |
---|
372 | struct stat st; |
---|
373 | int result = stat(filename, &st); |
---|
374 | if (result == 0) { |
---|
375 | printf("Exists\n"); |
---|
376 | } else { |
---|
377 | printf("not Exists\n"); |
---|
378 | } |
---|
379 | } |
---|
380 | sleep(5); |
---|
381 | gsc_label_level_dec(); |
---|
382 | gsc_label_level_dec(); |
---|
383 | return(errs); |
---|
384 | } |
---|
385 | |
---|
386 | |
---|
387 | |
---|