Skip to content

Fix async uart and add example for async uart echo #96

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
45 changes: 45 additions & 0 deletions examples/ch32v307/src/bin/uart_echo_async.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#![no_std]
#![no_main]

use embassy_executor::Spawner;
use hal::gpio::{Level, Output};
use hal::usart;
use hal::usart::Uart;
use {ch32_hal as hal, panic_halt as _};

use ch32_hal::bind_interrupts;

bind_interrupts!(struct Irqs {
USART4 => ch32_hal::usart::InterruptHandler<ch32_hal::peripherals::USART4>;
});

#[embassy_executor::main(entry = "ch32_hal::entry")]
async fn main(_spawner: Spawner) -> ! {
let p = hal::init(Default::default());

// GPIO
let mut led = Output::new(p.PB4, Level::Low, Default::default());

let cfg = usart::Config::default();
let (mut tx_uart, mut rx_uart) = Uart::new(p.USART4, p.PE1, p.PE0, Irqs, p.DMA2_CH5, p.DMA2_CH3, cfg)
.unwrap()
.split();

tx_uart.write(b"Init ok\r\n").await.unwrap();

let mut buf = [0u8; 255];
loop {
match rx_uart.read_until_idle(&mut buf).await {
Ok(size) => {
for character in &mut buf[..size] {
if *character >= b'a' && *character <= b'z' {
*character -= 32;
}
}
tx_uart.write(&buf[..size]).await.unwrap();
led.toggle();
}
Err(_err) => {}
}
}
}
2 changes: 2 additions & 0 deletions src/usart/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,7 @@ impl<'d, T: Instance> Uart<'d, T, Async> {

rx.set_as_input(Pull::None);
tx.set_as_af_output(AFType::OutputPushPull, Speed::High);
T::set_remap(REMAP);

Self::new_inner(
peri,
Expand Down Expand Up @@ -869,6 +870,7 @@ impl<'d, T: Instance> Uart<'d, T, Async> {
tx.set_as_af_output(AFType::OutputPushPull, Speed::High);
rts.set_as_af_output(AFType::OutputPushPull, Speed::High);
cts.set_as_input(Pull::None);
T::set_remap(REMAP);

Self::new_inner(
peri,
Expand Down