From 13d4399dcdea7a924c752eb74f543b0987b63fd5 Mon Sep 17 00:00:00 2001 From: Frank Mori Hess Date: Sat, 3 May 2003 22:08:24 +0000 Subject: [PATCH] converted calibration of measurement computing 60xx boards over to generic code --- comedi_calibrate/cal_common.c | 210 +++++++++++++++++++++++---- comedi_calibrate/calib.h | 16 ++- comedi_calibrate/cb64.c | 213 +++++++--------------------- comedi_calibrate/comedi_calibrate.c | 12 +- comedi_calibrate/ni.c | 24 ++-- 5 files changed, 268 insertions(+), 207 deletions(-) diff --git a/comedi_calibrate/cal_common.c b/comedi_calibrate/cal_common.c index c0454f1..c70068d 100644 --- a/comedi_calibrate/cal_common.c +++ b/comedi_calibrate/cal_common.c @@ -62,6 +62,8 @@ void generic_prep_adc_caldacs( calibration_setup_t *setup, { reset_caldac( setup, layout->adc_offset( channel ) ); reset_caldac( setup, layout->adc_gain( channel ) ); + reset_caldac( setup, layout->adc_offset_fine( channel ) ); + reset_caldac( setup, layout->adc_gain_fine( channel ) ); }else { retval = comedi_apply_calibration( setup->dev, setup->ad_subdev, @@ -71,6 +73,8 @@ void generic_prep_adc_caldacs( calibration_setup_t *setup, DPRINT( 0, "Failed to apply existing calibration, reseting dac caldacs.\n" ); reset_caldac( setup, layout->adc_offset( channel ) ); reset_caldac( setup, layout->adc_gain( channel ) ); + reset_caldac( setup, layout->adc_offset_fine( channel ) ); + reset_caldac( setup, layout->adc_gain_fine( channel ) ); } } } @@ -98,12 +102,126 @@ void generic_prep_dac_caldacs( calibration_setup_t *setup, } } } +static void generic_do_dac_channel( calibration_setup_t *setup, const generic_layout_t *layout , + saved_calibration_t *current_cal, unsigned int channel, unsigned int range ) +{ + generic_prep_dac_caldacs( setup, layout, channel, range ); + + generic_do_relative( setup, current_cal, layout->dac_ground_observable( setup, channel, range ), + layout->dac_high_observable( setup, channel, range ), layout->dac_gain( channel ) ); + + generic_do_cal( setup, current_cal, layout->dac_ground_observable( setup, channel, range ), + layout->dac_offset( channel ) ); + + current_cal->subdevice = setup->da_subdev; + sc_push_channel( current_cal, channel ); + sc_push_range( current_cal, range ); + sc_push_aref( current_cal, SC_ALL_AREFS ); +} + +static void generic_do_adc_channel( calibration_setup_t *setup, const generic_layout_t *layout , + saved_calibration_t *current_cal, unsigned int channel, unsigned int range ) +{ + generic_prep_adc_caldacs( setup, layout, channel, range ); + + generic_do_relative( setup, current_cal, layout->adc_high_observable( setup, channel, range ), + layout->adc_ground_observable( setup, channel, range ), layout->adc_gain( channel ) ); + + generic_do_cal( setup, current_cal, layout->adc_ground_observable( setup, channel, range ), + layout->adc_offset( channel ) ); + + generic_do_relative( setup, current_cal, layout->adc_high_observable( setup, channel, range ), + layout->adc_ground_observable( setup, channel, range ), layout->adc_gain_fine( channel ) ); + + generic_do_cal( setup, current_cal, layout->adc_ground_observable( setup, channel, range ), + layout->adc_offset_fine( channel ) ); + + current_cal->subdevice = setup->ad_subdev; + sc_push_channel( current_cal, channel ); + sc_push_range( current_cal, range ); + sc_push_aref( current_cal, SC_ALL_AREFS ); +} + +static int calibration_is_valid( const saved_calibration_t *saved_cal, + unsigned int subdevice, unsigned int channel, unsigned int range ) +{ + int i; + + if( saved_cal->subdevice != subdevice ) return 0; + if( saved_cal->channels_length ) + { + for( i = 0; i < saved_cal->channels_length; i++ ) + { + if( saved_cal->channels[ i ] == channel ) + break; + } + if( i == saved_cal->channels_length ) return 0; + } + if( saved_cal->ranges_length ) + { + for( i = 0; i < saved_cal->ranges_length; i++ ) + { + if( saved_cal->ranges[ i ] == range ) + break; + } + if( i == saved_cal->ranges_length ) return 0; + } + return 1; +} + +static void apply_calibration( calibration_setup_t *setup, saved_calibration_t *saved_cals, + unsigned int saved_cals_length, unsigned int subdevice, unsigned int channel, + unsigned int range ) +{ + int i, retval; + + for( i = 0; i < saved_cals_length; i++ ) + { + int j; + + if( calibration_is_valid( &saved_cals[ i ], subdevice, channel, range ) ) + { + for( j = 0; j < saved_cals[ i ].caldacs_length; j++ ) + { + caldac_t *caldac; + + caldac = &saved_cals[ i ].caldacs[ j ]; + retval = comedi_data_write( setup->dev, caldac->subdev, caldac->chan, + 0, 0, caldac->current ); + assert( retval >= 0 ); + } + } + } +} + +static void generic_prep_adc_for_dac( calibration_setup_t *setup, const generic_layout_t *layout, + saved_calibration_t *saved_cals, unsigned int saved_cals_length, + unsigned int dac_channel, unsigned int dac_range ) +{ + unsigned int adc_channel, adc_range; + int i; + int chanspec; + + for( i = 0; i < setup->n_observables; i++ ) + { + chanspec = setup->observables[ i ].preobserve_insn.chanspec; + if( CR_CHAN( chanspec ) == dac_channel && CR_RANGE( chanspec ) == dac_range ) + break; + } + assert( i < setup->n_observables ); + chanspec = setup->observables[ i ].observe_insn.chanspec; + adc_channel = CR_CHAN( chanspec ); + adc_range = CR_RANGE( chanspec ); + + apply_calibration( setup, saved_cals, saved_cals_length, setup->ad_subdev, + adc_channel, adc_range ); +} int generic_cal_by_channel_and_range( calibration_setup_t *setup, const generic_layout_t *layout ) { int range, channel, num_ai_ranges, num_ai_channels, num_ao_ranges, - num_ao_channels, retval, num_calibrations, i; + num_ao_channels, retval, num_calibrations, num_ai_calibrations, i; saved_calibration_t *saved_cals, *current_cal; assert( comedi_range_is_chan_specific( setup->dev, setup->ad_subdev ) == 0 ); @@ -126,7 +244,8 @@ int generic_cal_by_channel_and_range( calibration_setup_t *setup, }else num_ao_ranges = num_ao_channels = 0; - num_calibrations = num_ai_ranges * num_ai_channels + num_ao_ranges * num_ao_channels; + num_ai_calibrations = num_ai_ranges * num_ai_channels; + num_calibrations = num_ai_calibrations + num_ao_ranges * num_ao_channels; saved_cals = malloc( num_calibrations * sizeof( saved_calibration_t ) ); if( saved_cals == NULL ) return -1; @@ -137,21 +256,7 @@ int generic_cal_by_channel_and_range( calibration_setup_t *setup, { for( range = 0; range < num_ai_ranges; range++ ) { - generic_prep_adc_caldacs( setup, layout, channel, range ); - - generic_do_cal( setup, current_cal, layout->adc_ground_observable( channel, range ), - layout->adc_offset( channel ) ); - - generic_do_cal( setup, current_cal, layout->adc_high_observable( channel, range ), - layout->adc_gain( channel ) ); - - current_cal->subdevice = setup->ad_subdev; - sc_push_caldac( current_cal, setup->caldacs[ layout->adc_gain( channel ) ] ); - sc_push_caldac( current_cal, setup->caldacs[ layout->adc_offset( channel ) ] ); - sc_push_channel( current_cal, channel ); - sc_push_range( current_cal, range ); - sc_push_aref( current_cal, SC_ALL_AREFS ); - + generic_do_adc_channel( setup, layout, current_cal, channel, range ); current_cal++; } } @@ -159,25 +264,67 @@ int generic_cal_by_channel_and_range( calibration_setup_t *setup, { for( range = 0; range < num_ao_ranges; range++ ) { - generic_prep_dac_caldacs( setup, layout, channel, range ); + generic_prep_adc_for_dac( setup, layout, saved_cals, num_ai_calibrations, + channel, range ); + generic_do_dac_channel( setup, layout, current_cal, channel, range ); + current_cal++; + } + } + + retval = write_calibration_file( setup, saved_cals, num_calibrations ); + for( i = 0; i < num_calibrations; i++ ) + clear_saved_calibration( &saved_cals[ i ] ); + free( saved_cals ); + return retval; +} + +int generic_cal_by_range( calibration_setup_t *setup, + const generic_layout_t *layout ) +{ + int channel, range, num_ai_ranges, num_ao_ranges, + num_ao_channels, retval, num_calibrations, i; + saved_calibration_t *saved_cals, *current_cal; + + assert( comedi_range_is_chan_specific( setup->dev, setup->ad_subdev ) == 0 ); + + num_ai_ranges = comedi_get_n_ranges( setup->dev, setup->ad_subdev, 0 ); + if( num_ai_ranges < 0 ) return -1; - generic_do_cal( setup, current_cal, layout->dac_ground_observable( channel, range ), - layout->dac_offset( channel ) ); + if( setup->da_subdev && setup->do_output ) + { + assert( comedi_range_is_chan_specific( setup->dev, setup->da_subdev ) == 0 ); + + num_ao_ranges = comedi_get_n_ranges( setup->dev, setup->da_subdev, 0 ); + if( num_ao_ranges < 0 ) return -1; + + num_ao_channels = comedi_get_n_channels( setup->dev, setup->da_subdev ); + if( num_ao_channels < 0 ) return -1; + }else + num_ao_ranges = num_ao_channels = 0; - generic_do_cal( setup, current_cal, layout->dac_high_observable( channel, range ), - layout->dac_gain( channel ) ); + num_calibrations = num_ai_ranges + num_ao_ranges * num_ao_channels; - current_cal->subdevice = setup->da_subdev; - sc_push_caldac( current_cal, setup->caldacs[ layout->dac_gain( channel ) ] ); - sc_push_caldac( current_cal, setup->caldacs[ layout->dac_offset( channel ) ] ); - sc_push_channel( current_cal, channel ); - sc_push_range( current_cal, range ); - sc_push_aref( current_cal, SC_ALL_AREFS ); + saved_cals = malloc( num_calibrations * sizeof( saved_calibration_t ) ); + if( saved_cals == NULL ) return -1; + + current_cal = saved_cals; + for( range = 0; range < num_ai_ranges; range++ ) + { + generic_do_adc_channel( setup, layout, current_cal, 0, range ); + sc_push_channel( current_cal, SC_ALL_CHANNELS ); + current_cal++; + } + for( channel = 0; channel < num_ao_channels; channel++ ) + { + for( range = 0; range < num_ao_ranges; range++ ) + { + generic_prep_adc_for_dac( setup, layout, saved_cals, num_ai_ranges, + channel, range ); + generic_do_dac_channel( setup, layout, current_cal, channel, range ); current_cal++; } } - retval = write_calibration_file( setup, saved_cals, num_calibrations ); for( i = 0; i < num_calibrations; i++ ) clear_saved_calibration( &saved_cals[ i ] ); @@ -189,7 +336,8 @@ static int dummy_caldac( unsigned int channel ) { return -1; } -static int dummy_observable( unsigned int channel, unsigned int range ) +static int dummy_observable( const calibration_setup_t *setup, + unsigned int channel, unsigned int range ) { return -1; } @@ -197,6 +345,8 @@ void init_generic_layout( generic_layout_t *layout ) { layout->adc_offset = dummy_caldac; layout->adc_gain = dummy_caldac; + layout->adc_offset_fine = dummy_caldac; + layout->adc_gain_fine = dummy_caldac; layout->dac_offset = dummy_caldac; layout->dac_gain = dummy_caldac; layout->adc_high_observable = dummy_observable; diff --git a/comedi_calibrate/calib.h b/comedi_calibrate/calib.h index 0038982..3cf1253 100644 --- a/comedi_calibrate/calib.h +++ b/comedi_calibrate/calib.h @@ -250,17 +250,25 @@ void clear_saved_calibration( saved_calibration_t *saved_cal ); typedef struct { int (*adc_offset)( unsigned int channel ); + int (*adc_offset_fine)( unsigned int channel ); int (*adc_gain)( unsigned int channel ); + int (*adc_gain_fine)( unsigned int channel ); int (*dac_offset)( unsigned int channel ); int (*dac_gain)( unsigned int channel ); - int (*adc_high_observable)( unsigned int channel, unsigned int range ); - int (*adc_ground_observable)( unsigned int channel, unsigned int range ); - int (*dac_high_observable)( unsigned int channel, unsigned int range ); - int (*dac_ground_observable)( unsigned int channel, unsigned int range ); + int (*adc_high_observable)( const calibration_setup_t *setup, + unsigned int channel, unsigned int range ); + int (*adc_ground_observable)( const calibration_setup_t *setup, + unsigned int channel, unsigned int range ); + int (*dac_high_observable)( const calibration_setup_t *setup, + unsigned int channel, unsigned int range ); + int (*dac_ground_observable)( const calibration_setup_t *setup, + unsigned int channel, unsigned int range ); } generic_layout_t; void init_generic_layout( generic_layout_t *layout ); int generic_cal_by_channel_and_range( calibration_setup_t *setup, const generic_layout_t *layout ); +int generic_cal_by_range( calibration_setup_t *setup, + const generic_layout_t *layout ); void generic_do_cal( calibration_setup_t *setup, saved_calibration_t *saved_cal, int observable, int caldac ); void generic_do_relative( calibration_setup_t *setup, diff --git a/comedi_calibrate/cb64.c b/comedi_calibrate/cb64.c index 7a17896..b156e0d 100644 --- a/comedi_calibrate/cb64.c +++ b/comedi_calibrate/cb64.c @@ -399,17 +399,19 @@ static int init_observables_64xx( calibration_setup_t *setup ) return 0; } -static unsigned int ai_high_observable_index_60xx( unsigned int ai_range ) +static int ai_high_observable_index_60xx( const calibration_setup_t *setup, + unsigned int channel, unsigned int ai_range ) { return ai_range * 2 + 1; } -static unsigned int ai_ground_observable_index_60xx( unsigned int ai_range ) +static int ai_ground_observable_index_60xx( const calibration_setup_t *setup, + unsigned int channel, unsigned int ai_range ) { return ai_range * 2; } -static unsigned int ao_high_observable_index_60xx( const calibration_setup_t *setup, +static int ao_high_observable_index_60xx( const calibration_setup_t *setup, unsigned int channel, unsigned int ao_range ) { int num_ai_ranges; @@ -420,7 +422,7 @@ static unsigned int ao_high_observable_index_60xx( const calibration_setup_t *se return 2 * num_ai_ranges + 2 * channel + 4 * ao_range + 1; } -static unsigned int ao_low_observable_index_60xx( const calibration_setup_t *setup, +static int ao_ground_observable_index_60xx( const calibration_setup_t *setup, unsigned int channel, unsigned int ao_range ) { int num_ai_ranges; @@ -532,7 +534,7 @@ static int init_observables_60xx( calibration_setup_t *setup ) for( i = 0; i < num_ranges; i++ ) { - o = setup->observables + ai_ground_observable_index_60xx( i ); + o = setup->observables + ai_ground_observable_index_60xx( setup, 0, i ); o->reference_source = CAL_SRC_GROUND; assert( o->name == NULL ); asprintf( &o->name, "calibration source %i, range %i, ground referenced", @@ -542,7 +544,7 @@ static int init_observables_60xx( calibration_setup_t *setup ) o->target = 0.0; setup->n_observables++; - o = setup->observables + ai_high_observable_index_60xx( i ); + o = setup->observables + ai_high_observable_index_60xx( setup, 0, i ); retval = high_ai_cal_src_60xx( setup, i ); if( retval < 0 ) return -1; o->reference_source = retval; @@ -574,7 +576,7 @@ static int init_observables_60xx( calibration_setup_t *setup ) int dac_chan; for( dac_chan = 0; dac_chan < 2; dac_chan++ ) { - o = setup->observables + ao_low_observable_index_60xx( setup, + o = setup->observables + ao_ground_observable_index_60xx( setup, dac_chan, i ); o->reference_source = ao_cal_src_60xx( dac_chan ); assert( o->name == NULL ); @@ -584,7 +586,7 @@ static int init_observables_60xx( calibration_setup_t *setup ) o->preobserve_insn.data = o->preobserve_data; o->observe_insn = tmpl; o->observe_insn.chanspec = CR_PACK( 0, 0, AREF_GROUND) | CR_ALT_SOURCE | CR_ALT_FILTER; - set_target( setup, ao_low_observable_index_60xx( setup, dac_chan, i ), 0.0 ); + set_target( setup, ao_ground_observable_index_60xx( setup, dac_chan, i ), 0.0 ); setup->n_observables++; o = setup->observables + ao_high_observable_index_60xx( setup, dac_chan, i ); @@ -605,16 +607,16 @@ static int init_observables_60xx( calibration_setup_t *setup ) return 0; } -static int ai_low_observable_index_4020( unsigned int channel, - unsigned int ai_range ) +static int ai_low_observable_index_4020( const calibration_setup_t *setup, + unsigned int channel, unsigned int ai_range ) { return 4 * channel + 2 * ai_range; } -static int ai_high_observable_index_4020( unsigned int channel, - unsigned int ai_range ) +static int ai_high_observable_index_4020( const calibration_setup_t *setup, + unsigned int channel, unsigned int ai_range ) { - return ai_low_observable_index_4020( channel, ai_range ) + 1; + return ai_low_observable_index_4020( setup, channel, ai_range ) + 1; } static int high_ai_cal_src_4020( calibration_setup_t *setup, unsigned int ai_range ) @@ -677,7 +679,7 @@ static int init_observables_4020( calibration_setup_t *setup ) { for( range = 0; range < num_ranges; range++ ) { - o = setup->observables + ai_low_observable_index_4020( channel, range ); + o = setup->observables + ai_low_observable_index_4020( setup, channel, range ); assert( o->name == NULL ); asprintf( &o->name, "ground calibration source, ch %i, range %i, ground referenced", channel, range ); @@ -687,7 +689,7 @@ static int init_observables_4020( calibration_setup_t *setup ) o->target = 0.0; setup->n_observables++; - o = setup->observables + ai_high_observable_index_4020( channel, range ); + o = setup->observables + ai_high_observable_index_4020( setup, channel, range ); retval = high_ai_cal_src_4020( setup, range ); if( retval < 0 ) return -1; o->reference_source = retval; @@ -867,159 +869,48 @@ static int cal_cb_pci_64xx( calibration_setup_t *setup ) return retval; } -static void prep_adc_caldacs_60xx( calibration_setup_t *setup, unsigned int range ) +static int adc_gain_coarse_60xx( unsigned int channel ) { - int retval; - - if( setup->do_reset ) - { - reset_caldac( setup, ADC_OFFSET_COARSE_60XX ); - reset_caldac( setup, ADC_OFFSET_FINE_60XX ); - reset_caldac( setup, ADC_GAIN_COARSE_60XX ); - reset_caldac( setup, ADC_GAIN_FINE_60XX ); - }else - { - retval = comedi_apply_calibration( setup->dev, setup->ad_subdev, - 0, range, AREF_GROUND, setup->cal_save_file_path); - if( retval < 0 ) - { - reset_caldac( setup, ADC_OFFSET_COARSE_60XX ); - reset_caldac( setup, ADC_OFFSET_FINE_60XX ); - reset_caldac( setup, ADC_GAIN_COARSE_60XX ); - reset_caldac( setup, ADC_GAIN_FINE_60XX ); - } - } + return ADC_GAIN_COARSE_60XX; } - -static void prep_dac_caldacs_60xx( calibration_setup_t *setup, - unsigned int channel, unsigned int range ) +static int adc_gain_fine_60xx( unsigned int channel ) { - int retval; - - if( setup->do_reset ) - { - if( channel == 0 ) - { - reset_caldac( setup, DAC0_OFFSET_60XX ); - reset_caldac( setup, DAC0_GAIN_60XX ); - }else - { - reset_caldac( setup, DAC1_OFFSET_60XX ); - reset_caldac( setup, DAC1_GAIN_60XX ); - } - }else - { - retval = comedi_apply_calibration( setup->dev, setup->da_subdev, - channel, range, AREF_GROUND, setup->cal_save_file_path); - if( retval < 0 ) - { - if( channel == 0 ) - { - reset_caldac( setup, DAC0_OFFSET_60XX ); - reset_caldac( setup, DAC0_GAIN_60XX ); - }else - { - reset_caldac( setup, DAC1_OFFSET_60XX ); - reset_caldac( setup, DAC1_GAIN_60XX ); - } - } - } + return ADC_GAIN_FINE_60XX; +} +static int adc_offset_coarse_60xx( unsigned int channel ) +{ + return ADC_OFFSET_COARSE_60XX; +} +static int adc_offset_fine_60xx( unsigned int channel ) +{ + return ADC_OFFSET_FINE_60XX; +} +static int dac_gain_60xx( unsigned int channel ) +{ + if( channel ) return DAC1_GAIN_60XX; + else return DAC0_GAIN_60XX; +} +static int dac_offset_60xx( unsigned int channel ) +{ + if( channel ) return DAC1_OFFSET_60XX; + else return DAC0_OFFSET_60XX; } - static int cal_cb_pci_60xx( calibration_setup_t *setup ) { - saved_calibration_t *saved_cals, *current_cal; - int i, num_ai_ranges, num_ao_ranges, num_calibrations, retval; - int adc_offset_fine_for_ao = -1, adc_offset_coarse_for_ao = -1, - adc_gain_fine_for_ao = -1, adc_gain_coarse_for_ao = -1; - static const int ai_range_for_ao = 0; - - num_ai_ranges = comedi_get_n_ranges( setup->dev, setup->ad_subdev, 0 ); - if( num_ai_ranges < 1 ) return -1; - if( setup->da_subdev >= 0 ) - { - num_ao_ranges = comedi_get_n_ranges( setup->dev, setup->da_subdev, 0 ); - if( num_ai_ranges < 1 ) return -1; - }else - num_ao_ranges = 0; - - num_calibrations = num_ai_ranges + 2 * num_ao_ranges; - - saved_cals = malloc( num_calibrations * sizeof( saved_calibration_t ) ); - if( saved_cals == NULL ) return -1; - - memset( saved_cals, 0, num_calibrations * sizeof( saved_calibration_t ) ); - - current_cal = saved_cals; - for( i = 0; i < num_ai_ranges ; i++ ) - { - prep_adc_caldacs_60xx( setup, i ); - - cal_binary( setup, ai_ground_observable_index_60xx( i ), ADC_OFFSET_COARSE_60XX ); - cal_binary( setup, ai_ground_observable_index_60xx( i ), ADC_OFFSET_FINE_60XX ); - cal_binary( setup, ai_high_observable_index_60xx( i ), ADC_GAIN_COARSE_60XX ); - cal_binary( setup, ai_high_observable_index_60xx( i ), ADC_GAIN_FINE_60XX ); - - current_cal->subdevice = setup->ad_subdev; - sc_push_caldac( current_cal, setup->caldacs[ ADC_OFFSET_FINE_60XX ] ); - sc_push_caldac( current_cal, setup->caldacs[ ADC_OFFSET_COARSE_60XX ] ); - sc_push_caldac( current_cal, setup->caldacs[ ADC_GAIN_FINE_60XX ] ); - sc_push_caldac( current_cal, setup->caldacs[ ADC_GAIN_COARSE_60XX ] ); - sc_push_channel( current_cal, SC_ALL_CHANNELS ); - sc_push_range( current_cal, i ); - sc_push_aref( current_cal, SC_ALL_AREFS ); - - current_cal++; - - if( i == ai_range_for_ao ) - { - adc_offset_fine_for_ao = setup->caldacs[ ADC_OFFSET_FINE_60XX ].current; - adc_offset_coarse_for_ao = setup->caldacs[ ADC_OFFSET_COARSE_60XX ].current; - adc_gain_fine_for_ao = setup->caldacs[ ADC_GAIN_FINE_60XX ].current; - adc_gain_coarse_for_ao = setup->caldacs[ ADC_GAIN_COARSE_60XX ].current; - } - } - - update_caldac( setup, ADC_OFFSET_FINE_60XX, adc_offset_fine_for_ao ); - update_caldac( setup, ADC_OFFSET_COARSE_60XX, adc_offset_coarse_for_ao ); - update_caldac( setup, ADC_GAIN_FINE_60XX, adc_gain_fine_for_ao ); - update_caldac( setup, ADC_GAIN_COARSE_60XX, adc_gain_coarse_for_ao ); - for( i = 0; i < num_ao_ranges && setup->do_output; i++ ) - { - prep_dac_caldacs_60xx( setup, 0, i ); - - cal_binary( setup, ao_low_observable_index_60xx( setup, 0, i ), DAC0_OFFSET_60XX ); - cal_binary( setup, ao_high_observable_index_60xx( setup, 0, i ), DAC0_GAIN_60XX ); - - current_cal->subdevice = setup->da_subdev; - sc_push_caldac( current_cal, setup->caldacs[ DAC0_OFFSET_60XX ] ); - sc_push_caldac( current_cal, setup->caldacs[ DAC0_GAIN_60XX ] ); - sc_push_channel( current_cal, 0 ); - sc_push_range( current_cal, i ); - sc_push_aref( current_cal, SC_ALL_AREFS ); - - current_cal++; - - prep_dac_caldacs_60xx( setup, 1, i ); - - cal_binary( setup, ao_low_observable_index_60xx( setup, 1, i ), DAC1_OFFSET_60XX ); - cal_binary( setup, ao_high_observable_index_60xx( setup, 1, i ), DAC1_GAIN_60XX ); - - current_cal->subdevice = setup->da_subdev; - sc_push_caldac( current_cal, setup->caldacs[ DAC1_OFFSET_60XX ] ); - sc_push_caldac( current_cal, setup->caldacs[ DAC1_GAIN_60XX ] ); - sc_push_channel( current_cal, 1 ); - sc_push_range( current_cal, i ); - sc_push_aref( current_cal, SC_ALL_AREFS ); - - current_cal++; - } + generic_layout_t layout; - retval = write_calibration_file( setup, saved_cals, num_calibrations ); - for( i = 0; i < num_calibrations; i++ ) - clear_saved_calibration( &saved_cals[ i ] ); - free( saved_cals ); - return retval; + init_generic_layout( &layout ); + layout.adc_gain = adc_gain_coarse_60xx; + layout.adc_gain_fine = adc_gain_fine_60xx; + layout.adc_offset = adc_offset_coarse_60xx; + layout.adc_offset_fine = adc_offset_fine_60xx; + layout.dac_gain = dac_gain_60xx; + layout.dac_offset = dac_offset_60xx; + layout.adc_high_observable = ai_high_observable_index_60xx; + layout.adc_ground_observable = ai_ground_observable_index_60xx; + layout.dac_high_observable = ao_high_observable_index_60xx; + layout.dac_ground_observable = ao_ground_observable_index_60xx; + return generic_cal_by_range( setup, &layout ); } static int adc_offset_4020( unsigned int channel ) diff --git a/comedi_calibrate/comedi_calibrate.c b/comedi_calibrate/comedi_calibrate.c index 5b3d858..1fd2289 100644 --- a/comedi_calibrate/comedi_calibrate.c +++ b/comedi_calibrate/comedi_calibrate.c @@ -249,7 +249,15 @@ ok: if(do_reset)reset_caldacs( &setup ); if(do_dump) observe( &setup ); - if(do_calibrate && setup.do_cal) setup.do_cal( &setup ); + if(do_calibrate && setup.do_cal) + { + retval = setup.do_cal( &setup ); + if( retval < 0 ) + { + fprintf( stderr, "calibration function returned error\n" ); + return -1; + } + } if(do_results) observe( &setup ); comedi_close(dev); @@ -788,8 +796,8 @@ void setup_caldacs( calibration_setup_t *setup, int caldac_subdev ) void reset_caldac( calibration_setup_t *setup, int caldac_index ) { - assert( caldac_index < setup->n_caldacs ); if( caldac_index < 0 ) return; + assert( caldac_index < setup->n_caldacs ); update_caldac( setup, caldac_index, setup->caldacs[ caldac_index ].maxdata / 2 ); } diff --git a/comedi_calibrate/ni.c b/comedi_calibrate/ni.c index 629522d..1d4f744 100644 --- a/comedi_calibrate/ni.c +++ b/comedi_calibrate/ni.c @@ -174,18 +174,22 @@ static inline unsigned int ni_ao_unip_linearity( unsigned int channel ) } static const int num_ao_observables_611x = 4; -static int ni_ao_zero_offset_611x( unsigned int channel, unsigned int range ) { +static int ni_ao_zero_offset_611x( const calibration_setup_t *setup, + unsigned int channel, unsigned int range ) { assert( range = 0 ); return 2 * channel; }; -static int ni_ao_reference_611x( unsigned int channel, unsigned int range ) { +static int ni_ao_reference_611x( const calibration_setup_t *setup, + unsigned int channel, unsigned int range ) { assert( range = 0 ); return 2 * channel + 1; }; -static int ni_zero_offset_611x( unsigned int channel, unsigned int range ) { +static int ni_zero_offset_611x( const calibration_setup_t *setup, + unsigned int channel, unsigned int range ) { return num_ao_observables_611x + 8 * range + 2 * channel; }; -static int ni_reference_611x( unsigned int channel, unsigned int range ) { +static int ni_reference_611x( const calibration_setup_t *setup, + unsigned int channel, unsigned int range ) { return num_ao_observables_611x + 8 * range + 2 * channel + 1; }; @@ -561,7 +565,7 @@ static void ni_setup_observables_611x( calibration_setup_t *setup ) for( range = 0; range < num_ai_ranges; range++ ) { /* 0 offset */ - o = setup->observables + ni_zero_offset_611x( channel, range ); + o = setup->observables + ni_zero_offset_611x( setup, channel, range ); assert( o->name == NULL ); asprintf( &o->name, "ai, ch %i, range %i, zero offset", channel, range ); @@ -572,7 +576,7 @@ static void ni_setup_observables_611x( calibration_setup_t *setup ) o->target = 0.0; /* voltage reference */ - o = setup->observables + ni_reference_611x( channel, range ); + o = setup->observables + ni_reference_611x( setup, channel, range ); assert( o->name == NULL ); asprintf( &o->name, "ai, ch %i, range %i, voltage reference", channel, range ); @@ -593,7 +597,7 @@ static void ni_setup_observables_611x( calibration_setup_t *setup ) static const int ai_range_for_ao = 2; /* ao zero offset */ - o = setup->observables + ni_ao_zero_offset_611x( channel, 0 ); + o = setup->observables + ni_ao_zero_offset_611x( setup, channel, 0 ); assert( o->name == NULL ); asprintf( &o->name, "ao ch %i, zero offset", channel ); o->preobserve_insn = po_tmpl; @@ -603,10 +607,10 @@ static void ni_setup_observables_611x( calibration_setup_t *setup ) o->observe_insn.chanspec = CR_PACK( 0, ai_range_for_ao, AREF_DIFF ) | CR_ALT_SOURCE | CR_ALT_FILTER; o->reference_source = REF_DAC0_GND; - set_target( setup, ni_ao_zero_offset_611x( channel, 0 ), 0.0 ); + set_target( setup, ni_ao_zero_offset_611x( setup, channel, 0 ), 0.0 ); /* ao gain */ - o = setup->observables + ni_ao_reference_611x( channel, 0 ); + o = setup->observables + ni_ao_reference_611x( setup, channel, 0 ); assert( o->name == NULL ); asprintf( &o->name, "ao ch %i, reference voltage", channel ); o->preobserve_insn = po_tmpl; @@ -616,7 +620,7 @@ static void ni_setup_observables_611x( calibration_setup_t *setup ) o->observe_insn.chanspec = CR_PACK( 0, ai_range_for_ao, AREF_DIFF ) | CR_ALT_SOURCE | CR_ALT_FILTER; o->reference_source = REF_DAC0_GND; - set_target( setup, ni_ao_reference_611x( channel, 0 ), 5.0 ); + set_target( setup, ni_ao_reference_611x( setup, channel, 0 ), 5.0 ); } setup->n_observables = num_ao_observables_611x + 2 * num_ai_ranges * num_ai_channels; -- 2.26.2