Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ The format is based on [Keep a Changelog] and this project adheres to [Semantic
- Updated directory iterator callback, to allow bailing out early (breaking change)
- `crc7` function now returns the actual CRC7 value without left-shifting by and XOR-ing by/with 1.
- Bumped `embedded-io` to 0.7
- `CardType::SDHC` renamed to `CardType::SdhcSdxc`
- `CardType` is now inside the `sdcard` module

### Added

Expand Down
1 change: 1 addition & 0 deletions src/sdcard/cid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::sdcard::crc7;

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

Expand Down
1 change: 1 addition & 0 deletions src/sdcard/csd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub enum Csd {

/// The CSD structure field is invalid.
#[derive(Debug, PartialEq, Eq, Copy, Clone, thiserror::Error)]
#[cfg_attr(feature = "defmt-log", derive(defmt::Format))]
pub enum CsdCreationError {
/// Invalid CSD structure field.
#[error("invalid CSD structure field")]
Expand Down
18 changes: 18 additions & 0 deletions src/sdcard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ pub mod cid;
pub mod csd;
pub mod spi;

/// The different types of card we support.
#[cfg_attr(feature = "defmt-log", derive(defmt::Format))]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum CardType {
/// An standard-capacity SD Card supporting v1.x of the standard.
///
/// Uses byte-addressing internally, so limited to 2GiB in size.
SD1,
/// An standard-capacity SD Card supporting v2.x of the standard.
///
/// Uses byte-addressing internally, so limited to 2GiB in size.
SD2,
/// An high-capacity 'SDHC' Card or an extended-capacity 'SDXC' card.
///
/// Uses block-addressing internally to support capacities above 2GiB.
SdhcSdxc,
}

// Possible errors the SD card can return
/// Card indicates last operation was a success
pub const ERROR_OK: u8 = 0x00;
Expand Down
26 changes: 4 additions & 22 deletions src/sdcard/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ where
fn read(&mut self, blocks: &mut [Block], start_block_idx: BlockIdx) -> Result<(), Error> {
let start_idx = match self.card_type {
Some(CardType::SD1 | CardType::SD2) => start_block_idx.0 * 512,
Some(CardType::SDHC) => start_block_idx.0,
Some(CardType::SdhcSdxc) => start_block_idx.0,
None => return Err(Error::CardNotFound),
};

Expand All @@ -227,7 +227,7 @@ where
fn write(&mut self, blocks: &[Block], start_block_idx: BlockIdx) -> Result<(), Error> {
let start_idx = match self.card_type {
Some(CardType::SD1 | CardType::SD2) => start_block_idx.0 * 512,
Some(CardType::SDHC) => start_block_idx.0,
Some(CardType::SdhcSdxc) => start_block_idx.0,
None => return Err(Error::CardNotFound),
};
if blocks.len() == 1 {
Expand Down Expand Up @@ -293,7 +293,7 @@ where
self.read_data(&mut csd_raw)?;
Ok(csd::Csd::V1(csd::CsdV1::from_be_bytes(&csd_raw)))
}
Some(CardType::SD2 | CardType::SDHC) => {
Some(CardType::SD2 | CardType::SdhcSdxc) => {
if self.card_command(CMD9, 0)? != 0 {
return Err(Error::RegisterReadError);
}
Expand Down Expand Up @@ -443,7 +443,7 @@ where
let mut buffer = [0xFF; 4];
s.transfer_bytes(&mut buffer)?;
if (buffer[0] & 0xC0) == 0xC0 {
card_type = CardType::SDHC;
card_type = CardType::SdhcSdxc;
}
// Ignore the other three bytes
}
Expand Down Expand Up @@ -630,24 +630,6 @@ impl core::fmt::Display for Error {

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

/// The different types of card we support.
#[cfg_attr(feature = "defmt-log", derive(defmt::Format))]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum CardType {
/// An standard-capacity SD Card supporting v1.x of the standard.
///
/// Uses byte-addressing internally, so limited to 2GiB in size.
SD1,
/// An standard-capacity SD Card supporting v2.x of the standard.
///
/// Uses byte-addressing internally, so limited to 2GiB in size.
SD2,
/// An high-capacity 'SDHC' Card.
///
/// Uses block-addressing internally to support capacities above 2GiB.
SDHC,
}

/// This an object you can use to busy-wait with a timeout.
///
/// Will let you call `delay` up to `max_retries` times before `delay` returns
Expand Down