EmbDev.net

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


von Al pacino D. (bigmicro)


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
}

von holger (Guest)


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.

von Al pacino D. (bigmicro)


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.

von Andreas S. (andreas) (Admin)


Rate this post
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
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
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
useful
not useful
You should compare ByteRead against ByteToRead, not against zero.

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.