stm32/usart_v1: add read

This commit is contained in:
Dario Nieuwenhuis 2021-05-15 04:25:44 +02:00
parent 852ca5a1c5
commit befc052cba
2 changed files with 34 additions and 2 deletions

View File

@ -25,9 +25,13 @@ async fn main_task() {
let config = Config::default();
let mut usart = Uart::new(p.USART3, p.PD9, p.PD8, config, 16_000_000);
loop {
info!("wrote");
usart.bwrite_all(b"Hello Embassy World!\r\n").unwrap();
info!("wrote Hello, starting echo");
let mut buf = [0u8; 1];
loop {
usart.read(&mut buf).unwrap();
usart.bwrite_all(&buf).unwrap();
}
}

View File

@ -102,6 +102,34 @@ impl<'d, T: Instance> Uart<'d, T> {
phantom: PhantomData,
}
}
pub fn read(&mut self, buffer: &mut [u8]) -> Result<(), Error> {
unsafe {
let r = self.inner.regs();
for b in buffer {
loop {
let sr = r.sr().read();
if sr.pe() {
r.dr().read();
return Err(Error::Parity);
} else if sr.fe() {
r.dr().read();
return Err(Error::Framing);
} else if sr.ne() {
r.dr().read();
return Err(Error::Noise);
} else if sr.ore() {
r.dr().read();
return Err(Error::Overrun);
} else if sr.rxne() {
break;
}
}
*b = r.dr().read().0 as u8;
}
}
Ok(())
}
}
impl<'d, T: Instance> embedded_hal::blocking::serial::Write<u8> for Uart<'d, T> {