Skip to content
Snippets Groups Projects
Verified Commit 86c7418a authored by rahix's avatar rahix
Browse files

chore(fatfs): Port to new mutex API


Using a bare FreeRTOS mutex is deprecated.  Replace it with the new
`struct mutex`.  This should increase stability of the module.  In the
process of switching, also remove the `EPIC_FAT_STATIC_SEMAPHORE` define
as it is no longer needed with the new mutex API.

Signed-off-by: default avatarRahix <rahix@rahix.de>
parent 75278836
No related branches found
No related tags found
No related merge requests found
...@@ -25,15 +25,12 @@ ...@@ -25,15 +25,12 @@
#include "modules/log.h" #include "modules/log.h"
#include "modules/modules.h" #include "modules/modules.h"
#include "api/common.h" #include "api/common.h"
#include "modules/mutex.h"
#define SSLOG_DEBUG(...) LOG_DEBUG("fatfs", __VA_ARGS__) #define SSLOG_DEBUG(...) LOG_DEBUG("fatfs", __VA_ARGS__)
#define SSLOG_INFO(...) LOG_INFO("fatfs", __VA_ARGS__) #define SSLOG_INFO(...) LOG_INFO("fatfs", __VA_ARGS__)
#define SSLOG_ERR(...) LOG_ERR("fatfs", __VA_ARGS__) #define SSLOG_ERR(...) LOG_ERR("fatfs", __VA_ARGS__)
#ifndef EPIC_FAT_STATIC_SEMAPHORE
#define EPIC_FAT_STATIC_SEMAPHORE 0
#endif
/* clang-format off */ /* clang-format off */
#define EPIC_FAT_MAX_OPENED (1 << (EPIC_FAT_FD_INDEX_BITS)) #define EPIC_FAT_MAX_OPENED (1 << (EPIC_FAT_FD_INDEX_BITS))
#define EPIC_FAT_FD_GENERATION_BITS (31 - (EPIC_FAT_FD_INDEX_BITS)) #define EPIC_FAT_FD_GENERATION_BITS (31 - (EPIC_FAT_FD_INDEX_BITS))
...@@ -67,8 +64,6 @@ struct EpicFileSystem { ...@@ -67,8 +64,6 @@ struct EpicFileSystem {
static const int s_libffToErrno[20]; static const int s_libffToErrno[20];
static const char *f_get_rc_string(FRESULT rc); static const char *f_get_rc_string(FRESULT rc);
static bool globalLockAccquire();
static void globalLockRelease();
static void efs_close_all(EpicFileSystem *fs, int coreMask); static void efs_close_all(EpicFileSystem *fs, int coreMask);
/** /**
...@@ -97,11 +92,7 @@ static void efs_init_stat(struct epic_stat *stat, FILINFO *finfo); ...@@ -97,11 +92,7 @@ static void efs_init_stat(struct epic_stat *stat, FILINFO *finfo);
static EpicFileSystem s_globalFileSystem; static EpicFileSystem s_globalFileSystem;
#if (EPIC_FAT_STATIC_SEMAPHORE == 1) static struct mutex fatfs_lock = { 0 };
static StaticSemaphore_t s_globalLockBuffer;
#endif
static SemaphoreHandle_t s_globalLock = NULL;
static void cb_attachTimer(void *a, uint32_t b) static void cb_attachTimer(void *a, uint32_t b)
{ {
...@@ -118,11 +109,7 @@ void fatfs_init() ...@@ -118,11 +109,7 @@ void fatfs_init()
assert(!s_initCalled); assert(!s_initCalled);
s_initCalled = true; s_initCalled = true;
#if (EPIC_FAT_STATIC_SEMAPHORE == 1) mutex_create(&fatfs_lock);
s_globalLock = xSemaphoreCreateMutexStatic(&s_globalLockBuffer);
#else
s_globalLock = xSemaphoreCreateMutex();
#endif
s_globalFileSystem.generationCount = 1; s_globalFileSystem.generationCount = 1;
fatfs_attach(); fatfs_attach();
...@@ -142,26 +129,24 @@ int fatfs_attach() ...@@ -142,26 +129,24 @@ int fatfs_attach()
{ {
FRESULT ff_res; FRESULT ff_res;
int rc = 0; int rc = 0;
if (globalLockAccquire()) {
EpicFileSystem *fs = &s_globalFileSystem;
if (!fs->attached) {
ff_res = f_mount(&fs->FatFs, "/", 0);
if (ff_res == FR_OK) {
fs->attached = true;
SSLOG_INFO("attached\n");
} else {
SSLOG_ERR(
"f_mount error %s\n",
f_get_rc_string(ff_res)
);
rc = -s_libffToErrno[ff_res];
}
}
globalLockRelease(); mutex_lock(&fatfs_lock);
} else {
SSLOG_ERR("Failed to lock\n"); EpicFileSystem *fs = &s_globalFileSystem;
if (!fs->attached) {
ff_res = f_mount(&fs->FatFs, "/", 0);
if (ff_res == FR_OK) {
fs->attached = true;
SSLOG_INFO("attached\n");
} else {
SSLOG_ERR(
"f_mount error %s\n", f_get_rc_string(ff_res)
);
rc = -s_libffToErrno[ff_res];
}
} }
mutex_unlock(&fatfs_lock);
return rc; return rc;
} }
...@@ -227,24 +212,12 @@ static const char *f_get_rc_string(FRESULT rc) ...@@ -227,24 +212,12 @@ static const char *f_get_rc_string(FRESULT rc)
return p; return p;
} }
static bool globalLockAccquire()
{
return (int)(xSemaphoreTake(s_globalLock, FF_FS_TIMEOUT) == pdTRUE);
}
static void globalLockRelease()
{
xSemaphoreGive(s_globalLock);
}
int efs_lock_global(EpicFileSystem **fs) int efs_lock_global(EpicFileSystem **fs)
{ {
*fs = NULL; *fs = NULL;
if (!globalLockAccquire()) { mutex_lock(&fatfs_lock);
return -EBUSY;
}
if (!s_globalFileSystem.attached) { if (!s_globalFileSystem.attached) {
globalLockRelease(); mutex_unlock(&fatfs_lock);
return -ENODEV; return -ENODEV;
} }
*fs = &s_globalFileSystem; *fs = &s_globalFileSystem;
...@@ -259,7 +232,7 @@ int efs_lock_global(EpicFileSystem **fs) ...@@ -259,7 +232,7 @@ int efs_lock_global(EpicFileSystem **fs)
void efs_unlock_global(EpicFileSystem *fs) void efs_unlock_global(EpicFileSystem *fs)
{ {
(void)fs; (void)fs;
globalLockRelease(); mutex_unlock(&fatfs_lock);
} }
static bool efs_get_opened( static bool efs_get_opened(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment