Page 1 of 1

SDC Card Corruption

Posted: Tue Feb 24, 2015 12:29 pm
by Tabulous
Hi just wondering if any one had any suggestions on this.

I'm suffering with intermittent SD-Card corruption, this only occurs when writing to the card and is noticeable when you take the card out and place into a windows machine, it complains about the log file been corrupt. 7-8 times out of 10 this everything works fine, but its the 2-3 times where the card log file is corrupt that concerns me. The power to the board is on all the time. The only time the power to the card is removed is when it is taken out to view on a windows machine.


Basically there is only one thing that access the card to write, my logging thread. Log messages are sent to the log thread using the synchronous msg method. Below is the code for this thread.



Code: Select all

static msg_t ThreadLOG(void *arg)
{
Thread *p;
UINT wbytes;
struct tm now;
struct log *m;
//uint32_t ticks;
FRESULT fResult;
RTCTime ts = {0,0,FALSE,0};


    (void)arg;
    chRegSetThreadName("logd");


    while( TRUE )
    {
        p = chMsgWait();

        if( !( fatMounted() ))
        {
            EDC_DEBUG( DBG_MASK_LOG, "LOG: fsys not mounted\r\n" );
        }
        else
        {
            fResult = f_chdir( "/LOGS" );

            if( fResult != FR_OK )
            {
                EDC_DEBUG( DBG_MASK_LOG, "LOG: f_chdir err &d\r\n", fResult );
            }
            else
            {
                rtcGetTime( &RTCD1, &ts );
                rtcGetTimeTm( &RTCD1, &now );

                m = (struct log *)chMsgGet(p);

                if( start.tm_mday != now.tm_mday )
                {
                    datePurgeLogfile( &now, PURGE_OLD );
                    datePurgeLogfile( &now, PURGE_NEW );
                }

                chsnprintf( fname, sizeof(fname),
                            LOGFILE_NAME,
                            now.tm_mday,
                            now.tm_mon + 1,
                            now.tm_year - 100 );

                fResult = f_open( &file, fname,
                                  FA_OPEN_ALWAYS | FA_WRITE );

                if( fResult != FR_OK )
                {
                    EDC_DEBUG( DBG_MASK_LOG, "LOG: f_open err &d\r\n", fResult );
                }
                else
                {
                    fResult = f_lseek(&file, file.fsize);

                    if( fResult != FR_OK )
                    {
                        EDC_DEBUG( DBG_MASK_LOG, "LOG: f_lseek err &d\r\n", fResult );
                    }
                    else
                    {
                        chsnprintf( logstr,
                                    sizeof(logstr),
                                    LOGFILE_ENTRY_FORMAT,
                                    now.tm_mday,
                                    now.tm_mon + 1,
                                    now.tm_year - 100,
                                    now.tm_hour,
                                    now.tm_min,
                                    now.tm_sec,
                                    ts.tv_msec,
                                    /*ticks,*/
                                    p->p_name,
                                    LOGIDSTR[m->id],
                                    (const char *)m->msg );

                        fResult = f_write( &file, logstr, strlen(logstr), &wbytes );

                        if( fResult != FR_OK )
                        {
                            EDC_DEBUG( DBG_MASK_LOG, "LOG: f_write err &d\r\n", fResult );
                        }
                    }

                    f_close( &file );
                }

                chMsgRelease(p , 0);
            }
        }
    }

    return( 0 );
}

Re: SDC Card Corruption

Posted: Tue Feb 24, 2015 12:50 pm
by Giovanni
It would interesting to know what is corrupted exactly, that could give us an hint about the problem. Is there any tool able to analyze a FAT file system?

You could try to implement a read-after-write verification at block level, into the binding module and see if there are failed writes (and eventually implement a write retry).

Giovanni

Re: SDC Card Corruption

Posted: Tue Feb 24, 2015 1:12 pm
by Tabulous
Is it possible the debugger could cause some issue, i.e. staarting and stoping the core under debug ?

I'm just trying it running standalone, switching power on and off repeatedly and it seems very hard to corrupt, well i've not got it to do it so far.

Re: SDC Card Corruption

Posted: Tue Feb 24, 2015 1:15 pm
by Giovanni
It is possible a stop could interfere with the SDC driver if an operation is in progress.

Giovanni