From cabe5b42cfe408c7bbde8b4bd40ce0282eb7e58c Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Mon, 8 Aug 2022 12:19:08 -0400 Subject: get serial printing on usart1 (tx: pa2, rx: pa3) --- Cargo.lock | 2 ++ Cargo.toml | 2 ++ README.org | 3 ++- src/led.rs | 49 +++++++++++++++++++++++-------------------------- src/main.rs | 34 +++++++++++++++++++++++++++++----- 5 files changed, 58 insertions(+), 32 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 282789f..7b0fdc3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -97,8 +97,10 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" name = "luchie" version = "0.1.0" dependencies = [ + "embedded-hal", "gd32vf103-pac", "gd32vf103xx-hal", + "nb 1.0.0", "riscv 0.8.0", "riscv-rt", ] diff --git a/Cargo.toml b/Cargo.toml index 0d23eb7..3a8e031 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,8 @@ gd32vf103-pac = "0.4.0" gd32vf103xx-hal = "0.5.0" riscv = "0.8.0" riscv-rt = "0.9.0" +nb = "1.0.0" +embedded-hal = "0.2.7" [[bin]] name = "luchie" diff --git a/README.org b/README.org index 1fd9975..9ad9910 100644 --- a/README.org +++ b/README.org @@ -1,4 +1,4 @@ -looks like the start board is a gd32vf103cbt from the tiny print silk-screened on the chip. [[https://www.tme.com/us/en-us/details/gd32vf103c-start/development-kits-others/gigadevice/][TME]] says it's a gd32vf103d6t6 +looks like the start board is a gd32vf103cbt from the tiny print silk-screened on the chip. [[https://www.tme.com/us/en-us/details/gd32vf103c-start/development-kits-others/gigadevice/][TME]] says it's a gd32vf103d6t6 — but [[https://www.gigadevice.com/products/microcontrollers/gd32-development-tools/gd32-mcu-starter-kits/][gigadevice]] says gd32vf103cbt6 which means it has 128k flash and 32k sram and 37(!) gpio pins @@ -17,3 +17,4 @@ usb is 2.0 full-speed (12Mbps) * USART On [[https://github.com/tyustli/rt-thread/tree/master/bsp/gd32vf103v-eval][github]] it says that ~uart0~ tx/rx is gpio 9/10 — but that's for the eval board. See the [[https://github.com/riscv-mcu/GD32VF103_Firmware_Library/tree/master/Examples/USART/Printf][risc-v repository]] for a printf example. +The [[https://docs.zephyrproject.org/latest/boards/riscv/gd32vf103c_starter/doc/index.html][zephyr documentation]] for it indicates the usb is on ~usart0~ with tx/rx pins ~pa9~ / ~pa10~ diff --git a/src/led.rs b/src/led.rs index 951a32e..536121b 100644 --- a/src/led.rs +++ b/src/led.rs @@ -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>, + 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>) -> 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::::timer6(t6, Hertz(1), &mut rcu); + let mut afio = p.AFIO.constrain(&mut rcu); - let led = LED::new(); + let timer = timer::Timer::::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() }; } } -- cgit v1.2.3