kopia lustrzana https://github.com/micropython/micropython
docs: Use vfs module instead of os.
Signed-off-by: Damien George <damien@micropython.org>pull/13584/head
rodzic
7d28789544
commit
4c56b39051
|
@ -650,15 +650,15 @@ SD card
|
||||||
|
|
||||||
See :ref:`machine.SDCard <machine.SDCard>`. ::
|
See :ref:`machine.SDCard <machine.SDCard>`. ::
|
||||||
|
|
||||||
import machine, os
|
import machine, os, vfs
|
||||||
|
|
||||||
# Slot 2 uses pins sck=18, cs=5, miso=19, mosi=23
|
# Slot 2 uses pins sck=18, cs=5, miso=19, mosi=23
|
||||||
sd = machine.SDCard(slot=2)
|
sd = machine.SDCard(slot=2)
|
||||||
os.mount(sd, '/sd') # mount
|
vfs.mount(sd, '/sd') # mount
|
||||||
|
|
||||||
os.listdir('/sd') # list directory contents
|
os.listdir('/sd') # list directory contents
|
||||||
|
|
||||||
os.umount('/sd') # eject
|
vfs.umount('/sd') # eject
|
||||||
|
|
||||||
RMT
|
RMT
|
||||||
---
|
---
|
||||||
|
|
|
@ -20,11 +20,11 @@ more info regarding the pins which can be remapped to be used with a SD card.
|
||||||
Example usage::
|
Example usage::
|
||||||
|
|
||||||
from machine import SD
|
from machine import SD
|
||||||
import os
|
import vfs
|
||||||
# clk cmd and dat0 pins must be passed along with
|
# clk cmd and dat0 pins must be passed along with
|
||||||
# their respective alternate functions
|
# their respective alternate functions
|
||||||
sd = machine.SD(pins=('GP10', 'GP11', 'GP15'))
|
sd = machine.SD(pins=('GP10', 'GP11', 'GP15'))
|
||||||
os.mount(sd, '/sd')
|
vfs.mount(sd, '/sd')
|
||||||
# do normal file operations
|
# do normal file operations
|
||||||
|
|
||||||
Constructors
|
Constructors
|
||||||
|
|
|
@ -30,7 +30,7 @@ vary from platform to platform.
|
||||||
The class implements the block protocol defined by :class:`vfs.AbstractBlockDev`.
|
The class implements the block protocol defined by :class:`vfs.AbstractBlockDev`.
|
||||||
This allows the mounting of an SD card to be as simple as::
|
This allows the mounting of an SD card to be as simple as::
|
||||||
|
|
||||||
os.mount(machine.SDCard(), "/sd")
|
vfs.mount(machine.SDCard(), "/sd")
|
||||||
|
|
||||||
The constructor takes the following parameters:
|
The constructor takes the following parameters:
|
||||||
|
|
||||||
|
|
|
@ -213,7 +213,7 @@ Miscellaneous functions
|
||||||
.. function:: mount(device, mountpoint, *, readonly=False, mkfs=False)
|
.. function:: mount(device, mountpoint, *, readonly=False, mkfs=False)
|
||||||
|
|
||||||
.. note:: This function is deprecated. Mounting and unmounting devices should
|
.. note:: This function is deprecated. Mounting and unmounting devices should
|
||||||
be performed by :meth:`os.mount` and :meth:`os.umount` instead.
|
be performed by :meth:`vfs.mount` and :meth:`vfs.umount` instead.
|
||||||
|
|
||||||
Mount a block device and make it available as part of the filesystem.
|
Mount a block device and make it available as part of the filesystem.
|
||||||
``device`` must be an object that provides the block protocol. (The
|
``device`` must be an object that provides the block protocol. (The
|
||||||
|
|
|
@ -443,27 +443,27 @@ SD card
|
||||||
|
|
||||||
See :ref:`machine.SDCard <machine.SDCard>`::
|
See :ref:`machine.SDCard <machine.SDCard>`::
|
||||||
|
|
||||||
import machine, os
|
import machine, os, vfs
|
||||||
|
|
||||||
sd = machine.SDCard()
|
sd = machine.SDCard()
|
||||||
fs = os.VfsFat(sd)
|
fs = vfs.VfsFat(sd)
|
||||||
os.mount(fs, "/sd") # mount
|
vfs.mount(fs, "/sd") # mount
|
||||||
os.listdir('/sd') # list directory contents
|
os.listdir('/sd') # list directory contents
|
||||||
os.umount('/sd') # eject
|
vfs.umount('/sd') # eject
|
||||||
|
|
||||||
Note: The i.mx-rt 1011 and 1015 based boards do not support the ``machine.SDCard``
|
Note: The i.mx-rt 1011 and 1015 based boards do not support the ``machine.SDCard``
|
||||||
class. For these, the SPI based driver ``sdcard.py`` from the MicroPython drivers
|
class. For these, the SPI based driver ``sdcard.py`` from the MicroPython drivers
|
||||||
can be used. When using it, you have to overdrive the CS pin of the SPI hardware
|
can be used. When using it, you have to overdrive the CS pin of the SPI hardware
|
||||||
module. Example::
|
module. Example::
|
||||||
|
|
||||||
import os, sdcard, machine
|
import vfs, sdcard, machine
|
||||||
|
|
||||||
cs_pin = "D10"
|
cs_pin = "D10"
|
||||||
spi = machine.SPI(0) # SPI0 with cs at Pin "D10" used for SDCARD
|
spi = machine.SPI(0) # SPI0 with cs at Pin "D10" used for SDCARD
|
||||||
cs = machine.Pin(cs_pin, machine.Pin.OUT, value=1)
|
cs = machine.Pin(cs_pin, machine.Pin.OUT, value=1)
|
||||||
sd = sdcard.SDCard(spi, cs)
|
sd = sdcard.SDCard(spi, cs)
|
||||||
vfs = os.VfsFat(sd)
|
fs = vfs.VfsFat(sd)
|
||||||
os.mount(vfs, "/sdcard")
|
vfs.mount(fs, "/sdcard")
|
||||||
|
|
||||||
OneWire driver
|
OneWire driver
|
||||||
--------------
|
--------------
|
||||||
|
|
|
@ -21,7 +21,7 @@ If needed, you can prevent the use of the SD card by creating an empty file
|
||||||
called ``/flash/SKIPSD``. If this file exists when the pyboard boots
|
called ``/flash/SKIPSD``. If this file exists when the pyboard boots
|
||||||
up then the SD card will be skipped and the pyboard will always boot from the
|
up then the SD card will be skipped and the pyboard will always boot from the
|
||||||
internal filesystem (in this case the SD card won't be mounted but you can still
|
internal filesystem (in this case the SD card won't be mounted but you can still
|
||||||
mount and use it later in your program using ``os.mount``).
|
mount and use it later in your program using ``vfs.mount``).
|
||||||
|
|
||||||
(Note that on older versions of the board, ``/flash`` is called ``0:/`` and ``/sd``
|
(Note that on older versions of the board, ``/flash`` is called ``0:/`` and ``/sd``
|
||||||
is called ``1:/``).
|
is called ``1:/``).
|
||||||
|
|
|
@ -108,11 +108,11 @@ RAM using a ``bytearray``::
|
||||||
|
|
||||||
It can be used as follows::
|
It can be used as follows::
|
||||||
|
|
||||||
import os
|
import vfs
|
||||||
|
|
||||||
bdev = RAMBlockDev(512, 50)
|
bdev = RAMBlockDev(512, 50)
|
||||||
os.VfsFat.mkfs(bdev)
|
vfs.VfsFat.mkfs(bdev)
|
||||||
os.mount(bdev, '/ramdisk')
|
vfs.mount(bdev, '/ramdisk')
|
||||||
|
|
||||||
An example of a block device that supports both the simple and extended
|
An example of a block device that supports both the simple and extended
|
||||||
interface (i.e. both signatures and behaviours of the
|
interface (i.e. both signatures and behaviours of the
|
||||||
|
@ -150,11 +150,11 @@ interface (i.e. both signatures and behaviours of the
|
||||||
As it supports the extended interface, it can be used with :class:`littlefs
|
As it supports the extended interface, it can be used with :class:`littlefs
|
||||||
<vfs.VfsLfs2>`::
|
<vfs.VfsLfs2>`::
|
||||||
|
|
||||||
import os
|
import vfs
|
||||||
|
|
||||||
bdev = RAMBlockDev(512, 50)
|
bdev = RAMBlockDev(512, 50)
|
||||||
os.VfsLfs2.mkfs(bdev)
|
vfs.VfsLfs2.mkfs(bdev)
|
||||||
os.mount(bdev, '/ramdisk')
|
vfs.mount(bdev, '/ramdisk')
|
||||||
|
|
||||||
Once mounted, the filesystem (regardless of its type) can be used as it
|
Once mounted, the filesystem (regardless of its type) can be used as it
|
||||||
normally would be used from Python code, for example::
|
normally would be used from Python code, for example::
|
||||||
|
@ -197,16 +197,16 @@ recommended to use littlefs instead.
|
||||||
To format the entire flash using FAT::
|
To format the entire flash using FAT::
|
||||||
|
|
||||||
# ESP8266 and ESP32
|
# ESP8266 and ESP32
|
||||||
import os
|
import vfs
|
||||||
os.umount('/')
|
vfs.umount('/')
|
||||||
os.VfsFat.mkfs(bdev)
|
vfs.VfsFat.mkfs(bdev)
|
||||||
os.mount(bdev, '/')
|
vfs.mount(bdev, '/')
|
||||||
|
|
||||||
# STM32
|
# STM32
|
||||||
import os, pyb
|
import os, vfs, pyb
|
||||||
os.umount('/flash')
|
vfs.umount('/flash')
|
||||||
os.VfsFat.mkfs(pyb.Flash(start=0))
|
vfs.VfsFat.mkfs(pyb.Flash(start=0))
|
||||||
os.mount(pyb.Flash(start=0), '/flash')
|
vfs.mount(pyb.Flash(start=0), '/flash')
|
||||||
os.chdir('/flash')
|
os.chdir('/flash')
|
||||||
|
|
||||||
Littlefs
|
Littlefs
|
||||||
|
@ -222,16 +222,16 @@ resistant to filesystem corruption.
|
||||||
To format the entire flash using littlefs v2::
|
To format the entire flash using littlefs v2::
|
||||||
|
|
||||||
# ESP8266 and ESP32
|
# ESP8266 and ESP32
|
||||||
import os
|
import vfs
|
||||||
os.umount('/')
|
vfs.umount('/')
|
||||||
os.VfsLfs2.mkfs(bdev)
|
vfs.VfsLfs2.mkfs(bdev)
|
||||||
os.mount(bdev, '/')
|
vfs.mount(bdev, '/')
|
||||||
|
|
||||||
# STM32
|
# STM32
|
||||||
import os, pyb
|
import os, vfs, pyb
|
||||||
os.umount('/flash')
|
vfs.umount('/flash')
|
||||||
os.VfsLfs2.mkfs(pyb.Flash(start=0))
|
vfs.VfsLfs2.mkfs(pyb.Flash(start=0))
|
||||||
os.mount(pyb.Flash(start=0), '/flash')
|
vfs.mount(pyb.Flash(start=0), '/flash')
|
||||||
os.chdir('/flash')
|
os.chdir('/flash')
|
||||||
|
|
||||||
A littlefs filesystem can be still be accessed on a PC over USB MSC using the
|
A littlefs filesystem can be still be accessed on a PC over USB MSC using the
|
||||||
|
@ -264,14 +264,14 @@ block devices spanning a subset of the flash device.
|
||||||
For example, to configure the first 256kiB as FAT (and available over USB MSC),
|
For example, to configure the first 256kiB as FAT (and available over USB MSC),
|
||||||
and the remainder as littlefs::
|
and the remainder as littlefs::
|
||||||
|
|
||||||
import os, pyb
|
import os, vfs, pyb
|
||||||
os.umount('/flash')
|
vfs.umount('/flash')
|
||||||
p1 = pyb.Flash(start=0, len=256*1024)
|
p1 = pyb.Flash(start=0, len=256*1024)
|
||||||
p2 = pyb.Flash(start=256*1024)
|
p2 = pyb.Flash(start=256*1024)
|
||||||
os.VfsFat.mkfs(p1)
|
vfs.VfsFat.mkfs(p1)
|
||||||
os.VfsLfs2.mkfs(p2)
|
vfs.VfsLfs2.mkfs(p2)
|
||||||
os.mount(p1, '/flash')
|
vfs.mount(p1, '/flash')
|
||||||
os.mount(p2, '/data')
|
vfs.mount(p2, '/data')
|
||||||
os.chdir('/flash')
|
os.chdir('/flash')
|
||||||
|
|
||||||
This might be useful to make your Python files, configuration and other
|
This might be useful to make your Python files, configuration and other
|
||||||
|
@ -282,9 +282,9 @@ failure, etc.
|
||||||
The partition at offset ``0`` will be mounted automatically (and the filesystem
|
The partition at offset ``0`` will be mounted automatically (and the filesystem
|
||||||
type automatically detected), but you can add::
|
type automatically detected), but you can add::
|
||||||
|
|
||||||
import os, pyb
|
import vfs, pyb
|
||||||
p2 = pyb.Flash(start=256*1024)
|
p2 = pyb.Flash(start=256*1024)
|
||||||
os.mount(p2, '/data')
|
vfs.mount(p2, '/data')
|
||||||
|
|
||||||
to ``boot.py`` to mount the data partition.
|
to ``boot.py`` to mount the data partition.
|
||||||
|
|
||||||
|
@ -297,7 +297,7 @@ define an arbitrary partition layout.
|
||||||
At boot, the partition named "vfs" will be mounted at ``/`` by default, but any
|
At boot, the partition named "vfs" will be mounted at ``/`` by default, but any
|
||||||
additional partitions can be mounted in your ``boot.py`` using::
|
additional partitions can be mounted in your ``boot.py`` using::
|
||||||
|
|
||||||
import esp32, os
|
import esp32, vfs
|
||||||
p = esp32.Partition.find(esp32.Partition.TYPE_DATA, label='foo')
|
p = esp32.Partition.find(esp32.Partition.TYPE_DATA, label='foo')
|
||||||
os.mount(p, '/foo')
|
vfs.mount(p, '/foo')
|
||||||
|
|
||||||
|
|
|
@ -387,15 +387,15 @@ SDCard
|
||||||
The frozen sdcard driver (drivers/sdcard/sdcard.py) is available by connecting microSD card device to hardware SPI0 pins.::
|
The frozen sdcard driver (drivers/sdcard/sdcard.py) is available by connecting microSD card device to hardware SPI0 pins.::
|
||||||
|
|
||||||
from machine import Pin, SPI
|
from machine import Pin, SPI
|
||||||
import os, sdcard
|
import os, vfs, sdcard
|
||||||
|
|
||||||
spi = SPI(0, baudrate=500000)
|
spi = SPI(0, baudrate=500000)
|
||||||
cs = Pin.cpu.P103
|
cs = Pin.cpu.P103
|
||||||
sd = sdcard.SDCard(spi, cs)
|
sd = sdcard.SDCard(spi, cs)
|
||||||
os.mount(sd, '/sd')
|
vfs.mount(sd, '/sd')
|
||||||
os.listdir('/')
|
os.listdir('/')
|
||||||
os.chdir('/sd')
|
os.chdir('/sd')
|
||||||
os.umount('/sd')
|
vfs.umount('/sd')
|
||||||
|
|
||||||
OneWire driver
|
OneWire driver
|
||||||
--------------
|
--------------
|
||||||
|
|
|
@ -373,7 +373,7 @@ functions are defined in ``os`` module:
|
||||||
Mounts a block device (like an ``SD`` object) in the specified mount
|
Mounts a block device (like an ``SD`` object) in the specified mount
|
||||||
point. Example::
|
point. Example::
|
||||||
|
|
||||||
os.mount(sd, '/sd')
|
vfs.mount(sd, '/sd')
|
||||||
|
|
||||||
.. function:: unmount(path)
|
.. function:: unmount(path)
|
||||||
|
|
||||||
|
|
|
@ -171,13 +171,13 @@ SD card
|
||||||
See :ref:`machine.SD <machine.SD>`. ::
|
See :ref:`machine.SD <machine.SD>`. ::
|
||||||
|
|
||||||
from machine import SD
|
from machine import SD
|
||||||
import os
|
import vfs
|
||||||
|
|
||||||
# clock pin, cmd pin, data0 pin
|
# clock pin, cmd pin, data0 pin
|
||||||
sd = SD(pins=('GP10', 'GP11', 'GP15'))
|
sd = SD(pins=('GP10', 'GP11', 'GP15'))
|
||||||
# or use default ones for the expansion board
|
# or use default ones for the expansion board
|
||||||
sd = SD()
|
sd = SD()
|
||||||
os.mount(sd, '/sd')
|
vfs.mount(sd, '/sd')
|
||||||
|
|
||||||
WLAN (WiFi)
|
WLAN (WiFi)
|
||||||
-----------
|
-----------
|
||||||
|
|
|
@ -109,12 +109,12 @@ Disk Access
|
||||||
|
|
||||||
Use the :ref:`zephyr.DiskAccess <zephyr.DiskAccess>` class to support filesystem::
|
Use the :ref:`zephyr.DiskAccess <zephyr.DiskAccess>` class to support filesystem::
|
||||||
|
|
||||||
import os
|
import vfs
|
||||||
from zephyr import DiskAccess
|
from zephyr import DiskAccess
|
||||||
|
|
||||||
block_dev = DiskAccess('SDHC') # create a block device object for an SD card
|
block_dev = DiskAccess('SDHC') # create a block device object for an SD card
|
||||||
os.VfsFat.mkfs(block_dev) # create FAT filesystem object using the disk storage block
|
vfs.VfsFat.mkfs(block_dev) # create FAT filesystem object using the disk storage block
|
||||||
os.mount(block_dev, '/sd') # mount the filesystem at the SD card subdirectory
|
vfs.mount(block_dev, '/sd') # mount the filesystem at the SD card subdirectory
|
||||||
|
|
||||||
# with the filesystem mounted, files can be manipulated as normal
|
# with the filesystem mounted, files can be manipulated as normal
|
||||||
with open('/sd/hello.txt','w') as f: # open a new file in the directory
|
with open('/sd/hello.txt','w') as f: # open a new file in the directory
|
||||||
|
@ -126,12 +126,12 @@ Flash Area
|
||||||
|
|
||||||
Use the :ref:`zephyr.FlashArea <zephyr.FlashArea>` class to support filesystem::
|
Use the :ref:`zephyr.FlashArea <zephyr.FlashArea>` class to support filesystem::
|
||||||
|
|
||||||
import os
|
import vfs
|
||||||
from zephyr import FlashArea
|
from zephyr import FlashArea
|
||||||
|
|
||||||
block_dev = FlashArea(4, 4096) # creates a block device object in the frdm-k64f flash scratch partition
|
block_dev = FlashArea(4, 4096) # creates a block device object in the frdm-k64f flash scratch partition
|
||||||
os.VfsLfs2.mkfs(block_dev) # create filesystem in lfs2 format using the flash block device
|
vfs.VfsLfs2.mkfs(block_dev) # create filesystem in lfs2 format using the flash block device
|
||||||
os.mount(block_dev, '/flash') # mount the filesystem at the flash subdirectory
|
vfs.mount(block_dev, '/flash') # mount the filesystem at the flash subdirectory
|
||||||
|
|
||||||
# with the filesystem mounted, files can be manipulated as normal
|
# with the filesystem mounted, files can be manipulated as normal
|
||||||
with open('/flash/hello.txt','w') as f: # open a new file in the directory
|
with open('/flash/hello.txt','w') as f: # open a new file in the directory
|
||||||
|
|
|
@ -21,11 +21,11 @@ file system to access SD cards via disk access (see below).
|
||||||
|
|
||||||
Example usage of FatFS with an SD card on the mimxrt1050_evk board::
|
Example usage of FatFS with an SD card on the mimxrt1050_evk board::
|
||||||
|
|
||||||
import os
|
import vfs
|
||||||
from zephyr import DiskAccess
|
from zephyr import DiskAccess
|
||||||
bdev = zephyr.DiskAccess('SDHC') # create block device object using DiskAccess
|
bdev = zephyr.DiskAccess('SDHC') # create block device object using DiskAccess
|
||||||
os.VfsFat.mkfs(bdev) # create FAT filesystem object using the disk storage block
|
vfs.VfsFat.mkfs(bdev) # create FAT filesystem object using the disk storage block
|
||||||
os.mount(bdev, '/sd') # mount the filesystem at the SD card subdirectory
|
vfs.mount(bdev, '/sd') # mount the filesystem at the SD card subdirectory
|
||||||
with open('/sd/hello.txt','w') as f: # open a new file in the directory
|
with open('/sd/hello.txt','w') as f: # open a new file in the directory
|
||||||
f.write('Hello world') # write to the file
|
f.write('Hello world') # write to the file
|
||||||
print(open('/sd/hello.txt').read()) # print contents of the file
|
print(open('/sd/hello.txt').read()) # print contents of the file
|
||||||
|
@ -43,11 +43,11 @@ implements the `vfs.AbstractBlockDev` protocol.
|
||||||
|
|
||||||
Example usage with the internal flash on the reel_board or the rv32m1_vega_ri5cy board::
|
Example usage with the internal flash on the reel_board or the rv32m1_vega_ri5cy board::
|
||||||
|
|
||||||
import os
|
import vfs
|
||||||
from zephyr import FlashArea
|
from zephyr import FlashArea
|
||||||
bdev = FlashArea(FlashArea.STORAGE, 4096) # create block device object using FlashArea
|
bdev = FlashArea(FlashArea.STORAGE, 4096) # create block device object using FlashArea
|
||||||
os.VfsLfs2.mkfs(bdev) # create Little filesystem object using the flash area block
|
vfs.VfsLfs2.mkfs(bdev) # create Little filesystem object using the flash area block
|
||||||
os.mount(bdev, '/flash') # mount the filesystem at the flash storage subdirectory
|
vfs.mount(bdev, '/flash') # mount the filesystem at the flash storage subdirectory
|
||||||
with open('/flash/hello.txt','w') as f: # open a new file in the directory
|
with open('/flash/hello.txt','w') as f: # open a new file in the directory
|
||||||
f.write('Hello world') # write to the file
|
f.write('Hello world') # write to the file
|
||||||
print(open('/flash/hello.txt').read()) # print contents of the file
|
print(open('/flash/hello.txt').read()) # print contents of the file
|
||||||
|
|
Ładowanie…
Reference in New Issue