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