]>
Commit | Line | Data |
---|---|---|
53668523 L |
1 | /*------------------------------------------------------------------------*/\r |
2 | /* Sample code of OS dependent controls for FatFs */\r | |
7b78a5a2 | 3 | /* (C)ChaN, 2014 */\r |
53668523 L |
4 | /*------------------------------------------------------------------------*/\r |
5 | \r | |
53668523 L |
6 | \r |
7 | #include "../ff.h"\r | |
8 | \r | |
9 | \r | |
10 | #if _FS_REENTRANT\r | |
11 | /*------------------------------------------------------------------------*/\r | |
12 | /* Create a Synchronization Object\r | |
13 | /*------------------------------------------------------------------------*/\r | |
7b78a5a2 L |
14 | /* This function is called in f_mount() function to create a new\r |
15 | / synchronization object, such as semaphore and mutex. When a 0 is returned,\r | |
16 | / the f_mount() function fails with FR_INT_ERR.\r | |
53668523 L |
17 | */\r |
18 | \r | |
7b78a5a2 | 19 | int ff_cre_syncobj ( /* !=0:Function succeeded, ==0:Could not create due to any error */\r |
53668523 | 20 | BYTE vol, /* Corresponding logical drive being processed */\r |
7b78a5a2 | 21 | _SYNC_t *sobj /* Pointer to return the created sync object */\r |
53668523 L |
22 | )\r |
23 | {\r | |
24 | int ret;\r | |
25 | \r | |
26 | \r | |
27 | *sobj = CreateMutex(NULL, FALSE, NULL); /* Win32 */\r | |
28 | ret = (int)(*sobj != INVALID_HANDLE_VALUE);\r | |
29 | \r | |
7b78a5a2 L |
30 | // *sobj = SyncObjects[vol]; /* uITRON (give a static created sync object) */\r |
31 | // ret = 1; /* The initial value of the semaphore must be 1. */\r | |
53668523 | 32 | \r |
7b78a5a2 | 33 | // *sobj = OSMutexCreate(0, &err); /* uC/OS-II */\r |
53668523 L |
34 | // ret = (int)(err == OS_NO_ERR);\r |
35 | \r | |
7b78a5a2 | 36 | // *sobj = xSemaphoreCreateMutex(); /* FreeRTOS */\r |
53668523 L |
37 | // ret = (int)(*sobj != NULL);\r |
38 | \r | |
39 | return ret;\r | |
40 | }\r | |
41 | \r | |
42 | \r | |
43 | \r | |
44 | /*------------------------------------------------------------------------*/\r | |
45 | /* Delete a Synchronization Object */\r | |
46 | /*------------------------------------------------------------------------*/\r | |
47 | /* This function is called in f_mount() function to delete a synchronization\r | |
7b78a5a2 L |
48 | / object that created with ff_cre_syncobj function. When a 0 is returned,\r |
49 | / the f_mount() function fails with FR_INT_ERR.\r | |
53668523 L |
50 | */\r |
51 | \r | |
7b78a5a2 | 52 | int ff_del_syncobj ( /* !=0:Function succeeded, ==0:Could not delete due to any error */\r |
53668523 L |
53 | _SYNC_t sobj /* Sync object tied to the logical drive to be deleted */\r |
54 | )\r | |
55 | {\r | |
56 | int ret;\r | |
57 | \r | |
58 | \r | |
59 | ret = CloseHandle(sobj); /* Win32 */\r | |
60 | \r | |
61 | // ret = 1; /* uITRON (nothing to do) */\r | |
62 | \r | |
63 | // OSMutexDel(sobj, OS_DEL_ALWAYS, &err); /* uC/OS-II */\r | |
64 | // ret = (int)(err == OS_NO_ERR);\r | |
65 | \r | |
7b78a5a2 | 66 | // vSemaphoreDelete(sobj); /* FreeRTOS */\r |
53668523 L |
67 | // ret = 1;\r |
68 | \r | |
69 | return ret;\r | |
70 | }\r | |
71 | \r | |
72 | \r | |
73 | \r | |
74 | /*------------------------------------------------------------------------*/\r | |
75 | /* Request Grant to Access the Volume */\r | |
76 | /*------------------------------------------------------------------------*/\r | |
77 | /* This function is called on entering file functions to lock the volume.\r | |
7b78a5a2 | 78 | / When a 0 is returned, the file function fails with FR_TIMEOUT.\r |
53668523 L |
79 | */\r |
80 | \r | |
7b78a5a2 | 81 | int ff_req_grant ( /* 1:Got a grant to access the volume, 0:Could not get a grant */\r |
53668523 L |
82 | _SYNC_t sobj /* Sync object to wait */\r |
83 | )\r | |
84 | {\r | |
85 | int ret;\r | |
86 | \r | |
87 | ret = (int)(WaitForSingleObject(sobj, _FS_TIMEOUT) == WAIT_OBJECT_0); /* Win32 */\r | |
88 | \r | |
89 | // ret = (int)(wai_sem(sobj) == E_OK); /* uITRON */\r | |
90 | \r | |
91 | // OSMutexPend(sobj, _FS_TIMEOUT, &err)); /* uC/OS-II */\r | |
92 | // ret = (int)(err == OS_NO_ERR);\r | |
93 | \r | |
94 | // ret = (int)(xSemaphoreTake(sobj, _FS_TIMEOUT) == pdTRUE); /* FreeRTOS */\r | |
95 | \r | |
96 | return ret;\r | |
97 | }\r | |
98 | \r | |
99 | \r | |
100 | \r | |
101 | /*------------------------------------------------------------------------*/\r | |
102 | /* Release Grant to Access the Volume */\r | |
103 | /*------------------------------------------------------------------------*/\r | |
104 | /* This function is called on leaving file functions to unlock the volume.\r | |
105 | */\r | |
106 | \r | |
107 | void ff_rel_grant (\r | |
108 | _SYNC_t sobj /* Sync object to be signaled */\r | |
109 | )\r | |
110 | {\r | |
111 | ReleaseMutex(sobj); /* Win32 */\r | |
112 | \r | |
113 | // sig_sem(sobj); /* uITRON */\r | |
114 | \r | |
115 | // OSMutexPost(sobj); /* uC/OS-II */\r | |
116 | \r | |
117 | // xSemaphoreGive(sobj); /* FreeRTOS */\r | |
118 | }\r | |
119 | \r | |
120 | #endif\r | |
121 | \r | |
122 | \r | |
123 | \r | |
124 | \r | |
125 | #if _USE_LFN == 3 /* LFN with a working buffer on the heap */\r | |
126 | /*------------------------------------------------------------------------*/\r | |
127 | /* Allocate a memory block */\r | |
128 | /*------------------------------------------------------------------------*/\r | |
129 | /* If a NULL is returned, the file function fails with FR_NOT_ENOUGH_CORE.\r | |
130 | */\r | |
131 | \r | |
132 | void* ff_memalloc ( /* Returns pointer to the allocated memory block */\r | |
133 | UINT msize /* Number of bytes to allocate */\r | |
134 | )\r | |
135 | {\r | |
7b78a5a2 | 136 | return malloc(msize); /* Allocate a new memory block with POSIX API */\r |
53668523 L |
137 | }\r |
138 | \r | |
139 | \r | |
140 | /*------------------------------------------------------------------------*/\r | |
141 | /* Free a memory block */\r | |
142 | /*------------------------------------------------------------------------*/\r | |
143 | \r | |
144 | void ff_memfree (\r | |
145 | void* mblock /* Pointer to the memory block to free */\r | |
146 | )\r | |
147 | {\r | |
7b78a5a2 | 148 | free(mblock); /* Discard the memory block with POSIX API */\r |
53668523 L |
149 | }\r |
150 | \r | |
151 | #endif\r |