Implement embedded_hal::serial traits for the UART.

This commit is contained in:
Nic0w 2021-04-26 22:05:37 +02:00
parent 50a428e2ad
commit f3fba80a71
3 changed files with 63 additions and 4 deletions

View file

@ -23,4 +23,5 @@ pub mod ssi;
pub mod timer;
pub mod uart;
pub mod usb;
pub mod serial;
pub mod watchdog;

50
rp2040-hal/src/serial.rs Normal file
View file

@ -0,0 +1,50 @@
//! Implementation for the embedded_hal::serial traits for the UART.
// See [embedded-hal](https://docs.rs/embedded-hal/0.2.4/embedded_hal/serial/index.html) for more details
use core::convert::Infallible;
use crate::uart::{
UARTPeripheral,
UARTDevice,
Enabled
};
use embedded_hal::serial::{
Read,
Write
};
use nb::Error::WouldBlock;
impl<D: UARTDevice> Read<u8> for UARTPeripheral<Enabled, D> {
type Error = Infallible;
fn read(&mut self) -> nb::Result<u8, Self::Error> {
let byte: &mut [u8] = &mut [0; 1];
if let Err(_) = self.read_raw(byte) {
Err(WouldBlock)
}
else {
Ok(byte[0])
}
}
}
impl<D: UARTDevice> Write<u8> for UARTPeripheral<Enabled, D> {
type Error = Infallible;
fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> {
if let Err(_) = self.write_raw(&[word]) {
Err(WouldBlock)
}
else {
Ok(())
}
}
fn flush(&mut self) -> nb::Result<(), Self::Error> {
self.transmit_flushed()
}
}

View file

@ -203,6 +203,14 @@ impl<D: UARTDevice> UARTPeripheral<Enabled, D> {
self.transition(Disabled)
}
pub(crate) fn transmit_flushed(&self) -> nb::Result<(), Infallible> {
if self.device.uartfr.read().txfe().bit_is_set() {
Ok(())
}
else { Err(WouldBlock) }
}
fn uart_is_writable(&self) -> bool {
self.device.uartfr.read().txff().bit_is_clear()
}
@ -216,7 +224,7 @@ impl<D: UARTDevice> UARTPeripheral<Enabled, D> {
/// - 0 bytes were written, a WouldBlock Error is returned
/// - some bytes were written, it is deemed to be a success
/// Upon success, the number of written bytes is returned.
pub fn write<'d>(&self, data: &'d [u8]) -> nb::Result<&'d [u8], Infallible> {
pub fn write_raw <'d>(&self, data: &'d [u8]) -> nb::Result<&'d [u8], Infallible> {
let mut bytes_written = 0;
@ -246,7 +254,7 @@ impl<D: UARTDevice> UARTPeripheral<Enabled, D> {
/// - 0 bytes were read, a WouldBlock Error is returned
/// - some bytes were read, it is deemed to be a success
/// Upon success, the number of read bytes is returned.
pub fn read<'b>(&self, buffer: &'b mut [u8]) -> nb::Result<&'b mut [u8], Infallible> {
pub fn read_raw<'b>(&self, buffer: &'b mut [u8]) -> nb::Result<&'b mut [u8], Infallible> {
let mut bytes_read = 0;
@ -277,7 +285,7 @@ impl<D: UARTDevice> UARTPeripheral<Enabled, D> {
let mut temp = data;
while !temp.is_empty() {
temp = match self.write(temp) {
temp = match self.write_raw(temp) {
Ok(remaining) => remaining,
Err(WouldBlock) => continue,
Err(_) => unreachable!()
@ -291,7 +299,7 @@ impl<D: UARTDevice> UARTPeripheral<Enabled, D> {
let mut offset = 0;
while offset != buffer.len() {
offset += match self.read(&mut buffer[offset..]) {
offset += match self.read_raw(&mut buffer[offset..]) {
Ok(remaining) => { remaining.len() },
Err(WouldBlock) => continue,
Err(_) => unreachable!()