| 1 |
/*************************************************************************** |
| 2 |
cb64.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 cb64_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_64xx( calibration_setup_t *setup ); |
| 47 |
static int setup_cb_pci_60xx( calibration_setup_t *setup ); |
| 48 |
static int setup_cb_pci_603x( calibration_setup_t *setup ); |
| 49 |
static int setup_cb_pci_6052( calibration_setup_t *setup ); |
| 50 |
static int setup_cb_pci_4020( calibration_setup_t *setup ); |
| 51 |
#ifdef notused |
| 52 |
static int setup_cb_pci_unknown( calibration_setup_t *setup ); |
| 53 |
#endif |
| 54 |
|
| 55 |
static int cal_cb_pci_64xx( calibration_setup_t *setup ); |
| 56 |
static int cal_cb_pci_60xx( calibration_setup_t *setup ); |
| 57 |
static int cal_cb_pci_6052( calibration_setup_t *setup ); |
| 58 |
static int cal_cb_pci_4020( calibration_setup_t *setup ); |
| 59 |
|
| 60 |
static int init_observables_64xx( calibration_setup_t *setup ); |
| 61 |
static int init_observables_60xx( calibration_setup_t *setup ); |
| 62 |
static int init_observables_4020( calibration_setup_t *setup ); |
| 63 |
#ifdef notused |
| 64 |
static int init_observables_unknown( calibration_setup_t *setup ); |
| 65 |
#endif |
| 66 |
|
| 67 |
static struct board_struct boards[]={ |
| 68 |
{ "pci-das6402/16", STATUS_SOME, setup_cb_pci_64xx }, |
| 69 |
{ "pci-das6402/12", STATUS_GUESS, setup_cb_pci_64xx }, |
| 70 |
{ "pci-das64/m1/16", STATUS_GUESS, setup_cb_pci_64xx }, |
| 71 |
{ "pci-das64/m2/16", STATUS_GUESS, setup_cb_pci_64xx }, |
| 72 |
{ "pci-das64/m3/16", STATUS_GUESS, setup_cb_pci_64xx }, |
| 73 |
{ "pci-das6014", STATUS_DONE, setup_cb_pci_60xx }, |
| 74 |
{ "pci-das6023", STATUS_DONE, setup_cb_pci_60xx }, |
| 75 |
{ "pci-das6025", STATUS_DONE, setup_cb_pci_60xx }, |
| 76 |
{ "pci-das6030", STATUS_GUESS, setup_cb_pci_60xx }, |
| 77 |
{ "pci-das6031", STATUS_GUESS, setup_cb_pci_603x }, |
| 78 |
{ "pci-das6032", STATUS_GUESS, setup_cb_pci_603x }, |
| 79 |
{ "pci-das6033", STATUS_GUESS, setup_cb_pci_603x }, |
| 80 |
{ "pci-das6034", STATUS_DONE, setup_cb_pci_603x }, |
| 81 |
{ "pci-das6035", STATUS_GUESS, setup_cb_pci_603x }, |
| 82 |
{ "pci-das6036", STATUS_GUESS, setup_cb_pci_603x }, |
| 83 |
{ "pci-das6040", STATUS_GUESS, setup_cb_pci_60xx }, |
| 84 |
{ "pci-das6052", STATUS_GUESS, setup_cb_pci_6052 }, |
| 85 |
{ "pci-das6070", STATUS_GUESS, setup_cb_pci_60xx }, |
| 86 |
{ "pci-das6071", STATUS_GUESS, setup_cb_pci_60xx }, |
| 87 |
{ "pci-das4020/12", STATUS_DONE, setup_cb_pci_4020 }, |
| 88 |
}; |
| 89 |
|
| 90 |
static const int num_boards = ( sizeof(boards) / sizeof(boards[0]) ); |
| 91 |
|
| 92 |
enum calibration_source_60xx |
| 93 |
{ |
| 94 |
CS_60XX_GROUND = 0, |
| 95 |
CS_60XX_10V = 1, |
| 96 |
CS_60XX_5V = 2, |
| 97 |
CS_60XX_500mV = 3, |
| 98 |
CS_60XX_50mV = 4, |
| 99 |
CS_60XX_UNUSED = 5, // 0V |
| 100 |
CS_60XX_DAC0 = 6, |
| 101 |
CS_60XX_DAC1 = 7, |
| 102 |
}; |
| 103 |
|
| 104 |
enum calibration_source_64xx |
| 105 |
{ |
| 106 |
CS_64XX_GROUND = 0, |
| 107 |
CS_64XX_7V = 1, |
| 108 |
CS_64XX_3500mV = 2, |
| 109 |
CS_64XX_1750mV = 3, |
| 110 |
CS_64XX_875mV = 4, |
| 111 |
CS_64XX_8600uV = 5, |
| 112 |
CS_64XX_DAC0 = 6, |
| 113 |
CS_64XX_DAC1 = 7, |
| 114 |
}; |
| 115 |
|
| 116 |
enum calibration_source_4020 |
| 117 |
{ |
| 118 |
CS_4020_4375mV = 5, |
| 119 |
CS_4020_625mV = 6, |
| 120 |
CS_4020_GROUND = 7, |
| 121 |
}; |
| 122 |
|
| 123 |
enum cal_knobs_60xx |
| 124 |
{ |
| 125 |
DAC0_OFFSET_60XX = 0, |
| 126 |
DAC0_GAIN_60XX = 1, |
| 127 |
DAC1_OFFSET_60XX = 2, |
| 128 |
DAC1_GAIN_60XX = 3, |
| 129 |
ADC_OFFSET_FINE_60XX = 4, |
| 130 |
ADC_OFFSET_COARSE_60XX = 5, |
| 131 |
ADC_GAIN_COARSE_60XX = 6, |
| 132 |
ADC_GAIN_FINE_60XX = 7, |
| 133 |
}; |
| 134 |
|
| 135 |
enum cal_knobs_64xx |
| 136 |
{ |
| 137 |
DAC0_GAIN_FINE_64XX = 0, |
| 138 |
DAC0_GAIN_COARSE_64XX = 1, |
| 139 |
DAC0_OFFSET_COARSE_64XX = 2, |
| 140 |
DAC1_OFFSET_COARSE_64XX = 3, |
| 141 |
DAC1_GAIN_FINE_64XX = 4, |
| 142 |
DAC1_GAIN_COARSE_64XX = 5, |
| 143 |
DAC0_OFFSET_FINE_64XX = 6, |
| 144 |
DAC1_OFFSET_FINE_64XX = 7, |
| 145 |
ADC_GAIN_64XX = 8, |
| 146 |
ADC_OFFSET_64XX = 9, |
| 147 |
}; |
| 148 |
|
| 149 |
enum cal_knobs_6052 // FIXME these values are uneducated guesses |
| 150 |
{ |
| 151 |
DAC0_GAIN_FINE_6052 = 0, |
| 152 |
DAC0_GAIN_COARSE_6052 = 1, |
| 153 |
DAC0_OFFSET_COARSE_6052 = 2, |
| 154 |
DAC1_OFFSET_COARSE_6052 = 3, |
| 155 |
DAC1_GAIN_FINE_6052 = 4, |
| 156 |
DAC1_GAIN_COARSE_6052 = 5, |
| 157 |
DAC0_OFFSET_FINE_6052 = 6, |
| 158 |
DAC1_OFFSET_FINE_6052 = 7, |
| 159 |
ADC_GAIN_6052 = 8, |
| 160 |
ADC_OFFSET_6052 = 9, |
| 161 |
}; |
| 162 |
|
| 163 |
int cb64_setup( calibration_setup_t *setup, const char *device_name ) |
| 164 |
{ |
| 165 |
unsigned int i; |
| 166 |
|
| 167 |
for( i = 0; i < num_boards; i++ ) |
| 168 |
{ |
| 169 |
if( !strcmp( device_name, boards[i].name ) ) |
| 170 |
{ |
| 171 |
setup->status = boards[i].status; |
| 172 |
return boards[i].setup( setup ); |
| 173 |
break; |
| 174 |
} |
| 175 |
} |
| 176 |
if( i == num_boards ) return -1; |
| 177 |
|
| 178 |
return 0; |
| 179 |
} |
| 180 |
|
| 181 |
static int setup_cb_pci_64xx( calibration_setup_t *setup ) |
| 182 |
{ |
| 183 |
static const int caldac_subdev = 6; |
| 184 |
static const int calpot_subdev = 7; |
| 185 |
|
| 186 |
init_observables_64xx( setup ); |
| 187 |
setup_caldacs( setup, caldac_subdev ); |
| 188 |
setup_caldacs( setup, calpot_subdev ); |
| 189 |
setup->do_cal = cal_cb_pci_64xx; |
| 190 |
return 0; |
| 191 |
} |
| 192 |
|
| 193 |
static int setup_cb_pci_60xx( calibration_setup_t *setup ) |
| 194 |
{ |
| 195 |
static const int caldac_subdev = 6; |
| 196 |
init_observables_60xx( setup ); |
| 197 |
setup_caldacs( setup, caldac_subdev ); |
| 198 |
setup->do_cal = cal_cb_pci_60xx; |
| 199 |
return 0; |
| 200 |
} |
| 201 |
|
| 202 |
static int setup_cb_pci_603x(calibration_setup_t *setup) |
| 203 |
{ |
| 204 |
int retval = setup_cb_pci_60xx(setup); |
| 205 |
//extra delay not needed by 6023 and 6024, but is needed by 6034 |
| 206 |
setup->sv_settling_time_ns = 100000000; |
| 207 |
return retval; |
| 208 |
} |
| 209 |
|
| 210 |
static int setup_cb_pci_4020( calibration_setup_t *setup ) |
| 211 |
{ |
| 212 |
static const int caldac_subdev = 6; |
| 213 |
|
| 214 |
init_observables_4020( setup ); |
| 215 |
setup_caldacs( setup, caldac_subdev ); |
| 216 |
setup->do_cal = cal_cb_pci_4020; |
| 217 |
return 0; |
| 218 |
} |
| 219 |
|
| 220 |
#ifdef notused |
| 221 |
static int setup_cb_pci_unknown( calibration_setup_t *setup ) |
| 222 |
{ |
| 223 |
static const int caldac_subdev = 6; |
| 224 |
init_observables_unknown( setup ); |
| 225 |
setup_caldacs( setup, caldac_subdev ); |
| 226 |
setup->do_cal = 0; |
| 227 |
return 0; |
| 228 |
} |
| 229 |
#endif |
| 230 |
|
| 231 |
static int ai_ground_observable_index_64xx( const calibration_setup_t *setup, |
| 232 |
unsigned int channel, unsigned int range ) |
| 233 |
{ |
| 234 |
return 2 * range; |
| 235 |
} |
| 236 |
|
| 237 |
static int ai_high_observable_index_64xx( const calibration_setup_t *setup, |
| 238 |
unsigned int channel, unsigned int range ) |
| 239 |
{ |
| 240 |
return 2 * range + 1; |
| 241 |
} |
| 242 |
|
| 243 |
static int ao_ground_observable_index_64xx( const calibration_setup_t *setup, |
| 244 |
unsigned int channel, unsigned int ao_range ) |
| 245 |
{ |
| 246 |
int num_ai_ranges; |
| 247 |
|
| 248 |
num_ai_ranges = comedi_get_n_ranges( setup->dev, setup->ad_subdev, 0 ); |
| 249 |
assert( num_ai_ranges >= 0 ); |
| 250 |
|
| 251 |
return 2 * num_ai_ranges + 2 * channel + 4 * ao_range; |
| 252 |
} |
| 253 |
|
| 254 |
static int ao_high_observable_index_64xx( const calibration_setup_t *setup, |
| 255 |
unsigned int channel, unsigned int ao_range ) |
| 256 |
{ |
| 257 |
int num_ai_ranges; |
| 258 |
|
| 259 |
num_ai_ranges = comedi_get_n_ranges( setup->dev, setup->ad_subdev, 0 ); |
| 260 |
assert( num_ai_ranges >= 0 ); |
| 261 |
|
| 262 |
return 2 * num_ai_ranges + 2 * channel + 4 * ao_range + 1; |
| 263 |
} |
| 264 |
|
| 265 |
static int source_eeprom_addr_64xx( calibration_setup_t *setup, unsigned int range_index ) |
| 266 |
{ |
| 267 |
enum source_eeprom_addr |
| 268 |
{ |
| 269 |
EEPROM_7V_CHAN = 0x30, |
| 270 |
EEPROM_3500mV_CHAN = 0x32, |
| 271 |
EEPROM_1750mV_CHAN = 0x34, |
| 272 |
EEPROM_875mV_CHAN = 0x36, |
| 273 |
EEPROM_8600uV_CHAN = 0x38, |
| 274 |
}; |
| 275 |
comedi_range *range; |
| 276 |
|
| 277 |
range = comedi_get_range( setup->dev, setup->ad_subdev, 0, range_index ); |
| 278 |
if( range == NULL ) |
| 279 |
{ |
| 280 |
fprintf(stderr, "%s: failed to get range\n", __FUNCTION__); |
| 281 |
return -1; |
| 282 |
} |
| 283 |
if( range->max > 7.0 ) |
| 284 |
return EEPROM_7V_CHAN; |
| 285 |
else if( range->max > 3.5 ) |
| 286 |
return EEPROM_3500mV_CHAN; |
| 287 |
else if( range->max > 0.875 ) |
| 288 |
return EEPROM_875mV_CHAN; |
| 289 |
else if( range->max > 0.0086 ) |
| 290 |
return EEPROM_8600uV_CHAN; |
| 291 |
|
| 292 |
return -1; |
| 293 |
} |
| 294 |
|
| 295 |
static int ai_low_cal_source_64xx( calibration_setup_t *setup, unsigned int range_index ) |
| 296 |
{ |
| 297 |
comedi_range *range; |
| 298 |
|
| 299 |
range = comedi_get_range( setup->dev, setup->ad_subdev, 0, range_index ); |
| 300 |
if( range == NULL ) |
| 301 |
{ |
| 302 |
fprintf(stderr, "%s: failed to get range\n", __FUNCTION__); |
| 303 |
return -1; |
| 304 |
} |
| 305 |
if( range->min > -0.001 ) |
| 306 |
return CS_64XX_8600uV; |
| 307 |
else |
| 308 |
return CS_64XX_GROUND; |
| 309 |
} |
| 310 |
|
| 311 |
static int ai_high_cal_source_64xx( calibration_setup_t *setup, unsigned int range_index ) |
| 312 |
{ |
| 313 |
comedi_range *range; |
| 314 |
|
| 315 |
range = comedi_get_range( setup->dev, setup->ad_subdev, 0, range_index ); |
| 316 |
if( range == NULL ) |
| 317 |
{ |
| 318 |
fprintf(stderr, "%s: failed to get range\n", __FUNCTION__); |
| 319 |
return -1; |
| 320 |
} |
| 321 |
if( range->max > 7.0 ) |
| 322 |
return CS_64XX_7V; |
| 323 |
else if( range->max > 3.5 ) |
| 324 |
return CS_64XX_3500mV; |
| 325 |
else if( range->max > 0.875 ) |
| 326 |
return CS_64XX_875mV; |
| 327 |
else if( range->max > 0.0086 ) |
| 328 |
return CS_64XX_8600uV; |
| 329 |
|
| 330 |
return -1; |
| 331 |
} |
| 332 |
|
| 333 |
static int ao_cal_src_64xx( unsigned int channel ) |
| 334 |
{ |
| 335 |
switch( channel ) |
| 336 |
{ |
| 337 |
case 0: |
| 338 |
return CS_64XX_DAC0; |
| 339 |
break; |
| 340 |
case 1: |
| 341 |
return CS_64XX_DAC1; |
| 342 |
break; |
| 343 |
default: |
| 344 |
return -1; |
| 345 |
break; |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
static int ao_set_high_target_64xx( calibration_setup_t *setup, unsigned int obs, |
| 350 |
unsigned int range_index ) |
| 351 |
{ |
| 352 |
double target; |
| 353 |
comedi_range *range; |
| 354 |
|
| 355 |
range = comedi_get_range( setup->dev, setup->da_subdev, 0, range_index ); |
| 356 |
if( range == NULL ) |
| 357 |
{ |
| 358 |
fprintf(stderr, "%s: failed to get range\n", __FUNCTION__); |
| 359 |
return -1; |
| 360 |
} |
| 361 |
target = range->max * 0.9; |
| 362 |
set_target( setup, obs, target ); |
| 363 |
return 0; |
| 364 |
} |
| 365 |
|
| 366 |
static int init_observables_64xx( calibration_setup_t *setup ) |
| 367 |
{ |
| 368 |
comedi_insn tmpl; |
| 369 |
observable *o; |
| 370 |
int retval; |
| 371 |
float target; |
| 372 |
int range, num_ai_ranges, num_ao_ranges; |
| 373 |
|
| 374 |
memset( &tmpl, 0, sizeof(tmpl) ); |
| 375 |
tmpl.insn = INSN_READ; |
| 376 |
tmpl.n = 1; |
| 377 |
tmpl.subdev = setup->ad_subdev; |
| 378 |
|
| 379 |
num_ai_ranges = comedi_get_n_ranges( setup->dev, setup->ad_subdev, 0 ); |
| 380 |
if( num_ai_ranges < 0 ) |
| 381 |
{ |
| 382 |
fprintf(stderr, "%s: failed to get number of ai ranges\n", __FUNCTION__); |
| 383 |
return -1; |
| 384 |
} |
| 385 |
setup->n_observables = 0; |
| 386 |
|
| 387 |
for( range = 0; range < num_ai_ranges; range++ ) |
| 388 |
{ |
| 389 |
o = setup->observables + ai_ground_observable_index_64xx( setup, 0, range ); |
| 390 |
retval = ai_low_cal_source_64xx( setup, range ); |
| 391 |
if( retval < 0 ) return -1; |
| 392 |
o->reference_source = retval; |
| 393 |
assert( o->name == NULL ); |
| 394 |
if( asprintf( &o->name, "calibration source %i, range %i, ground referenced", |
| 395 |
o->reference_source, range ) < 0 ) |
| 396 |
{ |
| 397 |
errno = ENOMEM; |
| 398 |
perror( NULL ); |
| 399 |
return -1; |
| 400 |
} |
| 401 |
o->observe_insn = tmpl; |
| 402 |
o->observe_insn.chanspec = CR_PACK( 0, range, AREF_GROUND) | CR_ALT_SOURCE | CR_ALT_FILTER; |
| 403 |
o->target = 0.0; |
| 404 |
setup->n_observables++; |
| 405 |
|
| 406 |
o = setup->observables + ai_high_observable_index_64xx( setup, 0, range ); |
| 407 |
retval = ai_high_cal_source_64xx( setup, range ); |
| 408 |
if( retval < 0 ) return -1; |
| 409 |
o->reference_source = retval; |
| 410 |
assert( o->name == NULL ); |
| 411 |
if( asprintf( &o->name, "calibration source %i, range %i, ground referenced", |
| 412 |
o->reference_source, range ) < 0 ) |
| 413 |
{ |
| 414 |
errno = ENOMEM; |
| 415 |
perror( NULL ); |
| 416 |
return -1; |
| 417 |
} |
| 418 |
o->name = "calibration source, 10V bipolar range, ground referenced"; |
| 419 |
o->observe_insn = tmpl; |
| 420 |
o->observe_insn.chanspec = CR_PACK( 0, range, AREF_GROUND) | CR_ALT_SOURCE | CR_ALT_FILTER; |
| 421 |
retval = cb_actual_source_voltage( setup->dev, setup->eeprom_subdev, |
| 422 |
source_eeprom_addr_64xx( setup, range ), &target ); |
| 423 |
if( retval < 0 ) return -1; |
| 424 |
o->target = target; |
| 425 |
setup->n_observables++; |
| 426 |
} |
| 427 |
if( setup->da_subdev >= 0 ) |
| 428 |
{ |
| 429 |
comedi_insn po_tmpl; |
| 430 |
|
| 431 |
memset( &po_tmpl, 0, sizeof(po_tmpl) ); |
| 432 |
po_tmpl.insn = INSN_WRITE; |
| 433 |
po_tmpl.n = 1; |
| 434 |
po_tmpl.subdev = setup->da_subdev; |
| 435 |
|
| 436 |
num_ao_ranges = comedi_get_n_ranges( setup->dev, setup->da_subdev, 0 ); |
| 437 |
if( num_ao_ranges < 0 ) |
| 438 |
{ |
| 439 |
fprintf(stderr, "%s: failed to get number of ao ranges\n", __FUNCTION__); |
| 440 |
return -1; |
| 441 |
} |
| 442 |
|
| 443 |
for( range = 0; range < num_ao_ranges; range++ ) |
| 444 |
{ |
| 445 |
int dac_chan, ai_range; |
| 446 |
ai_range = get_bipolar_lowgain( setup->dev, setup->ad_subdev ); |
| 447 |
if( ai_range < 0 ) return -1; |
| 448 |
|
| 449 |
for( dac_chan = 0; dac_chan < 2; dac_chan++ ) |
| 450 |
{ |
| 451 |
o = setup->observables + ao_ground_observable_index_64xx( setup, |
| 452 |
dac_chan, range ); |
| 453 |
o->reference_source = ao_cal_src_64xx( dac_chan ); |
| 454 |
assert( o->name == NULL ); |
| 455 |
if( asprintf( &o->name, "dac%i low, range %i, ground referenced", dac_chan, range ) < 0 ) |
| 456 |
{ |
| 457 |
errno = ENOMEM; |
| 458 |
perror( NULL ); |
| 459 |
return -1; |
| 460 |
} |
| 461 |
o->preobserve_insn = po_tmpl; |
| 462 |
o->preobserve_insn.chanspec = CR_PACK( dac_chan, range, AREF_GROUND ); |
| 463 |
o->preobserve_insn.data = o->preobserve_data; |
| 464 |
o->observe_insn = tmpl; |
| 465 |
o->observe_insn.chanspec = CR_PACK( 0, ai_range, AREF_GROUND) | CR_ALT_SOURCE | CR_ALT_FILTER; |
| 466 |
set_target( setup, ao_ground_observable_index_64xx( setup, dac_chan, range ), 0.0 ); |
| 467 |
setup->n_observables++; |
| 468 |
|
| 469 |
o = setup->observables + ao_high_observable_index_64xx( setup, dac_chan, range ); |
| 470 |
o->reference_source = ao_cal_src_64xx( dac_chan ); |
| 471 |
assert( o->name == NULL ); |
| 472 |
if( asprintf( &o->name, "dac%i high, range %i, ground referenced", dac_chan, range ) < 0 ) |
| 473 |
{ |
| 474 |
errno = ENOMEM; |
| 475 |
perror( NULL ); |
| 476 |
return -1; |
| 477 |
} |
| 478 |
o->preobserve_insn = po_tmpl; |
| 479 |
o->preobserve_insn.chanspec = CR_PACK( dac_chan, range, AREF_GROUND ); |
| 480 |
o->preobserve_insn.data = o->preobserve_data; |
| 481 |
o->observe_insn = tmpl; |
| 482 |
o->observe_insn.chanspec = CR_PACK( 0, ai_range, AREF_GROUND) | CR_ALT_SOURCE | CR_ALT_FILTER; |
| 483 |
retval = ao_set_high_target_64xx( setup, |
| 484 |
ao_high_observable_index_64xx( setup, dac_chan, range ), range ); |
| 485 |
if( retval < 0 ) return -1; |
| 486 |
setup->n_observables++; |
| 487 |
} |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
return 0; |
| 492 |
} |
| 493 |
|
| 494 |
static int ai_high_observable_index_60xx( const calibration_setup_t *setup, |
| 495 |
unsigned int channel, unsigned int ai_range ) |
| 496 |
{ |
| 497 |
return ai_range * 2 + 1; |
| 498 |
} |
| 499 |
|
| 500 |
static int ai_ground_observable_index_60xx( const calibration_setup_t *setup, |
| 501 |
unsigned int channel, unsigned int ai_range ) |
| 502 |
{ |
| 503 |
return ai_range * 2; |
| 504 |
} |
| 505 |
|
| 506 |
static int ao_high_observable_index_60xx( const calibration_setup_t *setup, |
| 507 |
unsigned int channel, unsigned int ao_range ) |
| 508 |
{ |
| 509 |
int num_ai_ranges; |
| 510 |
|
| 511 |
num_ai_ranges = comedi_get_n_ranges( setup->dev, setup->ad_subdev, 0 ); |
| 512 |
assert( num_ai_ranges >= 0 ); |
| 513 |
|
| 514 |
return 2 * num_ai_ranges + 2 * channel + 4 * ao_range + 1; |
| 515 |
} |
| 516 |
|
| 517 |
static int ao_ground_observable_index_60xx( const calibration_setup_t *setup, |
| 518 |
unsigned int channel, unsigned int ao_range ) |
| 519 |
{ |
| 520 |
int num_ai_ranges; |
| 521 |
|
| 522 |
num_ai_ranges = comedi_get_n_ranges( setup->dev, setup->ad_subdev, 0 ); |
| 523 |
assert( num_ai_ranges >= 0 ); |
| 524 |
|
| 525 |
return 2 * num_ai_ranges + 2 * channel + 4 * ao_range; |
| 526 |
} |
| 527 |
|
| 528 |
static int ai_cal_src_voltage_60xx( calibration_setup_t *setup, |
| 529 |
unsigned int calibration_source, float *voltage ) |
| 530 |
{ |
| 531 |
enum source_eeprom_addr |
| 532 |
{ |
| 533 |
EEPROM_10V_CHAN = 0x30, |
| 534 |
EEPROM_5V_CHAN = 0x32, |
| 535 |
EEPROM_500mV_CHAN = 0x38, |
| 536 |
EEPROM_50mV_CHAN = 0x3e, |
| 537 |
EEPROM_8mV_CHAN = 0x40, // bogus? |
| 538 |
}; |
| 539 |
int retval; |
| 540 |
|
| 541 |
switch( calibration_source ) |
| 542 |
{ |
| 543 |
case CS_60XX_GROUND: |
| 544 |
*voltage = 0.0; |
| 545 |
retval = 0; |
| 546 |
break; |
| 547 |
case CS_60XX_10V: |
| 548 |
retval = cb_actual_source_voltage( setup->dev, setup->eeprom_subdev, |
| 549 |
EEPROM_10V_CHAN, voltage ); |
| 550 |
break; |
| 551 |
case CS_60XX_5V: |
| 552 |
retval = cb_actual_source_voltage( setup->dev, setup->eeprom_subdev, |
| 553 |
EEPROM_5V_CHAN, voltage ); |
| 554 |
break; |
| 555 |
case CS_60XX_500mV: |
| 556 |
retval = cb_actual_source_voltage( setup->dev, setup->eeprom_subdev, |
| 557 |
EEPROM_500mV_CHAN, voltage ); |
| 558 |
break; |
| 559 |
case CS_60XX_50mV: |
| 560 |
retval = cb_actual_source_voltage( setup->dev, setup->eeprom_subdev, |
| 561 |
EEPROM_50mV_CHAN, voltage ); |
| 562 |
break; |
| 563 |
default: |
| 564 |
fprintf( stderr, "invalid calibration_source in ai_cal_src_voltage_60xx()\n" ); |
| 565 |
retval = -1; |
| 566 |
break; |
| 567 |
} |
| 568 |
return retval; |
| 569 |
} |
| 570 |
|
| 571 |
static int high_ai_cal_src_60xx( calibration_setup_t *setup, unsigned int ai_range ) |
| 572 |
{ |
| 573 |
comedi_range *range; |
| 574 |
|
| 575 |
range = comedi_get_range( setup->dev, setup->ad_subdev, 0, ai_range ); |
| 576 |
if( range == NULL ) |
| 577 |
{ |
| 578 |
fprintf(stderr, "%s: failed to get range\n", __FUNCTION__); |
| 579 |
return -1; |
| 580 |
} |
| 581 |
if( range->max > 9.999 ) |
| 582 |
return CS_60XX_10V; |
| 583 |
else if( range->max > 4.999 ) |
| 584 |
return CS_60XX_5V; |
| 585 |
else if( range->max > 0.4999 ) |
| 586 |
return CS_60XX_500mV; |
| 587 |
else if( range->max > 0.04999 ) |
| 588 |
return CS_60XX_50mV; |
| 589 |
|
| 590 |
return -1; |
| 591 |
} |
| 592 |
|
| 593 |
static int ao_cal_src_60xx( unsigned int channel ) |
| 594 |
{ |
| 595 |
switch( channel ) |
| 596 |
{ |
| 597 |
case 0: |
| 598 |
return CS_60XX_DAC0; |
| 599 |
break; |
| 600 |
case 1: |
| 601 |
return CS_60XX_DAC1; |
| 602 |
break; |
| 603 |
default: |
| 604 |
return -1; |
| 605 |
break; |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
static int init_observables_60xx( calibration_setup_t *setup ) |
| 610 |
{ |
| 611 |
comedi_insn tmpl; |
| 612 |
observable *o; |
| 613 |
int retval, num_ranges, i; |
| 614 |
float target; |
| 615 |
enum |
| 616 |
{ |
| 617 |
CAL_SRC_GROUND = 0, |
| 618 |
}; |
| 619 |
|
| 620 |
memset( &tmpl, 0, sizeof(tmpl) ); |
| 621 |
tmpl.insn = INSN_READ; |
| 622 |
tmpl.n = 1; |
| 623 |
tmpl.subdev = setup->ad_subdev; |
| 624 |
|
| 625 |
setup->n_observables = 0; |
| 626 |
|
| 627 |
num_ranges = comedi_get_n_ranges( setup->dev, setup->ad_subdev, 0 ); |
| 628 |
if( num_ranges < 0 ) |
| 629 |
{ |
| 630 |
fprintf(stderr, "%s: failed to get number of ai ranges\n", __FUNCTION__); |
| 631 |
return -1; |
| 632 |
} |
| 633 |
for( i = 0; i < num_ranges; i++ ) |
| 634 |
{ |
| 635 |
o = setup->observables + ai_ground_observable_index_60xx( setup, 0, i ); |
| 636 |
o->reference_source = CAL_SRC_GROUND; |
| 637 |
assert( o->name == NULL ); |
| 638 |
if( asprintf( &o->name, "calibration source %i, range %i, ground referenced", |
| 639 |
o->reference_source, i ) < 0 ) |
| 640 |
{ |
| 641 |
errno = ENOMEM; |
| 642 |
perror( NULL ); |
| 643 |
return -1; |
| 644 |
} |
| 645 |
o->observe_insn = tmpl; |
| 646 |
o->observe_insn.chanspec = CR_PACK( 0, i, AREF_GROUND) | CR_ALT_SOURCE | CR_ALT_FILTER; |
| 647 |
o->target = 0.0; |
| 648 |
setup->n_observables++; |
| 649 |
|
| 650 |
o = setup->observables + ai_high_observable_index_60xx( setup, 0, i ); |
| 651 |
retval = high_ai_cal_src_60xx( setup, i ); |
| 652 |
if( retval < 0 ) return -1; |
| 653 |
o->reference_source = retval; |
| 654 |
assert( o->name == NULL ); |
| 655 |
if( asprintf( &o->name, "calibration source %i, range %i, ground referenced", |
| 656 |
o->reference_source, i ) < 0 ) |
| 657 |
{ |
| 658 |
errno = ENOMEM; |
| 659 |
perror( NULL ); |
| 660 |
return -1; |
| 661 |
} |
| 662 |
o->observe_insn = tmpl; |
| 663 |
o->observe_insn.chanspec = CR_PACK( 0, i, AREF_GROUND) | CR_ALT_SOURCE | CR_ALT_FILTER; |
| 664 |
retval = ai_cal_src_voltage_60xx( setup, o->reference_source, &target ); |
| 665 |
if( retval < 0 ) return -1; |
| 666 |
o->target = target; |
| 667 |
setup->n_observables++; |
| 668 |
} |
| 669 |
|
| 670 |
if( setup->da_subdev >= 0 ) |
| 671 |
{ |
| 672 |
comedi_insn po_tmpl; |
| 673 |
|
| 674 |
memset( &po_tmpl, 0, sizeof(po_tmpl) ); |
| 675 |
po_tmpl.insn = INSN_WRITE; |
| 676 |
po_tmpl.n = 1; |
| 677 |
po_tmpl.subdev = setup->da_subdev; |
| 678 |
|
| 679 |
num_ranges = comedi_get_n_ranges( setup->dev, setup->da_subdev, 0 ); |
| 680 |
if( num_ranges < 0 ) |
| 681 |
{ |
| 682 |
fprintf(stderr, "%s: failed to get number of ao ranges\n", __FUNCTION__); |
| 683 |
return -1; |
| 684 |
} |
| 685 |
|
| 686 |
for( i = 0; i < num_ranges; i++ ) |
| 687 |
{ |
| 688 |
int dac_chan; |
| 689 |
for( dac_chan = 0; dac_chan < 2; dac_chan++ ) |
| 690 |
{ |
| 691 |
o = setup->observables + ao_ground_observable_index_60xx( setup, |
| 692 |
dac_chan, i ); |
| 693 |
o->reference_source = ao_cal_src_60xx( dac_chan ); |
| 694 |
assert( o->name == NULL ); |
| 695 |
if( asprintf( &o->name, "dac%i low, range %i, ground referenced", dac_chan, i ) < 0 ) |
| 696 |
{ |
| 697 |
errno = ENOMEM; |
| 698 |
perror( NULL ); |
| 699 |
return -1; |
| 700 |
} |
| 701 |
o->preobserve_insn = po_tmpl; |
| 702 |
o->preobserve_insn.chanspec = CR_PACK( dac_chan, i, AREF_GROUND ); |
| 703 |
o->preobserve_insn.data = o->preobserve_data; |
| 704 |
o->observe_insn = tmpl; |
| 705 |
o->observe_insn.chanspec = CR_PACK( 0, 0, AREF_GROUND) | CR_ALT_SOURCE | CR_ALT_FILTER; |
| 706 |
set_target( setup, ao_ground_observable_index_60xx( setup, dac_chan, i ), 0.0 ); |
| 707 |
setup->n_observables++; |
| 708 |
|
| 709 |
o = setup->observables + ao_high_observable_index_60xx( setup, dac_chan, i ); |
| 710 |
o->reference_source = ao_cal_src_60xx( dac_chan ); |
| 711 |
assert( o->name == NULL ); |
| 712 |
if( asprintf( &o->name, "dac%i high, range %i, ground referenced", dac_chan, i ) < 0 ) |
| 713 |
{ |
| 714 |
errno = ENOMEM; |
| 715 |
perror( NULL ); |
| 716 |
return -1; |
| 717 |
} |
| 718 |
o->preobserve_insn = po_tmpl; |
| 719 |
o->preobserve_insn.chanspec = CR_PACK( dac_chan, i, AREF_GROUND ); |
| 720 |
o->preobserve_insn.data = o->preobserve_data; |
| 721 |
o->observe_insn = tmpl; |
| 722 |
o->observe_insn.chanspec = CR_PACK( 0, 0, AREF_GROUND) | CR_ALT_SOURCE | CR_ALT_FILTER; |
| 723 |
set_target( setup, ao_high_observable_index_60xx( setup, dac_chan, i ), 9.0 ); |
| 724 |
setup->n_observables++; |
| 725 |
} |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
return 0; |
| 730 |
} |
| 731 |
|
| 732 |
static int ai_low_observable_index_4020( const calibration_setup_t *setup, |
| 733 |
unsigned int channel, unsigned int ai_range ) |
| 734 |
{ |
| 735 |
return 4 * channel + 2 * ai_range; |
| 736 |
} |
| 737 |
|
| 738 |
static int ai_high_observable_index_4020( const calibration_setup_t *setup, |
| 739 |
unsigned int channel, unsigned int ai_range ) |
| 740 |
{ |
| 741 |
return ai_low_observable_index_4020( setup, channel, ai_range ) + 1; |
| 742 |
} |
| 743 |
|
| 744 |
static int high_ai_cal_src_4020( calibration_setup_t *setup, unsigned int ai_range ) |
| 745 |
{ |
| 746 |
comedi_range *range; |
| 747 |
|
| 748 |
range = comedi_get_range( setup->dev, setup->ad_subdev, 0, ai_range ); |
| 749 |
if( range == NULL ) |
| 750 |
{ |
| 751 |
fprintf(stderr, "%s: failed to get range\n", __FUNCTION__); |
| 752 |
return -1; |
| 753 |
} |
| 754 |
if( range->max > 4.4 ) |
| 755 |
return CS_4020_4375mV; |
| 756 |
else if( range->max > 0.7 ) |
| 757 |
return CS_4020_625mV; |
| 758 |
|
| 759 |
return -1; |
| 760 |
} |
| 761 |
|
| 762 |
static int source_eeprom_addr_4020( calibration_setup_t *setup, unsigned int range_index ) |
| 763 |
{ |
| 764 |
enum source_eeprom_addr |
| 765 |
{ |
| 766 |
EEPROM_4375mV_CHAN = 0x30, |
| 767 |
EEPROM_625mV_CHAN = 0x32, |
| 768 |
}; |
| 769 |
comedi_range *range; |
| 770 |
|
| 771 |
range = comedi_get_range( setup->dev, setup->ad_subdev, 0, range_index ); |
| 772 |
if( range == NULL ) |
| 773 |
{ |
| 774 |
fprintf(stderr, "%s: failed to get range\n", __FUNCTION__); |
| 775 |
return -1; |
| 776 |
} |
| 777 |
if( range->max > 4.4 ) |
| 778 |
return EEPROM_4375mV_CHAN; |
| 779 |
else if( range->max > 0.7 ) |
| 780 |
return EEPROM_625mV_CHAN; |
| 781 |
|
| 782 |
return -1; |
| 783 |
} |
| 784 |
|
| 785 |
static int init_observables_4020( calibration_setup_t *setup ) |
| 786 |
{ |
| 787 |
comedi_insn tmpl; |
| 788 |
observable *o; |
| 789 |
float target; |
| 790 |
int retval; |
| 791 |
int range, channel, num_ranges, num_channels; |
| 792 |
|
| 793 |
memset( &tmpl, 0, sizeof(tmpl) ); |
| 794 |
tmpl.insn = INSN_READ; |
| 795 |
tmpl.n = 1; |
| 796 |
tmpl.subdev = setup->ad_subdev; |
| 797 |
|
| 798 |
num_ranges = comedi_get_n_ranges( setup->dev, setup->ad_subdev, 0 ); |
| 799 |
if( num_ranges < 0 ) |
| 800 |
{ |
| 801 |
fprintf(stderr, "%s: failed to get number of ai ranges\n", __FUNCTION__); |
| 802 |
return -1; |
| 803 |
} |
| 804 |
|
| 805 |
num_channels = comedi_get_n_channels( setup->dev, setup->ad_subdev ); |
| 806 |
if( num_channels < 0 ) |
| 807 |
{ |
| 808 |
fprintf(stderr, "%s: failed to get number of ai channels\n", __FUNCTION__); |
| 809 |
return -1; |
| 810 |
} |
| 811 |
|
| 812 |
setup->n_observables = 0; |
| 813 |
|
| 814 |
for( channel = 0; channel < num_channels; channel++ ) |
| 815 |
{ |
| 816 |
for( range = 0; range < num_ranges; range++ ) |
| 817 |
{ |
| 818 |
o = setup->observables + ai_low_observable_index_4020( setup, channel, range ); |
| 819 |
assert( o->name == NULL ); |
| 820 |
if( asprintf( &o->name, "ground calibration source, ch %i, range %i, ground referenced", |
| 821 |
channel, range ) < 0 ) |
| 822 |
{ |
| 823 |
errno = ENOMEM; |
| 824 |
perror( NULL ); |
| 825 |
return -1; |
| 826 |
} |
| 827 |
o->reference_source = CS_4020_GROUND; |
| 828 |
o->observe_insn = tmpl; |
| 829 |
o->observe_insn.chanspec = CR_PACK( channel, range, AREF_GROUND) | CR_ALT_SOURCE | CR_ALT_FILTER; |
| 830 |
o->target = 0.0; |
| 831 |
setup->n_observables++; |
| 832 |
|
| 833 |
o = setup->observables + ai_high_observable_index_4020( setup, channel, range ); |
| 834 |
retval = high_ai_cal_src_4020( setup, range ); |
| 835 |
if( retval < 0 ) return -1; |
| 836 |
o->reference_source = retval; |
| 837 |
assert( o->name == NULL ); |
| 838 |
if( asprintf( &o->name, "calibration source %i, ch %i, range %i, ground referenced", |
| 839 |
o->reference_source, channel, range ) < 0) |
| 840 |
{ |
| 841 |
errno = ENOMEM; |
| 842 |
perror( NULL ); |
| 843 |
return -1; |
| 844 |
} |
| 845 |
o->observe_insn = tmpl; |
| 846 |
o->observe_insn.chanspec = CR_PACK( channel, range, AREF_GROUND) | CR_ALT_SOURCE | CR_ALT_FILTER; |
| 847 |
retval = cb_actual_source_voltage( setup->dev, setup->eeprom_subdev, |
| 848 |
source_eeprom_addr_4020( setup, range ), &target ); |
| 849 |
if( retval < 0 ) return retval; |
| 850 |
o->target = target; |
| 851 |
setup->n_observables++; |
| 852 |
} |
| 853 |
} |
| 854 |
|
| 855 |
return 0; |
| 856 |
} |
| 857 |
|
| 858 |
static int adc_offset_64xx( unsigned int channel ) |
| 859 |
{ |
| 860 |
return ADC_OFFSET_64XX; |
| 861 |
} |
| 862 |
static int adc_gain_64xx( unsigned int channel ) |
| 863 |
{ |
| 864 |
return ADC_GAIN_64XX; |
| 865 |
} |
| 866 |
static int dac_gain_coarse_64xx( unsigned int channel ) |
| 867 |
{ |
| 868 |
if( channel ) return DAC1_GAIN_COARSE_64XX; |
| 869 |
else return DAC0_GAIN_COARSE_64XX; |
| 870 |
} |
| 871 |
static int dac_gain_fine_64xx( unsigned int channel ) |
| 872 |
{ |
| 873 |
if( channel ) return DAC1_GAIN_FINE_64XX; |
| 874 |
else return DAC0_GAIN_FINE_64XX; |
| 875 |
} |
| 876 |
static int dac_offset_coarse_64xx( unsigned int channel ) |
| 877 |
{ |
| 878 |
if( channel ) return DAC1_OFFSET_COARSE_64XX; |
| 879 |
else return DAC0_OFFSET_COARSE_64XX; |
| 880 |
} |
| 881 |
static int dac_offset_fine_64xx( unsigned int channel ) |
| 882 |
{ |
| 883 |
if( channel ) return DAC1_OFFSET_FINE_64XX; |
| 884 |
else return DAC0_OFFSET_FINE_64XX; |
| 885 |
} |
| 886 |
static int cal_cb_pci_64xx( calibration_setup_t *setup ) |
| 887 |
{ |
| 888 |
generic_layout_t layout; |
| 889 |
|
| 890 |
init_generic_layout( &layout ); |
| 891 |
layout.adc_gain = adc_gain_64xx; |
| 892 |
layout.adc_offset = adc_offset_64xx; |
| 893 |
layout.dac_gain = dac_gain_coarse_64xx; |
| 894 |
layout.dac_gain_fine = dac_gain_fine_64xx; |
| 895 |
layout.dac_offset = dac_offset_coarse_64xx; |
| 896 |
layout.dac_offset_fine = dac_offset_fine_64xx; |
| 897 |
layout.adc_high_observable = ai_high_observable_index_64xx; |
| 898 |
layout.adc_ground_observable = ai_ground_observable_index_64xx; |
| 899 |
layout.dac_high_observable = ao_high_observable_index_64xx; |
| 900 |
layout.dac_ground_observable = ao_ground_observable_index_64xx; |
| 901 |
layout.adc_fractional_tolerance = get_tolerance( setup, setup->ad_subdev, 1.0 ); |
| 902 |
layout.dac_fractional_tolerance = get_tolerance( setup, setup->da_subdev, 1.0 ); |
| 903 |
return generic_cal_by_range( setup, &layout ); |
| 904 |
} |
| 905 |
|
| 906 |
static int adc_gain_coarse_60xx( unsigned int channel ) |
| 907 |
{ |
| 908 |
return ADC_GAIN_COARSE_60XX; |
| 909 |
} |
| 910 |
static int adc_gain_fine_60xx( unsigned int channel ) |
| 911 |
{ |
| 912 |
return ADC_GAIN_FINE_60XX; |
| 913 |
} |
| 914 |
static int adc_offset_coarse_60xx( unsigned int channel ) |
| 915 |
{ |
| 916 |
return ADC_OFFSET_COARSE_60XX; |
| 917 |
} |
| 918 |
static int adc_offset_fine_60xx( unsigned int channel ) |
| 919 |
{ |
| 920 |
return ADC_OFFSET_FINE_60XX; |
| 921 |
} |
| 922 |
static int dac_gain_60xx( unsigned int channel ) |
| 923 |
{ |
| 924 |
if( channel ) return DAC1_GAIN_60XX; |
| 925 |
else return DAC0_GAIN_60XX; |
| 926 |
} |
| 927 |
static int dac_offset_60xx( unsigned int channel ) |
| 928 |
{ |
| 929 |
if( channel ) return DAC1_OFFSET_60XX; |
| 930 |
else return DAC0_OFFSET_60XX; |
| 931 |
} |
| 932 |
static int cal_cb_pci_60xx( calibration_setup_t *setup ) |
| 933 |
{ |
| 934 |
generic_layout_t layout; |
| 935 |
|
| 936 |
init_generic_layout( &layout ); |
| 937 |
layout.adc_gain = adc_gain_coarse_60xx; |
| 938 |
layout.adc_gain_fine = adc_gain_fine_60xx; |
| 939 |
layout.adc_offset = adc_offset_coarse_60xx; |
| 940 |
layout.adc_offset_fine = adc_offset_fine_60xx; |
| 941 |
layout.dac_gain = dac_gain_60xx; |
| 942 |
layout.dac_offset = dac_offset_60xx; |
| 943 |
layout.adc_high_observable = ai_high_observable_index_60xx; |
| 944 |
layout.adc_ground_observable = ai_ground_observable_index_60xx; |
| 945 |
layout.dac_high_observable = ao_high_observable_index_60xx; |
| 946 |
layout.dac_ground_observable = ao_ground_observable_index_60xx; |
| 947 |
layout.adc_fractional_tolerance = get_tolerance( setup, setup->ad_subdev, 1.0 ); |
| 948 |
layout.dac_fractional_tolerance = get_tolerance( setup, setup->da_subdev, 1.0 ); |
| 949 |
return generic_cal_by_range( setup, &layout ); |
| 950 |
} |
| 951 |
|
| 952 |
static int adc_offset_4020( unsigned int channel ) |
| 953 |
{ |
| 954 |
return channel; |
| 955 |
} |
| 956 |
static int adc_gain_4020( unsigned int channel ) |
| 957 |
{ |
| 958 |
return 4 + channel; |
| 959 |
} |
| 960 |
|
| 961 |
static int cal_cb_pci_4020( calibration_setup_t *setup ) |
| 962 |
{ |
| 963 |
generic_layout_t layout; |
| 964 |
|
| 965 |
init_generic_layout( &layout ); |
| 966 |
layout.adc_offset = adc_offset_4020; |
| 967 |
layout.adc_gain = adc_gain_4020; |
| 968 |
layout.adc_high_observable = ai_high_observable_index_4020; |
| 969 |
layout.adc_ground_observable = ai_low_observable_index_4020; |
| 970 |
layout.adc_fractional_tolerance = get_tolerance( setup, setup->ad_subdev, 1.0 ); |
| 971 |
return generic_cal_by_channel_and_range( setup, &layout ); |
| 972 |
} |
| 973 |
|
| 974 |
#ifdef notused |
| 975 |
static int init_observables_unknown( calibration_setup_t *setup ) |
| 976 |
{ |
| 977 |
comedi_insn tmpl; |
| 978 |
observable *o; |
| 979 |
int retval, num_ranges, i; |
| 980 |
enum |
| 981 |
{ |
| 982 |
CAL_SRC_GROUND = 0, |
| 983 |
}; |
| 984 |
static const int num_cal_sources = 16; |
| 985 |
static const int range = 0; |
| 986 |
|
| 987 |
memset( &tmpl, 0, sizeof(tmpl) ); |
| 988 |
tmpl.insn = INSN_READ; |
| 989 |
tmpl.n = 1; |
| 990 |
tmpl.subdev = setup->ad_subdev; |
| 991 |
|
| 992 |
setup->n_observables = 0; |
| 993 |
|
| 994 |
num_ranges = comedi_get_n_ranges( setup->dev, setup->ad_subdev, 0 ); |
| 995 |
if( num_ranges < 0 ) |
| 996 |
{ |
| 997 |
fprintf(stderr, "%s: failed to get number of ai ranges\n", __FUNCTION__); |
| 998 |
return -1; |
| 999 |
} |
| 1000 |
for( i = 0; i < num_cal_sources; i++ ) |
| 1001 |
{ |
| 1002 |
o = setup->observables + i; |
| 1003 |
o->reference_source = i; |
| 1004 |
assert( o->name == NULL ); |
| 1005 |
if( asprintf( &o->name, "calibration source %i, range %i, ground referenced", |
| 1006 |
o->reference_source, range) < 0) |
| 1007 |
{ |
| 1008 |
errno = ENOMEM; |
| 1009 |
perror( NULL ); |
| 1010 |
return -1; |
| 1011 |
} |
| 1012 |
o->observe_insn = tmpl; |
| 1013 |
o->observe_insn.chanspec = CR_PACK( 0, range, AREF_GROUND) | CR_ALT_SOURCE | CR_ALT_FILTER; |
| 1014 |
o->target = 0.0; |
| 1015 |
setup->n_observables++; |
| 1016 |
} |
| 1017 |
for(i = 0; i < 16; ++i) |
| 1018 |
{ |
| 1019 |
int eeprom_channel = 0x30 + i; |
| 1020 |
float voltage; |
| 1021 |
retval = cb_actual_source_voltage(setup->dev, setup->eeprom_subdev, eeprom_channel, &voltage); |
| 1022 |
if(retval == 0) |
| 1023 |
printf("eeprom ch 0x%x gives calibration source of %gV\n", eeprom_channel, voltage); |
| 1024 |
else |
| 1025 |
printf("cb_actual_source_voltage returned %i\n", retval); |
| 1026 |
} |
| 1027 |
return 0; |
| 1028 |
} |
| 1029 |
#endif |
| 1030 |
|
| 1031 |
/* We were unable to read the onboard calibration reference voltages on the 6052, so we'll just |
| 1032 |
* load caldac values from eeprom until the problem is resolved somehow. */ |
| 1033 |
|
| 1034 |
static int setup_cb_pci_6052( calibration_setup_t *setup ) |
| 1035 |
{ |
| 1036 |
if( setup->caldac_subdev < 0 ) |
| 1037 |
{ |
| 1038 |
fprintf( stderr, "no caldac subdevice found\n"); |
| 1039 |
return -1; |
| 1040 |
} |
| 1041 |
setup->do_cal = &cal_cb_pci_6052; |
| 1042 |
setup_caldacs(setup, setup->caldac_subdev); |
| 1043 |
|
| 1044 |
return 0; |
| 1045 |
} |
| 1046 |
|
| 1047 |
|
| 1048 |
/* load analog input caldacs from eeprom values (depend on range used) */ |
| 1049 |
static void grab_ai_calibration_cb_pci_6052(calibration_setup_t *setup, unsigned int range) |
| 1050 |
{ |
| 1051 |
comedi_calibration_setting_t *current_cal; |
| 1052 |
enum source_eeprom_addr |
| 1053 |
{ |
| 1054 |
EEPROM_5V_CHAN = 0x30, |
| 1055 |
EEPROM_3500mV_CHAN = 0x32, |
| 1056 |
EEPROM_1750mV_CHAN = 0x34, |
| 1057 |
EEPROM_875mV_CHAN = 0x36, |
| 1058 |
EEPROM_8600uV_CHAN = 0x38, |
| 1059 |
}; |
| 1060 |
/* According to Measurement Computing the adc eeprom channels are: |
| 1061 |
ADC Calibration values |
| 1062 |
50 BIP10V offset |
| 1063 |
51 BIP10V gain |
| 1064 |
52 BIP5V offset |
| 1065 |
53 BIP5V gain |
| 1066 |
54 BIP2.5V offset |
| 1067 |
55 BIP2.5V gain |
| 1068 |
56 BIP1V offset |
| 1069 |
57 BIP1V gain |
| 1070 |
58 BIP0.5V offset |
| 1071 |
59 BIP0.5V gain |
| 1072 |
5A BIP0.25V offset |
| 1073 |
5B BIP0.25V gain |
| 1074 |
5C BIP0.1V offset |
| 1075 |
5D BIP0.1V gain |
| 1076 |
5E BIP50mV offset |
| 1077 |
5F BIP50mV gain |
| 1078 |
60 UNI10V offset |
| 1079 |
61 UNI10V gain |
| 1080 |
62 UNI5V offset |
| 1081 |
63 UNI5V gain |
| 1082 |
64 UNI2.5V offset |
| 1083 |
65 UNI2.5V gain |
| 1084 |
66 UNI1V offset |
| 1085 |
67 UNI1V gain |
| 1086 |
68 UNI0.5V offset |
| 1087 |
69 UNI0.5V gain |
| 1088 |
6A UNI0.25V offset |
| 1089 |
6B UNI0.25V gain |
| 1090 |
6C UNI0.1V offset |
| 1091 |
6D UNI0.1V gain |
| 1092 |
*/ |
| 1093 |
int offset_eeprom_channel; |
| 1094 |
int gain_eeprom_channel; |
| 1095 |
int value; |
| 1096 |
const double epsilon = 0.001; |
| 1097 |
comedi_range *range_ptr = comedi_get_range(setup->dev, setup->ad_subdev, 0, range); |
| 1098 |
if(range_ptr == NULL) |
| 1099 |
{ |
| 1100 |
fprintf(stderr, "%s: comedi_get_range returned NULL\n", __FUNCTION__); |
| 1101 |
abort(); |
| 1102 |
} |
| 1103 |
if(is_unipolar(setup->dev, setup->ad_subdev, 0, range) == 0) |
| 1104 |
{ |
| 1105 |
if(fabs(10. - range_ptr->max) < epsilon) |
| 1106 |
{ |
| 1107 |
offset_eeprom_channel = 0x50; |
| 1108 |
gain_eeprom_channel = 0x51; |
| 1109 |
}else if(fabs(5. - range_ptr->max) < epsilon) |
| 1110 |
{ |
| 1111 |
offset_eeprom_channel = 0x52; |
| 1112 |
gain_eeprom_channel = 0x53; |
| 1113 |
}else if(fabs(2.5 - range_ptr->max) < epsilon) |
| 1114 |
{ |
| 1115 |
offset_eeprom_channel = 0x54; |
| 1116 |
gain_eeprom_channel = 0x55; |
| 1117 |
}else if(fabs(1. - range_ptr->max) < epsilon) |
| 1118 |
{ |
| 1119 |
offset_eeprom_channel = 0x56; |
| 1120 |
gain_eeprom_channel = 0x57; |
| 1121 |
}else if(fabs(0.5 - range_ptr->max) < epsilon) |
| 1122 |
{ |
| 1123 |
offset_eeprom_channel = 0x58; |
| 1124 |
gain_eeprom_channel = 0x59; |
| 1125 |
}else if(fabs(0.25 - range_ptr->max) < epsilon) |
| 1126 |
{ |
| 1127 |
offset_eeprom_channel = 0x5a; |
| 1128 |
gain_eeprom_channel = 0x5b; |
| 1129 |
}else if(fabs(0.1 - range_ptr->max) < epsilon) |
| 1130 |
{ |
| 1131 |
offset_eeprom_channel = 0x5c; |
| 1132 |
gain_eeprom_channel = 0x5d; |
| 1133 |
}else if(fabs(0.5 - range_ptr->max) < epsilon) |
| 1134 |
{ |
| 1135 |
offset_eeprom_channel = 0x5e; |
| 1136 |
gain_eeprom_channel = 0x5f; |
| 1137 |
}else |
| 1138 |
{ |
| 1139 |
fprintf(stderr, "%s: logic error, failed to determine eeprom channel for range min=%g, max=%g\n", |
| 1140 |
__FUNCTION__, range_ptr->min, range_ptr->max); |
| 1141 |
abort(); |
| 1142 |
} |
| 1143 |
}else |
| 1144 |
{ |
| 1145 |
if(fabs(10. - range_ptr->max) < epsilon) |
| 1146 |
{ |
| 1147 |
offset_eeprom_channel = 0x60; |
| 1148 |
gain_eeprom_channel = 0x61; |
| 1149 |
}else if(fabs(5. - range_ptr->max) < epsilon) |
| 1150 |
{ |
| 1151 |
offset_eeprom_channel = 0x62; |
| 1152 |
gain_eeprom_channel = 0x63; |
| 1153 |
}else if(fabs(2.5 - range_ptr->max) < epsilon) |
| 1154 |
{ |
| 1155 |
offset_eeprom_channel = 0x64; |
| 1156 |
gain_eeprom_channel = 0x65; |
| 1157 |
}else if(fabs(1. - range_ptr->max) < epsilon) |
| 1158 |
{ |
| 1159 |
offset_eeprom_channel = 0x66; |
| 1160 |
gain_eeprom_channel = 0x67; |
| 1161 |
}else if(fabs(0.5 - range_ptr->max) < epsilon) |
| 1162 |
{ |
| 1163 |
offset_eeprom_channel = 0x68; |
| 1164 |
gain_eeprom_channel = 0x69; |
| 1165 |
}else if(fabs(0.25 - range_ptr->max) < epsilon) |
| 1166 |
{ |
| 1167 |
offset_eeprom_channel = 0x6a; |
| 1168 |
gain_eeprom_channel = 0x6b; |
| 1169 |
}else if(fabs(0.1 - range_ptr->max) < epsilon) |
| 1170 |
{ |
| 1171 |
offset_eeprom_channel = 0x6c; |
| 1172 |
gain_eeprom_channel = 0x6d; |
| 1173 |
}else |
| 1174 |
{ |
| 1175 |
fprintf(stderr, "%s: logic error, failed to determine eeprom channel for range min=%g, max=%g\n", |
| 1176 |
__FUNCTION__, range_ptr->min, range_ptr->max); |
| 1177 |
abort(); |
| 1178 |
} |
| 1179 |
} |
| 1180 |
current_cal = sc_alloc_calibration_setting(setup->new_calibration); |
| 1181 |
|
| 1182 |
/* load offset */ |
| 1183 |
value = read_eeprom(setup, offset_eeprom_channel); |
| 1184 |
if(value < 0) |
| 1185 |
{ |
| 1186 |
fprintf(stderr, "%s: line %i: read_eeprom() returned error\n", __FUNCTION__, __LINE__); |
| 1187 |
abort(); |
| 1188 |
} |
| 1189 |
update_caldac(setup, ADC_OFFSET_6052, value); |
| 1190 |
sc_push_caldac(current_cal, setup->caldacs[ADC_OFFSET_6052]); |
| 1191 |
|
| 1192 |
/* load gain */ |
| 1193 |
value = read_eeprom(setup, gain_eeprom_channel); |
| 1194 |
if(value < 0) |
| 1195 |
{ |
| 1196 |
fprintf(stderr, "%s: line %i: read_eeprom() returned error\n", __FUNCTION__, __LINE__); |
| 1197 |
abort(); |
| 1198 |
} |
| 1199 |
update_caldac(setup, ADC_GAIN_6052, value); |
| 1200 |
sc_push_caldac(current_cal, setup->caldacs[ADC_GAIN_6052]); |
| 1201 |
|
| 1202 |
current_cal->subdevice = setup->ad_subdev; |
| 1203 |
sc_push_channel(current_cal, SC_ALL_CHANNELS); |
| 1204 |
sc_push_range(current_cal, range); |
| 1205 |
sc_push_aref(current_cal, SC_ALL_AREFS); |
| 1206 |
|
| 1207 |
DPRINT( 0, "loaded adc range %i calibration from eeprom\n", range ); |
| 1208 |
} |
| 1209 |
|
| 1210 |
static int dac_gain_coarse_6052( unsigned int channel ) |
| 1211 |
{ |
| 1212 |
if( channel ) return DAC1_GAIN_COARSE_6052; |
| 1213 |
else return DAC0_GAIN_COARSE_6052; |
| 1214 |
} |
| 1215 |
static int dac_gain_fine_6052( unsigned int channel ) |
| 1216 |
{ |
| 1217 |
if( channel ) return DAC1_GAIN_FINE_6052; |
| 1218 |
else return DAC0_GAIN_FINE_6052; |
| 1219 |
} |
| 1220 |
static int dac_offset_coarse_6052( unsigned int channel ) |
| 1221 |
{ |
| 1222 |
if( channel ) return DAC1_OFFSET_COARSE_6052; |
| 1223 |
else return DAC0_OFFSET_COARSE_6052; |
| 1224 |
} |
| 1225 |
static int dac_offset_fine_6052( unsigned int channel ) |
| 1226 |
{ |
| 1227 |
if( channel ) return DAC1_OFFSET_FINE_6052; |
| 1228 |
else return DAC0_OFFSET_FINE_6052; |
| 1229 |
} |
| 1230 |
|
| 1231 |
/* load analog output caldacs from eeprom values (depend on range used) */ |
| 1232 |
static void grab_ao_calibration_cb_pci_6052(calibration_setup_t *setup, |
| 1233 |
unsigned int channel, unsigned int range) |
| 1234 |
{ |
| 1235 |
comedi_calibration_setting_t *current_cal; |
| 1236 |
int value; |
| 1237 |
int gain_eeprom_chan; |
| 1238 |
int offset_eeprom_chan; |
| 1239 |
/* According to Measurement Computing the dac eeprom channels are: |
| 1240 |
DAC Calibration values |
| 1241 |
70 DAC0 BIP10V coarse offset DAC0 BIP10V fine offset |
| 1242 |
71 DAC0 BIP10V coarse gain DAC0 BIP10V fine gain |
| 1243 |
72 DAC0 UNI10V coarse offset DAC0 UNI10V fine offset |
| 1244 |
73 DAC0 UNI10V coarse gain DAC0 UNI10V fine gain |
| 1245 |
74 DAC1 BIP10V coarse offset DAC1 BIP10V fine offset |
| 1246 |
75 DAC1 BIP10V coarse gain DAC1 BIP10V fine gain |
| 1247 |
76 DAC1 UNI10V coarse offset DAC1 UNI10V fine offset |
| 1248 |
77 DAC1 UNI10V coarse gain DAC1 UNI10V fine gain |
| 1249 |
*/ |
| 1250 |
current_cal = sc_alloc_calibration_setting(setup->new_calibration); |
| 1251 |
|
| 1252 |
if(is_unipolar(setup->dev, setup->da_subdev, channel, range)) |
| 1253 |
{ |
| 1254 |
switch(channel) |
| 1255 |
{ |
| 1256 |
case 0: |
| 1257 |
offset_eeprom_chan = 0x72; |
| 1258 |
gain_eeprom_chan = 0x73; |
| 1259 |
break; |
| 1260 |
case 1: |
| 1261 |
offset_eeprom_chan = 0x76; |
| 1262 |
gain_eeprom_chan = 0x77; |
| 1263 |
break; |
| 1264 |
default: |
| 1265 |
fprintf(stderr, "%s: line %i: logic error\n", __FUNCTION__, __LINE__); |
| 1266 |
abort(); |
| 1267 |
break; |
| 1268 |
} |
| 1269 |
}else |
| 1270 |
{ |
| 1271 |
switch(channel) |
| 1272 |
{ |
| 1273 |
case 0: |
| 1274 |
offset_eeprom_chan = 0x70; |
| 1275 |
gain_eeprom_chan = 0x71; |
| 1276 |
break; |
| 1277 |
case 1: |
| 1278 |
offset_eeprom_chan = 0x74; |
| 1279 |
gain_eeprom_chan = 0x75; |
| 1280 |
break; |
| 1281 |
default: |
| 1282 |
fprintf(stderr, "%s: line %i: logic error\n", __FUNCTION__, __LINE__); |
| 1283 |
abort(); |
| 1284 |
break; |
| 1285 |
} |
| 1286 |
} |
| 1287 |
|
| 1288 |
/* load offset */ |
| 1289 |
value = read_eeprom(setup, offset_eeprom_chan); |
| 1290 |
if(value < 0) |
| 1291 |
{ |
| 1292 |
fprintf(stderr, "%s: line %i: read_eeprom() returned error\n", __FUNCTION__, __LINE__); |
| 1293 |
abort(); |
| 1294 |
} |
| 1295 |
update_caldac(setup, dac_offset_coarse_6052(channel), (value >> 8) & 0xff); |
| 1296 |
sc_push_caldac(current_cal, setup->caldacs[dac_offset_coarse_6052(channel)]); |
| 1297 |
update_caldac(setup, dac_offset_fine_6052(channel), value & 0xff); |
| 1298 |
sc_push_caldac(current_cal, setup->caldacs[dac_offset_fine_6052(channel)]); |
| 1299 |
|
| 1300 |
// load gain calibration |
| 1301 |
value = read_eeprom(setup, gain_eeprom_chan); |
| 1302 |
if(value < 0) |
| 1303 |
{ |
| 1304 |
fprintf(stderr, "%s: line %i: read_eeprom() returned error\n", __FUNCTION__, __LINE__); |
| 1305 |
abort(); |
| 1306 |
} |
| 1307 |
update_caldac(setup, dac_gain_coarse_6052(channel), (value >> 8) & 0xff); |
| 1308 |
sc_push_caldac(current_cal, setup->caldacs[dac_gain_coarse_6052(channel)]); |
| 1309 |
update_caldac(setup, dac_gain_fine_6052(channel), value & 0xff); |
| 1310 |
sc_push_caldac(current_cal, setup->caldacs[dac_gain_fine_6052(channel)]); |
| 1311 |
|
| 1312 |
current_cal->subdevice = setup->da_subdev; |
| 1313 |
sc_push_channel(current_cal, channel); |
| 1314 |
sc_push_range(current_cal, range); |
| 1315 |
sc_push_aref(current_cal, SC_ALL_AREFS); |
| 1316 |
|
| 1317 |
DPRINT( 0, "loaded dac channel %i range %i calibration from eeprom\n", |
| 1318 |
channel, range ); |
| 1319 |
} |
| 1320 |
|
| 1321 |
|
| 1322 |
static int cal_cb_pci_6052( calibration_setup_t *setup ) |
| 1323 |
{ |
| 1324 |
int range, channel, num_ai_ranges, num_ao_ranges; |
| 1325 |
|
| 1326 |
num_ai_ranges = comedi_get_n_ranges(setup->dev, setup->ad_subdev, 0); |
| 1327 |
if(num_ai_ranges < 0) |
| 1328 |
{ |
| 1329 |
fprintf(stderr, "%s: line %i: comedi_get_n_ranges() returned error\n", __FUNCTION__, __LINE__); |
| 1330 |
abort(); |
| 1331 |
} |
| 1332 |
num_ao_ranges = comedi_get_n_ranges(setup->dev, setup->da_subdev, 0); |
| 1333 |
if(num_ao_ranges < 0) |
| 1334 |
{ |
| 1335 |
fprintf(stderr, "%s: line %i: comedi_get_n_ranges() returned error\n", __FUNCTION__, __LINE__); |
| 1336 |
abort(); |
| 1337 |
} |
| 1338 |
|
| 1339 |
for(range = 0; range < num_ai_ranges; range++) |
| 1340 |
grab_ai_calibration_cb_pci_6052(setup, range); |
| 1341 |
|
| 1342 |
if(setup->da_subdev >= 0 && setup->do_output) |
| 1343 |
{ |
| 1344 |
for(channel = 0; channel < 2; channel++) |
| 1345 |
{ |
| 1346 |
for(range = 0; range < num_ao_ranges; range++) |
| 1347 |
grab_ao_calibration_cb_pci_6052(setup, channel, range); |
| 1348 |
} |
| 1349 |
} |
| 1350 |
|
| 1351 |
return write_calibration_file(setup->cal_save_file_path, setup->new_calibration); |
| 1352 |
} |
| 1353 |
|