cancel
Showing results for 
Search instead for 
Did you mean: 

Newbie Query: Storing Count values in Text File (FATFS-SDIO)

rumlyen
Associate II
Posted on January 02, 2016 at 20:59

I want to store consecutive Timer counts (32bit) to a text file. To avoid SDIO write for every count, I've tried to store in a string first and SDIO write after 40 counts (for example). Can we store values column-wise using '\n' in 'sprintf' command?

I have not succeeded. Also, what would be the appropriate string size?

Is there any other simpler methods to store buffer/count values in a Text file?

Thank you in advance for your kind reply.

char string[size];    // What would be appropriate string size?

void StoreSD(void)

{

 memset(&fs32, 0, sizeof(FATFS));

 res = f_mount(0, &fs32);

 memset(&fil, 0, sizeof(FIL));

 if (f_open(&fil, ''Data.TXT'', FA_OPEN_ALWAYS | FA_WRITE) == FR_OK)

  { 

    f_lseek(&fil, fil.fsize);

    f_write(&fil, string, strlen(string), &BytesWritten);

    f_close(&fil);

  }

}

int main()

{

 while (1)

   { 

     if(WriteIndex == 40)   // Storing 40 Timer counts (32-bit)

   {

   TIM5->CNT = 0;         // Reset TIM5 Count value

   StoreSD();

   WriteIndex = 0;

   .......

   .......

  

   }

   else

   {

   sprintf(&string[WriteIndex],''%ld%s'',TIM5->CNT,''\t'');   // Store 32-bit Count value to a string with a 'tab'. '\n' doesn't work.

  

   TIM5->CNT = 0;                 // Reset TIM5 Count value

   WriteIndex=WriteIndex+6;       // Next 32-bit Count store index. only 6 works. why '6' ?

   }

  

   }

  

  }
2 REPLIES 2
Posted on January 02, 2016 at 22:15

Using the description ''doesn't work'' isn't very helpful, define what you expect to occur, and define what happens.

It's a text file, you control the characters going into it. If you need a mix of TAB's, and CR/LF's then you need to control that. You can't assume how much each sprintf() adds, it does return a character count, doesn't it? You also can't assume a fixed number of bytes for accumulation, or invalid things like 40 being divisible by 6. I'd probably want to buffer at least 512 characters as this is the minimum sector size, buffering 32KB would be better, but you need to manage the output and the line breaks as you want them. Also mounting, and opening/closing in this fashion, is likely to be destructive to the media. Underneath it's still flash memory, with a finite life, and grinding over it like this is chronically inefficient.

#define SIZE 40
char string[SIZE + 16]; // space for counts, with some overflow
void StoreSD(void)
{
// Why do this over and over? Open a file, and f_sync() the content periodically
memset(&fil, 0, sizeof(FIL));
if (f_open(&fil, ''Data.TXT'', FA_OPEN_ALWAYS | FA_WRITE) == FR_OK)
{ 
f_lseek(&fil, fil.fsize);
f_write(&fil, string, strlen(string), &BytesWritten);
f_close(&fil);
}
}
int main()
{
// Mount the media once at start up
memset(&fs32, 0, sizeof(FATFS));
res = f_mount(0, &fs32);
// ideally just open the file, rather constantly open/write/close inefficiently
while (1)
{ 
if (WriteIndex >= SIZE) // Storing Timer counts (32-bit)
{
TIM5->CNT = 0; // Reset TIM5 Count value
StoreSD();
WriteIndex = 0;
.......
.......
}
else
{
WriteIndex += sprintf(&string[WriteIndex],''%ld\t'',TIM5->CNT); // Store 32-bit Count value to a string with a 'tab'. 
TIM5->CNT = 0; // Reset TIM5 Count value
} 
}
}

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
rumlyen
Associate II
Posted on January 03, 2016 at 01:35

Thank you very much. Your excellent solution works for me. Previously I was trying only '\n' rather than '\r\n' for new line.

f_sync is also working nicely, only one query: can I left open multiple txt file and use f_sync to write them periodically (say logging another buffer). I've not found many examples online about f_sync.

I request your guidance once more.