files on sdcard written jut 50%

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
ucasano
Posts: 57
Joined: Mon Aug 06, 2012 1:05 pm
Has thanked: 1 time

files on sdcard written jut 50%

Post 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
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: files on sdcard written jut 50%

Post by Giovanni »

The code is not complete, probably f_write() writes more than you expect, pointers math?

Giovanni
ucasano
Posts: 57
Joined: Mon Aug 06, 2012 1:05 pm
Has thanked: 1 time

Re: files on sdcard written jut 50%

Post 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;

lesto
Posts: 55
Joined: Thu May 22, 2014 11:27 am

Re: files on sdcard written jut 50%

Post 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
Post Reply