ffsystem.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*------------------------------------------------------------------------*/
  2. /* A Sample Code of User Provided OS Dependent Functions for FatFs */
  3. /*------------------------------------------------------------------------*/
  4. #include "ff.h"
  5. #ifdef __LUATOS__
  6. #include "luat_base.h"
  7. #endif
  8. #if FF_USE_LFN == 3 /* Use dynamic memory allocation */
  9. /*------------------------------------------------------------------------*/
  10. /* Allocate/Free a Memory Block */
  11. /*------------------------------------------------------------------------*/
  12. #ifdef __LUATOS__
  13. #include "luat_mem.h"
  14. #else
  15. #include <stdlib.h> /* with POSIX API */
  16. #endif
  17. void* ff_memalloc ( /* Returns pointer to the allocated memory block (null if not enough core) */
  18. UINT msize /* Number of bytes to allocate */
  19. )
  20. {
  21. #ifdef __LUATOS__
  22. return luat_heap_malloc((size_t)msize); /* Allocate a new memory block with POSIX API */
  23. #else
  24. return malloc((size_t)msize); /* Allocate a new memory block with POSIX API */
  25. #endif
  26. }
  27. void ff_memfree (
  28. void* mblock /* Pointer to the memory block to free (no effect if null) */
  29. )
  30. {
  31. #ifdef __LUATOS__
  32. luat_heap_free(mblock); /* Free the memory block with POSIX API */
  33. #else
  34. free(mblock); /* Free the memory block with POSIX API */
  35. #endif
  36. }
  37. #endif
  38. #if FF_FS_REENTRANT /* Mutal exclusion */
  39. /*------------------------------------------------------------------------*/
  40. /* Definitions of Mutex */
  41. /*------------------------------------------------------------------------*/
  42. #ifdef __LUATOS__
  43. #define OS_TYPE 5 /* 0:Win32, 1:uITRON4.0, 2:uC/OS-II, 3:FreeRTOS, 4:CMSIS-RTOS, 4:LUATOS */
  44. #else
  45. #define OS_TYPE 0 /* 0:Win32, 1:uITRON4.0, 2:uC/OS-II, 3:FreeRTOS, 4:CMSIS-RTOS, 4:LUATOS */
  46. #endif
  47. #if OS_TYPE == 0 /* Win32 */
  48. #include <windows.h>
  49. static HANDLE Mutex[FF_VOLUMES + 1]; /* Table of mutex handle */
  50. #elif OS_TYPE == 1 /* uITRON */
  51. #include "itron.h"
  52. #include "kernel.h"
  53. static mtxid Mutex[FF_VOLUMES + 1]; /* Table of mutex ID */
  54. #elif OS_TYPE == 2 /* uc/OS-II */
  55. #include "includes.h"
  56. static OS_EVENT *Mutex[FF_VOLUMES + 1]; /* Table of mutex pinter */
  57. #elif OS_TYPE == 3 /* FreeRTOS */
  58. #include "FreeRTOS.h"
  59. #include "semphr.h"
  60. static SemaphoreHandle_t Mutex[FF_VOLUMES + 1]; /* Table of mutex handle */
  61. #elif OS_TYPE == 4 /* CMSIS-RTOS */
  62. #include "cmsis_os.h"
  63. static osMutexId Mutex[FF_VOLUMES + 1]; /* Table of mutex ID */
  64. #elif OS_TYPE == 5 /* LUATOS */
  65. #include "luat_rtos.h"
  66. static luat_rtos_mutex_t Mutex[FF_VOLUMES + 1]; /* Table of mutex ID */
  67. #endif
  68. /*------------------------------------------------------------------------*/
  69. /* Create a Mutex */
  70. /*------------------------------------------------------------------------*/
  71. /* This function is called in f_mount function to create a new mutex
  72. / or semaphore for the volume. When a 0 is returned, the f_mount function
  73. / fails with FR_INT_ERR.
  74. */
  75. int ff_mutex_create ( /* Returns 1:Function succeeded or 0:Could not create the mutex */
  76. int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */
  77. )
  78. {
  79. #if OS_TYPE == 0 /* Win32 */
  80. Mutex[vol] = CreateMutex(NULL, FALSE, NULL);
  81. return (int)(Mutex[vol] != INVALID_HANDLE_VALUE);
  82. #elif OS_TYPE == 1 /* uITRON */
  83. T_CMTX cmtx = {TA_TPRI,1};
  84. Mutex[vol] = acre_mtx(&cmtx);
  85. return (int)(Mutex[vol] > 0);
  86. #elif OS_TYPE == 2 /* uC/OS-II */
  87. OS_ERR err;
  88. Mutex[vol] = OSMutexCreate(0, &err);
  89. return (int)(err == OS_NO_ERR);
  90. #elif OS_TYPE == 3 /* FreeRTOS */
  91. Mutex[vol] = xSemaphoreCreateMutex();
  92. return (int)(Mutex[vol] != NULL);
  93. #elif OS_TYPE == 4 /* CMSIS-RTOS */
  94. osMutexDef(cmsis_os_mutex);
  95. Mutex[vol] = osMutexCreate(osMutex(cmsis_os_mutex));
  96. return (int)(Mutex[vol] != NULL);
  97. #elif OS_TYPE == 5 /* LUATOS */
  98. luat_rtos_mutex_create(&Mutex[vol]);
  99. return (int)(Mutex[vol] != NULL);
  100. #endif
  101. }
  102. /*------------------------------------------------------------------------*/
  103. /* Delete a Mutex */
  104. /*------------------------------------------------------------------------*/
  105. /* This function is called in f_mount function to delete a mutex or
  106. / semaphore of the volume created with ff_mutex_create function.
  107. */
  108. void ff_mutex_delete ( /* Returns 1:Function succeeded or 0:Could not delete due to an error */
  109. int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */
  110. )
  111. {
  112. #if OS_TYPE == 0 /* Win32 */
  113. CloseHandle(Mutex[vol]);
  114. #elif OS_TYPE == 1 /* uITRON */
  115. del_mtx(Mutex[vol]);
  116. #elif OS_TYPE == 2 /* uC/OS-II */
  117. OS_ERR err;
  118. OSMutexDel(Mutex[vol], OS_DEL_ALWAYS, &err);
  119. #elif OS_TYPE == 3 /* FreeRTOS */
  120. vSemaphoreDelete(Mutex[vol]);
  121. #elif OS_TYPE == 4 /* CMSIS-RTOS */
  122. osMutexDelete(Mutex[vol]);
  123. #elif OS_TYPE == 5 /* LUATOS */
  124. luat_rtos_mutex_delete(Mutex[vol]);
  125. #endif
  126. }
  127. /*------------------------------------------------------------------------*/
  128. /* Request a Grant to Access the Volume */
  129. /*------------------------------------------------------------------------*/
  130. /* This function is called on enter file functions to lock the volume.
  131. / When a 0 is returned, the file function fails with FR_TIMEOUT.
  132. */
  133. int ff_mutex_take ( /* Returns 1:Succeeded or 0:Timeout */
  134. int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */
  135. )
  136. {
  137. #if OS_TYPE == 0 /* Win32 */
  138. return (int)(WaitForSingleObject(Mutex[vol], FF_FS_TIMEOUT) == WAIT_OBJECT_0);
  139. #elif OS_TYPE == 1 /* uITRON */
  140. return (int)(tloc_mtx(Mutex[vol], FF_FS_TIMEOUT) == E_OK);
  141. #elif OS_TYPE == 2 /* uC/OS-II */
  142. OS_ERR err;
  143. OSMutexPend(Mutex[vol], FF_FS_TIMEOUT, &err));
  144. return (int)(err == OS_NO_ERR);
  145. #elif OS_TYPE == 3 /* FreeRTOS */
  146. return (int)(xSemaphoreTake(Mutex[vol], FF_FS_TIMEOUT) == pdTRUE);
  147. #elif OS_TYPE == 4 /* CMSIS-RTOS */
  148. return (int)(osMutexWait(Mutex[vol], FF_FS_TIMEOUT) == osOK);
  149. #elif OS_TYPE == 5 /* LUATOS */
  150. return (int)(luat_rtos_mutex_lock(Mutex[vol], LUAT_WAIT_FOREVER) == 0);
  151. #endif
  152. }
  153. /*------------------------------------------------------------------------*/
  154. /* Release a Grant to Access the Volume */
  155. /*------------------------------------------------------------------------*/
  156. /* This function is called on leave file functions to unlock the volume.
  157. */
  158. void ff_mutex_give (
  159. int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */
  160. )
  161. {
  162. #if OS_TYPE == 0 /* Win32 */
  163. ReleaseMutex(Mutex[vol]);
  164. #elif OS_TYPE == 1 /* uITRON */
  165. unl_mtx(Mutex[vol]);
  166. #elif OS_TYPE == 2 /* uC/OS-II */
  167. OSMutexPost(Mutex[vol]);
  168. #elif OS_TYPE == 3 /* FreeRTOS */
  169. xSemaphoreGive(Mutex[vol]);
  170. #elif OS_TYPE == 4 /* CMSIS-RTOS */
  171. osMutexRelease(Mutex[vol]);
  172. #elif OS_TYPE == 5 /* LUATOS */
  173. luat_rtos_mutex_unlock(Mutex[vol]);
  174. #endif
  175. }
  176. #endif /* FF_FS_REENTRANT */