From 0fa9832546305c03b93777aa1d605bfc39e79290 Mon Sep 17 00:00:00 2001
From: Florian Jung <flo@windfisch.org>
Date: Thu, 3 Oct 2019 23:55:15 +0200
Subject: [PATCH] usart support

---
 blinky-hal/Cargo.lock  |  1 +
 blinky-hal/Cargo.toml  |  2 ++
 blinky-hal/src/main.rs | 43 ++++++++++++++++++++++++++++++++++++++----
 3 files changed, 42 insertions(+), 4 deletions(-)

diff --git a/blinky-hal/Cargo.lock b/blinky-hal/Cargo.lock
index 42e646d..5702cb1 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 0b02768..3e896b1 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 a4ccc16..5ad670d 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);
-- 
GitLab