diff options
| author | Dario Nieuwenhuis <[email protected]> | 2024-05-31 18:09:27 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-05-31 18:09:27 +0000 |
| commit | ba940017ee8cabbb2a371243adc040b3460cd71c (patch) | |
| tree | cbf45009f8e8f2da41c9813c9367f9c6ba761e29 /examples/stm32f4 | |
| parent | 4452a06e76707e41195ccc0cd82848af871c182c (diff) | |
| parent | 7d869192571c0e49da425afbdc37433b8a820786 (diff) | |
Merge pull request #3014 from brunob45/pwm_input
Add PWM Input for STM32
Diffstat (limited to 'examples/stm32f4')
| -rw-r--r-- | examples/stm32f4/src/bin/pwm_input.rs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/examples/stm32f4/src/bin/pwm_input.rs b/examples/stm32f4/src/bin/pwm_input.rs new file mode 100644 index 000000000..ce200549d --- /dev/null +++ b/examples/stm32f4/src/bin/pwm_input.rs | |||
| @@ -0,0 +1,54 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::*; | ||
| 5 | use embassy_executor::Spawner; | ||
| 6 | use embassy_stm32::gpio::{Level, Output, Pull, Speed}; | ||
| 7 | use embassy_stm32::time::khz; | ||
| 8 | use embassy_stm32::timer::pwm_input::PwmInput; | ||
| 9 | use embassy_stm32::{bind_interrupts, peripherals, timer}; | ||
| 10 | use embassy_time::Timer; | ||
| 11 | use {defmt_rtt as _, panic_probe as _}; | ||
| 12 | |||
| 13 | /// Connect PB2 and PA6 with a 1k Ohm resistor | ||
| 14 | |||
| 15 | #[embassy_executor::task] | ||
| 16 | async fn blinky(led: peripherals::PB2) { | ||
| 17 | let mut led = Output::new(led, Level::High, Speed::Low); | ||
| 18 | |||
| 19 | loop { | ||
| 20 | info!("high"); | ||
| 21 | led.set_high(); | ||
| 22 | Timer::after_millis(300).await; | ||
| 23 | |||
| 24 | info!("low"); | ||
| 25 | led.set_low(); | ||
| 26 | Timer::after_millis(300).await; | ||
| 27 | } | ||
| 28 | } | ||
| 29 | |||
| 30 | bind_interrupts!(struct Irqs { | ||
| 31 | TIM2 => timer::CaptureCompareInterruptHandler<peripherals::TIM2>; | ||
| 32 | }); | ||
| 33 | |||
| 34 | #[embassy_executor::main] | ||
| 35 | async fn main(spawner: Spawner) { | ||
| 36 | let p = embassy_stm32::init(Default::default()); | ||
| 37 | info!("Hello World!"); | ||
| 38 | |||
| 39 | unwrap!(spawner.spawn(blinky(p.PB2))); | ||
| 40 | |||
| 41 | let mut pwm_input = PwmInput::new(p.TIM3, p.PA6, Pull::None, khz(10)); | ||
| 42 | pwm_input.enable(); | ||
| 43 | |||
| 44 | loop { | ||
| 45 | Timer::after_millis(500).await; | ||
| 46 | let period = pwm_input.get_period_ticks(); | ||
| 47 | let width = pwm_input.get_width_ticks(); | ||
| 48 | let duty_cycle = pwm_input.get_duty_cycle(); | ||
| 49 | info!( | ||
| 50 | "period ticks: {} width ticks: {} duty cycle: {}", | ||
| 51 | period, width, duty_cycle | ||
| 52 | ); | ||
| 53 | } | ||
| 54 | } | ||
