aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/stm32f1/src/bin/input_capture.rs52
-rw-r--r--examples/stm32f1/src/bin/pwm_input.rs54
-rw-r--r--examples/stm32f4/src/bin/pwm_input.rs54
3 files changed, 160 insertions, 0 deletions
diff --git a/examples/stm32f1/src/bin/input_capture.rs b/examples/stm32f1/src/bin/input_capture.rs
new file mode 100644
index 000000000..5e2dab9e6
--- /dev/null
+++ b/examples/stm32f1/src/bin/input_capture.rs
@@ -0,0 +1,52 @@
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::input_capture::{CapturePin, InputCapture};
9use embassy_stm32::timer::{self, Channel};
10use embassy_stm32::{bind_interrupts, peripherals};
11use embassy_time::Timer;
12use {defmt_rtt as _, panic_probe as _};
13
14/// Connect PA2 and PC13 with a 1k Ohm resistor
15
16#[embassy_executor::task]
17async fn blinky(led: peripherals::PC13) {
18 let mut led = Output::new(led, Level::High, Speed::Low);
19
20 loop {
21 info!("high");
22 led.set_high();
23 Timer::after_millis(300).await;
24
25 info!("low");
26 led.set_low();
27 Timer::after_millis(300).await;
28 }
29}
30
31bind_interrupts!(struct Irqs {
32 TIM2 => timer::CaptureCompareInterruptHandler<peripherals::TIM2>;
33});
34
35#[embassy_executor::main]
36async fn main(spawner: Spawner) {
37 let p = embassy_stm32::init(Default::default());
38 info!("Hello World!");
39
40 unwrap!(spawner.spawn(blinky(p.PC13)));
41
42 let ch3 = CapturePin::new_ch3(p.PA2, Pull::None);
43 let mut ic = InputCapture::new(p.TIM2, None, None, Some(ch3), None, Irqs, khz(1000), Default::default());
44
45 loop {
46 info!("wait for rising edge");
47 ic.wait_for_rising_edge(Channel::Ch3).await;
48
49 let capture_value = ic.get_capture_value(Channel::Ch3);
50 info!("new capture! {}", capture_value);
51 }
52}
diff --git a/examples/stm32f1/src/bin/pwm_input.rs b/examples/stm32f1/src/bin/pwm_input.rs
new file mode 100644
index 000000000..f74853d4e
--- /dev/null
+++ b/examples/stm32f1/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 PA0 and PC13 with a 1k Ohm resistor
14
15#[embassy_executor::task]
16async fn blinky(led: peripherals::PC13) {
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.PC13)));
40
41 let mut pwm_input = PwmInput::new(p.TIM2, p.PA0, 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}
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}