From 2c3fa991ad9adb63d6fa2704c6e084bff0b9609d Mon Sep 17 00:00:00 2001 From: pca006132 Date: Mon, 15 Jun 2020 15:00:54 +0800 Subject: [PATCH] implemented display trait for errors --- experiments/src/main.rs | 2 +- libboard_zynq/src/sdio/mod.rs | 13 +++++++++++++ libboard_zynq/src/sdio/sd_card.rs | 13 +++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/experiments/src/main.rs b/experiments/src/main.rs index a9cb3e9..7938e95 100644 --- a/experiments/src/main.rs +++ b/experiments/src/main.rs @@ -88,7 +88,7 @@ pub fn main_core0() { let result = SdCard::from_sdio(sd); match &result { Ok(_) => info!("OK!"), - Err(a) => info!("{:?}", a), + Err(a) => info!("{}", a), }; const SIZE: usize = 512 * 2 + 1; let mut sd_card = result.unwrap(); diff --git a/libboard_zynq/src/sdio/mod.rs b/libboard_zynq/src/sdio/mod.rs index 4c2aa16..85b412d 100644 --- a/libboard_zynq/src/sdio/mod.rs +++ b/libboard_zynq/src/sdio/mod.rs @@ -28,6 +28,19 @@ pub enum CmdTransferError { Other(regs::interrupt_status::Read), } +impl core::fmt::Display for CmdTransferError { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + use CmdTransferError::*; + write!(f, "Command transfer error: ")?; + match self { + CmdInhibited => write!(f, "Command line inhibited."), + DatLineInhibited => write!(f, "Data line inhibited, possibly due to ongonging data transfer."), + CmdTimeout => write!(f, "Command timeout, check if the card is inserted properly."), + Other(x) => write!(f, "Unknown Error, interrupt status = 0x{:0X}", x.inner), + } + } +} + #[derive(PartialEq, Debug, Clone, Copy)] pub enum CardType { CardNone, diff --git a/libboard_zynq/src/sdio/sd_card.rs b/libboard_zynq/src/sdio/sd_card.rs index 7b083ba..a80aa56 100644 --- a/libboard_zynq/src/sdio/sd_card.rs +++ b/libboard_zynq/src/sdio/sd_card.rs @@ -11,6 +11,19 @@ pub enum CardInitializationError { InitializationFailedCmd(CmdTransferError), } +impl core::fmt::Display for CardInitializationError { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + use CardInitializationError::*; + write!(f, "Card initialization error: \n ")?; + match self { + AlreadyInitialized => write!(f, "Card already initialized."), + NoCardInserted => write!(f, "No card inserted, check if the card is inserted properly."), + InitializationFailedOther => write!(f, "Unknown error. Please check the debug messages."), + InitializationFailedCmd(x) => write!(f, "{}", x) + } + } +} + impl From for CardInitializationError { fn from(error: CmdTransferError) -> Self { CardInitializationError::InitializationFailedCmd(error)