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 );
}