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