FatFS: mutex protection, ChibiOS glue layer

This commit is contained in:
Andrey Gusakov 2025-01-24 12:25:31 +03:00 committed by rusefillc
parent 2637b1aa5a
commit 09105c6a3e
3 changed files with 114 additions and 2 deletions

View File

@ -1,7 +1,8 @@
# FATFS files. # FATFS files.
FATFSSRC = ${CHIBIOS}/os/various/fatfs_bindings/fatfs_diskio.c \ FATFSSRC = ${CHIBIOS}/os/various/fatfs_bindings/fatfs_diskio.c \
${CHIBIOS}/os/various/fatfs_bindings/fatfs_syscall.c \ ${PROJECT_DIR}/ext/FatFS/ffsystem.c \
${PROJECT_DIR}/ext/FatFS/ff.c \ ${PROJECT_DIR}/ext/FatFS/ff.c \
${PROJECT_DIR}/ext/FatFS/ffunicode.c ${PROJECT_DIR}/ext/FatFS/ffunicode.c
FATFSINC = ${PROJECT_DIR}/ext FATFSINC = ${PROJECT_DIR}/ext

View File

@ -280,7 +280,7 @@
/ lock control is independent of re-entrancy. */ / lock control is independent of re-entrancy. */
#define FF_FS_REENTRANT 0 #define FF_FS_REENTRANT 1
#define FF_FS_TIMEOUT TIME_MS2I(1000) #define FF_FS_TIMEOUT TIME_MS2I(1000)
#define FF_SYNC_t semaphore_t* #define FF_SYNC_t semaphore_t*
/* The option FF_FS_REENTRANT switches the re-entrancy (thread safe) of the FatFs /* The option FF_FS_REENTRANT switches the re-entrancy (thread safe) of the FatFs

View File

@ -0,0 +1,111 @@
/*------------------------------------------------------------------------*/
/* A Sample Code of User Provided OS Dependent Functions for FatFs */
/*------------------------------------------------------------------------*/
#include "hal.h"
#include "ff.h"
#if FF_USE_LFN == 3 /* Use dynamic memory allocation */
/*------------------------------------------------------------------------*/
/* Allocate/Free a Memory Block */
/*------------------------------------------------------------------------*/
#include <stdlib.h> /* with POSIX API */
void* ff_memalloc ( /* Returns pointer to the allocated memory block (null if not enough core) */
UINT msize /* Number of bytes to allocate */
)
{
return malloc((size_t)msize); /* Allocate a new memory block */
}
void ff_memfree (
void* mblock /* Pointer to the memory block to free (no effect if null) */
)
{
free(mblock); /* Free the memory block */
}
#endif
#if FF_FS_REENTRANT /* Mutal exclusion */
/*------------------------------------------------------------------------*/
/* Definitions of Mutex */
/*------------------------------------------------------------------------*/
static semaphore_t ff_sem[FF_VOLUMES + 1];
/*------------------------------------------------------------------------*/
/* Create a Mutex */
/*------------------------------------------------------------------------*/
/* This function is called in f_mount function to create a new mutex
/ or semaphore for the volume. When a 0 is returned, the f_mount function
/ fails with FR_INT_ERR.
*/
int ff_mutex_create ( /* Returns 1:Function succeeded or 0:Could not create the mutex */
int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */
)
{
semaphore_t *sobj = &ff_sem[vol];
chSemObjectInit(sobj, 1);
return 1;
}
/*------------------------------------------------------------------------*/
/* Delete a Mutex */
/*------------------------------------------------------------------------*/
/* This function is called in f_mount function to delete a mutex or
/ semaphore of the volume created with ff_mutex_create function.
*/
void ff_mutex_delete ( /* Returns 1:Function succeeded or 0:Could not delete due to an error */
int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */
)
{
semaphore_t *sobj = &ff_sem[vol];
chSemReset(sobj, 0);
}
/*------------------------------------------------------------------------*/
/* Request a Grant to Access the Volume */
/*------------------------------------------------------------------------*/
/* This function is called on enter file functions to lock the volume.
/ When a 0 is returned, the file function fails with FR_TIMEOUT.
*/
int ff_mutex_take ( /* Returns 1:Succeeded or 0:Timeout */
int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */
)
{
semaphore_t *sobj = &ff_sem[vol];
msg_t msg = chSemWaitTimeout(sobj, (systime_t)FF_FS_TIMEOUT);
return msg == MSG_OK;
}
/*------------------------------------------------------------------------*/
/* Release a Grant to Access the Volume */
/*------------------------------------------------------------------------*/
/* This function is called on leave file functions to unlock the volume.
*/
void ff_mutex_give (
int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */
)
{
semaphore_t *sobj = &ff_sem[vol];
chSemSignal(sobj);
}
#endif /* FF_FS_REENTRANT */