Skip to content

Commit 0668e1c

Browse files
bors[bot]jkristell
andauthored
Merge #139
139: Add support for the sdio peripheral r=therealprof a=jkristell Left to do - [x] Add it to all supported devices - [x] Pinmap variants - [x] Add an example - [x] Document limitation (only SDHC cards) - [x] Verify power configuration Co-authored-by: Johan Kristell <[email protected]>
2 parents 9c0ebd7 + ed248a7 commit 0668e1c

File tree

5 files changed

+919
-0
lines changed

5 files changed

+919
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1313

1414
- Implement `timer::Cancel` trait for `Timer<TIM>`.
1515
- Added DWT cycle counter based delay and stopwatch, including an example.
16+
- Added sdio driver
1617

1718
## [v0.8.0] - 2020-04-30
1819

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ opt-level = "s"
9696
name = "usb_serial"
9797
required-features = ["rt", "stm32f401", "usb_fs"]
9898

99+
[[example]]
100+
name = "sd"
101+
required-features = ["rt", "stm32f405"]
102+
99103
[[example]]
100104
name = "delay-blinky"
101105
required-features = ["rt", "stm32f411"]

examples/sd.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#![no_std]
2+
#![no_main]
3+
4+
use cortex_m_rt::entry;
5+
use cortex_m_semihosting::{hprint, hprintln};
6+
use panic_semihosting as _;
7+
8+
use stm32f4xx_hal::{delay, prelude::*, sdio::Sdio, stm32};
9+
10+
#[entry]
11+
fn main() -> ! {
12+
let device = stm32::Peripherals::take().unwrap();
13+
let core = cortex_m::Peripherals::take().unwrap();
14+
15+
let rcc = device.RCC.constrain();
16+
let clocks = rcc
17+
.cfgr
18+
.use_hse(12.mhz())
19+
.require_pll48clk()
20+
.sysclk(168.mhz())
21+
.hclk(168.mhz())
22+
.pclk1(42.mhz())
23+
.pclk2(84.mhz())
24+
.freeze();
25+
26+
assert!(clocks.is_pll48clk_valid());
27+
28+
let mut delay = delay::Delay::new(core.SYST, clocks);
29+
30+
let gpioc = device.GPIOC.split();
31+
let gpiod = device.GPIOD.split();
32+
33+
let d0 = gpioc.pc8.into_alternate_af12().internal_pull_up(true);
34+
let d1 = gpioc.pc9.into_alternate_af12().internal_pull_up(true);
35+
let d2 = gpioc.pc10.into_alternate_af12().internal_pull_up(true);
36+
let d3 = gpioc.pc11.into_alternate_af12().internal_pull_up(true);
37+
let clk = gpioc.pc12.into_alternate_af12().internal_pull_up(false);
38+
let cmd = gpiod.pd2.into_alternate_af12().internal_pull_up(true);
39+
let mut sdio = Sdio::new(device.SDIO, (clk, cmd, d0, d1, d2, d3));
40+
41+
hprintln!("Waiting for card...").ok();
42+
43+
// Wait for card to be ready
44+
loop {
45+
match sdio.init_card() {
46+
Ok(_) => break,
47+
Err(_err) => (),
48+
}
49+
50+
delay.delay_ms(1000u32);
51+
}
52+
53+
let nblocks = sdio.card().map(|c| c.block_count()).unwrap_or(0);
54+
hprintln!("Card detected: nbr of blocks: {:?}", nblocks).ok();
55+
56+
// Read a block from the card and print the data
57+
let mut block = [0u8; 512];
58+
59+
match sdio.read_block(0, &mut block) {
60+
Ok(()) => (),
61+
Err(err) => {
62+
hprintln!("Failed to read block: {:?}", err).ok();
63+
}
64+
}
65+
66+
for b in block.iter() {
67+
hprint!("{:X} ", b).ok();
68+
}
69+
70+
loop {
71+
continue;
72+
}
73+
}

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ pub mod pwm;
145145
pub mod qei;
146146
#[cfg(feature = "device-selected")]
147147
pub mod rcc;
148+
#[cfg(all(
149+
feature = "device-selected",
150+
not(any(feature = "stm32f410", feature = "stm32f446",))
151+
))]
152+
pub mod sdio;
148153
#[cfg(feature = "device-selected")]
149154
pub mod serial;
150155
#[cfg(feature = "device-selected")]

0 commit comments

Comments
 (0)