Assistance Needed with Zephyr Project for File System Implementation

Dear Nordic DevZone Support Team,

I hope this message finds you well. I am reaching out for assistance with a project I am working on that involves implementing a file system using Zephyr.

My task involves creating a file system on an nRF52840 board using Zephyr. The goal is to create a text file (e.g., .txt) on the board, write data to this file, and then access this file from a PC via USB connection.

To achieve this, I have configured Zephyr to use LittleFS as the file system. I have carefully followed the documentation and configured the `prj.conf` file to enable LittleFS and specified the mount point as `/lfs`. Additionally, I have set up the LittleFS mount point to `/lfs` in my main application code.

However, when running the application, I encounter an error indicating that the LittleFS filesystem fails to mount properly during runtime. Specifically, I receive an error indicating that the file `/lfs/data.txt` cannot be opened (`FAIL: open /lfs/data.txt: -16`).

I have attached a zip file containing my project folder for your reference. Could you please review my configuration and code to help me identify the cause of this issue? Additionally, if there are any specific steps or configurations I may have overlooked, I would greatly appreciate your guidance in resolving this matter.

Thank you very much for your attention to this matter, and I look forward to your assistance.

Main.c : 

#include <stdio.h>
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/fs/fs.h>
#include <zephyr/fs/littlefs.h>
#include <zephyr/usb/usb_device.h>
#include <zephyr/usb/usbd.h>
#include <zephyr/usb/class/usbd_msc.h>

/* LittleFS Code */
#define MAX_PATH_LEN 255
#define FILENAME "/lfs/data.txt"


/* LittleFS configuration */
FS_LITTLEFS_DECLARE_DEFAULT_CONFIG(storage);
static struct fs_mount_t lfs_storage_mnt = {
    .type = FS_LITTLEFS,
    .fs_data = &storage,
    .storage_dev = (void *)FLASH_AREA_ID(littlefs_storage),
    .mnt_point = "/lfs", // Point de montage du système de fichiers LittleFS
};



/* USB Mass Storage Code */
#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
USBD_CONFIGURATION_DEFINE(config_1,
                          USB_SCD_SELF_POWERED,
                          200);

USBD_DESC_LANG_DEFINE(sample_lang);
USBD_DESC_MANUFACTURER_DEFINE(sample_mfr, "ZEPHYR");
USBD_DESC_PRODUCT_DEFINE(sample_product, "Zephyr USBD MSC");
USBD_DESC_SERIAL_NUMBER_DEFINE(sample_sn, "0123456789ABCDEF");

USBD_DEVICE_DEFINE(sample_usbd,
                   DEVICE_DT_GET(DT_NODELABEL(zephyr_udc0)),
                   0x2fe3, 0x0008);

USBD_DEFINE_MSC_LUN(RAM, "Zephyr", "RAMDisk", "0.00");

static int enable_usb_device_next(void)
{
    // Enable USB device
}

#endif /* CONFIG_USB_DEVICE_STACK_NEXT */

/* Main Function */
void main(void)
{
    struct fs_mount_t *mp = &lfs_storage_mnt;
    unsigned int id = (uintptr_t)mp->storage_dev;
    char fname[MAX_PATH_LEN];
    const struct flash_area *pfa;
    int rc;

    /* Mount LittleFS */
    snprintf(fname, sizeof(fname), "%s/data.txt", mp->mnt_point); // Nom du fichier à créer

    rc = flash_area_open(id, &pfa);
    if (rc < 0)
    {
        printf("FAIL: unable to find flash area %u: %d\n",
               id, rc);
        return;
    }

    if (IS_ENABLED(CONFIG_APP_WIPE_STORAGE))
    {
        printf("Erasing flash area ... ");
        rc = flash_area_erase(pfa, 0, pfa->fa_size);
        printf("%d\n", rc);
    }

    flash_area_close(pfa);

    rc = fs_mount(mp);
    if (rc < 0)
    {
        printf("FAIL: mount id %u at %s: %d\n",
               (unsigned int)mp->storage_dev, mp->mnt_point,
               rc);
        return;
    }

    struct fs_file_t file;

    rc = fs_open(&file, fname, FS_O_CREATE | FS_O_RDWR);
    if (rc < 0)
    {
        printf("FAIL: open %s: %d\n", fname, rc);
        fs_unmount(mp);
        return;
    }

    const char *data = "Hello, world!"; // Données à écrire dans le fichier

    rc = fs_write(&file, data, strlen(data));
    if (rc < 0)
    {
        printf("FAIL: write %s: %d\n", fname, rc);
    }

    fs_close(&file);

    fs_unmount(mp);

#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
    enable_usb_device_next();
#endif /* CONFIG_USB_DEVICE_STACK_NEXT */
}

prj.conf : 


# Optionally force the file system to be recreated
#CONFIG_APP_WIPE_STORAGE=y

# fs_dirent structures are big.
CONFIG_MAIN_STACK_SIZE=4096

# Let __ASSERT do its job
CONFIG_DEBUG=y

CONFIG_LOG=y
CONFIG_LOG_MODE_MINIMAL=y

CONFIG_FLASH=y
CONFIG_FLASH_MAP=y
CONFIG_FLASH_PAGE_LAYOUT=y

CONFIG_FILE_SYSTEM=y
CONFIG_FILE_SYSTEM_LITTLEFS=y



CONFIG_DISK_ACCESS=y
CONFIG_LOG=y
CONFIG_FILE_SYSTEM=y
CONFIG_FAT_FILESYSTEM_ELM=y
CONFIG_PRINTK=y
#CONFIG_MAIN_STACK_SIZE=2048

CONFIG_STDOUT_CONSOLE=y

#USB related configs
CONFIG_USB_DEVICE_STACK=y
CONFIG_USB_DEVICE_PRODUCT="Zephyr MSC sample"
CONFIG_USB_DEVICE_PID=0x0008
CONFIG_LOG=y
CONFIG_USB_DRIVER_LOG_LEVEL_ERR=y
CONFIG_USB_MASS_STORAGE=y
CONFIG_USB_DEVICE_LOG_LEVEL_ERR=y
CONFIG_USB_MASS_STORAGE_LOG_LEVEL_ERR=y
CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT=n

#CONFIG_MAIN_STACK_SIZE=1536

# fs_dirent structures are big.
#CONFIG_MAIN_STACK_SIZE=2048

# SPI Flash
CONFIG_SPI=y
#CONFIG_SPI_NOR=y
#CONFIG_SPI_NOR_FLASH_LAYOUT_PAGE_SIZE=4096

# Enable flash operations.
CONFIG_FLASH=y
CONFIG_FLASH_MAP=y
CONFIG_FLASH_PAGE_LAYOUT=y

# Enable the LittleFS file system.
CONFIG_FILE_SYSTEM=y
CONFIG_FILE_SYSTEM_LITTLEFS=y

# Enable Zephyr application to be booted by MCUboot
CONFIG_BOOTLOADER_MCUBOOT=y

# Erase
# CONFIG_APP_WIPE_STORAGE=y

# Log immediate
CONFIG_LOG=y
CONFIG_LOG_MODE_IMMEDIATE=y

# Settings
CONFIG_SETTINGS=y
CONFIG_SETTINGS_FS=y
CONFIG_SETTINGS_FS_DIR="/lfs/settings"
CONFIG_SETTINGS_FS_FILE="/lfs/settings/run"

# Partition manager settings
CONFIG_PM_EXTERNAL_FLASH_MCUBOOT_SECONDARY=n


untitled_1.zip

Best regards.

Parents Reply Children
No Data
Related