samples: subsys: fs: create common fs sample
Sample for FAT fs is reworked to run also with Ext2 file system Signed-off-by: Franciszek Zdobylak <fzdobylak@antmicro.com>
This commit is contained in:
parent
1f62d99e7e
commit
16183f665e
21 changed files with 119 additions and 33 deletions
|
|
@ -1,20 +1,21 @@
|
||||||
#
|
#
|
||||||
# Copyright (c) 2023 Nordic Semiconductor ASA
|
# Copyright (c) 2023 Nordic Semiconductor ASA
|
||||||
|
# Copyright (c) 2023 Antmicro <www.antmicro.com>
|
||||||
#
|
#
|
||||||
# SPDX-License-Identifier: Apache-2.0
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
#
|
#
|
||||||
|
|
||||||
mainmenu "FAT Filesystem Sample Application"
|
mainmenu "Filesystems Sample Application"
|
||||||
|
|
||||||
config SAMPLE_FATFS_CREATE_SOME_ENTRIES
|
config FS_SAMPLE_CREATE_SOME_ENTRIES
|
||||||
bool "When no files are found on mounted partition create some"
|
bool "When no files are found on mounted partition create some"
|
||||||
default y
|
default y
|
||||||
help
|
help
|
||||||
In case when no files could be listed, because there are none,
|
In case when no files could be listed, because there are none,
|
||||||
"some.dir" directory and "other.txt" file will be created
|
"some.dir" directory and "other.txt" file will be created
|
||||||
and list will run again to show them. This is useful when
|
and list will run again to show them. This is useful when
|
||||||
showing how FAT works on non-SD devices like internal flash
|
showing how file system works on non-SD devices like internal
|
||||||
or (Q)SPI connected memories, where it is not possible to
|
flash or (Q)SPI connected memories, where it is not possible to
|
||||||
easily add files with use of other device.
|
easily add files with use of other device.
|
||||||
|
|
||||||
source "Kconfig.zephyr"
|
source "Kconfig.zephyr"
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
.. _fat_fs:
|
.. _fs_sample:
|
||||||
|
|
||||||
FAT Filesystem Sample Application
|
Filesystems Sample Application
|
||||||
###################################
|
###################################
|
||||||
|
|
||||||
Overview
|
Overview
|
||||||
********
|
********
|
||||||
|
|
||||||
This sample app demonstrates use of the filesystem API and uses the FAT file
|
This sample app demonstrates use of the file system API and uses the FAT or Ext2 file
|
||||||
system driver with SDHC card, SoC flash or external flash chip.
|
system driver with SDHC card, SoC flash or external flash chip.
|
||||||
|
|
||||||
To access device the sample uses :ref:`disk_access_api`.
|
To access device the sample uses :ref:`disk_access_api`.
|
||||||
|
|
@ -14,13 +14,11 @@ To access device the sample uses :ref:`disk_access_api`.
|
||||||
Requirements for SD card support
|
Requirements for SD card support
|
||||||
********************************
|
********************************
|
||||||
|
|
||||||
This project requires SD card support and microSD card formatted with FAT filesystem.
|
This project requires SD card support and microSD card formatted with proper file system
|
||||||
See the :ref:`disk_access_api` documentation for Zephyr implementation details.
|
(FAT or Ext2) See the :ref:`disk_access_api` documentation for Zephyr implementation details.
|
||||||
Boards that by default use SD card for storage:
|
Boards that by default use SD card for storage: ``arduino_mkrzero``, ``esp_wrover_kit``,
|
||||||
``arduino_mkrzero``, ``esp_wrover_kit``, ``mimxrt1050_evk``, ``nrf52840_blip``
|
``mimxrt1050_evk``, ``nrf52840_blip`` and ``olimexino_stm32``. The sample should be able
|
||||||
and ``olimexino_stm32``.
|
to run with any other board that has "zephyr,sdmmc-disk" DT node enabled.
|
||||||
The sample should be able to run with any other board that has "zephyr,sdmmc-disk"
|
|
||||||
DT node enabled.
|
|
||||||
|
|
||||||
Requirements for setting up FAT FS on SoC flash
|
Requirements for setting up FAT FS on SoC flash
|
||||||
***********************************************
|
***********************************************
|
||||||
|
|
@ -38,14 +36,14 @@ This type of configuration requires external flash device to be available
|
||||||
on DK board. Currently following boards support the configuration:
|
on DK board. Currently following boards support the configuration:
|
||||||
``nrf52840dk_nrf52840`` by ``nrf52840dk_nrf52840_qspi`` configuration.
|
``nrf52840dk_nrf52840`` by ``nrf52840dk_nrf52840_qspi`` configuration.
|
||||||
|
|
||||||
Building and Running
|
Building and Running FAT samples
|
||||||
********************
|
********************************
|
||||||
|
|
||||||
Boards with default configurations, for example ``arduino_mkrzero`` or
|
Boards with default configurations, for example ``arduino_mkrzero`` or
|
||||||
``nrf52840dk_nrf52840`` using internal flash can be build using command:
|
``nrf52840dk_nrf52840`` using internal flash can be build using command:
|
||||||
|
|
||||||
.. zephyr-app-commands::
|
.. zephyr-app-commands::
|
||||||
:zephyr-app: samples/subsys/fs/fat_fs
|
:zephyr-app: samples/subsys/fs/fs_sample
|
||||||
:board: nrf52840_blip
|
:board: nrf52840_blip
|
||||||
:goals: build
|
:goals: build
|
||||||
:compact:
|
:compact:
|
||||||
|
|
@ -57,7 +55,7 @@ for example ``nrf52840dk_nrf52840`` with MX25 device over QSPI, configuration
|
||||||
and DTS overlays need to be also selected. The command would look like this:
|
and DTS overlays need to be also selected. The command would look like this:
|
||||||
|
|
||||||
.. zephyr-app-commands::
|
.. zephyr-app-commands::
|
||||||
:zephyr-app: samples/subsys/fs/fat_fs
|
:zephyr-app: samples/subsys/fs/fs_sample
|
||||||
:board: nrf52840dk_nrf52840
|
:board: nrf52840dk_nrf52840
|
||||||
:gen-args: -DEXTRA_CONF_FILE=nrf52840dk_nrf52840_qspi.conf -DDTC_OVERLAY_FILE=nrf52840dk_nrf52840_qspi.overlay
|
:gen-args: -DEXTRA_CONF_FILE=nrf52840dk_nrf52840_qspi.conf -DDTC_OVERLAY_FILE=nrf52840dk_nrf52840_qspi.overlay
|
||||||
:goals: build
|
:goals: build
|
||||||
|
|
@ -70,3 +68,20 @@ sample lists them out on the debug serial output.
|
||||||
.. warning::
|
.. warning::
|
||||||
In case when mount fails the device may get re-formatted to FAT FS.
|
In case when mount fails the device may get re-formatted to FAT FS.
|
||||||
To disable this behaviour disable :kconfig:option:`CONFIG_FS_FATFS_MOUNT_MKFS` .
|
To disable this behaviour disable :kconfig:option:`CONFIG_FS_FATFS_MOUNT_MKFS` .
|
||||||
|
|
||||||
|
Building and Running EXT2 samples
|
||||||
|
*********************************
|
||||||
|
|
||||||
|
Ext2 sample can be build for ``hifive_unmatched`` or ``bl5340_dvk_cpuapp``. Because
|
||||||
|
FAT is default file system for this sample, additional flags must be passed to build
|
||||||
|
the sample.
|
||||||
|
|
||||||
|
.. zephyr-app-commands::
|
||||||
|
:zephyr-app: samples/subsys/fs/fs_sample
|
||||||
|
:board: hifive_unmatched
|
||||||
|
:gen-args: -DCONF_FILE=prj_ext.conf
|
||||||
|
:goals: build
|
||||||
|
:compact:
|
||||||
|
|
||||||
|
A microSD card must be present in a microSD card slot of the board, for the sample to execute.
|
||||||
|
After starting the sample a contents of a root directory should be printed on the console.
|
||||||
20
samples/subsys/fs/fs_sample/boards/hifive_unmatched.overlay
Normal file
20
samples/subsys/fs/fs_sample/boards/hifive_unmatched.overlay
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2023 Antmicro
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
&spi2 {
|
||||||
|
status = "okay";
|
||||||
|
|
||||||
|
sdhc0: sdhc@0 {
|
||||||
|
compatible = "zephyr,sdhc-spi-slot";
|
||||||
|
reg = <0>;
|
||||||
|
status = "okay";
|
||||||
|
mmc {
|
||||||
|
compatible = "zephyr,sdmmc-disk";
|
||||||
|
status = "okay";
|
||||||
|
};
|
||||||
|
spi-max-frequency = <20000000>;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
@ -9,4 +9,4 @@ CONFIG_DISK_DRIVER_FLASH=y
|
||||||
# There may be no files on internal SoC flash, so this Kconfig
|
# There may be no files on internal SoC flash, so this Kconfig
|
||||||
# options has ben enabled to create some if listing does not
|
# options has ben enabled to create some if listing does not
|
||||||
# find in the first place.
|
# find in the first place.
|
||||||
CONFIG_SAMPLE_FATFS_CREATE_SOME_ENTRIES=y
|
CONFIG_FS_SAMPLE_CREATE_SOME_ENTRIES=y
|
||||||
|
|
@ -12,4 +12,4 @@ CONFIG_DISK_DRIVER_FLASH=y
|
||||||
# There may be no files on internal SoC flash, so this Kconfig
|
# There may be no files on internal SoC flash, so this Kconfig
|
||||||
# options has ben enabled to create some if listing does not
|
# options has ben enabled to create some if listing does not
|
||||||
# find in the first place.
|
# find in the first place.
|
||||||
CONFIG_SAMPLE_FATFS_CREATE_SOME_ENTRIES=y
|
CONFIG_FS_SAMPLE_CREATE_SOME_ENTRIES=y
|
||||||
20
samples/subsys/fs/fs_sample/prj_ext.conf
Normal file
20
samples/subsys/fs/fs_sample/prj_ext.conf
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
# Copyright (c) 2023 Antmicro <www.antmicro.com>
|
||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
CONFIG_LOG=y
|
||||||
|
CONFIG_LOG_MODE_IMMEDIATE=y
|
||||||
|
|
||||||
|
CONFIG_MAIN_STACK_SIZE=2048
|
||||||
|
|
||||||
|
CONFIG_FILE_SYSTEM=y
|
||||||
|
CONFIG_FILE_SYSTEM_EXT2=y
|
||||||
|
|
||||||
|
# Enable to allow formatting
|
||||||
|
# CONFIG_FILE_SYSTEM_MKFS=y
|
||||||
|
# CONFIG_TEST_RANDOM_GENERATOR=y
|
||||||
|
|
||||||
|
CONFIG_DISK_ACCESS=y
|
||||||
|
CONFIG_DISK_DRIVER_SDMMC=y
|
||||||
|
|
||||||
|
# First block of first partition after GPT
|
||||||
|
CONFIG_EXT2_DISK_STARTING_SECTOR=2082
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
sample:
|
sample:
|
||||||
name: Fat filesystem sample
|
name: Filesystems sample
|
||||||
common:
|
common:
|
||||||
tags: filesystem
|
tags: filesystem
|
||||||
modules:
|
modules:
|
||||||
|
|
@ -42,3 +42,6 @@ tests:
|
||||||
filter: dt_compat_enabled("zephyr,sdmmc-disk")
|
filter: dt_compat_enabled("zephyr,sdmmc-disk")
|
||||||
integration_platforms:
|
integration_platforms:
|
||||||
- frdm_k64f
|
- frdm_k64f
|
||||||
|
sample.filesystem.ext2:
|
||||||
|
extra_args: CONF_FILE="prj_ext.conf"
|
||||||
|
platform_allow: hifive_unmatched bl5340_dvk_cpuapp
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2019 Tavish Naruka <tavishnaruka@gmail.com>
|
* Copyright (c) 2019 Tavish Naruka <tavishnaruka@gmail.com>
|
||||||
* Copyright (c) 2023 Nordic Semiconductor ASA
|
* Copyright (c) 2023 Nordic Semiconductor ASA
|
||||||
|
* Copyright (c) 2023 Antmicro <www.antmicro.com>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
@ -12,16 +13,17 @@
|
||||||
#include <zephyr/storage/disk_access.h>
|
#include <zephyr/storage/disk_access.h>
|
||||||
#include <zephyr/logging/log.h>
|
#include <zephyr/logging/log.h>
|
||||||
#include <zephyr/fs/fs.h>
|
#include <zephyr/fs/fs.h>
|
||||||
|
|
||||||
|
#if defined(CONFIG_FAT_FILESYSTEM_ELM)
|
||||||
|
|
||||||
#include <ff.h>
|
#include <ff.h>
|
||||||
|
|
||||||
LOG_MODULE_REGISTER(main);
|
/*
|
||||||
|
* Note the fatfs library is able to mount only strings inside _VOLUME_STRS
|
||||||
|
* in ffconf.h
|
||||||
|
*/
|
||||||
#define DISK_DRIVE_NAME "SD"
|
#define DISK_DRIVE_NAME "SD"
|
||||||
#define DISK_MOUNT_PT "/"DISK_DRIVE_NAME":"
|
#define DISK_MOUNT_PT "/"DISK_DRIVE_NAME":"
|
||||||
#define MAX_PATH 128
|
|
||||||
#define SOME_FILE_NAME "some.dat"
|
|
||||||
#define SOME_DIR_NAME "some"
|
|
||||||
#define SOME_REQUIRED_LEN MAX(sizeof(SOME_FILE_NAME), sizeof(SOME_DIR_NAME))
|
|
||||||
|
|
||||||
static FATFS fat_fs;
|
static FATFS fat_fs;
|
||||||
/* mounting info */
|
/* mounting info */
|
||||||
|
|
@ -30,8 +32,31 @@ static struct fs_mount_t mp = {
|
||||||
.fs_data = &fat_fs,
|
.fs_data = &fat_fs,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#elif defined(CONFIG_FILE_SYSTEM_EXT2)
|
||||||
|
|
||||||
|
#include <zephyr/fs/ext2.h>
|
||||||
|
|
||||||
|
#define DISK_DRIVE_NAME "SDMMC"
|
||||||
|
#define DISK_MOUNT_PT "/ext"
|
||||||
|
|
||||||
|
static struct fs_mount_t mp = {
|
||||||
|
.type = FS_EXT2,
|
||||||
|
.flags = FS_MOUNT_FLAG_NO_FORMAT,
|
||||||
|
.storage_dev = (void *)DISK_DRIVE_NAME,
|
||||||
|
.mnt_point = "/ext",
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
LOG_MODULE_REGISTER(main);
|
||||||
|
|
||||||
|
#define MAX_PATH 128
|
||||||
|
#define SOME_FILE_NAME "some.dat"
|
||||||
|
#define SOME_DIR_NAME "some"
|
||||||
|
#define SOME_REQUIRED_LEN MAX(sizeof(SOME_FILE_NAME), sizeof(SOME_DIR_NAME))
|
||||||
|
|
||||||
static int lsdir(const char *path);
|
static int lsdir(const char *path);
|
||||||
#ifdef CONFIG_SAMPLE_FATFS_CREATE_SOME_ENTRIES
|
#ifdef CONFIG_FS_SAMPLE_CREATE_SOME_ENTRIES
|
||||||
static bool create_some_entries(const char *base_path)
|
static bool create_some_entries(const char *base_path)
|
||||||
{
|
{
|
||||||
char path[MAX_PATH];
|
char path[MAX_PATH];
|
||||||
|
|
@ -71,10 +96,6 @@ static bool create_some_entries(const char *base_path)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*
|
|
||||||
* Note the fatfs library is able to mount only strings inside _VOLUME_STRS
|
|
||||||
* in ffconf.h
|
|
||||||
*/
|
|
||||||
static const char *disk_mount_pt = DISK_MOUNT_PT;
|
static const char *disk_mount_pt = DISK_MOUNT_PT;
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
|
|
@ -113,10 +134,14 @@ int main(void)
|
||||||
|
|
||||||
int res = fs_mount(&mp);
|
int res = fs_mount(&mp);
|
||||||
|
|
||||||
|
#if defined(CONFIG_FAT_FILESYSTEM_ELM)
|
||||||
if (res == FR_OK) {
|
if (res == FR_OK) {
|
||||||
|
#else
|
||||||
|
if (res == 0) {
|
||||||
|
#endif
|
||||||
printk("Disk mounted.\n");
|
printk("Disk mounted.\n");
|
||||||
if (lsdir(disk_mount_pt) == 0) {
|
if (lsdir(disk_mount_pt) == 0) {
|
||||||
#ifdef CONFIG_SAMPLE_FATFS_CREATE_SOME_ENTRIES
|
#ifdef CONFIG_FS_SAMPLE_CREATE_SOME_ENTRIES
|
||||||
if (create_some_entries(disk_mount_pt)) {
|
if (create_some_entries(disk_mount_pt)) {
|
||||||
lsdir(disk_mount_pt);
|
lsdir(disk_mount_pt);
|
||||||
}
|
}
|
||||||
|
|
@ -126,6 +151,8 @@ int main(void)
|
||||||
printk("Error mounting disk.\n");
|
printk("Error mounting disk.\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fs_unmount(&mp);
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
k_sleep(K_MSEC(1000));
|
k_sleep(K_MSEC(1000));
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue