#![no_std]
#[cfg(test)]
extern crate std;
pub mod ns16550;
pub mod pl011;
use core::fmt::Display;
use bitflags::bitflags;
pub trait PollingUart {
fn poll_status(&mut self) -> PollingEvent;
fn write_byte(&mut self, byte: u8);
fn read_byte(&mut self, status: PollingEvent) -> Option<Result<u8, TransferError>>;
}
bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PollingEvent: u32 {
const RX_READY = 0x01;
const TX_READY = 0x02;
const RX_ERROR = 0x04;
const TX_ERROR = 0x08;
const OVERRUN = 0x10;
const MODEM_STATUS = 0x20;
}
}
impl PollingEvent {
pub const fn rx_ready(self) -> bool {
self.contains(Self::RX_READY)
}
pub const fn tx_ready(self) -> bool {
self.contains(Self::TX_READY)
}
pub const fn rx_error(self) -> bool {
self.intersects(Self::RX_ERROR.union(Self::OVERRUN))
}
}
pub type SerialEvent = PollingEvent;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SerialDirection {
Input,
Output,
}
#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Eq)]
pub enum TransferError {
#[error("data overrun by `{0:#x}`")]
Overrun(u8),
#[error("parity error")]
Parity,
#[error("framing error")]
Framing,
#[error("break condition")]
Break,
#[error("serial closed")]
Closed,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TransBytesError {
pub bytes_transferred: usize,
pub kind: TransferError,
}
impl Display for TransBytesError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"transfer error after transferring {} bytes: {}",
self.bytes_transferred, self.kind
)
}
}
impl core::error::Error for TransBytesError {}
pub use rdif_serial::*;