diff options
| author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-11-27 02:07:43 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-11-27 02:07:43 +0000 |
| commit | 543cc65e569d24d07c90e97752b4cfc995d33dc0 (patch) | |
| tree | 34615657d5507b9d043f86cb48048e22cb69c432 /examples | |
| parent | 793f4b1f7d6d1452d665fc7ec70bddd3ac7a69b9 (diff) | |
| parent | 006e567716362acfde90afcfd8a25be1fe0d6897 (diff) | |
Merge #449
449: STM32: Add PWM support r=Dirbaio a=bgamari
Here is a first-cut at implementing PWM support for STM32 targets via the TIM peripherals. Currently this only contains pin configuration for the STM32G0 but it would be straightforward to extend to other platforms.
Co-authored-by: Ben Gamari <[email protected]>
Co-authored-by: Dario Nieuwenhuis <[email protected]>
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32g4/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/stm32g4/src/bin/pwm.rs | 36 |
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] |
| 9 | embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt"] } | 9 | embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt"] } |
| 10 | embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] } | 10 | embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] } |
| 11 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "time-driver-tim2", "stm32g491re", "memory-x", "unstable-pac"] } | 11 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "time-driver-tim3", "stm32g491re", "memory-x", "unstable-pac"] } |
| 12 | embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" } | 12 | embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" } |
| 13 | 13 | ||
| 14 | defmt = "0.3" | 14 | defmt = "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"] | ||
| 6 | mod example_common; | ||
| 7 | use embassy::executor::Spawner; | ||
| 8 | use embassy::time::{Duration, Timer}; | ||
| 9 | use embassy_stm32::gpio::NoPin; | ||
| 10 | use embassy_stm32::pwm::{Channel, Pwm}; | ||
| 11 | use embassy_stm32::time::U32Ext; | ||
| 12 | use embassy_stm32::Peripherals; | ||
| 13 | use example_common::*; | ||
| 14 | |||
| 15 | #[embassy::main] | ||
| 16 | async 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 | } | ||
