| 1 |
/* |
| 2 |
calibration support for NI labpc and compatible boards |
| 3 |
|
| 4 |
copyright (C) 2003 by Frank Mori Hess |
| 5 |
|
| 6 |
*/ |
| 7 |
|
| 8 |
/*************************************************************************** |
| 9 |
* * |
| 10 |
* This program is free software; you can redistribute it and/or modify * |
| 11 |
* it under the terms of the GNU Lesser General Public License as * |
| 12 |
* published by * |
| 13 |
* the Free Software Foundation; either version 2.1 of the License, or * |
| 14 |
* (at your option) any later version. * |
| 15 |
* * |
| 16 |
***************************************************************************/ |
| 17 |
|
| 18 |
#define _GNU_SOURCE |
| 19 |
|
| 20 |
#include <stdio.h> |
| 21 |
#include <fcntl.h> |
| 22 |
#include <unistd.h> |
| 23 |
#include <errno.h> |
| 24 |
#include <getopt.h> |
| 25 |
#include <ctype.h> |
| 26 |
#include <math.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <string.h> |
| 29 |
#include <assert.h> |
| 30 |
|
| 31 |
#include "calib.h" |
| 32 |
|
| 33 |
char ni_labpc_id[] = "$Id$"; |
| 34 |
|
| 35 |
int ni_labpc_setup( calibration_setup_t *setup , const char *device_name ); |
| 36 |
static int cal_ni_labpc( calibration_setup_t *setup ); |
| 37 |
|
| 38 |
enum labpc_caldacs |
| 39 |
{ |
| 40 |
DAC0_GAIN = 1, |
| 41 |
ADC_OFFSET_FINE = 2, |
| 42 |
DAC1_GAIN = 5, |
| 43 |
ADC_GAIN = 6, |
| 44 |
ADC_OFFSET_COARSE_ALT = 7, |
| 45 |
DAC1_OFFSET = 9, |
| 46 |
ADC_POSTGAIN_OFFSET = 10, |
| 47 |
DAC0_OFFSET_ALT = 11, |
| 48 |
ADC_OFFSET_COARSE = 12, |
| 49 |
DAC0_OFFSET = 13, |
| 50 |
}; |
| 51 |
static inline unsigned int DAC_OFFSET( unsigned int channel ) |
| 52 |
{ |
| 53 |
if( channel ) return DAC1_OFFSET; |
| 54 |
else return DAC0_OFFSET; |
| 55 |
} |
| 56 |
static inline unsigned int DAC_GAIN( unsigned int channel ) |
| 57 |
{ |
| 58 |
if( channel ) return DAC1_GAIN; |
| 59 |
else return DAC0_GAIN; |
| 60 |
} |
| 61 |
|
| 62 |
int ni_labpc_setup( calibration_setup_t *setup , const char *device_name ) |
| 63 |
{ |
| 64 |
if( setup->caldac_subdev < 0 ) |
| 65 |
{ |
| 66 |
fprintf( stderr, "no caldac subdevice found\n"); |
| 67 |
return -1; |
| 68 |
} |
| 69 |
setup->status = STATUS_DONE; |
| 70 |
setup->do_cal = cal_ni_labpc; |
| 71 |
setup_caldacs( setup, setup->caldac_subdev ); |
| 72 |
|
| 73 |
return 0; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
/* load analog input caldacs from eeprom values (depend on range used) */ |
| 78 |
static void labpc_grab_ai_calibration( calibration_setup_t *setup, unsigned int range ) |
| 79 |
{ |
| 80 |
comedi_calibration_setting_t *current_cal; |
| 81 |
/* points to (end of) analog input bipolar calibration values */ |
| 82 |
const int ai_bip_frame = read_eeprom( setup, 127 ); |
| 83 |
enum offset_indices |
| 84 |
{ |
| 85 |
coarse_offset_index = 0, |
| 86 |
fine_offset_index = -1, |
| 87 |
}; |
| 88 |
/* points to (end of) analog input unipolar calibration values */ |
| 89 |
const int ai_unip_frame = read_eeprom( setup, 126 ); |
| 90 |
/* points to (end of) analog input bipolar calibration values */ |
| 91 |
const int bip_gain_frame = read_eeprom( setup, 123 ); |
| 92 |
/* points to (end of) analog input bipolar calibration values */ |
| 93 |
const int unip_gain_frame = read_eeprom( setup, 122 ); |
| 94 |
/* points to (end of) analog input bipolar calibration values */ |
| 95 |
const int bip_offset_frame = read_eeprom( setup, 121 ); |
| 96 |
/* points to (end of) analog input bipolar calibration values */ |
| 97 |
const int unip_offset_frame = read_eeprom( setup, 120 ); |
| 98 |
int ai_frame, gain_frame, offset_frame; |
| 99 |
/* eeprom offsets by range */ |
| 100 |
int range_to_index[ 14 ] = |
| 101 |
{ |
| 102 |
0, |
| 103 |
-2, |
| 104 |
-3, |
| 105 |
-4, |
| 106 |
-5, |
| 107 |
-6, |
| 108 |
-7, |
| 109 |
0, |
| 110 |
-2, |
| 111 |
-3, |
| 112 |
-4, |
| 113 |
-5, |
| 114 |
-6, |
| 115 |
-7, |
| 116 |
}; |
| 117 |
int value; |
| 118 |
|
| 119 |
if( is_unipolar( setup->dev, setup->ad_subdev, 0, range ) ) |
| 120 |
{ |
| 121 |
ai_frame = ai_unip_frame; |
| 122 |
gain_frame = unip_gain_frame; |
| 123 |
offset_frame = unip_offset_frame; |
| 124 |
}else |
| 125 |
{ |
| 126 |
ai_frame = ai_bip_frame; |
| 127 |
gain_frame = bip_gain_frame; |
| 128 |
offset_frame = bip_offset_frame; |
| 129 |
} |
| 130 |
assert( ai_frame >=0 && gain_frame >= 0 && offset_frame >= 0 ); |
| 131 |
|
| 132 |
current_cal = sc_alloc_calibration_setting(setup->new_calibration); |
| 133 |
|
| 134 |
/* load coarse offset */ |
| 135 |
value = read_eeprom( setup, ai_frame + coarse_offset_index ); |
| 136 |
assert( value >= 0 ); |
| 137 |
update_caldac( setup, ADC_OFFSET_COARSE, value ); |
| 138 |
sc_push_caldac( current_cal, setup->caldacs[ ADC_OFFSET_COARSE ] ); |
| 139 |
update_caldac( setup, ADC_OFFSET_COARSE_ALT, value ); |
| 140 |
sc_push_caldac( current_cal, setup->caldacs[ ADC_OFFSET_COARSE_ALT ] ); |
| 141 |
|
| 142 |
/* load fine offset */ |
| 143 |
value = read_eeprom( setup, ai_frame + fine_offset_index ); |
| 144 |
assert( value >= 0 ); |
| 145 |
update_caldac( setup, ADC_OFFSET_FINE, value ); |
| 146 |
sc_push_caldac( current_cal, setup->caldacs[ ADC_OFFSET_FINE ] ); |
| 147 |
|
| 148 |
/* load postgain offset */ |
| 149 |
value = read_eeprom( setup, offset_frame + range_to_index[ range ] ); |
| 150 |
assert( value >= 0 ); |
| 151 |
update_caldac( setup, ADC_POSTGAIN_OFFSET, value ); |
| 152 |
sc_push_caldac( current_cal, setup->caldacs[ ADC_POSTGAIN_OFFSET ] ); |
| 153 |
|
| 154 |
/* load gain */ |
| 155 |
value = read_eeprom( setup, gain_frame + range_to_index[ range ] ); |
| 156 |
assert( value >= 0 ); |
| 157 |
update_caldac( setup, ADC_GAIN, value ); |
| 158 |
sc_push_caldac( current_cal, setup->caldacs[ ADC_GAIN ] ); |
| 159 |
|
| 160 |
current_cal->subdevice = setup->ad_subdev; |
| 161 |
sc_push_channel( current_cal, SC_ALL_CHANNELS ); |
| 162 |
sc_push_range( current_cal, range ); |
| 163 |
sc_push_aref( current_cal, SC_ALL_AREFS ); |
| 164 |
|
| 165 |
DPRINT( 0, "loaded adc range %i calibration from eeprom\n", range ); |
| 166 |
} |
| 167 |
|
| 168 |
static int ao_offset_index( unsigned int channel ) |
| 169 |
{ |
| 170 |
if( channel ) return -2; |
| 171 |
else return 0; |
| 172 |
} |
| 173 |
|
| 174 |
static int ao_gain_index( unsigned int channel ) |
| 175 |
{ |
| 176 |
if( channel ) return -3; |
| 177 |
else return -1; |
| 178 |
} |
| 179 |
|
| 180 |
/* load analog output caldacs from eeprom values (depend on range used) */ |
| 181 |
static void labpc_grab_ao_calibration( calibration_setup_t *setup, |
| 182 |
unsigned int channel, unsigned int range) |
| 183 |
{ |
| 184 |
comedi_calibration_setting_t *current_cal; |
| 185 |
/* points to (end of) analog output bipolar calibration values */ |
| 186 |
int ao_bip_frame = read_eeprom( setup, 125 ); |
| 187 |
/* points to (end of) analog output bipolar calibration values */ |
| 188 |
int ao_unip_frame = read_eeprom( setup, 124 ); |
| 189 |
int ao_frame; |
| 190 |
int value; |
| 191 |
|
| 192 |
current_cal = sc_alloc_calibration_setting(setup->new_calibration); |
| 193 |
|
| 194 |
if( is_unipolar( setup->dev, setup->da_subdev, 0, range ) ) |
| 195 |
ao_frame = ao_unip_frame; |
| 196 |
else |
| 197 |
ao_frame = ao_bip_frame; |
| 198 |
assert( ao_frame >= 0 ); |
| 199 |
|
| 200 |
/* load offset */ |
| 201 |
value = read_eeprom( setup, ao_frame + ao_offset_index( channel ) ); |
| 202 |
assert( value >= 0 ); |
| 203 |
update_caldac( setup, DAC_OFFSET( channel ), value ); |
| 204 |
sc_push_caldac( current_cal, setup->caldacs[ DAC_OFFSET( channel ) ] ); |
| 205 |
if( channel == 0 ) |
| 206 |
{ |
| 207 |
update_caldac( setup, DAC0_OFFSET_ALT, value ); |
| 208 |
sc_push_caldac( current_cal, setup->caldacs[ DAC0_OFFSET_ALT ] ); |
| 209 |
} |
| 210 |
|
| 211 |
// load gain calibration |
| 212 |
value = read_eeprom( setup, ao_frame + ao_gain_index( channel ) ); |
| 213 |
assert( value >= 0 ); |
| 214 |
update_caldac( setup, DAC_GAIN( channel ), value ); |
| 215 |
sc_push_caldac( current_cal, setup->caldacs[ DAC_GAIN( channel ) ] ); |
| 216 |
|
| 217 |
current_cal->subdevice = setup->da_subdev; |
| 218 |
sc_push_channel( current_cal, channel ); |
| 219 |
sc_push_range( current_cal, range ); |
| 220 |
sc_push_aref( current_cal, SC_ALL_AREFS ); |
| 221 |
|
| 222 |
DPRINT( 0, "loaded dac channel %i range %i calibration from eeprom\n", |
| 223 |
channel, range ); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
static int cal_ni_labpc( calibration_setup_t *setup ) |
| 228 |
{ |
| 229 |
int range, channel, num_ai_ranges, num_ao_ranges; |
| 230 |
|
| 231 |
if( comedi_get_version_code( setup->dev ) <= COMEDI_VERSION_CODE( 0, 7, 66 ) ) |
| 232 |
{ |
| 233 |
DPRINT(0, "WARNING: you need comedi driver version 0.7.67 or later\n" |
| 234 |
"for this calibration to work properly\n" ); |
| 235 |
} |
| 236 |
|
| 237 |
num_ai_ranges = comedi_get_n_ranges( setup->dev, setup->ad_subdev, 0 ); |
| 238 |
assert( num_ai_ranges > 0 ); |
| 239 |
num_ao_ranges = comedi_get_n_ranges( setup->dev, setup->da_subdev, 0 ); |
| 240 |
assert( num_ao_ranges > 0 ); |
| 241 |
|
| 242 |
for( range = 0; range < num_ai_ranges; range++ ) |
| 243 |
labpc_grab_ai_calibration( setup, range ); |
| 244 |
|
| 245 |
if( setup->da_subdev >= 0 && setup->do_output ) |
| 246 |
{ |
| 247 |
for( channel = 0; channel < 2; channel++ ) |
| 248 |
{ |
| 249 |
for( range = 0; range < num_ao_ranges; range++ ) |
| 250 |
labpc_grab_ao_calibration( setup, channel, range ); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
return write_calibration_file(setup->cal_save_file_path, setup->new_calibration); |
| 255 |
} |
| 256 |
|