From 4c5ce6de85b3625a09fa0ad538ef08fd9d028e0d Mon Sep 17 00:00:00 2001 From: "Michael D. M. Dryden" <mdryden@chem.utoronto.ca> Date: Tue, 2 Feb 2016 15:18:38 -0500 Subject: [PATCH] Move experiment TCs to not conflict with shutter. --- DSTAT/src/experiment.c | 201 ++++++++++++++++++++--------------------- DSTAT/src/experiment.h | 4 + 2 files changed, 104 insertions(+), 101 deletions(-) diff --git a/DSTAT/src/experiment.c b/DSTAT/src/experiment.c index dcb5c23..2d2bad9 100644 --- a/DSTAT/src/experiment.c +++ b/DSTAT/src/experiment.c @@ -563,14 +563,13 @@ uint8_t lsv_experiment(int16_t start, int16_t stop, uint16_t slope, int8_t first if (first_run == 1 || first_run == 2){ volt_exp_start(); ads1255_rdatac(); - tc_enable(&TCC1); - ads1255_sync(); - tc_enable(&TCC0); - tc_set_overflow_interrupt_callback(&TCC0, tcf0_ovf_callback); - tc_set_overflow_interrupt_callback(&TCC1, tce1_ovf_callback_lsv); - tc_set_cca_interrupt_callback(&TCC1, lsv_cca_callback); + tc_enable(&EXP_TC0_0); + tc_enable(&EXP_TC1_0); + tc_set_overflow_interrupt_callback(&EXP_TC0_0, tcf0_ovf_callback); + tc_set_overflow_interrupt_callback(&EXP_TC1_0, tce1_ovf_callback_lsv); + tc_set_cca_interrupt_callback(&EXP_TC1_0, lsv_cca_callback); portd_int0_callback = porte_int0_lsv; //ADC read //set EVCH0 event @@ -612,33 +611,33 @@ uint8_t lsv_experiment(int16_t start, int16_t stop, uint16_t slope, int8_t first } ads1255_wakeup(); - tc_write_period(&TCC1, 0xffff); - tc_write_period(&TCC0, (uint16_t)timer_period); + tc_write_period(&EXP_TC1_0, 0xffff); + tc_write_period(&EXP_TC0_0, (uint16_t)timer_period); } - TCC1.CNT = dacindex_start; + EXP_TC1_0.CNT = dacindex_start; if (stop > start) { up = 1; - tc_set_direction(&TCC1, TC_UP); + tc_set_direction(&EXP_TC1_0, TC_UP); } else { up = -1; - tc_set_direction(&TCC1, TC_DOWN); + tc_set_direction(&EXP_TC1_0, TC_DOWN); } - tc_write_cc(&TCC1, TC_CCA, dacindex_stop); - tc_enable_cc_channels(&TCC1, TC_CCAEN); - TCC0.CNT = 0; + tc_write_cc(&EXP_TC1_0, TC_CCA, dacindex_stop); + tc_enable_cc_channels(&EXP_TC1_0, TC_CCAEN); + EXP_TC0_0.CNT = 0; - tc_set_cca_interrupt_level(&TCC1, TC_INT_LVL_HI); //Stop experiment - tc_set_overflow_interrupt_level(&TCC0, TC_OVFINTLVL_LO_gc); //Set DAC + tc_set_cca_interrupt_level(&EXP_TC1_0, TC_INT_LVL_HI); //Stop experiment + tc_set_overflow_interrupt_level(&EXP_TC0_0, TC_OVFINTLVL_LO_gc); //Set DAC PORTD.INTCTRL = PORT_INT0LVL_MED_gc; //ADC read - tc_write_clock_source(&TCC1, TC_CLKSEL_EVCH0_gc); + tc_write_clock_source(&EXP_TC1_0, TC_CLKSEL_EVCH0_gc); //Experiment run with interrupts while (up != 0){ @@ -655,9 +654,9 @@ uint8_t lsv_experiment(int16_t start, int16_t stop, uint16_t slope, int8_t first if (first_run == -1 || first_run == 2) { aborting: - tc_disable(&TCC0); - TCC0.CNT = 0x0; - tc_disable(&TCC1); + tc_disable(&EXP_TC0_0); + EXP_TC0_0.CNT = 0x0; + tc_disable(&EXP_TC1_0); volt_exp_stop(); ads1255_standby(); return ret; @@ -679,7 +678,7 @@ static void porte_int0_lsv(void){ data.result = ads1255_read_fast24(); static uint16_t last_value = 0; - uint32_t current = TCC1.CNT; + uint32_t current = EXP_TC1_0.CNT; data.index = (current+last_value)>>1; //DAC value is average of current and last timer - approximation of center of averaging window printf("B\n"); @@ -691,21 +690,21 @@ static void porte_int0_lsv(void){ } static void tcf0_ovf_callback(void){ - max5443_set_voltage1(TCC1.CNT); + max5443_set_voltage1(EXP_TC1_0.CNT); } static void tce1_ovf_callback_lsv(void){ PORTD.INTCTRL = PORT_INT0LVL_OFF_gc; - tc_set_overflow_interrupt_level(&TCC0, TC_OVFINTLVL_OFF_gc); - tc_set_overflow_interrupt_level(&TCC1, TC_OVFINTLVL_OFF_gc); + tc_set_overflow_interrupt_level(&EXP_TC0_0, TC_OVFINTLVL_OFF_gc); + tc_set_overflow_interrupt_level(&EXP_TC1_0, TC_OVFINTLVL_OFF_gc); up = 0; return; } static void lsv_cca_callback(void){ PORTD.INTCTRL = PORT_INT0LVL_OFF_gc; - tc_set_overflow_interrupt_level(&TCC0, TC_OVFINTLVL_OFF_gc); - tc_set_cca_interrupt_level(&TCC1, TC_INT_LVL_OFF); + tc_set_overflow_interrupt_level(&EXP_TC0_0, TC_OVFINTLVL_OFF_gc); + tc_set_cca_interrupt_level(&EXP_TC1_0, TC_INT_LVL_OFF); up = 0; return; } @@ -729,23 +728,23 @@ void pot_experiment(uint16_t time_seconds, uint8_t exp_type){ portd_int0_callback = portd_int0_ca; //ADC interrupt - tc_enable(&TCC0); + tc_enable(&EXP_TC0_0); ads1255_mux(ADS_MUX_POT); ads1255_rdatac(); ads1255_wakeup(); - tc_write_period(&TCC0,0xffff); - tc_write_clock_source(&TCC0, TC_CLKSEL_EVCH0_gc); - tc_set_direction(&TCC0, TC_UP); + tc_write_period(&EXP_TC0_0,0xffff); + tc_write_clock_source(&EXP_TC0_0, TC_CLKSEL_EVCH0_gc); + tc_set_direction(&EXP_TC0_0, TC_UP); up = 1; if (time_seconds >= 1){ //only enable interrupt if non-zero timeout specified - tc_set_cca_interrupt_callback(&TCC0, ca_cca_callback); - tc_write_cc(&TCC0, TC_CCA, time_seconds-1); - tc_enable_cc_channels(&TCC0, TC_CCAEN); - tc_clear_cc_interrupt(&TCC0, TC_CCA); - tc_set_cca_interrupt_level(&TCC0, TC_INT_LVL_MED); + tc_set_cca_interrupt_callback(&EXP_TC0_0, ca_cca_callback); + tc_write_cc(&EXP_TC0_0, TC_CCA, time_seconds-1); + tc_enable_cc_channels(&EXP_TC0_0, TC_CCAEN); + tc_clear_cc_interrupt(&EXP_TC0_0, TC_CCA); + tc_set_cca_interrupt_level(&EXP_TC0_0, TC_INT_LVL_MED); } if (exp_type == POT_OCP) @@ -759,7 +758,7 @@ void pot_experiment(uint16_t time_seconds, uint8_t exp_type){ RTC.CNT=0; PORTD.INTCTRL = PORT_INT0LVL_LO_gc; - TCC0.CNT = 0; + EXP_TC0_0.CNT = 0; while (up !=0){ if (udi_cdc_is_rx_ready()){ @@ -772,9 +771,9 @@ void pot_experiment(uint16_t time_seconds, uint8_t exp_type){ } aborting: - tc_set_cca_interrupt_level(&TCC0, TC_INT_LVL_OFF); - tc_write_clock_source(&TCC0, TC_CLKSEL_OFF_gc); - tc_disable(&TCC0); + tc_set_cca_interrupt_level(&EXP_TC0_0, TC_INT_LVL_OFF); + tc_write_clock_source(&EXP_TC0_0, TC_CLKSEL_OFF_gc); + tc_disable(&EXP_TC0_0); volt_exp_stop(); ads1255_standby(); @@ -800,19 +799,19 @@ void ca_experiment(uint16_t steps, uint16_t step_dac[], uint16_t step_seconds[]) portd_int0_callback = portd_int0_ca; //ADC interrupt - tc_enable(&TCC0); - tc_set_cca_interrupt_callback(&TCC0, ca_cca_callback); + tc_enable(&EXP_TC0_0); + tc_set_cca_interrupt_callback(&EXP_TC0_0, ca_cca_callback); ads1255_rdatac(); ads1255_wakeup(); - tc_write_period(&TCC0,0xffff); - tc_write_clock_source(&TCC0, TC_CLKSEL_EVCH0_gc); - tc_set_direction(&TCC0, TC_UP); - tc_enable_cc_channels(&TCC0, TC_CCAEN); - tc_set_cca_interrupt_level(&TCC0, TC_INT_LVL_MED); + tc_write_period(&EXP_TC0_0,0xffff); + tc_write_clock_source(&EXP_TC0_0, TC_CLKSEL_EVCH0_gc); + tc_set_direction(&EXP_TC0_0, TC_UP); + tc_enable_cc_channels(&EXP_TC0_0, TC_CCAEN); + tc_set_cca_interrupt_level(&EXP_TC0_0, TC_INT_LVL_MED); - TCC0.CNT = 0; + EXP_TC0_0.CNT = 0; max5443_set_voltage1(step_dac[0]); volt_exp_start(); @@ -820,7 +819,7 @@ void ca_experiment(uint16_t steps, uint16_t step_dac[], uint16_t step_seconds[]) for (uint8_t i = 0; i < steps; ++i) { up = 1; - tc_write_cc(&TCC0, TC_CCA, TCC0.CNT+step_seconds[i]-1); + tc_write_cc(&EXP_TC0_0, TC_CCA, EXP_TC0_0.CNT+step_seconds[i]-1); RTC.CNT=0; max5443_set_voltage1(step_dac[i]); printf("#DAC: %u\n\r", step_dac[i]); @@ -837,9 +836,9 @@ void ca_experiment(uint16_t steps, uint16_t step_dac[], uint16_t step_seconds[]) } aborting: - tc_set_cca_interrupt_level(&TCC0, TC_INT_LVL_OFF); - tc_write_clock_source(&TCC0, TC_CLKSEL_OFF_gc); - tc_disable(&TCC0); + tc_set_cca_interrupt_level(&EXP_TC0_0, TC_INT_LVL_OFF); + tc_write_clock_source(&EXP_TC0_0, TC_CLKSEL_OFF_gc); + tc_disable(&EXP_TC0_0); volt_exp_stop(); ads1255_standby(); @@ -854,7 +853,7 @@ static void portd_int0_ca(void){ int32_t current; } data; - data.time1 = TCC0.CNT; + data.time1 = EXP_TC0_0.CNT; data.time2 = RTC.CNT; data.current = ads1255_read_fast24(); @@ -897,16 +896,16 @@ void swv_experiment(int16_t start, int16_t stop, uint16_t step, uint16_t pulse_h else direction = 0; - tc_enable(&TCF0); - tc_enable(&TCC0); + tc_enable(&EXP_TC0_1); + tc_enable(&EXP_TC0_0); frequency *= 2; //compensate for half-period triggers //calculate time to ADC trigger period = ceil((1/(double)frequency)*F_CPU); uint32_t adc_period = ceil(((1/(double)frequency)-(double)(sample_delay_ms_100div/1e5))*F_CPU); - set_timer_period(period, &TCF0); - set_timer_period(adc_period, &TCC0); + set_timer_period(period, &EXP_TC0_1); + set_timer_period(adc_period, &EXP_TC0_0); ads1255_wakeup(); ads1255_standby(); @@ -914,15 +913,15 @@ void swv_experiment(int16_t start, int16_t stop, uint16_t step, uint16_t pulse_h volt_exp_start(); do{ - TCF0.CNT = 0; - TCC0.CNT = 0; + EXP_TC0_1.CNT = 0; + EXP_TC0_0.CNT = 0; if (_swv_singledir(dacindex_start, dacindex_stop, dacindex_pulse_height, dacindex_step, direction)) goto aborting; //function will return non-zero if abort called over USB if (scans > 0){ //non-cyclic mode skips out after one direction - TCF0.CNT = 0; - TCC0.CNT = 0; + EXP_TC0_1.CNT = 0; + EXP_TC0_0.CNT = 0; if (_swv_singledir(dacindex_stop, dacindex_start, dacindex_pulse_height, dacindex_step, !direction)) //swap start and stop and invert direction for second half of scan goto aborting; } @@ -935,12 +934,12 @@ void swv_experiment(int16_t start, int16_t stop, uint16_t step, uint16_t pulse_h aborting: volt_exp_stop(); - tc_write_clock_source(&TCF0, TC_CLKSEL_OFF_gc); - tc_disable(&TCF0); - TCF0.CNT = 0; - tc_write_clock_source(&TCC0, TC_CLKSEL_OFF_gc); - tc_disable(&TCC0); - TCC0.CNT = 0; + tc_write_clock_source(&EXP_TC0_1, TC_CLKSEL_OFF_gc); + tc_disable(&EXP_TC0_1); + EXP_TC0_1.CNT = 0; + tc_write_clock_source(&EXP_TC0_0, TC_CLKSEL_OFF_gc); + tc_disable(&EXP_TC0_0); + EXP_TC0_0.CNT = 0; ads1255_standby(); return; @@ -966,10 +965,10 @@ uint8_t _swv_singledir (uint16_t dacindex, uint16_t dacindex_stop, uint16_t daci max5443_set_voltage1(dacindex-dacindex_pulse_height); while ((dacindex <= dacindex_stop && direction == 1) || (dacindex >= dacindex_stop && direction == 0)){ - tc_clear_overflow(&TCF0); - tc_clear_overflow(&TCC0); + tc_clear_overflow(&EXP_TC0_1); + tc_clear_overflow(&EXP_TC0_0); - while (!tc_is_overflow(&TCC0)){ //ADC tc overflow + while (!tc_is_overflow(&EXP_TC0_0)){ //ADC tc overflow if (udi_cdc_is_rx_ready()){ //check for abort signal over USB if (getchar() == 'a') return 1; @@ -981,18 +980,18 @@ uint8_t _swv_singledir (uint16_t dacindex, uint16_t dacindex_stop, uint16_t daci forward = ads1255_read_single24(); ads1255_standby(); - while (!tc_is_overflow(&TCF0)); //wait for end of half-cycle - TCC0.CNT = 0; + while (!tc_is_overflow(&EXP_TC0_1)); //wait for end of half-cycle + EXP_TC0_0.CNT = 0; if (direction == 1) //switch voltage to other half of cycle max5443_set_voltage1(dacindex-dacindex_pulse_height); else max5443_set_voltage1(dacindex+dacindex_pulse_height); - tc_clear_overflow(&TCF0); //reset timer OVF - tc_clear_overflow(&TCC0); + tc_clear_overflow(&EXP_TC0_1); //reset timer OVF + tc_clear_overflow(&EXP_TC0_0); - while (!tc_is_overflow(&TCC0)){ //ADC tc overflow + while (!tc_is_overflow(&EXP_TC0_0)){ //ADC tc overflow if (udi_cdc_is_rx_ready()){ //check for abort signal over USB if (getchar() == 'a') return 1; @@ -1004,8 +1003,8 @@ uint8_t _swv_singledir (uint16_t dacindex, uint16_t dacindex_stop, uint16_t daci reverse = ads1255_read_single24(); ads1255_standby(); - while (!tc_is_overflow(&TCF0)); //wait for end of half-cycle - TCC0.CNT = 0; + while (!tc_is_overflow(&EXP_TC0_1)); //wait for end of half-cycle + EXP_TC0_0.CNT = 0; lastindex = dacindex; @@ -1065,29 +1064,29 @@ void dpv_experiment(int16_t start, int16_t stop, uint16_t step, uint16_t pulse_h else direction = 0; - tc_enable(&TCF0); - tc_enable(&TCC0); + tc_enable(&EXP_TC0_1); + tc_enable(&EXP_TC0_0); //calculate time to ADC trigger cpu_period = ceil((double)pulse_period*1e-3*F_CPU); uint32_t adc_period = ceil((((double)pulse_period*1e-3)-(double)(sample_delay_ms_100div/1e5))*F_CPU); - uint16_t divider = set_timer_period(cpu_period, &TCF0); - uint16_t adc_divider = set_timer_period(adc_period, &TCC0); + uint16_t divider = set_timer_period(cpu_period, &EXP_TC0_1); + uint16_t adc_divider = set_timer_period(adc_period, &EXP_TC0_0); cpu_width = (double)pulse_width*1e-3*F_CPU; uint32_t adc_width = ceil((((double)pulse_width*1e-3)-(double)(sample_delay_ms_100div/1e5))*F_CPU); - tc_write_cc(&TCF0, TC_CCA, (uint16_t)(cpu_width/divider)); - tc_enable_cc_channels(&TCF0, TC_CCAEN); - tc_write_cc(&TCC0, TC_CCA, (uint16_t)(adc_width/adc_divider)); - tc_enable_cc_channels(&TCC0, TC_CCAEN); + tc_write_cc(&EXP_TC0_1, TC_CCA, (uint16_t)(cpu_width/divider)); + tc_enable_cc_channels(&EXP_TC0_1, TC_CCAEN); + tc_write_cc(&EXP_TC0_0, TC_CCA, (uint16_t)(adc_width/adc_divider)); + tc_enable_cc_channels(&EXP_TC0_0, TC_CCAEN); ads1255_wakeup(); ads1255_standby(); volt_exp_start(); - TCF0.CNT = 0; - TCC0.CNT = 0; + EXP_TC0_1.CNT = 0; + EXP_TC0_0.CNT = 0; if (_dpv_singledir(dacindex_start, dacindex_stop, dacindex_pulse_height, dacindex_step, direction)) goto aborting; //function will return non-zero if abort called over USB @@ -1096,12 +1095,12 @@ void dpv_experiment(int16_t start, int16_t stop, uint16_t step, uint16_t pulse_h aborting: volt_exp_stop(); - tc_write_clock_source(&TCF0, TC_CLKSEL_OFF_gc); - tc_disable(&TCF0); - tc_write_clock_source(&TCC0, TC_CLKSEL_OFF_gc); - tc_disable(&TCC0); - TCF0.CNT = 0; - TCC0.CNT = 0; + tc_write_clock_source(&EXP_TC0_1, TC_CLKSEL_OFF_gc); + tc_disable(&EXP_TC0_1); + tc_write_clock_source(&EXP_TC0_0, TC_CLKSEL_OFF_gc); + tc_disable(&EXP_TC0_0); + EXP_TC0_1.CNT = 0; + EXP_TC0_0.CNT = 0; ads1255_standby(); return; @@ -1127,12 +1126,12 @@ uint8_t _dpv_singledir (uint16_t dacindex, uint16_t dacindex_stop, uint16_t daci max5443_set_voltage1(dacindex-dacindex_pulse_height); while ((dacindex <= dacindex_stop && direction == 1) || (dacindex >= dacindex_stop && direction == 0)){ - tc_clear_overflow(&TCF0); - tc_clear_cc_interrupt(&TCF0, TC_CCA); - tc_clear_overflow(&TCC0); - tc_clear_cc_interrupt(&TCC0, TC_CCA); + tc_clear_overflow(&EXP_TC0_1); + tc_clear_cc_interrupt(&EXP_TC0_1, TC_CCA); + tc_clear_overflow(&EXP_TC0_0); + tc_clear_cc_interrupt(&EXP_TC0_0, TC_CCA); - while (!tc_is_cc_interrupt(&TCC0, TC_CCA)){ //wait until ADC TC CCA match + while (!tc_is_cc_interrupt(&EXP_TC0_0, TC_CCA)){ //wait until ADC TC CCA match if (udi_cdc_is_rx_ready()){ //check for abort signal over USB if (getchar() == 'a') return 1; @@ -1144,12 +1143,12 @@ uint8_t _dpv_singledir (uint16_t dacindex, uint16_t dacindex_stop, uint16_t daci forward = ads1255_read_single24(); ads1255_standby(); - while (!tc_is_cc_interrupt(&TCF0, TC_CCA)); //wait for end of half-cycle + while (!tc_is_cc_interrupt(&EXP_TC0_1, TC_CCA)); //wait for end of half-cycle //switch voltage to baseline max5443_set_voltage1(dacindex); - while (!tc_is_overflow(&TCC0)){ //wait for ADC TC overflow + while (!tc_is_overflow(&EXP_TC0_0)){ //wait for ADC TC overflow if (udi_cdc_is_rx_ready()){ if (getchar() == 'a') return 1; @@ -1161,8 +1160,8 @@ uint8_t _dpv_singledir (uint16_t dacindex, uint16_t dacindex_stop, uint16_t daci reverse = ads1255_read_single24(); ads1255_standby(); - while (!tc_is_overflow(&TCF0)); //wait for end of half-cycle - TCC0.CNT = 0; // Resync ADC TC + while (!tc_is_overflow(&EXP_TC0_1)); //wait for end of half-cycle + EXP_TC0_0.CNT = 0; // Resync ADC TC lastindex = dacindex; diff --git a/DSTAT/src/experiment.h b/DSTAT/src/experiment.h index 5c47469..9b42ecc 100644 --- a/DSTAT/src/experiment.h +++ b/DSTAT/src/experiment.h @@ -72,6 +72,10 @@ #define RTC_COMPARE_INT_LEVEL RTC_COMPINTLVL_HI_gc +#define EXP_TC0_0 TCC0 +#define EXP_TC1_0 TCC1 +#define EXP_TC0_1 TCF0 + extern uint16_t g_gain; extern uint8_t g_short; extern uint8_t autogain_enable; -- GitLab