1
1
/*------------------------------------------------------------------------*/
2
- /* Sample Code of OS Dependent Functions for FatFs */
3
- /* (C)ChaN, 2017 */
2
+ /* A Sample Code of User Provided OS Dependent Functions for FatFs */
4
3
/*------------------------------------------------------------------------*/
5
4
6
5
12
11
#include "esp_heap_caps.h"
13
12
#endif
14
13
15
- void * ff_memalloc ( /* Returns pointer to the allocated memory block (null on not enough core) */
14
+ /*------------------------------------------------------------------------*/
15
+ /* Allocate/Free a Memory Block */
16
+ /*------------------------------------------------------------------------*/
17
+
18
+ void * ff_memalloc ( /* Returns pointer to the allocated memory block (null if not enough core) */
16
19
unsigned msize /* Number of bytes to allocate */
17
20
)
18
21
{
@@ -25,12 +28,8 @@ void* ff_memalloc ( /* Returns pointer to the allocated memory block (null on
25
28
}
26
29
27
30
28
- /*------------------------------------------------------------------------*/
29
- /* Free a memory block */
30
- /*------------------------------------------------------------------------*/
31
-
32
31
void ff_memfree (
33
- void * mblock /* Pointer to the memory block to free (nothing to do for null) */
32
+ void * mblock /* Pointer to the memory block to free (no effect if null) */
34
33
)
35
34
{
36
35
free (mblock ); /* Free the memory block with POSIX API */
@@ -39,70 +38,175 @@ void ff_memfree (
39
38
40
39
41
40
42
- #if FF_FS_REENTRANT /* Mutal exclusion */
43
41
42
+ #if FF_FS_REENTRANT /* Mutal exclusion */
44
43
/*------------------------------------------------------------------------*/
45
- /* Create a Synchronization Object */
44
+ /* Definitions of Mutex */
46
45
/*------------------------------------------------------------------------*/
47
- /* This function is called in f_mount() function to create a new
48
- / synchronization object for the volume, such as semaphore and mutex.
49
- / When a 0 is returned, the f_mount() function fails with FR_INT_ERR.
50
- */
51
46
47
+ #define OS_TYPE 3 /* 0:Win32, 1:uITRON4.0, 2:uC/OS-II, 3:FreeRTOS, 4:CMSIS-RTOS */
48
+
49
+
50
+ #if OS_TYPE == 0 /* Win32 */
51
+ #include <windows.h>
52
+ static HANDLE Mutex [FF_VOLUMES + 1 ]; /* Table of mutex handle */
53
+
54
+ #elif OS_TYPE == 1 /* uITRON */
55
+ #include "itron.h"
56
+ #include "kernel.h"
57
+ static mtxid Mutex [FF_VOLUMES + 1 ]; /* Table of mutex ID */
58
+
59
+ #elif OS_TYPE == 2 /* uc/OS-II */
60
+ #include "includes.h"
61
+ static OS_EVENT * Mutex [FF_VOLUMES + 1 ]; /* Table of mutex pinter */
62
+
63
+ #elif OS_TYPE == 3 /* FreeRTOS */
64
+ #include "freertos/FreeRTOS.h"
65
+ #include "freertos/semphr.h"
66
+ static SemaphoreHandle_t Mutex [FF_VOLUMES + 1 ]; /* Table of mutex handle */
67
+
68
+ #elif OS_TYPE == 4 /* CMSIS-RTOS */
69
+ #include "cmsis_os.h"
70
+ static osMutexId Mutex [FF_VOLUMES + 1 ]; /* Table of mutex ID */
52
71
53
- int ff_cre_syncobj ( /* 1:Function succeeded, 0:Could not create the sync object */
54
- BYTE vol , /* Corresponding volume (logical drive number) */
55
- FF_SYNC_t * sobj /* Pointer to return the created sync object */
72
+ #endif
73
+
74
+
75
+
76
+ /*------------------------------------------------------------------------*/
77
+ /* Create a Mutex */
78
+ /*------------------------------------------------------------------------*/
79
+ /* This function is called in f_mount function to create a new mutex
80
+ / or semaphore for the volume. When a 0 is returned, the f_mount function
81
+ / fails with FR_INT_ERR.
82
+ */
83
+
84
+ int ff_mutex_create ( /* Returns 1:Function succeeded or 0:Could not create the mutex */
85
+ int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */
56
86
)
57
87
{
58
- * sobj = xSemaphoreCreateMutex ();
59
- return (* sobj != NULL ) ? 1 : 0 ;
88
+ #if OS_TYPE == 0 /* Win32 */
89
+ Mutex [vol ] = CreateMutex (NULL , FALSE, NULL );
90
+ return (int )(Mutex [vol ] != INVALID_HANDLE_VALUE );
91
+
92
+ #elif OS_TYPE == 1 /* uITRON */
93
+ T_CMTX cmtx = {TA_TPRI ,1 };
94
+
95
+ Mutex [vol ] = acre_mtx (& cmtx );
96
+ return (int )(Mutex [vol ] > 0 );
97
+
98
+ #elif OS_TYPE == 2 /* uC/OS-II */
99
+ OS_ERR err ;
100
+
101
+ Mutex [vol ] = OSMutexCreate (0 , & err );
102
+ return (int )(err == OS_NO_ERR );
103
+
104
+ #elif OS_TYPE == 3 /* FreeRTOS */
105
+ Mutex [vol ] = xSemaphoreCreateMutex ();
106
+ return (int )(Mutex [vol ] != NULL );
107
+
108
+ #elif OS_TYPE == 4 /* CMSIS-RTOS */
109
+ osMutexDef (cmsis_os_mutex );
110
+
111
+ Mutex [vol ] = osMutexCreate (osMutex (cmsis_os_mutex ));
112
+ return (int )(Mutex [vol ] != NULL );
113
+
114
+ #endif
60
115
}
61
116
62
117
63
118
/*------------------------------------------------------------------------*/
64
- /* Delete a Synchronization Object */
119
+ /* Delete a Mutex */
65
120
/*------------------------------------------------------------------------*/
66
- /* This function is called in f_mount() function to delete a synchronization
67
- / object that created with ff_cre_syncobj() function. When a 0 is returned,
68
- / the f_mount() function fails with FR_INT_ERR.
121
+ /* This function is called in f_mount function to delete a mutex or
122
+ / semaphore of the volume created with ff_mutex_create function.
69
123
*/
70
124
71
- int ff_del_syncobj ( /* 1:Function succeeded, 0:Could not delete due to an error */
72
- FF_SYNC_t sobj /* Sync object tied to the logical drive to be deleted */
125
+ void ff_mutex_delete ( /* Returns 1:Function succeeded or 0:Could not delete due to an error */
126
+ int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */
73
127
)
74
128
{
75
- vSemaphoreDelete (sobj );
76
- return 1 ;
129
+ #if OS_TYPE == 0 /* Win32 */
130
+ CloseHandle (Mutex [vol ]);
131
+
132
+ #elif OS_TYPE == 1 /* uITRON */
133
+ del_mtx (Mutex [vol ]);
134
+
135
+ #elif OS_TYPE == 2 /* uC/OS-II */
136
+ OS_ERR err ;
137
+
138
+ OSMutexDel (Mutex [vol ], OS_DEL_ALWAYS , & err );
139
+
140
+ #elif OS_TYPE == 3 /* FreeRTOS */
141
+ vSemaphoreDelete (Mutex [vol ]);
142
+
143
+ #elif OS_TYPE == 4 /* CMSIS-RTOS */
144
+ osMutexDelete (Mutex [vol ]);
145
+
146
+ #endif
77
147
}
78
148
79
149
80
150
/*------------------------------------------------------------------------*/
81
- /* Request Grant to Access the Volume */
151
+ /* Request a Grant to Access the Volume */
82
152
/*------------------------------------------------------------------------*/
83
- /* This function is called on entering file functions to lock the volume.
153
+ /* This function is called on enter file functions to lock the volume.
84
154
/ When a 0 is returned, the file function fails with FR_TIMEOUT.
85
155
*/
86
156
87
- int ff_req_grant ( /* 1:Got a grant to access the volume, 0:Could not get a grant */
88
- FF_SYNC_t sobj /* Sync object to wait */
157
+ int ff_mutex_take ( /* Returns 1:Succeeded or 0:Timeout */
158
+ int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */
89
159
)
90
160
{
91
- return (xSemaphoreTake (sobj , FF_FS_TIMEOUT ) == pdTRUE ) ? 1 : 0 ;
161
+ #if OS_TYPE == 0 /* Win32 */
162
+ return (int )(WaitForSingleObject (Mutex [vol ], FF_FS_TIMEOUT ) == WAIT_OBJECT_0 );
163
+
164
+ #elif OS_TYPE == 1 /* uITRON */
165
+ return (int )(tloc_mtx (Mutex [vol ], FF_FS_TIMEOUT ) == E_OK );
166
+
167
+ #elif OS_TYPE == 2 /* uC/OS-II */
168
+ OS_ERR err ;
169
+
170
+ OSMutexPend (Mutex [vol ], FF_FS_TIMEOUT , & err ));
171
+ return (int )(err == OS_NO_ERR );
172
+
173
+ #elif OS_TYPE == 3 /* FreeRTOS */
174
+ return (int )(xSemaphoreTake (Mutex [vol ], FF_FS_TIMEOUT ) == pdTRUE );
175
+
176
+ #elif OS_TYPE == 4 /* CMSIS-RTOS */
177
+ return (int )(osMutexWait (Mutex [vol ], FF_FS_TIMEOUT ) == osOK );
178
+
179
+ #endif
92
180
}
93
181
94
182
183
+
95
184
/*------------------------------------------------------------------------*/
96
- /* Release Grant to Access the Volume */
185
+ /* Release a Grant to Access the Volume */
97
186
/*------------------------------------------------------------------------*/
98
- /* This function is called on leaving file functions to unlock the volume.
187
+ /* This function is called on leave file functions to unlock the volume.
99
188
*/
100
189
101
- void ff_rel_grant (
102
- FF_SYNC_t sobj /* Sync object to be signaled */
190
+ void ff_mutex_give (
191
+ int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */
103
192
)
104
193
{
105
- xSemaphoreGive (sobj );
194
+ #if OS_TYPE == 0 /* Win32 */
195
+ ReleaseMutex (Mutex [vol ]);
196
+
197
+ #elif OS_TYPE == 1 /* uITRON */
198
+ unl_mtx (Mutex [vol ]);
199
+
200
+ #elif OS_TYPE == 2 /* uC/OS-II */
201
+ OSMutexPost (Mutex [vol ]);
202
+
203
+ #elif OS_TYPE == 3 /* FreeRTOS */
204
+ xSemaphoreGive (Mutex [vol ]);
205
+
206
+ #elif OS_TYPE == 4 /* CMSIS-RTOS */
207
+ osMutexRelease (Mutex [vol ]);
208
+
209
+ #endif
106
210
}
107
211
108
- #endif // FF_FS_REENTRANT
212
+ #endif /* FF_FS_REENTRANT */
0 commit comments