Skip to content

Commit ed248a7

Browse files
committed
Address review comments
1 parent e438ff2 commit ed248a7

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

src/sdio.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Sdio host
2+
13
use crate::bb;
24
#[allow(unused_imports)]
35
use crate::gpio::{gpioa::*, gpiob::*, gpioc::*, gpiod::*, Alternate, AF12};
@@ -135,7 +137,7 @@ enum CmdAppOper {
135137
}
136138

137139
#[derive(Eq, PartialEq, Copy, Clone)]
138-
pub enum Response {
140+
enum Response {
139141
None = 0,
140142
Short = 1,
141143
Long = 3,
@@ -179,6 +181,7 @@ pub enum Error {
179181
NoCard,
180182
}
181183

184+
/// Sdio device
182185
pub struct Sdio {
183186
sdio: SDIO,
184187
bw: Buswidth,
@@ -192,6 +195,7 @@ struct Cmd {
192195
}
193196

194197
#[derive(Debug, Copy, Clone, Default)]
198+
/// Card identification
195199
pub struct Cid {
196200
pub manufacturerid: u8,
197201
pub oem_applicationid: u16,
@@ -204,6 +208,7 @@ pub struct Cid {
204208
}
205209

206210
#[derive(Debug, Copy, Clone, Default)]
211+
/// Card specific data
207212
pub struct Csd {
208213
pub sys_spec_version: u8,
209214
pub max_bus_clk_frec: u8,
@@ -213,6 +218,7 @@ pub struct Csd {
213218
}
214219

215220
#[derive(Debug, Default, Copy, Clone)]
221+
/// Sd card status
216222
pub struct Status {
217223
pub bus_width: u8,
218224
pub secure_mode: u8,
@@ -227,6 +233,7 @@ pub struct Status {
227233
}
228234

229235
#[derive(Debug, Default)]
236+
/// Sd card
230237
pub struct Card {
231238
pub version: CardVersion,
232239
pub ctype: CardType,
@@ -239,9 +246,8 @@ pub struct Card {
239246
}
240247

241248
impl Sdio {
249+
/// Create and enable the Sdio device
242250
pub fn new<PINS: Pins>(sdio: SDIO, _pins: PINS) -> Self {
243-
//let sdio = unsafe { &*SDIO::ptr() };
244-
245251
unsafe {
246252
//NOTE(unsafe) this reference will only be used for atomic writes with no side effects
247253
let rcc = &*RCC::ptr();
@@ -279,10 +285,7 @@ impl Sdio {
279285
}
280286
}
281287

282-
pub fn card(&self) -> Result<&Card, Error> {
283-
self.card.as_ref().ok_or(Error::NoCard)
284-
}
285-
288+
/// initialize card
286289
pub fn init_card(&mut self) -> Result<(), Error> {
287290
// Enable power to card
288291
self.sdio
@@ -294,23 +297,26 @@ impl Sdio {
294297

295298
self.cmd(Cmd::idle())?;
296299

300+
// Check if cards supports CMD 8 (with pattern)
297301
self.cmd(Cmd::hs_send_ext_csd(0x1AA))?;
298302
let r1 = self.sdio.resp1.read().bits();
299303

300304
let mut card = if r1 == 0x1AA {
301-
/* v2 card */
305+
/* Card echoed back the pattern, we have a v2 card */
302306
Card::default()
303307
} else {
304308
return Err(Error::UnsupportedCardVersion);
305309
};
306310

307311
let ocr = loop {
312+
// Signal that next command is a app command
308313
self.cmd(Cmd::app_cmd(0))?;
309314

310315
let arg = CmdAppOper::VOLTAGE_WINDOW_SD as u32
311316
| CmdAppOper::HIGH_CAPACITY as u32
312317
| CmdAppOper::SD_SWITCH_1_8V_CAPACITY as u32;
313318

319+
// Initialize card
314320
match self.cmd(Cmd::app_op_cmd(arg)) {
315321
Ok(_) => (),
316322
Err(Error::Crc) => (),
@@ -363,6 +369,12 @@ impl Sdio {
363369
Ok(())
364370
}
365371

372+
/// Get a reference to the initialized card
373+
pub fn card(&self) -> Result<&Card, Error> {
374+
self.card.as_ref().ok_or(Error::NoCard)
375+
}
376+
377+
/// Read block from card. buf must be at least 512 bytes
366378
pub fn read_block(&mut self, addr: u32, buf: &mut [u8]) -> Result<(), Error> {
367379
let _card = self.card()?;
368380

@@ -419,6 +431,7 @@ impl Sdio {
419431
Ok(())
420432
}
421433

434+
/// Write block to card. buf must be at least 512 bytes
422435
pub fn write_block(&mut self, addr: u32, buf: &[u8]) -> Result<(), Error> {
423436
let _card = self.card()?;
424437

@@ -622,6 +635,7 @@ impl Sdio {
622635
Ok(())
623636
}
624637

638+
/// Send command to card
625639
fn cmd(&self, cmd: Cmd) -> Result<(), Error> {
626640
// Clear interrupts
627641
self.sdio.icr.modify(|_, w| {
@@ -787,17 +801,14 @@ impl Cmd {
787801
Cmd::new(8, arg, Response::Short)
788802
}
789803

790-
// SEND_CSD
791804
const fn send_csd(rca: u32) -> Cmd {
792805
Cmd::new(9, rca, Response::Long)
793806
}
794807

795-
/// Set Status (13)
796808
const fn send_card_status() -> Cmd {
797809
Cmd::new(13, 0, Response::Short)
798810
}
799811

800-
/// Set block length
801812
const fn set_blocklen(blocklen: u32) -> Cmd {
802813
Cmd::new(16, blocklen, Response::Short)
803814
}

0 commit comments

Comments
 (0)