leds: init

This commit is contained in:
Astro 2020-09-06 21:10:10 +02:00
parent 9a912392be
commit 4e6aa5fe0c
3 changed files with 70 additions and 7 deletions

44
src/leds.rs Normal file
View File

@ -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<PD9<Output<PushPull>>>,
/// Green LED L3
pub g3: Led<PD10<Output<PushPull>>>,
/// Green LED L4
pub g4: Led<PD11<Output<PushPull>>>,
}
impl Leds {
pub fn new<M1, M2, M3>(r1: PD9<M1>, g3: PD10<M2>, g4: PD11<M3>) -> 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<P> {
pin: P,
}
impl<P: OutputPin> Led<P> {
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();
}
}

View File

@ -30,6 +30,7 @@ use smoltcp::{
mod init_log; mod init_log;
use init_log::init_log; use init_log::init_log;
mod leds;
mod pins; mod pins;
use pins::Pins; use pins::Pins;
mod ad7172; mod ad7172;
@ -89,12 +90,17 @@ fn main() -> ! {
timer::setup(cp.SYST, clocks); timer::setup(cp.SYST, clocks);
let (pins, eth_pins) = Pins::setup( let (pins, mut leds, eth_pins) = Pins::setup(
clocks, dp.TIM1, dp.TIM3, 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.SPI2, dp.SPI4, dp.SPI5,
dp.ADC1, dp.ADC1,
); );
leds.r1.on();
leds.g3.off();
leds.g4.off();
let mut channels = Channels::new(pins); let mut channels = Channels::new(pins);
channels.calibrate_dac_value(0); channels.calibrate_dac_value(0);
channels.calibrate_dac_value(1); channels.calibrate_dac_value(1);
@ -110,6 +116,8 @@ fn main() -> ! {
net::run(clocks, dp.ETHERNET_MAC, dp.ETHERNET_DMA, eth_pins, hwaddr, |iface| { net::run(clocks, dp.ETHERNET_MAC, dp.ETHERNET_DMA, eth_pins, hwaddr, |iface| {
Server::<Session>::run(iface, |server| { Server::<Session>::run(iface, |server| {
leds.r1.off();
loop { loop {
let instant = Instant::from_millis(i64::from(timer::now())); let instant = Instant::from_millis(i64::from(timer::now()));
let updated_channel = channels.poll_adc(instant); let updated_channel = channels.poll_adc(instant);
@ -259,11 +267,13 @@ fn main() -> ! {
} }
Command::PwmPid { channel } => { Command::PwmPid { channel } => {
channels.channel_state(channel).pid_engaged = true; channels.channel_state(channel).pid_engaged = true;
leds.g3.on();
let _ = writeln!(socket, "channel {}: PID enabled to control PWM", channel let _ = writeln!(socket, "channel {}: PID enabled to control PWM", channel
); );
} }
Command::Pwm { channel, pin: PwmPin::ISet, duty } => { Command::Pwm { channel, pin: PwmPin::ISet, duty } => {
channels.channel_state(channel).pid_engaged = false; channels.channel_state(channel).pid_engaged = false;
leds.g3.off();
let voltage = Volts(duty); let voltage = Volts(duty);
channels.set_dac(channel, voltage); channels.set_dac(channel, voltage);
let _ = writeln!( let _ = writeln!(
@ -376,6 +386,7 @@ fn main() -> ! {
// Update watchdog // Update watchdog
wd.feed(); wd.feed();
leds.g4.off();
cortex_m::interrupt::free(|cs| { cortex_m::interrupt::free(|cs| {
if !net::is_pending(cs) { if !net::is_pending(cs) {
// Wait for interrupts // Wait for interrupts
@ -383,6 +394,7 @@ fn main() -> ! {
wfi(); wfi();
} }
}); });
leds.g4.on();
} }
}); });
}); });

View File

@ -6,6 +6,7 @@ use stm32f4xx_hal::{
gpioa::*, gpioa::*,
gpiob::*, gpiob::*,
gpioc::*, gpioc::*,
gpiod::*,
gpioe::*, gpioe::*,
gpiof::*, gpiof::*,
gpiog::*, gpiog::*,
@ -16,11 +17,14 @@ use stm32f4xx_hal::{
rcc::Clocks, rcc::Clocks,
pwm::{self, PwmChannels}, pwm::{self, PwmChannels},
spi::{Spi, NoMiso}, 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, time::U32Ext,
}; };
use stm32_eth::EthPins; use stm32_eth::EthPins;
use crate::channel::{Channel0, Channel1}; use crate::{
channel::{Channel0, Channel1},
leds::Leds,
};
pub type EthernetPins = EthPins< pub type EthernetPins = EthPins<
@ -96,13 +100,14 @@ impl Pins {
pub fn setup( pub fn setup(
clocks: Clocks, clocks: Clocks,
tim1: TIM1, tim3: TIM3, 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, spi2: SPI2, spi4: SPI4, spi5: SPI5,
adc1: ADC1, adc1: ADC1,
) -> (Self, EthernetPins) { ) -> (Self, Leds, EthernetPins) {
let gpioa = gpioa.split(); let gpioa = gpioa.split();
let gpiob = gpiob.split(); let gpiob = gpiob.split();
let gpioc = gpioc.split(); let gpioc = gpioc.split();
let gpiod = gpiod.split();
let gpioe = gpioe.split(); let gpioe = gpioe.split();
let gpiof = gpiof.split(); let gpiof = gpiof.split();
let gpiog = gpiog.split(); let gpiog = gpiog.split();
@ -167,6 +172,8 @@ impl Pins {
channel1, channel1,
}; };
let leds = Leds::new(gpiod.pd9, gpiod.pd10.into_push_pull_output(), gpiod.pd11.into_push_pull_output());
let eth_pins = EthPins { let eth_pins = EthPins {
ref_clk: gpioa.pa1, ref_clk: gpioa.pa1,
md_io: gpioa.pa2, md_io: gpioa.pa2,
@ -179,7 +186,7 @@ impl Pins {
rx_d1: gpioc.pc5, rx_d1: gpioc.pc5,
}; };
(pins, eth_pins) (pins, leds, eth_pins)
} }
/// Configure the GPIO pins for SPI operation, and initialize SPI /// Configure the GPIO pins for SPI operation, and initialize SPI