sdmmc_driver/lib.rs
1#![doc = include_str!("../README.md")]
2
3use sdmmc::class0::{cmd0, cmd1, cmd4, cmd6, cmd9, cmd10};
4use sdmmc::class8::acmd6::BusWidth;
5use sdmmc::register::Ocr;
6use sdmmc::response::Response;
7
8mod bus_mode;
9mod config;
10mod power_mode;
11mod timing;
12
13pub use bus_mode::*;
14pub use config::*;
15pub use power_mode::*;
16pub use timing::*;
17
18pub mod result;
19
20#[cfg(feature = "jh7110")]
21pub mod jh7110;
22
23use result::Result;
24
25/// Represents the common operations for handling SD/MMC cards.
26///
27/// Includes functions for:
28///
29/// - initializing the card
30/// - card detection
31/// - reading/writing a card
32/// - locking/unlocking a card
33pub trait SdMmc {
34 /// Initializes a SD/MMC card.
35 ///
36 /// Handles the configuration of clocks, block size, speed mode, etc.
37 fn init(&mut self, config: &mut MmcConfig, ocr: Ocr) -> Result<()>;
38
39 /// Re-initializes a SD/MMC card.
40 ///
41 /// Handles configuration of a card that was already previously initialized.
42 fn reinit(&mut self, config: &mut MmcConfig, ocr: Ocr) -> Result<()>;
43
44 /// Detects if a card is present.
45 ///
46 /// For non-removable cards, implementations should return `true`.
47 fn detect(&self) -> Result<bool>;
48
49 /// Reads data from the SD/MMC card.
50 ///
51 /// Returns the number of bytes read from the card.
52 fn read(&mut self, addr: u32, buf: &mut [u8]) -> Result<usize>;
53
54 /// Writes data to the SD/MMC card.
55 fn write(&mut self, addr: u32, buf: &[u8]) -> Result<()>;
56
57 /// Locks the card into read-only mode.
58 fn lock(&mut self) -> Result<()>;
59
60 /// Unlocks the card for read-write operations.
61 fn unlock(&mut self) -> Result<()>;
62}
63
64/// Common operations for configuring, controlling, and reading registers from SD/MMC cards.
65pub trait SdMmcOps {
66 /// Sends the `GO_IDLE` command, and waits for the response from the card.
67 fn go_idle(&mut self, arg: cmd0::Argument) -> Result<Response>;
68
69 /// Sends the `SEND_OP_COND` command, which requests the card to send the contents of the `OP_COND` register.
70 fn send_op_cond(&mut self, arg: cmd1::Argument) -> Result<Response>;
71
72 /// Sends the `SET_RELATIVE_ADDR` command.
73 fn set_relative_address(&mut self) -> Result<Response>;
74
75 /// Sends the `SET_DSR` command, which sets the `DSR` register on cards that support the register.
76 fn set_dsr(&mut self, arg: cmd4::Argument) -> Result<Response>;
77
78 /// Sends the `SWITCH` command, which is used to program the `EXT_CSD` card register.
79 fn switch(&mut self, arg: cmd6::Argument) -> Result<Response>;
80
81 /// Sends the `SELECT_CARD` command, which selects the active card.
82 fn select_card(&mut self) -> Result<Response>;
83
84 /// Sends the `SEND_EXT_CSD` command, which requests the card to send the contents of the `EXT_CSD` register.
85 fn send_ext_csd(&mut self) -> Result<Response>;
86
87 /// Sends the `SEND_CSD` command, which requests the card to send the contents of the `CSD` register.
88 fn send_csd(&mut self, arg: cmd9::Argument) -> Result<Response>;
89
90 /// Sends the `SEND_CID` command, which requests the card to send the contents of the `CID` register.
91 fn send_cid(&mut self, arg: cmd10::Argument) -> Result<Response>;
92
93 /// Sends the `VOLTAGE_SWITCH` command, which requests a change for card supply voltage.
94 fn voltage_switch(&mut self) -> Result<Response>;
95
96 /// Sends the hardware configuration settings to the SD/MMC card.
97 fn set_config(&mut self, config: &MmcConfig) -> Result<()>;
98
99 /// Sends the `LOCK_UNLOCK` command, which locks or unlocks the device.
100 fn lock_unlock(&mut self) -> Result<Response>;
101
102 /// Sends the `STOP_TRANSMISSION` command to the device.
103 fn stop_transmission(&mut self) -> Result<Response>;
104
105 /// Sends the `SET_BLOCK_COUNT` command, which sets the number of blocks for future read/write block commands.
106 fn set_block_count(&mut self, count: u32) -> Result<Response>;
107
108 /// Sends the `SET_BLOCKLEN` command, which sets the block length for future read/write block commands.
109 fn set_block_length(&mut self, block_len: u32) -> Result<Response>;
110
111 /// Sends the `READ_BLOCK` command, which read a block of data from the device.
112 fn read_block(&mut self, address: u32) -> Result<Response>;
113
114 /// Sends the `READ_MULTIPLE_BLOCK` command, which begins reading blocks from the device.
115 ///
116 /// The device continues reading blocks until:
117 ///
118 /// - a `STOP_TRANSMISSION` command is sent
119 /// - the number of blocks in `SET_BLOCK_COUNT` has been sent
120 fn read_multiple_blocks(&mut self, address: u32) -> Result<Response>;
121
122 /// Sends the `WRITE_BLOCK` command, which writes a block of data to the device.
123 fn write_block(&mut self, address: u32) -> Result<Response>;
124
125 /// Sends the `WRITE_MULTIPLE_BLOCK` command, which begins sending blocks to the device.
126 ///
127 /// The device continues writing blocks until:
128 ///
129 /// - a `STOP_TRANSMISSION` command is sent
130 /// - the number of blocks in `SET_BLOCK_COUNT` has been sent
131 fn write_multiple_blocks(&mut self, address: u32) -> Result<Response>;
132}