Skip to content

Commit

Permalink
test: fs: enable fstab devicetree integration for fatfs
Browse files Browse the repository at this point in the history
Added tests in order to verify the functionality of the
newly added fatfs fstab integration.

Signed-off-by: Carlo Kirchmeier <[email protected]>
  • Loading branch information
kica-z committed Jan 17, 2025
1 parent f17a971 commit 4635e1a
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 0 deletions.
9 changes: 9 additions & 0 deletions samples/subsys/fs/fatfs_fstab/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) 2025 Endress+Hauser AG
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(fatfs_fstab_sample)

FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})
35 changes: 35 additions & 0 deletions samples/subsys/fs/fatfs_fstab/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
.. zephyr:code-sample:: fatfs-fstab
:name: Fatfs filesystem fstab
:relevant-api: file_system_api

Define fatfs filesystems in the devicetree.

Overview
***********

This sample shows how to define a fatfs fstab entry in the devicetree.
This scenario uses a fatfs on a RAM disk. The sample is run on the
:zephyr:board:`native_sim` board.

Building and running
********************

To run this sample, build it for the :zephyr:board:`native_sim` board
and afterwards run the generated executable file within the build folder.

The RAM disk sample for the :zephyr:board:`native_sim` board can be built as follows:

.. zephyr-app-commands::
:zephyr-app: samples/subsys/fs/fatfs-fstab
:host-os: unix
:board: native_sim
:gen-args: -DDTC_OVERLAY_FILE=fatfs_fstab.overlay
:goals: build
:compact:

Sample Output
=============

When the sample runs successfully you should see following message on the screen:
.. code-block:: console
I: Filesystem access successful
24 changes: 24 additions & 0 deletions samples/subsys/fs/fatfs_fstab/fatfs_fstab.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2025 Endress+Hauser AG
*
* SPDX-License-Identifier: Apache-2.0
*/

/ {
fstab {
compatible = "zephyr,fstab";
ffs1: ffs1 {
compatible = "zephyr,fstab,fatfs";
automount;
disk-access;
mount-point = "/RAM";
};
};

ramdisk0 {
compatible = "zephyr,ram-disk";
disk-name = "RAM";
sector-size = <512>;
sector-count = <128>;
};
};
9 changes: 9 additions & 0 deletions samples/subsys/fs/fatfs_fstab/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CONFIG_DISK_ACCESS=y
CONFIG_DISK_DRIVERS=y

CONFIG_LOG=y
CONFIG_LOG_MODE_MINIMAL=y

CONFIG_FILE_SYSTEM=y
CONFIG_FILE_SYSTEM_MKFS=y
CONFIG_FAT_FILESYSTEM_ELM=y
9 changes: 9 additions & 0 deletions samples/subsys/fs/fatfs_fstab/sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
sample:
name: fatfs fstab sample
tests:
sample.filesystem.fat_fs.fstab:
platform_allow:
- native_sim
extra_args:
- EXTRA_DTC_OVERLAY_FILE="fatfs_fstab.overlay"
tags: filesystem
41 changes: 41 additions & 0 deletions samples/subsys/fs/fatfs_fstab/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2025 Endress+Hauser AG
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <ff.h>
#include <zephyr/device.h>
#include <zephyr/fs/fs.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/storage/disk_access.h>

LOG_MODULE_REGISTER(main, LOG_LEVEL_INF);

int main(void)
{
struct fs_file_t file;
int rc;
static const char data[] = "Hello";

fs_file_t_init(&file);
rc = fs_open(&file, "/RAM:/hello.txt", FS_O_CREATE | FS_O_WRITE);
if (rc != 0) {
LOG_ERR("Accessing filesystem failed");
return rc;
}
rc = fs_write(&file, data, strlen(data));
if (rc != strlen(data)) {
LOG_ERR("Writing filesystem failed");
return rc;
}
rc = fs_close(&file);
if (rc != 0) {
LOG_ERR("Closing file failed");
return rc;
}

LOG_INF("Filesystem access successful");
return 0;
}

0 comments on commit 4635e1a

Please sign in to comment.