From 4e6aa5fe0c2c56782b0cf58808a6b8c0786425c9 Mon Sep 17 00:00:00 2001 From: Astro Date: Sun, 6 Sep 2020 21:10:10 +0200 Subject: [PATCH] leds: init --- src/leds.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 16 ++++++++++++++-- src/pins.rs | 17 ++++++++++++----- 3 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 src/leds.rs diff --git a/src/leds.rs b/src/leds.rs new file mode 100644 index 0000000..c03c9c5 --- /dev/null +++ b/src/leds.rs @@ -0,0 +1,44 @@ +use stm32f4xx_hal::{ + gpio::{ + gpiod::{PD9, PD10, PD11}, + Output, PushPull, + }, + hal::digital::v2::OutputPin, +}; + +pub struct Leds { + /// Red LED L1 + pub r1: Led>>, + /// Green LED L3 + pub g3: Led>>, + /// Green LED L4 + pub g4: Led>>, +} + +impl Leds { + pub fn new(r1: PD9, g3: PD10, g4: PD11) -> Self { + Leds { + r1: Led::new(r1.into_push_pull_output()), + g3: Led::new(g3.into_push_pull_output()), + g4: Led::new(g4.into_push_pull_output()), + } + } +} + +pub struct Led

{ + pin: P, +} + +impl Led

{ + pub fn new(pin: P) -> Self { + Led { pin } + } + + pub fn on(&mut self) { + let _ = self.pin.set_high(); + } + + pub fn off(&mut self) { + let _ = self.pin.set_low(); + } +} diff --git a/src/main.rs b/src/main.rs index 9149b08..c5157a3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,6 +30,7 @@ use smoltcp::{ mod init_log; use init_log::init_log; +mod leds; mod pins; use pins::Pins; mod ad7172; @@ -89,12 +90,17 @@ fn main() -> ! { timer::setup(cp.SYST, clocks); - let (pins, eth_pins) = Pins::setup( + let (pins, mut leds, eth_pins) = Pins::setup( clocks, dp.TIM1, dp.TIM3, - dp.GPIOA, dp.GPIOB, dp.GPIOC, dp.GPIOE, dp.GPIOF, dp.GPIOG, + dp.GPIOA, dp.GPIOB, dp.GPIOC, dp.GPIOD, dp.GPIOE, dp.GPIOF, dp.GPIOG, dp.SPI2, dp.SPI4, dp.SPI5, dp.ADC1, ); + + leds.r1.on(); + leds.g3.off(); + leds.g4.off(); + let mut channels = Channels::new(pins); channels.calibrate_dac_value(0); channels.calibrate_dac_value(1); @@ -110,6 +116,8 @@ fn main() -> ! { net::run(clocks, dp.ETHERNET_MAC, dp.ETHERNET_DMA, eth_pins, hwaddr, |iface| { Server::::run(iface, |server| { + leds.r1.off(); + loop { let instant = Instant::from_millis(i64::from(timer::now())); let updated_channel = channels.poll_adc(instant); @@ -259,11 +267,13 @@ fn main() -> ! { } Command::PwmPid { channel } => { channels.channel_state(channel).pid_engaged = true; + leds.g3.on(); let _ = writeln!(socket, "channel {}: PID enabled to control PWM", channel ); } Command::Pwm { channel, pin: PwmPin::ISet, duty } => { channels.channel_state(channel).pid_engaged = false; + leds.g3.off(); let voltage = Volts(duty); channels.set_dac(channel, voltage); let _ = writeln!( @@ -376,6 +386,7 @@ fn main() -> ! { // Update watchdog wd.feed(); + leds.g4.off(); cortex_m::interrupt::free(|cs| { if !net::is_pending(cs) { // Wait for interrupts @@ -383,6 +394,7 @@ fn main() -> ! { wfi(); } }); + leds.g4.on(); } }); }); diff --git a/src/pins.rs b/src/pins.rs index 9caa9e2..7ccdf5b 100644 --- a/src/pins.rs +++ b/src/pins.rs @@ -6,6 +6,7 @@ use stm32f4xx_hal::{ gpioa::*, gpiob::*, gpioc::*, + gpiod::*, gpioe::*, gpiof::*, gpiog::*, @@ -16,11 +17,14 @@ use stm32f4xx_hal::{ rcc::Clocks, pwm::{self, PwmChannels}, spi::{Spi, NoMiso}, - stm32::{ADC1, GPIOA, GPIOB, GPIOC, GPIOE, GPIOF, GPIOG, SPI2, SPI4, SPI5, TIM1, TIM3}, + stm32::{ADC1, GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, SPI2, SPI4, SPI5, TIM1, TIM3}, time::U32Ext, }; use stm32_eth::EthPins; -use crate::channel::{Channel0, Channel1}; +use crate::{ + channel::{Channel0, Channel1}, + leds::Leds, +}; pub type EthernetPins = EthPins< @@ -96,13 +100,14 @@ impl Pins { pub fn setup( clocks: Clocks, tim1: TIM1, tim3: TIM3, - gpioa: GPIOA, gpiob: GPIOB, gpioc: GPIOC, gpioe: GPIOE, gpiof: GPIOF, gpiog: GPIOG, + gpioa: GPIOA, gpiob: GPIOB, gpioc: GPIOC, gpiod: GPIOD, gpioe: GPIOE, gpiof: GPIOF, gpiog: GPIOG, spi2: SPI2, spi4: SPI4, spi5: SPI5, adc1: ADC1, - ) -> (Self, EthernetPins) { + ) -> (Self, Leds, EthernetPins) { let gpioa = gpioa.split(); let gpiob = gpiob.split(); let gpioc = gpioc.split(); + let gpiod = gpiod.split(); let gpioe = gpioe.split(); let gpiof = gpiof.split(); let gpiog = gpiog.split(); @@ -167,6 +172,8 @@ impl Pins { channel1, }; + let leds = Leds::new(gpiod.pd9, gpiod.pd10.into_push_pull_output(), gpiod.pd11.into_push_pull_output()); + let eth_pins = EthPins { ref_clk: gpioa.pa1, md_io: gpioa.pa2, @@ -179,7 +186,7 @@ impl Pins { rx_d1: gpioc.pc5, }; - (pins, eth_pins) + (pins, leds, eth_pins) } /// Configure the GPIO pins for SPI operation, and initialize SPI