Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exti usart examples #20

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 26 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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.
Expand Down
28 changes: 28 additions & 0 deletions examples/py32f072/src/bin/button_exti.rs
Original file line number Diff line number Diff line change
@@ -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!");
}
}
36 changes: 36 additions & 0 deletions examples/py32f072/src/bin/usart.rs
Original file line number Diff line number Diff line change
@@ -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));
}
}
54 changes: 54 additions & 0 deletions examples/py32f072/src/bin/usart_buffered.rs
Original file line number Diff line number Diff line change
@@ -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<peripherals::USART1>;
});

#[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);
}
}
6 changes: 3 additions & 3 deletions examples/py32f072/src/bin/usb_hid_keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
Loading