Skip to content

Commit 08d652d

Browse files
kica-zde-nordic
authored andcommitted
test: fs: enable fstab devicetree integration for fatfs
Added tests in order to verify the functionality of the newly added fatfs fstab integration. Signed-off-by: Carlo Kirchmeier <carlo.kirchmeier@zuehlke.com> Co-authored-by: Carlo Kirchmeier <carlo.kirchmeier@zuehlke.com> Co-authored-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
1 parent 1ea0267 commit 08d652d

File tree

6 files changed

+167
-0
lines changed

6 files changed

+167
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright (c) 2025 Endress+Hauser AG
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
cmake_minimum_required(VERSION 3.20.0)
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(fatfs_fstab_sample)
7+
8+
FILE(GLOB app_sources src/*.c)
9+
target_sources(app PRIVATE ${app_sources})
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
.. zephyr:code-sample:: fatfs-fstab
2+
:name: Fatfs filesystem fstab
3+
:relevant-api: file_system_api
4+
5+
Define fatfs filesystems in the devicetree.
6+
7+
Overview
8+
***********
9+
10+
This sample shows how to define a fatfs fstab entry in the devicetree.
11+
This scenario uses a fatfs on a RAM disk. The sample is run on the
12+
``native_sim`` board.
13+
14+
Building and running
15+
********************
16+
17+
To run this sample, build it for the ``native_sim`` board
18+
and afterwards run the generated executable file within the build folder.
19+
20+
The RAM disk sample for the ``native_sim`` board can be built as follows:
21+
22+
.. zephyr-app-commands::
23+
:zephyr-app: samples/subsys/fs/fatfs_fstab
24+
:host-os: unix
25+
:board: native_sim
26+
:gen-args: -DDTC_OVERLAY_FILE=fatfs_fstab.overlay
27+
:goals: build
28+
:compact:
29+
30+
Sample Output
31+
=============
32+
33+
When the sample runs successfully you should see following message on the screen:
34+
.. code-block:: console
35+
36+
I: Filesystem access successful
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2025 Endress+Hauser AG
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/ {
8+
fstab {
9+
compatible = "zephyr,fstab";
10+
ffs1: ffs1 {
11+
compatible = "zephyr,fstab,fatfs";
12+
automount;
13+
disk-access;
14+
mount-point = "/RAM:";
15+
};
16+
};
17+
18+
ramdisk0 {
19+
compatible = "zephyr,ram-disk";
20+
disk-name = "RAM";
21+
sector-size = <512>;
22+
sector-count = <128>;
23+
};
24+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CONFIG_DISK_ACCESS=y
2+
CONFIG_DISK_DRIVERS=y
3+
4+
CONFIG_LOG=y
5+
CONFIG_LOG_MODE_MINIMAL=y
6+
7+
CONFIG_FILE_SYSTEM=y
8+
CONFIG_FILE_SYSTEM_MKFS=y
9+
CONFIG_FAT_FILESYSTEM_ELM=y
10+
CONFIG_FS_FATFS_FSTAB_AUTOMOUNT=y
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
sample:
2+
name: fatfs fstab sample
3+
tests:
4+
sample.filesystem.fat_fs.fstab:
5+
platform_allow:
6+
- native_sim
7+
extra_args:
8+
- EXTRA_DTC_OVERLAY_FILE="fatfs_fstab.overlay"
9+
tags: filesystem
10+
harness: console
11+
harness_config:
12+
type: one_line
13+
regex:
14+
- "I: Filesystem access successful"
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (c) 2025 Endress+Hauser AG
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <ff.h>
8+
#include <zephyr/device.h>
9+
#include <zephyr/fs/fs.h>
10+
#include <zephyr/kernel.h>
11+
#include <zephyr/logging/log.h>
12+
#include <zephyr/storage/disk_access.h>
13+
14+
LOG_MODULE_REGISTER(main, LOG_LEVEL_INF);
15+
16+
#define AUTOMOUNT_NODE DT_NODELABEL(ffs1)
17+
FS_FSTAB_DECLARE_ENTRY(AUTOMOUNT_NODE);
18+
19+
#define FILE_PATH "/RAM:/hello.txt"
20+
21+
int main(void)
22+
{
23+
struct fs_file_t file;
24+
int rc;
25+
static const char data[] = "Hello";
26+
/* You can get direct mount point to automounted node too */
27+
struct fs_mount_t *auto_mount_point = &FS_FSTAB_ENTRY(AUTOMOUNT_NODE);
28+
struct fs_dirent stat;
29+
30+
fs_file_t_init(&file);
31+
32+
rc = fs_open(&file, FILE_PATH, FS_O_CREATE | FS_O_WRITE);
33+
if (rc != 0) {
34+
LOG_ERR("Accessing filesystem failed");
35+
return rc;
36+
}
37+
38+
rc = fs_write(&file, data, strlen(data));
39+
if (rc != strlen(data)) {
40+
LOG_ERR("Writing filesystem failed");
41+
return rc;
42+
}
43+
44+
rc = fs_close(&file);
45+
if (rc != 0) {
46+
LOG_ERR("Closing file failed");
47+
return rc;
48+
}
49+
50+
/* You can unmount the automount node */
51+
rc = fs_unmount(auto_mount_point);
52+
if (rc != 0) {
53+
LOG_ERR("Failed to do unmount");
54+
return rc;
55+
};
56+
57+
/* And mount it back */
58+
rc = fs_mount(auto_mount_point);
59+
if (rc != 0) {
60+
LOG_ERR("Failed to remount the auto-mount node");
61+
return rc;
62+
}
63+
64+
/* Is the file still there? */
65+
rc = fs_stat(FILE_PATH, &stat);
66+
if (rc != 0) {
67+
LOG_ERR("File status check failed %d", rc);
68+
return rc;
69+
}
70+
71+
LOG_INF("Filesystem access successful");
72+
73+
return 0;
74+
}

0 commit comments

Comments
 (0)