Move serial traits impl. back to uart.rs

This commit is contained in:
Nic0w 2021-05-02 08:41:20 +02:00
parent 835ad7a5c1
commit abf91a3687
3 changed files with 39 additions and 51 deletions

View file

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

View file

@ -1,50 +0,0 @@
//! 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

@ -6,6 +6,12 @@ use core::ops::Deref;
use embedded_time::rate::Baud;
use embedded_time::rate::Hertz;
use embedded_time::fixed_point::FixedPoint;
use embedded_hal::serial::{
Read,
Write
};
use nb::Error::{
WouldBlock,
Other
@ -435,3 +441,36 @@ fn set_format<'w>(w: &'w mut UART_LCR_H_Writer, data_bits: &DataBits, stop_bits:
w
}
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()
}
}