FAT32: Closing and reopening the same file

General discussion on mikroPascal PRO for dsPIC30/33 and PIC24.
Post Reply
Author
Message
erkileitaru
Posts: 28
Joined: 29 Dec 2009 08:53

FAT32: Closing and reopening the same file

#1 Post by erkileitaru » 06 Jan 2018 20:13

Hi. I made a datalogger on Clicker2 PIC24 board. Data is logged to SD card. I want to periodically save the file to minimize the risk of data loss. I set up timer to close the file and then open again to make it save. The problem is that i need to init again the Fat32, otherwise file handlers return -1 after opening.

if SalvestusSalvestusloendur=0 then
begin
SalvestusSalvestusloendur:=60;
FAT32_Close(SalvestusLogifhandle);
FAT32_Close(SalvestusNMEAfhandle);
Fat32_init;
SalvestusLogifhandle :=FAT32_Open(@SalvestusLogi_Nimi,FILE_APPEND_F32);
SalvestusNMEAfhandle :=FAT32_Open(@SalvestusNMEA_Nimi,FILE_APPEND_F32);
end;

My question is - does the FAT32 library need some time between closing and opening or why it only works with FAT32_init between closing and opening?

EDIT: Another question - can I see FAT32 library source to find out what exactly causes specific error codes?

User avatar
strahinja.jacimovic
mikroElektronika team
Posts: 200
Joined: 18 Dec 2017 18:19

Re: FAT32: Closing and reopening the same file

#2 Post by strahinja.jacimovic » 08 Jan 2018 12:42

Hi,

MMC/SD card and MMC libraries must be initialized for file operations.

The libraries are defined that way.

The Init function initializes MMC/SD card, reads MMC/SD FAT16 boot sector and extracts necessary data needed by the library.

The Close function closes currently opened file and undoes everything the Init function did.

So, yes you have to Init the library after using the close function.

As for the source code, we cannot really provide that. Sorry.

Kind regards,
Strahinja Jacimovic

erkileitaru
Posts: 28
Joined: 29 Dec 2009 08:53

Re: FAT32: Closing and reopening the same file

#3 Post by erkileitaru » 08 Jan 2018 12:50

Thanks. This is vital information. I think it was not mentioned in library help file.

User avatar
strahinja.jacimovic
mikroElektronika team
Posts: 200
Joined: 18 Dec 2017 18:19

Re: FAT32: Closing and reopening the same file

#4 Post by strahinja.jacimovic » 08 Jan 2018 13:10

Hi,

You're welcome.

Kind regards,
Strahinja Jacimovic

erkileitaru
Posts: 28
Joined: 29 Dec 2009 08:53

Re: FAT32: Closing and reopening the same file

#5 Post by erkileitaru » 08 Jan 2018 16:36

But still - If I have 2 files opened and close one of them. Do I need to close another one also? Because as you described it, I cannot save to the still opened file. Sounds illogical to me...

User avatar
strahinja.jacimovic
mikroElektronika team
Posts: 200
Joined: 18 Dec 2017 18:19

Re: FAT32: Closing and reopening the same file

#6 Post by strahinja.jacimovic » 08 Jan 2018 16:43

Hi,

When you have 2 opened files, it will work as long as you have the opened one selected for the operation.

Kind regards,
Strahinja Jacimovic

erkileitaru
Posts: 28
Joined: 29 Dec 2009 08:53

Re: FAT32: Closing and reopening the same file

#7 Post by erkileitaru » 08 Jan 2018 17:21

But if Close undoes everything that Init did, then the other opened file is not usable anymore.

I have problem that if I close file, I cannot create new file before making Init again.

User avatar
strahinja.jacimovic
mikroElektronika team
Posts: 200
Joined: 18 Dec 2017 18:19

Re: FAT32: Closing and reopening the same file

#8 Post by strahinja.jacimovic » 08 Jan 2018 17:54

Hi,

I see your dilemma.

Maybe you could use the function Activate to select the active file.

For example, open 4 files, select the active one. When you're done reading or writing or whatever, select a different one and so on.

Then, once you have finished just close all of them. This way you wouldn't have to initialize over and over again, and then just close all of the opened files.

Kind regards,
Strahinja Jacimovic

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re: FAT32: Closing and reopening the same file

#9 Post by Dany » 08 Jan 2018 19:33

strahinja.jacimovic wrote:The Close function closes currently opened file and undoes everything the Init function did.
So, yes you have to Init the library after using the close function.
Are you sure it works this way? I tought that a close operation only closes one file, that is, it updates the FAT information for that file (this is the normal case for FAT based operating systems). So, all the work init has done should stay intact I think...
The reason why re-opening returns -1 is not clear. Re-opening should be possible without problems...
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

erkileitaru
Posts: 28
Joined: 29 Dec 2009 08:53

Re: FAT32: Closing and reopening the same file

#10 Post by erkileitaru » 08 Jan 2018 20:24

Activate? I was talking about FAT32 library. I cant see any commands to choose active file. I included the list of routines. I want also know how the SPI part works. Does it use interrupts or some timeout timers? Does it hurt if I have other interrupts enabled while the thing is writing to file?

Library Routines:
FAT32_Init
FAT32_Format
FAT32_Dir
FAT32_MakeDir
FAT32_ChangeDir
FAT32_Exists
FAT32_Rename
FAT32_Delete
FAT32_DeleteRec
FAT32_Open
FAT32_Close
FAT32_Read
FAT32_ReadSwap
FAT32_GetAttr
FAT32_SetAttr
FAT32_FindFirst
FAT32_FindNext
FAT32_Write
FAT32_WriteSwap
FAT32_Tell
FAT32_Seek
FAT32_SeekSwap
FAT32_MakeSwap
FAT32_Eof
FAT32_Size
FAT32_SetTime
FAT32_GetCTime
FAT32_IncTime
FAT32_GetMTime
FAT32_GetError
FAT32_SectToClust
FAT32_ClustToSect
FAT32_ScanDisk
FAT32_GetFreeSpace
FAT32_GetFileHandle

Post Reply

Return to “mikroPascal PRO for dsPIC30/33 and PIC24 General”