Skip to content

Commit 3aa510f

Browse files
Clarify what inits that card and what doesn't.
Closes #87 Closes #90
1 parent ccebb44 commit 3aa510f

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed

src/sdcard/mod.rs

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,18 @@ where
4949
{
5050
/// Create a new SD/MMC Card driver using a raw SPI interface.
5151
///
52+
/// The card will not be initialised at this time. Initialisation is
53+
/// deferred until a method is called on the object.
54+
///
5255
/// Uses the default options.
5356
pub fn new(spi: SPI, cs: CS, delayer: DELAYER) -> SdCard<SPI, CS, DELAYER> {
5457
Self::new_with_options(spi, cs, delayer, AcquireOpts::default())
5558
}
5659

5760
/// Construct a new SD/MMC Card driver, using a raw SPI interface and the given options.
61+
///
62+
/// The card will not be initialised at this time. Initialisation is
63+
/// deferred until a method is called on the object.
5864
pub fn new_with_options(
5965
spi: SPI,
6066
cs: CS,
@@ -72,8 +78,13 @@ where
7278
}
7379
}
7480

75-
/// Get a temporary borrow on the underlying SPI device. Useful if you
76-
/// need to re-clock the SPI.
81+
/// Get a temporary borrow on the underlying SPI device.
82+
///
83+
/// The given closure will be called exactly once, and will be passed a
84+
/// mutable reference to the underlying SPI object.
85+
///
86+
/// Useful if you need to re-clock the SPI, but does not perform card
87+
/// initialisation.
7788
pub fn spi<T, F>(&self, func: F) -> T
7889
where
7990
F: FnOnce(&mut SPI) -> T,
@@ -83,13 +94,17 @@ where
8394
}
8495

8596
/// Return the usable size of this SD card in bytes.
97+
///
98+
/// This will trigger card (re-)initialisation.
8699
pub fn num_bytes(&self) -> Result<u64, Error> {
87100
let mut inner = self.inner.borrow_mut();
88101
inner.check_init()?;
89102
inner.num_bytes()
90103
}
91104

92105
/// Can this card erase single blocks?
106+
///
107+
/// This will trigger card (re-)initialisation.
93108
pub fn erase_single_block_enabled(&self) -> Result<bool, Error> {
94109
let mut inner = self.inner.borrow_mut();
95110
inner.check_init()?;
@@ -105,17 +120,30 @@ where
105120
}
106121

107122
/// Get the card type.
123+
///
124+
/// This will trigger card (re-)initialisation.
108125
pub fn get_card_type(&self) -> Option<CardType> {
109-
let inner = self.inner.borrow();
126+
let mut inner = self.inner.borrow_mut();
127+
inner.check_init().ok()?;
110128
inner.card_type
111129
}
112130

113131
/// Tell the driver the card has been initialised.
114132
///
133+
/// This is here in case you were previously using the SD Card, and then a
134+
/// previous instance of this object got destroyed but you know for certain
135+
/// the SD Card remained powered up and initialised, and you'd just like to
136+
/// read/write to/from the card again without going through the
137+
/// initialisation sequence again.
138+
///
115139
/// # Safety
116140
///
117-
/// Only do this if the card has actually been initialised and is of the
118-
/// indicated type, otherwise corruption may occur.
141+
/// Only do this if the SD Card has actually been initialised. That is, if
142+
/// you have been through the card initialisation sequence as specified in
143+
/// the SD Card Specification by sending each appropriate command in turn,
144+
/// either manually or using another variable of this [`SdCard`]. The card
145+
/// must also be of the indicated type. Failure to uphold this will cause
146+
/// data corruption.
119147
pub unsafe fn mark_card_as_init(&self, card_type: CardType) {
120148
let mut inner = self.inner.borrow_mut();
121149
inner.card_type = Some(card_type);
@@ -133,6 +161,8 @@ where
133161
type Error = Error;
134162

135163
/// Read one or more blocks, starting at the given block index.
164+
///
165+
/// This will trigger card (re-)initialisation.
136166
fn read(
137167
&self,
138168
blocks: &mut [Block],
@@ -151,6 +181,8 @@ where
151181
}
152182

153183
/// Write one or more blocks, starting at the given block index.
184+
///
185+
/// This will trigger card (re-)initialisation.
154186
fn write(&self, blocks: &[Block], start_block_idx: BlockIdx) -> Result<(), Self::Error> {
155187
let mut inner = self.inner.borrow_mut();
156188
debug!("Writing {} blocks @ {}", blocks.len(), start_block_idx.0);
@@ -159,6 +191,8 @@ where
159191
}
160192

161193
/// Determine how many blocks this device can hold.
194+
///
195+
/// This will trigger card (re-)initialisation.
162196
fn num_blocks(&self) -> Result<BlockCount, Self::Error> {
163197
let mut inner = self.inner.borrow_mut();
164198
inner.check_init()?;

0 commit comments

Comments
 (0)