From 5418488a2feadf6da67e0ffb74e10f1567fc89f5 Mon Sep 17 00:00:00 2001 From: Astro Date: Thu, 28 May 2020 02:01:55 +0200 Subject: [PATCH] replace channel[01].adc with pins_adc --- src/channel.rs | 3 --- src/channels.rs | 31 ++++++++++++++++++------------- src/pins.rs | 15 ++++----------- 3 files changed, 22 insertions(+), 27 deletions(-) diff --git a/src/channel.rs b/src/channel.rs index 7bc79ef..f38564e 100644 --- a/src/channel.rs +++ b/src/channel.rs @@ -19,8 +19,6 @@ pub struct Channel { /// 1 / Volts pub dac_factor: f64, pub shdn: C::Shdn, - /// stm32f4 integrated adc - pub adc: C::Adc, pub vref_pin: C::VRefPin, pub itec_pin: C::ItecPin, /// feedback from `dac` output @@ -42,7 +40,6 @@ impl Channel { state, dac, dac_factor, shdn: pins.shdn, - adc: pins.adc, vref_pin: pins.vref_pin, itec_pin: pins.itec_pin, dac_feedback_pin: pins.dac_feedback_pin, diff --git a/src/channels.rs b/src/channels.rs index bd12f9e..8526732 100644 --- a/src/channels.rs +++ b/src/channels.rs @@ -16,6 +16,8 @@ pub struct Channels { channel0: Channel, channel1: Channel, pub adc: ad7172::Adc, + /// stm32f4 integrated adc + pins_adc: pins::PinsAdc, tec_u_meas_adc: pins::TecUMeasAdc, pub pwm: pins::PwmPins, } @@ -24,6 +26,7 @@ impl Channels { pub fn new(pins: pins::Pins) -> Self { let channel0 = Channel::new(pins.channel0); let channel1 = Channel::new(pins.channel1); + let pins_adc = pins.pins_adc; let tec_u_meas_adc = pins.tec_u_meas_adc; let pwm = pins.pwm; @@ -47,7 +50,7 @@ impl Channels { adc.setup_channel(1, ad7172::Input::Ain2, ad7172::Input::Ain3).unwrap(); adc.start_continuous_conversion().unwrap(); - Channels { channel0, channel1, adc, tec_u_meas_adc, pwm } + Channels { channel0, channel1, adc, pins_adc, tec_u_meas_adc, pwm } } pub fn channel_state>(&mut self, channel: I) -> &mut ChannelState { @@ -106,19 +109,21 @@ impl Channels { pub fn read_dac_feedback(&mut self, channel: usize) -> Volts { match channel { 0 => { - let sample = self.channel0.adc.convert( + let sample = self.pins_adc.convert( &self.channel0.dac_feedback_pin, stm32f4xx_hal::adc::config::SampleTime::Cycles_480 ); - let mv = self.channel0.adc.sample_to_millivolts(sample); + let mv = self.pins_adc.sample_to_millivolts(sample); + info!("dac0_fb: {}/{:03X}", mv, sample); Volts(mv as f64 / 1000.0) } 1 => { - let sample = self.channel1.adc.convert( + let sample = self.pins_adc.convert( &self.channel1.dac_feedback_pin, stm32f4xx_hal::adc::config::SampleTime::Cycles_480 ); - let mv = self.channel1.adc.sample_to_millivolts(sample); + let mv = self.pins_adc.sample_to_millivolts(sample); + info!("dac1_fb: {}/{:03X}", mv, sample); Volts(mv as f64 / 1000.0) } _ => unreachable!(), @@ -140,19 +145,19 @@ impl Channels { pub fn read_itec(&mut self, channel: usize) -> Volts { match channel { 0 => { - let sample = self.channel0.adc.convert( + let sample = self.pins_adc.convert( &self.channel0.itec_pin, stm32f4xx_hal::adc::config::SampleTime::Cycles_480 ); - let mv = self.channel0.adc.sample_to_millivolts(sample); + let mv = self.pins_adc.sample_to_millivolts(sample); Volts(mv as f64 / 1000.0) } 1 => { - let sample = self.channel1.adc.convert( + let sample = self.pins_adc.convert( &self.channel1.itec_pin, stm32f4xx_hal::adc::config::SampleTime::Cycles_480 ); - let mv = self.channel1.adc.sample_to_millivolts(sample); + let mv = self.pins_adc.sample_to_millivolts(sample); Volts(mv as f64 / 1000.0) } _ => unreachable!(), @@ -163,19 +168,19 @@ impl Channels { pub fn read_vref(&mut self, channel: usize) -> Volts { match channel { 0 => { - let sample = self.channel0.adc.convert( + let sample = self.pins_adc.convert( &self.channel0.vref_pin, stm32f4xx_hal::adc::config::SampleTime::Cycles_480 ); - let mv = self.channel0.adc.sample_to_millivolts(sample); + let mv = self.pins_adc.sample_to_millivolts(sample); Volts(mv as f64 / 1000.0) } 1 => { - let sample = self.channel1.adc.convert( + let sample = self.pins_adc.convert( &self.channel1.vref_pin, stm32f4xx_hal::adc::config::SampleTime::Cycles_480 ); - let mv = self.channel1.adc.sample_to_millivolts(sample); + let mv = self.pins_adc.sample_to_millivolts(sample); Volts(mv as f64 / 1000.0) } _ => unreachable!(), diff --git a/src/pins.rs b/src/pins.rs index ff4545f..1ec0304 100644 --- a/src/pins.rs +++ b/src/pins.rs @@ -26,7 +26,6 @@ pub trait ChannelPins { type DacSpi: Transfer; type DacSync: OutputPin; type Shdn: OutputPin; - type Adc; type VRefPin; type ItecPin; type DacFeedbackPin; @@ -37,7 +36,6 @@ impl ChannelPins for Channel0 { type DacSpi = Dac0Spi; type DacSync = PE4>; type Shdn = PE10>; - type Adc = Adc; type VRefPin = PA0; type ItecPin = PA6; type DacFeedbackPin = PA4; @@ -48,7 +46,6 @@ impl ChannelPins for Channel1 { type DacSpi = Dac1Spi; type DacSync = PF6>; type Shdn = PE15>; - type Adc = Adc; type VRefPin = PA3; type ItecPin = PB0; type DacFeedbackPin = PA5; @@ -60,6 +57,7 @@ pub type AdcSpi = Spi>, PB14>, PB15>; type Dac0Spi = Spi>, NoMiso, PE6>)>; type Dac1Spi = Spi>, NoMiso, PF9>)>; +pub type PinsAdc = Adc; pub type TecUMeasAdc = Adc; @@ -67,7 +65,6 @@ pub struct ChannelPinSet { pub dac_spi: C::DacSpi, pub dac_sync: C::DacSync, pub shdn: C::Shdn, - pub adc: C::Adc, pub vref_pin: C::VRefPin, pub itec_pin: C::ItecPin, pub dac_feedback_pin: C::DacFeedbackPin, @@ -78,6 +75,7 @@ pub struct Pins { pub adc_spi: AdcSpi, pub adc_nss: AdcNss, pub tec_u_meas_adc: TecUMeasAdc, + pub pins_adc: PinsAdc, pub pwm: PwmPins, pub channel0: ChannelPinSet, pub channel1: ChannelPinSet, @@ -107,6 +105,7 @@ impl Pins { let adc_spi = Self::setup_spi_adc(clocks, spi2, gpiob.pb10, gpiob.pb14, gpiob.pb15); let adc_nss = gpiob.pb12.into_push_pull_output(); + let pins_adc = Adc::adc1(adc1, true, Default::default()); let tec_u_meas_adc = Adc::adc3(adc3, true, Default::default()); let pwm = PwmPins::setup( @@ -122,8 +121,6 @@ impl Pins { ); let mut shdn0 = gpioe.pe10.into_push_pull_output(); let _ = shdn0.set_low(); - let mut adc0 = Adc::adc1(adc1, true, Default::default()); - adc0.enable(); let vref0_pin = gpioa.pa0.into_analog(); let itec0_pin = gpioa.pa6.into_analog(); let dac_feedback0_pin = gpioa.pa4.into_analog(); @@ -132,7 +129,6 @@ impl Pins { dac_spi: dac0_spi, dac_sync: dac0_sync, shdn: shdn0, - adc: adc0, vref_pin: vref0_pin, itec_pin: itec0_pin, dac_feedback_pin: dac_feedback0_pin, @@ -145,8 +141,6 @@ impl Pins { ); let mut shdn1 = gpioe.pe15.into_push_pull_output(); let _ = shdn1.set_low(); - let mut adc1 = Adc::adc2(adc2, true, Default::default()); - adc1.enable(); let vref1_pin = gpioa.pa3.into_analog(); let itec1_pin = gpiob.pb0.into_analog(); let dac_feedback1_pin = gpioa.pa5.into_analog(); @@ -155,7 +149,6 @@ impl Pins { dac_spi: dac1_spi, dac_sync: dac1_sync, shdn: shdn1, - adc: adc1, vref_pin: vref1_pin, itec_pin: itec1_pin, dac_feedback_pin: dac_feedback1_pin, @@ -164,7 +157,7 @@ impl Pins { Pins { adc_spi, adc_nss, - tec_u_meas_adc, + pins_adc, tec_u_meas_adc, pwm, channel0, channel1,