diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/led.rs | 49 | ||||
-rwxr-xr-x | src/main.rs | 34 |
2 files changed, 52 insertions, 31 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 { diff --git a/src/main.rs b/src/main.rs index 2740d25..a711398 100755 --- a/src/main.rs +++ b/src/main.rs @@ -3,11 +3,15 @@ mod blink; mod led; +mod log; use gd32vf103xx_hal::{ + afio::AfioExt, eclic::{self, EclicExt}, + gpio::GpioExt, pac::{self, Peripherals}, rcu::RcuExt, + serial::Serial, time::Hertz, timer, }; @@ -26,19 +30,39 @@ pub enum PollResult { #[entry] fn main(_hartid: usize) -> ! { - let p = unsafe { Peripherals::steal() }; + let p = Peripherals::take().expect("couldn't take peripherals"); pac::ECLIC::set_threshold_level(eclic::Level::L0); pac::ECLIC::set_level_priority_bits(eclic::LevelPriorityBits::L3P1); - let t6 = p.TIMER6; let mut rcu = p.RCU.configure().freeze(); - let timer = timer::Timer::<pac::TIMER6>::timer6(t6, Hertz(1), &mut rcu); + let mut afio = p.AFIO.constrain(&mut rcu); - let led = LED::new(); + let timer = timer::Timer::<pac::TIMER6>::timer6(p.TIMER6, Hertz(1), &mut rcu); + let gpioa = p.GPIOA.split(&mut rcu); + let led = LED::new(gpioa.pa7); let mut blink = blink::Task::new(timer, Hertz(5), led); + + // TODO: figure out how to use the usb serial on the start board. + // + // this version has to be wired up physically. + let serial = Serial::new( + p.USART1, + (gpioa.pa2, gpioa.pa3), + Default::default(), + &mut afio, + &mut rcu, + ); + let (tx, _rx) = serial.split(); + log::new(tx); + loop { - if blink.poll() == PollResult::WouldBlock { + let mut can_sleep = true; + + can_sleep &= blink.poll() == PollResult::WouldBlock; + log::log("hi\r\n"); + + if can_sleep { unsafe { wfi() }; } } |