ffsystem.c 4.8 KB

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