Skip to content

Commit 4311956

Browse files
authored
Merge pull request #226 from robamu/sdcard-module-improvements
improvements for SD card module
2 parents c1267c1 + 6f84c7f commit 4311956

5 files changed

Lines changed: 26 additions & 22 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ The format is based on [Keep a Changelog] and this project adheres to [Semantic
1818
- Updated directory iterator callback, to allow bailing out early (breaking change)
1919
- `crc7` function now returns the actual CRC7 value without left-shifting by and XOR-ing by/with 1.
2020
- Bumped `embedded-io` to 0.7
21+
- `CardType::SDHC` renamed to `CardType::SdhcSdxc`
22+
- `CardType` is now inside the `sdcard` module
2123

2224
### Added
2325

src/sdcard/cid.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::sdcard::crc7;
66

77
/// Checksum is invalid.
88
#[derive(Debug, PartialEq, Eq, Copy, Clone, thiserror::Error)]
9+
#[cfg_attr(feature = "defmt-log", derive(defmt::Format))]
910
#[error("checksum is invalid")]
1011
pub struct ChecksumInvalidError;
1112

src/sdcard/csd.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub enum Csd {
1616

1717
/// The CSD structure field is invalid.
1818
#[derive(Debug, PartialEq, Eq, Copy, Clone, thiserror::Error)]
19+
#[cfg_attr(feature = "defmt-log", derive(defmt::Format))]
1920
pub enum CsdCreationError {
2021
/// Invalid CSD structure field.
2122
#[error("invalid CSD structure field")]

src/sdcard/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,24 @@ pub mod cid;
3333
pub mod csd;
3434
pub mod spi;
3535

36+
/// The different types of card we support.
37+
#[cfg_attr(feature = "defmt-log", derive(defmt::Format))]
38+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
39+
pub enum CardType {
40+
/// An standard-capacity SD Card supporting v1.x of the standard.
41+
///
42+
/// Uses byte-addressing internally, so limited to 2GiB in size.
43+
SD1,
44+
/// An standard-capacity SD Card supporting v2.x of the standard.
45+
///
46+
/// Uses byte-addressing internally, so limited to 2GiB in size.
47+
SD2,
48+
/// An high-capacity 'SDHC' Card or an extended-capacity 'SDXC' card.
49+
///
50+
/// Uses block-addressing internally to support capacities above 2GiB.
51+
SdhcSdxc,
52+
}
53+
3654
// Possible errors the SD card can return
3755
/// Card indicates last operation was a success
3856
pub const ERROR_OK: u8 = 0x00;

src/sdcard/spi.rs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ where
203203
fn read(&mut self, blocks: &mut [Block], start_block_idx: BlockIdx) -> Result<(), Error> {
204204
let start_idx = match self.card_type {
205205
Some(CardType::SD1 | CardType::SD2) => start_block_idx.0 * 512,
206-
Some(CardType::SDHC) => start_block_idx.0,
206+
Some(CardType::SdhcSdxc) => start_block_idx.0,
207207
None => return Err(Error::CardNotFound),
208208
};
209209

@@ -227,7 +227,7 @@ where
227227
fn write(&mut self, blocks: &[Block], start_block_idx: BlockIdx) -> Result<(), Error> {
228228
let start_idx = match self.card_type {
229229
Some(CardType::SD1 | CardType::SD2) => start_block_idx.0 * 512,
230-
Some(CardType::SDHC) => start_block_idx.0,
230+
Some(CardType::SdhcSdxc) => start_block_idx.0,
231231
None => return Err(Error::CardNotFound),
232232
};
233233
if blocks.len() == 1 {
@@ -293,7 +293,7 @@ where
293293
self.read_data(&mut csd_raw)?;
294294
Ok(csd::Csd::V1(csd::CsdV1::from_be_bytes(&csd_raw)))
295295
}
296-
Some(CardType::SD2 | CardType::SDHC) => {
296+
Some(CardType::SD2 | CardType::SdhcSdxc) => {
297297
if self.card_command(CMD9, 0)? != 0 {
298298
return Err(Error::RegisterReadError);
299299
}
@@ -443,7 +443,7 @@ where
443443
let mut buffer = [0xFF; 4];
444444
s.transfer_bytes(&mut buffer)?;
445445
if (buffer[0] & 0xC0) == 0xC0 {
446-
card_type = CardType::SDHC;
446+
card_type = CardType::SdhcSdxc;
447447
}
448448
// Ignore the other three bytes
449449
}
@@ -630,24 +630,6 @@ impl core::fmt::Display for Error {
630630

631631
impl core::error::Error for Error {}
632632

633-
/// The different types of card we support.
634-
#[cfg_attr(feature = "defmt-log", derive(defmt::Format))]
635-
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
636-
pub enum CardType {
637-
/// An standard-capacity SD Card supporting v1.x of the standard.
638-
///
639-
/// Uses byte-addressing internally, so limited to 2GiB in size.
640-
SD1,
641-
/// An standard-capacity SD Card supporting v2.x of the standard.
642-
///
643-
/// Uses byte-addressing internally, so limited to 2GiB in size.
644-
SD2,
645-
/// An high-capacity 'SDHC' Card.
646-
///
647-
/// Uses block-addressing internally to support capacities above 2GiB.
648-
SDHC,
649-
}
650-
651633
/// This an object you can use to busy-wait with a timeout.
652634
///
653635
/// Will let you call `delay` up to `max_retries` times before `delay` returns

0 commit comments

Comments
 (0)