Unable to initialize / hot swap sd card during run time

Hi

I have a system which needs to be run for 10days continuously. I have and SD card to log data during this period.

I need to verify the SD card data in between, so I send data from Bluetooth to alert my system that i am going to unmount and remove my SD card and also for mounting the SD card after I insert it back to SD card slot.

The cards gets detected easily and is accessible during initialization. 

int res = fs_unmount(&mp); is the command I use for mounting and detecting the sd card.
But after hot swap the SD card card gets detected but fails to respond due to sd card read timeout error. Kindly provide any suggestion so that I can effectively swap SD cards during runtime.
below is the snippet of the overlay file.
&spi4 {
    status ="okay";
    cs-gpios = <&gpio1 11 GPIO_ACTIVE_LOW>,<&gpio1 12 GPIO_ACTIVE_LOW>;

    sdhc0: sdhc@0 {
       
        compatible = "zephyr,sdhc-spi-slot";
        reg = <0>;
        status = "okay";
        mmc {
            compatible = "zephyr,sdmmc-disk";
            status = "okay";
        };
        spi-max-frequency = < 24000000 >;
   
    };
  • Hi

    Does the fs_mount() fail after this hot swap as well, or does it mount correctly, but fail to read properly once mounted? Do you have a log so we can see exactly where this error is triggered? And what SDK version are you using for development exactly?

    I'm not entirely certain on what is causing this error, but have a few theories as to what could cause this. It might be that you need to reinitialize the filesystem after the SD card is removed and put back in. A reason for you needing to reinit is that the address range isn't reset or updated when you hot swap the SD card during runtime for example.

    Also, what kind of SD card reader are you using? Does it have a physical "switch" that lets you know if a SD card is inserted and does it report whether a card is mounted to the nRF SoC, or is the reader agnostic to whether a card is inserted?

    Best regards,

    Simon

  • Hi

    The SDK toolchain version is V2.2.0. 

    Below are the sd card logs I get on fs_mount() after hot swap:

    [00:00:32.266,052] <err> sd: Failed to read from SDMMC -116
    [00:00:32.272,583] <err> sd: Card read failed
    [00:00:32.277,893] <err> fs: fs mount error (-5).

    The reader I am using is an sd card breakout board with spi interface. It doesn't have any switch to indicate sd card is inserted.

  • On further debugging, I could find that, it is getting timeout inside this function in sdmmc_subsys.c file.

    static int disk_sdmmc_access_init(struct disk_info *disk)
    {
        const struct device *dev = disk->dev;
        const struct sdmmc_config *cfg = dev->config;
        struct sdmmc_data *data = dev->data;
        int ret;

        if (data->status == SD_OK) {
            // Called twice, don't reinit
            return 0;
        }

        if (!sd_is_card_present(cfg->host_controller)) {
            return DISK_STATUS_NOMEDIA;
        }

        ret = sd_init(cfg->host_controller, &data->card);
        if (ret) {
            data->status = SD_ERROR;
            return ret;
        }
        data->status = SD_OK;
        return 0;
    }

    static int disk_sdmmc_access_status(struct disk_info *disk)
    {
        const struct device *dev = disk->dev;
        const struct sdmmc_config *cfg = dev->config;
        struct sdmmc_data *data = dev->data;

        if (!sd_is_card_present(cfg->host_controller)) {
            return DISK_STATUS_NOMEDIA;
        }
        if (data->status == SD_OK) {
            return DISK_STATUS_OK;
        } else {
            return DISK_STATUS_UNINIT;
        }
    }
     
    The code returns at highlighted part when we try to mount again .How can reinitialize sd card again?. 
  • Hi

    So it already finds the SD card initialized. In that case I think you need to call an "uninit" function first, so that the device knows that the SD card has been removed at one point and then can initialize again. You can either do this when the error you see occurs, or uninit the disk if/when the SPI interface isn't able to communicate with the SD card anymore.

    Best regards,

    Simon

  • Hi,

    I couldn't find any uninit function in sd.c or any associated c files. Are you suggesting to write a custom uninit function?. Do you have any examples of the same?.   I don't want to uninitialize SPI as I am using the same SPI lines for a pressure sensor.

Related