diff --git a/epicardium/fs/filesystem_fat.c b/epicardium/fs/filesystem_fat.c index 43a4725d6c1b0e238a4d3373cd38d339eefb9a9d..7d95510360324ac5e105a53aa90a3a96ee0936c1 100644 --- a/epicardium/fs/filesystem_fat.c +++ b/epicardium/fs/filesystem_fat.c @@ -22,6 +22,8 @@ #include "epicardium.h" #include "card10.h" #include "modules/log.h" +#include "modules/modules.h" +#include "api/common.h" #define SSLOG_DEBUG(...) LOG_DEBUG("fatfs", __VA_ARGS__) #define SSLOG_INFO(...) LOG_INFO("fatfs", __VA_ARGS__) @@ -44,6 +46,7 @@ struct FatObject { uint32_t generation; + int coreMask; enum epic_stat_type type; union { FIL file; @@ -56,6 +59,7 @@ struct EpicFileSystem { uint32_t generationCount; bool initialized; FATFS FatFs; + int lockCoreMask; }; // this table converts from FRESULT to POSIX errno @@ -64,7 +68,7 @@ static const int s_libffToErrno[20]; static const char *f_get_rc_string(FRESULT rc); static bool globalLockAccquire(); static void globalLockRelease(); -static void efs_close_all(EpicFileSystem *fs); +static void efs_close_all(EpicFileSystem *fs, int coreMask); /** * if EPICSTAT_NONE is passed to `expected`, the type is not checked. @@ -159,7 +163,7 @@ void fatfs_detach() FRESULT ff_res; EpicFileSystem *fs; if (efs_lock_global(&fs) == 0) { - efs_close_all(fs); + efs_close_all(fs, EPICARDIUM_COREMASK_BOTH); //unmount by passing NULL as fs object, will destroy our sync object via ff_del_syncobj ff_res = f_mount(NULL, "/", 0); @@ -177,6 +181,14 @@ void fatfs_detach() } } +void fatfs_close_all(int coreMask) +{ + EpicFileSystem *fs; + if (efs_lock_global(&fs) == 0) { + efs_close_all(fs, coreMask); + efs_unlock_global(fs); + } +} static const char *f_get_rc_string(FRESULT rc) { static const TCHAR *rcstrings = @@ -216,6 +228,11 @@ int efs_lock_global(EpicFileSystem **fs) return -ENODEV; } *fs = &s_globalFileSystem; + if (xTaskGetCurrentTaskHandle() == dispatcher_task_id) { + s_globalFileSystem.lockCoreMask = EPICARDIUM_COREMASK_1; + } else { + s_globalFileSystem.lockCoreMask = EPICARDIUM_COREMASK_0; + } return 0; } @@ -292,6 +309,7 @@ static int efs_obj_init( } obj->type = type; obj->generation = generation; + obj->coreMask = fs->lockCoreMask; return EPIC_FAT_FD(index, generation); } @@ -300,6 +318,7 @@ static void efs_obj_deinit(EpicFileSystem *fs, struct FatObject *obj) { obj->type = EPICSTAT_NONE; obj->generation = 0; + obj->coreMask = 0; } /* here we're trying to mirror glibc's behaviour: @@ -392,9 +411,13 @@ int efs_close(EpicFileSystem *fs, int fd) return res; } -void efs_close_all(EpicFileSystem *fs) +void efs_close_all(EpicFileSystem *fs, int coreMask) { + assert(coreMask != 0); for (int i = 0; i < EPIC_FAT_MAX_OPENED; ++i) { + if (!(fs->pool[i].coreMask & coreMask)) { + continue; + } switch (fs->pool[i].type) { case EPICSTAT_FILE: f_close(&fs->pool[i].file); diff --git a/epicardium/modules/filesystem.h b/epicardium/modules/filesystem.h index 7a638d5ab4a87d39fe5b3bd4edd5b3c1901dc194..720147ecd04a8f07755052d05adb90871b360bd7 100644 --- a/epicardium/modules/filesystem.h +++ b/epicardium/modules/filesystem.h @@ -22,4 +22,13 @@ int fatfs_attach(void); /** close all opened FDs, sync and deinitialize FLASH layer */ void fatfs_detach(void); +/** close all onpened FDs + * TODO: add ability to close FDs opened by core0/core1 only + */ +#define EPICARDIUM_COREMASK_0 0x01 +#define EPICARDIUM_COREMASK_1 0x02 +#define EPICARDIUM_COREMASK_BOTH 0x03 + +void fatfs_close_all(int coreMask); + #endif//EPICARDIUM_MODULE_FILESYSTEM_INCLUDED diff --git a/epicardium/modules/hardware.c b/epicardium/modules/hardware.c index 44ae9ab75dd0f9dd7223f40837db38e6b2d47e08..01d76545607e64c0ea08cf48f1ebb578445e3d7b 100644 --- a/epicardium/modules/hardware.c +++ b/epicardium/modules/hardware.c @@ -206,6 +206,11 @@ int hardware_reset(void) api_interrupt_init(); api_dispatcher_init(); + /* + * close all FDs currently owned by core1 + */ + fatfs_close_all(EPICARDIUM_COREMASK_1); + /* Personal State */ const int personal_state_is_persistent = epic_personal_state_is_persistent();