diff --git a/DSTAT-temp/src/config/conf_clock.h b/DSTAT-temp/src/config/conf_clock.h index 7a734eb7dc86b8b3c996c2f527cac91607b8aae4..824a374ff2a314d0d49140a6c1d86b3f49a3e2f4 100644 --- a/DSTAT-temp/src/config/conf_clock.h +++ b/DSTAT-temp/src/config/conf_clock.h @@ -93,6 +93,6 @@ #define CONFIG_OSC_AUTOCAL_RC32MHZ_REF_OSC OSC_ID_USBSOF /* Use to enable and select RTC clock source */ -#define CONFIG_RTC_SOURCE SYSCLK_RTCSRC_ULP +#define CONFIG_RTC_SOURCE SYSCLK_RTCSRC_TOSC #endif /* CONF_CLOCK_H_INCLUDED */ diff --git a/DSTAT-temp/src/experiment.c b/DSTAT-temp/src/experiment.c index dfc8d313b83dee151e976d17e96067ea9cc55e31..53b1927db7908b7f855878bf07a3b8fdf74ce977 100644 --- a/DSTAT-temp/src/experiment.c +++ b/DSTAT-temp/src/experiment.c @@ -34,6 +34,7 @@ volatile uint16_t tcf0period = 0; uint32_t skip_samples = 0; //Private function declarations +static void precond_rtc_callback(uint32_t time); static void porte_int0_lsv(void); static void tcf0_ovf_callback(void); static void tce1_ovf_callback_lsv(void); @@ -226,6 +227,69 @@ void pot_exp_stop(void){ arch_ioport_set_port_level(IOPORT_PORTB, PIN3_bm|PIN4_bm|PIN5_bm, 0); } +void precond(int16_t v1, uint16_t t1, int16_t v2, uint16_t t2){ //assumes potentiostat switches are already set + /** + * Performs experiment preconditioning. + * + * @param v1 First potential (DAC index). + * @param t1 First duration (s). + * @param v2 Second potential (DAC index). + * @param t2 Second duration (s). + */ + + while (RTC.STATUS & RTC_SYNCBUSY_bm); + RTC.PER = 65535; + while (RTC.STATUS & RTC_SYNCBUSY_bm); + RTC.CTRL = RTC_PRESCALER_DIV1024_gc; //1s tick + rtc_set_callback((rtc_callback_t)precond_rtc_callback); + + up = 1; + + //first potential + if (v1 > 0){ + max5443_set_voltage1(v1); + rtc_set_alarm(t1); + RTC.CNT = 0; + pot_exp_start(); + while (up){ + if (udi_cdc_is_rx_ready()){ + if (getchar() == 'a'){ + precond_rtc_callback(t1); + printf("##ABORT\n\r"); + goto aborting; + } + } + } + } + + up = 1; + + if (v2 > 0){ + max5443_set_voltage1(v2); + rtc_set_alarm(t2); + RTC.CNT = 0; + pot_exp_start(); + while (up){ + if (udi_cdc_is_rx_ready()){ + if (getchar() == 'a'){ + precond_rtc_callback(t2); + printf("##ABORT\n\r"); + goto aborting; + } + } + } + } + + aborting: + pot_exp_stop(); + return; +} + +static void precond_rtc_callback(uint32_t time){ + up = 0; + RTC.INTCTRL |= RTC_COMPINTLVL_OFF_gc; +} + void cv_experiment(int16_t v1, int16_t v2, int16_t start, uint8_t scans, uint16_t slope){ /** * Perform a CV experiment. @@ -380,12 +444,12 @@ uint8_t lsv_experiment(int16_t start, int16_t stop, uint16_t slope, int8_t first dma_channel_set_trigger_source(&dmach_conf, DMA_CH_TRIGSRC_OFF_gc); dma_channel_set_single_shot(&dmach_conf); - dma_set_callback(0, lsv_dma_callback); + dma_set_callback(0, (dma_callback_t)lsv_dma_callback); dma_channel_set_interrupt_level(&dmach_conf, DMA_INT_LVL_HI); dma_channel_write_config(0, &dmach_conf); - dma_set_callback(1, lsv_dma_callback1); + dma_set_callback(1, (dma_callback_t)lsv_dma_callback1); dma_channel_set_source_address(&dmach_conf, (uint16_t)(uintptr_t)&TCC1.CNTL); dma_channel_write_config(1, &dmach_conf); } @@ -525,6 +589,7 @@ void ca_experiment(uint16_t steps, uint16_t step_dac[], uint16_t step_seconds[]) tc_set_cca_interrupt_level(&TCC0, TC_INT_LVL_MED); TCC0.CNT = 0; + max5443_set_voltage1(step_dac[0]); pot_exp_start(); for (uint8_t i = 0; i < steps; ++i) @@ -750,4 +815,4 @@ ISR(PORTD_INT0_vect){ if (portd_int0_callback) { portd_int0_callback(); } -} \ No newline at end of file +} \ No newline at end of file diff --git a/DSTAT-temp/src/experiment.h b/DSTAT-temp/src/experiment.h index 93a116e1133de0d884f46fa33cbdd395a99638a4..549a46684cb286b2c5060e6fe58e80429b9cb09b 100644 --- a/DSTAT-temp/src/experiment.h +++ b/DSTAT-temp/src/experiment.h @@ -54,6 +54,8 @@ #define SIN_IMP_CYCLEPTS 50 +#define RTC_COMPARE_INT_LEVEL RTC_COMPINTLVL_HI_gc + extern uint16_t g_gain; extern uint8_t autogain_enable; @@ -64,6 +66,7 @@ int8_t autogainswitch(void); void pot_set_gain(void); void pot_exp_start(void); void pot_exp_stop(void); +void precond(int16_t v1, uint16_t t1, int16_t v2, uint16_t t2); void cv_experiment(int16_t v1, int16_t v2, int16_t start, uint8_t scans, uint16_t slope); uint8_t lsv_experiment(int16_t start, int16_t stop, uint16_t slope, int8_t first_run); void ca_experiment(uint16_t steps, uint16_t step_dac[], uint16_t step_seconds[]);