1 /*------------------------------------------------------------------------*/
2 /* Sample Code of OS Dependent Functions for FatFs */
3 /* (C)ChaN, 2018 */
4 /*------------------------------------------------------------------------*/
5
6
7 #include "ff.h"
8
9
10 #if FF_USE_LFN == 3 /* Dynamic memory allocation */
11
12 /*------------------------------------------------------------------------*/
13 /* Allocate a memory block */
14 /*------------------------------------------------------------------------*/
15
ff_memalloc(UINT msize)16 void* ff_memalloc ( /* Returns pointer to the allocated memory block (null if not enough core) */
17 UINT msize /* Number of bytes to allocate */
18 )
19 {
20 return malloc(msize); /* Allocate a new memory block with POSIX API */
21 }
22
23
24 /*------------------------------------------------------------------------*/
25 /* Free a memory block */
26 /*------------------------------------------------------------------------*/
27
ff_memfree(void * mblock)28 void ff_memfree (
29 void* mblock /* Pointer to the memory block to free (nothing to do if null) */
30 )
31 {
32 free(mblock); /* Free the memory block with POSIX API */
33 }
34
35 #endif
36
37
38
39 #if FF_FS_REENTRANT /* Mutal exclusion */
40
41 /*------------------------------------------------------------------------*/
42 /* Create a Synchronization Object */
43 /*------------------------------------------------------------------------*/
44 /* This function is called in f_mount() function to create a new
45 / synchronization object for the volume, such as semaphore and mutex.
46 / When a 0 is returned, the f_mount() function fails with FR_INT_ERR.
47 */
48
49 //const osMutexDef_t Mutex[FF_VOLUMES]; /* Table of CMSIS-RTOS mutex */
50
51
ff_cre_syncobj(BYTE vol,FF_SYNC_t * sobj)52 int ff_cre_syncobj ( /* 1:Function succeeded, 0:Could not create the sync object */
53 BYTE vol, /* Corresponding volume (logical drive number) */
54 FF_SYNC_t* sobj /* Pointer to return the created sync object */
55 )
56 {
57 /* Win32 */
58 *sobj = CreateMutex(NULL, FALSE, NULL);
59 return (int)(*sobj != INVALID_HANDLE_VALUE);
60
61 /* uITRON */
62 // T_CSEM csem = {TA_TPRI,1,1};
63 // *sobj = acre_sem(&csem);
64 // return (int)(*sobj > 0);
65
66 /* uC/OS-II */
67 // OS_ERR err;
68 // *sobj = OSMutexCreate(0, &err);
69 // return (int)(err == OS_NO_ERR);
70
71 /* FreeRTOS */
72 // *sobj = xSemaphoreCreateMutex();
73 // return (int)(*sobj != NULL);
74
75 /* CMSIS-RTOS */
76 // *sobj = osMutexCreate(&Mutex[vol]);
77 // return (int)(*sobj != NULL);
78 }
79
80
81 /*------------------------------------------------------------------------*/
82 /* Delete a Synchronization Object */
83 /*------------------------------------------------------------------------*/
84 /* This function is called in f_mount() function to delete a synchronization
85 / object that created with ff_cre_syncobj() function. When a 0 is returned,
86 / the f_mount() function fails with FR_INT_ERR.
87 */
88
ff_del_syncobj(FF_SYNC_t sobj)89 int ff_del_syncobj ( /* 1:Function succeeded, 0:Could not delete due to an error */
90 FF_SYNC_t sobj /* Sync object tied to the logical drive to be deleted */
91 )
92 {
93 /* Win32 */
94 return (int)CloseHandle(sobj);
95
96 /* uITRON */
97 // return (int)(del_sem(sobj) == E_OK);
98
99 /* uC/OS-II */
100 // OS_ERR err;
101 // OSMutexDel(sobj, OS_DEL_ALWAYS, &err);
102 // return (int)(err == OS_NO_ERR);
103
104 /* FreeRTOS */
105 // vSemaphoreDelete(sobj);
106 // return 1;
107
108 /* CMSIS-RTOS */
109 // return (int)(osMutexDelete(sobj) == osOK);
110 }
111
112
113 /*------------------------------------------------------------------------*/
114 /* Request Grant to Access the Volume */
115 /*------------------------------------------------------------------------*/
116 /* This function is called on entering file functions to lock the volume.
117 / When a 0 is returned, the file function fails with FR_TIMEOUT.
118 */
119
ff_req_grant(FF_SYNC_t sobj)120 int ff_req_grant ( /* 1:Got a grant to access the volume, 0:Could not get a grant */
121 FF_SYNC_t sobj /* Sync object to wait */
122 )
123 {
124 /* Win32 */
125 return (int)(WaitForSingleObject(sobj, FF_FS_TIMEOUT) == WAIT_OBJECT_0);
126
127 /* uITRON */
128 // return (int)(wai_sem(sobj) == E_OK);
129
130 /* uC/OS-II */
131 // OS_ERR err;
132 // OSMutexPend(sobj, FF_FS_TIMEOUT, &err));
133 // return (int)(err == OS_NO_ERR);
134
135 /* FreeRTOS */
136 // return (int)(xSemaphoreTake(sobj, FF_FS_TIMEOUT) == pdTRUE);
137
138 /* CMSIS-RTOS */
139 // return (int)(osMutexWait(sobj, FF_FS_TIMEOUT) == osOK);
140 }
141
142
143 /*------------------------------------------------------------------------*/
144 /* Release Grant to Access the Volume */
145 /*------------------------------------------------------------------------*/
146 /* This function is called on leaving file functions to unlock the volume.
147 */
148
ff_rel_grant(FF_SYNC_t sobj)149 void ff_rel_grant (
150 FF_SYNC_t sobj /* Sync object to be signaled */
151 )
152 {
153 /* Win32 */
154 ReleaseMutex(sobj);
155
156 /* uITRON */
157 // sig_sem(sobj);
158
159 /* uC/OS-II */
160 // OSMutexPost(sobj);
161
162 /* FreeRTOS */
163 // xSemaphoreGive(sobj);
164
165 /* CMSIS-RTOS */
166 // osMutexRelease(sobj);
167 }
168
169 #endif
170
171