]> cloudbase.mooo.com Git - z180-stamp.git/blame_incremental - fatfs/source/ffsystem.c
Keep some ro data in flash using __memx address space.
[z180-stamp.git] / fatfs / source / ffsystem.c
... / ...
CommitLineData
1/*------------------------------------------------------------------------*/\r
2/* A Sample Code of User Provided OS Dependent Functions for FatFs */\r
3/*------------------------------------------------------------------------*/\r
4\r
5#include "ff.h"\r
6\r
7\r
8#if FF_USE_LFN == 3 /* Use dynamic memory allocation */\r
9\r
10/*------------------------------------------------------------------------*/\r
11/* Allocate/Free a Memory Block */\r
12/*------------------------------------------------------------------------*/\r
13\r
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
18 UINT msize /* Number of bytes to allocate */\r
19)\r
20{\r
21 return malloc((size_t)msize); /* Allocate a new memory block */\r
22}\r
23\r
24\r
25void ff_memfree (\r
26 void* mblock /* Pointer to the memory block to free (no effect if null) */\r
27)\r
28{\r
29 free(mblock); /* Free the memory block */\r
30}\r
31\r
32#endif\r
33\r
34\r
35\r
36\r
37#if FF_FS_REENTRANT /* Mutal exclusion */\r
38/*------------------------------------------------------------------------*/\r
39/* Definitions of Mutex */\r
40/*------------------------------------------------------------------------*/\r
41\r
42#define OS_TYPE 0 /* 0:Win32, 1:uITRON4.0, 2:uC/OS-II, 3:FreeRTOS, 4:CMSIS-RTOS */\r
43\r
44\r
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
78\r
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
81)\r
82{\r
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
110}\r
111\r
112\r
113/*------------------------------------------------------------------------*/\r
114/* Delete a Mutex */\r
115/*------------------------------------------------------------------------*/\r
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
118*/\r
119\r
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
122)\r
123{\r
124#if OS_TYPE == 0 /* Win32 */\r
125 CloseHandle(Mutex[vol]);\r
126\r
127#elif OS_TYPE == 1 /* uITRON */\r
128 del_mtx(Mutex[vol]);\r
129\r
130#elif OS_TYPE == 2 /* uC/OS-II */\r
131 OS_ERR err;\r
132\r
133 OSMutexDel(Mutex[vol], OS_DEL_ALWAYS, &err);\r
134\r
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
142}\r
143\r
144\r
145/*------------------------------------------------------------------------*/\r
146/* Request a Grant to Access the Volume */\r
147/*------------------------------------------------------------------------*/\r
148/* This function is called on enter file functions to lock the volume.\r
149/ When a 0 is returned, the file function fails with FR_TIMEOUT.\r
150*/\r
151\r
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
154)\r
155{\r
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
164\r
165 OSMutexPend(Mutex[vol], FF_FS_TIMEOUT, &err));\r
166 return (int)(err == OS_NO_ERR);\r
167\r
168#elif OS_TYPE == 3 /* FreeRTOS */\r
169 return (int)(xSemaphoreTake(Mutex[vol], FF_FS_TIMEOUT) == pdTRUE);\r
170\r
171#elif OS_TYPE == 4 /* CMSIS-RTOS */\r
172 return (int)(osMutexWait(Mutex[vol], FF_FS_TIMEOUT) == osOK);\r
173\r
174#endif\r
175}\r
176\r
177\r
178\r
179/*------------------------------------------------------------------------*/\r
180/* Release a Grant to Access the Volume */\r
181/*------------------------------------------------------------------------*/\r
182/* This function is called on leave file functions to unlock the volume.\r
183*/\r
184\r
185void ff_mutex_give (\r
186 int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */\r
187)\r
188{\r
189#if OS_TYPE == 0 /* Win32 */\r
190 ReleaseMutex(Mutex[vol]);\r
191\r
192#elif OS_TYPE == 1 /* uITRON */\r
193 unl_mtx(Mutex[vol]);\r
194\r
195#elif OS_TYPE == 2 /* uC/OS-II */\r
196 OSMutexPost(Mutex[vol]);\r
197\r
198#elif OS_TYPE == 3 /* FreeRTOS */\r
199 xSemaphoreGive(Mutex[vol]);\r
200\r
201#elif OS_TYPE == 4 /* CMSIS-RTOS */\r
202 osMutexRelease(Mutex[vol]);\r
203\r
204#endif\r
205}\r
206\r
207#endif /* FF_FS_REENTRANT */\r
208\r