1 | /*------------------------------------------------------------------------*/
|
---|
2 | /* Sample code of OS dependent controls for FatFs */
|
---|
3 | /* (C)ChaN, 2014 */
|
---|
4 | /* Portions COPYRIGHT 2017 STMicroelectronics */
|
---|
5 | /* Portions Copyright (C) 2014, ChaN, all right reserved */
|
---|
6 | /*------------------------------------------------------------------------*/
|
---|
7 |
|
---|
8 | /**
|
---|
9 | ******************************************************************************
|
---|
10 | * @attention
|
---|
11 | *
|
---|
12 | * Copyright (c) 2017 STMicroelectronics. All rights reserved.
|
---|
13 | *
|
---|
14 | * This software component is licensed by ST under BSD 3-Clause license,
|
---|
15 | * the "License"; You may not use this file except in compliance with the
|
---|
16 | * License. You may obtain a copy of the License at:
|
---|
17 | * opensource.org/licenses/BSD-3-Clause
|
---|
18 | *
|
---|
19 | ******************************************************************************
|
---|
20 | **/
|
---|
21 |
|
---|
22 |
|
---|
23 |
|
---|
24 | #include "../ff.h"
|
---|
25 |
|
---|
26 |
|
---|
27 | #if _FS_REENTRANT
|
---|
28 | /*------------------------------------------------------------------------*/
|
---|
29 | /* Create a Synchronization Object */
|
---|
30 | /*------------------------------------------------------------------------*/
|
---|
31 | /* This function is called in f_mount() function to create a new
|
---|
32 | / synchronization object, such as semaphore and mutex. When a 0 is returned,
|
---|
33 | / the f_mount() function fails with FR_INT_ERR.
|
---|
34 | */
|
---|
35 |
|
---|
36 | int ff_cre_syncobj ( /* 1:Function succeeded, 0:Could not create the sync object */
|
---|
37 | BYTE vol, /* Corresponding volume (logical drive number) */
|
---|
38 | _SYNC_t *sobj /* Pointer to return the created sync object */
|
---|
39 | )
|
---|
40 | {
|
---|
41 |
|
---|
42 | int ret;
|
---|
43 | #if _USE_MUTEX
|
---|
44 |
|
---|
45 | #if (osCMSIS < 0x20000U)
|
---|
46 | osMutexDef(MTX);
|
---|
47 | *sobj = osMutexCreate(osMutex(MTX));
|
---|
48 | #else
|
---|
49 | *sobj = osMutexNew(NULL);
|
---|
50 | #endif
|
---|
51 |
|
---|
52 | #else
|
---|
53 |
|
---|
54 | #if (osCMSIS < 0x20000U)
|
---|
55 | osSemaphoreDef(SEM);
|
---|
56 | *sobj = osSemaphoreCreate(osSemaphore(SEM), 1);
|
---|
57 | #else
|
---|
58 | *sobj = osSemaphoreNew(1, 1, NULL);
|
---|
59 | #endif
|
---|
60 |
|
---|
61 | #endif
|
---|
62 | ret = (*sobj != NULL);
|
---|
63 |
|
---|
64 | return ret;
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 |
|
---|
69 | /*------------------------------------------------------------------------*/
|
---|
70 | /* Delete a Synchronization Object */
|
---|
71 | /*------------------------------------------------------------------------*/
|
---|
72 | /* This function is called in f_mount() function to delete a synchronization
|
---|
73 | / object that created with ff_cre_syncobj() function. When a 0 is returned,
|
---|
74 | / the f_mount() function fails with FR_INT_ERR.
|
---|
75 | */
|
---|
76 |
|
---|
77 | int ff_del_syncobj ( /* 1:Function succeeded, 0:Could not delete due to any error */
|
---|
78 | _SYNC_t sobj /* Sync object tied to the logical drive to be deleted */
|
---|
79 | )
|
---|
80 | {
|
---|
81 | #if _USE_MUTEX
|
---|
82 | osMutexDelete (sobj);
|
---|
83 | #else
|
---|
84 | osSemaphoreDelete (sobj);
|
---|
85 | #endif
|
---|
86 | return 1;
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 |
|
---|
91 | /*------------------------------------------------------------------------*/
|
---|
92 | /* Request Grant to Access the Volume */
|
---|
93 | /*------------------------------------------------------------------------*/
|
---|
94 | /* This function is called on entering file functions to lock the volume.
|
---|
95 | / When a 0 is returned, the file function fails with FR_TIMEOUT.
|
---|
96 | */
|
---|
97 |
|
---|
98 | int ff_req_grant ( /* 1:Got a grant to access the volume, 0:Could not get a grant */
|
---|
99 | _SYNC_t sobj /* Sync object to wait */
|
---|
100 | )
|
---|
101 | {
|
---|
102 | int ret = 0;
|
---|
103 | #if (osCMSIS < 0x20000U)
|
---|
104 |
|
---|
105 | #if _USE_MUTEX
|
---|
106 | if(osMutexWait(sobj, _FS_TIMEOUT) == osOK)
|
---|
107 | #else
|
---|
108 | if(osSemaphoreWait(sobj, _FS_TIMEOUT) == osOK)
|
---|
109 | #endif
|
---|
110 |
|
---|
111 | #else
|
---|
112 |
|
---|
113 | #if _USE_MUTEX
|
---|
114 | if(osMutexAcquire(sobj, _FS_TIMEOUT) == osOK)
|
---|
115 | #else
|
---|
116 | if(osSemaphoreAcquire(sobj, _FS_TIMEOUT) == osOK)
|
---|
117 | #endif
|
---|
118 |
|
---|
119 | #endif
|
---|
120 | {
|
---|
121 | ret = 1;
|
---|
122 | }
|
---|
123 |
|
---|
124 | return ret;
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 |
|
---|
129 | /*------------------------------------------------------------------------*/
|
---|
130 | /* Release Grant to Access the Volume */
|
---|
131 | /*------------------------------------------------------------------------*/
|
---|
132 | /* This function is called on leaving file functions to unlock the volume.
|
---|
133 | */
|
---|
134 |
|
---|
135 | void ff_rel_grant (
|
---|
136 | _SYNC_t sobj /* Sync object to be signaled */
|
---|
137 | )
|
---|
138 | {
|
---|
139 | #if _USE_MUTEX
|
---|
140 | osMutexRelease(sobj);
|
---|
141 | #else
|
---|
142 | osSemaphoreRelease(sobj);
|
---|
143 | #endif
|
---|
144 | }
|
---|
145 |
|
---|
146 | #endif
|
---|
147 |
|
---|
148 |
|
---|
149 |
|
---|
150 |
|
---|
151 | #if _USE_LFN == 3 /* LFN with a working buffer on the heap */
|
---|
152 | /*------------------------------------------------------------------------*/
|
---|
153 | /* Allocate a memory block */
|
---|
154 | /*------------------------------------------------------------------------*/
|
---|
155 | /* If a NULL is returned, the file function fails with FR_NOT_ENOUGH_CORE.
|
---|
156 | */
|
---|
157 |
|
---|
158 | void* ff_memalloc ( /* Returns pointer to the allocated memory block */
|
---|
159 | UINT msize /* Number of bytes to allocate */
|
---|
160 | )
|
---|
161 | {
|
---|
162 | return ff_malloc(msize); /* Allocate a new memory block with POSIX API */
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | /*------------------------------------------------------------------------*/
|
---|
167 | /* Free a memory block */
|
---|
168 | /*------------------------------------------------------------------------*/
|
---|
169 |
|
---|
170 | void ff_memfree (
|
---|
171 | void* mblock /* Pointer to the memory block to free */
|
---|
172 | )
|
---|
173 | {
|
---|
174 | ff_free(mblock); /* Discard the memory block with POSIX API */
|
---|
175 | }
|
---|
176 |
|
---|
177 | #endif
|
---|