From 30350a3651d6fd8e9a0896838415b04d3a5dec87 Mon Sep 17 00:00:00 2001 From: Egor Savkin Date: Wed, 14 Dec 2022 17:25:29 +0800 Subject: [PATCH] Draft support fan pwm settings Signed-off-by: Egor Savkin --- src/channels.rs | 18 +++++++++++++++++- src/command_handler.rs | 4 ++++ src/command_parser.rs | 15 ++++++++++++++- src/main.rs | 2 +- src/pins.rs | 24 +++++++++++++++++++----- 5 files changed, 55 insertions(+), 8 deletions(-) diff --git a/src/channels.rs b/src/channels.rs index 7aa34e9..5786310 100644 --- a/src/channels.rs +++ b/src/channels.rs @@ -342,12 +342,20 @@ impl Channels { get(&self.pwm.max_i_neg0), (0, PwmPin::MaxV) => get(&self.pwm.max_v0), + (0, PwmPin::Tacho) => + get(&self.pwm.tacho), + (0, PwmPin::Fan) => + get(&self.pwm.fan), (1, PwmPin::MaxIPos) => get(&self.pwm.max_i_pos1), (1, PwmPin::MaxINeg) => get(&self.pwm.max_i_neg1), (1, PwmPin::MaxV) => get(&self.pwm.max_v1), + (1, PwmPin::Tacho) => + get(&self.pwm.tacho), + (1, PwmPin::Fan) => + get(&self.pwm.fan), _ => unreachable!(), } @@ -381,7 +389,7 @@ impl Channels { (self.read_tec_u_meas(channel) - ElectricPotential::new::(1.5)) * 4.0 } - fn set_pwm(&mut self, channel: usize, pin: PwmPin, duty: f64) -> f64 { + pub fn set_pwm(&mut self, channel: usize, pin: PwmPin, duty: f64) -> f64 { fn set>(pin: &mut P, duty: f64) -> f64 { let max = pin.get_max_duty(); let value = ((duty * (max as f64)) as u16).min(max); @@ -397,12 +405,16 @@ impl Channels { set(&mut self.pwm.max_i_neg0, duty), (0, PwmPin::MaxV) => set(&mut self.pwm.max_v0, duty), + (0, PwmPin::Fan) => + set(&mut self.pwm.fan, duty), (1, PwmPin::MaxIPos) => set(&mut self.pwm.max_i_pos1, duty), (1, PwmPin::MaxINeg) => set(&mut self.pwm.max_i_neg1, duty), (1, PwmPin::MaxV) => set(&mut self.pwm.max_v1, duty), + (1, PwmPin::Fan) => + set(&mut self.pwm.fan, duty), _ => unreachable!(), } @@ -481,6 +493,8 @@ impl Channels { max_v: (self.get_max_v(channel), ElectricPotential::new::(5.0)).into(), max_i_pos: self.get_max_i_pos(channel).into(), max_i_neg: self.get_max_i_neg(channel).into(), + tacho: self.get_pwm(0, PwmPin::Tacho), + fan: self.get_pwm(0, PwmPin::Fan), } } @@ -578,6 +592,8 @@ pub struct PwmSummary { max_v: PwmSummaryField, max_i_pos: PwmSummaryField, max_i_neg: PwmSummaryField, + tacho: f64, + fan: f64, } #[derive(Serialize)] diff --git a/src/command_handler.rs b/src/command_handler.rs index 3144b0c..4932975 100644 --- a/src/command_handler.rs +++ b/src/command_handler.rs @@ -199,6 +199,10 @@ impl Handler { let current = ElectricCurrent::new::(value); channels.set_max_i_neg(channel, current); } + PwmPin::Tacho => {} + PwmPin::Fan => { + channels.set_pwm(channel, PwmPin::Fan, value); + } } send_line(socket, b"{}"); Ok(Handler::Handled) diff --git a/src/command_parser.rs b/src/command_parser.rs index 622f819..71770de 100644 --- a/src/command_parser.rs +++ b/src/command_parser.rs @@ -127,6 +127,8 @@ pub enum PwmPin { MaxIPos, MaxINeg, MaxV, + Tacho, + Fan } #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] @@ -299,7 +301,18 @@ fn pwm_setup(input: &[u8]) -> IResult<&[u8], Result<(PwmPin, f64), Error>> { ) ), result_with_pin(PwmPin::MaxV) - )) + ), + map( + preceded( + tag("fan"), + preceded( + whitespace, + float + ) + ), + result_with_pin(PwmPin::Fan) + ) + ) )(input) } diff --git a/src/main.rs b/src/main.rs index c1d7267..5be9240 100644 --- a/src/main.rs +++ b/src/main.rs @@ -119,7 +119,7 @@ fn main() -> ! { timer::setup(cp.SYST, clocks); let (pins, mut leds, mut eeprom, eth_pins, usb) = Pins::setup( - clocks, dp.TIM1, dp.TIM3, + clocks, dp.TIM1, dp.TIM3, dp.TIM8, dp.GPIOA, dp.GPIOB, dp.GPIOC, dp.GPIOD, dp.GPIOE, dp.GPIOF, dp.GPIOG, dp.I2C1, dp.SPI2, dp.SPI4, dp.SPI5, diff --git a/src/pins.rs b/src/pins.rs index f7b55a1..9f98ed7 100644 --- a/src/pins.rs +++ b/src/pins.rs @@ -23,7 +23,7 @@ use stm32f4xx_hal::{ I2C1, OTG_FS_GLOBAL, OTG_FS_DEVICE, OTG_FS_PWRCLK, SPI2, SPI4, SPI5, - TIM1, TIM3, + TIM1, TIM3, TIM8 }, timer::Timer, time::U32Ext, @@ -114,7 +114,7 @@ impl Pins { /// Setup GPIO pins and configure MCU peripherals pub fn setup( clocks: Clocks, - tim1: TIM1, tim3: TIM3, + tim1: TIM1, tim3: TIM3, tim8: TIM8, gpioa: GPIOA, gpiob: GPIOB, gpioc: GPIOC, gpiod: GPIOD, gpioe: GPIOE, gpiof: GPIOF, gpiog: GPIOG, i2c1: I2C1, spi2: SPI2, spi4: SPI4, spi5: SPI5, @@ -135,10 +135,10 @@ impl Pins { let pins_adc = Adc::adc1(adc1, true, Default::default()); let pwm = PwmPins::setup( - clocks, tim1, tim3, + clocks, tim1, tim3, tim8, gpioc.pc6, gpioc.pc7, gpioe.pe9, gpioe.pe11, - gpioe.pe13, gpioe.pe14 + gpioe.pe13, gpioe.pe14, gpioc.pc8, gpioc.pc9 ); let (dac0_spi, dac0_sync) = Self::setup_dac0( @@ -283,19 +283,24 @@ pub struct PwmPins { pub max_i_pos1: PwmChannels, pub max_i_neg0: PwmChannels, pub max_i_neg1: PwmChannels, + pub tacho: PwmChannels, + pub fan: PwmChannels, } impl PwmPins { - fn setup( + fn setup( clocks: Clocks, tim1: TIM1, tim3: TIM3, + tim8: TIM8, max_v0: PC6, max_v1: PC7, max_i_pos0: PE9, max_i_pos1: PE11, max_i_neg0: PE13, max_i_neg1: PE14, + tacho: PC8, + fan: PC9, ) -> PwmPins { let freq = 20u32.khz(); @@ -312,6 +317,14 @@ impl PwmPins { init_pwm_pin(&mut max_v0); init_pwm_pin(&mut max_v1); + let channels = ( + tacho.into_alternate(), + fan.into_alternate(), + ); + let (mut tacho, mut fan) = Timer::new(tim8, &clocks).pwm(channels, freq); + init_pwm_pin(&mut tacho); + init_pwm_pin(&mut fan); + let channels = ( max_i_pos0.into_alternate(), max_i_pos1.into_alternate(), @@ -329,6 +342,7 @@ impl PwmPins { max_v0, max_v1, max_i_pos0, max_i_pos1, max_i_neg0, max_i_neg1, + tacho, fan } } }