11 releases (7 breaking)

0.8.1 Nov 3, 2024
0.8.0 Jul 12, 2024
0.7.0 Feb 9, 2024
0.6.0 Oct 20, 2023
0.1.1 Dec 23, 2018

#77 in Embedded development

Download history 1488/week @ 2024-08-22 684/week @ 2024-08-29 753/week @ 2024-09-05 822/week @ 2024-09-12 573/week @ 2024-09-19 1083/week @ 2024-09-26 384/week @ 2024-10-03 441/week @ 2024-10-10 1201/week @ 2024-10-17 1173/week @ 2024-10-24 735/week @ 2024-10-31 1284/week @ 2024-11-07 656/week @ 2024-11-14 734/week @ 2024-11-21 1173/week @ 2024-11-28 1216/week @ 2024-12-05

3,875 downloads per month
Used in 18 crates

MIT/Apache

265KB
5K SLoC

Embedded SD/MMC crates.io Documentation

This crate is intended to allow you to read/write files on a FAT formatted SD card on your Rust Embedded device, as easily as using the SdFat Arduino library. It is written in pure-Rust, is #![no_std] and does not use alloc or collections to keep the memory footprint low. In the first instance it is designed for readability and simplicity over performance.

Using the crate

You will need something that implements the BlockDevice trait, which can read and write the 512-byte blocks (or sectors) from your card. If you were to implement this over USB Mass Storage, there's no reason this crate couldn't work with a USB Thumb Drive, but we only supply a BlockDevice suitable for reading SD and SDHC cards over SPI.

// Build an SD Card interface out of an SPI device, a chip-select pin and the delay object
let sdcard = embedded_sdmmc::SdCard::new(sdmmc_spi, delay);
// Get the card size (this also triggers card initialisation because it's not been done yet)
println!("Card size is {} bytes", sdcard.num_bytes()?);
// Now let's look for volumes (also known as partitions) on our block device.
// To do this we need a Volume Manager. It will take ownership of the block device.
let mut volume_mgr = embedded_sdmmc::VolumeManager::new(sdcard, time_source);
// Try and access Volume 0 (i.e. the first partition).
// The volume object holds information about the filesystem on that volume.
let mut volume0 = volume_mgr.open_volume(embedded_sdmmc::VolumeIdx(0))?;
println!("Volume 0: {:?}", volume0);
// Open the root directory (mutably borrows from the volume).
let mut root_dir = volume0.open_root_dir()?;
// Open a file called "MY_FILE.TXT" in the root directory
// This mutably borrows the directory.
let mut my_file = root_dir.open_file_in_dir("MY_FILE.TXT", embedded_sdmmc::Mode::ReadOnly)?;
// Print the contents of the file, assuming it's in ISO-8859-1 encoding
while !my_file.is_eof() {
    let mut buffer = [0u8; 32];
    let num_read = my_file.read(&mut buffer)?;
    for b in &buffer[0..num_read] {
        print!("{}", *b as char);
    }
}

Open directories and files

By default the VolumeManager will initialize with a maximum number of 4 open directories, files and volumes. This can be customized by specifying the MAX_DIR, MAX_FILES and MAX_VOLUMES generic consts of the VolumeManager:

// Create a volume manager with a maximum of 6 open directories, 12 open files, and 4 volumes (or partitions)
let mut cont: VolumeManager<_, _, 6, 12, 4> = VolumeManager::new_with_limits(block, time_source);

Supported features

  • Open files in all supported methods from an open directory
  • Open an arbitrary number of directories and files
  • Read data from open files
  • Write data to open files
  • Close files
  • Delete files
  • Iterate root directory
  • Iterate sub-directories
  • Log over defmt or the common log interface (feature flags).

No-std usage

This repository houses no examples for no-std usage, however you can check out the following examples:

Todo List (PRs welcome!)

  • Create new dirs
  • Delete (empty) directories
  • Handle MS-DOS /path/foo/bar.txt style paths.

Changelog

The changelog has moved to CHANGELOG.md

License

Licensed under either of

at your option.

Copyright notices are stored in the NOTICE file.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~540–770KB
~15K SLoC