SPI Interface for MicroSD Card

Hello,

I am attempting to mount and write to a microSD card via SPI. I am using the Fat_fs example found under "zephyr/samples/subsys/fs/fat_fs" and I am getting an error code of -5 during disk initialization. 

When I run the same code without the card inserted, I get the exact same behavior. So it almost seems like the driver isn't detecting the card at all. I have check that the sd card is FAT formatted and is working on my computer.

Also when using a logic analyzer, I can see that the MISO, MOSI, CS, and SCK signals appear to be working. However, pretty much all responses from the card (such as the CSD) return empty with all zeros. 

Below is the dts, config, and some notes about the board.

* MISO is tied high by an external resistor. 

Thanks in advance for the help

Parents
  • Is there any progress on this?  I'm implementing SDcard storage on a Thingy91 and have had to revert to 2.0.2; like Owain I've observed that the disk access list is empty if I build using 2.1.0 or 2.1.2.

  • Have you tried my solution? I'm using it with 2.1.2

    CONFIG_SDMMC_SUBSYS=y 

  • Thanks MMucka, that almost worked for me but I'm out of time to investigate further.  For now I have a usable build with 2.0.2, I'll look at moving to 2.1.2 when I get a chance.

  • This is for version v2.2.0 but might be relevant for another version too, it is worth a try.

    I just ran into this problem and I had to change the overlay I am using, this is the change I made...

    I am using the spi3 peripheral with an nRF9160, but the SDK might be similar.

    Before

    &spi3 {
            compatible = "nordic,nrf-spim";
            status = "okay";
            cs-gpios = < &gpio0 18 GPIO_ACTIVE_LOW >;
            pinctrl-0 = <&spi3_default>;
            pinctrl-1 = <&spi3_sleep>;
            pinctrl-names = "default", "sleep";
            sdhc0: sdhc@0 {
                    compatible = "zephyr,mmc-spi-slot";
                    reg = <0>;
                    status = "okay";
                    label = "SDHC0";
                    spi-max-frequency = <24000000>;
            };
    };
    
    &pinctrl {
            spi3_default: spi3_default {
                    group1 {
                            psels = <NRF_PSEL(SPIM_SCK, 0, 19)>,
                                    <NRF_PSEL(SPIM_MOSI, 0, 21)>,
                                    <NRF_PSEL(SPIM_MISO, 0, 22)>;
                    };
            };
    
            spi3_sleep: spi3_sleep {
                    group1 {
                            psels = <NRF_PSEL(SPIM_SCK, 0, 19)>,
                                    <NRF_PSEL(SPIM_MOSI, 0, 21)>,
                                    <NRF_PSEL(SPIM_MISO, 0, 22)>;
                            low-power-enable;
                    };
            };
    };

    After

    &spi3 {
            compatible = "nordic,nrf-spim";
            status = "okay";
            cs-gpios = < &gpio0 18 GPIO_ACTIVE_LOW >;
            pinctrl-0 = <&spi3_default>;
            pinctrl-1 = <&spi3_sleep>;
            pinctrl-names = "default", "sleep";
            sdhc0: sdhc@0 {
                    compatible = "zephyr,sdhc-spi-slot";
                    reg = <0>;
                    status = "okay";
                    mmc {
                        compatible = "zephyr,sdmmc-disk";
                        status = "okay";
                    };
                    spi-max-frequency = <24000000>;
            };
    };
    
    &pinctrl {
            spi3_default: spi3_default {
                    group1 {
                            psels = <NRF_PSEL(SPIM_SCK, 0, 19)>,
                                    <NRF_PSEL(SPIM_MOSI, 0, 21)>,
                                    <NRF_PSEL(SPIM_MISO, 0, 22)>;
                    };
            };
    
            spi3_sleep: spi3_sleep {
                    group1 {
                            psels = <NRF_PSEL(SPIM_SCK, 0, 19)>,
                                    <NRF_PSEL(SPIM_MOSI, 0, 21)>,
                                    <NRF_PSEL(SPIM_MISO, 0, 22)>;
                            low-power-enable;
                    };
            };
    };

    In prj.conf here is what I needed to use an external SD card.

    CONFIG_FILE_SYSTEM=y
    CONFIG_FAT_FILESYSTEM_ELM=y

Reply
  • This is for version v2.2.0 but might be relevant for another version too, it is worth a try.

    I just ran into this problem and I had to change the overlay I am using, this is the change I made...

    I am using the spi3 peripheral with an nRF9160, but the SDK might be similar.

    Before

    &spi3 {
            compatible = "nordic,nrf-spim";
            status = "okay";
            cs-gpios = < &gpio0 18 GPIO_ACTIVE_LOW >;
            pinctrl-0 = <&spi3_default>;
            pinctrl-1 = <&spi3_sleep>;
            pinctrl-names = "default", "sleep";
            sdhc0: sdhc@0 {
                    compatible = "zephyr,mmc-spi-slot";
                    reg = <0>;
                    status = "okay";
                    label = "SDHC0";
                    spi-max-frequency = <24000000>;
            };
    };
    
    &pinctrl {
            spi3_default: spi3_default {
                    group1 {
                            psels = <NRF_PSEL(SPIM_SCK, 0, 19)>,
                                    <NRF_PSEL(SPIM_MOSI, 0, 21)>,
                                    <NRF_PSEL(SPIM_MISO, 0, 22)>;
                    };
            };
    
            spi3_sleep: spi3_sleep {
                    group1 {
                            psels = <NRF_PSEL(SPIM_SCK, 0, 19)>,
                                    <NRF_PSEL(SPIM_MOSI, 0, 21)>,
                                    <NRF_PSEL(SPIM_MISO, 0, 22)>;
                            low-power-enable;
                    };
            };
    };

    After

    &spi3 {
            compatible = "nordic,nrf-spim";
            status = "okay";
            cs-gpios = < &gpio0 18 GPIO_ACTIVE_LOW >;
            pinctrl-0 = <&spi3_default>;
            pinctrl-1 = <&spi3_sleep>;
            pinctrl-names = "default", "sleep";
            sdhc0: sdhc@0 {
                    compatible = "zephyr,sdhc-spi-slot";
                    reg = <0>;
                    status = "okay";
                    mmc {
                        compatible = "zephyr,sdmmc-disk";
                        status = "okay";
                    };
                    spi-max-frequency = <24000000>;
            };
    };
    
    &pinctrl {
            spi3_default: spi3_default {
                    group1 {
                            psels = <NRF_PSEL(SPIM_SCK, 0, 19)>,
                                    <NRF_PSEL(SPIM_MOSI, 0, 21)>,
                                    <NRF_PSEL(SPIM_MISO, 0, 22)>;
                    };
            };
    
            spi3_sleep: spi3_sleep {
                    group1 {
                            psels = <NRF_PSEL(SPIM_SCK, 0, 19)>,
                                    <NRF_PSEL(SPIM_MOSI, 0, 21)>,
                                    <NRF_PSEL(SPIM_MISO, 0, 22)>;
                            low-power-enable;
                    };
            };
    };

    In prj.conf here is what I needed to use an external SD card.

    CONFIG_FILE_SYSTEM=y
    CONFIG_FAT_FILESYSTEM_ELM=y

Children
Related