Skip to content

Commit 2e7dd74

Browse files
jonathanpallanteldruin
authored andcommitted
Add more debugging.
1 parent ded98b3 commit 2e7dd74

File tree

1 file changed

+34
-22
lines changed

1 file changed

+34
-22
lines changed

src/sdmmc.rs

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,14 @@ where
5757
CS: embedded_hal::digital::v2::OutputPin,
5858
<SPI as embedded_hal::blocking::spi::Transfer<u8>>::Error: core::fmt::Debug,
5959
{
60-
/// Create a new SD/MMC interface using a raw SPI interface.
60+
/// Create a new SD/MMC Card driver using a raw SPI interface.
61+
///
62+
/// Uses the default options.
6163
pub fn new(spi: SPI, cs: CS) -> SdCard<SPI, CS> {
62-
SdCard {
63-
inner: RefCell::new(SdCardInner {
64-
spi,
65-
cs,
66-
card_type: CardType::Unknown,
67-
options: AcquireOpts { require_crc: true },
68-
}),
69-
}
64+
Self::new_with_options(spi, cs, AcquireOpts::default())
7065
}
7166

72-
/// Construct a new SD/MMC interface, using the given options.
67+
/// Construct a new SD/MMC Card driver, using a raw SPI interface and the given options.
7368
pub fn new_with_options(spi: SPI, cs: CS, options: AcquireOpts) -> SdCard<SPI, CS> {
7469
SdCard {
7570
inner: RefCell::new(SdCardInner {
@@ -105,6 +100,14 @@ where
105100
inner.check_init()?;
106101
inner.erase_single_block_enabled()
107102
}
103+
104+
/// Mark the card as requiring a reset.
105+
///
106+
/// The next operation will assume the card has been freshly inserted.
107+
pub fn mark_card_uninit(&self) {
108+
let mut inner = self.inner.borrow_mut();
109+
inner.card_type = CardType::Unknown;
110+
}
108111
}
109112

110113
impl<SPI, CS> BlockDevice for SdCard<SPI, CS>
@@ -120,16 +123,23 @@ where
120123
&self,
121124
blocks: &mut [Block],
122125
start_block_idx: BlockIdx,
123-
_reason: &str,
126+
reason: &str,
124127
) -> Result<(), Self::Error> {
125128
let mut inner = self.inner.borrow_mut();
129+
debug!(
130+
"Read {} blocks @ {} for {}",
131+
blocks.len(),
132+
start_block_idx.0,
133+
reason
134+
);
126135
inner.check_init()?;
127-
inner.read(blocks, start_block_idx, _reason)
136+
inner.read(blocks, start_block_idx)
128137
}
129138

130139
/// Write one or more blocks, starting at the given block index.
131140
fn write(&self, blocks: &[Block], start_block_idx: BlockIdx) -> Result<(), Self::Error> {
132141
let mut inner = self.inner.borrow_mut();
142+
debug!("Writing {} blocks @ {}", blocks.len(), start_block_idx.0);
133143
inner.check_init()?;
134144
inner.write(blocks, start_block_idx)
135145
}
@@ -138,7 +148,7 @@ where
138148
fn num_blocks(&self) -> Result<BlockCount, Self::Error> {
139149
let mut inner = self.inner.borrow_mut();
140150
inner.check_init()?;
141-
inner.num_blocks()
151+
inner.card_size_blocks()
142152
}
143153
}
144154

@@ -164,12 +174,7 @@ where
164174
<SPI as embedded_hal::blocking::spi::Transfer<u8>>::Error: core::fmt::Debug,
165175
{
166176
/// Read one or more blocks, starting at the given block index.
167-
fn read(
168-
&mut self,
169-
blocks: &mut [Block],
170-
start_block_idx: BlockIdx,
171-
_reason: &str,
172-
) -> Result<(), Error> {
177+
fn read(&mut self, blocks: &mut [Block], start_block_idx: BlockIdx) -> Result<(), Error> {
173178
let start_idx = match self.card_type {
174179
CardType::SD1 | CardType::SD2 => start_block_idx.0 * 512,
175180
CardType::Sdhc => start_block_idx.0,
@@ -228,16 +233,23 @@ where
228233
}
229234

230235
/// Determine how many blocks this device can hold.
231-
fn num_blocks(&mut self) -> Result<BlockCount, Error> {
232-
let num_bytes = self.card_size_bytes()?;
233-
let num_blocks = (num_bytes / 512) as u32;
236+
fn card_size_blocks(&mut self) -> Result<BlockCount, Error> {
237+
let num_blocks = self.with_chip_select(|s| {
238+
let csd = s.read_csd()?;
239+
debug!("CSD: {:?}", csd);
240+
match csd {
241+
Csd::V1(ref contents) => Ok(contents.card_capacity_blocks()),
242+
Csd::V2(ref contents) => Ok(contents.card_capacity_blocks()),
243+
}
244+
})?;
234245
Ok(BlockCount(num_blocks))
235246
}
236247

237248
/// Return the usable size of this SD card in bytes.
238249
fn card_size_bytes(&mut self) -> Result<u64, Error> {
239250
self.with_chip_select(|s| {
240251
let csd = s.read_csd()?;
252+
debug!("CSD: {:?}", csd);
241253
match csd {
242254
Csd::V1(ref contents) => Ok(contents.card_capacity_bytes()),
243255
Csd::V2(ref contents) => Ok(contents.card_capacity_bytes()),

0 commit comments

Comments
 (0)