1 /*------------------------------------------------------------------------*/
2 /* Sample Code of OS Dependent Functions for FatFs                        */
3 /* (C)ChaN, 2018                                                          */
4 /*------------------------------------------------------------------------*/
5 
6 #include <stdlib.h>
7 #include <aos/kernel.h>
8 #include "ff.h"
9 
10 #if FF_USE_LFN == 3	/* Dynamic memory allocation */
11 
12 /*------------------------------------------------------------------------*/
13 /* Allocate a memory block                                                */
14 /*------------------------------------------------------------------------*/
15 
ff_memalloc(UINT msize)16 void* ff_memalloc (	/* Returns pointer to the allocated memory block (null if not enough core) */
17     UINT msize		/* Number of bytes to allocate */
18 )
19 {
20     return aos_malloc(msize);	/* Allocate a new memory block with POSIX API */
21 }
22 
23 
24 /*------------------------------------------------------------------------*/
25 /* Free a memory block                                                    */
26 /*------------------------------------------------------------------------*/
27 
ff_memfree(void * mblock)28 void ff_memfree (
29     void* mblock	/* Pointer to the memory block to free (nothing to do if null) */
30 )
31 {
32     aos_free(mblock);	/* Free the memory block with POSIX API */
33 }
34 
35 #endif
36 
37 
38 
39 #if FF_FS_REENTRANT	/* Mutal exclusion */
40 
41 /*------------------------------------------------------------------------*/
42 /* Create a Synchronization Object                                        */
43 /*------------------------------------------------------------------------*/
44 /* This function is called in f_mount() function to create a new
45 /  synchronization object for the volume, such as semaphore and mutex.
46 /  When a 0 is returned, the f_mount() function fails with FR_INT_ERR.
47 */
48 
ff_cre_syncobj(BYTE vol,FF_SYNC_t * sobj)49 int ff_cre_syncobj (	/* 1:Function succeeded, 0:Could not create the sync object */
50     BYTE vol,			/* Corresponding volume (logical drive number) */
51     FF_SYNC_t* sobj		/* Pointer to return the created sync object */
52 )
53 {
54     if(aos_mutex_new(sobj) != 0) {
55         return 0;
56     }
57     return 1;
58 }
59 
60 
61 /*------------------------------------------------------------------------*/
62 /* Delete a Synchronization Object                                        */
63 /*------------------------------------------------------------------------*/
64 /* This function is called in f_mount() function to delete a synchronization
65 /  object that created with ff_cre_syncobj() function. When a 0 is returned,
66 /  the f_mount() function fails with FR_INT_ERR.
67 */
68 
ff_del_syncobj(FF_SYNC_t sobj)69 int ff_del_syncobj (	/* 1:Function succeeded, 0:Could not delete due to an error */
70     FF_SYNC_t sobj		/* Sync object tied to the logical drive to be deleted */
71 )
72 {
73     aos_mutex_free(&sobj);
74     return 1;
75 }
76 
77 
78 /*------------------------------------------------------------------------*/
79 /* Request Grant to Access the Volume                                     */
80 /*------------------------------------------------------------------------*/
81 /* This function is called on entering file functions to lock the volume.
82 /  When a 0 is returned, the file function fails with FR_TIMEOUT.
83 */
84 
ff_req_grant(FF_SYNC_t sobj)85 int ff_req_grant (	/* 1:Got a grant to access the volume, 0:Could not get a grant */
86     FF_SYNC_t sobj	/* Sync object to wait */
87 )
88 {
89     if(aos_mutex_lock(&sobj, FF_FS_TIMEOUT) != 0) {
90         return 0;
91     }
92     return 1;
93 }
94 
95 
96 /*------------------------------------------------------------------------*/
97 /* Release Grant to Access the Volume                                     */
98 /*------------------------------------------------------------------------*/
99 /* This function is called on leaving file functions to unlock the volume.
100 */
101 
ff_rel_grant(FF_SYNC_t sobj)102 void ff_rel_grant (
103     FF_SYNC_t sobj	/* Sync object to be signaled */
104 )
105 {
106     aos_mutex_unlock(&sobj);
107 }
108 
109 #endif
110 
111