forked from stm32-rs/stm32h7xx-hal
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sdmmc.rs
180 lines (143 loc) · 4.35 KB
/
sdmmc.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//! SDMMC card example
//!
//! Tested on a STM32H747I-DISCO development board with a SanDisk Extreme 32 GB
//! SDHC UHS-I card.
#![deny(warnings)]
#![no_main]
#![no_std]
#[macro_use]
mod utilities;
use cortex_m_rt::entry;
use stm32h7xx_hal::gpio::Speed;
use stm32h7xx_hal::hal::digital::v2::ToggleableOutputPin;
use stm32h7xx_hal::{pac, prelude::*};
use log::info;
#[entry]
fn main() -> ! {
utilities::logger::init();
let mut cp = cortex_m::Peripherals::take().unwrap();
let dp = pac::Peripherals::take().unwrap();
// Constrain and Freeze power
info!("Setup PWR... ");
let pwr = dp.PWR.constrain();
let pwrcfg = example_power!(pwr).freeze();
// Constrain and Freeze clock
info!("Setup RCC... ");
let rcc = dp.RCC.constrain();
let ccdr = rcc
.sys_ck(200.mhz())
.pll1_q_ck(100.mhz())
.freeze(pwrcfg, &dp.SYSCFG);
let gpiob = dp.GPIOB.split(ccdr.peripheral.GPIOB);
gpiob.pb3.into_alternate_af0();
let gpioc = dp.GPIOC.split(ccdr.peripheral.GPIOC);
let gpiod = dp.GPIOD.split(ccdr.peripheral.GPIOD);
let gpioi = dp.GPIOI.split(ccdr.peripheral.GPIOI);
let mut led = gpioi.pi12.into_push_pull_output();
// Get the delay provider.
let mut delay = cp.SYST.delay(ccdr.clocks);
// SDMMC pins
let clk = gpioc
.pc12
.into_alternate_af12()
.internal_pull_up(false)
.set_speed(Speed::VeryHigh);
let cmd = gpiod
.pd2
.into_alternate_af12()
.internal_pull_up(true)
.set_speed(Speed::VeryHigh);
let d0 = gpioc
.pc8
.into_alternate_af12()
.internal_pull_up(true)
.set_speed(Speed::VeryHigh);
let d1 = gpioc
.pc9
.into_alternate_af12()
.internal_pull_up(true)
.set_speed(Speed::VeryHigh);
let d2 = gpioc
.pc10
.into_alternate_af12()
.internal_pull_up(true)
.set_speed(Speed::VeryHigh);
let d3 = gpioc
.pc11
.into_alternate_af12()
.internal_pull_up(true)
.set_speed(Speed::VeryHigh);
// Card detect pin
let _cd = gpioi.pi8.into_pull_up_input();
// Create SDMMC
let mut sdmmc = dp.SDMMC1.sdmmc(
(clk, cmd, d0, d1, d2, d3),
ccdr.peripheral.SDMMC1,
&ccdr.clocks,
);
// On most development boards this can be increased up to 50MHz. We choose a
// lower frequency here so that it should work even with flying leads
// connected to a SD card breakout.
let bus_frequency = 2.mhz();
// Loop until we have a card
loop {
match sdmmc.init_card(bus_frequency) {
Ok(_) => break,
Err(err) => {
info!("Init err: {:?}", err);
}
}
info!("Waiting for card...");
delay.delay_ms(1000u32);
led.toggle().ok();
}
// Print card information
info!("");
info!("----------------------");
let size = sdmmc.card().unwrap().size();
info!("Size: {}", size);
let ocr = sdmmc.card().unwrap().ocr;
info!("{:?}", ocr);
let scr = sdmmc.card().unwrap().scr;
info!("{:?}", scr);
let cid = sdmmc.card().unwrap().cid;
info!("{:?}", cid);
let csd = sdmmc.card().unwrap().csd;
info!("{:?}", csd);
let status = sdmmc.card().unwrap().status;
info!("{:?}", status);
info!("Bus Clock: {}", sdmmc.clock());
info!("----------------------");
info!("");
// Read test
let mut buffer = [0u8; 5120];
cp.DWT.enable_cycle_counter();
let start = pac::DWT::get_cycle_count();
for i in 0..10 {
// Read 10 blocks
sdmmc.read_blocks(10 * i, &mut buffer).unwrap();
}
let end = pac::DWT::get_cycle_count();
let duration = (end - start) as f32 / ccdr.clocks.c_ck().0 as f32;
info!("Read 100 blocks at {} bytes/s", 5120. / duration);
info!("");
// Write 10 blocks
let write_buffer = [0x34; 512];
for i in 0..10 {
if let Err(err) = sdmmc.write_block(i, &write_buffer) {
info!("Failed to write block {}: {:?}", i, err);
} else {
info!("Wrote block {}", i);
}
// Read back
sdmmc.read_blocks(0, &mut buffer).unwrap();
}
// Check the read
for byte in buffer.iter() {
assert_eq!(*byte, 0x34);
}
info!("Done!");
loop {
cortex_m::asm::nop()
}
}