cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f103 cubx fatfs problem

omid hamdi
Senior
Posted on August 04, 2017 at 16:41

hi

i used open file 

unsigned char read_file (char *filename,unsigned long address,uint8_t *data){

FIL MyFile; /* File object */

char SDPath[4]; /* SD card logical drive path */

FATFS FatFs; /* Work area (file system object) for logical drive */

FRESULT res; /* FatFs function common result code */

uint32_t bytesread=2; /* File write/read counts */

if(FATFS_LinkDriver(&USER_Driver, SDPath) == 0)

{

if(f_mount(&FatFs, (TCHAR const*)SDPath, 0) != FR_OK)

{

/* FatFs Initialization Error */

showerror ('Fat Fs Not Int');

return 1;

}

else

{

if(f_open(&MyFile, filename , FA_READ) != FR_OK)

{

/* 'STM32.TXT' file Open for read Error */

showerror ('File Not Fined');

return 2;

}

else

{

res = f_read(&MyFile, data ,sizeof(data), (UINT*)&bytesread);

if((bytesread == 0) || (res != FR_OK))

{

/* 'STM32.TXT' file Read or EOF Error */

showerror (' Read Error ');

return 3;

}

else

{

f_close(&MyFile);

return 0;

}

}

}

}

FATFS_UnLinkDriver(SDPath);

}

bout sizeof(data) is wrong number not compatible with size file please advise me how can resolved 

4 REPLIES 4
Posted on August 04, 2017 at 16:56

>>

bout sizeof(data) is wrong number not compatible with size file please advise me how can resolved 

Pass the size of the data in as a parameter to the function, you don't want the size of the pointer, but the underlying buffer you are passing

ie

unsigned char read_file (char *filename,unsigned long address,uint8_t *data, uint32_t size)

{

...

res = f_read(&MyFile, data ,size, (UINT*)&bytesread);

...

}

where

uint8_t buffer[1024]

read_file(..., buffer, sizeof(buffer));

What's address?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
omid hamdi
Senior
Posted on August 04, 2017 at 17:05

address not used 

bout how can understand size of file which function can help to me for get size of file usually size of file must fined in fat  

i need open file and read until end of file 

Posted on August 04, 2017 at 17:27

So use f_size(&MyFile)

But your buffer needs to be large enough to hold the data, so the buffer behind 'data' passed in needs to be big enough, or you need to malloc() the buffer locally if you have a big enough heap.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on August 04, 2017 at 17:31

You can use smaller buffers, where you repeatedly call f_read until the bytes read is zero, or less than requested.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..