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.rs83
2 files changed, 84 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..4cbc82f7b
--- /dev/null
+++ b/examples/rp/src/bin/spi_sdmmc.rs
@@ -0,0 +1,83 @@
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 embassy_rp::pac::SIO.spinlock(31).write_value(1);
37 let p = embassy_rp::init(Default::default());
38
39 // SPI clock needs to be running at <= 400kHz during initialization
40 let mut config = spi::Config::default();
41 config.frequency = 400_000;
42 let spi = Spi::new_blocking(p.SPI1, p.PIN_10, p.PIN_11, p.PIN_12, config);
43 // Use a dummy cs pin here, for embedded-hal SpiDevice compatibility reasons
44 let spi_dev = ExclusiveDevice::new_no_delay(spi, DummyCsPin);
45 // Real cs pin
46 let cs = Output::new(p.PIN_16, Level::High);
47
48 let sdcard = SdCard::new(spi_dev, cs, embassy_time::Delay);
49 info!("Card size is {} bytes", sdcard.num_bytes().unwrap());
50
51 // Now that the card is initialized, the SPI clock can go faster
52 let mut config = spi::Config::default();
53 config.frequency = 16_000_000;
54 sdcard.spi(|dev| dev.bus_mut().set_config(&config)).ok();
55
56 // Now let's look for volumes (also known as partitions) on our block device.
57 // To do this we need a Volume Manager. It will take ownership of the block device.
58 let mut volume_mgr = embedded_sdmmc::VolumeManager::new(sdcard, DummyTimesource());
59
60 // Try and access Volume 0 (i.e. the first partition).
61 // The volume object holds information about the filesystem on that volume.
62 let mut volume0 = volume_mgr.open_volume(embedded_sdmmc::VolumeIdx(0)).unwrap();
63 info!("Volume 0: {:?}", defmt::Debug2Format(&volume0));
64
65 // Open the root directory (mutably borrows from the volume).
66 let mut root_dir = volume0.open_root_dir().unwrap();
67
68 // Open a file called "MY_FILE.TXT" in the root directory
69 // This mutably borrows the directory.
70 let mut my_file = root_dir
71 .open_file_in_dir("MY_FILE.TXT", embedded_sdmmc::Mode::ReadOnly)
72 .unwrap();
73
74 // Print the contents of the file
75 while !my_file.is_eof() {
76 let mut buf = [0u8; 32];
77 if let Ok(n) = my_file.read(&mut buf) {
78 info!("{:a}", buf[..n]);
79 }
80 }
81
82 loop {}
83}