1#![no_std]
2
3#[cfg(test)]
58extern crate std;
59
60pub mod ns16550;
61pub mod pl011;
62
63use bitflags::bitflags;
64
65pub trait PollingUart {
67 fn poll_status(&mut self) -> PollingEvent;
68
69 fn write_byte(&mut self, byte: u8);
70
71 fn read_byte(&mut self, status: PollingEvent) -> Option<Result<u8, TransferError>>;
72}
73
74bitflags! {
75 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
76 pub struct PollingEvent: u32 {
77 const RX_READY = 0x01;
78 const TX_READY = 0x02;
79 const RX_ERROR = 0x04;
80 const TX_ERROR = 0x08;
81 const OVERRUN = 0x10;
82 const MODEM_STATUS = 0x20;
83 }
84}
85
86impl PollingEvent {
87 pub const fn rx_ready(self) -> bool {
88 self.contains(Self::RX_READY)
89 }
90
91 pub const fn tx_ready(self) -> bool {
92 self.contains(Self::TX_READY)
93 }
94
95 pub const fn rx_error(self) -> bool {
96 self.intersects(Self::RX_ERROR.union(Self::OVERRUN))
97 }
98}
99
100pub type SerialEvent = PollingEvent;
101
102#[derive(Debug, Clone, Copy, PartialEq, Eq)]
103pub enum SerialDirection {
104 Input,
105 Output,
106}
107
108#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Eq)]
109pub enum TransferError {
110 #[error("data overrun by `{0:#x}`")]
111 Overrun(u8),
112 #[error("parity error")]
113 Parity,
114 #[error("framing error")]
115 Framing,
116 #[error("break condition")]
117 Break,
118 #[error("serial closed")]
119 Closed,
120}
121
122#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Eq)]
123#[error("transfer error after transferring {bytes_transferred} bytes: {kind}")]
124pub struct TransBytesError {
125 pub bytes_transferred: usize,
126 #[source]
127 pub kind: TransferError,
128}
129
130pub use rdif_serial::*;
132
133#[cfg(test)]
134mod tests {
135 use super::*;
136
137 #[test]
138 fn transferred_byte_error_preserves_transfer_source() {
139 let error = TransBytesError {
140 bytes_transferred: 7,
141 kind: TransferError::Framing,
142 };
143
144 assert!(core::error::Error::source(&error).is_some());
145 }
146}