Skip to content

Commit 4d842eb

Browse files
authored
Merge pull request #222 from robamu/improve-csd-parsing
Improve CSD parsing
2 parents 3389fee + 3ebcc89 commit 4d842eb

5 files changed

Lines changed: 771 additions & 177 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ The format is based on [Keep a Changelog] and this project adheres to [Semantic
1616
- Renamed `delete_file_in_dir` to `delete_entry_in_dir` because it can now delete empty directories (breaking change)
1717
- Added `open_long_name_file_in_dir` API, to open files using their Long File Name
1818
- Updated directory iterator callback, to allow bailing out early (breaking change)
19+
- `crc7` function now returns the actual CRC7 value without left-shifting by and XOR-ing by/with 1.
20+
21+
### Added
22+
23+
- Added `CsdV3` data structure.
24+
- `Csd::new` constructor which creates the CSD from a raw `&[u8; 16]` and also performs a CRC7
25+
check.
26+
- Added `verify_crc7` function on CSD structures.
1927

2028
## [Version 0.9.0] - 2025-06-08
2129

Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ byteorder = {version = "1", default-features = false}
1818
defmt = {version = "1.0.1", optional = true}
1919
embedded-hal = "1.0.0"
2020
embedded-io = "0.6.1"
21+
bitbybit = "2"
22+
arbitrary-int = "2"
23+
bitflags = "2"
2124
heapless = "0.9.1"
2225
log = {version = "0.4", default-features = false, optional = true}
26+
thiserror = {version="2", default-features = false}
2327

2428
[dev-dependencies]
2529
chrono = "0.4"
@@ -31,6 +35,6 @@ sha2 = "0.10"
3135

3236
[features]
3337
default = ["log"]
34-
defmt-log = ["dep:defmt"]
38+
defmt-log = ["dep:defmt", "arbitrary-int/defmt"]
3539
log = ["dep:log"]
3640

src/sdcard/mod.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ where
278278
let num_blocks = match csd {
279279
Csd::V1(ref contents) => contents.card_capacity_blocks(),
280280
Csd::V2(ref contents) => contents.card_capacity_blocks(),
281+
Csd::V3(ref contents) => contents.card_capacity_blocks(),
281282
};
282283
Ok(BlockCount(num_blocks))
283284
}
@@ -289,6 +290,7 @@ where
289290
match csd {
290291
Csd::V1(ref contents) => Ok(contents.card_capacity_bytes()),
291292
Csd::V2(ref contents) => Ok(contents.card_capacity_bytes()),
293+
Csd::V3(ref contents) => Ok(contents.card_capacity_bytes()),
292294
}
293295
}
294296

@@ -298,27 +300,27 @@ where
298300
match csd {
299301
Csd::V1(ref contents) => Ok(contents.erase_single_block_enabled()),
300302
Csd::V2(ref contents) => Ok(contents.erase_single_block_enabled()),
303+
Csd::V3(ref contents) => Ok(contents.erase_single_block_enabled()),
301304
}
302305
}
303306

304307
/// Read the 'card specific data' block.
305308
fn read_csd(&mut self) -> Result<Csd, Error> {
309+
let mut csd_raw: [u8; 16] = [0; 16];
306310
match self.card_type {
307311
Some(CardType::SD1) => {
308-
let mut csd = CsdV1::new();
309312
if self.card_command(CMD9, 0)? != 0 {
310313
return Err(Error::RegisterReadError);
311314
}
312-
self.read_data(&mut csd.data)?;
313-
Ok(Csd::V1(csd))
315+
self.read_data(&mut csd_raw)?;
316+
Ok(Csd::V1(CsdV1::from_be_bytes(&csd_raw)))
314317
}
315318
Some(CardType::SD2 | CardType::SDHC) => {
316-
let mut csd = CsdV2::new();
317319
if self.card_command(CMD9, 0)? != 0 {
318320
return Err(Error::RegisterReadError);
319321
}
320-
self.read_data(&mut csd.data)?;
321-
Ok(Csd::V2(csd))
322+
self.read_data(&mut csd_raw)?;
323+
Ok(Csd::V2(CsdV2::from_be_bytes(&csd_raw)))
322324
}
323325
None => Err(Error::CardNotFound),
324326
}

0 commit comments

Comments
 (0)