aboutsummaryrefslogtreecommitdiff
path: root/examples/stm32g0
diff options
context:
space:
mode:
authorChen Yuheng <[email protected]>2024-06-19 10:57:18 +0800
committerChen Yuheng <[email protected]>2024-06-19 10:57:18 +0800
commit0579d248efc1ee78f039fbfabcf2e0272e32ee53 (patch)
treefe6dca408b8a015d0eef4a473c1213193f22f0a4 /examples/stm32g0
parentb0172bb58217d625a13fed8122827b8d0b03c46a (diff)
Add PWM examples for stm32g0
Diffstat (limited to 'examples/stm32g0')
-rw-r--r--examples/stm32g0/src/bin/input_capture.rs67
-rw-r--r--examples/stm32g0/src/bin/pwm_complementary.rs58
-rw-r--r--examples/stm32g0/src/bin/pwm_input.rs65
3 files changed, 190 insertions, 0 deletions
diff --git a/examples/stm32g0/src/bin/input_capture.rs b/examples/stm32g0/src/bin/input_capture.rs
new file mode 100644
index 000000000..69fdae96d
--- /dev/null
+++ b/examples/stm32g0/src/bin/input_capture.rs
@@ -0,0 +1,67 @@
1//! Input capture example
2//!
3//! This example showcases how to use the input capture feature of the timer peripheral.
4//! Connect PB1 and PA6 with a 1k Ohm resistor or Connect PB1 and PA8 with a 1k Ohm resistor
5//! to see the output.
6//! When connecting PB1 (software pwm) and PA6 the output is around 10000 (it will be a bit bigger, around 10040)
7//! Output is 1000 when connecting PB1 (PWMOUT) and PA6.
8//!
9#![no_std]
10#![no_main]
11
12use defmt::*;
13use embassy_executor::Spawner;
14use embassy_stm32::gpio::{Level, Output, OutputType, Pull, Speed};
15use embassy_stm32::time::khz;
16use embassy_stm32::timer::input_capture::{CapturePin, InputCapture};
17use embassy_stm32::timer::simple_pwm::{PwmPin, SimplePwm};
18use embassy_stm32::timer::Channel;
19use embassy_stm32::{bind_interrupts, peripherals, timer};
20use embassy_time::Timer;
21use {defmt_rtt as _, panic_probe as _};
22
23// Connect PB1 and PA6 with a 1k Ohm resistor
24
25#[embassy_executor::task]
26async fn blinky(led: peripherals::PB1) {
27 let mut led = Output::new(led, Level::High, Speed::Low);
28
29 loop {
30 led.set_high();
31 Timer::after_millis(50).await;
32
33 led.set_low();
34 Timer::after_millis(50).await;
35 }
36}
37
38bind_interrupts!(struct Irqs {
39 TIM2 => timer::CaptureCompareInterruptHandler<peripherals::TIM2>;
40});
41
42#[embassy_executor::main]
43async fn main(spawner: Spawner) {
44 let p = embassy_stm32::init(Default::default());
45 info!("Hello World!");
46
47 unwrap!(spawner.spawn(blinky(p.PB1)));
48
49 // Connect PB1 and PA8 with a 1k Ohm resistor
50 let ch1 = PwmPin::new_ch1(p.PA8, OutputType::PushPull);
51 let mut pwm = SimplePwm::new(p.TIM1, Some(ch1), None, None, None, khz(1), Default::default());
52 pwm.enable(Channel::Ch1);
53 pwm.set_duty(Channel::Ch1, 50);
54
55 let ch1 = CapturePin::new_ch1(p.PA0, Pull::None);
56 let mut ic = InputCapture::new(p.TIM2, Some(ch1), None, None, None, Irqs, khz(1000), Default::default());
57
58 let mut old_capture = 0;
59
60 loop {
61 ic.wait_for_rising_edge(Channel::Ch1).await;
62
63 let capture_value = ic.get_capture_value(Channel::Ch1);
64 info!("{}", capture_value - old_capture);
65 old_capture = capture_value;
66 }
67}
diff --git a/examples/stm32g0/src/bin/pwm_complementary.rs b/examples/stm32g0/src/bin/pwm_complementary.rs
new file mode 100644
index 000000000..98305ae42
--- /dev/null
+++ b/examples/stm32g0/src/bin/pwm_complementary.rs
@@ -0,0 +1,58 @@
1//! PWM complementary example
2//!
3//! This example uses two complementary pwm outputs from TIM1 with different duty cycles
4//! ___ ___
5//! |_________| |_________| PA8
6//! _________ _________
7//! ___| |___| | PA7
8//! _________ _________
9//! |___| |___| PB3
10//! ___ ___
11//! _________| |_________| | PB0
12
13#![no_std]
14#![no_main]
15
16use defmt::info;
17use embassy_executor::Spawner;
18use embassy_stm32::gpio::OutputType;
19use embassy_stm32::time::khz;
20use embassy_stm32::timer::complementary_pwm::{ComplementaryPwm, ComplementaryPwmPin};
21use embassy_stm32::timer::simple_pwm::PwmPin;
22use embassy_stm32::timer::Channel;
23use embassy_stm32::Config as PeripheralConfig;
24use {defmt_rtt as _, panic_probe as _};
25
26#[embassy_executor::main]
27async fn main(_spawner: Spawner) {
28 let p = embassy_stm32::init(Default::default());
29
30 let ch1 = PwmPin::new_ch1(p.PA8, OutputType::PushPull);
31 let ch1n = ComplementaryPwmPin::new_ch1(p.PA7, OutputType::PushPull);
32 let ch2 = PwmPin::new_ch2(p.PB3, OutputType::PushPull);
33 let ch2n = ComplementaryPwmPin::new_ch2(p.PB0, OutputType::PushPull);
34
35 let mut pwm = ComplementaryPwm::new(
36 p.TIM1,
37 Some(ch1),
38 Some(ch1n),
39 Some(ch2),
40 Some(ch2n),
41 None,
42 None,
43 None,
44 None,
45 khz(100),
46 Default::default(),
47 );
48
49 let max = pwm.get_max_duty();
50 info!("Max duty: {}", max);
51
52 pwm.set_duty(Channel::Ch1, max / 4);
53 pwm.enable(Channel::Ch1);
54 pwm.set_duty(Channel::Ch2, max * 3 / 4);
55 pwm.enable(Channel::Ch2);
56
57 loop {}
58}
diff --git a/examples/stm32g0/src/bin/pwm_input.rs b/examples/stm32g0/src/bin/pwm_input.rs
new file mode 100644
index 000000000..152ecda86
--- /dev/null
+++ b/examples/stm32g0/src/bin/pwm_input.rs
@@ -0,0 +1,65 @@
1//! PWM input example
2//!
3//! This program demonstrates how to capture the parameters of the input waveform (frequency, width and duty cycle)
4//! Connect PB1 and PA6 with a 1k Ohm resistor or Connect PB1 and PA8 with a 1k Ohm resistor
5//! to see the output.
6//!
7
8#![no_std]
9#![no_main]
10
11use defmt::*;
12use embassy_executor::Spawner;
13use embassy_stm32::gpio::{Level, Output, OutputType, Pull, Speed};
14use embassy_stm32::time::khz;
15use embassy_stm32::timer::pwm_input::PwmInput;
16use embassy_stm32::timer::simple_pwm::{PwmPin, SimplePwm};
17use embassy_stm32::timer::Channel;
18use embassy_stm32::{bind_interrupts, peripherals, timer};
19use embassy_time::Timer;
20use {defmt_rtt as _, panic_probe as _};
21
22// Connect PB1 and PA6 with a 1k Ohm resistor
23#[embassy_executor::task]
24async fn blinky(led: peripherals::PB1) {
25 let mut led = Output::new(led, Level::High, Speed::Low);
26
27 loop {
28 led.set_high();
29 Timer::after_millis(50).await;
30
31 led.set_low();
32 Timer::after_millis(50).await;
33 }
34}
35
36bind_interrupts!(struct Irqs {
37 TIM2 => timer::CaptureCompareInterruptHandler<peripherals::TIM2>;
38});
39
40#[embassy_executor::main]
41async fn main(spawner: Spawner) {
42 let p = embassy_stm32::init(Default::default());
43
44 unwrap!(spawner.spawn(blinky(p.PB1)));
45 // Connect PA8 and PA6 with a 1k Ohm resistor
46 let ch1 = PwmPin::new_ch1(p.PA8, OutputType::PushPull);
47 let mut pwm = SimplePwm::new(p.TIM1, Some(ch1), None, None, None, khz(1), Default::default());
48 let max = pwm.get_max_duty();
49 pwm.set_duty(Channel::Ch1, max / 4);
50 pwm.enable(Channel::Ch1);
51
52 let mut pwm_input = PwmInput::new(p.TIM2, p.PA0, Pull::None, khz(1000));
53 pwm_input.enable();
54
55 loop {
56 Timer::after_millis(500).await;
57 let period = pwm_input.get_period_ticks();
58 let width = pwm_input.get_width_ticks();
59 let duty_cycle = pwm_input.get_duty_cycle();
60 info!(
61 "period ticks: {} width ticks: {} duty cycle: {}",
62 period, width, duty_cycle
63 );
64 }
65}