From c768bdc93aa9ac88294262403346670b898b68f3 Mon Sep 17 00:00:00 2001 From: linuswck Date: Mon, 22 Jan 2024 12:24:19 +0800 Subject: [PATCH] Thermostat: Add fn to calibrate ADC's VDDA - ADC1 exclusive feature - Calibrated VDDA val can be passed to other ADCs to adjust gain error --- src/thermostat/max1968.rs | 16 ++++++++++++++-- src/thermostat/thermostat.rs | 5 +++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/thermostat/max1968.rs b/src/thermostat/max1968.rs index 0348458..f51acf5 100644 --- a/src/thermostat/max1968.rs +++ b/src/thermostat/max1968.rs @@ -115,11 +115,17 @@ impl MAX1968Phy { impl MAX1968 { pub fn new(phy_ch0: MAX1968Phy, adc1: ADC1) -> Self { + // Set ADC to a slowest sampling interval for more accurate calibration let config = AdcConfig::default() - .clock(config::Clock::Pclk2_div_2) + .clock(config::Clock::Pclk2_div_8) .default_sample_time(config::SampleTime::Cycles_480); + let mut pins_adc = Adc::adc1(adc1, true, config); + pins_adc.calibrate(); - let pins_adc = Adc::adc1(adc1, true, config); + let config = AdcConfig::default() + .clock(config::Clock::Pclk2_div_8) + .default_sample_time(config::SampleTime::Cycles_480); + pins_adc.apply_config(config); MAX1968 { phy: phy_ch0, @@ -127,6 +133,12 @@ impl MAX1968 { } } + // Return the calibrated VDDA Voltage + // Can be used to set reference voltage for other ADC + pub fn get_calibrated_vdda(&mut self) -> u32 { + self.pins_adc.reference_voltage() + } + pub fn power_down(&mut self) { self.phy.shdn.set_low(); } diff --git a/src/thermostat/thermostat.rs b/src/thermostat/thermostat.rs index 81fb71a..ef9c095 100644 --- a/src/thermostat/thermostat.rs +++ b/src/thermostat/thermostat.rs @@ -301,6 +301,11 @@ impl Thermostat{ max_i_neg: TecSettingsSummaryField { value: self.tec_setting.max_i_neg_set, max: TecSettings::MAX_I_NEG_CURRENT }, } } + + pub fn get_calibrated_vdda(&mut self) -> u32 { + self.max1968.get_calibrated_vdda() + } + } #[derive(Miniconf)]