Page 1 of 1

files on sdcard written jut 50%

Posted: Thu Sep 25, 2014 6:02 pm
by ucasano
Hi, I am trying to write some text to a file in placed in an sd card, as reported below:

Code: Select all

   log_res = f_open(&log_file, "0:/log.txt", FA_OPEN_ALWAYS | FA_WRITE);
   if (log_res) {
      return;
   }
   f_sync(&log_file);

        /* append to the end of file */
   log_res = f_lseek(&log_file, f_size(&log_file));

        /* write data */
   log_res = f_write(&log_file, logbuffer, pbuffer - logbuffer, &log_bw);
   f_sync(&log_file);

   if (log_bw < (uint16_t)(pbuffer - logbuffer))
      setFsFull();
   
   /* log_res = f_truncate(&log_file); */
   log_res = f_close(&log_file);


after that, I have the following situation on the file "log.txt":

512 bytes text
512 bytes 0x00
512 bytes text
512 bytes 0x00
...

Is it right? I am using fat fs include in chibios 2.6.5, with 512 bytes sector size, and a 4GB sd card.
Can somebody please explain such a behavior?

Thank you in advance.
-g

Re: files on sdcard written jut 50%

Posted: Thu Sep 25, 2014 6:28 pm
by Giovanni
The code is not complete, probably f_write() writes more than you expect, pointers math?

Giovanni

Re: files on sdcard written jut 50%

Posted: Fri Sep 26, 2014 7:50 am
by ucasano
The missing code:

Code: Select all

static char logbuffer[256];

char *pbuffer = logbuffer;
char *string; /* this is an input parameter passed to the function */
uint16_t str_size = strlen(string);

memcpy(pbuffer, string, str_size);
pbuffer += str_size;


Re: files on sdcard written jut 50%

Posted: Fri Oct 03, 2014 2:23 pm
by lesto
you are copying string into logbuffer, why? are you calling it from another thread?

Also here you show us how you write 255 byte, but you have a problem at 512 byte, so the code what call that should also be included