1 | // $URL: http://subversion:8080/svn/gsc/trunk/drivers/LINUX/18AI32SSC1M/savedata/savedata.c $ |
---|
2 | // $Rev: 1895 $ |
---|
3 | // $Date: 2009-04-27 14:00:34 -0500 (Mon, 27 Apr 2009) $ |
---|
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 | * |
---|
29 | * Function: _channels |
---|
30 | * |
---|
31 | * Purpose: |
---|
32 | * |
---|
33 | * Check to see how many channels the board has. |
---|
34 | * |
---|
35 | * Arguments: |
---|
36 | * |
---|
37 | * fd The handle for the board to access. |
---|
38 | * |
---|
39 | * chan Report the number of channels here. |
---|
40 | * |
---|
41 | * Returned: |
---|
42 | * |
---|
43 | * >= 0 The number of errors encounterred. |
---|
44 | * |
---|
45 | ******************************************************************************/ |
---|
46 | |
---|
47 | static int _channels(int fd, __s32* chans) |
---|
48 | { |
---|
49 | __s32 active = -1; |
---|
50 | char buf[64]; |
---|
51 | int errs = 0; |
---|
52 | __s32 first = -1; |
---|
53 | __s32 last = -1; |
---|
54 | __s32 single = -1; |
---|
55 | |
---|
56 | gsc_label("Input Channels"); |
---|
57 | |
---|
58 | active = -1; |
---|
59 | errs += ai32ssc1m_dsl_ioctl(fd, AI32SSC1M_IOCTL_CHAN_ACTIVE, &active ); |
---|
60 | errs += ai32ssc1m_dsl_ioctl(fd, AI32SSC1M_IOCTL_CHAN_FIRST, &first ); |
---|
61 | errs += ai32ssc1m_dsl_ioctl(fd, AI32SSC1M_IOCTL_CHAN_LAST, &last ); |
---|
62 | errs += ai32ssc1m_dsl_ioctl(fd, AI32SSC1M_IOCTL_CHAN_SINGLE, &single ); |
---|
63 | |
---|
64 | if (errs == 0) |
---|
65 | { |
---|
66 | switch (active) |
---|
67 | { |
---|
68 | default: |
---|
69 | |
---|
70 | errs++; |
---|
71 | printf("FAIL <--- (invalid ACTIVE selection: %ld)\n", (long) active); |
---|
72 | break; |
---|
73 | |
---|
74 | case AI32SSC1M_CHAN_ACTIVE_SINGLE: |
---|
75 | |
---|
76 | chans[0] = 1; |
---|
77 | sprintf(buf, "# %ld", (long) single); |
---|
78 | break; |
---|
79 | |
---|
80 | case AI32SSC1M_CHAN_ACTIVE_0_1: |
---|
81 | |
---|
82 | chans[0] = 2; |
---|
83 | strcpy(buf, "#s 0-1"); |
---|
84 | break; |
---|
85 | |
---|
86 | case AI32SSC1M_CHAN_ACTIVE_0_3: |
---|
87 | |
---|
88 | chans[0] = 4; |
---|
89 | strcpy(buf, "#s 0-3"); |
---|
90 | break; |
---|
91 | |
---|
92 | case AI32SSC1M_CHAN_ACTIVE_0_7: |
---|
93 | |
---|
94 | chans[0] = 8; |
---|
95 | strcpy(buf, "#s 0-7"); |
---|
96 | break; |
---|
97 | |
---|
98 | case AI32SSC1M_CHAN_ACTIVE_0_15: |
---|
99 | |
---|
100 | chans[0] = 16; |
---|
101 | strcpy(buf, "#s 0-15"); |
---|
102 | break; |
---|
103 | |
---|
104 | case AI32SSC1M_CHAN_ACTIVE_0_31: |
---|
105 | |
---|
106 | chans[0] = 32; |
---|
107 | strcpy(buf, "#s 0-31"); |
---|
108 | break; |
---|
109 | |
---|
110 | case AI32SSC1M_CHAN_ACTIVE_RANGE: |
---|
111 | |
---|
112 | chans[0] = last - first + 1; |
---|
113 | |
---|
114 | if (first == last) |
---|
115 | sprintf(buf, "# %ld", (long) first); |
---|
116 | else |
---|
117 | sprintf(buf, "#s %ld-%ld", (long) first, (long) last); |
---|
118 | |
---|
119 | break; |
---|
120 | } |
---|
121 | |
---|
122 | if (errs == 0) |
---|
123 | { |
---|
124 | printf( "%ld Channel%s (%s)\n", |
---|
125 | (long) chans[0], |
---|
126 | (chans[0] == 1) ? "" : "s", |
---|
127 | buf); |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | return(errs); |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | |
---|
136 | /****************************************************************************** |
---|
137 | * |
---|
138 | * Function: _read_data |
---|
139 | * |
---|
140 | * Purpose: |
---|
141 | * |
---|
142 | * Read data into the buffer. |
---|
143 | * |
---|
144 | * Arguments: |
---|
145 | * |
---|
146 | * fd The handle for the board to access. |
---|
147 | * |
---|
148 | * Returned: |
---|
149 | * |
---|
150 | * >= 0 The number of errors encounterred. |
---|
151 | * |
---|
152 | ******************************************************************************/ |
---|
153 | |
---|
154 | static int _read_data(int fd) |
---|
155 | { |
---|
156 | int errs; |
---|
157 | long get = sizeof(_buffer) / 4; |
---|
158 | int got; |
---|
159 | |
---|
160 | gsc_label("Reading"); |
---|
161 | got = ai32ssc1m_dsl_read(fd, _buffer, get); |
---|
162 | |
---|
163 | if (got < 0) |
---|
164 | { |
---|
165 | errs = 1; |
---|
166 | } |
---|
167 | else if (got != get) |
---|
168 | { |
---|
169 | errs = 1; |
---|
170 | printf( "FAIL <--- (got %ld samples, requested %ld)\n", |
---|
171 | (long) got, |
---|
172 | (long) get); |
---|
173 | } |
---|
174 | else |
---|
175 | { |
---|
176 | errs = 0; |
---|
177 | printf( "PASS (%ld samples)\n", |
---|
178 | (long) get); |
---|
179 | } |
---|
180 | |
---|
181 | return(errs); |
---|
182 | } |
---|
183 | |
---|
184 | |
---|
185 | |
---|
186 | /****************************************************************************** |
---|
187 | * |
---|
188 | * Function: _save_data |
---|
189 | * |
---|
190 | * Purpose: |
---|
191 | * |
---|
192 | * Save the read data to a text file. |
---|
193 | * |
---|
194 | * Arguments: |
---|
195 | * |
---|
196 | * fd The handle for the board to access. |
---|
197 | * |
---|
198 | * chan The number of channels. |
---|
199 | * |
---|
200 | * errs have there been any errors so far? |
---|
201 | * |
---|
202 | * Returned: |
---|
203 | * |
---|
204 | * >= 0 The number of errors encounterred. |
---|
205 | * |
---|
206 | ******************************************************************************/ |
---|
207 | |
---|
208 | static int _save_data_hex(int fd, int chan, int errs,char* name) |
---|
209 | { |
---|
210 | FILE* file; |
---|
211 | int i; |
---|
212 | long l; |
---|
213 | long samples = sizeof(_buffer) / 4; |
---|
214 | |
---|
215 | gsc_label("Saving"); |
---|
216 | |
---|
217 | for (;;) |
---|
218 | { |
---|
219 | if (errs) |
---|
220 | { |
---|
221 | printf("SKIPPED (errors)\n"); |
---|
222 | errs = 0; |
---|
223 | break; |
---|
224 | } |
---|
225 | |
---|
226 | file = fopen(name, "w+b"); |
---|
227 | |
---|
228 | if (file == NULL) |
---|
229 | { |
---|
230 | printf("FAIL <--- (unable to create %s)\n", name); |
---|
231 | errs = 1; |
---|
232 | break; |
---|
233 | } |
---|
234 | |
---|
235 | for (l = 0; l < samples; l++) |
---|
236 | { |
---|
237 | if ((l) && (_buffer[l] & 0x80000000)) |
---|
238 | { |
---|
239 | i = fprintf(file, "\r\n"); |
---|
240 | |
---|
241 | if (i != 2) |
---|
242 | { |
---|
243 | printf("FAIL <--- (fprintf() failure to %s)\n", name);; |
---|
244 | errs = 1; |
---|
245 | break; |
---|
246 | } |
---|
247 | } |
---|
248 | |
---|
249 | i = fprintf(file, " %08lX", (long) _buffer[l]); |
---|
250 | |
---|
251 | if (i != 10) |
---|
252 | { |
---|
253 | printf("FAIL <--- (fprintf() failure to %s)\n", name); |
---|
254 | errs = 1; |
---|
255 | break; |
---|
256 | } |
---|
257 | } |
---|
258 | |
---|
259 | fclose(file); |
---|
260 | |
---|
261 | if (errs == 0) |
---|
262 | printf("PASS (%s)\n", name); |
---|
263 | |
---|
264 | break; |
---|
265 | } |
---|
266 | |
---|
267 | return(errs); |
---|
268 | } // _save_data_hex |
---|
269 | |
---|
270 | /****************************************************************************** |
---|
271 | * |
---|
272 | * Function: _save_data |
---|
273 | * |
---|
274 | * Purpose: |
---|
275 | * |
---|
276 | * Save the read data to a text file. |
---|
277 | * |
---|
278 | * Arguments: |
---|
279 | * |
---|
280 | * fd The handle for the board to access. |
---|
281 | * |
---|
282 | * chan The number of channels. |
---|
283 | * |
---|
284 | * errs have there been any errors so far? |
---|
285 | * |
---|
286 | * Returned: |
---|
287 | * |
---|
288 | * >= 0 The number of errors encounterred. |
---|
289 | * |
---|
290 | ******************************************************************************/ |
---|
291 | |
---|
292 | static int _save_data_dec(int fd, int chan, int errs,char* name) |
---|
293 | { |
---|
294 | FILE* file; |
---|
295 | int i; |
---|
296 | long l; |
---|
297 | long samples = sizeof(_buffer) / 4; |
---|
298 | |
---|
299 | gsc_label("Saving"); |
---|
300 | |
---|
301 | for (;;) |
---|
302 | { |
---|
303 | if (errs) |
---|
304 | { |
---|
305 | printf("SKIPPED (errors)\n"); |
---|
306 | errs = 0; |
---|
307 | break; |
---|
308 | } |
---|
309 | |
---|
310 | file = fopen(name, "w+b"); |
---|
311 | |
---|
312 | if (file == NULL) |
---|
313 | { |
---|
314 | printf("FAIL <--- (unable to create %s)\n", name); |
---|
315 | errs = 1; |
---|
316 | break; |
---|
317 | } |
---|
318 | |
---|
319 | for (l = 0; l < samples; l++) |
---|
320 | { |
---|
321 | if ((l) && (_buffer[l] & 0x80000000)) |
---|
322 | { |
---|
323 | i = fprintf(file, "\r\n"); |
---|
324 | |
---|
325 | if (i != 2) |
---|
326 | { |
---|
327 | printf("FAIL <--- (fprintf() failure to %s)\n", name);; |
---|
328 | errs = 1; |
---|
329 | break; |
---|
330 | } |
---|
331 | } |
---|
332 | |
---|
333 | if ((_buffer[l] & 0x80000000)) |
---|
334 | { |
---|
335 | i = fprintf(file, " %12ld", (long) ( _buffer[l] - 0x80000000) ); |
---|
336 | } else { |
---|
337 | i = fprintf(file, " %12ld", (long) _buffer[l]); |
---|
338 | } |
---|
339 | if (i < 5) |
---|
340 | { |
---|
341 | printf("FAIL <--- (fprintf() failure to %s)\n", name); |
---|
342 | errs = 1; |
---|
343 | break; |
---|
344 | } |
---|
345 | } |
---|
346 | |
---|
347 | fclose(file); |
---|
348 | |
---|
349 | if (errs == 0) |
---|
350 | printf("PASS (%s)\n", name); |
---|
351 | |
---|
352 | break; |
---|
353 | } |
---|
354 | |
---|
355 | return(errs); |
---|
356 | } // _save_data_dec |
---|
357 | |
---|
358 | |
---|
359 | /****************************************************************************** |
---|
360 | * |
---|
361 | * Function: _save_data |
---|
362 | * |
---|
363 | * Purpose: |
---|
364 | * |
---|
365 | * Save the read data to a text file. |
---|
366 | * |
---|
367 | * Arguments: |
---|
368 | * |
---|
369 | * fd The handle for the board to access. |
---|
370 | * |
---|
371 | * chan The number of channels. |
---|
372 | * |
---|
373 | * errs have there been any errors so far? |
---|
374 | * |
---|
375 | * Returned: |
---|
376 | * |
---|
377 | * >= 0 The number of errors encounterred. |
---|
378 | * |
---|
379 | ******************************************************************************/ |
---|
380 | |
---|
381 | static int _save_data(int fd, int chan, int errs) |
---|
382 | { |
---|
383 | char name_hex[50]; |
---|
384 | char name_dec[50]; |
---|
385 | |
---|
386 | time_t t = time(NULL); |
---|
387 | struct tm tm = *localtime(&t); |
---|
388 | |
---|
389 | sprintf(name_hex,"data/data_SPESO_%d-%02d-%02d_%02d-%02d-%02d_hex.txt", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); |
---|
390 | sprintf(name_dec,"data/data_SPESO_%d-%02d-%02d_%02d-%02d-%02d_dec.txt", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); |
---|
391 | |
---|
392 | errs +=_save_data_hex(fd, chan, errs, name_hex); |
---|
393 | errs +=_save_data_dec(fd, chan, errs, name_dec); |
---|
394 | |
---|
395 | return(errs); |
---|
396 | } // _save_data |
---|
397 | |
---|
398 | |
---|
399 | /****************************************************************************** |
---|
400 | * |
---|
401 | * Function: save_data |
---|
402 | * |
---|
403 | * Purpose: |
---|
404 | * |
---|
405 | * Configure the board, then capture data to a file. |
---|
406 | * |
---|
407 | * Arguments: |
---|
408 | * |
---|
409 | * fd The handle for the board to access. |
---|
410 | * |
---|
411 | * io_mode Use the I/O mode for data transfer. |
---|
412 | * |
---|
413 | * Returned: |
---|
414 | * |
---|
415 | * >= 0 The number of errors encounterred. |
---|
416 | * |
---|
417 | ******************************************************************************/ |
---|
418 | |
---|
419 | /* |
---|
420 | int save_data(int fd, __s32 io_mode) |
---|
421 | { |
---|
422 | __s32 chans = 32; |
---|
423 | int errs = 0; |
---|
424 | |
---|
425 | gsc_label("Capture & Save"); |
---|
426 | printf("\n"); |
---|
427 | gsc_label_level_inc(); |
---|
428 | |
---|
429 | errs += ai32ssc1m_config_basic(fd, -1, 1440); // 1440 ~= 25000 S/S/C |
---|
430 | errs += ai32ssc1m_rx_io_mode(fd, -1, io_mode, NULL); |
---|
431 | errs += _channels(fd, &chans); |
---|
432 | errs += _read_data(fd); |
---|
433 | errs += ai32ssc1m_ain_buf_overflow(fd, -1, -1, NULL); |
---|
434 | errs += ai32ssc1m_ain_buf_underflow(fd, -1, -1, NULL); |
---|
435 | errs += _save_data(fd, chans, errs); |
---|
436 | |
---|
437 | gsc_label_level_dec(); |
---|
438 | return(errs); |
---|
439 | } // save_data |
---|
440 | */ |
---|
441 | |
---|
442 | |
---|
443 | /****************************************************************************** |
---|
444 | * |
---|
445 | * Function: save_data_continuous |
---|
446 | * |
---|
447 | * Purpose: |
---|
448 | * |
---|
449 | * Configure the board, then capture data to a file. |
---|
450 | * |
---|
451 | * Arguments: |
---|
452 | * |
---|
453 | * fd The handle for the board to access. |
---|
454 | * |
---|
455 | * io_mode Use the I/O mode for data transfer. |
---|
456 | * |
---|
457 | * Returned: |
---|
458 | * |
---|
459 | * >= 0 The number of errors encounterred. |
---|
460 | * |
---|
461 | ******************************************************************************/ |
---|
462 | |
---|
463 | //int save_data_continuous(int fd, __s32 io_mode) |
---|
464 | int save_data(int fd, __s32 io_mode) |
---|
465 | { |
---|
466 | __s32 chans = 32; |
---|
467 | int errs = 0; |
---|
468 | |
---|
469 | //ND |
---|
470 | int index=-1; |
---|
471 | int max_rate=36000000;//Hz |
---|
472 | int my_rate=20000;//Hz |
---|
473 | int nrate=max_rate/my_rate; |
---|
474 | __s32 range = AI32SSC1M_AIN_RANGE_5V; |
---|
475 | //__s32 range = AI32SSC1M_AIN_RANGE_10V; |
---|
476 | |
---|
477 | gsc_label("Capture & Save"); |
---|
478 | gsc_label_level_inc(); |
---|
479 | |
---|
480 | gsc_label("At 1"); |
---|
481 | errs += ai32ssc1m_config_basic(fd, -1, 1440); // 1440 ~= 25000 S/S/C |
---|
482 | gsc_label("At 2"); |
---|
483 | printf("rate= %d Hz (factor= %d) \n",my_rate,nrate); |
---|
484 | errs += ai32ssc1m_rx_io_mode(fd, -1, io_mode, NULL); |
---|
485 | |
---|
486 | gsc_label("At 3"); |
---|
487 | //ND |
---|
488 | errs += ai32ssc1m_ain_range (fd, index, range, NULL); |
---|
489 | errs += ai32ssc1m_rag_nrate (fd, index, nrate, NULL); |
---|
490 | errs += ai32ssc1m_burst_sync (fd, index, AI32SSC1M_BURST_SYNC_EXT, NULL); |
---|
491 | |
---|
492 | // errs += ai32ssc1m_burst_sync (fd, index, AI32SSC1M_BURST_SYNC_DISABLE, NULL); |
---|
493 | errs += ai32ssc1m_burst_size (fd, index, 1024, NULL); |
---|
494 | // errs += ai32ssc1m_io_inv (fd, index, AI32SSC1M_IO_INV_HIGH,NULL); |
---|
495 | errs += ai32ssc1m_io_inv (fd, index, AI32SSC1M_IO_INV_LOW,NULL); |
---|
496 | |
---|
497 | gsc_label("At 4"); |
---|
498 | |
---|
499 | errs += ai32ssc1m_chan_active (fd, index, AI32SSC1M_CHAN_ACTIVE_RANGE, NULL); |
---|
500 | errs += ai32ssc1m_chan_first (fd, index, 26, NULL); |
---|
501 | errs += ai32ssc1m_chan_last (fd, index, 31, NULL); |
---|
502 | errs += ai32ssc1m_chan_active (fd, index, AI32SSC1M_CHAN_ACTIVE_RANGE, NULL); |
---|
503 | errs += ai32ssc1m_ain_buf_clear (fd, index); |
---|
504 | // errs += ai32ssc1m_input_sync (fd, index); |
---|
505 | // errs += ai32ssc1m_input_sync (fd, index); |
---|
506 | |
---|
507 | errs += _channels(fd, &chans); |
---|
508 | |
---|
509 | |
---|
510 | errs += _read_data(fd); |
---|
511 | errs += ai32ssc1m_ain_buf_overflow(fd, -1, -1, NULL); |
---|
512 | errs += ai32ssc1m_ain_buf_underflow(fd, -1, -1, NULL); |
---|
513 | errs += _save_data(fd, 31-26, errs); |
---|
514 | |
---|
515 | gsc_label_level_dec(); |
---|
516 | return(errs); |
---|
517 | } // save_data_continuous |
---|
518 | |
---|
519 | |
---|
520 | |
---|