The following example demonstrates how to implement VFS using LittleFS.

1. Create a file system BIN file using vfs.py

Package vgs_test.txt from the test directory into a BIN file.

Content of vgs_test.txt:

this is a vfs test. 2025.11.18

 

Command: vfs.py -t LITTLEFS -s 4096 -c 32 -dir test -out littlefs.bin

Actual command used: 

~/mcu/Ameba-rtos/ameba-rtos/tools/image_scripts/vfs.py -t LITTLEFS -s 4096 -c 32 -dir test -out littlefs.bin

Output:

littlefs.bin has been successfully generated. args: 

 ├─ type: LITTLEFS 

 ├─ block_size: 4096 

 ├─ block_count: 32 

 ├─ image_size: 131072 

 ├─ source_directory: test 

 └─ output_image: littlefs.bin

2. menuconfig Configuration

Enable the option: Enable VGS LITTLEFS

 

3. Application Code

Reference example:

/ameba-rtos/component/example/storage/vfs

1)fats.conf

# This file is generated automatically by:

# python menuconfig.py -s ../component/example/storage/vfs/fatfs.conf

CONFIG_VFS_LITTLEFS_INCLUDED=y   // Use LITTLEFS

CONFIG_VFS_FATFS_INCLUDED=n

2)example_vfs.c

#include "ameba_soc.h"

#include "os_wrapper.h"

#include "vfs.h"

#include "example_vfs.h"

 

void example_vfs_thread(void *param)

{

rtos_time_delay_ms(3000);

char filename[] = "vfs_test.txt";

char path[128] = {0};

char buffer[32] = {0};

char *prefix;

FILE *finfo;

int res = 0;

 

(void)param;

 

RTK_LOGS(NOTAG, RTK_LOG_INFO, "\r\n====================Example: VFS====================\r\n");

 

prefix = find_vfs_tag(VFS_REGION_1);

DiagSnPrintf(path, sizeof(path), "%s:%s", prefix, filename);

 

finfo = fopen(path, "r");

if (finfo == NULL) {

RTK_LOGS(NOTAG, RTK_LOG_ERROR, "[%s] fopen() failed\r\n", __FUNCTION__);

goto exit;

}

res = fread(buffer, 128, 1, finfo);

if (res < 0) {

RTK_LOGS(NOTAG, RTK_LOG_ERROR, "[%s] fread() failed, err is %d\r\n", __FUNCTION__, res);

} else {

RTK_LOGS(NOTAG, RTK_LOG_ERROR, "[%s] fread() content, read buffer is %s\r\n", __FUNCTION__, buffer);

}

 

fclose(finfo);

exit:

rtos_task_delete(NULL);

}

 

void example_vfs(void)

{

if (rtos_task_create(NULL, ((const char *)"example_vfs_thread"), example_vfs_thread, NULL, 4096 * 4, 1) != RTK_SUCCESS) {

RTK_LOGS(NOTAG, RTK_LOG_ERROR, "\n\r%s rtos_task_create(example_vfs_thread) failed", __FUNCTION__);

}

}

 

prefix = find_vfs_tag(VFS_REGION_1);   // Get the VFS1 tag

DiagSnPrintf(path, sizeof(path), "%s:%s", prefix, filename); // Build file path

finfo = fopen(path, "r");             // Open file

res = fread(buffer, 128, 1, finfo);    // Read file

4. Flash Programming

For the VFS Flash region, the StartAddr and EndAddr configuration should follow: Actual end address = EndAddr + 1

Region

Address

Size (KB)

Description

VFS

0x083E_0000

128

Used for file system

 

5. Execution Result

UART output

The output shows that the system successfully reads the content of vfs_test.txt from the file system flashed from littlefs.bin, and prints it via the serial port.