00001 /*------------------------------------------------------------------------*/ 00002 /* Sample code of OS dependent controls for FatFs R0.08 */ 00003 /* (C)ChaN, 2010 */ 00004 /*------------------------------------------------------------------------*/ 00005 00006 #include <stdlib.h> /* ANSI memory controls */ 00007 #include <malloc.h> /* ANSI memory controls */ 00008 00009 #include "../ff.h" 00010 00011 00012 #if _FS_REENTRANT 00013 /*------------------------------------------------------------------------*/ 00014 /* Create a Synchronization Object 00015 /*------------------------------------------------------------------------*/ 00016 /* This function is called in f_mount function to create a new 00017 / synchronization object, such as semaphore and mutex. When a FALSE is 00018 / returned, the f_mount function fails with FR_INT_ERR. 00019 */ 00020 00021 BOOL ff_cre_syncobj ( /* TRUE:Function succeeded, FALSE:Could not create due to any error */ 00022 BYTE vol, /* Corresponding logical drive being processed */ 00023 _SYNC_t *sobj /* Pointer to return the created sync object */ 00024 ) 00025 { 00026 BOOL ret; 00027 00028 *sobj = CreateMutex(NULL, FALSE, NULL); /* Win32 */ 00029 ret = (*sobj != INVALID_HANDLE_VALUE) ? TRUE : FALSE; 00030 00031 // *sobj = SyncObjects[vol]; /* uITRON (give a static created sync object) */ 00032 // ret = TRUE; /* The initial value of the semaphore must be 1. */ 00033 00034 // *sobj = OSMutexCreate(0, &err); /* uC/OS-II */ 00035 // ret = (err == OS_NO_ERR) ? TRUE : FALSE; 00036 00037 // *sobj = xSemaphoreCreateMutex(); /* FreeRTOS */ 00038 // ret = (*sobj != NULL) ? TRUE : FALSE; 00039 00040 return ret; 00041 } 00042 00043 00044 00045 /*------------------------------------------------------------------------*/ 00046 /* Delete a Synchronization Object */ 00047 /*------------------------------------------------------------------------*/ 00048 /* This function is called in f_mount function to delete a synchronization 00049 / object that created with ff_cre_syncobj function. When a FALSE is 00050 / returned, the f_mount function fails with FR_INT_ERR. 00051 */ 00052 00053 BOOL ff_del_syncobj ( /* TRUE:Function succeeded, FALSE:Could not delete due to any error */ 00054 _SYNC_t sobj /* Sync object tied to the logical drive to be deleted */ 00055 ) 00056 { 00057 BOOL ret; 00058 00059 ret = CloseHandle(sobj); /* Win32 * 00060 00061 // ret = TRUE; /* uITRON (nothing to do) * 00062 00063 // OSMutexDel(sobj, OS_DEL_ALWAYS, &err); /* uC/OS-II */ 00064 // ret = (err == OS_NO_ERR) ? TRUE : FALSE; 00065 00066 // ret = TRUE; /* FreeRTOS (nothing to do) */ 00067 00068 return ret; 00069 } 00070 00071 00072 00073 /*------------------------------------------------------------------------*/ 00074 /* Request Grant to Access the Volume */ 00075 /*------------------------------------------------------------------------*/ 00076 /* This function is called on entering file functions to lock the volume. 00077 / When a FALSE is returned, the file function fails with FR_TIMEOUT. 00078 */ 00079 00080 BOOL ff_req_grant ( /* TRUE:Got a grant to access the volume, FALSE:Could not get a grant */ 00081 _SYNC_t sobj /* Sync object to wait */ 00082 ) 00083 { 00084 BOOL ret; 00085 00086 ret = (WaitForSingleObject(sobj, _FS_TIMEOUT) == WAIT_OBJECT_0) ? TRUE : FALSE; /* Win32 */ 00087 00088 // ret = (wai_sem(sobj) == E_OK) ? TRUE : FALSE; /* uITRON */ 00089 00090 // OSMutexPend(sobj, _FS_TIMEOUT, &err)); /* uC/OS-II */ 00091 // ret = (err == OS_NO_ERR) ? TRUE : FALSE; 00092 00093 // ret = (xSemaphoreTake(sobj, _FS_TIMEOUT) == pdTRUE) ? TRUE : FALSE; /* FreeRTOS */ 00094 00095 return ret; 00096 } 00097 00098 00099 00100 /*------------------------------------------------------------------------*/ 00101 /* Release Grant to Access the Volume */ 00102 /*------------------------------------------------------------------------*/ 00103 /* This function is called on leaving file functions to unlock the volume. 00104 */ 00105 00106 void ff_rel_grant ( 00107 _SYNC_t sobj /* Sync object to be signalled */ 00108 ) 00109 { 00110 ReleaseMutex(sobj); /* Win32 */ 00111 00112 // sig_sem(sobj); /* uITRON */ 00113 00114 // OSMutexPost(sobj); /* uC/OS-II */ 00115 00116 // xSemaphoreGive(sobj); /* FreeRTOS */ 00117 00118 } 00119 00120 #endif 00121 00122 00123 00124 00125 #if _USE_LFN == 3 /* LFN with a working buffer on the heap */ 00126 /*------------------------------------------------------------------------*/ 00127 /* Allocate a memory block */ 00128 /*------------------------------------------------------------------------*/ 00129 /* If a NULL is returned, the file function fails with FR_NOT_ENOUGH_CORE. 00130 */ 00131 00132 void* ff_memalloc ( /* Returns pointer to the allocated memory block */ 00133 UINT size /* Number of bytes to allocate */ 00134 ) 00135 { 00136 return malloc(size); 00137 } 00138 00139 00140 /*------------------------------------------------------------------------*/ 00141 /* Free a memory block */ 00142 /*------------------------------------------------------------------------*/ 00143 00144 void ff_memfree( 00145 void* mblock /* Pointer to the memory block to free */ 00146 ) 00147 { 00148 free(mblock); 00149 } 00150 00151 #endif