diff options
Diffstat (limited to 'examples/stm32f7/src/bin/pwm.rs')
| -rw-r--r-- | examples/stm32f7/src/bin/pwm.rs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/examples/stm32f7/src/bin/pwm.rs b/examples/stm32f7/src/bin/pwm.rs new file mode 100644 index 000000000..b071eb597 --- /dev/null +++ b/examples/stm32f7/src/bin/pwm.rs | |||
| @@ -0,0 +1,61 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::*; | ||
| 5 | use embassy_executor::Spawner; | ||
| 6 | use embassy_stm32::Config; | ||
| 7 | use embassy_stm32::gpio::OutputType; | ||
| 8 | use embassy_stm32::time::{Hertz, mhz}; | ||
| 9 | use embassy_stm32::timer::simple_pwm::{PwmPin, SimplePwm}; | ||
| 10 | use embassy_time::Timer; | ||
| 11 | use {defmt_rtt as _, panic_probe as _}; | ||
| 12 | |||
| 13 | // If you are trying this and your USB device doesn't connect, the most | ||
| 14 | // common issues are the RCC config and vbus_detection | ||
| 15 | // | ||
| 16 | // See https://embassy.dev/book/#_the_usb_examples_are_not_working_on_my_board_is_there_anything_else_i_need_to_configure | ||
| 17 | // for more information. | ||
| 18 | #[embassy_executor::main] | ||
| 19 | async fn main(_spawner: Spawner) { | ||
| 20 | info!("Hello World!"); | ||
| 21 | |||
| 22 | let mut config = Config::default(); | ||
| 23 | { | ||
| 24 | use embassy_stm32::rcc::*; | ||
| 25 | config.rcc.hse = Some(Hse { | ||
| 26 | freq: Hertz(8_000_000), | ||
| 27 | mode: HseMode::Bypass, | ||
| 28 | }); | ||
| 29 | config.rcc.pll_src = PllSource::HSE; | ||
| 30 | config.rcc.pll = Some(Pll { | ||
| 31 | prediv: PllPreDiv::DIV4, | ||
| 32 | mul: PllMul::MUL200, | ||
| 33 | divp: Some(PllPDiv::DIV2), // 8mhz / 4 * 200 / 2 = 200Mhz | ||
| 34 | divq: Some(PllQDiv::DIV4), // 8mhz / 4 * 200 / 4 = 100Mhz | ||
| 35 | divr: None, | ||
| 36 | }); | ||
| 37 | config.rcc.ahb_pre = AHBPrescaler::DIV1; | ||
| 38 | config.rcc.apb1_pre = APBPrescaler::DIV4; | ||
| 39 | config.rcc.apb2_pre = APBPrescaler::DIV2; | ||
| 40 | config.rcc.sys = Sysclk::PLL1_P; | ||
| 41 | } | ||
| 42 | let p = embassy_stm32::init(config); | ||
| 43 | let ch1_pin = PwmPin::new(p.PE9, OutputType::PushPull); | ||
| 44 | let mut pwm = SimplePwm::new(p.TIM1, Some(ch1_pin), None, None, None, mhz(1), Default::default()); | ||
| 45 | let mut ch1 = pwm.ch1(); | ||
| 46 | ch1.enable(); | ||
| 47 | |||
| 48 | info!("PWM initialized"); | ||
| 49 | info!("PWM max duty {}", ch1.max_duty_cycle()); | ||
| 50 | |||
| 51 | loop { | ||
| 52 | ch1.set_duty_cycle_fully_off(); | ||
| 53 | Timer::after_millis(300).await; | ||
| 54 | ch1.set_duty_cycle_fraction(1, 4); | ||
| 55 | Timer::after_millis(300).await; | ||
| 56 | ch1.set_duty_cycle_fraction(1, 2); | ||
| 57 | Timer::after_millis(300).await; | ||
| 58 | ch1.set_duty_cycle(ch1.max_duty_cycle() - 1); | ||
| 59 | Timer::after_millis(300).await; | ||
| 60 | } | ||
| 61 | } | ||
