rdif_serial/
lib.rs

1#![no_std]
2
3extern crate alloc;
4
5use alloc::boxed::Box;
6
7pub use futures::future::LocalBoxFuture;
8pub use rdif_base::{DriverGeneric, ErrorBase};
9
10pub trait Sender: Send {
11    fn write(&mut self, buf: &[u8]) -> Result<usize, SerialError>;
12    fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> LocalBoxFuture<'a, Result<(), SerialError>>;
13}
14
15pub trait Reciever: Send {
16    fn read(&mut self, buf: &mut [u8]) -> Result<usize, SerialError>;
17    fn read_all<'a>(&'a mut self, buf: &'a mut [u8])
18    -> LocalBoxFuture<'a, Result<(), SerialError>>;
19}
20
21pub trait Interface: DriverGeneric {
22    /// Call in irq handler.
23    fn handle_irq(&mut self);
24    /// [`Sender`] will be given back when dropped.
25    fn take_tx(&mut self) -> Option<Box<dyn Sender>>;
26    /// [`Reciever`] will be given back when dropped.
27    fn take_rx(&mut self) -> Option<Box<dyn Reciever>>;
28}
29
30/// Serial error kind.
31///
32/// This represents a common set of serial operation errors. HAL implementations are
33/// free to define more specific or additional error types. However, by providing
34/// a mapping to these common serial errors, generic code can still react to them.
35#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
36#[non_exhaustive]
37pub enum SerialError {
38    /// The peripheral receive buffer was overrun.
39    Overrun,
40    /// Received data does not conform to the peripheral configuration.
41    /// Can be caused by a misconfigured device on either end of the serial line.
42    FrameFormat,
43    /// Parity check failed.
44    Parity,
45    /// Serial line is too noisy to read valid data.
46    Noise,
47    /// Device was closed.
48    Closed,
49    /// A different error occurred. The original error may contain more information.
50    Other,
51}
52
53impl core::error::Error for SerialError {}
54
55impl core::fmt::Display for SerialError {
56    #[inline]
57    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
58        match self {
59            Self::Overrun => write!(f, "The peripheral receive buffer was overrun"),
60            Self::Parity => write!(f, "Parity check failed"),
61            Self::Noise => write!(f, "Serial line is too noisy to read valid data"),
62            Self::FrameFormat => write!(
63                f,
64                "Received data does not conform to the peripheral configuration"
65            ),
66            Self::Closed => write!(f, "Device was closed"),
67            Self::Other => write!(
68                f,
69                "A different error occurred. The original error may contain more information"
70            ),
71        }
72    }
73}