From d028fa6bc45be2767e42565ee5f30c1d01b190cc Mon Sep 17 00:00:00 2001 From: swym <0xfd000000@gmail.com> Date: Mon, 15 Jul 2019 23:06:34 +0200 Subject: [PATCH] libff13: enable reentrancy glue functions for handling the locking are implement as weak symbols in libff13 itself and overriden to use FreeRTOS's semaphores in epicardium --- epicardium/cdcacm.c | 3 +- epicardium/fatfs.c | 109 +++++++++++++++++++++++++++++++++++++ epicardium/fatfs.h | 0 epicardium/main.c | 2 + epicardium/meson.build | 3 +- lib/ff13/Source/ff.h | 8 +-- lib/ff13/Source/ffconf.h | 6 +- lib/ff13/Source/ffsystem.c | 83 +++++----------------------- 8 files changed, 136 insertions(+), 78 deletions(-) create mode 100644 epicardium/fatfs.c create mode 100644 epicardium/fatfs.h diff --git a/epicardium/cdcacm.c b/epicardium/cdcacm.c index fd0f2d86..83481dd6 100644 --- a/epicardium/cdcacm.c +++ b/epicardium/cdcacm.c @@ -67,7 +67,8 @@ #define TOSTRING(x) STRINGIFY(x) /***** Global Data *****/ -volatile int configured; +//SWYM: rename to CDC_xy or put into struct CDC_state +volatile int configured; //SWYM: actually unused... volatile int suspended; volatile unsigned int event_flags; int remote_wake_en; diff --git a/epicardium/fatfs.c b/epicardium/fatfs.c new file mode 100644 index 00000000..5513f68b --- /dev/null +++ b/epicardium/fatfs.c @@ -0,0 +1,109 @@ +/* + * support routines for FatFs + */ + +#include <stddef.h> //NULL +#include <stdio.h> +#include <stdbool.h> +#include <ff.h> + +#include <FreeRTOS.h> +#include <semphr.h> + +static bool mount(void); + + +DIR dir; +FATFS FatFs; + +static volatile struct { + bool initiaized; +}s_state = { + .initiaized = false, +}; + +bool mount() +{ + FRESULT res; + res = f_mount(&FatFs,"/",0); + if(res != FR_OK) { + printf("f_mount error %d\n", res); + return false; + } + + res = f_opendir(&dir, "0:"); + if(res != FR_OK) { + printf("f_opendir error %d\n", res); + return false; + } + + return true; +} + +void fatfs_init() { + if(mount()) { + s_state.initiaized = true; + printf("FatFs mounted\n"); + } +} + +int ff_cre_syncobj ( /* 1:Function succeeded, 0:Could not create the sync object */ + BYTE vol, /* Corresponding volume (logical drive number) */ + FF_SYNC_t *sobj /* Pointer to return the created sync object */ +) +{ + *sobj = xSemaphoreCreateMutex(); + return (int)(*sobj != NULL); + +} + + +/*------------------------------------------------------------------------*/ +/* Delete a Synchronization Object */ +/*------------------------------------------------------------------------*/ +/* This function is called in f_mount() function to delete a synchronization +/ object that created with ff_cre_syncobj() function. When a 0 is returned, +/ the f_mount() function fails with FR_INT_ERR. +*/ + +int ff_del_syncobj ( /* 1:Function succeeded, 0:Could not delete due to an error */ + FF_SYNC_t sobj /* Sync object tied to the logical drive to be deleted */ +) +{ + printf("%s\n", __PRETTY_FUNCTION__); + /* FreeRTOS */ + vSemaphoreDelete(sobj); + return 1; + +} + + +/*------------------------------------------------------------------------*/ +/* Request Grant to Access the Volume */ +/*------------------------------------------------------------------------*/ +/* This function is called on entering file functions to lock the volume. +/ When a 0 is returned, the file function fails with FR_TIMEOUT. +*/ + +int ff_req_grant ( /* 1:Got a grant to access the volume, 0:Could not get a grant */ + FF_SYNC_t sobj /* Sync object to wait */ +) +{ + /* FreeRTOS */ + return (int)(xSemaphoreTake(sobj, FF_FS_TIMEOUT) == pdTRUE); +} + + +/*------------------------------------------------------------------------*/ +/* Release Grant to Access the Volume */ +/*------------------------------------------------------------------------*/ +/* This function is called on leaving file functions to unlock the volume. +*/ + +void ff_rel_grant ( + FF_SYNC_t sobj /* Sync object to be signaled */ +) +{ + /* FreeRTOS */ + xSemaphoreGive(sobj); +} diff --git a/epicardium/fatfs.h b/epicardium/fatfs.h new file mode 100644 index 00000000..e69de29b diff --git a/epicardium/main.c b/epicardium/main.c index d6debe46..76090140 100644 --- a/epicardium/main.c +++ b/epicardium/main.c @@ -4,6 +4,7 @@ #include "max32665.h" #include "uart.h" #include "cdcacm.h" +#include "fatfs.h" #include "card10.h" #include "pmic.h" @@ -45,6 +46,7 @@ int main(void) SCB->SCR |= SCB_SCR_SEVONPEND_Msk; cdcacm_init(); + fatfs_init(); printf("=> Initializing tasks ...\n"); diff --git a/epicardium/meson.build b/epicardium/meson.build index 30510169..0e6d4988 100644 --- a/epicardium/meson.build +++ b/epicardium/meson.build @@ -67,10 +67,11 @@ subdir('modules/') elf = executable( name + '.elf', 'cdcacm.c', + 'fatfs.c', 'main.c', 'support.c', module_sources, - dependencies: [libcard10, max32665_startup_core0, maxusb], + dependencies: [libcard10, max32665_startup_core0, maxusb, libff13], link_with: [api_dispatcher_lib, freertos], link_whole: [max32665_startup_core0_lib, board_card10_lib], include_directories: [freertos_includes], diff --git a/lib/ff13/Source/ff.h b/lib/ff13/Source/ff.h index 7ee117b7..85beeb6d 100644 --- a/lib/ff13/Source/ff.h +++ b/lib/ff13/Source/ff.h @@ -311,10 +311,10 @@ void ff_memfree (void* mblock); /* Free memory block */ /* Sync functions */ #if FF_FS_REENTRANT -int ff_cre_syncobj (BYTE vol, FF_SYNC_t* sobj); /* Create a sync object */ -int ff_req_grant (FF_SYNC_t sobj); /* Lock sync object */ -void ff_rel_grant (FF_SYNC_t sobj); /* Unlock sync object */ -int ff_del_syncobj (FF_SYNC_t sobj); /* Delete a sync object */ +int ff_cre_syncobj (BYTE vol, FF_SYNC_t* sobj) __attribute__((weak)); /* Create a sync object */ +int ff_req_grant (FF_SYNC_t sobj) __attribute__((weak)); /* Lock sync object */ +void ff_rel_grant (FF_SYNC_t sobj) __attribute__((weak)); /* Unlock sync object */ +int ff_del_syncobj (FF_SYNC_t sobj) __attribute__((weak)); /* Delete a sync object */ #endif diff --git a/lib/ff13/Source/ffconf.h b/lib/ff13/Source/ffconf.h index 9c8a1dc1..3a14f3dc 100644 --- a/lib/ff13/Source/ffconf.h +++ b/lib/ff13/Source/ffconf.h @@ -248,9 +248,11 @@ / lock control is independent of re-entrancy. */ -#define FF_FS_REENTRANT 0 +#define FF_FS_REENTRANT 1 #define FF_FS_TIMEOUT 1000 -#define FF_SYNC_t HANDLE +//in FreeRTOS, SemaphoreHandle_t is a typedef for QueueHandle_t, which is just a typedef for: +#define FF_SYNC_t struct QueueDefinition * + /* The option FF_FS_REENTRANT switches the re-entrancy (thread safe) of the FatFs / module itself. Note that regardless of this option, file access to different / volume is always re-entrant and volume control functions, f_mount(), f_mkfs() diff --git a/lib/ff13/Source/ffsystem.c b/lib/ff13/Source/ffsystem.c index 3dc44579..7dfafc21 100644 --- a/lib/ff13/Source/ffsystem.c +++ b/lib/ff13/Source/ffsystem.c @@ -39,6 +39,10 @@ void ff_memfree ( #if FF_FS_REENTRANT /* Mutal exclusion */ +/* we're providing a dummy implementation here, these functions + * are to be overridden in the main application (see ffsupport.c) + */ + /*------------------------------------------------------------------------*/ /* Create a Synchronization Object */ /*------------------------------------------------------------------------*/ @@ -47,35 +51,16 @@ void ff_memfree ( / When a 0 is returned, the f_mount() function fails with FR_INT_ERR. */ -//const osMutexDef_t Mutex[FF_VOLUMES]; /* CMSIS-RTOS */ - +static int s_notAMutex; int ff_cre_syncobj ( /* 1:Function succeeded, 0:Could not create the sync object */ BYTE vol, /* Corresponding volume (logical drive number) */ FF_SYNC_t *sobj /* Pointer to return the created sync object */ ) { - /* Win32 */ - *sobj = CreateMutex(NULL, FALSE, NULL); - return (int)(*sobj != INVALID_HANDLE_VALUE); - - /* uITRON */ -// T_CSEM csem = {TA_TPRI,1,1}; -// *sobj = acre_sem(&csem); -// return (int)(*sobj > 0); - - /* uC/OS-II */ -// OS_ERR err; -// *sobj = OSMutexCreate(0, &err); -// return (int)(err == OS_NO_ERR); - - /* FreeRTOS */ -// *sobj = xSemaphoreCreateMutex(); -// return (int)(*sobj != NULL); - - /* CMSIS-RTOS */ -// *sobj = osMutexCreate(Mutex + vol); -// return (int)(*sobj != NULL); + (void) vol; + *sobj = (FF_SYNC_t)&s_notAMutex; + return 1; } @@ -91,23 +76,8 @@ int ff_del_syncobj ( /* 1:Function succeeded, 0:Could not delete due to an error FF_SYNC_t sobj /* Sync object tied to the logical drive to be deleted */ ) { - /* Win32 */ - return (int)CloseHandle(sobj); - - /* uITRON */ -// return (int)(del_sem(sobj) == E_OK); - - /* uC/OS-II */ -// OS_ERR err; -// OSMutexDel(sobj, OS_DEL_ALWAYS, &err); -// return (int)(err == OS_NO_ERR); - - /* FreeRTOS */ -// vSemaphoreDelete(sobj); -// return 1; - - /* CMSIS-RTOS */ -// return (int)(osMutexDelete(sobj) == osOK); + (void) sobj; + return 1; } @@ -122,22 +92,8 @@ int ff_req_grant ( /* 1:Got a grant to access the volume, 0:Could not get a gran FF_SYNC_t sobj /* Sync object to wait */ ) { - /* Win32 */ - return (int)(WaitForSingleObject(sobj, FF_FS_TIMEOUT) == WAIT_OBJECT_0); - - /* uITRON */ -// return (int)(wai_sem(sobj) == E_OK); - - /* uC/OS-II */ -// OS_ERR err; -// OSMutexPend(sobj, FF_FS_TIMEOUT, &err)); -// return (int)(err == OS_NO_ERR); - - /* FreeRTOS */ -// return (int)(xSemaphoreTake(sobj, FF_FS_TIMEOUT) == pdTRUE); - - /* CMSIS-RTOS */ -// return (int)(osMutexWait(sobj, FF_FS_TIMEOUT) == osOK); + (void) sobj; + return 1; } @@ -151,20 +107,7 @@ void ff_rel_grant ( FF_SYNC_t sobj /* Sync object to be signaled */ ) { - /* Win32 */ - ReleaseMutex(sobj); - - /* uITRON */ -// sig_sem(sobj); - - /* uC/OS-II */ -// OSMutexPost(sobj); - - /* FreeRTOS */ -// xSemaphoreGive(sobj); - - /* CMSIS-RTOS */ -// osMutexRelease(sobj); + (void) sobj; } #endif -- GitLab