Skip to content

Commit

Permalink
example: add 072 exti usart example
Browse files Browse the repository at this point in the history
  • Loading branch information
decaday committed Dec 12, 2024
1 parent b6fa1db commit 7b8fd67
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 3 deletions.
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

0 comments on commit 7b8fd67

Please sign in to comment.