aboutsummaryrefslogtreecommitdiff
path: root/examples/stm32f4/src
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2024-05-31 18:09:27 +0000
committerGitHub <[email protected]>2024-05-31 18:09:27 +0000
commitba940017ee8cabbb2a371243adc040b3460cd71c (patch)
treecbf45009f8e8f2da41c9813c9367f9c6ba761e29 /examples/stm32f4/src
parent4452a06e76707e41195ccc0cd82848af871c182c (diff)
parent7d869192571c0e49da425afbdc37433b8a820786 (diff)
Merge pull request #3014 from brunob45/pwm_input
Add PWM Input for STM32
Diffstat (limited to 'examples/stm32f4/src')
-rw-r--r--examples/stm32f4/src/bin/pwm_input.rs54
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
4use defmt::*;
5use embassy_executor::Spawner;
6use embassy_stm32::gpio::{Level, Output, Pull, Speed};
7use embassy_stm32::time::khz;
8use embassy_stm32::timer::pwm_input::PwmInput;
9use embassy_stm32::{bind_interrupts, peripherals, timer};
10use embassy_time::Timer;
11use {defmt_rtt as _, panic_probe as _};
12
13/// Connect PB2 and PA6 with a 1k Ohm resistor
14
15#[embassy_executor::task]
16async 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
30bind_interrupts!(struct Irqs {
31 TIM2 => timer::CaptureCompareInterruptHandler<peripherals::TIM2>;
32});
33
34#[embassy_executor::main]
35async 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}