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 <Nicola.Ochsenbein@husqvarnagroup.com>
This commit is contained in:
Nicola Ochsenbein 2022-12-16 16:05:02 +01:00 committed by Carles Cufí
parent 303eb76293
commit eba73727ee
4 changed files with 91 additions and 1 deletions

View file

@ -18,9 +18,12 @@ if(CONFIG_FAT_FILESYSTEM_ELM)
zephyr_library_sources_ifdef(CONFIG_FS_FATFS_LFN
${ZEPHYR_FATFS_MODULE_DIR}/option/ffunicode.c
${ZEPHYR_FATFS_MODULE_DIR}/option/ffsystem.c
)
if(DEFINED CONFIG_FS_FATFS_LFN OR DEFINED CONFIG_FS_FATFS_REENTRANT)
zephyr_library_sources(zfs_ffsystem.c)
endif()
zephyr_library_link_libraries(ELMFAT)
target_link_libraries(ELMFAT INTERFACE zephyr_interface)
endif()

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2022 Nordic Semiconductor ASA
* Copyright (c) 2023 Husqvarna AB
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -62,6 +63,14 @@
#define FF_FS_EXFAT CONFIG_FS_FATFS_EXFAT
#endif /* defined(CONFIG_FS_FATFS_EXFAT) */
#if defined(CONFIG_FS_FATFS_REENTRANT)
#undef FF_FS_REENTRANT
#undef FF_FS_TIMEOUT
#include <zephyr/kernel.h>
#define FF_FS_REENTRANT CONFIG_FS_FATFS_REENTRANT
#define FF_FS_TIMEOUT K_FOREVER
#endif /* defined(CONFIG_FS_FATFS_REENTRANT) */
/*
* These options are override from default values, but have no Kconfig
* options.

View file

@ -0,0 +1,69 @@
/*
* OS Dependent Functions for FatFs
*
* Copyright (c) 2023 Husqvarna AB
*
* SPDX-License-Identifier: Apache-2.0
*/
/* The file is based on template file by (C)ChaN, 2022, as
* available from FAT FS module source:
* https://github.com/zephyrproject-rtos/fatfs/blob/master/option/ffsystem.c
*/
#include <ff.h>
#if FF_USE_LFN == 3 /* Dynamic memory allocation */
/* Allocate a memory block */
void *ff_memalloc(UINT msize)
{
return k_malloc(msize);
}
/* Free a memory block */
void ff_memfree(void *mblock)
{
k_free(mblock);
}
#endif /* FF_USE_LFN == 3 */
#if FF_FS_REENTRANT /* Mutual exclusion */
/* Table of Zephyr mutex. One for each volume and an extra one for the ff system.
* See also the template file used as reference. Link is available in the header of this file.
*/
static struct k_mutex fs_reentrant_mutex[FF_VOLUMES + 1];
/* Create a Mutex
* Mutex ID vol: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES)
* Returns 1: Succeeded or 0: Could not create the mutex
*/
int ff_mutex_create(int vol)
{
return (int)(k_mutex_init(&fs_reentrant_mutex[vol]) == 0);
}
/* Delete a Mutex
* Mutex ID vol: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES)
*/
void ff_mutex_delete(int vol)
{
/* (nothing to do) */
(void)vol;
}
/* Request Grant to Access the Volume
* Mutex ID vol: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES)
* Returns 1: Succeeded or 0: Timeout
*/
int ff_mutex_take(int vol)
{
return (int)(k_mutex_lock(&fs_reentrant_mutex[vol], FF_FS_TIMEOUT) == 0);
}
/* Release Grant to Access the Volume
* Mutex ID vol: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES)
*/
void ff_mutex_give(int vol)
{
k_mutex_unlock(&fs_reentrant_mutex[vol]);
}
#endif /* FF_FS_REENTRANT */

View file

@ -1,5 +1,6 @@
# Copyright (c) 2016 Intel Corporation
# Copyright (c) 2020 Nordic Semiconductor ASA
# Copyright (c) 2023 Husqvarna AB
# SPDX-License-Identifier: Apache-2.0
config FAT_FILESYSTEM_ELM
@ -220,6 +221,14 @@ config FS_FATFS_WINDOW_ALIGNMENT
that, in worst scenario, value provided here may cause FATFS
structure to have size of twice the value.
config FS_FATFS_REENTRANT
bool "FatFs reentrant"
depends on !FS_FATFS_LFN_MODE_BSS
help
Enable the FatFs re-entrancy (thread safe) option for file/directory
access for each volume. Will create a zephyr mutex object for each
FatFs volume and a FatFs system mutex.
endmenu
endif # FAT_FILESYSTEM_ELM