<?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>How to Write to SD Card at high rate  ?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/18436/how-to-write-to-sd-card-at-high-rate</link><description>Hi All, 
 I have just started working on interfacing SD card with nrf52 using SDK 12.0. I have to write to SD card at a rate of 84 packets per second and each packet consists of 20 bytes. I am taking fatfs example in sdk 12.0 as reference. 
 I am sampling</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 22 Dec 2016 08:45:11 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/18436/how-to-write-to-sd-card-at-high-rate" /><item><title>RE: How to Write to SD Card at high rate  ?</title><link>https://devzone.nordicsemi.com/thread/71144?ContentTypeID=1</link><pubDate>Thu, 22 Dec 2016 08:45:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ee626f69-05e4-4ffe-a7f7-1b16919d3137</guid><dc:creator>Ole Bauck</dc:creator><description>&lt;p&gt;Can you print out the return code when you get the &amp;quot;unable to open or create file&amp;quot;? This should give you an indication of what is wrong. Something like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;NRF_LOG_INFO(&amp;quot;Unable to open or create file: &amp;quot; FILE_NAME &amp;quot;.\r\n&amp;quot;);
NRF_LOG_INFO(&amp;quot;Error code: %d\r\n&amp;quot;, ff_result);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can also try to check the return code of the f_close function to check if the file was successfully closed.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to Write to SD Card at high rate  ?</title><link>https://devzone.nordicsemi.com/thread/71143?ContentTypeID=1</link><pubDate>Mon, 19 Dec 2016 08:46:17 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1f3a0fd1-53b5-4067-8987-bcbbb038a02e</guid><dc:creator>Ankush</dc:creator><description>&lt;p&gt;Any updates Here?&lt;/p&gt;
&lt;p&gt;Can anyone guide me how can I resolve above mentioned issues?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to Write to SD Card at high rate  ?</title><link>https://devzone.nordicsemi.com/thread/71142?ContentTypeID=1</link><pubDate>Sat, 17 Dec 2016 11:54:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:217d2359-3d9c-406b-9a9a-1b6e61a812ad</guid><dc:creator>Ankush</dc:creator><description>&lt;p&gt;Here I observed one more thing. If I set the flag in main function as below:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;if(SD_flag==false) { SD_flag = true; } if(SD_flag==true) { SD_flag = false; SD_Write(); nrf_delay_ms(20); }&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;I am able to write large data to SD card with less restarting and that to after storing 300kB in SD card. Why it is happening?&lt;/p&gt;
&lt;p&gt;Is it related to SPI clock frequency? I have kept it as 250 kHz.
What should be the SPI0 clock frequency for SD card writing?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to Write to SD Card at high rate  ?</title><link>https://devzone.nordicsemi.com/thread/71141?ContentTypeID=1</link><pubDate>Sat, 17 Dec 2016 10:29:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:49836d68-2483-43de-be0d-f26291829d4d</guid><dc:creator>Ankush</dc:creator><description>&lt;p&gt;Hi Ole ,&lt;/p&gt;
&lt;p&gt;Thanks for the reply. As suggested by you,I have added the f_sync() function after f_open() and f_write() functions.But still &lt;code&gt;Unable to open or create file: ank.TXT&lt;/code&gt; error comes some times. Also, the return response of&lt;br /&gt;
f_sync() function is not &lt;code&gt;FR_OK&lt;/code&gt; all the time. Following is the small part of modified code:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;    void SD_Write()
{
    static FIL file;
    FRESULT ff_result;
    uint32_t bytes_written;
 
    
    uint8_t d[20] = {255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255};
    char buffer [100]={0};
    int n,eof=0;

    ff_result = f_open(&amp;amp;file, FILE_NAME1, FA_READ | FA_WRITE | FA_OPEN_APPEND);
    if (ff_result != FR_OK)
    {
        printf(&amp;quot;Unable to open or create file: &amp;quot; FILE_NAME1 &amp;quot;.\r\n&amp;quot;);
        (void) f_close(&amp;amp;file);
        return;
    }
    ff_result = f_sync(&amp;amp;file);
    
    n=sprintf (buffer,&amp;quot;%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\r&amp;quot;,d[0],d[1],d[2],d[3],d[4],d[5],d[6],d[7],d[8],d[9],d[10],d[11],d[12],d[13],d[14],d[15],d[16],d[17],d[18],d[19]);

    ff_result = f_write(&amp;amp;file, buffer, n, (UINT *) &amp;amp;bytes_written);
    if (ff_result != FR_OK)
    {
        printf(&amp;quot;Write failed\r\n.&amp;quot;);
    }

    ff_result = f_sync(&amp;amp;file);
    if (ff_result != FR_OK)
    {
        printf(&amp;quot;Synced Failed\r\n&amp;quot;);
    }
    
    //Check End of File for closing the file
    eof=f_eof(&amp;amp;file);
    if(eof)
    {
        //do nothing
    }
    else
    {
        printf(&amp;quot;eof=%d\r\n&amp;quot;,eof);
        (void) f_close(&amp;amp;file);
    }
}

