From 09be55e12a1d88bb3561b00d2ae8fd2d9c461116 Mon Sep 17 00:00:00 2001 From: atse Date: Wed, 28 Feb 2024 16:35:20 +0800 Subject: [PATCH] Don't load REF pin of MAX1968 chip on HWRevs < 3.0 The REF pin of the MAX1968 on hardware revisions v2.x is missing a buffer, loading the pin on every CPU ADC read. Avoid reading from it and leave the pin floating on affected hardware revisions, and return the nominal 1.5V instead. --- src/channels.rs | 36 +++++++++++++++++++++++------------- src/pins.rs | 26 ++++++++++++++++++-------- 2 files changed, 41 insertions(+), 21 deletions(-) diff --git a/src/channels.rs b/src/channels.rs index cfec178..70ef7a0 100644 --- a/src/channels.rs +++ b/src/channels.rs @@ -18,7 +18,7 @@ use crate::{ channel_state::ChannelState, command_parser::{CenterPoint, PwmPin}, command_handler::JsonBuffer, - pins, + pins::{self, Channel0VRef, Channel1VRef}, steinhart_hart, hw_rev, }; @@ -203,20 +203,30 @@ impl<'a> Channels<'a> { pub fn read_vref(&mut self, channel: usize) -> ElectricPotential { match channel { 0 => { - let sample = self.pins_adc.convert( - &self.channel0.vref_pin, - stm32f4xx_hal::adc::config::SampleTime::Cycles_480 - ); - let mv = self.pins_adc.sample_to_millivolts(sample); - ElectricPotential::new::(mv as f64) + match &self.channel0.vref_pin { + Channel0VRef::Analog(vref_pin) => { + let sample = self.pins_adc.convert( + vref_pin, + stm32f4xx_hal::adc::config::SampleTime::Cycles_480 + ); + let mv = self.pins_adc.sample_to_millivolts(sample); + ElectricPotential::new::(mv as f64) + }, + Channel0VRef::Disabled(_) => ElectricPotential::new::(1.5) + } } 1 => { - let sample = self.pins_adc.convert( - &self.channel1.vref_pin, - stm32f4xx_hal::adc::config::SampleTime::Cycles_480 - ); - let mv = self.pins_adc.sample_to_millivolts(sample); - ElectricPotential::new::(mv as f64) + match &self.channel1.vref_pin { + Channel1VRef::Analog(vref_pin) => { + let sample = self.pins_adc.convert( + vref_pin, + stm32f4xx_hal::adc::config::SampleTime::Cycles_480 + ); + let mv = self.pins_adc.sample_to_millivolts(sample); + ElectricPotential::new::(mv as f64) + }, + Channel1VRef::Disabled(_) => ElectricPotential::new::(1.5) + } } _ => unreachable!(), } diff --git a/src/pins.rs b/src/pins.rs index 6294661..c95294b 100644 --- a/src/pins.rs +++ b/src/pins.rs @@ -66,21 +66,31 @@ pub trait ChannelPins { type TecUMeasPin; } +pub enum Channel0VRef { + Analog(PA0), + Disabled(PA0>), +} + impl ChannelPins for Channel0 { type DacSpi = Dac0Spi; type DacSync = PE4>; type Shdn = PE10>; - type VRefPin = PA0; + type VRefPin = Channel0VRef; type ItecPin = PA6; type DacFeedbackPin = PA4; type TecUMeasPin = PC2; } +pub enum Channel1VRef { + Analog(PA3), + Disabled(PA3>), +} + impl ChannelPins for Channel1 { type DacSpi = Dac1Spi; type DacSync = PF6>; type Shdn = PE15>; - type VRefPin = PA3; + type VRefPin = Channel1VRef; type ItecPin = PB0; type DacFeedbackPin = PA5; type TecUMeasPin = PC3; @@ -150,13 +160,17 @@ impl Pins { gpioe.pe13, gpioe.pe14 ); + let hwrev = HWRev::detect_hw_rev(&HWRevPins {hwrev0: gpiod.pd0, hwrev1: gpiod.pd1, + hwrev2: gpiod.pd2, hwrev3: gpiod.pd3}); + let hw_settings = hwrev.settings(); + let (dac0_spi, dac0_sync) = Self::setup_dac0( clocks, spi4, gpioe.pe2, gpioe.pe4, gpioe.pe6 ); let mut shdn0 = gpioe.pe10.into_push_pull_output(); let _ = shdn0.set_low(); - let vref0_pin = gpioa.pa0.into_analog(); + let vref0_pin = if hwrev.major > 2 {Channel0VRef::Analog(gpioa.pa0.into_analog())} else {Channel0VRef::Disabled(gpioa.pa0)}; let itec0_pin = gpioa.pa6.into_analog(); let dac_feedback0_pin = gpioa.pa4.into_analog(); let tec_u_meas0_pin = gpioc.pc2.into_analog(); @@ -176,7 +190,7 @@ impl Pins { ); let mut shdn1 = gpioe.pe15.into_push_pull_output(); let _ = shdn1.set_low(); - let vref1_pin = gpioa.pa3.into_analog(); + let vref1_pin = if hwrev.major > 2 {Channel1VRef::Analog(gpioa.pa3.into_analog())} else {Channel1VRef::Disabled(gpioa.pa3)}; let itec1_pin = gpiob.pb0.into_analog(); let dac_feedback1_pin = gpioa.pa5.into_analog(); let tec_u_meas1_pin = gpioc.pc3.into_analog(); @@ -198,10 +212,6 @@ impl Pins { channel1, }; - let hwrev = HWRev::detect_hw_rev(&HWRevPins {hwrev0: gpiod.pd0, hwrev1: gpiod.pd1, - hwrev2: gpiod.pd2, hwrev3: gpiod.pd3}); - let hw_settings = hwrev.settings(); - let leds = Leds::new(gpiod.pd9, gpiod.pd10.into_push_pull_output(), gpiod.pd11.into_push_pull_output()); let eeprom_scl = gpiob.pb8.into_alternate().set_open_drain();