what file system

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
KJM
Posts: 158
Joined: Sun Nov 18, 2018 10:53 pm
Location: Sydney AU

what file system

Post by KJM » Mon Jan 17, 2022 8:42 pm

Does the latest micropython (1.17.0, 2021-09-02) use FAT or LittleFS? Is there a sys or os cmd that might tell me?

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: what file system

Post by Roberthh » Tue Jan 18, 2022 7:13 am

You can have either files system on the flash of your device, depding on how you initialize it. AFAIK, the default is LittleFS. Damien once made a little script that checks the file system:

Code: Select all

"""
Dump info about a filesystem.
"""

import sys, struct

def pr(addr, label, data):
    if type(data) == bytes or type(data) == bytearray:
        data = ':'.join('%02x' % b for b in data)
    print('%04x  %-32s %s' % (addr, label, data))

def decode_bootsec_fat(b):
    print('FAT filesystem')
    pr(0, 'jump', b[0:3])
    pr(3, 'OEM name', str(b[3:11], 'ascii'))
    pr(11, 'sector size (bytes)', struct.unpack_from('<H', b, 11)[0])
    pr(13, 'cluster size (sectors)', struct.unpack_from('<B', b, 13)[0])
    pr(14, 'reserved area (sectors)', struct.unpack_from('<H', b, 14)[0])
    pr(16, 'number of FATs', struct.unpack_from('<B', b, 16)[0])
    pr(17, 'size of root dir area', struct.unpack_from('<H', b, 17)[0])
    pr(19, 'volume size (sectors)', struct.unpack_from('<H', b, 19)[0])
    pr(21, 'media descsriptor', hex(struct.unpack_from('<B', b, 21)[0]))
    pr(22, 'FAT size (sectors)', struct.unpack_from('<H', b, 22)[0])
    pr(24, 'track size (sectors)', struct.unpack_from('<H', b, 24)[0])
    pr(26, 'number of heads', struct.unpack_from('<H', b, 26)[0])
    pr(28, 'volume offset (sectors)', struct.unpack_from('<L', b, 28)[0])
    pr(32, 'volume size (32-bit) (sectors)', struct.unpack_from('<I', b, 32)[0])
    pr(36, 'physical drive number', struct.unpack_from('<B', b, 36)[0])
    pr(37, 'error flag', hex(struct.unpack_from('<B', b, 37)[0]))
    pr(38, 'extended boot signature', hex(struct.unpack_from('<B', b, 38)[0]))
    pr(39, 'volume serial number', hex(struct.unpack_from('<L', b, 39)[0]))
    pr(43, 'volume label', str(b[43:51], 'ascii'))
    pr(54, 'filesystem type', str(b[54:62], 'ascii'))
    pr(510, 'signature', hex(struct.unpack_from('<H', b, 510)[0]))


def decode_bootsec_lfs1(b):
    print("Littlefs v1 filesystem")
    pr(24, "type", struct.unpack_from("<I", b, 24)[0])
    pr(25, "elen", struct.unpack_from("<I", b, 25)[0])
    pr(26, "alen", struct.unpack_from("<I", b, 26)[0])
    pr(27, "nlen", struct.unpack_from("<I", b, 27)[0])
    pr(28, "block_size", struct.unpack_from("<I", b, 28)[0])
    pr(32, "block_count", struct.unpack_from("<I", b, 32)[0])
    pr(36, "version", hex(struct.unpack_from("<I", b, 36)[0]))
    pr(40, "magic", str(b[40:40 + 8], 'ascii'))


def decode_bootsec_lfs2(b):
    print("Littlefs v2 filesystem")
    pr(8, "magic", str(b[8:8 + 8], "ascii"))
    pr(20, "version", hex(struct.unpack_from("<I", b, 20)[0]))
    pr(24, "block_size", struct.unpack_from("<I", b, 24)[0])
    pr(28, "block_count", struct.unpack_from("<I", b, 28)[0])
    pr(32, "name_max", struct.unpack_from("<I", b, 32)[0])
    pr(36, "file_max", struct.unpack_from("<I", b, 36)[0])
    pr(40, "attr_max", struct.unpack_from("<I", b, 40)[0])


def decode_bootsec(b):
    if b[40:48] == b"littlefs":
        decode_bootsec_lfs1(b)
    elif b[8:16] == b"littlefs":
        decode_bootsec_lfs2(b)
    else:
        decode_bootsec_fat(b)

def main():
    if sys.platform == 'pyboard':
        import pyb
        #sd = pyb.SDCard()
        sd = pyb.Flash(start=0)
        bootsec = bytearray(512)
        sd.readblocks(0, bootsec)
    elif sys.platform in ('esp8266', 'esp32'):
        import esp
        bootsec = bytearray(512)
        esp.flash_read(esp.flash_user_start(), bootsec)
    else:
        with open(sys.argv[1], 'rb') as f:
            bootsec = f.read(512)
    decode_bootsec(bootsec)

