From 3f5f3cf79e94d8e3975b589f582c613c34b12bc5 Mon Sep 17 00:00:00 2001
From: Jorge Aparicio <jorge@japaric.io>
Date: Sun, 23 Jul 2017 21:42:05 -0500
Subject: [PATCH] update examples

---
 examples/blinky-blocking.rs | 60 +++++++++++--------------------------
 examples/capture1.rs        |  2 +-
 examples/capture2.rs        |  2 +-
 examples/capture3.rs        |  2 +-
 examples/capture4.rs        |  2 +-
 examples/hello-world.rs     | 44 +++++++++++++++++++++++++++
 examples/hello.rs           | 33 ++++----------------
 examples/spi1.rs            |  2 +-
 examples/spi2.rs            |  2 +-
 examples/wait1.rs           |  2 +-
 examples/wait2.rs           |  2 +-
 examples/wait3.rs           |  2 +-
 examples/wait4.rs           |  2 +-
 examples/ws2812.rs          |  9 +++---
 src/lib.rs                  |  1 -
 15 files changed, 83 insertions(+), 84 deletions(-)
 create mode 100644 examples/hello-world.rs

diff --git a/examples/blinky-blocking.rs b/examples/blinky-blocking.rs
index 8f58160..d0045e6 100644
--- a/examples/blinky-blocking.rs
+++ b/examples/blinky-blocking.rs
@@ -3,63 +3,42 @@
 #![allow(unreachable_code)] // for the `block!` macro
 #![deny(unsafe_code)]
 #![deny(warnings)]
-#![feature(const_fn)]
-#![feature(used)]
+#![feature(proc_macro)]
 #![no_std]
 
 extern crate blue_pill;
 
-extern crate embedded_hal as hal;
-
-// version = "0.2.3"
-extern crate cortex_m_rt;
-
-// version = "0.1.0"
-#[macro_use]
 extern crate cortex_m_rtfm as rtfm;
 
-#[macro_use]
+#[macro_use(block)]
 extern crate nb;
 
+use blue_pill::Timer;
 use blue_pill::led::{self, Green};
+use blue_pill::prelude::*;
 use blue_pill::time::Hertz;
-use blue_pill::{Timer, stm32f103xx};
-use hal::prelude::*;
-use rtfm::{P0, T0, TMax};
+use rtfm::app;
 
-// CONFIGURATION
 const FREQUENCY: Hertz = Hertz(1);
 
-// RESOURCES
-peripherals!(stm32f103xx, {
-    GPIOC: Peripheral {
-        ceiling: C0,
-    },
-    RCC: Peripheral {
-        ceiling: C0,
-    },
-    TIM3: Peripheral {
-        ceiling: C0,
-    },
-});
+app! {
+    device: blue_pill::stm32f103xx,
 
-// INITIALIZATION PHASE
-fn init(ref prio: P0, thr: &TMax) {
-    let gpioc = &GPIOC.access(prio, thr);
-    let rcc = &RCC.access(prio, thr);
-    let tim3 = TIM3.access(prio, thr);
+    idle: {
+        resources: [TIM3],
+    }
+}
 
-    let timer = Timer(&*tim3);
+fn init(p: init::Peripherals) {
+    led::init(p.GPIOC, p.RCC);
 
-    led::init(gpioc, rcc);
-    timer.init(FREQUENCY.invert(), rcc);
-}
+    let timer = Timer(&*p.TIM3);
 
-// IDLE LOOP
-fn idle(ref prio: P0, ref thr: T0) -> ! {
-    let tim3 = TIM3.access(prio, thr);
+    timer.init(FREQUENCY.invert(), p.RCC);
+}
 
-    let timer = Timer(&*tim3);
+fn idle(r: idle::Resources) -> ! {
+    let timer = Timer(&*r.TIM3);
 
     timer.resume();
     let mut state = false;
@@ -75,6 +54,3 @@ fn idle(ref prio: P0, ref thr: T0) -> ! {
         }
     }
 }
