|
| 1 | +/** |
| 2 | + ****************************************************************************** |
| 3 | + * @file ffsystem_cmsis_os.c |
| 4 | + * @author MCD Application Team |
| 5 | + * @brief ffsystem cmsis_os functions implementation |
| 6 | + ****************************************************************************** |
| 7 | + * @attention |
| 8 | + * |
| 9 | + * Copyright (C) 2018, ChaN, all right reserved |
| 10 | + * Copyright (c) 2023 STMicroelectronics |
| 11 | + * |
| 12 | + * This software is licensed under terms that can be found in the LICENSE file |
| 13 | + * in the root directory of this software component. |
| 14 | + * If no LICENSE file comes with this software, it is provided AS-IS. |
| 15 | + * |
| 16 | + ****************************************************************************** |
| 17 | + */ |
| 18 | + |
| 19 | +/* Includes -------------------------------------------------------------*/ |
| 20 | +#include "ff.h" |
| 21 | + |
| 22 | +#if FF_USE_LFN == 3 |
| 23 | +#include "FreeRTOS.h" |
| 24 | +#endif |
| 25 | + |
| 26 | +#if FF_FS_REENTRANT |
| 27 | +#include "cmsis_os2.h" |
| 28 | +#endif |
| 29 | + |
| 30 | +/* Dynamic memory allocation */ |
| 31 | +#if FF_USE_LFN == 3 |
| 32 | + |
| 33 | +/*------------------------------------------------------------------------*/ |
| 34 | +/* Allocate/Free a Memory Block */ |
| 35 | +/*------------------------------------------------------------------------*/ |
| 36 | + |
| 37 | +void* ff_memalloc ( /* Returns pointer to the allocated memory block (null if not enough core) */ |
| 38 | + UINT msize /* Number of bytes to allocate */ |
| 39 | +) |
| 40 | +{ |
| 41 | + return (void *) pvPortMalloc((size_t)msize); /* Allocate a new memory block */ |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +/*------------------------------------------------------------------------*/ |
| 46 | +/* Free a memory block */ |
| 47 | +/*------------------------------------------------------------------------*/ |
| 48 | + |
| 49 | +void ff_memfree ( |
| 50 | + void* mblock /* Pointer to the memory block to free (no effect if null) */ |
| 51 | +) |
| 52 | +{ |
| 53 | + vPortFree(mblock); /* Free the memory block */ |
| 54 | +} |
| 55 | + |
| 56 | +#endif |
| 57 | + |
| 58 | + |
| 59 | +/* Mutal exclusion */ |
| 60 | +#if FF_FS_REENTRANT |
| 61 | + |
| 62 | +/* Definition table of Mutex */ |
| 63 | +static osMutexId_t Mutex[FF_VOLUMES + 1]; /* Table of mutex ID */ |
| 64 | + |
| 65 | +/*------------------------------------------------------------------------*/ |
| 66 | +/* Create a Mutex */ |
| 67 | +/*------------------------------------------------------------------------*/ |
| 68 | +/* This function is called in f_mount function to create a new mutex |
| 69 | +/ or semaphore for the volume. When a 0 is returned, the f_mount function |
| 70 | +/ fails with FR_INT_ERR. |
| 71 | +*/ |
| 72 | + |
| 73 | +int ff_mutex_create ( /* Returns 1:Function succeeded or 0:Could not create the mutex */ |
| 74 | + int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */ |
| 75 | +) |
| 76 | +{ |
| 77 | + int ret; |
| 78 | + const osMutexAttr_t attr = { |
| 79 | + "FatFsMutex", |
| 80 | + osMutexRecursive, |
| 81 | + NULL, |
| 82 | + 0U |
| 83 | + }; |
| 84 | + |
| 85 | + Mutex[vol] = osMutexNew(&attr); |
| 86 | + if (Mutex[vol] != NULL) |
| 87 | + { |
| 88 | + ret = 1; |
| 89 | + } |
| 90 | + else |
| 91 | + { |
| 92 | + ret = 0; |
| 93 | + } |
| 94 | + |
| 95 | + return ret; |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +/*------------------------------------------------------------------------*/ |
| 100 | +/* Delete a Mutex */ |
| 101 | +/*------------------------------------------------------------------------*/ |
| 102 | +/* This function is called in f_mount function to delete a mutex or |
| 103 | +/ semaphore of the volume created with ff_mutex_create function. |
| 104 | +*/ |
| 105 | + |
| 106 | +void ff_mutex_delete ( /* Returns 1:Function succeeded or 0:Could not delete due to an error */ |
| 107 | + int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */ |
| 108 | +) |
| 109 | +{ |
| 110 | + osMutexDelete(Mutex[vol]); |
| 111 | +} |
| 112 | + |
| 113 | + |
| 114 | +/*------------------------------------------------------------------------*/ |
| 115 | +/* Request a Grant to Access the Volume */ |
| 116 | +/*------------------------------------------------------------------------*/ |
| 117 | +/* This function is called on enter file functions to lock the volume. |
| 118 | +/ When a 0 is returned, the file function fails with FR_TIMEOUT. |
| 119 | +*/ |
| 120 | + |
| 121 | +int ff_mutex_take ( /* Returns 1:Succeeded or 0:Timeout */ |
| 122 | + int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */ |
| 123 | +) |
| 124 | +{ |
| 125 | + int ret; |
| 126 | + |
| 127 | + if(osMutexAcquire(Mutex[vol], FF_FS_TIMEOUT) == osOK) |
| 128 | + { |
| 129 | + ret = 1; |
| 130 | + } |
| 131 | + else |
| 132 | + { |
| 133 | + ret = 0; |
| 134 | + } |
| 135 | + |
| 136 | + return ret; |
| 137 | +} |
| 138 | + |
| 139 | +/*------------------------------------------------------------------------*/ |
| 140 | +/* Release a Grant to Access the Volume */ |
| 141 | +/*------------------------------------------------------------------------*/ |
| 142 | +/* This function is called on leave file functions to unlock the volume. |
| 143 | +*/ |
| 144 | + |
| 145 | +void ff_mutex_give ( |
| 146 | + int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */ |
| 147 | +) |
| 148 | +{ |
| 149 | + osMutexRelease(Mutex[vol]); |
| 150 | +} |
| 151 | +#endif |
0 commit comments