va416xx-rs/va416xx-hal/examples/uart.rs

49 lines
1.4 KiB
Rust

//! UART example application. Sends a test string over a UART and then enters
//! echo mode
#![no_main]
#![no_std]
use cortex_m_rt::entry;
use embedded_io::{Read, Write};
use panic_rtt_target as _;
use rtt_target::{rprintln, rtt_init_print};
use va416xx_hal::time::{Hertz, MegaHertz};
use va416xx_hal::{gpio::PinsG, pac, uart};
#[entry]
fn main() -> ! {
rtt_init_print!();
rprintln!("-- VA416xx UART example application--");
// SAFETY: Peripherals are only stolen once here.
let mut dp = unsafe { pac::Peripherals::steal() };
let gpiob = PinsG::new(&mut dp.sysconfig, Some(dp.ioconfig), dp.portg);
let tx = gpiob.pg0.into_funsel_1();
let rx = gpiob.pg1.into_funsel_1();
let uartb = uart::Uart::uart0(
dp.uart0,
(tx, rx),
Hertz::from_raw(115200),
&mut dp.sysconfig,
Hertz::from_raw(MegaHertz::from_raw(20).to_Hz()),
);
let (mut tx, mut rx) = uartb.split();
let mut recv_buf: [u8; 32] = [0; 32];
writeln!(tx, "Hello World").unwrap();
loop {
// Echo what is received on the serial link.
match rx.read(&mut recv_buf) {
Ok(recvd) => {
if let Err(e) = tx.write(&recv_buf[0..recvd]) {
rprintln!("UART TX error: {:?}", e);
}
}
Err(e) => {
rprintln!("UART RX error {:?}", e);
}
}
}
}