EmbDev.net

Forum: µC & Digital Electronics Chan's FatFS read problem!


von Al pacino D. (bigmicro)


Rate this post
0 useful
not useful
Hello my friends,

I'm really confused about the Chan's FatFS read-function!
I'm able to write and do loggings etc... but reading a file greater than 
1 kB doesn't work! I can read up to 1 kB (not including 1 kB).

The problem is I see only 0 (zero) above 1 kB!
My buffer is 2 kB, so it's not the bufferproblem.
int main()
{
  FATFS Fs;
  FIL FileObject;
  char data[2048];
  const char* FileName = "0:Basic.bin";  
  unsigned int ByteToRead = sizeof(data);
  unsigned int ByteRead;
  unsigned char SD = 0;
  
  memset(data, 0, sizeof(data));
  
  if( f_mount(SD, &Fs) != FR_OK )
    return 0;
  
  if(f_open(&FileObject, FileName, FA_OPEN_EXISTING|FA_READ) != FR_OK)
    return 0;

  FRESULT res;
  int f, e; f = e = 0;
  while(1)
  {    
    res = f_read(&FileObject, data, ByteToRead, &ByteRead);
    
    if(res == FR_OK)
    {
      f++;     //just to check number of cycles
      if(ByteRead == 0) break;
      e++;
    }
  }

  return 0;
}

von holger (Guest)


Rate this post
0 useful
not useful
I am missing two things in your code:
disk_initialize ()  How does it work without this ?
f_close()

I would try to read 2048 bytes in four 512 byte steps.
Only to see if f_read() has a problem reading more than
one sector.

von Al pacino D. (bigmicro)


Rate this post
0 useful
not useful
holger wrote:
> I am missing two things in your code:
> disk_initialize ()  How does it work without this ?
> f_close()
>
> I would try to read 2048 bytes in four 512 byte steps.
> Only to see if f_read() has a problem reading more than
> one sector.

Holger,

disk_initialize() is done when you call f_open().

f_close? hmmm I thought this was only needed when you write to SD, for 
flushing the data. But maybe it also works for reading! I'll try that.

So f_read() reads only 512 bytes max.?? I thought as long as the buffer 
can be filled.

Thank you.

von Andreas S. (andreas) (Admin) Flattr this


Rate this post
0 useful
not useful
f_read normally has no problems reading more than 512 bytes. But you can 
easily check how much it really read by looking at ByteRead.

von Al pacino D. (bigmicro)


Rate this post
0 useful
not useful
f_close() also didn't worked.
I managend by looping till the buffer is filled. But what if I want to 
know how big the file is, so I know when to stop copying to the buffer?

von holger (Guest)


Rate this post
0 useful
not useful
>But what if I want to
>know how big the file is, so I know when to stop copying to the buffer?

Ask FileObject.fsize ?

von Philipp B. (philipp_burch)


Rate this post
0 useful
not useful
You should compare ByteRead against ByteToRead, not against zero.

Reply

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.