Memory: microSD + external flash

Read and write in the two available memories.

Note

The method to use both memories is exactly the same. If the SBC has a microSD card inserted, it will use it as its main memory.
If no memory card is inserted, the SBC will use its external flash memory (16MB).

Log GPS/GNSS information to memory

This example logs all incoming messages from Gps1 during 10 seconds into a file called data.txt, then it opens this file, reads its content and displays it in the microPython terminal.
The filesystem functions are not particular of the SBC but generic from MicroPython.
You can find more information about the MicroPython filesystem here.

>>> import sbc
>>> import time
>>> fl = open( "/flash/data.txt", "w" )
>>> gps1 = sbc.Gps(1, 115200)
>>> time_start = time.time()
>>> time_dif = 0
>>> while time_dif<10:
>>>     buf = gps1.read()
>>>     if buf:
>>>         fl.write(buf)
>>>         buf = None
>>>     time_dif = time.time()-time_start
>>> fl.close()

Read memory file and print its content in the terminal

This example opens the data.txt file, reads its content and displays it in the microPython terminal.
The filesystem functions are not particular of the SBC but generic from MicroPython.
You can find more information about the MicroPython filesystem here.

>>> fl = open("/flash/data.txt")
>>> print(fl.read())
hello world
>>> fl.close()

Create a littleFS bombproof file system partition

littleFS is a fail-safe filesystem designed for microcontrollers. Its main advantages are:

  • Power-loss resilience - littlefs is designed to handle random power failures. All file operations have strong copy-on-write guarantees and if power is lost the filesystem will fall back to the last known good state.

  • Dynamic wear leveling - littlefs is designed with flash in mind, and provides wear leveling over dynamic blocks. Additionally, littlefs can detect bad blocks and work around them.

  • Bounded RAM/ROM - littlefs is designed to work with a small amount of memory. RAM usage is strictly bounded, which means RAM consumption does not change as the filesystem grows. The filesystem contains no unbounded recursion and dynamic memory is limited to configurable buffers that can be provided statically.

The littleFS functions are not particular of the SBC but generic from MicroPython.
You can find more information about littleFS here and in the littleFS development project site.

This example splits the external 16MB Flash memory in two partitions: 6MB for FAT and 2MB for LFS.

>>> import pyb
>>> import os
>>> fat_start = 0
>>> fat_size = 6*1024*1024 # 6MB
>>> lfs_start = fat_size
>>> lfs_size = 2*1024*1024 # 2MB
>>> p1 = pyb.Flash( start=fat_start, len=fat_size )
>>> p2 = pyb.Flash( start=lfs_start, len=lfs_size )
>>> os.VfsFat.mkfs( p1 )
>>> os.VfsLfs2.mkfs( p2 )
>>> os.mount( p1, "/flash_fat" )
>>> os.mount( p2, "/flash_lfs" )
>>> print( "done" )
done

Mount a littleFS and FAT partition

This example mounts an already created 6MB FAT and 2MB LFS partition.

>>> import pyb
>>> import os
>>> fat_start = 0
>>> fat_size = 6*1024*1024 # 6MB
>>> lfs_start = fat_size
>>> lfs_size = 2*1024*1024 # 2MB
>>> p1 = pyb.Flash( start=fat_start, len=fat_size )
>>> p2 = pyb.Flash( start=lfs_start, len=lfs_size )
>>> os.mount( p1, "/flash_fat" )
>>> os.mount( p2, "/flash_lfs" )
>>> print( "done" )
done

Write/read information from a littleFS and FAT partition

This example mounts an already created 6MB FAT and 2MB LFS partition and manipulates files on them.

>>> import pyb
>>> import os
>>> fat_start = 0
>>> fat_size = 6*1024*1024 # 6MB
>>> lfs_start = fat_size
>>> lfs_size = 2*1024*1024 # 2MB
>>> p1 = pyb.Flash( start=fat_start, len=fat_size )
>>> p2 = pyb.Flash( start=lfs_start, len=lfs_size )
>>> os.mount(p1,"/flash_fat")
>>> os.mount(p2,"/flash_lfs")
>>> fl = open("/flash_fat/data.txt","w")
>>> fl.write("hello world FAT")
>>> fl.close()
>>> fl = open("/flash_fat/data.txt")
>>> print(fl.read())
hello world FAT
>>> fl.close()
>>> fl = open("/flash_lfs/data.txt","w")
>>> fl.write("hello world LFS" )
>>> fl.close()
>>> fl = open("/flash_lfs/data.txt")
>>> print(fl.read())
hello world LFS
>>> fl.close()