diff options
author | Brian Cully <bjc@kublai.com> | 2022-08-08 12:19:08 -0400 |
---|---|---|
committer | Brian Cully <bjc@kublai.com> | 2022-08-08 12:19:08 -0400 |
commit | cabe5b42cfe408c7bbde8b4bd40ce0282eb7e58c (patch) | |
tree | 9247007dc67478babeab603150bbe9daeeaa6bf6 /src/led.rs | |
parent | 340eb6eb0d3fb47f5d968c323c92943662457fda (diff) | |
download | luchie-cabe5b42cfe408c7bbde8b4bd40ce0282eb7e58c.tar.gz luchie-cabe5b42cfe408c7bbde8b4bd40ce0282eb7e58c.zip |
get serial printing on usart1 (tx: pa2, rx: pa3)
Diffstat (limited to 'src/led.rs')
-rw-r--r-- | src/led.rs | 49 |
1 files changed, 23 insertions, 26 deletions
@@ -1,41 +1,38 @@ -use gd32vf103_pac::Peripherals; +use gd32vf103xx_hal::{ + gpio::{Floating, Input, Output, PushPull, Pxx, State, gpioa::PA7}, +}; +use embedded_hal::digital::v2::OutputPin; -#[derive(Clone, Copy)] -pub struct LED(); +pub struct LED { + pin: Pxx<Output<PushPull>>, + state: State, +} impl LED { - pub fn new() -> Self { - let peripherals = unsafe { Peripherals::steal() }; - peripherals.RCU.apb2en.modify(|_, w| { - w.paen().set_bit() - }); - - peripherals.GPIOA.ctl0.modify(|_, w| unsafe { - // output mode, push-pull - w.ctl7().bits(0b00); - // 50 mhz output rate - w.md7().bits(0b11); - w - }); - Self() + pub fn new(pin: PA7<Input<Floating>>) -> Self { + let mut p = pin.into_push_pull_output().downgrade(); + p.set_low().ok(); + Self { pin: p, state: State::Low } } pub fn is_on(&self) -> bool { - let gpio = unsafe { Peripherals::steal() }.GPIOA; - gpio.octl.read().octl7().bit() + match self.state { + State::High => true, + State::Low => false, + } } - pub fn on(&self) { - let gpio = unsafe { Peripherals::steal() }.GPIOA; - gpio.bop.write(|w| w.bop7().set_bit()); + pub fn on(&mut self) { + self.state = State::High; + self.pin.set_high().ok(); } - pub fn off(&self) { - let gpio = unsafe { Peripherals::steal() }.GPIOA; - gpio.bc.write(|w| w.cr7().set_bit()); + pub fn off(&mut self) { + self.state = State::Low; + self.pin.set_low().ok(); } - pub fn toggle(&self) { + pub fn toggle(&mut self) { if self.is_on() { self.off(); } else { |