I'm trying to set up a PIC18F67K22 to log data to an SD card, but I am having some trouble getting FatFs working properly. I implemented some low level code following the diskio.c API that is provided, and I am able to monitor the SPI data stream using a Saleae Logic Analyzer. Everything that I see going back and forth is sensible, but I cannot write data to the file.

I am able to create a file, but the file is always empty. After creating the file, my code no longer even attempts to communicate with the SD card (the SD chip select line never goes low during a recording).

Am I using FatFs' f_write and f_sync functions correctly here? What are some reasons as to why the functions would return FR_OK, even though they never write to the disk?

FIL file;
char text[64];

void update_record() {

static unsigned long timer;

if (record == 0 || ms - timer < 1000)

timer = ms;

UINT bw, i;
f_write(&file, "test", 4, &bw);

text[9] = ',';
text[10] = '\n';

for(i = 0; i < 8; i++) text[i] = 0x20;
floatToString(Values[0].d_double, "");
fr = f_write(&file, &text, 8, &bw);
fr = f_write(&file, &text[9], 1, &bw);

for(i = 0; i < 8; i++) text[i] = 0x20;
floatToString(Values[1].d_double, "");
fr = f_write(&file, &text, 8, &bw);
fr = f_write(&file, &text[9], 1, &bw);

fr = f_write(&file, &text[10], 1, &bw);

fr = f_sync(&file);

  • \$\begingroup\$ Maybe check all thouse return values for errors..? \$\endgroup\$ – Turbo J Jun 17 '15 at 0:00
  • \$\begingroup\$ I did. I used the debug tools in MPLAB X to step through. I said in my post that every single one came back as FR_OK. \$\endgroup\$ – kingcoyote Jun 17 '15 at 0:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.