fatfs - read/write fatfs format#

Adapted Air780E/Air700E Air780EP Air601 Air101/Air103 Air105 ESP32C3 ESP32S3

Note

This page document is automatically generated by this file. If there is any error, please submit issue or help modify pr, thank you!

Tip

This library has its own demo,click this link to view the demo example of fatfs

Example

-- Usually, only fatfs.mount is used to mount the tf/sd card, and the io library is enough for other operations.

fatfs.mount(mode,mount_point, spiid_or_spidevice, spi_cs, spi_speed, power_pin, power_on_delay, auto_format)#

mount fatfs

Parameters

Incoming Value Type

Explanation

int

fatfs mode, optional fatfs.SPI,fatfs.SDIO,fatfs.RAM,fatfs.USB

string

The mount point of the virtual file system. The default value is /fatfs

int

The spi device pointer, or spi id, or sdio id

int

The GPIO number of the selected pin of the chip, spi mode is valid, if the previous parameter is passed spi device, this parameter does not need to be passed

int

SPI The highest speed is 10M by default. If the first two parameters are spi device, this parameter does not need to be transmitted.

int

TF Card power control pin, TF card before the initial pull down reset and then pull up, if not, or built-in power control mode, this parameter does not need to be transmitted

int

TF Card power reset process time, unit ms, default value is 1

bool

Whether to try formatting when mounting failure, the default is true, that is, automatic formatting. This parameter is added on 2023.8.16

Return Value

return value type

explanation

bool

Returns true on success, otherwise it returns nil or false

string

Reasons for failure

Examples

-- Method 1, using SPI mode
    local spiId = 2
    local result = spi.setup(
        spiId,--Serial port id
        255, -- Don't use default CS pins
        0,--CPHA
        0,--CPOL
        8,--Data Width
        400*1000  -- Use lower frequency when initializing
    )
    local TF_CS = pin.PB3
    gpio.setup(TF_CS, 1)
    --fatfs.debug(1) -- If the mount fails, you can try to open the debugging information to find the reason.
    -- Reminder, if TF/SD module with level conversion, usually does not support baud rate above 10M!!
    fatfs.mount(fatfs.SPI,"SD", spiId, TF_CS, 24000000)
    local data, err = fatfs.getfree("SD")
    if data then
        log.info("fatfs", "getfree", json.encode(data))
    else
        log.info("fatfs", "err", err)
    end
    -- For the next operation, use io.open("/sd/xxx", "w") and other io library API

fatfs.getfree(mount_point)#

Get free space information

Parameters

Incoming Value Type

Explanation

string

The mount point must be the same as the value passed in by fatfs.mount.

Return Value

return value type

explanation

table

Returns table if successful, otherwise nil

int

The underlying return value that caused the failure

Examples

-- table Included content is
-- total_sectors Total number of sectors
-- free_sectors Number of Free Sectors
-- total_kb Total bytes, unit kb
-- free_kb Free bytes, unit kb
-- Note that the current sector size is fixed at 512 bytes

    local data, err = fatfs.getfree("SD")
    if data then
        log.info("fatfs", "getfree", json.encode(data))
    else
        log.info("fatfs", "err", err)
    end

fatfs.debug(value)#

Set Debug Mode

Parameters

Incoming Value Type

Explanation

bool

Whether to enter the debug mode. True means to enter the debug mode and add debug logs.

Return Value

return value type

explanation

nil

No return value

Examples

None