Add FatFs module by ChaN.
[bertos.git] / bertos / fs / fatfs / src / option / syncobj.c
1 /*------------------------------------------------------------------------*/
2 /* Sample code of OS dependent synchronization object controls            */
3 /* for FatFs R0.07a  (C)ChaN, 2009                                        */
4 /*------------------------------------------------------------------------*/
5
6 #include <windows.h>    // Win32
7 //#include <ucos_ii.h>  // uC/OS-II
8
9 #include "../ff.h"
10
11 #if _FS_REENTRANT
12
13 /*------------------------------------------------------------------------*/
14 /* Create a Synchronization Object for a Volume
15 /*------------------------------------------------------------------------*/
16 /* This function is called in f_mount function to create a new
17 /  synchronization object, such as semaphore and mutex. When a FALSE is
18 /  returned, the f_mount function fails with FR_INT_ERR.
19 */
20
21 BOOL ff_cre_syncobj (   /* TRUE:Function succeeded, FALSE:Could not create due to any error */
22         BYTE vol,                       /* Corresponding logical drive being processed */
23         _SYNC_t *sobj           /* Pointer to return the created sync object */
24 )
25 {
26         BOOL ret;
27
28         *sobj = CreateMutex(NULL, FALSE, NULL);                                 // Win32
29         ret = (*sobj != INVALID_HANDLE_VALUE) ? TRUE : FALSE;   //
30
31 //      *sobj = VolumeSemId[vol];       // uITRON (give a static created sync object)
32 //      ret = TRUE;                                     // The initial value of the semaphore must be 1.
33
34 //      *sobj = OSMutexCreate(0, &err);                         // uC/OS-II
35 //      ret = (err == OS_NO_ERR) ? TRUE : FALSE;        //
36
37         return ret;
38 }
39
40
41
42 /*------------------------------------------------------------------------*/
43 /* Delete a Synchronization Object                                        */
44 /*------------------------------------------------------------------------*/
45 /* This function is called in f_mount function to delete a synchronization
46 /  object that created with ff_cre_syncobj function. When a FALSE is
47 /  returned, the f_mount function fails with FR_INT_ERR.
48 */
49
50 BOOL ff_del_syncobj (   /* TRUE:Function succeeded, FALSE:Could not delete due to any error */
51         _SYNC_t sobj            /* Sync object tied to the logical drive to be deleted */
52 )
53 {
54         BOOL ret;
55
56         ret = CloseHandle(sobj);        // Win32
57
58 //      ret = TRUE;                                     // uITRON (nothing to do)
59
60 //      OSMutexDel(sobj, OS_DEL_ALWAYS, &err);          // uC/OS-II
61 //      ret = (err == OS_NO_ERR) ? TRUE : FALSE;        //
62
63         return ret;
64 }
65
66
67
68 /*------------------------------------------------------------------------*/
69 /* Request Grant to Access the Volume                                     */
70 /*------------------------------------------------------------------------*/
71 /* This function is called on entering file functions to lock the volume.
72 /  When a FALSE is returned, the file function fails with FR_TIMEOUT.
73 */
74
75 BOOL ff_req_grant (     /* TRUE:Got a grant to access the volume, FALSE:Could not get a grant */
76         _SYNC_t sobj    /* Sync object to wait */
77 )
78 {
79         BOOL ret;
80
81         ret = (WaitForSingleObject(sobj, _TIMEOUT) == WAIT_OBJECT_0) ? TRUE : FALSE;    // Win32
82
83 //      ret = (wai_sem(sobj) == E_OK) ? TRUE : FALSE;   // uITRON
84
85 //      OSMutexPend(sobj, _TIMEOUT, &err));                             // uC/OS-II
86 //      ret = (err == OS_NO_ERR) ? TRUE : FALSE;                //
87
88         return ret;
89 }
90
91
92
93 /*------------------------------------------------------------------------*/
94 /* Release Grant to Access the Volume                                     */
95 /*------------------------------------------------------------------------*/
96 /* This function is called on leaving file functions to unlock the volume.
97 */
98
99 void ff_rel_grant (
100         _SYNC_t sobj    /* Sync object to be signaled */
101 )
102 {
103         ReleaseMutex(sobj);     // Win32
104
105 //      sig_sem(sobj);          // uITRON
106
107 //      OSMutexPost(sobj);      // uC/OS-II
108 }
109
110
111 #else
112
113 #error This file is not needed in this configuration.
114
115 #endif