Skip to content

Commit 7bbd349

Browse files
authored
Merge pull request #734 from skibon02/master
Sdio-host: update to 0.9.0, add AddressMode enum
2 parents d567d5f + d33f0ea commit 7bbd349

File tree

3 files changed

+29
-27
lines changed

3 files changed

+29
-27
lines changed

CHANGELOG.md

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

1010
- add trait bound `RegisterBlockImpl` to type `RegisterBlock` associated with `serial::Instance` [#732]
1111
- remove unneeded trait bound for methods that take in a `serial::Instance` and use the associated `RegisterBlock`
12+
- bump `sdio-host` to 0.9.0, refactor SDIO initialization [#734]
1213

1314
## [v0.20.0] - 2024-01-14
1415

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ nb = "1.1"
3535
rand_core = "0.6.4"
3636
stm32f4 = "0.15.1"
3737
synopsys-usb-otg = { version = "0.4.0", features = ["cortex-m"], optional = true }
38-
sdio-host = { version = "0.6.0", optional = true }
38+
sdio-host = { version = "0.9.0", optional = true }
3939
embedded-dma = "0.2.0"
4040
bare-metal = { version = "1" }
4141
void = { default-features = false, version = "1.0.2" }

src/sdio.rs

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ pub enum Error {
133133
NoCard,
134134
}
135135

136+
#[derive(Debug, Copy, Clone)]
137+
pub enum AddressMode {
138+
Byte,
139+
Block512,
140+
}
141+
136142
/// A peripheral that uses the SDIO hardware, generic over the particular type of device.
137143
pub struct Sdio<P: SdioPeripheral> {
138144
sdio: SDIO,
@@ -153,7 +159,6 @@ pub struct SdCard {
153159

154160
/// eMMC device peripheral
155161
pub struct Emmc {
156-
pub capacity: CardCapacity,
157162
pub ocr: OCR<EMMC>,
158163
pub rca: RCA<EMMC>, // Relative Card Address
159164
pub cid: CID<EMMC>,
@@ -232,9 +237,9 @@ impl<P: SdioPeripheral> Sdio<P> {
232237

233238
// Always read 1 block of 512 bytes
234239
// SDSC cards are byte addressed hence the blockaddress is in multiples of 512 bytes
235-
let blockaddr = match card.get_capacity() {
236-
CardCapacity::StandardCapacity => blockaddr * 512,
237-
_ => blockaddr,
240+
let blockaddr = match card.get_address_mode() {
241+
AddressMode::Byte => blockaddr * 512,
242+
AddressMode::Block512 => blockaddr,
238243
};
239244
self.cmd(common_cmd::set_block_length(512))?;
240245
self.start_datapath_transfer(512, 9, true);
@@ -276,9 +281,9 @@ impl<P: SdioPeripheral> Sdio<P> {
276281

277282
// Always write 1 block of 512 bytes
278283
// SDSC cards are byte addressed hence the blockaddress is in multiples of 512 bytes
279-
let blockaddr = match card.get_capacity() {
280-
CardCapacity::StandardCapacity => blockaddr * 512,
281-
_ => blockaddr,
284+
let blockaddr = match card.get_address_mode() {
285+
AddressMode::Byte => blockaddr * 512,
286+
AddressMode::Block512 => blockaddr,
282287
};
283288
self.cmd(common_cmd::set_block_length(512))?;
284289
self.start_datapath_transfer(512, 9, false);
@@ -496,7 +501,7 @@ impl Sdio<SdCard> {
496501
// Initialize card
497502

498503
// 3.2-3.3V
499-
let voltage_window = 1 << 20;
504+
let voltage_window = 1 << 5;
500505
match self.app_cmd(sd_cmd::sd_send_op_cond(true, false, true, voltage_window)) {
501506
Ok(_) => (),
502507
Err(Error::Crc) => (),
@@ -653,7 +658,7 @@ impl Sdio<SdCard> {
653658
}
654659

655660
impl Sdio<Emmc> {
656-
/// Initializes eMMC device (if present) and sets the bus at the specified frequency.
661+
/// Initializes eMMC device (if present) and sets the bus at the specified frequency. eMMC device must support 512 byte blocks.
657662
pub fn init(&mut self, freq: ClockFreq) -> Result<(), Error> {
658663
let card_addr: RCA<EMMC> = RCA::from(1u16);
659664

@@ -675,27 +680,20 @@ impl Sdio<Emmc> {
675680
Err(Error::Crc) => (),
676681
Err(err) => return Err(err),
677682
};
678-
let ocr = OCR::from(self.sdio.resp1.read().bits());
683+
let ocr = OCR::<EMMC>::from(self.sdio.resp1.read().bits());
679684
if !ocr.is_busy() {
680685
break ocr;
681686
}
682687
};
683688

684-
// True for SDHC and SDXC False for SDSC
685-
let capacity = if ocr.high_capacity() {
686-
CardCapacity::HighCapacity
687-
} else {
688-
CardCapacity::StandardCapacity
689-
};
690-
691689
// Get CID
692690
self.cmd(common_cmd::all_send_cid())?;
693691
let mut cid = [0; 4];
694692
cid[3] = self.sdio.resp1.read().bits();
695693
cid[2] = self.sdio.resp2.read().bits();
696694
cid[1] = self.sdio.resp3.read().bits();
697695
cid[0] = self.sdio.resp4.read().bits();
698-
let cid = CID::from(cid);
696+
let cid = CID::<EMMC>::from(cid);
699697

700698
self.cmd(emmc_cmd::assign_relative_address(card_addr.address()))?;
701699

@@ -706,12 +704,11 @@ impl Sdio<Emmc> {
706704
csd[2] = self.sdio.resp2.read().bits();
707705
csd[1] = self.sdio.resp3.read().bits();
708706
csd[0] = self.sdio.resp4.read().bits();
709-
let csd = CSD::from(csd);
707+
let csd = CSD::<EMMC>::from(csd);
710708

711709
self.select_card(card_addr.address())?;
712710

713711
let card = Emmc {
714-
capacity,
715712
ocr,
716713
rca: card_addr,
717714
cid,
@@ -807,7 +804,7 @@ fn clear_all_interrupts(icr: &pac::sdio::ICR) {
807804

808805
impl SdCard {
809806
/// Size in blocks
810-
pub fn block_count(&self) -> u32 {
807+
pub fn block_count(&self) -> u64 {
811808
self.csd.block_count()
812809
}
813810

@@ -821,21 +818,25 @@ impl SdioPeripheral for SdCard {
821818
fn get_address(&self) -> u16 {
822819
self.rca.address()
823820
}
824-
fn get_capacity(&self) -> CardCapacity {
825-
self.capacity
821+
fn get_address_mode(&self) -> AddressMode {
822+
match self.capacity {
823+
CardCapacity::StandardCapacity => AddressMode::Byte,
824+
CardCapacity::HighCapacity => AddressMode::Block512,
825+
_ => AddressMode::Block512,
826+
}
826827
}
827828
}
828829

829830
impl SdioPeripheral for Emmc {
830831
fn get_address(&self) -> u16 {
831832
self.rca.address()
832833
}
833-
fn get_capacity(&self) -> CardCapacity {
834-
self.capacity
834+
fn get_address_mode(&self) -> AddressMode {
835+
AddressMode::Block512
835836
}
836837
}
837838

838839
pub trait SdioPeripheral {
839840
fn get_address(&self) -> u16;
840-
fn get_capacity(&self) -> CardCapacity;
841+
fn get_address_mode(&self) -> AddressMode;
841842
}

0 commit comments

Comments
 (0)