esp-rs / esp-idf-svc

Type-Safe Rust Wrappers for various ESP-IDF services (WiFi, Network, Httpd, Logging, etc.)
https://docs.esp-rs.org/esp-idf-svc/
Apache License 2.0
332 stars 183 forks source link

sd mmc init faild #439

Closed niuhuan closed 4 months ago

niuhuan commented 5 months ago

I am truly certain that my hardware is not faulty, and I can use the tf card normally with other firmware.

I have checked the correctness of the PINs, but still cannot read the tfcard

image

My code is copy lot of https://github.com/esp-rs/esp-idf-svc/blob/master/examples/sd_mmc.rs


    use esp_idf_svc::{
        fs::{Fat, FatConfiguration},
        hal::{
            gpio::{self, AnyIOPin},
            prelude::*,
        },
        sd::{host::SdHost, mmc::SlotConfiguration, SdConfiguration},
    };
    use std::{fs::File, io::Write};

    let slot = SlotConfiguration::new(
        pins.gpio16,
        pins.gpio12,
        pins.gpio14,
        Some(pins.gpio17),
        Some(pins.gpio21),
        Some(pins.gpio18),
        Option::<gpio::Gpio16>::None,
        Option::<gpio::Gpio17>::None,
        Option::<gpio::Gpio15>::None,
        Option::<gpio::Gpio18>::None,
        Option::<AnyIOPin>::None,
        Option::<AnyIOPin>::None,
    );

    log::info!("INIT MMC");

    let host_config = SdConfiguration::new();

    let host = SdHost::new_with_mmc(&host_config, slot);

    let fat_config = FatConfiguration::new();

    let _fat = Fat::mount(fat_config, host, "/")?;

    let mut  read = fs::read_dir("/")?;
    while let Some(entry) = read.next() {
        let entry = entry?;
        let path = entry.path();
        log::info!("File: {:?}", path);
    }

    let mut file = File::create("/test.txt")?;

    file.write_all(b"Hello, world!")?;
I (434) cpu_start: cpu freq: 160000000 Hz
I (435) cpu_start: Application information:
I (437) cpu_start: Project name:     libespidf
I (443) cpu_start: App version:      1
I (447) cpu_start: Compile time:     Jun 23 2024 21:51:15
I (453) cpu_start: ELF file SHA256:  0000000000000000...
I (459) cpu_start: ESP-IDF:          v5.1.3
I (464) cpu_start: Min chip rev:     v0.0
I (469) cpu_start: Max chip rev:     v0.99
I (473) cpu_start: Chip rev:         v0.2
I (478) heap_init: Initializing. RAM available for dynamic allocation:
I (485) heap_init: At 3FC9F3F8 len 0004A318 (296 KiB): DRAM
I (492) heap_init: At 3FCE9710 len 00005724 (21 KiB): STACK/DRAM
I (498) heap_init: At 3FCF0000 len 00008000 (32 KiB): DRAM
I (504) heap_init: At 600FE010 len 00001FD8 (7 KiB): RTCRAM
I (512) spi_flash: detected chip: winbond
I (515) spi_flash: flash io: dio
W (520) timer_group: legacy driver is deprecated, please migrate to `driver/gptimer.h`
I (528) sleep: Configure to isolate all GPIO pins in sleep state
I (535) sleep: Enable automatic switching of GPIO sleep configuration
I (542) app_start: Starting scheduler on CPU0
I (547) app_start: Starting scheduler on CPU1
I (547) main_task: Started on CPU0
I (557) main_task: Calling app_main()
I (567) myesp: MCU STARTED!
I (587) myesp::mmc_controller: INIT MMC
I (587) gpio: GPIO[12]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (587) gpio: GPIO[16]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (597) gpio: GPIO[14]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (607) gpio: GPIO[17]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (617) gpio: GPIO[21]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (627) gpio: GPIO[18]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
E (657) vfs_fat_sdmmc: sdmmc_card_init failed (0x107).
I (657) esp_idf_svc::nvs: NvsDefault dropped
I (657) esp_idf_svc::eventloop: System event loop dropped
Error: ESP_ERR_TIMEOUT
I (657) main_task: Returned from app_main()
niuhuan commented 5 months ago

this code has been panic at let _fat = Fat::mount(fat_configuration, host, "/")?;


    let pins = peripherals.pins;
    let spi3 = peripherals.spi3;
    let device = esp_idf_svc::hal::spi::SpiDriver::new(
        spi3,
        pins.gpio12,
        pins.gpio14,
        Some(pins.gpio16),
        &esp_idf_svc::hal::spi::config::DriverConfig::default(),
    )?;
    let sd_device = esp_idf_svc::sd::spi::SpiDevice::new(
        device,
        pins.gpio18,
        Option::<esp_idf_svc::hal::gpio::AnyInputPin>::None,
        Option::<esp_idf_svc::hal::gpio::AnyInputPin>::None,
        Option::<esp_idf_svc::hal::gpio::AnyInputPin>::None,
    );
    let host_config = SdConfiguration::new();
    let host = SdHost::new_with_spi(&host_config, sd_device);
    let fat_configuration = FatConfiguration::new();
    let _fat = Fat::mount(fat_configuration, host, "/")?;
    let mut file = File::create("/test.txt")?;
    file.write_all(b"Hello, world!")?;
I (548) main_task: Started on CPU0
I (558) main_task: Calling app_main()
I (568) myesp: MCU STARTED!
I (588) myesp::mmc_controller: INIT MMC
I (588) gpio: GPIO[18]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
E (618) vfs_fat_sdmmc: sdmmc_card_init failed (0x107).
I (618) gpio: GPIO[18]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
I (618) gpio: GPIO[14]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
I (628) gpio: GPIO[16]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (638) gpio: GPIO[12]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
thread 'main' panicked at D:\Developments\Runtimes\.cargo\registry\src\index.crates.io-6f17d22bba15001f\esp-idf-hal-0.44.0\src\spi.rs:482:52:
called `Result::unwrap()` on an `Err` value: ESP_ERR_INVALID_STATE (error code 259)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

abort() was called at PC 0x4202f1da on core 0
0x4202f1da - panic_abort::__rust_start_panic::abort
    at D:\Developments\Runtimes\.rustup\toolchains\esp\lib\rustlib\src\rust\library\panic_abort\src\lib.rs:48
niuhuan commented 5 months ago

panic on this unwrap

impl<'d> Drop for SpiDriver<'d> {
    fn drop(&mut self) {
        esp!(unsafe { spi_bus_free(self.host()) }).unwrap();
    }
}
AlixANNERAUD commented 4 months ago

Hello, Sorry for the delay, I am currently traveling. I just made a fix for the SD SPI part, I invite you to try with my branch that fixes the encountered bugs. Here is the hardware configuration I used. If this is okay with you, I will continue with the SD MMC part.

matt-agius commented 4 months ago

I was having the same issue. Your branch fixed it.

Using:

Vollbrecht commented 4 months ago

closing since esp-rs/esp-idf-hal#457 landed. If you still got problems please report back.