diff --git a/examples/py32f072/src/bin/button_exti.rs b/examples/py32f072/src/bin/button_exti.rs new file mode 100644 index 0000000..22cd697 --- /dev/null +++ b/examples/py32f072/src/bin/button_exti.rs @@ -0,0 +1,28 @@ +#![no_std] +#![no_main] +#![feature(impl_trait_in_assoc_type)] + +use defmt::*; +use embassy_executor::Spawner; +use py32_hal::exti::ExtiInput; +use py32_hal::gpio::Pull; +use {defmt_rtt as _, panic_probe as _}; + +#[embassy_executor::main] +async fn main(_spawner: Spawner) { + let p = py32_hal::init(Default::default()); + info!("Hello World!"); + + let mut button = ExtiInput::new(p.PB0, p.EXTI0, Pull::Up); + // let mut button = ExtiInput::new(p.PF4, p.EXTI4, Pull::None); // BOOT button + // let mut button = ExtiInput::new(p.PA12, p.EXTI12, Pull::Up); + + info!("Press the USER button..."); + + loop { + button.wait_for_falling_edge().await; + info!("Pressed!"); + button.wait_for_rising_edge().await; + info!("Released!"); + } +} diff --git a/examples/py32f072/src/bin/usart.rs b/examples/py32f072/src/bin/usart.rs new file mode 100644 index 0000000..f310a5f --- /dev/null +++ b/examples/py32f072/src/bin/usart.rs @@ -0,0 +1,36 @@ +#![no_std] +#![no_main] +#![feature(impl_trait_in_assoc_type)] + +use embassy_executor::Spawner; +use defmt::*; +use py32_hal::usart::{Config, Uart}; +use py32_hal::rcc::{Pll, PllSource, Sysclk, PllMul}; +use py32_hal::time::Hertz; +use {defmt_rtt as _, panic_halt as _}; + + +#[embassy_executor::main] +async fn main(_spawner: Spawner) { + let mut cfg: py32_hal::Config = Default::default(); + cfg.rcc.hsi = Some(Hertz::mhz(24)); + cfg.rcc.pll = Some(Pll { + src: PllSource::HSI, + mul: PllMul::MUL3, + }); + cfg.rcc.sys = Sysclk::PLL; + let p = py32_hal::init(cfg); + + let config = Config::default(); + // let mut usart = Uart::new_blocking(p.USART2, p.PA3, p.PA2, config).unwrap(); + let mut usart = Uart::new_blocking(p.USART1, p.PA10, p.PA9, config).unwrap(); + + unwrap!(usart.blocking_write(b"Hello Embassy World!")); + info!("wrote Hello, starting echo"); + + let mut buf = [0u8; 1]; + loop { + unwrap!(usart.blocking_read(&mut buf)); + unwrap!(usart.blocking_write(&buf)); + } +} diff --git a/examples/py32f072/src/bin/usart_buffered.rs b/examples/py32f072/src/bin/usart_buffered.rs new file mode 100644 index 0000000..79c7f1a --- /dev/null +++ b/examples/py32f072/src/bin/usart_buffered.rs @@ -0,0 +1,54 @@ +#![no_std] +#![no_main] +#![feature(impl_trait_in_assoc_type)] + +use defmt::*; +use embassy_executor::Spawner; +use py32_hal::usart::{BufferedUart, Config}; +use py32_hal::{bind_interrupts, peripherals, usart}; +use embedded_io_async::Read; +use embedded_io_async::Write; +use py32_hal::rcc::{Pll, PllSource, Sysclk, PllMul}; +use py32_hal::time::Hertz; +use {defmt_rtt as _, panic_halt as _}; + +bind_interrupts!(struct Irqs { + USART1 => usart::BufferedInterruptHandler; +}); + +#[embassy_executor::main] +async fn main(_spawner: Spawner) { + let mut cfg: py32_hal::Config = Default::default(); + cfg.rcc.hsi = Some(Hertz::mhz(24)); + cfg.rcc.pll = Some(Pll { + src: PllSource::HSI, + mul: PllMul::MUL3, + }); + cfg.rcc.sys = Sysclk::PLL; + let p = py32_hal::init(cfg); + info!("Hello World!"); + + let config = Config::default(); + let mut tx_buf = [0u8; 256]; + let mut rx_buf = [0u8; 256]; + let mut usart = BufferedUart::new(p.USART1, Irqs, p.PA10, p.PA9, &mut tx_buf, &mut rx_buf, config).unwrap(); + + usart.write_all(b"Hello Embassy World!\r\n").await.unwrap(); + info!("wrote Hello, starting echo"); + + let mut buf = [0; 5]; + loop { + // When using defmt, be cautious with the info! and other logging macros! + // If you're using a single channel (as is usually the case), defmt requires global_logger to acquire interrupts to be disabled. + // For example, defmt-rtt uses critical_section, which temporarily disables global interrupts. + //This can lead to USART Overrun error(SR.ORE), causing some data to be lost. + usart.read_exact(&mut buf[..]).await.unwrap(); + // info!("Received:{} {}", buf, buf.len()); + usart.write_all(&buf[..]).await.unwrap(); + + // use embedded_io_async::BufRead; + // let buf = usart.fill_buf().await.unwrap(); + // let n = buf.len(); + // usart.consume(n); + } +} diff --git a/examples/py32f072/src/bin/usb_hid_keyboard.rs b/examples/py32f072/src/bin/usb_hid_keyboard.rs index 6ea5672..0875429 100644 --- a/examples/py32f072/src/bin/usb_hid_keyboard.rs +++ b/examples/py32f072/src/bin/usb_hid_keyboard.rs @@ -100,12 +100,12 @@ async fn main(_spawner: Spawner) { let (reader, mut writer) = hid.split(); - let mut button = ExtiInput::new(p.PB0, p.EXTI0, Pull::Down); + let mut button = ExtiInput::new(p.PB0, p.EXTI0, Pull::Up); // Do stuff with the class! let in_fut = async { loop { - button.wait_for_rising_edge().await; + button.wait_for_falling_edge().await; // signal_pin.wait_for_high().await; info!("Button pressed!"); // Create a report with the A key pressed. (no shift modifier) @@ -121,7 +121,7 @@ async fn main(_spawner: Spawner) { Err(e) => warn!("Failed to send report: {:?}", e), }; - button.wait_for_falling_edge().await; + button.wait_for_rising_edge().await; // signal_pin.wait_for_low().await; info!("Button released!"); let report = KeyboardReport {