From 7b8fd672f7df629a535963d96cbe1ebdc274b2ee Mon Sep 17 00:00:00 2001 From: decaday Date: Thu, 12 Dec 2024 15:09:07 +0800 Subject: [PATCH 1/2] example: add 072 exti usart example --- examples/py32f072/src/bin/button_exti.rs | 28 ++++++++++ examples/py32f072/src/bin/usart.rs | 36 +++++++++++++ examples/py32f072/src/bin/usart_buffered.rs | 54 +++++++++++++++++++ examples/py32f072/src/bin/usb_hid_keyboard.rs | 6 +-- 4 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 examples/py32f072/src/bin/button_exti.rs create mode 100644 examples/py32f072/src/bin/usart.rs create mode 100644 examples/py32f072/src/bin/usart_buffered.rs 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 { From 9d446f3f0ec7bfff465570292d22a619a7aaf488 Mon Sep 17 00:00:00 2001 From: decaday Date: Thu, 12 Dec 2024 15:09:42 +0800 Subject: [PATCH 2/2] docs: update readme --- README.md | 46 ++++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 8a98eb8..3053248 100644 --- a/README.md +++ b/README.md @@ -37,25 +37,27 @@ Supported chip flags: `py32f030f16`, `py32f030k28`, `py32f072c1b`, More is comin Note: Currently the program behavior has nothing to do with chip packaging. -Others should work if you are careful as most peripherals are similar enough.In fact, the IPs of peripherals in different PY32 series may be consistent. Moreover, some series use the same die, so it might not require much work. +Others should work if you are careful as most peripherals are similar enough. In fact, the IPs of peripherals in different PY32 series may be consistent. Moreover, some series use the same die, so it might not require much work. For a full list of chip capabilities and peripherals, check the [py32-data](https://github.com/py32-rs/py32-data) repository. -| Family | F002B/L020/F001 | F030/F003/F002A | F040/F07x/MD410 | F403 | -| ---------- | --------------- | --------------- | --------------- | ---- | -| Embassy | | ✅ | ✅ | | -| RCC | | ✅ | ✅ | | -| GPIO | | ✅ | ✅ | | -| INTERRUPT | | ✅ | ✅ | | -| DMA | N/A | | | | -| EXTI | | ✅+ | ❓ | | -| USART | | ✅ | ❓ | | -| I2C | | ✅ | ❓ | | -| SPI | | | | | -| ADC | | ✅+ | ✅ | | -| RTC | | | | | -| Timer(PWM) | | ✅ | ❓ | | -| USB | N/A | N/A | ✅+ | | +| Family | F002B/L020 | F030/F003/F002A | F040/F07x/MD410 | F403 | +| ---------- | ---------- | --------------- | --------------- | ---- | +| Embassy | | ✅ | ✅ | | +| RCC | | ✅ | ✅ | | +| GPIO | | ✅ | ✅ | | +| INTERRUPT | | ✅ | ✅ | | +| DMA | N/A | | | | +| EXTI | | ✅+ | ✅+ | | +| USART | | ✅+ | ✅+ | | +| I2C | | ✅ | ❓ | | +| SPI | | | | | +| ADC | | ✅+ | ✅ | | +| RTC | | | | | +| Timer(PWM) | | ✅ | ❓ | | +| USB | N/A | N/A | ✅+ | | +| DAC | N/A | N/A | | | +| I2S | N/A | N/A | | | - ✅ : Implemented - Blank : Not implemented @@ -91,11 +93,9 @@ Embassy requires that any TIM used as a time-driver has at least two channels, s For PY32F07x, F040, you can use TIM15, TIM3 or TIM1. -## Minimum supported Rust version(MSRV) +## Awesome List -This project is developed with a recent **nightly** version of Rust compiler. And is expected to work with beta versions of Rust. - -Feel free to change this if you did some testing with some version of Rust. +[py32csdk-hal-sys](https://github.com/decaday/py32csdk-hal-sys): PY32F0 MCU c SDK bindings rust crate ## Contributing @@ -110,6 +110,12 @@ All kinds of contributions are welcome. - Adding new peripheral drivers - ... +## Minimum supported Rust version(MSRV) + +This project is developed with a recent **nightly** version of Rust compiler. And is expected to work with beta versions of Rust. + +Feel free to change this if you did some testing with some version of Rust. + ## License This project is licensed under the MIT or Apache-2.0 license, at your option.