sdk-core v2026.11.05
PS2 SDK - core tier (build-critical EE/IOP modules)
Loading...
Searching...
No Matches
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
17void* 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
25void 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 5 /* 0:Win32, 1:uITRON4.0, 2:uC/OS-II, 3:FreeRTOS, 4:CMSIS-RTOS 5:IOP-PS2*/
43
44
45#if OS_TYPE == 0 /* Win32 */
46#include <windows.h>
47static HANDLE Mutex[FF_VOLUMES + 1]; /* Table of mutex handle */
48
49#elif OS_TYPE == 1 /* uITRON */
50#include "itron.h"
51#include "kernel.h"
52static mtxid Mutex[FF_VOLUMES + 1]; /* Table of mutex ID */
53
54#elif OS_TYPE == 2 /* uc/OS-II */
55#include "includes.h"
56static 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"
61static SemaphoreHandle_t Mutex[FF_VOLUMES + 1]; /* Table of mutex handle */
62
63#elif OS_TYPE == 4 /* CMSIS-RTOS */
64#include "cmsis_os.h"
65static osMutexId Mutex[FF_VOLUMES + 1]; /* Table of mutex ID */
66
67#elif OS_TYPE == 5 /* IOP-PS2 */
68#include <thsemap.h>
69static int Mutex[FF_VOLUMES + 1]; /* Table of mutex ID */
70
71#endif
72
73
74
75/*------------------------------------------------------------------------*/
76/* Create a Mutex */
77/*------------------------------------------------------------------------*/
78/* This function is called in f_mount function to create a new mutex
79/ or semaphore for the volume. When a 0 is returned, the f_mount function
80/ fails with FR_INT_ERR.
81*/
82
83int ff_mutex_create ( /* Returns 1:Function succeeded or 0:Could not create the mutex */
84 int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */
85)
86{
87#if OS_TYPE == 0 /* Win32 */
88 Mutex[vol] = CreateMutex(NULL, FALSE, NULL);
89 return (int)(Mutex[vol] != INVALID_HANDLE_VALUE);
90
91#elif OS_TYPE == 1 /* uITRON */
92 T_CMTX cmtx = {TA_TPRI,1};
93
94 Mutex[vol] = acre_mtx(&cmtx);
95 return (int)(Mutex[vol] > 0);
96
97#elif OS_TYPE == 2 /* uC/OS-II */
98 OS_ERR err;
99
100 Mutex[vol] = OSMutexCreate(0, &err);
101 return (int)(err == OS_NO_ERR);
102
103#elif OS_TYPE == 3 /* FreeRTOS */
104 Mutex[vol] = xSemaphoreCreateMutex();
105 return (int)(Mutex[vol] != NULL);
106
107#elif OS_TYPE == 4 /* CMSIS-RTOS */
108 osMutexDef(cmsis_os_mutex);
109
110 Mutex[vol] = osMutexCreate(osMutex(cmsis_os_mutex));
111 return (int)(Mutex[vol] != NULL);
112
113#elif OS_TYPE == 5 /* IOP-PS2 */
114 iop_sema_t sp;
115
116 sp.initial = 1;
117 sp.max = 1;
118 sp.option = 0;
119 sp.attr = 0;
120 Mutex[vol] = CreateSema(&sp);
121 return (int)(Mutex[vol] >= 0);
122#endif
123}
124
125
126/*------------------------------------------------------------------------*/
127/* Delete a Mutex */
128/*------------------------------------------------------------------------*/
129/* This function is called in f_mount function to delete a mutex or
130/ semaphore of the volume created with ff_mutex_create function.
131*/
132
133void ff_mutex_delete ( /* Returns 1:Function succeeded or 0:Could not delete due to an error */
134 int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */
135)
136{
137#if OS_TYPE == 0 /* Win32 */
138 CloseHandle(Mutex[vol]);
139
140#elif OS_TYPE == 1 /* uITRON */
141 del_mtx(Mutex[vol]);
142
143#elif OS_TYPE == 2 /* uC/OS-II */
144 OS_ERR err;
145
146 OSMutexDel(Mutex[vol], OS_DEL_ALWAYS, &err);
147
148#elif OS_TYPE == 3 /* FreeRTOS */
149 vSemaphoreDelete(Mutex[vol]);
150
151#elif OS_TYPE == 4 /* CMSIS-RTOS */
152 osMutexDelete(Mutex[vol]);
153
154#elif OS_TYPE == 5 /* IOP-PS2 */
155 DeleteSema(Mutex[vol]);
156
157#endif
158}
159
160
161/*------------------------------------------------------------------------*/
162/* Request a Grant to Access the Volume */
163/*------------------------------------------------------------------------*/
164/* This function is called on enter file functions to lock the volume.
165/ When a 0 is returned, the file function fails with FR_TIMEOUT.
166*/
167
168int ff_mutex_take ( /* Returns 1:Succeeded or 0:Timeout */
169 int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */
170)
171{
172#if OS_TYPE == 0 /* Win32 */
173 return (int)(WaitForSingleObject(Mutex[vol], FF_FS_TIMEOUT) == WAIT_OBJECT_0);
174
175#elif OS_TYPE == 1 /* uITRON */
176 return (int)(tloc_mtx(Mutex[vol], FF_FS_TIMEOUT) == E_OK);
177
178#elif OS_TYPE == 2 /* uC/OS-II */
179 OS_ERR err;
180
181 OSMutexPend(Mutex[vol], FF_FS_TIMEOUT, &err));
182 return (int)(err == OS_NO_ERR);
183
184#elif OS_TYPE == 3 /* FreeRTOS */
185 return (int)(xSemaphoreTake(Mutex[vol], FF_FS_TIMEOUT) == pdTRUE);
186
187#elif OS_TYPE == 4 /* CMSIS-RTOS */
188 return (int)(osMutexWait(Mutex[vol], FF_FS_TIMEOUT) == osOK);
189
190#elif OS_TYPE == 5 /* IOP-PS2 */
191 return (int)(WaitSema(Mutex[vol]) == 0);
192
193#endif
194}
195
196
197
198/*------------------------------------------------------------------------*/
199/* Release a Grant to Access the Volume */
200/*------------------------------------------------------------------------*/
201/* This function is called on leave file functions to unlock the volume.
202*/
203
204void ff_mutex_give (
205 int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */
206)
207{
208#if OS_TYPE == 0 /* Win32 */
209 ReleaseMutex(Mutex[vol]);
210
211#elif OS_TYPE == 1 /* uITRON */
212 unl_mtx(Mutex[vol]);
213
214#elif OS_TYPE == 2 /* uC/OS-II */
215 OSMutexPost(Mutex[vol]);
216
217#elif OS_TYPE == 3 /* FreeRTOS */
218 xSemaphoreGive(Mutex[vol]);
219
220#elif OS_TYPE == 4 /* CMSIS-RTOS */
221 osMutexRelease(Mutex[vol]);
222
223#elif OS_TYPE == 5 /* IOP-PS2 */
224 SignalSema(Mutex[vol]);
225
226#endif
227}
228
229#endif /* FF_FS_REENTRANT */
230