diff options
| author | Eicke Hecht <[email protected]> | 2025-11-23 12:29:56 +0100 |
|---|---|---|
| committer | Eicke Hecht <[email protected]> | 2025-11-23 12:29:56 +0100 |
| commit | 36f4075b9054576ccf6dba2dedb08c62484a0599 (patch) | |
| tree | bdc5e19df803ace7073a9d212327ae64fedb704e /examples | |
| parent | ac1f0ec4ecc6450de7d1c6044e799bca8791f7e5 (diff) | |
fix: Fix waveform for channels > 1 and disallow for unsupported dmas
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32f7/src/bin/pwm.rs | 65 |
1 files changed, 65 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..872d99859 --- /dev/null +++ b/examples/stm32f7/src/bin/pwm.rs | |||
| @@ -0,0 +1,65 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::{panic, *}; | ||
| 5 | use embassy_executor::Spawner; | ||
| 6 | use embassy_futures::join::join; | ||
| 7 | use embassy_stm32::time::Hertz; | ||
| 8 | use embassy_stm32::{Config, peripherals}; | ||
| 9 | use embassy_stm32::gpio::OutputType; | ||
| 10 | use embassy_stm32::time::mhz; | ||
| 11 | use embassy_stm32::timer::simple_pwm::{PwmPin, SimplePwm}; | ||
| 12 | use embassy_time::Timer; | ||
| 13 | use {defmt_rtt as _, panic_probe as _}; | ||
| 14 | |||
| 15 | // If you are trying this and your USB device doesn't connect, the most | ||
| 16 | // common issues are the RCC config and vbus_detection | ||
| 17 | // | ||
| 18 | // See https://embassy.dev/book/#_the_usb_examples_are_not_working_on_my_board_is_there_anything_else_i_need_to_configure | ||
| 19 | // for more information. | ||
| 20 | #[embassy_executor::main] | ||
| 21 | async fn main(_spawner: Spawner) { | ||
| 22 | info!("Hello World!"); | ||
| 23 | |||
| 24 | let mut config = Config::default(); | ||
| 25 | { | ||
| 26 | use embassy_stm32::rcc::*; | ||
| 27 | config.rcc.hse = Some(Hse { | ||
| 28 | freq: Hertz(8_000_000), | ||
| 29 | mode: HseMode::Bypass, | ||
| 30 | }); | ||
| 31 | config.rcc.pll_src = PllSource::HSE; | ||
| 32 | config.rcc.pll = Some(Pll { | ||
| 33 | prediv: PllPreDiv::DIV4, | ||
| 34 | mul: PllMul::MUL200, | ||
| 35 | divp: Some(PllPDiv::DIV2), // 8mhz / 4 * 200 / 2 = 200Mhz | ||
| 36 | divq: Some(PllQDiv::DIV4), // 8mhz / 4 * 200 / 4 = 100Mhz | ||
| 37 | divr: None, | ||
| 38 | }); | ||
| 39 | config.rcc.ahb_pre = AHBPrescaler::DIV1; | ||
| 40 | config.rcc.apb1_pre = APBPrescaler::DIV4; | ||
| 41 | config.rcc.apb2_pre = APBPrescaler::DIV2; | ||
| 42 | config.rcc.sys = Sysclk::PLL1_P; | ||
| 43 | } | ||
| 44 | let p = embassy_stm32::init(config); | ||
| 45 | let ch1_pin = PwmPin::new(p.PE9, OutputType::PushPull); | ||
| 46 | let ch2_pin = PwmPin::new(p.PE11, OutputType::PushPull); | ||
| 47 | let mut pwm = SimplePwm::new(p.TIM1, Some(ch1_pin), Some(ch2_pin), None, None, mhz(1), Default::default()); | ||
| 48 | let mut ch1 = pwm.ch1(); | ||
| 49 | ch1.enable(); | ||
| 50 | info!("PWM initialized"); | ||
| 51 | info!("PWM max duty {}", ch1.max_duty_cycle()); | ||
| 52 | |||
| 53 | info!("PWM duty on channel 1 (D6) 50%"); | ||
| 54 | ch1.set_duty_cycle_fraction(1, 2); | ||
| 55 | info!("PWM waveform on channel 2 (D5)"); | ||
| 56 | const max_duty: usize = 200; | ||
| 57 | let mut duty = [0u16;max_duty]; | ||
| 58 | for i in 0..max_duty { | ||
| 59 | duty[i] = i as u16; | ||
| 60 | } | ||
| 61 | pwm.waveform_continuous::<embassy_stm32::timer::Ch2>(p.DMA2_CH6, &duty).await; | ||
| 62 | |||
| 63 | |||
| 64 | } | ||
| 65 | |||
