#[cfg(doc)]
use crate::Uart16550;
use crate::backend::RegisterAddress;
use crate::spec::{FIFO_SIZE, NonIntegerDivisorError};
use core::error::Error;
use core::fmt;
use core::fmt::Display;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum InvalidAddressError<A: RegisterAddress> {
InvalidBaseAddress(A),
InvalidStride(u8),
}
impl<A: RegisterAddress> Display for InvalidAddressError<A> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidBaseAddress(addr) => {
write!(f, "invalid register address: {addr:x?}")
}
Self::InvalidStride(stride) => {
write!(
f,
"invalid stride {stride}: must be non-zero and a power of two (typically 1, 2, 4, or 8)"
)
}
}
}
}
impl<A: RegisterAddress> Error for InvalidAddressError<A> {}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum LoopbackError {
SendError(ByteSendError),
UnexpectedLoopbackByte {
expected: u8,
actual: u8,
},
UnexpectedLoopbackMsg {
expected: [u8; FIFO_SIZE],
actual: [u8; FIFO_SIZE],
},
}
impl Display for LoopbackError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::SendError(e) => {
write!(f, "loopback test failed: couldn't send data: {e}")
}
Self::UnexpectedLoopbackByte { expected, actual } => {
write!(
f,
"loopback test failed: read unexpected byte! expected={expected}, actual={actual}"
)
}
Self::UnexpectedLoopbackMsg { expected, actual } => {
let expected = core::str::from_utf8(expected);
let maybe_actual_str = core::str::from_utf8(actual);
write!(
f,
"loopback test failed: read unexpected string! expected (str)={expected:?}, actual (str)={maybe_actual_str:?}, actual (bytes)={actual:?}"
)
}
}
}
}
impl Error for LoopbackError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::SendError(e) => Some(e),
_ => None,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum InitError {
DeviceNotPresent,
InvalidBaudRate(NonIntegerDivisorError),
}
impl Display for InitError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::DeviceNotPresent => {
write!(f, "the device could not be detected")
}
Self::InvalidBaudRate(e) => {
write!(f, "invalid baud rate: {e}")
}
}
}
}
impl Error for InitError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::InvalidBaudRate(err) => Some(err),
_ => None,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct ByteReceiveError;
impl Display for ByteReceiveError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "there is no data to read")
}
}
impl Error for ByteReceiveError {}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum ByteSendError {
NoCapacity,
RemoteNotClearToSend,
}
impl Display for ByteSendError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NoCapacity => write!(
f,
"device has no capacity to send another byte (the FIFO might be full)"
),
Self::RemoteNotClearToSend => {
write!(f, "the remote didn't raised its clear to send signal")
}
}
}
}
impl Error for ByteSendError {}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum RemoteReadyToReceiveError {
NoRemoteConnectedNoDSR,
NoRemoteConnectedNoCD,
RemoteNotClearToSend,
}
impl Display for RemoteReadyToReceiveError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NoRemoteConnectedNoDSR => {
write!(
f,
"there is no remote connected: missing Data Set Ready (DSR)"
)
}
Self::NoRemoteConnectedNoCD => {
write!(
f,
"there is no remote connected: missing Carrier Detect (CD)"
)
}
Self::RemoteNotClearToSend => {
write!(f, "remote is not (yet) ready to receive more data")
}
}
}
}
impl Error for RemoteReadyToReceiveError {}