Skip to content

Re-Init of SDIO Interface does not work #692

Open
@Simsys

Description

@Simsys

I would like to use SD cards in my project, which can also be changed during operation. To do this, it is necessary to reinitialize the SDIO peripherals. Unfortunately, this does not work. Based on the example sd.rs the following code:

#![no_main]
#![no_std]

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

use cortex_m_rt::entry;
use stm32f4xx_hal::{
    pac,
    sdio::{ClockFreq, SdCard, Sdio},
    prelude::*,
};


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

    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();

    rtt_init_print!();

    let mut delay = core.SYST.delay(&clocks);

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

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

    loop {
        match sdio.init(ClockFreq::F24Mhz) {
            Ok(_) => {
                let nblocks = sdio.card().map(|c| c.block_count()).unwrap_or(0);
                rprintln!("Card detected, nbr of blocks: {:?}", nblocks);
            },
                Err(_err) => rprintln!("Init Error"),
        }
        delay.delay_ms(3000u32);
    }
}

The output is as follows. Replacing the SD card does not change anything.

Card detected: nbr of blocks: 1955840
Init Error
Init Error
Init Error
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions