cancel
Showing results for 
Search instead for 
Did you mean: 

FAT write problem uin sd card

rajesh23
Associate II
Posted on August 16, 2016 at 12:16

hi,

i am trying to do a data logger using FAT with sd card. in the below code trying to write in sd card with 100ms interval, the code has write into sdcard at only one time. latter it has gone to error


/*##-1- Link the micro SD disk I/O driver ##################################*/

if
(FATFS_LinkDriver(&SD_Driver, SDPath) == 0)

{

/*##-2- Register the file system object to the FatFs module ##############*/

if
(f_mount(&SDFatFs, (TCHAR 
const
*)SDPath, 0) != FR_OK)

{

/* FatFs Initialization Error */

Error_Handler();

}

else

{

/*##-3- Create a FAT file system (format) on the logical drive #########*/

/* WARNING: Formatting the uSD card will delete all content on the device */

// if(f_mkfs((TCHAR const*)SDPath, 0, 0) != FR_OK)

{

/* FatFs Format Error */

Error_Handler();

}

// else

{


while
(1)

{

/*##-4- Create and Open a new text file object with write access #####*/

if
(f_open(&MyFile, 
''STMTXT''
, FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)

{

/* 'STMTXT' file Open for write Error */

Error_Handler();

}

else

{



/*##-5- Write data to the text file ################################*/

res = f_write(&MyFile, wtext, bytesread, (
void
*)&byteswritten);

// while(byteswritten != bytesread)

{

BSP_LED_Toggle(LED1);

// HAL_Delay(1000);


}

f_close(&MyFile);

HAL_Delay(100);



}


}

{

}

what is the way to make a data logger using STM32F746G.disco baord regards Rajesh #stm32 #usb #fatfs #no-hablo-hal #fr_not_ready #stm32f746g-disco-
10 REPLIES 10
Posted on August 16, 2016 at 16:51

Don't know how you pasted the code in a manner that is unreadable I don't know.

Opening and closing the file, writing a few bytes, every 100ms is probably not going to fly. It will likely be too slow and will eat the flash array.

For the data loggers I've built, we have a reasonably large buffer that has sector/cluster alignment flushing as it fills completely, and is f_sync()'ed periodically.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
rajesh23
Associate II
Posted on August 17, 2016 at 08:00

hi,

Thanks for your reply here i have given the code below once again


/*##-1- Link the micro SD disk I/O driver ##################################*/

if
(FATFS_LinkDriver(&SD_Driver, SDPath) == 0)

{

/*##-2- Register the file system object to the FatFs module ##############*/

if
(f_mount(&SDFatFs, (TCHAR 
const
*)SDPath, 0) != FR_OK)

{

/* FatFs Initialization Error */

Error_Handler();

}

else

{

/*##-3- Create a FAT file system (format) on the logical drive #########*/

/* WARNING: Formatting the uSD card will delete all content on the device */

// if(f_mkfs((TCHAR const*)SDPath, 0, 0) != FR_OK)

{

/* FatFs Format Error */

Error_Handler();

}

// else

{


while
(1)

{

/*##-4- Create and Open a new text file object with write access #####*/

if
(f_open(&MyFile, 
''STMTXT''
, FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)

{

/* 'STMTXT' file Open for write Error */

Error_Handler();

}

else

{

/*##-5- Write data to the text file ################################*/

res = f_write(&MyFile, wtext, bytesread, (
void
*)&byteswritten);

// while(byteswritten != bytesread)

{

BSP_LED_Toggle(LED1);

// HAL_Delay(1000);


}

f_close(&MyFile);

HAL_Delay(100);



}


}

}

}

in above i am trying to open a file and write at every 100ms interval ,the code works for only one time later it gone to error. how can i find cluster size ? can you able give a link corresponding to this issue Regards Rajesh
rajesh23
Associate II
Posted on August 17, 2016 at 12:26 hi :clive1 Thanks......... you are awesome man !!!!!!!!!!! it works. i have tried code below

/*##-1- Link the micro SD disk I/O driver ##################################*/
if
(FATFS_LinkDriver(&SD_Driver, SDPath) == 0)
{
/*##-2- Register the file system object to the FatFs module ##############*/
if
(f_mount(&SDFatFs, (TCHAR 
const
*)SDPath, 0) != FR_OK)
{
/* FatFs Initialization Error */
Error_Handler();
}
else
{
if
(f_open(&MyFile, 
''STMTXT''
, FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
{
/* 'STMTXT' file Open for write Error */
Error_Handler();
}
else
{
f_sync(&MyFile);
}
while
(1)
{
/*##-4- Create and Open a new text file object with write access #####*/
res = f_lseek(&MyFile, f_size(&MyFile));
if
(res == FR_OK)
{
res = f_write(&MyFile, (
const
void
*)wtext, (bytesread-1), (
void
*)&byteswritten);
sprintf(&buffer[0],
''%d''
,Count++);
strcpy(&wtext[bytesread -2],buffer);
f_puts(&wtext[0],&MyFile);
f_sync(&MyFile);
BSP_LED_Toggle(LED1);
}
HAL_Delay(50);
}
f_close(&MyFile);
if
(f_mkfs((TCHAR 
const
*)SDPath, 0, 0) != FR_OK)
{
/* FatFs Format Error */
Error_Handler();
}
}
}

Regards Rajesh
rajesh23
Associate II
Posted on August 23, 2016 at 14:51 hi, Igo the same issue when the code ported to my application Free rtos, STM32F746G- disco , Ac6 ide 1. i am using two tasks i. GUI thread for for display ii.Logg thread to store 512 bytes. problem:

when the code runs the Loggthread Writes 512 bytes for 6 to 12 times only .

after that it is gone to

Disk error or invalid object

i want to logg some data periodically in SD card, what is the best method to do that ?.

This is very urgent please help me ,


/* Create GUI task */

osThreadDef(GUI_Thread, GUIThread, osPriorityNormal, 0, 2148);

osThreadCreate(osThread(GUI_Thread), NULL);



osThreadDef(LOGG_Thread, loggThread, osPriorityNormal, 0, 1524);

logHandle = osThreadCreate(osThread(LOGG_Thread), NULL);


if
(logHandle == NULL)

{

while
(1)

{

osDelay(1000);

BSP_LED_On(LED1);

}

}

else

{

/**

*pause the

*/

// vTaskSuspend(logHandle);

}

The below loggthread writes 512 bytes to uSD card.


static
void
loggThread( 
void
const
* argument )

{


static
uint8_t status= 0.;

char
* cptr = malloc(512*
sizeof
(
char
));

FRESULT res;


while
(1)

{

if
(status == 0x00)

{

if
(cptr !=NULL)

{

if
( openlogFile(&loggfile) == FR_OK)

{

int
i;


for
(i =0; i< 510; i++)

{

cptr[i] = 
'S'
;

}

cptr[i++] = 
'
'
;

cptr[i] = 
'\0'
;

status = 0x01;

}

else

{

delay_ms(100);

}

}

else

{

if
(HAL_UART_Transmit_IT(&UartHandle, (uint8_t*)
''alloc error
''
,11 ) != HAL_OK)

{


}

}


}

else

{

res = write2loggFile(&loggfile, cptr) ;

if
( res == FR_OK)

{

if
(HAL_UART_Transmit_IT(&UartHandle, (uint8_t*)
''write succeed 
''
,15 ) != HAL_OK)

{

delay_ms(200);

}

}

else

{

f_sync(&loggfile);

HAL_UART_Transmit_IT(&UartHandle, (uint8_t*)&res,1 ) ;


}


}

}

}

}






FRESULT openlogFile(FIL* MyFile)

{


FRESULT fr = FR_NOT_READY;


if
( BSP_SD_IsDetected() == SD_PRESENT )

{


if
(f_open(MyFile,
''loggfile.csv''
,FA_OPEN_EXISTING | FA_WRITE) != FR_OK)

{

if
(f_open(MyFile,
''loggfile.csv''
,FA_CREATE_NEW | FA_WRITE) != FR_OK)

{


}

else

{

f_puts(loggFileheader,MyFile);

f_sync(MyFile);

fr = FR_OK;


}

}

else

{

f_sync(MyFile);

fr = FR_OK;

}


}

return
fr;

}



FRESULT write2loggFile(FIL* MyFile, 
const
TCHAR* str)

{

FRESULT fr = FR_NOT_READY;


if
( BSP_SD_IsDetected() == SD_PRESENT )

{

fr = f_lseek(MyFile, f_size(MyFile));

if
( fr == FR_OK)

{

f_puts(str,MyFile);

f_sync(MyFile);

BSP_LED_Toggle(LED1);


fr = FR_OK;

}

}

return
fr;

}

Regards Rajesh
Posted on August 23, 2016 at 17:03

Unfortunately your application (F7, HAL, FREERTOS) is significantly divergent from my uses, you will need to ensure that access to the SDIO peripheral is serialized properly (one use at a time), that IRQs and data transfers are handled properly, and into compatible memory space (ie non-cached). You'll have to own the debugging and integration task, I would perhaps start by instrumenting the code so I could see what is going on below FatFs, and have sanity checks to ensure access was serialized correct, and where and how thing might get hung up.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
rajesh23
Associate II
Posted on August 31, 2016 at 08:50 hi, i'am still waiting for your reply... i am using single thread for FAT writing, no other threads were not disturbs the FAT low level driver or middleware.


char
mybuffer[] = 
''Unfortunately your application (F7, HAL, FREERTOS) is significantly divergent from my uses, you will need to ensure that access to the SDIO peripheral is serialized properly (one use at a time), that IRQs and data transfers are handled properly, and into compatible memory space (ie non-cached). You'll have to own the debugging and integration task, I would perhaps start by instrumenting the code so I could see what is going on below FatFs, and have sanity checks to ensure access was serialized correct, and 0
''
;


static
void
loggThread( 
void
const
* argument )

{


DRESULT dr;

FATFS fs;

FIL fil;

FRESULT fr, result =0;

UINT status;

fr = f_open(&fil, 
''fastrec.log''
, FA_READ | FA_WRITE | FA_OPEN_ALWAYS);

if
(fr == FR_OK)

{

if
(HAL_UART_Transmit_IT(&UartHandle, (uint8_t*)&result,
sizeof
(result) ) != HAL_OK)

{


}

}


uint8_t count =0;

HAL_SD_TransferStateTypedef sdStatus;


while
(1)

{


if
(fr)

{



}

else

{


PrintDec(count,&mybuffer[0]);

if
(count >99)

{

count =0;

}

else

{

count++;

}


result =f_write(&fil,mybuffer,
sizeof
(mybuffer),&status);

f_sync(&fil);

}


// if (HAL_UART_Transmit_IT(&UartHandle, (uint8_t*)''
'',1 ) != HAL_OK)

// {

//

// }


if
(HAL_UART_Transmit_IT(&UartHandle, (uint8_t*)&result,
sizeof
(result) ) != HAL_OK)

{


}

//

//

// if (HAL_UART_Transmit_IT(&UartHandle, (uint8_t*)&fil.flag,sizeof(fil.flag) ) != HAL_OK)

// {

//

// }

//

//

// if (HAL_UART_Transmit_IT(&UartHandle, (uint8_t*)&fil.flag,sizeof(fil.flag) ) != HAL_OK)

// {

//

// }

// sdStatus = BSP_SD_GetStatus();

//

// if (HAL_UART_Transmit_IT(&UartHandle, (uint8_t*)&sdStatus,sizeof(sdStatus) ) != HAL_OK)

// {

//

// }



if
(HAL_UART_Transmit_IT(&UartHandle, (uint8_t*)&fil.clust,
sizeof
(fil.clust) ) != HAL_OK)

{


}

HAL_Delay(100);



if
(HAL_UART_Transmit_IT(&UartHandle, (uint8_t*)
''
''
,1 ) != HAL_OK)

{


}


if
(HAL_UART_Transmit_IT(&UartHandle, (uint8_t*)&fil.sclust,
sizeof
(fil.sclust) ) != HAL_OK)

{


}




HAL_Delay(100);


}

}

in above code. i have found, there is no error read by function(

sdStatus = BSP_SD_GetStatus();

) at the time of FAT error 0x01 -> FR_DISK_ERR. it always gives>>>>>>> SD_TRANSFER_OK<<<<<<<<<<. Regards. Rajesh
Posted on August 31, 2016 at 18:26

i'am still waiting for your reply...

I cannot provide resources to debug your project, you will need to own them yourself. I've suggested some ways to debug this, start at the DISKIO.C layer

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
rajesh23
Associate II
Posted on September 02, 2016 at 09:52

Hi clive1

  1. I am trying to write to a file in sd card using FAT file system.

http://www.elm-chan.org/fsw/ff/00index_e.html

  1. Using free RTOS I have created two tasks

i)

                   

Thread to run GUI for user interface

ii)

                  

Thread Log data to SD card
  1. I have tested the SD card the FAT write code without free RTOS it was writing without no problem.
  2. When I write to sdcard using free RTOS it writes to few kilo bytes only then it becomes error “FR_DISK_ERRâ€�,                 /* (1) A hard error occurred in the low level disk I/O layer */ .
  3. I have enabled macro for thread safe by _FS_REENTRANT.

I have written code check the status of SD card before writing to it.

FRESULT

Write2LoggFile(

FIL

*MyFile ,

const

char

*information ,

UINT

length )

{

      

UINT

timeOut =10;

      

FRESULT

res =

FR_NOT_READY

;

      

while

(timeOut--)

       {

             

if

(

SD_status

(0) ==

RES_OK

)

              {

                     res =

f_lseek

(MyFile, f_size(MyFile));

                     res =

f_write

(MyFile,information,length,&timeOut);

                    

f_sync

(MyFile);

                    

break

;

              }

             

HAL_Delay

(50);

       }

      

if

(res ==

FR_OK

)

       {

              res =

f_sync

(&loggfile);

       }

      

return

res;

}

Even though it gave same problem after writing few kilo bytes to SD card.

Why the problem occurs when i used two threads ?

 

Regards

Rajesh

Posted on September 02, 2016 at 19:27

I don't know, but you're debugging at too high a level. Instrument the DISKIO.C level to understand WTH is going on there, what sequences break it and why you have a disk error, and how it propagates up into FatFs

I would make sure the DMA and SDIO interrupts and FIFO are being properly handled in the Write case.

I would look at the memory region being used, the alignment, and the interactions at the abstraction layers.

Check that the stack is big enough.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..