syscall.c 4.7 KB

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