Skip to content

Commit eba7372

Browse files
Ox11carlescufi
authored andcommitted
modules: fs: Add reentrant zephyr support
This commit enables zephyr to configure the FatFs FF_FS_REENTRANT option and support fs actions from multiple threads. CONFIG_FS_FATFS_REENTRANT enables the option and provides zephyr mutex wrappers. Signed-off-by: Nicola Ochsenbein <[email protected]>
1 parent 303eb76 commit eba7372

File tree

4 files changed

+91
-1
lines changed

4 files changed

+91
-1
lines changed

modules/fatfs/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ if(CONFIG_FAT_FILESYSTEM_ELM)
1818

1919
zephyr_library_sources_ifdef(CONFIG_FS_FATFS_LFN
2020
${ZEPHYR_FATFS_MODULE_DIR}/option/ffunicode.c
21-
${ZEPHYR_FATFS_MODULE_DIR}/option/ffsystem.c
2221
)
2322

23+
if(DEFINED CONFIG_FS_FATFS_LFN OR DEFINED CONFIG_FS_FATFS_REENTRANT)
24+
zephyr_library_sources(zfs_ffsystem.c)
25+
endif()
26+
2427
zephyr_library_link_libraries(ELMFAT)
2528
target_link_libraries(ELMFAT INTERFACE zephyr_interface)
2629
endif()

modules/fatfs/zephyr_fatfs_config.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2022 Nordic Semiconductor ASA
3+
* Copyright (c) 2023 Husqvarna AB
34
*
45
* SPDX-License-Identifier: Apache-2.0
56
*/
@@ -62,6 +63,14 @@
6263
#define FF_FS_EXFAT CONFIG_FS_FATFS_EXFAT
6364
#endif /* defined(CONFIG_FS_FATFS_EXFAT) */
6465

66+
#if defined(CONFIG_FS_FATFS_REENTRANT)
67+
#undef FF_FS_REENTRANT
68+
#undef FF_FS_TIMEOUT
69+
#include <zephyr/kernel.h>
70+
#define FF_FS_REENTRANT CONFIG_FS_FATFS_REENTRANT
71+
#define FF_FS_TIMEOUT K_FOREVER
72+
#endif /* defined(CONFIG_FS_FATFS_REENTRANT) */
73+
6574
/*
6675
* These options are override from default values, but have no Kconfig
6776
* options.

modules/fatfs/zfs_ffsystem.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* OS Dependent Functions for FatFs
3+
*
4+
* Copyright (c) 2023 Husqvarna AB
5+
*
6+
* SPDX-License-Identifier: Apache-2.0
7+
*/
8+
/* The file is based on template file by (C)ChaN, 2022, as
9+
* available from FAT FS module source:
10+
* https://github.com/zephyrproject-rtos/fatfs/blob/master/option/ffsystem.c
11+
*/
12+
13+
#include <ff.h>
14+
15+
#if FF_USE_LFN == 3 /* Dynamic memory allocation */
16+
/* Allocate a memory block */
17+
void *ff_memalloc(UINT msize)
18+
{
19+
return k_malloc(msize);
20+
}
21+
22+
/* Free a memory block */
23+
void ff_memfree(void *mblock)
24+
{
25+
k_free(mblock);
26+
}
27+
#endif /* FF_USE_LFN == 3 */
28+
29+
#if FF_FS_REENTRANT /* Mutual exclusion */
30+
/* Table of Zephyr mutex. One for each volume and an extra one for the ff system.
31+
* See also the template file used as reference. Link is available in the header of this file.
32+
*/
33+
static struct k_mutex fs_reentrant_mutex[FF_VOLUMES + 1];
34+
35+
/* Create a Mutex
36+
* Mutex ID vol: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES)
37+
* Returns 1: Succeeded or 0: Could not create the mutex
38+
*/
39+
int ff_mutex_create(int vol)
40+
{
41+
return (int)(k_mutex_init(&fs_reentrant_mutex[vol]) == 0);
42+
}
43+
44+
/* Delete a Mutex
45+
* Mutex ID vol: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES)
46+
*/
47+
void ff_mutex_delete(int vol)
48+
{
49+
/* (nothing to do) */
50+
(void)vol;
51+
}
52+
53+
/* Request Grant to Access the Volume
54+
* Mutex ID vol: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES)
55+
* Returns 1: Succeeded or 0: Timeout
56+
*/
57+
int ff_mutex_take(int vol)
58+
{
59+
return (int)(k_mutex_lock(&fs_reentrant_mutex[vol], FF_FS_TIMEOUT) == 0);
60+
}
61+
62+
/* Release Grant to Access the Volume
63+
* Mutex ID vol: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES)
64+
*/
65+
void ff_mutex_give(int vol)
66+
{
67+
k_mutex_unlock(&fs_reentrant_mutex[vol]);
68+
}
69+
#endif /* FF_FS_REENTRANT */

subsys/fs/Kconfig.fatfs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Copyright (c) 2016 Intel Corporation
22
# Copyright (c) 2020 Nordic Semiconductor ASA
3+
# Copyright (c) 2023 Husqvarna AB
34
# SPDX-License-Identifier: Apache-2.0
45

56
config FAT_FILESYSTEM_ELM
@@ -220,6 +221,14 @@ config FS_FATFS_WINDOW_ALIGNMENT
220221
that, in worst scenario, value provided here may cause FATFS
221222
structure to have size of twice the value.
222223

224+
config FS_FATFS_REENTRANT
225+
bool "FatFs reentrant"
226+
depends on !FS_FATFS_LFN_MODE_BSS
227+
help
228+
Enable the FatFs re-entrancy (thread safe) option for file/directory
229+
access for each volume. Will create a zephyr mutex object for each
230+
FatFs volume and a FatFs system mutex.
231+
223232
endmenu
224233

225234
endif # FAT_FILESYSTEM_ELM

0 commit comments

Comments
 (0)