2016-08-16 03:16 AM
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-
2016-08-16 07:51 AM
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.2016-08-16 11:00 PM
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
2016-08-17 03:26 AM
/*##-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
2016-08-23 05:51 AM
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
2016-08-23 08:03 AM
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.
2016-08-30 11:50 PM
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. Rajesh2016-08-31 09:26 AM
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 layer2016-09-02 12:52 AM
http://www.elm-chan.org/fsw/ff/00index_e.html
i)
Thread to run GUI for user interface
ii)
Thread Log data to SD card
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 ?
RegardsRajesh
2016-09-02 10:27 AM
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.