<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Unable to connect to SD card via SPI</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/116233/unable-to-connect-to-sd-card-via-spi</link><description>Hi, I would like to write a log to an SD card but I am completely unable to even connect to it using SPI in Zephyr/nRF Connect SDK. There seems to be some mismatch between the prj.conf and the overlay files which I do not know how to resolve: interestingly</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 11 Nov 2024 01:06:29 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/116233/unable-to-connect-to-sd-card-via-spi" /><item><title>RE: Unable to connect to SD card via SPI</title><link>https://devzone.nordicsemi.com/thread/509787?ContentTypeID=1</link><pubDate>Mon, 11 Nov 2024 01:06:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:55949ae4-c289-46d8-b6d5-536492e7d87f</guid><dc:creator>aniederberger</dc:creator><description>&lt;p&gt;&lt;span style="font-size:inherit;"&gt;&lt;strong&gt;Looking at the SPI signals showed that the chip-select pin is never pulled low,&lt;/strong&gt; which I interpret as&amp;nbsp;&lt;span style="font-family:&amp;#39;courier new&amp;#39;, courier;"&gt;disk_access_init(DISK_NAME)&lt;/span&gt;&amp;nbsp;not being able to&amp;nbsp;find the (correct) SD card Devicetree entry. Therefore, my hunch is that the problem is&amp;nbsp;either in how I am defining the &lt;span style="font-family:&amp;#39;courier new&amp;#39;, courier;"&gt;label&lt;/span&gt; in the overlay and/or in the &lt;span style="font-family:&amp;#39;courier new&amp;#39;, courier;"&gt;prj.conf&lt;/span&gt; file. In fact, &lt;span style="font-family:&amp;#39;courier new&amp;#39;, courier;"&gt;src/main.c&lt;/span&gt; shows that &lt;span style="font-family:&amp;#39;courier new&amp;#39;, courier;"&gt;device_get_binding(...)&lt;/span&gt; is able to locate the Devicetree entry. In contrast, I found diving into the source code that &lt;span style="font-family:&amp;#39;courier new&amp;#39;, courier;"&gt;disk_access_init(...)&lt;/span&gt;, which tries to locate the Devicetree entry&amp;nbsp;using &lt;span style="font-family:&amp;#39;courier new&amp;#39;, courier;"&gt;disk_access_get_di(...)&lt;/span&gt; cannot. Thoughts?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:75%;"&gt;PS: For reference, I am pasting below:&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style="font-size:75%;"&gt;the corrected overlay file: I played with putting the &lt;span style="font-family:&amp;#39;courier new&amp;#39;, courier;"&gt;label&lt;/span&gt; property at different places in the overlay structure but it did not seem to make a difference. Also, I tried different &lt;span style="font-family:&amp;#39;courier new&amp;#39;, courier;"&gt;psi-max-frequency&lt;/span&gt; values ranging from 240k&amp;nbsp;to 24M without success. Please note that the&amp;nbsp;&lt;a title="Zephyr documentation on Disk Access" href="https://docs.zephyrproject.org/latest/services/storage/disk/access.html"&gt;Zephyr documentation on Disk Access&lt;/a&gt;&amp;nbsp;defines the cs-gpios as &lt;span style="font-family:&amp;#39;courier new&amp;#39;, courier;"&gt;&amp;amp;porta&lt;/span&gt;, not as &lt;span style="font-family:&amp;#39;courier new&amp;#39;, courier;"&gt;&amp;amp;gpio0&lt;/span&gt;, and&amp;nbsp;it does not use &lt;span style="font-family:&amp;#39;courier new&amp;#39;, courier;"&gt;label&lt;/span&gt;, but &lt;span style="font-family:&amp;#39;courier new&amp;#39;, courier;"&gt;disk-name&lt;/span&gt;, which leads to a compilation error on my system.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size:75%;"&gt;src/main.c file without the debugging for-loop (which repeats the connection attempt 100 times) for simplicity.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size:75%;"&gt;prj.conf now including the LOG and GPIO configs just in case; I&amp;nbsp;played with that&amp;nbsp;adding and removing lines but could not get it to work properly. Is there a config that I should add and don&amp;#39;t know about?&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size:75%;"&gt;nRF Serial Terminal output showing that the device binding works vial the &lt;span style="font-family:&amp;#39;courier new&amp;#39;, courier;"&gt;label&lt;/span&gt; property but not when trying to&amp;nbsp;initialize disk access.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;// boards/nrf52dk_nrf52832.overlay:

