forked from stm32-rs/stm32h7xx-hal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdmmc.rs
204 lines (163 loc) · 4.93 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//! 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::sdmmc::{SdCard, Sdmmc};
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 gpioc = dp.GPIOC.split(ccdr.peripheral.GPIOC);
let gpiod = dp.GPIOD.split(ccdr.peripheral.GPIOD);
// STM32H747I-DISCO development board
#[cfg(any(feature = "rm0399"))]
let mut led = {
let gpioi = dp.GPIOI.split(ccdr.peripheral.GPIOI);
// Card detect pin
let _cd = gpioi.pi8.into_pull_up_input();
gpioi.pi12.into_push_pull_output()
};
#[cfg(not(feature = "rm0399"))]
let mut led = {
let gpioe = dp.GPIOE.split(ccdr.peripheral.GPIOE);
gpioe.pe1.into_push_pull_output()
};
// Get the delay provider.
let mut delay = cp.SYST.delay(ccdr.clocks);
// SDMMC pins
let clk = gpioc
.pc12
.into_alternate()
.internal_pull_up(false)
.speed(Speed::VeryHigh);
let cmd = gpiod
.pd2
.into_alternate()
.internal_pull_up(true)
.speed(Speed::VeryHigh);
let d0 = gpioc
.pc8
.into_alternate()
.internal_pull_up(true)
.speed(Speed::VeryHigh);
let d1 = gpioc
.pc9
.into_alternate()
.internal_pull_up(true)
.speed(Speed::VeryHigh);
let d2 = gpioc
.pc10
.into_alternate()
.internal_pull_up(true)
.speed(Speed::VeryHigh);
let d3 = gpioc
.pc11
.into_alternate()
.internal_pull_up(true)
.speed(Speed::VeryHigh);
// Create SDMMC
let mut sdmmc: Sdmmc<_, SdCard> = 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(bus_frequency) {
Ok(_) => break,
Err(err) => {
info!("Init err: {:?}", err);
}
}
info!("Waiting for card...");
delay.delay_ms(1000u32);
led.toggle();
}
// 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::cycle_count();
for i in 0..10 {
// Read 10 blocks
sdmmc.read_blocks(10 * i, &mut buffer).unwrap();
}
let end = pac::DWT::cycle_count();
let duration = (end - start) as f32 / ccdr.clocks.c_ck().raw() as f32;
info!("Read 10 blocks at {} bytes/s", 5120. / duration);
info!("");
let write_buffer = [0x34; 512];
let start = pac::DWT::cycle_count();
for i in 0..10 {
if let Err(err) = sdmmc.write_block(i, &write_buffer) {
info!("Failed to write block {}: {:?}", i, err);
}
}
let end = pac::DWT::cycle_count();
let duration = (end - start) as f32 / ccdr.clocks.c_ck().raw() as f32;
info!("Wrote 10 blocks at {} bytes/s", 5120. / duration);
info!("");
info!("Verification test...");
// Write 10 blocks
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!("Verified 10 blocks");
info!("");
info!("Done!");
loop {
cortex_m::asm::nop()
}
}