2
0

ffsystem.c 5.8 KB

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