From 70127491f69f5627db14145b7c3c52a4ce82150e Mon Sep 17 00:00:00 2001 From: Astro Date: Mon, 18 May 2020 22:43:44 +0200 Subject: [PATCH] add vref_pin --- src/channel.rs | 2 ++ src/channels.rs | 22 ++++++++++++++++++++++ src/main.rs | 10 +++++----- src/pins.rs | 8 ++++++++ src/units.rs | 9 ++++++++- 5 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/channel.rs b/src/channel.rs index 86480f1..c0b72a2 100644 --- a/src/channel.rs +++ b/src/channel.rs @@ -19,6 +19,7 @@ pub struct Channel { 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 pub dac_feedback_pin: C::DacFeedbackPin, @@ -36,6 +37,7 @@ impl Channel { dac, shdn: pins.shdn, adc: pins.adc, + vref_pin: pins.vref_pin, 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 dd84442..459c5cf 100644 --- a/src/channels.rs +++ b/src/channels.rs @@ -142,6 +142,28 @@ impl Channels { } } + pub fn read_vref(&mut self, channel: usize) -> Volts { + match channel { + 0 => { + let sample = self.channel0.adc.convert( + &self.channel0.vref_pin, + stm32f4xx_hal::adc::config::SampleTime::Cycles_480 + ); + let mv = self.channel0.adc.sample_to_millivolts(sample); + Volts(mv as f64 / 1000.0) + } + 1 => { + let sample = self.channel1.adc.convert( + &self.channel1.vref_pin, + stm32f4xx_hal::adc::config::SampleTime::Cycles_480 + ); + let mv = self.channel1.adc.sample_to_millivolts(sample); + Volts(mv as f64 / 1000.0) + } + _ => unreachable!(), + } + } + pub fn read_tec_u_meas(&mut self, channel: usize) -> Volts { match channel { 0 => { diff --git a/src/main.rs b/src/main.rs index a51602e..d70b68a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,7 +43,7 @@ mod command_parser; use command_parser::{Command, ShowCommand, PwmPin}; mod timer; mod units; -use units::{Amps, Ohms, Volts}; +use units::{Ohms, Volts}; mod pid; mod steinhart_hart; mod channels; @@ -143,19 +143,19 @@ fn main() -> ! { Command::Show(ShowCommand::Input) => { for channel in 0..CHANNELS { if let Some(adc_data) = channels.channel_state(channel).adc_data { + let vref = channels.read_vref(channel); let dac_feedback = channels.read_dac_feedback(channel); - let dac_i = dac_feedback / Ohms(5.0); let itec = channels.read_itec(channel); - let tec_i = (itec - Volts(1.5)) / Ohms(0.4); + 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={} tec_u_meas={}", + socket, "t={} adc_raw{}=0x{:06X} vref={} dac_feedback={} itec={} tec={} tec_u_meas={}", state.adc_time, channel, adc_data, - dac_feedback, dac_i, + vref, dac_feedback, itec, tec_i, tec_u_meas, ); diff --git a/src/pins.rs b/src/pins.rs index c46e70f..ff4545f 100644 --- a/src/pins.rs +++ b/src/pins.rs @@ -27,6 +27,7 @@ pub trait ChannelPins { type DacSync: OutputPin; type Shdn: OutputPin; type Adc; + type VRefPin; type ItecPin; type DacFeedbackPin; type TecUMeasPin; @@ -37,6 +38,7 @@ impl ChannelPins for Channel0 { type DacSync = PE4>; type Shdn = PE10>; type Adc = Adc; + type VRefPin = PA0; type ItecPin = PA6; type DacFeedbackPin = PA4; type TecUMeasPin = PC2; @@ -47,6 +49,7 @@ impl ChannelPins for Channel1 { type DacSync = PF6>; type Shdn = PE15>; type Adc = Adc; + type VRefPin = PA3; type ItecPin = PB0; type DacFeedbackPin = PA5; type TecUMeasPin = PC3; @@ -65,6 +68,7 @@ pub struct ChannelPinSet { 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, pub tec_u_meas_pin: C::TecUMeasPin, @@ -120,6 +124,7 @@ impl Pins { 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(); let tec_u_meas0_pin = gpioc.pc2.into_analog(); @@ -128,6 +133,7 @@ impl Pins { dac_sync: dac0_sync, shdn: shdn0, adc: adc0, + vref_pin: vref0_pin, itec_pin: itec0_pin, dac_feedback_pin: dac_feedback0_pin, tec_u_meas_pin: tec_u_meas0_pin, @@ -141,6 +147,7 @@ impl Pins { 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(); let tec_u_meas1_pin = gpioc.pc3.into_analog(); @@ -149,6 +156,7 @@ impl Pins { dac_sync: dac1_sync, shdn: shdn1, adc: adc1, + vref_pin: vref1_pin, itec_pin: itec1_pin, dac_feedback_pin: dac_feedback1_pin, tec_u_meas_pin: tec_u_meas1_pin, diff --git a/src/units.rs b/src/units.rs index 1bf8ea2..a52e07e 100644 --- a/src/units.rs +++ b/src/units.rs @@ -1,6 +1,6 @@ use core::{ fmt, - ops::{Add, Div, Sub}, + ops::{Add, Div, Neg, Sub}, }; macro_rules! impl_add_sub { @@ -18,6 +18,13 @@ macro_rules! impl_add_sub { $Type(self.0 - rhs.0) } } + + impl Neg for $Type { + type Output = $Type; + fn neg(self) -> $Type { + $Type(-self.0) + } + } } }