embedded_hal_sdmmc/
lib.rs

1//! Types and traits for SD/MMC peripherals.
2
3#![cfg_attr(not(feature = "std"), no_std)]
4
5mod bus_width;
6mod card_mode;
7mod card_type;
8mod fifo_status;
9mod reset;
10
11pub mod command;
12pub mod response;
13pub mod tuning;
14
15pub use bus_width::{MmcBusWidth, SdBusWidth};
16pub use card_mode::CardMode;
17pub use card_type::CardType;
18pub use fifo_status::FifoStatus;
19pub use reset::Reset;
20
21use command::Command;
22use response::Response;
23use tuning::{TuningMode, TuningWidth};
24
25/// Common operations for SD/MMC peripherals.
26pub trait Common {
27    /// Associated error type for the SD/MMC trait.
28    type Error;
29
30    /// Gets the device [CardType].
31    fn card_type(&self) -> CardType;
32
33    /// Gets the device [CardMode].
34    fn card_mode(&self) -> CardMode;
35
36    /// Performs bus setup for the SD/MMC device.
37    fn setup_bus(&mut self) -> Result<(), Self::Error>;
38
39    /// Performs device initialization sequence.
40    fn init(&mut self) -> Result<(), Self::Error>;
41
42    /// Waits for the CMD line to reset (usually during power-up).
43    fn wait_for_reset(&mut self, reset: Reset, timeout: u64) -> Result<(), Self::Error>;
44
45    /// Waits for the busy signal to clear for maximum `timeout_us` microseconds.
46    fn wait_while_busy(&mut self, timout_us: u64) -> Result<(), Self::Error>;
47
48    /// Reads data from the MMC data lines.
49    fn read_data(&mut self, data: &mut [u8]) -> Result<(), Self::Error>;
50
51    /// Writes data to the MMC data lines.
52    fn write_data(&mut self, data: &[u8]) -> Result<(), Self::Error>;
53
54    /// Sets the sample phase for the MMC controller.
55    fn set_sample_phase(&mut self, sample_phase: u8);
56
57    /// Waits for the FIFO to indicate readiness for read/write operations.
58    fn fifo_ready(&self, fifo_status: FifoStatus) -> Result<(), Self::Error>;
59
60    /// Handles tuning block requests.
61    ///
62    /// For hosts:
63    ///
64    /// - requests the device to send a tuning block
65    ///
66    /// For devices:
67    ///
68    /// - sends the host the requested tuning block
69    fn send_tuning(&mut self, mode: TuningMode, width: TuningWidth) -> Result<(), Self::Error>;
70
71    /// Gets the interrupts status as a 32-bit bitfield.
72    fn interrupt(&self) -> u32;
73
74    /// Sets the interrupts based on a 32-bit bitfield.
75    fn set_interrupt(&mut self, int: u32);
76
77    /// Clear all interrupts.
78    fn clear_all_interrupt(&mut self);
79
80    /// Gets the response interrupts status as a 32-bit bitfield.
81    fn response_interrupt(&self) -> u32;
82
83    /// Sets the response interrupts based on a 32-bit bitfield.
84    fn set_response_interrupt(&mut self, int: u32);
85
86    /// Clear all interrupts.
87    fn clear_all_response_interrupt(&mut self);
88}
89
90/// Common operations for SD/MMC host peripherals.
91pub trait Host: Common {
92    /// Writes a SD/MMC command to the card.
93    fn write_command<C: Command>(&mut self, cmd: &C) -> Result<(), Self::Error>;
94
95    /// Reads a SD/MMC response based on the provided command argument.
96    ///
97    /// # Note
98    ///
99    /// `cmd` should match the last call to `write_command`.
100    fn read_response<C: Command, R: Response>(&mut self, cmd: &C) -> Result<R, Self::Error>;
101}
102
103/// Common operations for SD/MMC device peripherals.
104pub trait Device: Common {
105    /// Reads a SD/MMC command sent from the host.
106    fn read_command<C: Command>(&mut self) -> Result<C, Self::Error>;
107
108    /// Writes a SD/MMC response based on the previous command.
109    fn write_response<R: Response>(&mut self, response: &R) -> Result<(), Self::Error>;
110}