syscall.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*------------------------------------------------------------------------*/
  2. /* Sample code of OS dependent controls for FatFs */
  3. /* (C)ChaN, 2014 */
  4. /*------------------------------------------------------------------------*/
  5. #include "../ff.h"
  6. #if _FS_REENTRANT
  7. /*------------------------------------------------------------------------*/
  8. /* Create a Synchronization Object
  9. /*------------------------------------------------------------------------*/
  10. /* This function is called in f_mount() function to create a new
  11. / synchronization object, such as semaphore and mutex. When a 0 is returned,
  12. / the f_mount() function fails with FR_INT_ERR.
  13. */
  14. int ff_cre_syncobj ( /* !=0:Function succeeded, ==0:Could not create due to any error */
  15. BYTE vol, /* Corresponding logical drive being processed */
  16. _SYNC_t *sobj /* Pointer to return the created sync object */
  17. )
  18. {
  19. int ret;
  20. *sobj = CreateMutex(NULL, FALSE, NULL); /* Win32 */
  21. ret = (int)(*sobj != INVALID_HANDLE_VALUE);
  22. // *sobj = SyncObjects[vol]; /* uITRON (give a static created sync object) */
  23. // ret = 1; /* The initial value of the semaphore must be 1. */
  24. // *sobj = OSMutexCreate(0, &err); /* uC/OS-II */
  25. // ret = (int)(err == OS_NO_ERR);
  26. // *sobj = xSemaphoreCreateMutex(); /* FreeRTOS */
  27. // ret = (int)(*sobj != NULL);
  28. return ret;
  29. }
  30. /*------------------------------------------------------------------------*/
  31. /* Delete a Synchronization Object */
  32. /*------------------------------------------------------------------------*/
  33. /* This function is called in f_mount() function to delete a synchronization
  34. / object that created with ff_cre_syncobj function. When a 0 is returned,
  35. / the f_mount() function fails with FR_INT_ERR.
  36. */
  37. int ff_del_syncobj ( /* !=0:Function succeeded, ==0:Could not delete due to any error */
  38. _SYNC_t sobj /* Sync object tied to the logical drive to be deleted */
  39. )
  40. {
  41. int ret;
  42. ret = CloseHandle(sobj); /* Win32 */
  43. // ret = 1; /* uITRON (nothing to do) */
  44. // OSMutexDel(sobj, OS_DEL_ALWAYS, &err); /* uC/OS-II */
  45. // ret = (int)(err == OS_NO_ERR);
  46. // vSemaphoreDelete(sobj); /* FreeRTOS */
  47. // ret = 1;
  48. return ret;
  49. }
  50. /*------------------------------------------------------------------------*/
  51. /* Request Grant to Access the Volume */
  52. /*------------------------------------------------------------------------*/
  53. /* This function is called on entering file functions to lock the volume.
  54. / When a 0 is returned, the file function fails with FR_TIMEOUT.
  55. */
  56. int ff_req_grant ( /* 1:Got a grant to access the volume, 0:Could not get a grant */
  57. _SYNC_t sobj /* Sync object to wait */
  58. )
  59. {
  60. int ret;
  61. ret = (int)(WaitForSingleObject(sobj, _FS_TIMEOUT) == WAIT_OBJECT_0); /* Win32 */
  62. // ret = (int)(wai_sem(sobj) == E_OK); /* uITRON */
  63. // OSMutexPend(sobj, _FS_TIMEOUT, &err)); /* uC/OS-II */
  64. // ret = (int)(err == OS_NO_ERR);
  65. // ret = (int)(xSemaphoreTake(sobj, _FS_TIMEOUT) == pdTRUE); /* FreeRTOS */
  66. return ret;
  67. }
  68. /*------------------------------------------------------------------------*/
  69. /* Release Grant to Access the Volume */
  70. /*------------------------------------------------------------------------*/
  71. /* This function is called on leaving file functions to unlock the volume.
  72. */
  73. void ff_rel_grant (
  74. _SYNC_t sobj /* Sync object to be signaled */
  75. )
  76. {
  77. ReleaseMutex(sobj); /* Win32 */
  78. // sig_sem(sobj); /* uITRON */
  79. // OSMutexPost(sobj); /* uC/OS-II */
  80. // xSemaphoreGive(sobj); /* FreeRTOS */
  81. }
  82. #endif
  83. #if _USE_LFN == 3 /* LFN with a working buffer on the heap */
  84. /*------------------------------------------------------------------------*/
  85. /* Allocate a memory block */
  86. /*------------------------------------------------------------------------*/
  87. /* If a NULL is returned, the file function fails with FR_NOT_ENOUGH_CORE.
  88. */
  89. void* ff_memalloc ( /* Returns pointer to the allocated memory block */
  90. UINT msize /* Number of bytes to allocate */
  91. )
  92. {
  93. return malloc(msize); /* Allocate a new memory block with POSIX API */
  94. }
  95. /*------------------------------------------------------------------------*/
  96. /* Free a memory block */
  97. /*------------------------------------------------------------------------*/
  98. void ff_memfree (
  99. void* mblock /* Pointer to the memory block to free */
  100. )
  101. {
  102. free(mblock); /* Discard the memory block with POSIX API */
  103. }
  104. #endif