1 /*------------------------------------------------------------------------*/
2 /* Sample Code of OS Dependent Functions for FatFs */
3 /* (C)ChaN, 2018 */
4 /*------------------------------------------------------------------------*/
5
6
7 #include "ff.h"
8 #ifdef __LITEOS_M__
9 #include "ffconf.h"
10 #include "los_memory.h"
11 #include "los_membox.h"
12 #endif
13
14 #ifdef __LITEOS_M__
15 #define FF_MEM_BLOCK_NUM (FAT_MAX_OPEN_FILES + FF_VOLUMES)
16 #define FF_MEMBOX_HEAD_SIZE (sizeof(LOS_MEMBOX_INFO) + OS_MEMBOX_NODE_HEAD_SIZE * FF_MEM_BLOCK_NUM)
17 #define FF_MEMBOX_ALLOC_SIZE (FF_MAX_SS * FF_MEM_BLOCK_NUM + FF_MEMBOX_HEAD_SIZE)
18 static unsigned char g_ffMemBoxArray[FF_MEMBOX_ALLOC_SIZE] = {0};
19 #endif
20
ff_memset(void * dst,int val,UINT cnt)21 void ff_memset (void* dst,
22 int val,
23 UINT cnt
24 )
25 {
26 BYTE *d = (BYTE*)dst;
27
28 do {
29 *d++ = (BYTE)val;
30 } while (--cnt);
31 }
32
33 #ifndef __LITEOS_M__
ff_strnlen(const void * str,size_t maxlen)34 int ff_strnlen(const void *str,
35 size_t maxlen
36 )
37 {
38 const BYTE *p = (const BYTE *)NULL;
39 const BYTE *c = (const BYTE*)str;
40 for (p = c; maxlen-- != 0 && *p != '\0'; ++p);
41
42 return p - c;
43 }
44 #endif
45
46 /*------------------------------------------------------------------------*/
47 /* Allocate a memory block */
48 /*------------------------------------------------------------------------*/
49
ff_memalloc(UINT msize)50 void* ff_memalloc ( /* Returns pointer to the allocated memory block (null if not enough core) */
51 UINT msize /* Number of bytes to allocate */
52 )
53 {
54 void* ptr = NULL;
55
56 if(msize == 0)
57 return NULL;
58 #ifndef __LITEOS_M__
59 ptr = LOS_MemAlloc((void *)OS_SYS_MEM_ADDR, msize); /* Allocate a new memory block*/
60 #else
61 static int initFlag = 0;
62 if (initFlag == 0) {
63 (VOID)LOS_MemboxInit(g_ffMemBoxArray, FF_MEMBOX_ALLOC_SIZE, FF_MAX_SS);
64 initFlag = 1;
65 }
66 ptr = LOS_MemboxAlloc(g_ffMemBoxArray);
67 #endif
68 if (ptr != NULL) {
69 ff_memset((void *)ptr, (int)0, msize);
70 }
71
72 return ptr;
73 }
74
75
76 /*------------------------------------------------------------------------*/
77 /* Free a memory block */
78 /*------------------------------------------------------------------------*/
79
ff_memfree(void * mblock)80 void ff_memfree (
81 void* mblock /* Pointer to the memory block to free (nothing to do if null) */
82 )
83 {
84 if (mblock == NULL)
85 return;
86 #ifndef __LITEOS_M__
87 (VOID)LOS_MemFree((void *)OS_SYS_MEM_ADDR, mblock);
88 #else
89 (VOID)LOS_MemboxFree(g_ffMemBoxArray, mblock);
90 #endif
91 }
92
93
94
95 #if FF_FS_REENTRANT /* Mutal exclusion */
96
97 /*------------------------------------------------------------------------*/
98 /* Create a Synchronization Object */
99 /*------------------------------------------------------------------------*/
100 /* This function is called in f_mount() function to create a new
101 / synchronization object for the volume, such as semaphore and mutex.
102 / When a 0 is returned, the f_mount() function fails with FR_INT_ERR.
103 */
104
105 //const osMutexDef_t Mutex[FF_VOLUMES]; /* Table of CMSIS-RTOS mutex */
106
107
ff_cre_syncobj(BYTE vol,FF_SYNC_t * sobj)108 int ff_cre_syncobj ( /* 1:Function succeeded, 0:Could not create the sync object */
109 BYTE vol, /* Corresponding volume (logical drive number) */
110 FF_SYNC_t* sobj /* Pointer to return the created sync object */
111 )
112 {
113 #ifndef __LITEOS_M__
114 int ret;
115
116
117 ret = LOS_MuxInit(sobj, NULL);
118 if (ret == LOS_OK ) {
119 return TRUE;
120 } else
121 return FALSE;
122 #else
123 return TRUE;
124 #endif
125 }
126
127
128 /*------------------------------------------------------------------------*/
129 /* Delete a Synchronization Object */
130 /*------------------------------------------------------------------------*/
131 /* This function is called in f_mount() function to delete a synchronization
132 / object that created with ff_cre_syncobj() function. When a 0 is returned,
133 / the f_mount() function fails with FR_INT_ERR.
134 */
135
ff_del_syncobj(FF_SYNC_t * sobj)136 int ff_del_syncobj ( /* 1:Function succeeded, 0:Could not delete due to an error */
137 FF_SYNC_t* sobj /* Sync object tied to the logical drive to be deleted */
138 )
139 {
140 #ifndef __LITEOS_M__
141 int ret;
142 ret = LOS_MuxDestroy(sobj);
143 if(ret == LOS_OK) {
144 return TRUE;
145 } else {
146 return FALSE;
147 }
148 #else
149 return TRUE;
150 #endif
151 }
152
153
154 /*------------------------------------------------------------------------*/
155 /* Request Grant to Access the Volume */
156 /*------------------------------------------------------------------------*/
157 /* This function is called on entering file functions to lock the volume.
158 / When a 0 is returned, the file function fails with FR_TIMEOUT.
159 */
160
ff_req_grant(FF_SYNC_t * sobj)161 int ff_req_grant ( /* 1:Got a grant to access the volume, 0:Could not get a grant */
162 FF_SYNC_t* sobj /* Sync object to wait */
163 )
164 {
165 #ifndef __LITEOS_M__
166 if (LOS_MuxLock(sobj, FF_FS_TIMEOUT) == LOS_OK) {
167 return TRUE;
168 }
169
170 return FALSE;
171 #else
172 return TRUE;
173 #endif
174 }
175
176
177 /*------------------------------------------------------------------------*/
178 /* Release Grant to Access the Volume */
179 /*------------------------------------------------------------------------*/
180 /* This function is called on leaving file functions to unlock the volume.
181 */
182
ff_rel_grant(FF_SYNC_t * sobj)183 void ff_rel_grant (
184 FF_SYNC_t* sobj /* Sync object to be signaled */
185 )
186 {
187 #ifndef __LITEOS_M__
188 (void)LOS_MuxUnlock(sobj);
189 #endif
190 }
191
192 #endif
193
194