ffsystem.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*------------------------------------------------------------------------*/
  2. /* Sample Code of OS Dependent Functions for FatFs */
  3. /* (C)ChaN, 2018 */
  4. /*------------------------------------------------------------------------*/
  5. #include <stdlib.h>
  6. #include "ff.h"
  7. #if FF_USE_LFN == 3 /* Dynamic memory allocation */
  8. /*------------------------------------------------------------------------*/
  9. /* Allocate a memory block */
  10. /*------------------------------------------------------------------------*/
  11. void* ff_memalloc ( /* Returns pointer to the allocated memory block (null if not enough core) */
  12. UINT msize /* Number of bytes to allocate */
  13. )
  14. {
  15. return malloc(msize); /* Allocate a new memory block with POSIX API */
  16. }
  17. /*------------------------------------------------------------------------*/
  18. /* Free a memory block */
  19. /*------------------------------------------------------------------------*/
  20. void ff_memfree (
  21. void* mblock /* Pointer to the memory block to free (nothing to do if null) */
  22. )
  23. {
  24. free(mblock); /* Free the memory block with POSIX API */
  25. }
  26. #endif
  27. #if FF_FS_REENTRANT /* Mutal exclusion */
  28. /*------------------------------------------------------------------------*/
  29. /* Create a Synchronization Object */
  30. /*------------------------------------------------------------------------*/
  31. /* This function is called in f_mount() function to create a new
  32. / synchronization object for the volume, such as semaphore and mutex.
  33. / When a 0 is returned, the f_mount() function fails with FR_INT_ERR.
  34. */
  35. //const osMutexDef_t Mutex[FF_VOLUMES]; /* Table of CMSIS-RTOS mutex */
  36. int ff_cre_syncobj ( /* 1:Function succeeded, 0:Could not create the sync object */
  37. BYTE vol, /* Corresponding volume (logical drive number) */
  38. FF_SYNC_t* sobj /* Pointer to return the created sync object */
  39. )
  40. {
  41. /* Win32 */
  42. *sobj = CreateMutex(NULL, FALSE, NULL);
  43. return (int)(*sobj != INVALID_HANDLE_VALUE);
  44. /* uITRON */
  45. // T_CSEM csem = {TA_TPRI,1,1};
  46. // *sobj = acre_sem(&csem);
  47. // return (int)(*sobj > 0);
  48. /* uC/OS-II */
  49. // OS_ERR err;
  50. // *sobj = OSMutexCreate(0, &err);
  51. // return (int)(err == OS_NO_ERR);
  52. /* FreeRTOS */
  53. // *sobj = xSemaphoreCreateMutex();
  54. // return (int)(*sobj != NULL);
  55. /* CMSIS-RTOS */
  56. // *sobj = osMutexCreate(&Mutex[vol]);
  57. // return (int)(*sobj != NULL);
  58. }
  59. /*------------------------------------------------------------------------*/
  60. /* Delete a Synchronization Object */
  61. /*------------------------------------------------------------------------*/
  62. /* This function is called in f_mount() function to delete a synchronization
  63. / object that created with ff_cre_syncobj() function. When a 0 is returned,
  64. / the f_mount() function fails with FR_INT_ERR.
  65. */
  66. int ff_del_syncobj ( /* 1:Function succeeded, 0:Could not delete due to an error */
  67. FF_SYNC_t sobj /* Sync object tied to the logical drive to be deleted */
  68. )
  69. {
  70. /* Win32 */
  71. return (int)CloseHandle(sobj);
  72. /* uITRON */
  73. // return (int)(del_sem(sobj) == E_OK);
  74. /* uC/OS-II */
  75. // OS_ERR err;
  76. // OSMutexDel(sobj, OS_DEL_ALWAYS, &err);
  77. // return (int)(err == OS_NO_ERR);
  78. /* FreeRTOS */
  79. // vSemaphoreDelete(sobj);
  80. // return 1;
  81. /* CMSIS-RTOS */
  82. // return (int)(osMutexDelete(sobj) == osOK);
  83. }
  84. /*------------------------------------------------------------------------*/
  85. /* Request Grant to Access the Volume */
  86. /*------------------------------------------------------------------------*/
  87. /* This function is called on entering file functions to lock the volume.
  88. / When a 0 is returned, the file function fails with FR_TIMEOUT.
  89. */
  90. int ff_req_grant ( /* 1:Got a grant to access the volume, 0:Could not get a grant */
  91. FF_SYNC_t sobj /* Sync object to wait */
  92. )
  93. {
  94. /* Win32 */
  95. return (int)(WaitForSingleObject(sobj, FF_FS_TIMEOUT) == WAIT_OBJECT_0);
  96. /* uITRON */
  97. // return (int)(wai_sem(sobj) == E_OK);
  98. /* uC/OS-II */
  99. // OS_ERR err;
  100. // OSMutexPend(sobj, FF_FS_TIMEOUT, &err));
  101. // return (int)(err == OS_NO_ERR);
  102. /* FreeRTOS */
  103. // return (int)(xSemaphoreTake(sobj, FF_FS_TIMEOUT) == pdTRUE);
  104. /* CMSIS-RTOS */
  105. // return (int)(osMutexWait(sobj, FF_FS_TIMEOUT) == osOK);
  106. }
  107. /*------------------------------------------------------------------------*/
  108. /* Release Grant to Access the Volume */
  109. /*------------------------------------------------------------------------*/
  110. /* This function is called on leaving file functions to unlock the volume.
  111. */
  112. void ff_rel_grant (
  113. FF_SYNC_t sobj /* Sync object to be signaled */
  114. )
  115. {
  116. /* Win32 */
  117. ReleaseMutex(sobj);
  118. /* uITRON */
  119. // sig_sem(sobj);
  120. /* uC/OS-II */
  121. // OSMutexPost(sobj);
  122. /* FreeRTOS */
  123. // xSemaphoreGive(sobj);
  124. /* CMSIS-RTOS */
  125. // osMutexRelease(sobj);
  126. }
  127. #endif