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

Sd devel #2

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
8 changes: 5 additions & 3 deletions .cargo/config
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
runner = "probe-run --chip STM32F405RGTx"

rustflags = ["-C", "link-arg=-Tlink.x"]

[build]
target = "thumbv7em-none-eabihf"

[target.'cfg(all(target_arch = "arm", target_os = "none"))']
runner = "probe-run --chip STM32F405rgt"

[target.thumbv7em-none-eabihf]
rustflags = [ "-C", "link-arg=-Tlink.x", ]
117 changes: 117 additions & 0 deletions examples/sd-stress.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#![no_std]
#![no_main]

use cortex_m_rt::entry;
use panic_rtt_target as _;
use rtt_target::{rprintln, rtt_init_print};

use stm32f4xx_hal::{
delay,
prelude::*,
sdio::{ClockFreq, Sdio},
stm32,
};

#[entry]
fn main() -> ! {
let device = stm32::Peripherals::take().unwrap();
let core = cortex_m::Peripherals::take().unwrap();

rtt_init_print!(BlockIfFull);

let rcc = device.RCC.constrain();
let clocks = rcc
.cfgr
.use_hse(12.mhz())
.require_pll48clk()
.sysclk(168.mhz())
.hclk(168.mhz())
.pclk1(42.mhz())
.pclk2(84.mhz())
.freeze();

assert!(clocks.is_pll48clk_valid());

let mut delay = delay::Delay::new(core.SYST, clocks);

let gpioc = device.GPIOC.split();
let gpiod = device.GPIOD.split();

let d0 = gpioc.pc8.into_alternate_af12().internal_pull_up(true);
let d1 = gpioc.pc9.into_alternate_af12().internal_pull_up(true);
let d2 = gpioc.pc10.into_alternate_af12().internal_pull_up(true);
let d3 = gpioc.pc11.into_alternate_af12().internal_pull_up(true);
let clk = gpioc.pc12.into_alternate_af12().internal_pull_up(false);
let cmd = gpiod.pd2.into_alternate_af12().internal_pull_up(true);
let mut sdio = Sdio::new(device.SDIO, (clk, cmd, d0, d1, d2, d3), clocks );

rprintln!("Waiting for card...");

// Wait for card to be ready
loop {
match sdio.init_card(ClockFreq::F24Mhz) {
Ok(_) => break,
Err(_err) => (),
}

delay.delay_ms(1000u32);
}

if let Ok(card) = sdio.card() {
rprintln!("Card detected");
rprintln!("Card address: {:X}", card.rca.address());
rprintln!("blocks: {}", card.block_count());

rprintln!("Card name: {}", card.cid.product_name());

rprintln!("{}", card.cid.oem_id());

rprintln!("ocr: {:?}", card.ocr);
rprintln!("cid: {:?}", card.cid);
//rprintln!("status: {:?}", card.);
rprintln!("scr: {:?}", card.scr);
rprintln!("scd: {:?}", card.csd);
rprintln!("rca: {:?}", card.rca.address());

for i in 0 .. 32 {
// Read a block from the card and print the data
let mut block = [(i & 0xFF) as u8; 512];

rprintln!("block: {}", i);


/*
match sdio.write_block(i, &mut block) {
Ok(()) => (),
Err(err) => {
rprintln!("Failed to write block {}: {:?}", i, err);
}
}
*/

match sdio.read_block(i, &mut block) {
Ok(()) => (),
Err(err) => {
rprintln!("Failed to read block: {}: {:?}", i, err);
}
}

}

}



rprintln!("Done");

/*
for b in block.iter() {
rprint!("{:X} ", b);
}

*/

loop {
continue;
}
}
34 changes: 25 additions & 9 deletions examples/sd-usb-mass-storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,18 @@ impl BlockDevice for Storage {
.try_into()
.map_err(|_e| BlockDeviceError::InvalidAddress)?;

sdio.read_block(lba, block).map_err(|e| {
rprintln!("read error: {:?}", e);
BlockDeviceError::HardwareError
})
// Use this until const generics and try_into is a thing
let mut b = [0; 512];

sdio
.read_block(lba, &mut b)
.map_err(|e| {
rprintln!("read error: {:?}", e);
BlockDeviceError::HardwareError
})?;

block.copy_from_slice(&b);
Ok(())
}

fn write_block(&mut self, lba: u32, block: &[u8]) -> Result<(), BlockDeviceError> {
Expand All @@ -57,10 +65,18 @@ impl BlockDevice for Storage {
.try_into()
.map_err(|_e| BlockDeviceError::InvalidAddress)?;

sdio.write_block(lba, block).map_err(|e| {
rprintln!("write error: {:?}", e);
BlockDeviceError::WriteError
})
// Use this until const generics and try_into is a thing
let mut b = [0; 512];
b.copy_from_slice(&block);

sdio
.write_block(lba, &b)
.map_err(|e| {
rprintln!("write error: {:?}", e);
BlockDeviceError::WriteError
})?;

Ok(())
}

fn max_lba(&self) -> u32 {
Expand Down Expand Up @@ -127,7 +143,7 @@ fn main() -> ! {
usb_pwrclk: dp.OTG_FS_PWRCLK,
pin_dm: gpioa.pa11.into_alternate_af10(),
pin_dp: gpioa.pa12.into_alternate_af10(),
hclk: clocks.hclk(),
hclk: clocks.hclk()
};

let usb_bus = UsbBus::new(usb, &mut EP_MEMORY);
Expand Down
16 changes: 9 additions & 7 deletions src/sd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ use stm32f4xx_hal::{
},
rcc::Clocks,
sdio::Sdio,
stm32::SDIO,
pac::SDIO,
rcc::Clocks,
};
use stm32f4xx_hal::gpio::Speed;

/// The sd host on the feather board
pub struct SdHost {
Expand All @@ -31,12 +33,12 @@ impl SdHost {
card_detect: PB12<M6>,
clocks: Clocks,
) -> Self {
let clk = clk.into_alternate_af12().internal_pull_up(false);
let cmd = cmd.into_alternate_af12().internal_pull_up(true);
let d0 = d0.into_alternate_af12().internal_pull_up(true);
let d1 = d1.into_alternate_af12().internal_pull_up(true);
let d2 = d2.into_alternate_af12().internal_pull_up(true);
let d3 = d3.into_alternate_af12().internal_pull_up(true);
let clk = clk.into_alternate_af12().internal_pull_up(false).set_speed(Speed::VeryHigh);
let cmd = cmd.into_alternate_af12().internal_pull_up(true).set_speed(Speed::VeryHigh);
let d0 = d0.into_alternate_af12().internal_pull_up(true).set_speed(Speed::VeryHigh);
let d1 = d1.into_alternate_af12().internal_pull_up(true).set_speed(Speed::VeryHigh);
let d2 = d2.into_alternate_af12().internal_pull_up(true).set_speed(Speed::VeryHigh);
let d3 = d3.into_alternate_af12().internal_pull_up(true).set_speed(Speed::VeryHigh);

// Card detect pin
let cd = card_detect.into_pull_up_input();
Expand Down