Page 1 of 1

File I/O

Posted: Sat Oct 11, 2014 7:30 pm
by nelfata
I am wondering if the following File functions are reliably working and were tested:
- file and directory creation
- file open with append flag
- file seek
- file flush
- file close

I did some timing measurement, it seems that most of these functions take about 40ms to execute. I am curious why the timings are so similar

Are there any particular SD cards that could behave differently? Does capacity matter?

I have a logging function writing to the SD card, it is rare but occasionally the files become corrupt.

If someone has made some tests, I would be interested to know the results.

Thank you.

Re: File I/O

Posted: Sat Oct 11, 2014 7:59 pm
by dhylands
I created a little shell program over here: that uses a bunch of those typesof file I/O

If I had to guess, I would say that if you have mass storage enabled, then corruption isn't surprising.

Re: File I/O

Posted: Sat Oct 11, 2014 8:37 pm
by nelfata
Does the flush() or sync() work as expected?

Re: File I/O

Posted: Sat Oct 11, 2014 11:40 pm
by dhylands
flush will write unwritten data for a file. sync will cause unwritten data for all storage areas to be flushed.

However, when mass storage is enabled, it thinks that it owns the volume and will write blocks with no consideration for what the pyboard is doing.

So if the host decides to flush a block that the pyboard also decided to write data to, then it's entirely possible that corruption will occur. The only way to prevent this is to disable mass storage.

Re: File I/O

Posted: Sun Oct 12, 2014 12:33 am
by nelfata
So what I understand from you now, is that mass storage access from the host PC connected through the MP USB interface could interfere with the MP access to the SD Card.

If the host is not connected, the flush and sync should perform the same function.
I looked briefly through the code, and the flush seems to be acting on the internal flash only.

Am I mistaken?

Re: File I/O

Posted: Sun Oct 12, 2014 5:33 am
by dhylands
flush is a per-file thing, so it works on internal and sdcard.

sync is only needed on the intenral flash (since the internal flash needs to use a cache). For sdcard, the write happens, it happens right away.

Because the internal flash pages are much larger than file system block sizes, the internal cache is used.

You need to do a flush and a sync to ensure that files on the internal flash are actually written to flash. For the sdcard, only a flush is required.