static void fatfs_example()
{
    static FATFS fs;
    static DIR dir;
    FRESULT ff_result;
    DSTATUS disk_state = STA_NOINIT;

    // Initialize FATFS disk I/O interface by providing the block device.
    static diskio_blkdev_t drives[] =
    {
            DISKIO_BLOCKDEV_CONFIG(NRF_BLOCKDEV_BASE_ADDR(m_block_dev_sdc, block_dev), NULL)
    };

    diskio_blockdev_register(drives, ARRAY_SIZE(drives));

    printf(&amp;quot;Initializing disk 0 (SDC)...\r\n&amp;quot;);
    for (uint32_t retries = 3; retries &amp;amp;&amp;amp; disk_state; --retries)
    {
        disk_state = disk_initialize(0);
    }
    if (disk_state)
    {
        printf(&amp;quot;Disk initialization failed.\r\n&amp;quot;);
        return;
    }

    printf(&amp;quot;Mounting volume...\r\n&amp;quot;);
    ff_result = f_mount(&amp;amp;fs, &amp;quot;&amp;quot;, 1);
    if (ff_result)
    {
        printf(&amp;quot;Mount failed.\r\n&amp;quot;);
        return;
    }

    printf(&amp;quot;\r\n Listing directory: /\r\n&amp;quot;);
    ff_result = f_opendir(&amp;amp;dir, &amp;quot;/&amp;quot;);
    if (ff_result)
    {
        printf(&amp;quot;Directory listing failed!\r\n&amp;quot;);
        return;
    }
    
     //remove perticular file 
    ff_result = f_unlink (&amp;quot;ANK.TXT&amp;quot;);
    if (ff_result != FR_OK)
    {
      printf(&amp;quot;NoFile : Write failed\r\n.&amp;quot;);
    }
    else
    {
        printf(&amp;quot;file removed = ANK.TXT \r\n&amp;quot;);
    }
  
    
    return;
}

int main(void)
{
    bool     erase_bonds;
    // Initialize.
    gpio_init();
    timers_init();
    uart_init();
    buttons_leds_init(&amp;amp;erase_bonds);
    ble_stack_init();
    peer_manager_init(erase_bonds);
    gap_params_init();
    advertising_init();
    gatt_init();
    services_init();
    sensor_simulator_init();
    conn_params_init();
    saadc_init();
    
    // Start execution.
    application_timers_start();
    advertising_start();
    printf(&amp;quot;UART Start...\r\n&amp;quot;);
    fatfs_example();

    
    // Enter main loop.
    for (;;)
    {

        if(SD_flag==true)
        {
            SD_flag = false;
            SD_Write();
        }

       power_manage();
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In Above code,&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;SD_flag&lt;/code&gt; is set after 12ms when &lt;code&gt;NRF_DRV_SAADC_EVT_DONE&lt;/code&gt; in SAADC callback occurs.&lt;/li&gt;
&lt;li&gt;I am storing 20 bytes means 80 unsigned characters in file in SD_write function.&lt;/li&gt;
&lt;li&gt;The code restarting issue is still there with IAR as my IDE.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Can you suggest something to deal with this?&lt;/p&gt;
&lt;p&gt;Is it because of high sampling rate of SAADC?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to Write to SD Card at high rate  ?</title><link>https://devzone.nordicsemi.com/thread/71140?ContentTypeID=1</link><pubDate>Fri, 16 Dec 2016 15:06:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dc2b60ec-7522-4d8c-b5a3-ac1efa32b417</guid><dc:creator>Ole Bauck</dc:creator><description>&lt;p&gt;You should check why your code is restarting. Most of the time it is because an error condition was met, see &lt;a href="https://devzone.nordicsemi.com/question/60125/my-device-is-freezing-and-restarting/"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Maybe you get the &lt;code&gt;Unable to open or create file: ank.TXT&lt;/code&gt; because you try to open the file while it is not closed yet.&lt;/p&gt;
&lt;p&gt;Can you try to use f_sync(...) as described &lt;a href="http://elm-chan.org/fsw/ff/en/appnote.html#critical"&gt;here&lt;/a&gt; to minimize the critical sections? If you use f_sync(..) you can leave the file open instead of closing it after every write, suitable for datalogging, see &lt;a href="http://elm-chan.org/fsw/ff/en/sync.html"&gt;here&lt;/a&gt;. Then you can close it before you remove the SD card or when the logging is done.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>