File I/O

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
nelfata
Posts: 74
Joined: Wed Apr 30, 2014 10:50 pm

File I/O

Post by nelfata » Sat Oct 11, 2014 7:30 pm

Hello,
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.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: File I/O

Post by dhylands » Sat Oct 11, 2014 7:59 pm

I created a little shell program over here: https://github.com/dhylands/upy-shell 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.

nelfata
Posts: 74
Joined: Wed Apr 30, 2014 10:50 pm

Re: File I/O

Post by nelfata » Sat Oct 11, 2014 8:37 pm

Does the flush() or sync() work as expected?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: File I/O

Post by dhylands » Sat Oct 11, 2014 11:40 pm

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.

nelfata
Posts: 74
Joined: Wed Apr 30, 2014 10:50 pm

Re: File I/O

Post by nelfata » Sun Oct 12, 2014 12:33 am

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?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: File I/O

Post by dhylands » Sun Oct 12, 2014 5:33 am

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.

Post Reply