]> cloudbase.mooo.com Git - z180-stamp.git/blob - fatfs/source/ffsystem.c
Import fatfs R0.15
[z180-stamp.git] / fatfs / source / ffsystem.c
1 /*------------------------------------------------------------------------*/
2 /* A Sample Code of User Provided OS Dependent Functions for FatFs */
3 /*------------------------------------------------------------------------*/
4
5 #include "ff.h"
6
7
8 #if FF_USE_LFN == 3 /* Use dynamic memory allocation */
9
10 /*------------------------------------------------------------------------*/
11 /* Allocate/Free a Memory Block */
12 /*------------------------------------------------------------------------*/
13
14 #include <stdlib.h> /* with POSIX API */
15
16
17 void* ff_memalloc ( /* Returns pointer to the allocated memory block (null if not enough core) */
18 UINT msize /* Number of bytes to allocate */
19 )
20 {
21 return malloc((size_t)msize); /* Allocate a new memory block */
22 }
23
24
25 void ff_memfree (
26 void* mblock /* Pointer to the memory block to free (no effect if null) */
27 )
28 {
29 free(mblock); /* Free the memory block */
30 }
31
32 #endif
33
34
35
36
37 #if FF_FS_REENTRANT /* Mutal exclusion */
38 /*------------------------------------------------------------------------*/
39 /* Definitions of Mutex */
40 /*------------------------------------------------------------------------*/
41
42 #define OS_TYPE 0 /* 0:Win32, 1:uITRON4.0, 2:uC/OS-II, 3:FreeRTOS, 4:CMSIS-RTOS */
43
44
45 #if OS_TYPE == 0 /* Win32 */
46 #include <windows.h>
47 static HANDLE Mutex[FF_VOLUMES + 1]; /* Table of mutex handle */
48
49 #elif OS_TYPE == 1 /* uITRON */
50 #include "itron.h"
51 #include "kernel.h"
52 static mtxid Mutex[FF_VOLUMES + 1]; /* Table of mutex ID */
53
54 #elif OS_TYPE == 2 /* uc/OS-II */
55 #include "includes.h"
56 static OS_EVENT *Mutex[FF_VOLUMES + 1]; /* Table of mutex pinter */
57
58 #elif OS_TYPE == 3 /* FreeRTOS */
59 #include "FreeRTOS.h"
60 #include "semphr.h"
61 static SemaphoreHandle_t Mutex[FF_VOLUMES + 1]; /* Table of mutex handle */
62
63 #elif OS_TYPE == 4 /* CMSIS-RTOS */
64 #include "cmsis_os.h"
65 static osMutexId Mutex[FF_VOLUMES + 1]; /* Table of mutex ID */
66
67 #endif
68
69
70
71 /*------------------------------------------------------------------------*/
72 /* Create a Mutex */
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.
77 */
78
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) */
81 )
82 {
83 #if OS_TYPE == 0 /* Win32 */
84 Mutex[vol] = CreateMutex(NULL, FALSE, NULL);
85 return (int)(Mutex[vol] != INVALID_HANDLE_VALUE);
86
87 #elif OS_TYPE == 1 /* uITRON */
88 T_CMTX cmtx = {TA_TPRI,1};
89
90 Mutex[vol] = acre_mtx(&cmtx);
91 return (int)(Mutex[vol] > 0);
92
93 #elif OS_TYPE == 2 /* uC/OS-II */
94 OS_ERR err;
95
96 Mutex[vol] = OSMutexCreate(0, &err);
97 return (int)(err == OS_NO_ERR);
98
99 #elif OS_TYPE == 3 /* FreeRTOS */
100 Mutex[vol] = xSemaphoreCreateMutex();
101 return (int)(Mutex[vol] != NULL);
102
103 #elif OS_TYPE == 4 /* CMSIS-RTOS */
104 osMutexDef(cmsis_os_mutex);
105
106 Mutex[vol] = osMutexCreate(osMutex(cmsis_os_mutex));
107 return (int)(Mutex[vol] != NULL);
108
109 #endif
110 }
111
112
113 /*------------------------------------------------------------------------*/
114 /* Delete a Mutex */
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.
118 */
119
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) */
122 )
123 {
124 #if OS_TYPE == 0 /* Win32 */
125 CloseHandle(Mutex[vol]);
126
127 #elif OS_TYPE == 1 /* uITRON */
128 del_mtx(Mutex[vol]);
129
130 #elif OS_TYPE == 2 /* uC/OS-II */
131 OS_ERR err;
132
133 OSMutexDel(Mutex[vol], OS_DEL_ALWAYS, &err);
134
135 #elif OS_TYPE == 3 /* FreeRTOS */
136 vSemaphoreDelete(Mutex[vol]);
137
138 #elif OS_TYPE == 4 /* CMSIS-RTOS */
139 osMutexDelete(Mutex[vol]);
140
141 #endif
142 }
143
144
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.
150 */
151
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) */
154 )
155 {
156 #if OS_TYPE == 0 /* Win32 */
157 return (int)(WaitForSingleObject(Mutex[vol], FF_FS_TIMEOUT) == WAIT_OBJECT_0);
158
159 #elif OS_TYPE == 1 /* uITRON */
160 return (int)(tloc_mtx(Mutex[vol], FF_FS_TIMEOUT) == E_OK);
161
162 #elif OS_TYPE == 2 /* uC/OS-II */
163 OS_ERR err;
164
165 OSMutexPend(Mutex[vol], FF_FS_TIMEOUT, &err));
166 return (int)(err == OS_NO_ERR);
167
168 #elif OS_TYPE == 3 /* FreeRTOS */
169 return (int)(xSemaphoreTake(Mutex[vol], FF_FS_TIMEOUT) == pdTRUE);
170
171 #elif OS_TYPE == 4 /* CMSIS-RTOS */
172 return (int)(osMutexWait(Mutex[vol], FF_FS_TIMEOUT) == osOK);
173
174 #endif
175 }
176
177
178
179 /*------------------------------------------------------------------------*/
180 /* Release a Grant to Access the Volume */
181 /*------------------------------------------------------------------------*/
182 /* This function is called on leave file functions to unlock the volume.
183 */
184
185 void ff_mutex_give (
186 int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */
187 )
188 {
189 #if OS_TYPE == 0 /* Win32 */
190 ReleaseMutex(Mutex[vol]);
191
192 #elif OS_TYPE == 1 /* uITRON */
193 unl_mtx(Mutex[vol]);
194
195 #elif OS_TYPE == 2 /* uC/OS-II */
196 OSMutexPost(Mutex[vol]);
197
198 #elif OS_TYPE == 3 /* FreeRTOS */
199 xSemaphoreGive(Mutex[vol]);
200
201 #elif OS_TYPE == 4 /* CMSIS-RTOS */
202 osMutexRelease(Mutex[vol]);
203
204 #endif
205 }
206
207 #endif /* FF_FS_REENTRANT */
208