From 5e998d1a6c2a3307faae6b3a2f55d6712a394d59 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timo=20Kr=C3=B6ger?= <timokroeger93@gmail.com>
Date: Wed, 21 Jul 2021 23:12:36 +0200
Subject: [PATCH] Cleanup stm32f4 examples

* Remove dependency on stm32f4 pac crate
* Remove unused `ZeroClock`
---
 examples/stm32f4/Cargo.toml             |  3 +-
 examples/stm32f4/src/bin/blinky.rs      | 34 ++++++++--------
 examples/stm32f4/src/bin/button.rs      | 37 ++++++++---------
 examples/stm32f4/src/bin/button_exti.rs | 52 ++++++++++--------------
 examples/stm32f4/src/bin/spi.rs         | 39 ++++++++----------
 examples/stm32f4/src/bin/usart.rs       | 53 ++++++++-----------------
 examples/stm32f4/src/bin/usart_dma.rs   | 53 +++++++------------------
 7 files changed, 101 insertions(+), 170 deletions(-)

diff --git a/examples/stm32f4/Cargo.toml b/examples/stm32f4/Cargo.toml
index 8625f83b5..704a76390 100644
--- a/examples/stm32f4/Cargo.toml
+++ b/examples/stm32f4/Cargo.toml
@@ -19,9 +19,8 @@ defmt-error = []
 [dependencies]
 embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-trace"] }
 embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] }
-embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "stm32f429zi"]  }
+embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "stm32f429zi", "unstable-pac"]  }
 embassy-extras = {version = "0.1.0", path = "../../embassy-extras" }
-stm32f4 = { version = "0.13", features = ["stm32f429"] }
 
 defmt = "0.2.0"
 defmt-rtt = "0.2.0"
diff --git a/examples/stm32f4/src/bin/blinky.rs b/examples/stm32f4/src/bin/blinky.rs
index d2b607c2c..0e411d782 100644
--- a/examples/stm32f4/src/bin/blinky.rs
+++ b/examples/stm32f4/src/bin/blinky.rs
@@ -9,34 +9,32 @@
 #[path = "../example_common.rs"]
 mod example_common;
 use embassy_stm32::gpio::{Level, Output, Speed};
+use embassy_stm32::pac;
 use embedded_hal::digital::v2::OutputPin;
 use example_common::*;
 
 use cortex_m_rt::entry;
-use stm32f4::stm32f429 as pac;
 
 #[entry]
 fn main() -> ! {
     info!("Hello World!");
 
-    let pp = pac::Peripherals::take().unwrap();
+    unsafe {
+        pac::DBGMCU.cr().modify(|w| {
+            w.set_dbg_sleep(true);
+            w.set_dbg_standby(true);
+            w.set_dbg_stop(true);
+        });
 
-    pp.DBGMCU.cr.modify(|_, w| {
-        w.dbg_sleep().set_bit();
-        w.dbg_standby().set_bit();
-        w.dbg_stop().set_bit()
-    });
-    pp.RCC.ahb1enr.modify(|_, w| w.dma1en().enabled());
-
-    pp.RCC.ahb1enr.modify(|_, w| {
-        w.gpioaen().enabled();
-        w.gpioben().enabled();
-        w.gpiocen().enabled();
-        w.gpioden().enabled();
-        w.gpioeen().enabled();
-        w.gpiofen().enabled();
-        w
-    });
+        pac::RCC.ahb1enr().modify(|w| {
+            w.set_gpioaen(true);
+            w.set_gpioben(true);
+            w.set_gpiocen(true);
+            w.set_gpioden(true);
+            w.set_gpioeen(true);
+            w.set_gpiofen(true);
+        });
+    }
 
     let p = embassy_stm32::init(Default::default());
 
diff --git a/examples/stm32f4/src/bin/button.rs b/examples/stm32f4/src/bin/button.rs
index c7160d219..901fce418 100644
--- a/examples/stm32f4/src/bin/button.rs
+++ b/examples/stm32f4/src/bin/button.rs
@@ -8,35 +8,32 @@
 
 #[path = "../example_common.rs"]
 mod example_common;
+use cortex_m_rt::entry;
 use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed};
