improve examples

This commit is contained in:
Bruno Bousquet 2024-06-02 17:07:28 -04:00
parent 66a1d101c3
commit 3642843a3c
4 changed files with 17 additions and 23 deletions

View File

@ -3,7 +3,7 @@
use defmt::*;
use embassy_executor::Spawner;
use embassy_stm32::gpio::{Level, Output, Pull, Speed};
use embassy_stm32::gpio::{AnyPin, Level, Output, Pin, Pull, Speed};
use embassy_stm32::time::khz;
use embassy_stm32::timer::input_capture::{CapturePin, InputCapture};
use embassy_stm32::timer::{self, Channel};
@ -14,7 +14,7 @@ use {defmt_rtt as _, panic_probe as _};
/// Connect PA2 and PC13 with a 1k Ohm resistor
#[embassy_executor::task]
async fn blinky(led: peripherals::PC13) {
async fn blinky(led: AnyPin) {
let mut led = Output::new(led, Level::High, Speed::Low);
loop {
@ -37,16 +37,14 @@ async fn main(spawner: Spawner) {
let p = embassy_stm32::init(Default::default());
info!("Hello World!");
unwrap!(spawner.spawn(blinky(p.PC13)));
unwrap!(spawner.spawn(blinky(p.PC13.degrade())));
let ch3 = CapturePin::new_ch3(p.PA2, Pull::None);
let mut ic = InputCapture::new(p.TIM2, None, None, Some(ch3), None, Irqs, khz(1000), Default::default());
loop {
info!("wait for rising edge");
ic.wait_for_rising_edge(Channel::Ch3).await;
let capture_value = ic.get_capture_value(Channel::Ch3);
let capture_value = ic.wait_for_rising_edge(Channel::Ch3).await;
info!("new capture! {}", capture_value);
}
}

View File

@ -3,7 +3,7 @@
use defmt::*;
use embassy_executor::Spawner;
use embassy_stm32::gpio::{Level, Output, Pull, Speed};
use embassy_stm32::gpio::{AnyPin, Level, Output, Pin, Pull, Speed};
use embassy_stm32::time::khz;
use embassy_stm32::timer::pwm_input::PwmInput;
use embassy_stm32::{bind_interrupts, peripherals, timer};
@ -13,7 +13,7 @@ use {defmt_rtt as _, panic_probe as _};
/// Connect PA0 and PC13 with a 1k Ohm resistor
#[embassy_executor::task]
async fn blinky(led: peripherals::PC13) {
async fn blinky(led: AnyPin) {
let mut led = Output::new(led, Level::High, Speed::Low);
loop {
@ -36,19 +36,14 @@ async fn main(spawner: Spawner) {
let p = embassy_stm32::init(Default::default());
info!("Hello World!");
unwrap!(spawner.spawn(blinky(p.PC13)));
unwrap!(spawner.spawn(blinky(p.PC13.degrade())));
let mut pwm_input = PwmInput::new(p.TIM2, p.PA0, Pull::None, Irqs, khz(10));
pwm_input.enable();
loop {
Timer::after_millis(500).await;
let period = pwm_input.get_period_ticks();
let width = pwm_input.get_width_ticks();
let duty_cycle = pwm_input.get_duty_cycle();
info!(
"period ticks: {} width ticks: {} duty cycle: {}",
period, width, duty_cycle
);
info!("wait for falling edge");
let width = pwm_input.wait_for_falling_edge().await;
info!("pulse width: {}", width);
}
}

View File

@ -3,7 +3,7 @@
use defmt::*;
use embassy_executor::Spawner;
use embassy_stm32::gpio::{Level, Output, Pull, Speed};
use embassy_stm32::gpio::{AnyPin, Level, Output, Pin, Pull, Speed};
use embassy_stm32::time::khz;
use embassy_stm32::timer::input_capture::{CapturePin, InputCapture};
use embassy_stm32::timer::{self, Channel};
@ -14,7 +14,7 @@ use {defmt_rtt as _, panic_probe as _};
/// Connect PB2 and PB10 with a 1k Ohm resistor
#[embassy_executor::task]
async fn blinky(led: peripherals::PB2) {
async fn blinky(led: AnyPin) {
let mut led = Output::new(led, Level::High, Speed::Low);
loop {
@ -37,7 +37,7 @@ async fn main(spawner: Spawner) {
let p = embassy_stm32::init(Default::default());
info!("Hello World!");
unwrap!(spawner.spawn(blinky(p.PB2)));
unwrap!(spawner.spawn(blinky(p.PB2.degrade())));
let ch3 = CapturePin::new_ch3(p.PB10, Pull::None);
let mut ic = InputCapture::new(p.TIM2, None, None, Some(ch3), None, Irqs, khz(1000), Default::default());

View File

@ -3,7 +3,7 @@
use defmt::*;
use embassy_executor::Spawner;
use embassy_stm32::gpio::{Level, Output, Pull, Speed};
use embassy_stm32::gpio::{AnyPin, Level, Output, Pin, Pull, Speed};
use embassy_stm32::time::khz;
use embassy_stm32::timer::pwm_input::PwmInput;
use embassy_stm32::{bind_interrupts, peripherals, timer};
@ -13,7 +13,7 @@ use {defmt_rtt as _, panic_probe as _};
/// Connect PB2 and PA6 with a 1k Ohm resistor
#[embassy_executor::task]
async fn blinky(led: peripherals::PB2) {
async fn blinky(led: AnyPin) {
let mut led = Output::new(led, Level::High, Speed::Low);
loop {
@ -36,12 +36,13 @@ async fn main(spawner: Spawner) {
let p = embassy_stm32::init(Default::default());
info!("Hello World!");
unwrap!(spawner.spawn(blinky(p.PB2)));
unwrap!(spawner.spawn(blinky(p.PB2.degrade())));
let mut pwm_input = PwmInput::new(p.TIM3, p.PA6, Pull::None, Irqs, khz(10));
pwm_input.enable();
loop {
info!("wait for rising edge");
let width = pwm_input.wait_for_falling_edge().await;
info!("pulse width: {}", width);
}