aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2021-11-27 03:05:26 +0100
committerDario Nieuwenhuis <[email protected]>2021-11-27 03:06:46 +0100
commite40555e245b9a0c351f2cf250231453a0ec78a3d (patch)
tree9eee5c71da1c8fe92c72f14548a9d5ac42ec39df
parentd7d12584118e7c460c441b8b12836680c8eecba3 (diff)
examples/stm32g4: add pwm example
-rw-r--r--examples/stm32g4/Cargo.toml2
-rw-r--r--examples/stm32g4/src/bin/pwm.rs36
2 files changed, 37 insertions, 1 deletions
diff --git a/examples/stm32g4/Cargo.toml b/examples/stm32g4/Cargo.toml
index 0f9d77f5e..f4378309a 100644
--- a/examples/stm32g4/Cargo.toml
+++ b/examples/stm32g4/Cargo.toml
@@ -8,7 +8,7 @@ resolver = "2"
8[dependencies] 8[dependencies]
9embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt"] } 9embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt"] }
10embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] } 10embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] }
11embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "time-driver-tim2", "stm32g491re", "memory-x", "unstable-pac"] } 11embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "time-driver-tim3", "stm32g491re", "memory-x", "unstable-pac"] }
12embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" } 12embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" }
13 13
14defmt = "0.3" 14defmt = "0.3"
diff --git a/examples/stm32g4/src/bin/pwm.rs b/examples/stm32g4/src/bin/pwm.rs
new file mode 100644
index 000000000..1aa7b85f3
--- /dev/null
+++ b/examples/stm32g4/src/bin/pwm.rs
@@ -0,0 +1,36 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5#[path = "../example_common.rs"]
6mod example_common;
7use embassy::executor::Spawner;
8use embassy::time::{Duration, Timer};
9use embassy_stm32::gpio::NoPin;
10use embassy_stm32::pwm::{Channel, Pwm};
11use embassy_stm32::time::U32Ext;
12use embassy_stm32::Peripherals;
13use example_common::*;
14
15#[embassy::main]
16async fn main(_spawner: Spawner, p: Peripherals) {
17 info!("Hello World!");
18
19 let mut pwm = Pwm::new(p.TIM2, p.PA5, NoPin, NoPin, NoPin, 10000.hz());
20 let max = pwm.get_max_duty();
21 pwm.enable(Channel::Ch1);
22
23 info!("PWM initialized");
24 info!("PWM max duty {}", max);
25
26 loop {
27 pwm.set_duty(Channel::Ch1, 0);
28 Timer::after(Duration::from_millis(300)).await;
29 pwm.set_duty(Channel::Ch1, max / 4);
30 Timer::after(Duration::from_millis(300)).await;
31 pwm.set_duty(Channel::Ch1, max / 2);
32 Timer::after(Duration::from_millis(300)).await;
33 pwm.set_duty(Channel::Ch1, max - 1);
34 Timer::after(Duration::from_millis(300)).await;
35 }
36}