&amp;amp;spi1 {
    status = &amp;quot;okay&amp;quot;;
    cs-gpios = &amp;lt;&amp;amp;gpio0 28 GPIO_ACTIVE_LOW&amp;gt;;
    sdhc0: sdhc@0 {
            compatible = &amp;quot;zephyr,sdhc-spi-slot&amp;quot;;
            reg = &amp;lt;0x0&amp;gt;;
            status = &amp;quot;okay&amp;quot;;
            label=&amp;quot;SD&amp;quot;;
            mmc {
                compatible = &amp;quot;zephyr,sdmmc-disk&amp;quot;;
                status = &amp;quot;okay&amp;quot;;
            };
            spi-max-frequency = &amp;lt;24000000&amp;gt;;
    };
};&lt;/pre&gt;&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;// src/main.c: 

#include &amp;lt;zephyr/device.h&amp;gt;
#include &amp;lt;zephyr/drivers/disk.h&amp;gt;
#include &amp;lt;zephyr/storage/disk_access.h&amp;gt;
#include &amp;lt;zephyr/logging/log.h&amp;gt;

LOG_MODULE_REGISTER(sd_problem, LOG_LEVEL_INF);

#define DISK_NAME &amp;quot;SD&amp;quot;

int main(void) {
    const struct device *SD_dev = device_get_binding(DISK_NAME);
    LOG_INF(&amp;quot;SD_dev pointer address: %i; name: %s.&amp;quot;, (int)SD_dev, SD_dev-&amp;gt;name);
    int ret = disk_access_init(DISK_NAME);
    if (ret) {
        LOG_ERR(&amp;quot;Failed to initialize SD card: %i&amp;quot;, ret);

    } else {
        LOG_INF(&amp;quot;Successfully initialized SD card&amp;quot;);
    return 0;
} &lt;/pre&gt;&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;# prj.conf:

CONFIG_GPIO=y
CONFIG_LOG=y
CONFIG_SPI=y
CONFIG_DISK_ACCESS=y
CONFIG_DISK_DRIVERS=y
CONFIG_SDMMC_STACK=y
CONFIG_SPI_NRFX=y
CONFIG_SDHC=y
&lt;/pre&gt;&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;# serial terminal output:

*** Booting nRF Connect SDK v2.7.0-5cb85570ca43 ***
*** Using Zephyr OS v3.6.99-100befc70c74 ***
[00:00:00.460,388] &amp;lt;inf&amp;gt; sd_problem: SD_dev pointer address: 36708; name: SD.
[00:00:00.460,418] &amp;lt;err&amp;gt; sd_problem: Failed to initialize SD card: -22&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Unable to connect to SD card via SPI</title><link>https://devzone.nordicsemi.com/thread/509769?ContentTypeID=1</link><pubDate>Sat, 09 Nov 2024 19:35:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fa7bfbc1-f509-4db7-8a92-7f008f492b66</guid><dc:creator>aniederberger</dc:creator><description>&lt;p&gt;&lt;span style="font-size:inherit;"&gt;Good catch. Just implemented the &lt;span style="font-family:&amp;#39;courier new&amp;#39;, courier;"&gt;GPIO_ACTIVE_LOW&lt;/span&gt; change in the overlay file and I checked with my logic analyzer (for-loop with 100 repeated attempts at&amp;nbsp;initializing). Same as before: no activity on&amp;nbsp;SPI pins, and &lt;span style="font-family:&amp;#39;courier new&amp;#39;, courier;"&gt;disk_access_init()&lt;/span&gt; returns &lt;span style="font-family:&amp;#39;courier new&amp;#39;, courier;"&gt;-22&lt;/span&gt;.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:inherit;"&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Unable to connect to SD card via SPI</title><link>https://devzone.nordicsemi.com/thread/509768?ContentTypeID=1</link><pubDate>Sat, 09 Nov 2024 19:29:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a38bc138-3126-4404-b303-00f934d9690d</guid><dc:creator>Turbo J</dc:creator><description>&lt;p&gt;CS is an active low signal, try &lt;code&gt;GPIO_ACTIVE_LOW &lt;/code&gt;in the overlay&lt;code&gt;. &lt;/code&gt;&lt;/p&gt;
&lt;p&gt;A logic analyser is recommended.&lt;code&gt;&lt;/code&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Unable to connect to SD card via SPI</title><link>https://devzone.nordicsemi.com/thread/509767?ContentTypeID=1</link><pubDate>Sat, 09 Nov 2024 19:11:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:650ccff7-50bc-4e3b-a729-339f0bd59a87</guid><dc:creator>aniederberger</dc:creator><description>&lt;p&gt;Hi Turbo J,&lt;br /&gt;Thanks for your reply. Yes, I believe that something like what you are suggesting will be the solution. I have tried variants of the following overlay and I was able to get it to compile, but not to connect to the disk.&lt;/p&gt;
&lt;p&gt;Here is the an overlay file I used similar to what you are suggesting. Note that I am using the depricated property &amp;quot;label&amp;quot;, here. What is the current way to connect to such a device?&lt;br /&gt;&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;// boards/nrf52dk_nrf52832.overlay:

&amp;amp;spi0 { status = &amp;quot;disabled&amp;quot;;};
&amp;amp;i2c1 { status = &amp;quot;disabled&amp;quot;;};

&amp;amp;spi1 {
    status = &amp;quot;okay&amp;quot;;
    cs-gpios = &amp;lt; &amp;amp;gpio0 28 GPIO_ACTIVE_HIGH &amp;gt;;
    label = &amp;quot;SD&amp;quot;;
    sdhc-spi-slot@0 {
        compatible = &amp;quot;zephyr,sdhc-spi-slot&amp;quot;;
        status = &amp;quot;okay&amp;quot;;
        reg = &amp;lt;0x0 &amp;gt;;
        spi-max-frequency = &amp;lt;8000000&amp;gt;;
        
        mmc {
            compatible = &amp;quot;zephyr,sdmmc-disk&amp;quot;;
            status = &amp;quot;okay&amp;quot;;
        };  
    };
};
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;It seems that the problem is precisely between the Devicetree and main.c&amp;nbsp;working with the correct GPIO ports&amp;nbsp;to do the SPi. In fact,&amp;nbsp;I tried disk_access_init(&amp;quot;SD&amp;quot;) using this overlay file&amp;nbsp;but&amp;nbsp;was never able to initialize the SD card, always getting error code -22, and I never even saw any communications when checking with my&amp;nbsp;oscilloscope.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Unable to connect to SD card via SPI</title><link>https://devzone.nordicsemi.com/thread/509753?ContentTypeID=1</link><pubDate>Sat, 09 Nov 2024 10:00:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9e391ea1-31cb-4a58-8f4a-76500096bbb8</guid><dc:creator>Turbo J</dc:creator><description>&lt;p&gt;I believe its in the DTS from some of the SD card examples, but you need to know what to look for:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;sdhc0: sdhc@0 {
		compatible = &amp;quot;zephyr,sdhc-spi-slot&amp;quot;;
		reg = &amp;lt;0x0 &amp;gt;;
// ... more stuff here, skipped
        mmc {
			compatible = &amp;quot;zephyr,sdmmc-disk&amp;quot;;
			status = &amp;quot;okay&amp;quot;;
			
		};
    };
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;The &amp;quot;Disk&amp;quot; part is on an internal node - maybe in order to support other SDIO cards. But these typically don&amp;#39;t support SPI mode.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>