Chan's FatFS read problem!

OP #680357
Rate this post
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.
1
int main()
2
{
3
  FATFS Fs;
4
  FIL FileObject;
5
  char data[2048];
6
  const char* FileName = "0:Basic.bin";  
7
  unsigned int ByteToRead = sizeof(data);
8
  unsigned int ByteRead;
9
  unsigned char SD = 0;
10
  
11
  memset(data, 0, sizeof(data));
12
  
13
  if( f_mount(SD, &Fs) != FR_OK )
14
    return 0;
15
  
16
  if(f_open(&FileObject, FileName, FA_OPEN_EXISTING|FA_READ) != FR_OK)
17
    return 0;
18

19
  FRESULT res;
20
  int f, e; f = e = 0;
21
  while(1)
22
  {    
23
    res = f_read(&FileObject, data, ByteToRead, &ByteRead);
24
    
25
    if(res == FR_OK)
26
    {
27
      f++;     //just to check number of cycles
28
      if(ByteRead == 0) break;
29
      e++;
30
    }
31
  }
32

33
  return 0;
34
}
Guest #680423
Rate this post
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.
OP #680429
Rate this post
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.

Reply

Please log in before posting.

or

Log in with Google account

Registration is free and takes only a minute.

Register now