Skip to content

Commit e438ff2

Browse files
committed
Create a simpler example and fix review comments
1 parent fb7403d commit e438ff2

File tree

4 files changed

+89
-238
lines changed

4 files changed

+89
-238
lines changed

Cargo.toml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,6 @@ ssd1306 = "0.2.6"
5959
embedded-graphics = "0.4.9"
6060
usb-device = "0.2.5"
6161
usbd-serial = "0.1.0"
62-
usbd_scsi = "0.1.0"
63-
usbd_mass_storage = "0.1.0"
64-
rtt-target = {version = "0.1.1", features = ["cortex-m"]}
65-
panic-rtt-target = { version = "0.1", features = ["cortex-m"] }
6662

6763
[features]
6864
device-selected = []
@@ -102,8 +98,8 @@ name = "usb_serial"
10298
required-features = ["rt", "stm32f401", "usb_fs"]
10399

104100
[[example]]
105-
name = "sdio-usb-block"
106-
required-features = ["rt", "stm32f405", "usb_fs"]
101+
name = "sd"
102+
required-features = ["rt", "stm32f405"]
107103

108104
[[example]]
109105
name = "delay-blinky"

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+
}

examples/sdio-usb-block.rs

Lines changed: 0 additions & 218 deletions
This file was deleted.

src/sdio.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ pub enum CardVersion {
148148

149149
#[derive(Debug, Copy, Clone)]
150150
pub enum CardType {
151-
/// Standard Capacity (< 2Gn)
151+
/// Standard Capacity (< 2Gb)
152152
SDSC,
153153
/// High capacity (< 32Gb)
154154
SDHC,
@@ -186,9 +186,9 @@ pub struct Sdio {
186186
}
187187

188188
struct Cmd {
189-
pub cmd: u8,
190-
pub arg: u32,
191-
pub resp: Response,
189+
cmd: u8,
190+
arg: u32,
191+
resp: Response,
192192
}
193193

194194
#[derive(Debug, Copy, Clone, Default)]
@@ -214,16 +214,16 @@ pub struct Csd {
214214

215215
#[derive(Debug, Default, Copy, Clone)]
216216
pub struct Status {
217-
bus_width: u8,
218-
secure_mode: u8,
219-
card_type: u16,
220-
protected_area_size: u32,
221-
speed_class: u8,
222-
performance_move: u8,
223-
allocation_units: u8,
224-
erase_size: u16,
225-
erase_timeout: u8,
226-
erase_offset: u8,
217+
pub bus_width: u8,
218+
pub secure_mode: u8,
219+
pub card_type: u16,
220+
pub protected_area_size: u32,
221+
pub speed_class: u8,
222+
pub performance_move: u8,
223+
pub allocation_units: u8,
224+
pub erase_size: u16,
225+
pub erase_timeout: u8,
226+
pub erase_offset: u8,
227227
}
228228

229229
#[derive(Debug, Default)]

0 commit comments

Comments
 (0)