diff --git a/epicardium/cdcacm.c b/epicardium/cdcacm.c index fd0f2d8614ba034458b987072298870288227089..83481dd61322d1d70589cb9deef6633b2237a807 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 0000000000000000000000000000000000000000..5513f68b3ed712f5c355facf8153ba6ebb814f54 --- /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 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/epicardium/main.c b/epicardium/main.c index d6debe46dbaadaf87db23fc7efe64c66ed8a3ce2..7609014000a1fd7b70d668ad9d19c9ff243c8e48 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 30510169f4e0a9f994ad860443439629e23f1c73..0e6d4988f7971f3367d66f9f5eddb653cfefe969 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 7ee117b784a3a1f5e2d923a2b0a807011f7dd57a..85beeb6defdcab82803558cdbdeee9f38ebbb122 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 9c8a1dc16160a115c545810d90dcab57cc5b4523..3a14f3dcb3cfcda8c403bda34cb8ddd8ba321148 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 3dc445795c82376c7cc0c18f6d615cd6470f45f4..7dfafc2196f3b0ed88b4b9ed77c1588d73e7d5d9 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