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