Skip to content
Snippets Groups Projects
Commit d028fa6b authored by swym's avatar swym
Browse files

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
parent e2335c58
No related branches found
No related tags found
No related merge requests found
......@@ -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;
......
/*
* 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);
}
......@@ -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");
......
......@@ -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],
......
......@@ -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
......
......@@ -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()
......
......@@ -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
......
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