aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/rp/Cargo.toml1
-rw-r--r--examples/rp/src/bin/spi_sdmmc.rs80
2 files changed, 81 insertions, 0 deletions
diff --git a/examples/rp/Cargo.toml b/examples/rp/Cargo.toml
index 8162e4dcb..73d19c28b 100644
--- a/examples/rp/Cargo.toml
+++ b/examples/rp/Cargo.toml
@@ -49,6 +49,7 @@ log = "0.4"
49pio-proc = "0.2" 49pio-proc = "0.2"
50pio = "0.2.1" 50pio = "0.2.1"
51rand = { version = "0.8.5", default-features = false } 51rand = { version = "0.8.5", default-features = false }
52embedded-sdmmc = "0.7.0"
52 53
53[profile.release] 54[profile.release]
54debug = 2 55debug = 2
diff --git a/examples/rp/src/bin/spi_sdmmc.rs b/examples/rp/src/bin/spi_sdmmc.rs
new file mode 100644
index 000000000..0912490d8
--- /dev/null
+++ b/examples/rp/src/bin/spi_sdmmc.rs
@@ -0,0 +1,80 @@
1//! This example shows how to use `embedded-sdmmc` with the RP2040 chip, over SPI.
2//!
3//! The example will attempt to read a file `MY_FILE.TXT` from the root directory
4//! of the SD card and print its contents.
5
6#![no_std]
7#![no_main]
8
9use defmt::*;
10use embassy_embedded_hal::SetConfig;
11use embassy_executor::Spawner;
12use embassy_rp::spi::Spi;
13use embassy_rp::{gpio, spi};
14use embedded_hal_bus::spi::ExclusiveDevice;
15use embedded_sdmmc::sdcard::{DummyCsPin, SdCard};
16use gpio::{Level, Output};
17use {defmt_rtt as _, panic_probe as _};
18
19struct DummyTimesource();
20
21impl embedded_sdmmc::TimeSource for DummyTimesource {
22 fn get_timestamp(&self) -> embedded_sdmmc::Timestamp {
23 embedded_sdmmc::Timestamp {
24 year_since_1970: 0,
25 zero_indexed_month: 0,
26 zero_indexed_day: 0,
27 hours: 0,
28 minutes: 0,
29 seconds: 0,
30 }
31 }
32}
33
34#[embassy_executor::main]
35async fn main(_spawner: Spawner) {
36 let p = embassy_rp::init(Default::default());
37
38 // SPI clock needs to be running at <= 400kHz during initialization
39 let mut config = spi::Config::default();
40 config.frequency = 400_000;
41 let spi = Spi::new_blocking(p.SPI1, p.PIN_10, p.PIN_11, p.PIN_12, config);
42 // Use a dummy cs pin here, for embedded-hal SpiDevice compatibility reasons
43 let spi_dev = ExclusiveDevice::new_no_delay(spi, DummyCsPin);
44 // Real cs pin
45 let cs = Output::new(p.PIN_16, Level::High);
46
47 let sdcard = SdCard::new(spi_dev, cs, embassy_time::Delay);
48 info!("Card size is {} bytes", sdcard.num_bytes().unwrap());
49
50 // Now that the card is initialized, the SPI clock can go faster
51 let mut config = spi::Config::default();
52 config.frequency = 16_000_000;
53 sdcard.spi(|dev| dev.bus_mut().set_config(&config)).ok();
54
55 // Now let's look for volumes (also known as partitions) on our block device.
56 // To do this we need a Volume Manager. It will take ownership of the block device.
57 let mut volume_mgr = embedded_sdmmc::VolumeManager::new(sdcard, DummyTimesource());
58
59 // Try and access Volume 0 (i.e. the first partition).
60 // The volume object holds information about the filesystem on that volume.
61 let mut volume0 = volume_mgr.open_volume(embedded_sdmmc::VolumeIdx(0)).unwrap();
62 info!("Volume 0: {:?}", defmt::Debug2Format(&volume0));
63
64 // Open the root directory (mutably borrows from the volume).
65 let mut root_dir = volume0.open_root_dir().unwrap();
66
67 // Open a file called "MY_FILE.TXT" in the root directory
68 // This mutably borrows the directory.
69 let mut my_file = root_dir
70 .open_file_in_dir("MY_FILE.TXT", embedded_sdmmc::Mode::ReadOnly)
71 .unwrap();
72
73 // Print the contents of the file
74 while !my_file.is_eof() {
75 let mut buf = [0u8; 32];
76 if let Ok(n) = my_file.read(&mut buf) {
77 info!("{:a}", buf[..n]);
78 }
79 }
80}