]>
cloudbase.mooo.com Git - z180-stamp.git/blob - fatfs/source/ffsystem.c
1 /*------------------------------------------------------------------------*/
2 /* A Sample Code of User Provided OS Dependent Functions for FatFs */
3 /*------------------------------------------------------------------------*/
8 #if FF_USE_LFN == 3 /* Use dynamic memory allocation */
10 /*------------------------------------------------------------------------*/
11 /* Allocate/Free a Memory Block */
12 /*------------------------------------------------------------------------*/
14 #include <stdlib.h> /* with POSIX API */
17 void* ff_memalloc ( /* Returns pointer to the allocated memory block (null if not enough core) */
18 UINT msize
/* Number of bytes to allocate */
21 return malloc((size_t)msize
); /* Allocate a new memory block */
26 void* mblock
/* Pointer to the memory block to free (no effect if null) */
29 free(mblock
); /* Free the memory block */
37 #if FF_FS_REENTRANT /* Mutal exclusion */
38 /*------------------------------------------------------------------------*/
39 /* Definitions of Mutex */
40 /*------------------------------------------------------------------------*/
42 #define OS_TYPE 0 /* 0:Win32, 1:uITRON4.0, 2:uC/OS-II, 3:FreeRTOS, 4:CMSIS-RTOS */
45 #if OS_TYPE == 0 /* Win32 */
47 static HANDLE Mutex
[FF_VOLUMES
+ 1]; /* Table of mutex handle */
49 #elif OS_TYPE == 1 /* uITRON */
52 static mtxid Mutex
[FF_VOLUMES
+ 1]; /* Table of mutex ID */
54 #elif OS_TYPE == 2 /* uc/OS-II */
56 static OS_EVENT
*Mutex
[FF_VOLUMES
+ 1]; /* Table of mutex pinter */
58 #elif OS_TYPE == 3 /* FreeRTOS */
61 static SemaphoreHandle_t Mutex
[FF_VOLUMES
+ 1]; /* Table of mutex handle */
63 #elif OS_TYPE == 4 /* CMSIS-RTOS */
65 static osMutexId Mutex
[FF_VOLUMES
+ 1]; /* Table of mutex ID */
71 /*------------------------------------------------------------------------*/
73 /*------------------------------------------------------------------------*/
74 /* This function is called in f_mount function to create a new mutex
75 / or semaphore for the volume. When a 0 is returned, the f_mount function
76 / fails with FR_INT_ERR.
79 int ff_mutex_create ( /* Returns 1:Function succeeded or 0:Could not create the mutex */
80 int vol
/* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */
83 #if OS_TYPE == 0 /* Win32 */
84 Mutex
[vol
] = CreateMutex(NULL
, FALSE
, NULL
);
85 return (int)(Mutex
[vol
] != INVALID_HANDLE_VALUE
);
87 #elif OS_TYPE == 1 /* uITRON */
88 T_CMTX cmtx
= {TA_TPRI
,1};
90 Mutex
[vol
] = acre_mtx(&cmtx
);
91 return (int)(Mutex
[vol
] > 0);
93 #elif OS_TYPE == 2 /* uC/OS-II */
96 Mutex
[vol
] = OSMutexCreate(0, &err
);
97 return (int)(err
== OS_NO_ERR
);
99 #elif OS_TYPE == 3 /* FreeRTOS */
100 Mutex
[vol
] = xSemaphoreCreateMutex();
101 return (int)(Mutex
[vol
] != NULL
);
103 #elif OS_TYPE == 4 /* CMSIS-RTOS */
104 osMutexDef(cmsis_os_mutex
);
106 Mutex
[vol
] = osMutexCreate(osMutex(cmsis_os_mutex
));
107 return (int)(Mutex
[vol
] != NULL
);
113 /*------------------------------------------------------------------------*/
115 /*------------------------------------------------------------------------*/
116 /* This function is called in f_mount function to delete a mutex or
117 / semaphore of the volume created with ff_mutex_create function.
120 void ff_mutex_delete ( /* Returns 1:Function succeeded or 0:Could not delete due to an error */
121 int vol
/* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */
124 #if OS_TYPE == 0 /* Win32 */
125 CloseHandle(Mutex
[vol
]);
127 #elif OS_TYPE == 1 /* uITRON */
130 #elif OS_TYPE == 2 /* uC/OS-II */
133 OSMutexDel(Mutex
[vol
], OS_DEL_ALWAYS
, &err
);
135 #elif OS_TYPE == 3 /* FreeRTOS */
136 vSemaphoreDelete(Mutex
[vol
]);
138 #elif OS_TYPE == 4 /* CMSIS-RTOS */
139 osMutexDelete(Mutex
[vol
]);
145 /*------------------------------------------------------------------------*/
146 /* Request a Grant to Access the Volume */
147 /*------------------------------------------------------------------------*/
148 /* This function is called on enter file functions to lock the volume.
149 / When a 0 is returned, the file function fails with FR_TIMEOUT.
152 int ff_mutex_take ( /* Returns 1:Succeeded or 0:Timeout */
153 int vol
/* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */
156 #if OS_TYPE == 0 /* Win32 */
157 return (int)(WaitForSingleObject(Mutex
[vol
], FF_FS_TIMEOUT
) == WAIT_OBJECT_0
);
159 #elif OS_TYPE == 1 /* uITRON */
160 return (int)(tloc_mtx(Mutex
[vol
], FF_FS_TIMEOUT
) == E_OK
);
162 #elif OS_TYPE == 2 /* uC/OS-II */
165 OSMutexPend(Mutex
[vol
], FF_FS_TIMEOUT
, &err
));
166 return (int)(err
== OS_NO_ERR
);
168 #elif OS_TYPE == 3 /* FreeRTOS */
169 return (int)(xSemaphoreTake(Mutex
[vol
], FF_FS_TIMEOUT
) == pdTRUE
);
171 #elif OS_TYPE == 4 /* CMSIS-RTOS */
172 return (int)(osMutexWait(Mutex
[vol
], FF_FS_TIMEOUT
) == osOK
);
179 /*------------------------------------------------------------------------*/
180 /* Release a Grant to Access the Volume */
181 /*------------------------------------------------------------------------*/
182 /* This function is called on leave file functions to unlock the volume.
186 int vol
/* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */
189 #if OS_TYPE == 0 /* Win32 */
190 ReleaseMutex(Mutex
[vol
]);
192 #elif OS_TYPE == 1 /* uITRON */
195 #elif OS_TYPE == 2 /* uC/OS-II */
196 OSMutexPost(Mutex
[vol
]);
198 #elif OS_TYPE == 3 /* FreeRTOS */
199 xSemaphoreGive(Mutex
[vol
]);
201 #elif OS_TYPE == 4 /* CMSIS-RTOS */
202 osMutexRelease(Mutex
[vol
]);
207 #endif /* FF_FS_REENTRANT */