ffsystem.c 4.9 KB

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