From 9540f6d2148eacb7aad75c468d19cdf847e4a02f Mon Sep 17 00:00:00 2001
From: Per Lindgren <per.lindgren@ltu.se>
Date: Sun, 10 Jan 2021 21:34:27 +0100
Subject: [PATCH] simple pwm added

---
 .cargo/config           |  4 ++--
 Cargo.toml              |  1 +
 README.md               | 27 ++++++++++++++-------
 examples/rtic_blinky.rs |  2 +-
 examples/rtt-pwm.rs     | 53 +++++++++++++++++++++++++++++++++++++++++
 examples/rtt-timing.rs  |  1 +
 openocd.cfg             |  2 +-
 openocd.gdb             |  2 +-
 8 files changed, 79 insertions(+), 13 deletions(-)
 create mode 100644 examples/rtt-pwm.rs

diff --git a/.cargo/config b/.cargo/config
index e4946d4..3b7dfa1 100644
--- a/.cargo/config
+++ b/.cargo/config
@@ -34,8 +34,8 @@ rustflags = [
 # Pick ONE of these compilation targets
 # target = "thumbv6m-none-eabi"        # Cortex-M0 and Cortex-M0+
 # target = "thumbv7m-none-eabi"        # Cortex-M3
-target = "thumbv7em-none-eabi"       # Cortex-M4 and Cortex-M7 (no FPU)
-# target = "thumbv7em-none-eabihf"     # Cortex-M4F and Cortex-M7F (with FPU)
+# target = "thumbv7em-none-eabi"       # Cortex-M4 and Cortex-M7 (no FPU)
+target = "thumbv7em-none-eabihf"     # Cortex-M4F and Cortex-M7F (with FPU)
 # target = "thumbv8m.base-none-eabi"   # Cortex-M23
 # target = "thumbv8m.main-none-eabi"   # Cortex-M33 (no FPU)
 # target = "thumbv8m.main-none-eabihf" # Cortex-M33 (with FPU)
diff --git a/Cargo.toml b/Cargo.toml
index e9e9cc8..c250d53 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -39,6 +39,7 @@ features = ["stm32f411", "rt"]
 [dependencies.stm32f4xx-hal]
 version = "0.8.3"
 features = ["rt", "stm32f411", "usb_fs"] 
+git = "https://github.com/stm32-rs/stm32f4xx-hal"
 
 # this lets you use `cargo fix`!
 [[bin]]
diff --git a/README.md b/README.md
index 884c8e6..e35bb5b 100644
--- a/README.md
+++ b/README.md
@@ -2,20 +2,31 @@
 
 ## Resources
 
-- [UM1724 - stm32 Nucleo-64](https://www.st.com/resource/en/user_manual/dm00105823-stm32-nucleo64-boards-mb1136-stmicroelectronics.pdf)
+- Nucleo 64
+  - [UM1724 - stm32 Nucleo-64](https://www.st.com/resource/en/user_manual/dm00105823-stm32-nucleo64-boards-mb1136-stmicroelectronics.pdf).
+  - [Nucleo 64 Schematics](https://www.st.com/resource/en/schematic_pack/nucleo_64pins_sch.zip) (The file MB1136.pdf is the schematics in pdf.)
+  - [stm32f4xx_hal](https://docs.rs/stm32f4xx-hal/0.8.3/stm32f4xx_hal/) documentation of the HAL API, and [git repository](https://github.com/stm32-rs/stm32f4xx-hal).
 
-- [RM0383 - F411 Reference Manual](https://www.st.com/resource/zh/reference_manual/dm00119316-stm32f411xce-advanced-armbased-32bit-mcus-stmicroelectronics.pdf) and [RM0368 - F401 Reference Manual](https://www.st.com/resource/en/reference_manual/dm00096844-stm32f401xbc-and-stm32f401xde-advanced-armbased-32bit-mcus-stmicroelectronics.pdf)
 
-- [Nucleo 64 Schematics](https://www.st.com/resource/en/schematic_pack/nucleo_64pins_sch.zip) (The file MB1136.pdf is the schematics in pdf.)
+- STM32F01/FO11
+  - [RM0383 - F411 Reference Manual](https://www.st.com/resource/zh/reference_manual/dm00119316-stm32f411xce-advanced-armbased-32bit-mcus-stmicroelectronics.pdf) 
+  - [RM0368 - F401 Reference Manual](https://www.st.com/resource/en/reference_manual/dm00096844-stm32f401xbc-and-stm32f401xde-advanced-armbased-32bit-mcus-stmicroelectronics.pdf)
+  - [PM0214 - M4 Programming manual](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&ved=2ahUKEwjOtd645OTtAhXEHXcKHdwYCoQQFjAAegQIBhAC&url=https%3A%2F%2Fwww.st.com%2Fresource%2Fen%2Fprogramming_manual%2Fdm00046982-stm32-cortex-m4-mcus-and-mpus-programming-manual-stmicroelectronics.pdf&usg=AOvVaw0n3XXybtMMDbifhDZse1Pl)
 
-- [PM0214 - M4 Programming manual](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&ved=2ahUKEwjOtd645OTtAhXEHXcKHdwYCoQQFjAAegQIBhAC&url=https%3A%2F%2Fwww.st.com%2Fresource%2Fen%2Fprogramming_manual%2Fdm00046982-stm32-cortex-m4-mcus-and-mpus-programming-manual-stmicroelectronics.pdf&usg=AOvVaw0n3XXybtMMDbifhDZse1Pl)
+- PixArt PMW33xx Optical Navigation Chip
+  - [PMW3389DM-T3QU](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&ved=2ahUKEwicx5OA9eTtAhWC-yoKHVfKAJ0QFjAAegQIBhAC&url=https%3A%2F%2Fwww.pixart.com%2F_getfs.php%3Ftb%3Dproduct%26id%3D4%26fs%3Dck2_fs_cn&usg=AOvVaw1A1rR533Pt-7EgnVSS-_ch), optical navigation chip
+  - [Jack Enterprise Breakout Board](https://www.tindie.com/products/jkicklighter/pmw3389-motion-sensor/), an example design with software linked.
 
-- [PMW3389DM-T3QU](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&ved=2ahUKEwicx5OA9eTtAhWC-yoKHVfKAJ0QFjAAegQIBhAC&url=https%3A%2F%2Fwww.pixart.com%2F_getfs.php%3Ftb%3Dproduct%26id%3D4%26fs%3Dck2_fs_cn&usg=AOvVaw1A1rR533Pt-7EgnVSS-_ch), optical navigation chip
+- General Embedded
+  - [Introduction to SPI](https://www.analog.com/en/analog-dialogue/articles/introduction-to-spi-interface.html#), a short introduction to the SPI interface.
 
-- [Jack Enterprise Breakout Board](https://www.tindie.com/products/jkicklighter/pmw3389-motion-sensor/), an example design with software linked.
 
-- [Introduction to SPI](https://www.analog.com/en/analog-dialogue/articles/introduction-to-spi-interface.html#), a short introduction to the SPI interface.
-  
+
+
+---
+
+## Connections
+
 - [USB Cable]
 
 | Signl | Color | Pin  |
diff --git a/examples/rtic_blinky.rs b/examples/rtic_blinky.rs
index 9bf9bb2..6828905 100644
--- a/examples/rtic_blinky.rs
+++ b/examples/rtic_blinky.rs
@@ -5,7 +5,7 @@
 
 use cortex_m::peripheral::DWT;
 use cortex_m_semihosting::hprintln;
-use panic_halt as _;
+use panic_rtt_target as _;
 use rtic::cyccnt::{Instant, U32Ext as _};
 use stm32f4xx_hal::stm32;
 
diff --git a/examples/rtt-pwm.rs b/examples/rtt-pwm.rs
new file mode 100644
index 0000000..b430315
--- /dev/null
+++ b/examples/rtt-pwm.rs
@@ -0,0 +1,53 @@
+//! examples/rtt_timing.rs
+//! cargo run --examples rtt-pwm
+
+#![deny(unsafe_code)]
+// #![deny(warnings)]
+#![no_main]
+#![no_std]
+
+use cortex_m::{asm, peripheral::DWT};
+use panic_halt as _;
+use rtt_target::{rprintln, rtt_init_print};
+use stm32f4xx_hal::{dma, gpio::Speed, prelude::*, pwm, stm32};
+
+#[rtic::app(device = stm32f4xx_hal::stm32, peripherals = true)]
+const APP: () = {
+    #[init]
+    fn init(mut cx: init::Context) {
+        rtt_init_print!();
+        rprintln!("init");
+        let dp = cx.device;
+
+        // Initialize (enable) the monotonic timer (CYCCNT)
+        cx.core.DCB.enable_trace();
+        cx.core.DWT.enable_cycle_counter();
+
+        // Set up the system clock. 16 MHz?
+        let rcc = dp.RCC.constrain();
+        let clocks = rcc.cfgr.freeze();
+
+        let gpioa = dp.GPIOA.split();
+        let channels = (
+            gpioa.pa8.into_alternate_af1().set_speed(Speed::Low),
+            gpioa.pa9.into_alternate_af1(),
+        );
+
+        let pwm = pwm::tim1(dp.TIM1, channels, clocks, 1u32.khz());
+        let (mut ch1, mut ch2) = pwm;
+        let max_duty = ch1.get_max_duty();
+        rprintln!("max_duty {}", max_duty);
+        ch1.set_duty(max_duty / 2);
+        ch1.enable();
+        ch2.set_duty((max_duty * 1) / 2);
+        ch2.enable();
+    }
+
+    #[idle]
+    fn idle(_cx: idle::Context) -> ! {
+        rprintln!("idle");
+        loop {
+            continue;
+        }
+    }
+};
diff --git a/examples/rtt-timing.rs b/examples/rtt-timing.rs
index 2f601f7..720e799 100644
--- a/examples/rtt-timing.rs
+++ b/examples/rtt-timing.rs
@@ -1,4 +1,5 @@
 //! examples/rtt_timing.rs
+//! cargo run --examples rtt-timing
 
 #![deny(unsafe_code)]
 #![deny(warnings)]
diff --git a/openocd.cfg b/openocd.cfg
index 1c082fe..577828f 100644
--- a/openocd.cfg
+++ b/openocd.cfg
@@ -1,4 +1,4 @@
-# Sample OpenOCD configuration for the STM32F3DISCOVERY development board
+# Sample OpenOCD configuration for the STM Nucleo-F401RE/F411RE development board
 
 # Depending on the hardware revision you got you'll have to pick ONE of these
 # interfaces. At any time only one interface should be commented out.
diff --git a/openocd.gdb b/openocd.gdb
index 918dff9..1623a86 100644
--- a/openocd.gdb
+++ b/openocd.gdb
@@ -32,7 +32,7 @@ monitor tpiu config internal itm.txt uart off 16000000
 # monitor tpiu config external uart off 8000000 2000000
 
 # # enable ITM port 0
-# monitor itm port 0 on
+monitor itm port 0 on
 
 load
 
-- 
GitLab