diff --git a/blinky-hal/Cargo.lock b/blinky-hal/Cargo.lock index 42e646d1723496052f6617824f4550254ef62637..5702cb17086f9f139e6e68ffb9c41aae48d5fe64 100644 --- a/blinky-hal/Cargo.lock +++ b/blinky-hal/Cargo.lock @@ -38,6 +38,7 @@ dependencies = [ "cortex-m 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", "cortex-m-rt 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)", "embedded-hal 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "nb 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "panic-halt 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "stm32f4xx-hal 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/blinky-hal/Cargo.toml b/blinky-hal/Cargo.toml index 0b02768af7c2a0e54ff0783cac4755514b5dfe5d..3e896b1c4ae5aa137e460ce01f97cf37a616dff6 100644 --- a/blinky-hal/Cargo.toml +++ b/blinky-hal/Cargo.toml @@ -39,3 +39,5 @@ version = "0.6.4" [dependencies.panic-halt] version = "0.2.0" + +[dependencies.nb] diff --git a/blinky-hal/src/main.rs b/blinky-hal/src/main.rs index a4ccc16db90a32da37647a05257dc719acaa0d27..5ad670dd08dfc22242ebcd1aed54af245d74903e 100644 --- a/blinky-hal/src/main.rs +++ b/blinky-hal/src/main.rs @@ -10,12 +10,18 @@ extern crate embedded_hal as hal; use cortex_m_rt::entry; +use core::fmt::Write; + use mcu::delay::Delay; use mcu::prelude::*; use mcu::stm32; use mcu::gpio; use mcu::gpio::gpiod::{PD12, PD13, PD14, PD15}; +use mcu::serial; + +use mcu::nb::block; + use hal::digital::OutputPin; @@ -32,6 +38,13 @@ struct Leds { fn main() -> ! { if let (Some(p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) { let gpiod = p.GPIOD.split(); + let gpioa = p.GPIOA.split(); + + // Constrain clock registers + let mut rcc = p.RCC.constrain(); + + // Configure clock to 168 MHz (i.e. the maximum) and freeze it + let clocks = rcc.cfgr.sysclk(84.mhz()).freeze(); // Configure LED outputs let mut leds = Leds { @@ -41,11 +54,14 @@ fn main() -> ! { blue: gpiod.pd15.into_push_pull_output(), }; - // Constrain clock registers - let mut rcc = p.RCC.constrain(); + let pin_tx = gpioa.pa2.into_alternate_af7(); + //let pin_rx = gpioa.pa3.into_alternate_af7(); + let pin_rx = serial::NoRx; + + let mut usart = serial::Serial::usart2(p.USART2, (pin_tx, pin_rx), serial::config::Config::default().baudrate(115200.bps()), clocks).unwrap(); + // separate into tx and rx channels + let (mut tx, mut rx) = usart.split(); - // Configure clock to 168 MHz (i.e. the maximum) and freeze it - let clocks = rcc.cfgr.sysclk(84.mhz()).freeze(); // Get delay provider let mut delay = Delay::new(cp.SYST, clocks); @@ -53,6 +69,25 @@ fn main() -> ! { loop { // Turn LED on leds.blue.set_high(); + + /*tx.write(b'h').ok(); + + tx.write(b'e').ok(); + tx.write(b'l').ok(); + tx.write(b'l').ok(); + tx.write(b'o').ok(); + tx.write(b'\r').ok(); + tx.write(b'\n').ok();*/ + + /*block!(usart.write(b'h')).ok(); + block!(usart.write(b'e')).ok(); + block!(usart.write(b'l')).ok(); + block!(usart.write(b'l')).ok(); + block!(usart.write(b'o')).ok();*/ + //usart.write_str("foo"); + tx.write_str("foo"); + writeln!(tx, "bar"); + // Delay twice for half a second due to limited timer resolution delay.delay_ms(500_u16);