-
-// TASKS
-tasks!(stm32f103xx, {});
diff --git a/examples/capture1.rs b/examples/capture1.rs
index 00ec6e7..f07ee8a 100644
--- a/examples/capture1.rs
+++ b/examples/capture1.rs
@@ -37,7 +37,7 @@ fn idle(r: idle::Resources) -> ! {
     const CHANNELS: [Channel; 4] =
         [Channel::_1, Channel::_2, Channel::_3, Channel::_4];
 
-    let capture = Capture(&**r.TIM1);
+    let capture = Capture(&*r.TIM1);
 
     for c in &CHANNELS {
         capture.enable(*c);
diff --git a/examples/capture2.rs b/examples/capture2.rs
index f3936e5..70b89ef 100644
--- a/examples/capture2.rs
+++ b/examples/capture2.rs
@@ -37,7 +37,7 @@ fn idle(r: idle::Resources) -> ! {
     const CHANNELS: [Channel; 4] =
         [Channel::_1, Channel::_2, Channel::_3, Channel::_4];
 
-    let capture = Capture(&**r.TIM2);
+    let capture = Capture(&*r.TIM2);
 
     for c in &CHANNELS {
         capture.enable(*c);
diff --git a/examples/capture3.rs b/examples/capture3.rs
index 835d0af..f3bc006 100644
--- a/examples/capture3.rs
+++ b/examples/capture3.rs
@@ -36,7 +36,7 @@ fn init(p: init::Peripherals) {
 fn idle(r: idle::Resources) -> ! {
     const CHANNELS: [Channel; 2] = [Channel::_1, Channel::_2];
 
-    let capture = Capture(&**r.TIM3);
+    let capture = Capture(&*r.TIM3);
 
     for c in &CHANNELS {
         capture.enable(*c);
diff --git a/examples/capture4.rs b/examples/capture4.rs
index b156b6c..3ea4494 100644
--- a/examples/capture4.rs
+++ b/examples/capture4.rs
@@ -37,7 +37,7 @@ fn idle(r: idle::Resources) -> ! {
     const CHANNELS: [Channel; 4] =
         [Channel::_1, Channel::_2, Channel::_3, Channel::_4];
 
-    let capture = Capture(&**r.TIM4);
+    let capture = Capture(&*r.TIM4);
 
     for c in &CHANNELS {
         capture.enable(*c);
diff --git a/examples/hello-world.rs b/examples/hello-world.rs
new file mode 100644
index 0000000..e053bdb
--- /dev/null
+++ b/examples/hello-world.rs
@@ -0,0 +1,44 @@
+//! Prints "Hello" and then "World" in the OpenOCD console
+
+#![deny(unsafe_code)]
+#![deny(warnings)]
+#![feature(const_fn)]
+#![feature(proc_macro)]
+#![no_std]
+
+extern crate blue_pill;
+extern crate cortex_m_rtfm as rtfm;
+extern crate cortex_m_semihosting as semihosting;
+
+use core::fmt::Write;
+
+use rtfm::app;
+use semihosting::hio::{self, HStdout};
+
+app! {
+    device: blue_pill::stm32f103xx,
+
+    resources: {
+        static HSTDOUT: Option<HStdout> = None;
+    },
+
+    idle: {
+        resources: [HSTDOUT],
+    },
+}
+
+fn init(_p: init::Peripherals, r: init::Resources) {
+    let mut hstdout = hio::hstdout().unwrap();
+
+    writeln!(hstdout, "Hello").unwrap();
+
+    **r.HSTDOUT = Some(hstdout);
+}
+
+fn idle(r: idle::Resources) -> ! {
+    writeln!(r.HSTDOUT.as_mut().unwrap(), "World").unwrap();
+
+    loop {
+        rtfm::wfi();
+    }
+}
diff --git a/examples/hello.rs b/examples/hello.rs
index f2c0938..63833fc 100644
--- a/examples/hello.rs
+++ b/examples/hello.rs
@@ -1,49 +1,28 @@
-//! Prints "Hello" and then "World" on the OpenOCD console
+//! Prints "Hello, World" in the OpenOCD console
 
 #![deny(unsafe_code)]
 #![deny(warnings)]
-#![feature(const_fn)]
 #![feature(proc_macro)]
 #![no_std]
 
 extern crate blue_pill;
-extern crate cortex_m;
 extern crate cortex_m_rtfm as rtfm;
-extern crate cortex_m_semihosting;
+extern crate cortex_m_semihosting as semihosting;
 
 use core::fmt::Write;
 
-use cortex_m_semihosting::hio::{self, HStdout};
 use rtfm::app;
+use semihosting::hio;
 
 app! {
     device: blue_pill::stm32f103xx,
-
-    resources: {
-        static HSTDOUT: Option<HStdout> = None;
-    },
-
-    idle: {
-        resources: [HSTDOUT],
-    },
 }
 
-// INITIALIZATION PHASE
-fn init(_p: init::Peripherals, r: init::Resources) {
-    let mut hstdout = hio::hstdout().unwrap();
-
-    writeln!(hstdout, "Hello").unwrap();
+fn init(_p: init::Peripherals) {}
 
-    **r.HSTDOUT = Some(hstdout);
-}
-
-// IDLE LOOP
-fn idle(r: idle::Resources) -> ! {
-    if let Some(mut hstdout) = r.HSTDOUT.take() {
-        writeln!(hstdout, "World").unwrap();
-    }
+fn idle() -> ! {
+    writeln!(hio::hstdout().unwrap(), "Hello, world!").unwrap();
 
-    // Sleep
     loop {
         rtfm::wfi();
     }
diff --git a/examples/spi1.rs b/examples/spi1.rs
index 0b8b2c6..cbcd231 100644
--- a/examples/spi1.rs
+++ b/examples/spi1.rs
@@ -41,7 +41,7 @@ fn idle(r: idle::Resources) -> ! {
     // Read mode
     pub const R: u8 = 1 << 7;
 
-    let spi = Spi(&**r.SPI1);
+    let spi = Spi(&*r.SPI1);
 
     rtfm::bkpt();
 
diff --git a/examples/spi2.rs b/examples/spi2.rs
index 2f352f0..7436e24 100644
--- a/examples/spi2.rs
+++ b/examples/spi2.rs
@@ -41,7 +41,7 @@ fn idle(r: idle::Resources) -> ! {
     // Read mode
     pub const R: u8 = 1 << 7;
 
-    let spi = Spi(&**r.SPI2);
+    let spi = Spi(&*r.SPI2);
 
     rtfm::bkpt();
 
diff --git a/examples/wait1.rs b/examples/wait1.rs
index 4b3a811..bfe353c 100644
--- a/examples/wait1.rs
+++ b/examples/wait1.rs
@@ -34,7 +34,7 @@ fn init(p: init::Peripherals) {
 }
 
 fn idle(r: idle::Resources) -> ! {
-    let timer = Timer(&**r.TIM1);
+    let timer = Timer(&*r.TIM1);
 
     let mut state = false;
     loop {
diff --git a/examples/wait2.rs b/examples/wait2.rs
index 61e0403..daad69e 100644
--- a/examples/wait2.rs
+++ b/examples/wait2.rs
@@ -33,7 +33,7 @@ fn init(p: init::Peripherals) {
 }
 
 fn idle(r: idle::Resources) -> ! {
-    let timer = Timer(&**r.TIM2);
+    let timer = Timer(&*r.TIM2);
 
     let mut state = false;
     loop {
diff --git a/examples/wait3.rs b/examples/wait3.rs
index 8b05861..6023c5d 100644
--- a/examples/wait3.rs
+++ b/examples/wait3.rs
@@ -34,7 +34,7 @@ fn init(p: init::Peripherals) {
 }
 
 fn idle(r: idle::Resources) -> ! {
-    let timer = Timer(&**r.TIM3);
+    let timer = Timer(&*r.TIM3);
 
     let mut state = false;
     loop {
diff --git a/examples/wait4.rs b/examples/wait4.rs
index cc6b2df..fe14a34 100644
--- a/examples/wait4.rs
+++ b/examples/wait4.rs
@@ -34,7 +34,7 @@ fn init(p: init::Peripherals) {
 }
 
 fn idle(r: idle::Resources) -> ! {
-    let timer = Timer(&**r.TIM4);
+    let timer = Timer(&*r.TIM4);
 
     let mut state = false;
     loop {
diff --git a/examples/ws2812.rs b/examples/ws2812.rs
index 59dc20b..5f61faa 100644
--- a/examples/ws2812.rs
+++ b/examples/ws2812.rs
@@ -17,7 +17,7 @@ use blue_pill::dma::{Buffer, Dma1Channel2};
 use blue_pill::prelude::*;
 use blue_pill::time::Hertz;
 use blue_pill::{Channel, Pwm};
-use rtfm::app;
+use rtfm::{app, Static};
 
 // CONFIGURATION
 const FREQUENCY: Hertz = Hertz(200_000);
@@ -52,11 +52,12 @@ fn init(p: init::Peripherals, r: init::Resources) {
 }
 
 fn idle(r: idle::Resources) -> ! {
-    let pwm = Pwm(&**r.TIM2);
+    let pwm = Pwm(&*r.TIM2);
+    let buffer = Static::wrap_mut(r.BUFFER);
 
-    pwm.set_duties(r.DMA1, Channel::_1, r.BUFFER).unwrap();
+    pwm.set_duties(r.DMA1, Channel::_1, buffer).unwrap();
 
-    block!(r.BUFFER.release(r.DMA1)).unwrap();
+    block!(buffer.release(r.DMA1)).unwrap();
 
     rtfm::bkpt();
 
diff --git a/src/lib.rs b/src/lib.rs
index 83a7832..3e15a7f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -19,7 +19,6 @@
 #![no_std]
 
 extern crate cast;
-extern crate either;
 extern crate embedded_hal as hal;
 extern crate nb;
 extern crate static_ref;
-- 
GitLab