From 3d6e1f18a60101c2e0813dc5e006e4c72c807f1f Mon Sep 17 00:00:00 2001 From: Astro Date: Mon, 18 May 2020 21:38:13 +0200 Subject: [PATCH] add tec_u_meas --- src/channel.rs | 2 ++ src/channels.rs | 27 ++++++++++++++++++++++++++- src/main.rs | 7 +++++-- src/pins.rs | 17 +++++++++++++++-- 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/channel.rs b/src/channel.rs index ab17a2c..86480f1 100644 --- a/src/channel.rs +++ b/src/channel.rs @@ -22,6 +22,7 @@ pub struct Channel { pub itec_pin: C::ItecPin, /// feedback from `dac` output pub dac_feedback_pin: C::DacFeedbackPin, + pub tec_u_meas_pin: C::TecUMeasPin, } impl Channel { @@ -37,6 +38,7 @@ impl Channel { adc: pins.adc, itec_pin: pins.itec_pin, dac_feedback_pin: pins.dac_feedback_pin, + tec_u_meas_pin: pins.tec_u_meas_pin, } } } diff --git a/src/channels.rs b/src/channels.rs index a6d5e84..dd84442 100644 --- a/src/channels.rs +++ b/src/channels.rs @@ -10,10 +10,12 @@ use crate::{ pub const CHANNELS: usize = 2; +// TODO: -pub pub struct Channels { pub channel0: Channel, pub channel1: Channel, pub adc: ad7172::Adc, + pub tec_u_meas_adc: pins::TecUMeasAdc, pub pwm: pins::PwmPins, } @@ -21,6 +23,7 @@ impl Channels { pub fn new(pins: pins::Pins) -> Self { let channel0 = Channel::new(pins.channel0); let channel1 = Channel::new(pins.channel1); + let tec_u_meas_adc = pins.tec_u_meas_adc; let pwm = pins.pwm; let mut adc = ad7172::Adc::new(pins.adc_spi, pins.adc_nss).unwrap(); @@ -43,7 +46,7 @@ impl Channels { adc.setup_channel(1, ad7172::Input::Ain2, ad7172::Input::Ain3).unwrap(); adc.start_continuous_conversion().unwrap(); - Channels { channel0, channel1, adc, pwm } + Channels { channel0, channel1, adc, tec_u_meas_adc, pwm } } pub fn channel_state>(&mut self, channel: I) -> &mut ChannelState { @@ -138,4 +141,26 @@ impl Channels { _ => unreachable!(), } } + + pub fn read_tec_u_meas(&mut self, channel: usize) -> Volts { + match channel { + 0 => { + let sample = self.tec_u_meas_adc.convert( + &self.channel0.tec_u_meas_pin, + stm32f4xx_hal::adc::config::SampleTime::Cycles_480 + ); + let mv = self.tec_u_meas_adc.sample_to_millivolts(sample); + Volts(mv as f64 / 1000.0) + } + 1 => { + let sample = self.tec_u_meas_adc.convert( + &self.channel1.tec_u_meas_pin, + stm32f4xx_hal::adc::config::SampleTime::Cycles_480 + ); + let mv = self.tec_u_meas_adc.sample_to_millivolts(sample); + Volts(mv as f64 / 1000.0) + } + _ => unreachable!(), + } + } } diff --git a/src/main.rs b/src/main.rs index 7f26000..a51602e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -93,7 +93,7 @@ fn main() -> ! { clocks, dp.TIM1, dp.TIM3, dp.GPIOA, dp.GPIOB, dp.GPIOC, dp.GPIOE, dp.GPIOF, dp.GPIOG, dp.SPI2, dp.SPI4, dp.SPI5, - dp.ADC1, dp.ADC2, + dp.ADC1, dp.ADC2, dp.ADC3, ); let mut channels = Channels::new(pins); timer::setup(cp.SYST, clocks); @@ -149,12 +149,15 @@ fn main() -> ! { let itec = channels.read_itec(channel); let tec_i = (itec - Volts(1.5)) / Ohms(0.4); + let tec_u_meas = channels.read_tec_u_meas(channel); + let state = channels.channel_state(channel); let _ = writeln!( - socket, "t={} raw{}=0x{:06X} dac_feedback={}/{} itec={} tec={}", + socket, "t={} raw{}=0x{:06X} dac_feedback={}/{} itec={} tec={} tec_u_meas={}", state.adc_time, channel, adc_data, dac_feedback, dac_i, itec, tec_i, + tec_u_meas, ); } } diff --git a/src/pins.rs b/src/pins.rs index cdf5a07..c46e70f 100644 --- a/src/pins.rs +++ b/src/pins.rs @@ -16,7 +16,7 @@ use stm32f4xx_hal::{ rcc::Clocks, pwm::{self, PwmChannels}, spi::{Spi, NoMiso}, - stm32::{ADC1, ADC2, GPIOA, GPIOB, GPIOC, GPIOE, GPIOF, GPIOG, SPI2, SPI4, SPI5, TIM1, TIM3}, + stm32::{ADC1, ADC2, ADC3, GPIOA, GPIOB, GPIOC, GPIOE, GPIOF, GPIOG, SPI2, SPI4, SPI5, TIM1, TIM3}, time::U32Ext, }; use crate::channel::{Channel0, Channel1}; @@ -29,6 +29,7 @@ pub trait ChannelPins { type Adc; type ItecPin; type DacFeedbackPin; + type TecUMeasPin; } impl ChannelPins for Channel0 { @@ -38,6 +39,7 @@ impl ChannelPins for Channel0 { type Adc = Adc; type ItecPin = PA6; type DacFeedbackPin = PA4; + type TecUMeasPin = PC2; } impl ChannelPins for Channel1 { @@ -47,6 +49,7 @@ impl ChannelPins for Channel1 { type Adc = Adc; type ItecPin = PB0; type DacFeedbackPin = PA5; + type TecUMeasPin = PC3; } /// SPI peripheral used for communication with the ADC @@ -55,6 +58,7 @@ pub type AdcNss = PB12>; type Dac0Spi = Spi>, NoMiso, PE6>)>; type Dac1Spi = Spi>, NoMiso, PF9>)>; +pub type TecUMeasAdc = Adc; pub struct ChannelPinSet { pub dac_spi: C::DacSpi, @@ -63,11 +67,13 @@ pub struct ChannelPinSet { pub adc: C::Adc, pub itec_pin: C::ItecPin, pub dac_feedback_pin: C::DacFeedbackPin, + pub tec_u_meas_pin: C::TecUMeasPin, } pub struct Pins { pub adc_spi: AdcSpi, pub adc_nss: AdcNss, + pub tec_u_meas_adc: TecUMeasAdc, pub pwm: PwmPins, pub channel0: ChannelPinSet, pub channel1: ChannelPinSet, @@ -80,7 +86,7 @@ impl Pins { tim1: TIM1, tim3: TIM3, gpioa: GPIOA, gpiob: GPIOB, gpioc: GPIOC, gpioe: GPIOE, gpiof: GPIOF, gpiog: GPIOG, spi2: SPI2, spi4: SPI4, spi5: SPI5, - adc1: ADC1, adc2: ADC2, + adc1: ADC1, adc2: ADC2, adc3: ADC3, ) -> Self { let gpioa = gpioa.split(); let gpiob = gpiob.split(); @@ -97,6 +103,8 @@ 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 tec_u_meas_adc = Adc::adc3(adc3, true, Default::default()); + let pwm = PwmPins::setup( clocks, tim1, tim3, gpioc.pc6, gpioc.pc7, @@ -114,6 +122,7 @@ impl Pins { adc0.enable(); let itec0_pin = gpioa.pa6.into_analog(); let dac_feedback0_pin = gpioa.pa4.into_analog(); + let tec_u_meas0_pin = gpioc.pc2.into_analog(); let channel0 = ChannelPinSet { dac_spi: dac0_spi, dac_sync: dac0_sync, @@ -121,6 +130,7 @@ impl Pins { adc: adc0, itec_pin: itec0_pin, dac_feedback_pin: dac_feedback0_pin, + tec_u_meas_pin: tec_u_meas0_pin, }; let (dac1_spi, dac1_sync) = Self::setup_dac1( @@ -133,6 +143,7 @@ impl Pins { adc1.enable(); let itec1_pin = gpiob.pb0.into_analog(); let dac_feedback1_pin = gpioa.pa5.into_analog(); + let tec_u_meas1_pin = gpioc.pc3.into_analog(); let channel1 = ChannelPinSet { dac_spi: dac1_spi, dac_sync: dac1_sync, @@ -140,10 +151,12 @@ impl Pins { adc: adc1, itec_pin: itec1_pin, dac_feedback_pin: dac_feedback1_pin, + tec_u_meas_pin: tec_u_meas1_pin, }; Pins { adc_spi, adc_nss, + tec_u_meas_adc, pwm, channel0, channel1,