+use embassy_stm32::pac;
 use embedded_hal::digital::v2::{InputPin, OutputPin};
 use example_common::*;
 
-use cortex_m_rt::entry;
-use stm32f4::stm32f429 as pac;
-
 #[entry]
 fn main() -> ! {
     info!("Hello World!");
 
-    let pp = pac::Peripherals::take().unwrap();
+    unsafe {
+        pac::DBGMCU.cr().modify(|w| {
+            w.set_dbg_sleep(true);
+            w.set_dbg_standby(true);
+            w.set_dbg_stop(true);
+        });
 
-    pp.DBGMCU.cr.modify(|_, w| {
-        w.dbg_sleep().set_bit();
-        w.dbg_standby().set_bit();
-        w.dbg_stop().set_bit()
-    });
-    pp.RCC.ahb1enr.modify(|_, w| w.dma1en().enabled());
-
-    pp.RCC.ahb1enr.modify(|_, w| {
-        w.gpioaen().enabled();
-        w.gpioben().enabled();
-        w.gpiocen().enabled();
-        w.gpioden().enabled();
-        w.gpioeen().enabled();
-        w.gpiofen().enabled();
-        w
-    });
+        pac::RCC.ahb1enr().modify(|w| {
+            w.set_gpioaen(true);
+            w.set_gpioben(true);
+            w.set_gpiocen(true);
+            w.set_gpioden(true);
+            w.set_gpioeen(true);
+            w.set_gpiofen(true);
+        });
+    }
 
     let p = embassy_stm32::init(Default::default());
 
diff --git a/examples/stm32f4/src/bin/button_exti.rs b/examples/stm32f4/src/bin/button_exti.rs
index 8fc889dad..63c273b1a 100644
--- a/examples/stm32f4/src/bin/button_exti.rs
+++ b/examples/stm32f4/src/bin/button_exti.rs
@@ -9,7 +9,6 @@
 #[path = "../example_common.rs"]
 mod example_common;
 use embassy::executor::Executor;
-use embassy::time::Clock;
 use embassy::util::Forever;
 use embassy_stm32::exti::ExtiInput;
 use embassy_stm32::gpio::{Input, Pull};
@@ -17,7 +16,7 @@ use embassy_traits::gpio::{WaitForFallingEdge, WaitForRisingEdge};
 use example_common::*;
 
 use cortex_m_rt::entry;
-use stm32f4::stm32f429 as pac;
+use embassy_stm32::pac;
 
 #[embassy::task]
 async fn main_task() {
@@ -36,44 +35,33 @@ async fn main_task() {
     }
 }
 
-struct ZeroClock;
-
-impl Clock for ZeroClock {
-    fn now(&self) -> u64 {
-        0
-    }
-}
-
 static EXECUTOR: Forever<Executor> = Forever::new();
 
 #[entry]
 fn main() -> ! {
     info!("Hello World!");
 
-    let pp = pac::Peripherals::take().unwrap();
+    unsafe {
+        pac::DBGMCU.cr().modify(|w| {
+            w.set_dbg_sleep(true);
+            w.set_dbg_standby(true);
+            w.set_dbg_stop(true);
+        });
 
-    pp.DBGMCU.cr.modify(|_, w| {
-        w.dbg_sleep().set_bit();
-        w.dbg_standby().set_bit();
-        w.dbg_stop().set_bit()
-    });
-    pp.RCC.ahb1enr.modify(|_, w| w.dma1en().enabled());
+        pac::RCC.ahb1enr().modify(|w| {
+            w.set_gpioaen(true);
+            w.set_gpioben(true);
+            w.set_gpiocen(true);
+            w.set_gpioden(true);
+            w.set_gpioeen(true);
+            w.set_gpiofen(true);
+        });
 
-    pp.RCC.ahb1enr.modify(|_, w| {
-        w.gpioaen().enabled();
-        w.gpioben().enabled();
-        w.gpiocen().enabled();
-        w.gpioden().enabled();
-        w.gpioeen().enabled();
-        w.gpiofen().enabled();
-        w
-    });
-    pp.RCC.apb2enr.modify(|_, w| {
-        w.syscfgen().enabled();
-        w
-    });
-
-    unsafe { embassy::time::set_clock(&ZeroClock) };
+        // EXTI clock
+        pac::RCC.apb2enr().modify(|w| {
+            w.set_syscfgen(true);
+        });
+    }
 
     let executor = EXECUTOR.put(Executor::new());
 
diff --git a/examples/stm32f4/src/bin/spi.rs b/examples/stm32f4/src/bin/spi.rs
index aa48ceed5..7cf391394 100644
--- a/examples/stm32f4/src/bin/spi.rs
+++ b/examples/stm32f4/src/bin/spi.rs
@@ -14,38 +14,31 @@ use embedded_hal::digital::v2::OutputPin;
 use example_common::*;
 
 use cortex_m_rt::entry;
+use embassy_stm32::pac;
 use embassy_stm32::spi::{Config, Spi};
 use embassy_stm32::time::Hertz;
 use embedded_hal::blocking::spi::Transfer;
-use stm32f4::stm32f429 as pac;
 
 #[entry]
 fn main() -> ! {
     info!("Hello World, dude!");
 
-    let pp = pac::Peripherals::take().unwrap();
+    unsafe {
+        pac::DBGMCU.cr().modify(|w| {
+            w.set_dbg_sleep(true);
+            w.set_dbg_standby(true);
+            w.set_dbg_stop(true);
+        });
 
-    pp.DBGMCU.cr.modify(|_, w| {
-        w.dbg_sleep().set_bit();
-        w.dbg_standby().set_bit();
-        w.dbg_stop().set_bit()
-    });
-    pp.RCC.ahb1enr.modify(|_, w| w.dma1en().set_bit());
-
-    pp.RCC.apb1enr.modify(|_, w| {
-        w.spi3en().enabled();
-        w
-    });
-
-    pp.RCC.ahb1enr.modify(|_, w| {
-        w.gpioaen().enabled();
-        w.gpioben().enabled();
-        w.gpiocen().enabled();
-        w.gpioden().enabled();
-        w.gpioeen().enabled();
-        w.gpiofen().enabled();
-        w
-    });
+        pac::RCC.ahb1enr().modify(|w| {
+            w.set_gpioaen(true);
+            w.set_gpioben(true);
+            w.set_gpiocen(true);
+            w.set_gpioden(true);
+            w.set_gpioeen(true);
+            w.set_gpiofen(true);
+        });
+    }
 
     let p = embassy_stm32::init(Default::default());
 
diff --git a/examples/stm32f4/src/bin/usart.rs b/examples/stm32f4/src/bin/usart.rs
index 31525036a..dbe17c910 100644
--- a/examples/stm32f4/src/bin/usart.rs
+++ b/examples/stm32f4/src/bin/usart.rs
@@ -10,14 +10,13 @@
 mod example_common;
 use cortex_m::prelude::_embedded_hal_blocking_serial_Write;
 use embassy::executor::Executor;
-use embassy::time::Clock;
 use embassy::util::Forever;
 use embassy_stm32::dma::NoDma;
 use embassy_stm32::usart::{Config, Uart};
 use example_common::*;
 
 use cortex_m_rt::entry;
-use stm32f4::stm32f429 as pac;
+use embassy_stm32::pac;
 
 #[embassy::task]
 async fn main_task() {
@@ -36,48 +35,28 @@ async fn main_task() {
     }
 }
 
-struct ZeroClock;
-
-impl Clock for ZeroClock {
-    fn now(&self) -> u64 {
-        0
-    }
-}
-
 static EXECUTOR: Forever<Executor> = Forever::new();
 
 #[entry]
 fn main() -> ! {
     info!("Hello World!");
 
-    let pp = pac::Peripherals::take().unwrap();
+    unsafe {
+        pac::DBGMCU.cr().modify(|w| {
+            w.set_dbg_sleep(true);
+            w.set_dbg_standby(true);
+            w.set_dbg_stop(true);
+        });
 
-    pp.DBGMCU.cr.modify(|_, w| {
-        w.dbg_sleep().set_bit();
-        w.dbg_standby().set_bit();
-        w.dbg_stop().set_bit()
-    });
-    pp.RCC.ahb1enr.modify(|_, w| w.dma1en().enabled());
-
-    pp.RCC.ahb1enr.modify(|_, w| {
-        w.gpioaen().enabled();
-        w.gpioben().enabled();
-        w.gpiocen().enabled();
-        w.gpioden().enabled();
-        w.gpioeen().enabled();
-        w.gpiofen().enabled();
-        w
-    });
-    pp.RCC.apb2enr.modify(|_, w| {
-        w.syscfgen().enabled();
-        w
-    });
-    pp.RCC.apb1enr.modify(|_, w| {
-        w.usart3en().enabled();
-        w
-    });
-
-    unsafe { embassy::time::set_clock(&ZeroClock) };
+        pac::RCC.ahb1enr().modify(|w| {
+            w.set_gpioaen(true);
+            w.set_gpioben(true);
+            w.set_gpiocen(true);
+            w.set_gpioden(true);
+            w.set_gpioeen(true);
+            w.set_gpiofen(true);
+        });
+    }
 
     let executor = EXECUTOR.put(Executor::new());
 
diff --git a/examples/stm32f4/src/bin/usart_dma.rs b/examples/stm32f4/src/bin/usart_dma.rs
index b578aebca..9de46375e 100644
--- a/examples/stm32f4/src/bin/usart_dma.rs
+++ b/examples/stm32f4/src/bin/usart_dma.rs
@@ -11,14 +11,13 @@ mod example_common;
 use core::fmt::Write;
 use cortex_m_rt::entry;
 use embassy::executor::Executor;
-use embassy::time::Clock;
 use embassy::util::Forever;
 use embassy_stm32::dma::NoDma;
+use embassy_stm32::pac;
 use embassy_stm32::usart::{Config, Uart};
 use embassy_traits::uart::Write as _;
 use example_common::*;
 use heapless::String;
-use stm32f4::stm32f429 as pac;
 
 #[embassy::task]
 async fn main_task() {
@@ -36,50 +35,28 @@ async fn main_task() {
     }
 }
 
-struct ZeroClock;
-
-impl Clock for ZeroClock {
-    fn now(&self) -> u64 {
-        0
-    }
-}
-
 static EXECUTOR: Forever<Executor> = Forever::new();
 
 #[entry]
 fn main() -> ! {
     info!("Hello World!");
 
-    let pp = pac::Peripherals::take().unwrap();
-
-    pp.DBGMCU.cr.modify(|_, w| {
-        w.dbg_sleep().set_bit();
-        w.dbg_standby().set_bit();
-        w.dbg_stop().set_bit()
-    });
-    pp.RCC.ahb1enr.modify(|_, w| w.dma1en().enabled());
-
-    pp.RCC.ahb1enr.modify(|_, w| {
-        w.gpioaen().enabled();
-        w.gpioben().enabled();
-        w.gpiocen().enabled();
-        w.gpioden().enabled();
-        w.gpioeen().enabled();
-        w.gpiofen().enabled();
-        w.dma1en().enabled();
-        w.dma2en().enabled();
-        w
-    });
-    pp.RCC.apb2enr.modify(|_, w| {
-        w.syscfgen().enabled();
-        w
-    });
-    pp.RCC.apb1enr.modify(|_, w| {
-        w.usart3en().enabled();
-        w
+    unsafe {
+        pac::DBGMCU.cr().modify(|w| {
+            w.set_dbg_sleep(true);
+            w.set_dbg_standby(true);
+            w.set_dbg_stop(true);
     });
 
-    unsafe { embassy::time::set_clock(&ZeroClock) };
+        pac::RCC.ahb1enr().modify(|w| {
+            w.set_gpioaen(true);
+            w.set_gpioben(true);
+            w.set_gpiocen(true);
+            w.set_gpioden(true);
+            w.set_gpioeen(true);
+            w.set_gpiofen(true);
+    });
+    }
 
     let executor = EXECUTOR.put(Executor::new());