main()

KJM
Posts: 158
Joined: Sun Nov 18, 2018 10:53 pm
Location: Sydney AU

Re: what file system

Post by KJM » Tue Jan 18, 2022 9:19 pm

Tnx Rob, Mr. George's script confirms LittleFS. Couple of quick questions if I may:
1) The 2Mb of flash on this Lolin D32 is plenty for my 300 line micropython programs but the 100kb of ram is proving a bottle neck. Is there a way to make a bit more ram available for user code from micropython?
2) I quite liked using nvs_get & nvs_set on pycom devices. Is there a way to access the NVS lib from upython 1.17.0 ?

PM-TPI
Posts: 75
Joined: Fri Jun 28, 2019 3:09 pm

Re: what file system

Post by PM-TPI » Wed Jul 20, 2022 4:54 pm

Damien's script returned... UnicodeError:

Code: Select all

FAT filesystem
0000  jump                             ff:ff:ff
♦Traceback (most recent call last):
  File "<stdin>", line 85, in <module>
  File "<stdin>", line 83, in main
  File "<stdin>", line 67, in decode_bootsec
  File "<stdin>", line 16, in decode_bootsec_fat
UnicodeError:
♦>
MicroPython v1.17-805-g7b1d10d69-dirty on 2022-04-11; 4MB/OTA/SPIram module with ESP32
Type "help()" for more information.
>>>
>>>

Does my MicroPython v1.17-805-g7b1d10d69-dirty... use FAT or LittleFS?
I made no changes from default.
If FAT wich one is used 12,16,32 and what cluster size is it set to.
I need to know the folder capacity I will have.
Will be logging 1 file a day for >4yrs ~3Kb per file
Last edited by PM-TPI on Wed Jul 20, 2022 5:59 pm, edited 1 time in total.

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: what file system

Post by scruss » Wed Jul 20, 2022 5:31 pm

KJM wrote:
Tue Jan 18, 2022 9:19 pm
Tnx Rob, Mr. George's script confirms LittleFS. Couple of quick questions if I may:
1) The 2Mb of flash on this Lolin D32 is plenty for my 300 line micropython programs but the 100kb of ram is proving a bottle neck. Is there a way to make a bit more ram available for user code from micropython?
2) I quite liked using nvs_get & nvs_set on pycom devices. Is there a way to access the NVS lib from upython 1.17.0 ?
  1. With the D32, you can't expand the RAM. The D32 Pro comes with 4 MB of (slightly slower) PSRAM.
  2. yes: please see Non-Volatile Storage

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: what file system

Post by Roberthh » Wed Jul 20, 2022 6:26 pm

I need to know the folder capacity I will have.
You can get the capacity of the file system with:

import os
os.statvfs("")

Which returns a tuple with the the first [0] (and second) being the block size, the third [2] being the number of blocks an the 4th [3] being the number of free blocks. When formatted as FAT, it is FAT12.

PM-TPI
Posts: 75
Joined: Fri Jun 28, 2019 3:09 pm

Re: what file system

Post by PM-TPI » Wed Jul 20, 2022 6:57 pm

I am using a 2GB SD
sooo....

Code: Select all

>>>uos.statvfs('/sd/data')
(32768, 32768, 60469, 60463, 60463, 0, 0, 0, 0, 255)
32768KB * 60469 = 1 981 448 192 ~ 2GB
and all that is usable?
I will have 1 .csv file ~ 5KB, and 1 .txt file ~1KB each day
2GB/6KB = 333K pair of files...

Is this correct?
5yr of files would be 3650

so am I good to go?

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: what file system

Post by Roberthh » Wed Jul 20, 2022 7:09 pm

If you have a 2 GB card you can use it up to that space. The 2GB card will most likely by FAT32, if (since) you formatted it on a PC.

PM-TPI
Posts: 75
Joined: Fri Jun 28, 2019 3:09 pm

Re: what file system

Post by PM-TPI » Wed Jul 20, 2022 7:51 pm

Never formatted cards...
We have units mfg in China
https://www.thermcoproducts.com/SmartLO ... C-DDL.html
looked to see what it says in format... FAT(default) w/allocation unit size = 32KB
and Windows property says FAT

I wish FAT were more specific FAT12 FAT16 FAT32
Even in micropython doc's just says FAT
Can FAT32 cards be Used ???

This came about when we purchased data loggers that used the root for files... we hit 512 files DONE.
I am trying to make sure we have no storage issues.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: what file system

Post by Roberthh » Wed Jul 20, 2022 8:13 pm

In general, FAT32 cards can be used with MicroPython. The internal flash, when sued as file storage, is formatted as FAT12. SDCards are usually formatted as FAT32.

Post Reply