From 7f8dd62a36e4fe1e79d95e18aeba5562f85d3189 Mon Sep 17 00:00:00 2001 From: Astro Date: Sat, 16 May 2020 23:59:31 +0200 Subject: [PATCH] add itec_pin --- src/channel.rs | 7 +++++-- src/channels.rs | 18 ++++++++++++++++-- src/main.rs | 11 +++++++++-- src/pins.rs | 28 ++++++++++++++++++---------- 4 files changed, 48 insertions(+), 16 deletions(-) diff --git a/src/channel.rs b/src/channel.rs index c3e885e..3b2fb31 100644 --- a/src/channel.rs +++ b/src/channel.rs @@ -14,8 +14,10 @@ pub struct Channel { /// for `i_set` pub dac: ad5680::Dac, pub shdn: C::Shdn, + /// stm32f4 integrated adc + pub adc: C::Adc, + pub itec_pin: C::ItecPin, /// feedback from `dac` output - pub dac_loopback: C::DacLoopback, pub dac_loopback_pin: C::DacLoopbackPin, } @@ -29,7 +31,8 @@ impl Channel { state, dac, shdn: pins.shdn, - dac_loopback: pins.dac_loopback, + adc: pins.adc, + itec_pin: pins.itec_pin, dac_loopback_pin: pins.dac_loopback_pin, } } diff --git a/src/channels.rs b/src/channels.rs index 0f6c70e..18a93a5 100644 --- a/src/channels.rs +++ b/src/channels.rs @@ -84,15 +84,29 @@ impl Channels { pub fn read_dac_loopback(&mut self, channel: usize) -> u16 { match channel { - 0 => self.channel0.dac_loopback.convert( + 0 => self.channel0.adc.convert( &self.channel0.dac_loopback_pin, stm32f4xx_hal::adc::config::SampleTime::Cycles_480 ), - 1 => self.channel1.dac_loopback.convert( + 1 => self.channel1.adc.convert( &self.channel1.dac_loopback_pin, stm32f4xx_hal::adc::config::SampleTime::Cycles_480 ), _ => unreachable!(), } } + + pub fn read_itec(&mut self, channel: usize) -> u16 { + match channel { + 0 => self.channel0.adc.convert( + &self.channel0.itec_pin, + stm32f4xx_hal::adc::config::SampleTime::Cycles_480 + ), + 1 => self.channel1.adc.convert( + &self.channel1.itec_pin, + stm32f4xx_hal::adc::config::SampleTime::Cycles_480 + ), + _ => unreachable!(), + } + } } diff --git a/src/main.rs b/src/main.rs index 40d94e6..ebdbc71 100644 --- a/src/main.rs +++ b/src/main.rs @@ -142,11 +142,18 @@ fn main() -> ! { for channel in 0..CHANNELS { if let Some(adc_data) = channels.channel_state(channel).adc_data { let dac_loopback = channels.read_dac_loopback(channel); + let itec = channels.read_itec(channel); + let dcc_u = 5.0; + let itec_u = dcc_u * (itec as f64) / (0xFFF as f64); + let tec_u = (itec_u - 1.5) / 8.0; + let tec_r = 5.0; + let tec_i = tec_u / tec_r; let state = channels.channel_state(channel); let _ = writeln!( - socket, "t={} raw{}=0x{:06X} dac_loopback=0x{:X}", + socket, "t={} raw{}=0x{:06X} dac_loopback=0x{:X} itec=0x{:X} tec={:.2}V/{:.2}A", state.adc_time, channel, adc_data, - dac_loopback + dac_loopback, itec, + tec_u, tec_i, ); } } diff --git a/src/pins.rs b/src/pins.rs index af43b64..169a94e 100644 --- a/src/pins.rs +++ b/src/pins.rs @@ -26,7 +26,8 @@ pub trait ChannelPins { type DacSpi: Transfer; type DacSync: OutputPin; type Shdn: OutputPin; - type DacLoopback; + type Adc; + type ItecPin; type DacLoopbackPin; } @@ -34,7 +35,8 @@ impl ChannelPins for Channel0 { type DacSpi = Dac0Spi; type DacSync = PE4>; type Shdn = PE10>; - type DacLoopback = Adc; + type Adc = Adc; + type ItecPin = PA6; type DacLoopbackPin = PA4; } @@ -42,7 +44,8 @@ impl ChannelPins for Channel1 { type DacSpi = Dac1Spi; type DacSync = PF6>; type Shdn = PE15>; - type DacLoopback = Adc; + type Adc = Adc; + type ItecPin = PB0; type DacLoopbackPin = PA5; } @@ -57,7 +60,8 @@ pub struct ChannelPinSet { pub dac_spi: C::DacSpi, pub dac_sync: C::DacSync, pub shdn: C::Shdn, - pub dac_loopback: C::DacLoopback, + pub adc: C::Adc, + pub itec_pin: C::ItecPin, pub dac_loopback_pin: C::DacLoopbackPin, } @@ -106,14 +110,16 @@ impl Pins { ); let mut shdn0 = gpioe.pe10.into_push_pull_output(); let _ = shdn0.set_low(); - let mut dac_loopback0 = Adc::adc1(adc1, true, Default::default()); - dac_loopback0.enable(); + let mut adc0 = Adc::adc1(adc1, true, Default::default()); + adc0.enable(); + let itec0_pin = gpioa.pa6.into_analog(); let dac_loopback0_pin = gpioa.pa4.into_analog(); let channel0 = ChannelPinSet { dac_spi: dac0_spi, dac_sync: dac0_sync, shdn: shdn0, - dac_loopback: dac_loopback0, + adc: adc0, + itec_pin: itec0_pin, dac_loopback_pin: dac_loopback0_pin, }; @@ -123,14 +129,16 @@ impl Pins { ); let mut shdn1 = gpioe.pe15.into_push_pull_output(); let _ = shdn1.set_low(); - let mut dac_loopback1 = Adc::adc2(adc2, true, Default::default()); - dac_loopback1.enable(); + let mut adc1 = Adc::adc2(adc2, true, Default::default()); + adc1.enable(); + let itec1_pin = gpiob.pb0.into_analog(); let dac_loopback1_pin = gpioa.pa5.into_analog(); let channel1 = ChannelPinSet { dac_spi: dac1_spi, dac_sync: dac1_sync, shdn: shdn1, - dac_loopback: dac_loopback1, + adc: adc1, + itec_pin: itec1_pin, dac_loopback_pin: dac_loopback1_pin, };