Skip to content

Commit e55fd5c

Browse files
NandoBongersNando Bongers
andauthored
SPI example - sd card (#42)
Co-authored-by: Nando Bongers <[email protected]>
1 parent 16d6ae2 commit e55fd5c

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ rtt-target = { version = "0.3.0", features = ["cortex-m"] }
6262
panic-rtt-target = { version = "0.1.1", features = ["cortex-m"] }
6363
mpu6050 = "0.1.4"
6464
bme680 = "0.6.0"
65+
embedded-sdmmc = "0.3.0"
6566

6667
#TODO: Separate feature sets
6768
[features]

examples/spi-sd.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// This example uses SPI to initialize an SD card.
2+
3+
#![no_main]
4+
#![no_std]
5+
6+
extern crate embedded_sdmmc;
7+
8+
use hal::gpio::gpiob::PB14;
9+
use hal::gpio::gpiob::PB15;
10+
use hal::gpio::gpiof::PF8;
11+
use hal::gpio::gpiof::PF9;
12+
use hal::gpio::Alternate;
13+
use hal::gpio::AF5;
14+
use hal::prelude::*;
15+
use hal::rcc::Config;
16+
use hal::spi;
17+
use hal::stm32;
18+
use hal::stm32::Peripherals;
19+
use hal::timer::Timer;
20+
use stm32g4xx_hal as hal;
21+
22+
use embedded_sdmmc::{
23+
Block, BlockCount, BlockDevice, BlockIdx, Controller, Error, Mode, TimeSource, Timestamp,
24+
VolumeIdx,
25+
};
26+
27+
use cortex_m_rt::entry;
28+
use log::info;
29+
30+
#[macro_use]
31+
mod utils;
32+
33+
#[entry]
34+
fn main() -> ! {
35+
let dp = Peripherals::take().unwrap();
36+
let rcc = dp.RCC.constrain();
37+
let mut rcc = rcc.freeze(Config::hsi());
38+
let gpiob = dp.GPIOB.split(&mut rcc);
39+
let gpiof = dp.GPIOF.split(&mut rcc);
40+
41+
let cs = {
42+
let mut cs = gpiof.pf8.into_push_pull_output();
43+
cs.set_high().unwrap();
44+
cs
45+
};
46+
47+
let sck: PF9<Alternate<AF5>> = gpiof.pf9.into_alternate();
48+
let miso: PB14<Alternate<AF5>> = gpiob.pb14.into_alternate();
49+
let mosi: PB15<Alternate<AF5>> = gpiob.pb15.into_alternate();
50+
51+
let mut spi = dp
52+
.SPI2
53+
.spi((sck, miso, mosi), spi::MODE_0, 400.khz(), &mut rcc);
54+
55+
struct Clock;
56+
57+
impl TimeSource for Clock {
58+
fn get_timestamp(&self) -> Timestamp {
59+
Timestamp {
60+
year_since_1970: 0,
61+
zero_indexed_month: 0,
62+
zero_indexed_day: 0,
63+
hours: 0,
64+
minutes: 0,
65+
seconds: 0,
66+
}
67+
}
68+
}
69+
70+
let mut cont = embedded_sdmmc::Controller::new(embedded_sdmmc::SdMmcSpi::new(spi, cs), Clock);
71+
72+
cont.device().init();
73+
loop {}
74+
}

0 commit comments

Comments
 (0)