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