Debian Codegraph @

comedi-calibrate/comedi_calibrate/cb.c

raw
1 /***************************************************************************
2 cb.c - calibration support for some Measurement computing boards.
3 -------------------
4
5 begin : Sat Apr 27 2002
6 copyright : (C) 2002,2003 by Frank Mori Hess
7 email : [email protected]
8
9 ***************************************************************************/
10
11 /***************************************************************************
12 * *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU Lesser General Public License as *
15 * published by *
16 * the Free Software Foundation; either version 2.1 of the License, or *
17 * (at your option) any later version. *
18 * *
19 ***************************************************************************/
20
21 #define _GNU_SOURCE
22
23 #include <stdio.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <getopt.h>
28 #include <ctype.h>
29 #include <math.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdint.h>
33 #include <assert.h>
34
35 #include "calib.h"
36
37
38 char cb_id[] = "$Id$";
39
40 struct board_struct{
41 char *name;
42 int status;
43 int (*setup)( calibration_setup_t *setup );
44 };
45
46 static int setup_cb_pci_1xxx( calibration_setup_t *setup );
47 static int setup_cb_pci_1602_16( calibration_setup_t *setup );
48
49 static int cal_cb_pci_1xxx( calibration_setup_t *setup );
50 static int cal_cb_pci_1602_16( calibration_setup_t *setup );
51
52 static int init_observables_1xxx( calibration_setup_t *setup );
53
54 static struct board_struct boards[]={
55 { "pci-das1000", STATUS_DONE, setup_cb_pci_1xxx },
56 { "pci-das1001", STATUS_GUESS, setup_cb_pci_1xxx },
57 { "pci-das1002", STATUS_GUESS, setup_cb_pci_1xxx },
58 { "pci-das1200", STATUS_DONE, setup_cb_pci_1xxx },
59 { "pci-das1200/jr", STATUS_GUESS, setup_cb_pci_1xxx },
60 { "pci-das1602/12", STATUS_GUESS, setup_cb_pci_1xxx },
61 { "pci-das1602/16", STATUS_DONE, setup_cb_pci_1602_16 },
62 { "pci-das1602/16/jr", STATUS_GUESS, setup_cb_pci_1602_16 },
63 };
64
65 static const int num_boards = ( sizeof(boards) / sizeof(boards[0]) );
66
67 enum calibration_source_1xxx
68 {
69 CS_1XXX_GROUND = 0,
70 CS_1XXX_7V = 1,
71 CS_1002_3500mV = 2,
72 CS_1002_1750mV = 3,
73 CS_1001_88600uV = 3,
74 CS_1XXX_875mV = 4,
75 CS_1XXX_8600uV = 5,
76 CS_1602_16_minus_10V = 5,
77 CS_1XXX_DAC0 = 6,
78 CS_1XXX_DAC1 = 7,
79 };
80 static inline int CS_1XXX_DAC( unsigned int channel )
81 {
82 if( channel )
83 return CS_1XXX_DAC1;
84 else
85 return CS_1XXX_DAC0;
86 }
87
88 int cb_setup( calibration_setup_t *setup, const char *device_name )
89 {
90 unsigned int i;
91
92 for( i = 0; i < num_boards; i++ )
93 {
94 if( !strcmp( device_name, boards[i].name ) )
95 {
96 setup->status = boards[i].status;
97 return boards[i].setup( setup );
98 break;
99 }
100 }
101 if( i == num_boards ) return -1;
102
103 return 0;
104 }
105
106 static int setup_cb_pci_1xxx( calibration_setup_t *setup )
107 {
108 int retval;
109 static const int caldac_subdev = 4;
110 static const int calpot_subdev = 5;
111
112 if( comedi_get_version_code( setup->dev ) <= COMEDI_VERSION_CODE( 0, 7, 66 ) )
113 {
114 DPRINT(0, "WARNING: you need comedi driver version 0.7.67 or later\n"
115 "for this calibration to work properly\n" );
116 }
117 //this larger delay is definitely needed by pci-das1000, possibly not for pci-das1200
118 setup->sv_settling_time_ns = 10000000;
119 retval = init_observables_1xxx( setup );
120 if( retval < 0 ) return retval;
121 setup_caldacs( setup, caldac_subdev );
122 setup_caldacs( setup, calpot_subdev );
123 setup->do_cal = cal_cb_pci_1xxx;
124 return 0;
125 }
126
127 static int setup_cb_pci_1602_16( calibration_setup_t *setup )
128 {
129 int retval;
130 static const int caldac_subdev = 4;
131 static const int calpot_subdev = 5;
132 static const int dac08_subdev = 6;
133
134 if( comedi_get_version_code( setup->dev ) <= COMEDI_VERSION_CODE( 0, 7, 66 ) )
135 {
136 DPRINT(0, "WARNING: you need comedi driver version 0.7.67 or later\n"
137 "for this calibration to work properly\n" );
138 }
139 setup->sv_settling_time_ns = 10000000;
140 setup->sv_order = 12;
141 retval = init_observables_1xxx( setup );
142
143 if( retval < 0 ) return retval;
144 setup_caldacs( setup, caldac_subdev );
145 setup_caldacs( setup, calpot_subdev );
146 setup_caldacs( setup, dac08_subdev );
147 setup->do_cal = cal_cb_pci_1602_16;
148 return 0;
149 }
150
151 static int ai_ground_observable_1xxx( const calibration_setup_t *setup,
152 unsigned int channel, unsigned int range )
153 {
154 return 2 * range;
155 }
156
157 static int ai_high_observable_1xxx( const calibration_setup_t *setup,
158 unsigned int channel, unsigned int range )
159 {
160 return ai_ground_observable_1xxx( setup, channel, range ) + 1;
161 }
162
163 static int ao_ground_observable_1xxx( const calibration_setup_t *setup,
164 unsigned int channel, unsigned int range )
165 {
166 int num_ai_ranges, num_ao_ranges;
167
168 num_ai_ranges = comedi_get_n_ranges( setup->dev, setup->ad_subdev, 0 );
169 assert( num_ai_ranges > 0 );
170 num_ao_ranges = comedi_get_n_ranges( setup->dev, setup->da_subdev, 0 );
171 assert( num_ao_ranges > 0 );
172
173 return 2 * num_ai_ranges + 2 * num_ao_ranges * channel + 2 * range;
174 }
175
176 static int ao_high_observable_1xxx( const calibration_setup_t *setup,
177 unsigned int channel, unsigned int range )
178 {
179 return ao_ground_observable_1xxx( setup, channel, range ) + 1;
180 }
181
182 static double ai_low_target_1xxx( calibration_setup_t *setup,
183 unsigned int range )
184 {
185 if( is_bipolar( setup->dev, setup->ad_subdev, 0, range ) )
186 return 0.0;
187 else
188 return very_low_target( setup->dev, setup->ad_subdev, 0, range );
189 }
190
191 static int source_eeprom_addr_1xxx( calibration_setup_t *setup, unsigned int range_index )
192 {
193 enum source_eeprom_addr
194 {
195 EEPROM_7V_CHAN = 0x80,
196 EEPROM_3500mV_CHAN = 0x84,
197 EEPROM_1750mV_CHAN = 0x88,
198 EEPROM_88600uV_CHAN_1001 = 0x88,
199 EEPROM_875mV_CHAN = 0x8c,
200 EEPROM_8600uV_CHAN = 0x90,
201 };
202 comedi_range *range;
203
204 range = comedi_get_range( setup->dev, setup->ad_subdev, 0, range_index );
205 if( range == NULL ) return -1;
206
207 if( range->max > 7.0 )
208 return EEPROM_7V_CHAN;
209 else if( range->max > 3.5 )
210 return EEPROM_3500mV_CHAN;
211 else if( range->max > 1.750 )
212 return EEPROM_1750mV_CHAN;
213 else if( range->max > 0.875 )
214 return EEPROM_875mV_CHAN;
215 else if( range->max > .0886 )
216 return EEPROM_88600uV_CHAN_1001;
217 else if( range->max > 0.0086 )
218 return EEPROM_8600uV_CHAN;
219
220 return -1;
221 }
222
223 static int ai_high_cal_source_1xxx( calibration_setup_t *setup, unsigned int range_index )
224 {
225 comedi_range *range;
226
227 range = comedi_get_range( setup->dev, setup->ad_subdev, 0, range_index );
228 if( range == NULL ) return -1;
229
230 if( range->max > 7.0 )
231 return CS_1XXX_7V;
232 else if( range->max > 3.5 )
233 return CS_1002_3500mV;
234 else if( range->max > 1.750 )
235 return CS_1002_1750mV;
236 else if( range->max > 0.875 )
237 return CS_1XXX_875mV;
238 else if( range->max > .0886 )
239 return CS_1001_88600uV;
240 else if( range->max > 0.0086 )
241 return CS_1XXX_8600uV;
242
243 return -1;
244 }
245
246 static int ao_set_high_target_1xxx( calibration_setup_t *setup, unsigned int obs,
247 unsigned int range_index )
248 {
249 double target;
250 comedi_range *range;
251
252 range = comedi_get_range( setup->dev, setup->da_subdev, 0, range_index );
253 if( range == NULL ) return -1;
254
255 target = range->max * 0.9;
256 set_target( setup, obs, target );
257 return 0;
258 }
259
260 static int init_observables_1xxx( calibration_setup_t *setup )
261 {
262 comedi_insn tmpl, po_tmpl;
263 observable *o;
264 int retval, range, num_ai_ranges, num_ao_ranges,
265 channel, num_channels;
266 float target;
267 int ai_for_ao_range;
268
269 setup->n_observables = 0;
270
271 memset( &tmpl, 0, sizeof(tmpl) );
272 tmpl.insn = INSN_READ;
273 tmpl.n = 1;
274 tmpl.subdev = setup->ad_subdev;
275
276 num_ai_ranges = comedi_get_n_ranges( setup->dev, setup->ad_subdev, 0 );
277 if( num_ai_ranges < 0 ) return -1;
278
279 for( range = 0; range < num_ai_ranges; range++ )
280 {
281 o = setup->observables + ai_ground_observable_1xxx( setup, 0, range );
282 o->reference_source = CS_1XXX_GROUND;
283 assert( o->name == NULL );
284 if( asprintf( &o->name, "calibration source %i, range %i, ground referenced",
285 o->reference_source, range ) < 0 )
286 {
287 errno = ENOMEM;
288 perror( NULL );
289 return -1;
290 }
291 o->observe_insn = tmpl;
292 o->observe_insn.chanspec = CR_PACK( 0, range, AREF_GROUND) |
293 CR_ALT_SOURCE | CR_ALT_FILTER;
294 o->target = ai_low_target_1xxx( setup, range );
295 setup->n_observables++;
296
297 o = setup->observables + ai_high_observable_1xxx( setup, 0, range );;
298 retval = ai_high_cal_source_1xxx( setup, range );
299 if( retval < 0 ) return -1;
300 o->reference_source = retval;
301 assert( o->name == NULL );
302 if( asprintf( &o->name, "calibration source %i, range %i, ground referenced",
303 o->reference_source, range ) < 0 )
304 {
305 errno = ENOMEM;
306 perror( NULL );
307 return -1;
308 }
309 o->observe_insn = tmpl;
310 o->observe_insn.chanspec = CR_PACK( 0, range, AREF_GROUND) |
311 CR_ALT_SOURCE | CR_ALT_FILTER;
312 retval = cb_actual_source_voltage( setup->dev, setup->eeprom_subdev,
313 source_eeprom_addr_1xxx( setup, range ), &target );
314 if( retval < 0 ) return -1;
315 o->target = target;
316 setup->n_observables++;
317 }
318
319 if( setup->da_subdev >= 0 )
320 {
321 num_channels = comedi_get_n_channels( setup->dev, setup->da_subdev );
322 if( num_channels < 0 ) return -1;
323
324 num_ao_ranges = comedi_get_n_ranges( setup->dev, setup->da_subdev, 0 );
325 if( num_ao_ranges < 0 ) return -1;
326
327 memset( &po_tmpl, 0, sizeof(po_tmpl) );
328 po_tmpl.insn = INSN_WRITE;
329 po_tmpl.n = 1;
330 po_tmpl.subdev = setup->da_subdev;
331
332 ai_for_ao_range = get_bipolar_lowgain( setup->dev, setup->ad_subdev );
333 if( ai_for_ao_range < 0 ) return -1;
334
335 for( range = 0; range < num_ao_ranges; range++ )
336 {
337 for( channel = 0; channel < num_channels; channel++ )
338 {
339 o = setup->observables + ao_ground_observable_1xxx( setup, channel, range );
340 o->reference_source = CS_1XXX_DAC( channel );
341 assert( o->name == NULL );
342 if( asprintf( &o->name, "DAC ground calibration source, ch %i, range %i",
343 channel, range ) < 0 )
344 {
345 errno = ENOMEM;
346 perror( NULL );
347 return -1;
348 }
349 o->preobserve_insn = po_tmpl;
350 o->preobserve_insn.chanspec = CR_PACK( channel, range, AREF_GROUND );
351 o->preobserve_insn.data = o->preobserve_data;
352 o->observe_insn = tmpl;
353 o->observe_insn.chanspec = CR_PACK( 0, ai_for_ao_range, AREF_GROUND) |
354 CR_ALT_SOURCE | CR_ALT_FILTER;
355 set_target( setup, ao_ground_observable_1xxx( setup, channel, range ), 0.0 );
356 setup->n_observables++;
357
358 o = setup->observables + ao_high_observable_1xxx( setup, channel, range );
359 o->reference_source = CS_1XXX_DAC( channel );
360 assert( o->name == NULL );
361 if( asprintf( &o->name, "DAC high calibration source, ch %i, range %i", channel,
362 range ) < 0 )
363 {
364 errno = ENOMEM;
365 perror( NULL );
366 return -1;
367 }
368 o->preobserve_insn = po_tmpl;
369 o->preobserve_insn.chanspec = CR_PACK( channel , range, AREF_GROUND );
370 o->preobserve_insn.data = o->preobserve_data;
371 o->observe_insn = tmpl;
372 o->observe_insn.chanspec = CR_PACK( 0, ai_for_ao_range, AREF_GROUND) |
373 CR_ALT_SOURCE | CR_ALT_FILTER;
374 ao_set_high_target_1xxx( setup, ao_high_observable_1xxx( setup, channel, range ),
375 range );
376 setup->n_observables++;
377 }
378 }
379 }
380
381 return 0;
382 }
383
384 enum cal_knobs_1xxx
385 {
386 DAC0_GAIN_FINE_1XXX = 0,
387 DAC0_GAIN_COARSE_1XXX = 1,
388 DAC0_OFFSET_1XXX = 2,
389 DAC1_OFFSET_1XXX = 3,
390 DAC1_GAIN_FINE_1XXX = 4,
391 DAC1_GAIN_COARSE_1XXX = 5,
392 ADC_OFFSET_COARSE_1XXX = 6,
393 ADC_OFFSET_FINE_1XXX = 7,
394 ADC_GAIN_1XXX = 8,
395 };
396 static int adc_offset_coarse_1xxx( unsigned int channel )
397 {
398 return ADC_OFFSET_COARSE_1XXX;
399 }
400 static int adc_offset_fine_1xxx( unsigned int channel )
401 {
402 return ADC_OFFSET_FINE_1XXX;
403 }
404 static int adc_gain_1xxx( unsigned int channel )
405 {
406 return ADC_GAIN_1XXX;
407 }
408 static int dac_offset_1xxx( unsigned int channel )
409 {
410 if( channel )
411 return DAC1_OFFSET_1XXX;
412 else
413 return DAC0_OFFSET_1XXX;
414 }
415 static int dac_gain_fine_1xxx( unsigned int channel )
416 {
417 if( channel )
418 return DAC1_GAIN_FINE_1XXX;
419 else
420 return DAC0_GAIN_FINE_1XXX;
421 }
422 static int dac_gain_coarse_1xxx( unsigned int channel )
423 {
424 if( channel )
425 return DAC1_GAIN_COARSE_1XXX;
426 else
427 return DAC0_GAIN_COARSE_1XXX;
428 }
429
430 static int cal_cb_pci_1xxx( calibration_setup_t *setup )
431 {
432 generic_layout_t layout;
433
434 init_generic_layout( &layout );
435 layout.adc_gain = adc_gain_1xxx;
436 layout.adc_offset = adc_offset_coarse_1xxx;
437 layout.adc_offset_fine = adc_offset_fine_1xxx;
438 layout.dac_gain = dac_gain_coarse_1xxx;
439 layout.dac_gain_fine = dac_gain_fine_1xxx;
440 layout.dac_offset = dac_offset_1xxx;
441 layout.adc_high_observable = ai_high_observable_1xxx;
442 layout.adc_ground_observable = ai_ground_observable_1xxx;
443 layout.dac_high_observable = ao_high_observable_1xxx;
444 layout.dac_ground_observable = ao_ground_observable_1xxx;
445 layout.adc_fractional_tolerance = get_tolerance( setup, setup->ad_subdev, 1.0 );
446 layout.dac_fractional_tolerance = get_tolerance( setup, setup->da_subdev, 1.0 );
447 return generic_cal_by_range( setup, &layout );
448 }
449
450 enum cal_knobs_1602_16
451 {
452 DAC0_GAIN_FINE_1602_16 = 0,
453 DAC0_GAIN_COARSE_1602_16 = 1,
454 DAC0_OFFSET_COARSE_1602_16 = 2,
455 DAC1_OFFSET_COARSE_1602_16 = 3,
456 DAC1_GAIN_FINE_1602_16 = 4,
457 DAC1_GAIN_COARSE_1602_16 = 5,
458 DAC0_OFFSET_FINE_1602_16 = 6,
459 DAC1_OFFSET_FINE_1602_16 = 7,
460 ADC_GAIN_1602_16 = 8,
461 ADC_POSTGAIN_OFFSET_1602_16 = 9,
462 ADC_PREGAIN_OFFSET_1602_16 = 10,
463 };
464 static int dac_gain_coarse_1602_16( unsigned int channel )
465 {
466 if( channel ) return DAC1_GAIN_COARSE_1602_16;
467 else return DAC0_GAIN_COARSE_1602_16;
468 }
469 static int dac_gain_fine_1602_16( unsigned int channel )
470 {
471 if( channel ) return DAC1_GAIN_FINE_1602_16;
472 else return DAC0_GAIN_FINE_1602_16;
473 }
474 static int dac_offset_coarse_1602_16( unsigned int channel )
475 {
476 if( channel ) return DAC1_OFFSET_COARSE_1602_16;
477 else return DAC0_OFFSET_COARSE_1602_16;
478 }
479 static int dac_offset_fine_1602_16( unsigned int channel )
480 {
481 if( channel ) return DAC1_OFFSET_FINE_1602_16;
482 else return DAC0_OFFSET_FINE_1602_16;
483 }
484 static int adc_gain_1602_16( unsigned int channel )
485 {
486 return ADC_GAIN_1602_16;
487 }
488 static int adc_pregain_offset_1602_16( unsigned int channel )
489 {
490 return ADC_PREGAIN_OFFSET_1602_16;
491 }
492 static int adc_postgain_offset_1602_16( unsigned int channel )
493 {
494 return ADC_POSTGAIN_OFFSET_1602_16;
495 }
496 static int cal_cb_pci_1602_16( calibration_setup_t *setup )
497 {
498 generic_layout_t layout;
499
500 init_generic_layout( &layout );
501 layout.adc_gain = adc_gain_1602_16;
502 layout.adc_offset = adc_pregain_offset_1602_16;
503 layout.adc_postgain_offset = adc_postgain_offset_1602_16;
504 layout.dac_gain = dac_gain_coarse_1602_16;
505 layout.dac_gain_fine = dac_gain_fine_1602_16;
506 layout.dac_offset = dac_offset_coarse_1602_16;
507 layout.dac_offset_fine = dac_offset_fine_1602_16;
508 layout.adc_high_observable = ai_high_observable_1xxx;
509 layout.adc_ground_observable = ai_ground_observable_1xxx;
510 layout.dac_high_observable = ao_high_observable_1xxx;
511 layout.dac_ground_observable = ao_ground_observable_1xxx;
512 layout.adc_fractional_tolerance = get_tolerance( setup, setup->ad_subdev, 1.0 );
513 layout.dac_fractional_tolerance = get_tolerance( setup, setup->da_subdev, 1.0 );
514 /* The bipolar postgain calibration should be good for both
515 * bipolar and unipolar ranges, so disable separate
516 * unipolar postgain offset calibration (it will fail
517 * horribly anyways if you try it).
518 */
519 layout.do_adc_unipolar_postgain = 0;
520 return generic_cal_by_range( setup, &layout );
521 }
522
523 // converts calibration source voltages from two 16 bit eeprom values to a floating point value
524 static float eeprom16_to_source( uint16_t *data )
525 {
526 union translator
527 {
528 uint32_t bits;
529 float value;
530 };
531
532 union translator my_translator;
533
534 my_translator.bits = ( data[ 0 ] & 0xffff ) | ( ( data[ 1 ] << 16 ) & 0xffff0000 );
535
536 return my_translator.value;
537 }
538
539 static float eeprom8_to_source( uint8_t *data )
540 {
541 union translator
542 {
543 uint32_t bits;
544 float value;
545 };
546 union translator my_translator;
547 int i;
548
549 my_translator.bits = 0;
550 for( i = 0; i < 4; i++ )
551 {
552 my_translator.bits |= ( data[ i ] & 0xffff ) << ( 8 * i );
553 }
554
555 return my_translator.value;
556 }
557
558 int cb_actual_source_voltage( comedi_t *dev, unsigned int subdevice, unsigned int eeprom_channel, float *voltage)
559 {
560 int retval;
561 unsigned int i;
562 lsampl_t data;
563 int max_data;
564
565 max_data = comedi_get_maxdata( dev, subdevice, eeprom_channel );
566
567 if( max_data == 0xffff )
568 {
569 uint16_t word[ 2 ];
570
571 for( i = 0; i < 2; i++ )
572 {
573 retval = comedi_data_read( dev, subdevice, eeprom_channel + i, 0, 0, &data );
574 if( retval < 0 )
575 {
576 comedi_perror( __FUNCTION__ );
577 return retval;
578 }
579 word[ i ] = data;
580 }
581 *voltage = eeprom16_to_source( word );
582 }else if( max_data == 0xff )
583 {
584 uint8_t byte[ 4 ];
585
586 for( i = 0; i < 4; i++ )
587 {
588 retval = comedi_data_read( dev, subdevice, eeprom_channel + i, 0, 0, &data );
589 if( retval < 0 )
590 {
591 comedi_perror( __FUNCTION__ );
592 return retval;
593 }
594 byte[ i ] = data;
595 }
596 *voltage = eeprom8_to_source( byte );
597 }else
598 {
599 fprintf( stderr, "%s: maxdata = 0x%x invalid for subdevice %i, channel %i\n",
600 __FUNCTION__, max_data, subdevice, eeprom_channel);
601 return -1;
602 }
603
604 DPRINT(1, "eeprom ch 0x%x gives calibration source of %gV\n", eeprom_channel, *voltage);
605 return 0;
606 }