Skip to content

Commit

Permalink
add sdcard sample (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
milewski committed Aug 12, 2023
1 parent cbf7721 commit 146ce22
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ rust-version = "1.71"

[workspace.dependencies]
esp-idf-sys = { version = "0.33.1", features = ["native","binstart"] }
esp-idf-hal = { version = "0.41.2", features = ["critical-section"] }
esp-idf-hal = { git = "https://github.com/esp-rs/esp-idf-hal", branch = "master", features = ["critical-section"] }
anyhow = "1.0.72"
embuild = "0.31.2"

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ My personal journey into embedded development with Lilygo T-Display S3 Microcont
- [Rotary Encoder](./esp32/rotary-encoder)
- [Line Tracking Sensor](./esp32/line-tracking-sensor)
- [Passive Buzzer](./esp32/passive-buzzer)
- [Micro SD Card Adapter](./esp32/micro-sdcard)

## Instructions

Expand Down
13 changes: 13 additions & 0 deletions esp32/micro-sdcard/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "micro-sdcard"
version.workspace = true
edition.workspace = true

[dependencies]
esp-idf-sys.workspace = true
esp-idf-hal.workspace = true
anyhow.workspace = true
embedded-sdmmc = { version = "0.5.0" }

[build-dependencies]
embuild.workspace = true
15 changes: 15 additions & 0 deletions esp32/micro-sdcard/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Micro SD Card Adapter

Read / Write data to an SDCARD Module.

## Features

### How to Run

To run the example, use the following command:

```bash
cargo run -p micro-sdcard
```

### Notes
6 changes: 6 additions & 0 deletions esp32/micro-sdcard/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Necessary because of this issue: https://github.com/rust-lang/cargo/issues/9641
fn main() -> Result<(), Box<dyn std::error::Error>> {
embuild::build::CfgArgs::output_propagated("ESP_IDF")?;
embuild::build::LinkArgs::output_propagated("ESP_IDF")?;
Ok(())
}
108 changes: 108 additions & 0 deletions esp32/micro-sdcard/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
use std::fmt::{Display, Formatter};

use embedded_sdmmc::{BlockDevice, File, Mode, SdCard, TimeSource, Timestamp, Volume, VolumeIdx, VolumeManager};
use esp_idf_hal::delay::FreeRtos;
use esp_idf_hal::gpio::{Gpio10, PinDriver};
use esp_idf_hal::peripherals::Peripherals;
use esp_idf_hal::spi::{Dma, SpiConfig, SpiDeviceDriver, SpiDriver};
use esp_idf_hal::spi::config::DriverConfig;

pub struct SdMmcClock;

impl TimeSource for SdMmcClock {
fn get_timestamp(&self) -> Timestamp {
Timestamp {
year_since_1970: 0,
zero_indexed_month: 0,
zero_indexed_day: 0,
hours: 0,
minutes: 0,
seconds: 0,
}
}
}

#[derive(Debug)]
enum CustomError {
UnableToTakePeripherals,
UnableToInitializeCSPin,
UnableToGetVolume,
UnableToOpenDirectory,
}

impl std::error::Error for CustomError {}

impl Display for CustomError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "Error: {:?}", self)
}
}

fn main() -> anyhow::Result<()> {
esp_idf_sys::link_patches();

let peripherals = Peripherals::take().ok_or(CustomError::UnableToTakePeripherals)?;

let cs = peripherals.pins.gpio10;
let sck = peripherals.pins.gpio3;
let mosi = peripherals.pins.gpio2;
let miso = peripherals.pins.gpio1;

let driver = SpiDriver::new(
peripherals.spi2,
sck,
mosi,
Some(miso),
&DriverConfig::default().dma(Dma::Disabled),
)?;

let config = SpiConfig::default();
let device = SpiDeviceDriver::new(driver, Option::<Gpio10>::None, &config)?;
let cs_pin = PinDriver::output(cs)?;
let sdcard = SdCard::new(device, cs_pin, FreeRtos);

println!("Card size {:?}", sdcard.num_bytes());

let mut volume_manager = VolumeManager::new(sdcard, SdMmcClock);
let mut volume0 = volume_manager.get_volume(VolumeIdx(0)).unwrap();

let root_dir = volume_manager.open_root_dir(&volume0).unwrap();

let mut my_entry = None;

volume_manager
.iterate_dir(&volume0, &root_dir, |entry| {
if entry.name.extension() == b"TXT" {
my_entry = Some(entry.clone());
}
})
.unwrap();

if let Some(entry) = my_entry {
let mut file = volume_manager
.open_dir_entry(&mut volume0, entry, Mode::ReadOnly)
.unwrap();

let content = read_file(&mut volume_manager, volume0, &mut file);

println!("{:?}", String::from_utf8(content))
}

loop {
FreeRtos::delay_ms(100);
}
}

fn read_file(volume_manager: &mut VolumeManager<impl BlockDevice, impl TimeSource>, volume: Volume, file: &mut File) -> Vec<u8> {
let mut content = vec![];

// While the end of the file is not reach, create chunks of 32 bytes and fill it in with the incoming data
while !file.eof() {
let mut buffer = [0u8; 32];
let num_read = volume_manager.read(&volume, file, &mut buffer).unwrap();

content.extend_from_slice(&buffer[0..num_read]);
}

content
}

0 comments on commit 146ce22

Please sign in to comment.