diff options
| author | Ulf Lilleengen <[email protected]> | 2024-06-22 14:11:50 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-06-22 14:11:50 +0000 |
| commit | 8b0c883443e975e6c43a964ed031da056f8b37ea (patch) | |
| tree | dc38c273bbd60175427e90203afb0ff95197373b | |
| parent | f6bd4a3f3736b73ecbf6e814b157414091980194 (diff) | |
| parent | a3c6626f4058c6cde6c27dd559d0b4d2b7d746ac (diff) | |
Merge pull request #3095 from Adancurusul/g0_development
Add PWM examples for stm32g0
| -rw-r--r-- | examples/stm32g0/src/bin/input_capture.rs | 67 | ||||
| -rw-r--r-- | examples/stm32g0/src/bin/pwm_complementary.rs | 57 | ||||
| -rw-r--r-- | examples/stm32g0/src/bin/pwm_input.rs | 65 |
3 files changed, 189 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 | |||
| 12 | use defmt::*; | ||
| 13 | use embassy_executor::Spawner; | ||
| 14 | use embassy_stm32::gpio::{Level, Output, OutputType, Pull, Speed}; | ||
| 15 | use embassy_stm32::time::khz; | ||
| 16 | use embassy_stm32::timer::input_capture::{CapturePin, InputCapture}; | ||
| 17 | use embassy_stm32::timer::simple_pwm::{PwmPin, SimplePwm}; | ||
| 18 | use embassy_stm32::timer::Channel; | ||
| 19 | use embassy_stm32::{bind_interrupts, peripherals, timer}; | ||
| 20 | use embassy_time::Timer; | ||
| 21 | use {defmt_rtt as _, panic_probe as _}; | ||
| 22 | |||
| 23 | // Connect PB1 and PA6 with a 1k Ohm resistor | ||
| 24 | |||
| 25 | #[embassy_executor::task] | ||
| 26 | async 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 | |||
| 38 | bind_interrupts!(struct Irqs { | ||
| 39 | TIM2 => timer::CaptureCompareInterruptHandler<peripherals::TIM2>; | ||
| 40 | }); | ||
| 41 | |||
| 42 | #[embassy_executor::main] | ||
| 43 | async 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..97b163c40 --- /dev/null +++ b/examples/stm32g0/src/bin/pwm_complementary.rs | |||
| @@ -0,0 +1,57 @@ | |||
| 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 | |||
| 16 | use defmt::info; | ||
| 17 | use embassy_executor::Spawner; | ||
| 18 | use embassy_stm32::gpio::OutputType; | ||
| 19 | use embassy_stm32::time::khz; | ||
| 20 | use embassy_stm32::timer::complementary_pwm::{ComplementaryPwm, ComplementaryPwmPin}; | ||
| 21 | use embassy_stm32::timer::simple_pwm::PwmPin; | ||
| 22 | use embassy_stm32::timer::Channel; | ||
| 23 | use {defmt_rtt as _, panic_probe as _}; | ||
| 24 | |||
| 25 | #[embassy_executor::main] | ||
| 26 | async fn main(_spawner: Spawner) { | ||
| 27 | let p = embassy_stm32::init(Default::default()); | ||
| 28 | |||
| 29 | let ch1 = PwmPin::new_ch1(p.PA8, OutputType::PushPull); | ||
| 30 | let ch1n = ComplementaryPwmPin::new_ch1(p.PA7, OutputType::PushPull); | ||
| 31 | let ch2 = PwmPin::new_ch2(p.PB3, OutputType::PushPull); | ||
| 32 | let ch2n = ComplementaryPwmPin::new_ch2(p.PB0, OutputType::PushPull); | ||
| 33 | |||
| 34 | let mut pwm = ComplementaryPwm::new( | ||
| 35 | p.TIM1, | ||
| 36 | Some(ch1), | ||
| 37 | Some(ch1n), | ||
| 38 | Some(ch2), | ||
| 39 | Some(ch2n), | ||
| 40 | None, | ||
| 41 | None, | ||
| 42 | None, | ||
| 43 | None, | ||
| 44 | khz(100), | ||
| 45 | Default::default(), | ||
| 46 | ); | ||
| 47 | |||
| 48 | let max = pwm.get_max_duty(); | ||
| 49 | info!("Max duty: {}", max); | ||
| 50 | |||
| 51 | pwm.set_duty(Channel::Ch1, max / 4); | ||
| 52 | pwm.enable(Channel::Ch1); | ||
| 53 | pwm.set_duty(Channel::Ch2, max * 3 / 4); | ||
| 54 | pwm.enable(Channel::Ch2); | ||
| 55 | |||
| 56 | loop {} | ||
| 57 | } | ||
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 | |||
| 11 | use defmt::*; | ||
| 12 | use embassy_executor::Spawner; | ||
| 13 | use embassy_stm32::gpio::{Level, Output, OutputType, Pull, Speed}; | ||
| 14 | use embassy_stm32::time::khz; | ||
| 15 | use embassy_stm32::timer::pwm_input::PwmInput; | ||
| 16 | use embassy_stm32::timer::simple_pwm::{PwmPin, SimplePwm}; | ||
| 17 | use embassy_stm32::timer::Channel; | ||
| 18 | use embassy_stm32::{bind_interrupts, peripherals, timer}; | ||
| 19 | use embassy_time::Timer; | ||
| 20 | use {defmt_rtt as _, panic_probe as _}; | ||
| 21 | |||
| 22 | // Connect PB1 and PA6 with a 1k Ohm resistor | ||
| 23 | #[embassy_executor::task] | ||
| 24 | async 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 | |||
| 36 | bind_interrupts!(struct Irqs { | ||
| 37 | TIM2 => timer::CaptureCompareInterruptHandler<peripherals::TIM2>; | ||
| 38 | }); | ||
| 39 | |||
| 40 | #[embassy_executor::main] | ||
| 41 | async 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 | } | ||
