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 fn handle_irq(&mut self);
24 fn take_tx(&mut self) -> Option<Box<dyn Sender>>;
26 fn take_rx(&mut self) -> Option<Box<dyn Reciever>>;
28}
29
30#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
36#[non_exhaustive]
37pub enum SerialError {
38 Overrun,
40 FrameFormat,
43 Parity,
45 Noise,
47 Closed,
49 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}