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