<?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>SDCard insert and mount</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/82205/sdcard-insert-and-mount</link><description>SDK 17 
 Soft device 7.2.0 
 nRF52840 
 
 Started with the &amp;#39;fatfs&amp;#39; example, updated it to allow uninit, and to not reinit the IO if init is called multiple times. 
 it&amp;#39;s working well. mount / unmount / remount, read / write, dir all works 
 However, if</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 30 Nov 2021 18:30:26 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/82205/sdcard-insert-and-mount" /><item><title>RE: SDCard insert and mount</title><link>https://devzone.nordicsemi.com/thread/341379?ContentTypeID=1</link><pubDate>Tue, 30 Nov 2021 18:30:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8eb5ae99-9709-48a4-a5d0-f41aee30a2c5</guid><dc:creator>Keith V</dc:creator><description>&lt;p&gt;Found it. In the eject function I was never calling disk_uninitialize() due to the check against m_disk_state being wrong it should look like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;for (uint32_t retries = 3; retries &amp;amp;&amp;amp; (m_disk_state != STA_NOINIT); --retries)
      {
         m_disk_state = disk_uninitialize(0);

      }

      if (m_disk_state != STA_NOINIT)
      {
         NRF_LOG_INFO(&amp;quot;Disk uninitialization failed.&amp;quot;);
         return;
      }&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SDCard insert and mount</title><link>https://devzone.nordicsemi.com/thread/341363?ContentTypeID=1</link><pubDate>Tue, 30 Nov 2021 16:16:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4af9226b-13a5-4519-97f4-4fd3cf58a0ed</guid><dc:creator>Keith V</dc:creator><description>&lt;p&gt;&amp;quot;Wait for the SD card to be detected?&amp;quot; I don&amp;#39;t have the detect line connected to any code.&lt;/p&gt;
&lt;p&gt;Everything is done manually through a CLLI at this point&lt;/p&gt;
&lt;p&gt;Before removing the code I un-initialize and unmount the card:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;static void sdEject(void)
{
   if (f_mount(NULL, &amp;quot;&amp;quot;, 0))
   {
      NRF_LOG_INFO(&amp;quot;UnMount failed\r\n&amp;quot;);
   }
   else
   {
      NRF_LOG_INFO(&amp;quot;SD Card UnMounted&amp;quot;);
      SDCinited = false;

      for (uint32_t retries = 3; retries &amp;amp;&amp;amp; m_disk_state; --retries)
      {
         m_disk_state = disk_uninitialize(0);

      }

      if (m_disk_state)
      {
         NRF_LOG_INFO(&amp;quot;Disk uninitialization failed.&amp;quot;);
         return;
      }

      NRF_LOG_INFO(&amp;quot;disk state : %d&amp;quot;, m_disk_state);
      NRF_LOG_INFO(&amp;quot;disk unitialized&amp;quot;);
   }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I&amp;#39;m manually&amp;nbsp; calling init() from the CLI after I put the disk in&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;static bool is_sd_present(void)
{
   bool pin = false;

   if( GPIOinited)
   {
      //detect pin grounded when sd card inserted
      pin =  nrf_gpio_pin_read(SDC_CD_PIN) == 0;
   }
   else { NRF_LOG_INFO(&amp;quot;SD Card IO not inited&amp;quot;);}

   return pin;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;void SDInit(nrf_cli_t const * p_cli, size_t argc, char ** argv)
{
    FRESULT ff_result;
    static bool registered;

    if(false == SDCinited)
    {
       GPIOInit();

       NRF_LOG_INFO(&amp;quot;Initializing SD Card&amp;quot;);

       if(is_sd_present())
       {
       
          if(false == registered)
          {
             registered = true;

             // Initialize FATFS disk I/O interface by providing the block device.
             static diskio_blkdev_t drives[] =
             {
                  DISKIO_BLOCKDEV_CONFIG(NRF_BLOCKDEV_BASE_ADDR(m_SDCard_block_dev_sdc, block_dev), NULL),
             };

             diskio_blockdev_register(drives, ARRAY_SIZE(drives));
          }

          m_disk_state = STA_NOINIT;

          NRF_LOG_INFO(&amp;quot;Initializing disk 0 (SDC)...&amp;quot;);
          for (uint32_t retries = 3; retries &amp;amp;&amp;amp; m_disk_state; --retries)
          {
             NRF_LOG_INFO(&amp;quot;retry.&amp;quot;);
              m_disk_state = disk_initialize(0);
          }
          if (m_disk_state)
          {
              NRF_LOG_INFO(&amp;quot;Disk initialization failed: %x&amp;quot;, m_disk_state);
              return;
          }

          uint32_t blocks_per_mb = (1024uL * 1024uL) / m_SDCard_block_dev_sdc.block_dev.p_ops-&amp;gt;geometry(&amp;amp;m_SDCard_block_dev_sdc.block_dev)-&amp;gt;blk_size;
          uint32_t capacity = m_SDCard_block_dev_sdc.block_dev.p_ops-&amp;gt;geometry(&amp;amp;m_SDCard_block_dev_sdc.block_dev)-&amp;gt;blk_count / blocks_per_mb;
          NRF_LOG_INFO(&amp;quot;Capacity: %d MB&amp;quot;, capacity);

          NRF_LOG_INFO(&amp;quot;Mounting volume...&amp;quot;);

          memset(&amp;amp;fs, 0, sizeof(fs));

          ff_result = f_mount(&amp;amp;fs, &amp;quot;&amp;quot;, 1);
          if (ff_result)
          {
              NRF_LOG_INFO(&amp;quot;Mount failed; %x&amp;quot;, ff_result);
              return;
          }
          else
          {
               NRF_LOG_INFO(&amp;quot;Mount succeeded&amp;quot;);
          }
 
          SDCinited = true;

      }
      else
      {
         NRF_LOG_INFO(&amp;quot;No SD Card Inserted!&amp;quot;);
      }
   }
   else
   {
      NRF_LOG_INFO(&amp;quot;SD Card already initialized!&amp;quot;);
   }


    return;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t see a reset() anywhere in the Fat files (ff.c) or the disk files (diskio_blkdev.c) Do you mean a processor reset???&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SDCard insert and mount</title><link>https://devzone.nordicsemi.com/thread/341230?ContentTypeID=1</link><pubDate>Tue, 30 Nov 2021 10:03:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7863c6c0-151c-44b2-b95f-29ad5adea5e0</guid><dc:creator>Simonr</dc:creator><description>&lt;p&gt;Hi Keith&lt;/p&gt;
&lt;p&gt;An FR_DISK_ERR from the find_volume() function points to an error in the disk I/O layer. What I&amp;#39;m guessing is happening, is that the mount process is already in progress when you reinsert the SD card, causing the mount to have a disk error. Can you try adding a reset when this occurs, so it can try mounting again, as I think the mount starting over should return successfully. Alternatively, wait for the SD card to be detected before starting the mount process.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Simon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>