Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sdio-host: update to 0.9.0, add AddressMode enum #734

Merged
merged 2 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

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

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

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ nb = "1.1"
rand_core = "0.6.4"
stm32f4 = "0.15.1"
synopsys-usb-otg = { version = "0.4.0", features = ["cortex-m"], optional = true }
sdio-host = { version = "0.6.0", optional = true }
sdio-host = { version = "0.9.0", optional = true }
embedded-dma = "0.2.0"
bare-metal = { version = "1" }
void = { default-features = false, version = "1.0.2" }
Expand Down
53 changes: 27 additions & 26 deletions src/sdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ pub enum Error {
NoCard,
}

#[derive(Debug, Copy, Clone)]
pub enum AddressMode {
Byte,
Block512,
}

/// A peripheral that uses the SDIO hardware, generic over the particular type of device.
pub struct Sdio<P: SdioPeripheral> {
sdio: SDIO,
Expand All @@ -153,7 +159,6 @@ pub struct SdCard {

/// eMMC device peripheral
pub struct Emmc {
pub capacity: CardCapacity,
pub ocr: OCR<EMMC>,
pub rca: RCA<EMMC>, // Relative Card Address
pub cid: CID<EMMC>,
Expand Down Expand Up @@ -232,9 +237,9 @@ impl<P: SdioPeripheral> Sdio<P> {

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

// Always write 1 block of 512 bytes
// SDSC cards are byte addressed hence the blockaddress is in multiples of 512 bytes
let blockaddr = match card.get_capacity() {
CardCapacity::StandardCapacity => blockaddr * 512,
_ => blockaddr,
let blockaddr = match card.get_address_mode() {
AddressMode::Byte => blockaddr * 512,
AddressMode::Block512 => blockaddr,
};
self.cmd(common_cmd::set_block_length(512))?;
self.start_datapath_transfer(512, 9, false);
Expand Down Expand Up @@ -496,7 +501,7 @@ impl Sdio<SdCard> {
// Initialize card

// 3.2-3.3V
let voltage_window = 1 << 20;
let voltage_window = 1 << 5;
match self.app_cmd(sd_cmd::sd_send_op_cond(true, false, true, voltage_window)) {
Ok(_) => (),
Err(Error::Crc) => (),
Expand Down Expand Up @@ -653,7 +658,7 @@ impl Sdio<SdCard> {
}

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

Expand All @@ -675,27 +680,20 @@ impl Sdio<Emmc> {
Err(Error::Crc) => (),
Err(err) => return Err(err),
};
let ocr = OCR::from(self.sdio.resp1.read().bits());
let ocr = OCR::<EMMC>::from(self.sdio.resp1.read().bits());
if !ocr.is_busy() {
break ocr;
}
};

// True for SDHC and SDXC False for SDSC
let capacity = if ocr.high_capacity() {
CardCapacity::HighCapacity
} else {
CardCapacity::StandardCapacity
};

// Get CID
self.cmd(common_cmd::all_send_cid())?;
let mut cid = [0; 4];
cid[3] = self.sdio.resp1.read().bits();
cid[2] = self.sdio.resp2.read().bits();
cid[1] = self.sdio.resp3.read().bits();
cid[0] = self.sdio.resp4.read().bits();
let cid = CID::from(cid);
let cid = CID::<EMMC>::from(cid);

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

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

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

let card = Emmc {
capacity,
ocr,
rca: card_addr,
cid,
Expand Down Expand Up @@ -807,7 +804,7 @@ fn clear_all_interrupts(icr: &pac::sdio::ICR) {

impl SdCard {
/// Size in blocks
pub fn block_count(&self) -> u32 {
pub fn block_count(&self) -> u64 {
self.csd.block_count()
}

Expand All @@ -821,21 +818,25 @@ impl SdioPeripheral for SdCard {
fn get_address(&self) -> u16 {
self.rca.address()
}
fn get_capacity(&self) -> CardCapacity {
self.capacity
fn get_address_mode(&self) -> AddressMode {
match self.capacity {
CardCapacity::StandardCapacity => AddressMode::Byte,
CardCapacity::HighCapacity => AddressMode::Block512,
_ => AddressMode::Block512,
}
}
}

impl SdioPeripheral for Emmc {
fn get_address(&self) -> u16 {
self.rca.address()
}
fn get_capacity(&self) -> CardCapacity {
self.capacity
fn get_address_mode(&self) -> AddressMode {
AddressMode::Block512
}
}

pub trait SdioPeripheral {
fn get_address(&self) -> u16;
fn get_capacity(&self) -> CardCapacity;
fn get_address_mode(&self) -> AddressMode;
}
Loading