syscall.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*------------------------------------------------------------------------*/
  2. /* Sample code of OS dependent controls for FatFs */
  3. /* (C)ChaN, 2014 */
  4. /*------------------------------------------------------------------------*/
  5. #include "../include/ff.h"
  6. #include <kernel.h>
  7. #define INVALID_HANDLE_VALUE 0xFFFFFFFF
  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 0 is returned,
  14. / the f_mount() function fails with FR_INT_ERR.
  15. */
  16. //K_MUTEX_DEFINE(FS_MUTEX);
  17. /* NOTICE: keep pace with _VOLUMES and _VOLUME_STRS */
  18. static K_MUTEX_DEFINE(ff_sync_0);
  19. static K_MUTEX_DEFINE(ff_sync_1);
  20. static K_MUTEX_DEFINE(ff_sync_2);
  21. static K_MUTEX_DEFINE(ff_sync_3);
  22. static K_MUTEX_DEFINE(ff_sync_4);
  23. int ff_cre_syncobj ( /* 1:Function succeeded, 0:Could not create the sync object */
  24. BYTE vol, /* Corresponding volume (logical drive number) */
  25. _SYNC_t *sobj /* Pointer to return the created sync object */
  26. )
  27. {
  28. int ret = 1;
  29. switch (vol) {
  30. case 0:
  31. *sobj = &ff_sync_0;
  32. break;
  33. case 1:
  34. *sobj = &ff_sync_1;
  35. break;
  36. case 2:
  37. *sobj = &ff_sync_2;
  38. break;
  39. case 3:
  40. *sobj = &ff_sync_3;
  41. break;
  42. case 4:
  43. *sobj = &ff_sync_4;
  44. break;
  45. default:
  46. ret = 0;
  47. break;
  48. }
  49. // *sobj = CreateMutex(NULL, FALSE, NULL); /* Win32 */
  50. // ret = (int)(*sobj != INVALID_HANDLE_VALUE);
  51. // *sobj = SyncObjects[vol]; /* uITRON (give a static sync object) */
  52. // ret = 1; /* The initial value of the semaphore must be 1. */
  53. // *sobj = OSMutexCreate(0, &err); /* uC/OS-II */
  54. // ret = (int)(err == OS_NO_ERR);
  55. // *sobj = xSemaphoreCreateMutex(); /* FreeRTOS */
  56. // ret = (int)(*sobj != NULL);
  57. return ret;
  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 any error */
  67. _SYNC_t sobj /* Sync object tied to the logical drive to be deleted */
  68. )
  69. {
  70. int ret;
  71. ret = 1;
  72. // ret = CloseHandle(sobj); /* Win32 */
  73. // ret = 1; /* uITRON (nothing to do) */
  74. // OSMutexDel(sobj, OS_DEL_ALWAYS, &err); /* uC/OS-II */
  75. // ret = (int)(err == OS_NO_ERR);
  76. // vSemaphoreDelete(sobj); /* FreeRTOS */
  77. // ret = 1;
  78. return ret;
  79. }
  80. /*------------------------------------------------------------------------*/
  81. /* Request Grant to Access the Volume */
  82. /*------------------------------------------------------------------------*/
  83. /* This function is called on entering file functions to lock the volume.
  84. / When a 0 is returned, the file function fails with FR_TIMEOUT.
  85. */
  86. int ff_req_grant ( /* 1:Got a grant to access the volume, 0:Could not get a grant */
  87. _SYNC_t sobj /* Sync object to wait */
  88. )
  89. {
  90. int ret = 1;
  91. if (sobj != 0 && (int)sobj != INVALID_HANDLE_VALUE) {
  92. ret = (int ) (k_mutex_lock(sobj, K_FOREVER) == 0);
  93. }
  94. // ret = (int)(WaitForSingleObject(sobj, _FS_TIMEOUT) == WAIT_OBJECT_0); /* Win32 */
  95. // ret = (int)(wai_sem(sobj) == E_OK); /* uITRON */
  96. // OSMutexPend(sobj, _FS_TIMEOUT, &err)); /* uC/OS-II */
  97. // ret = (int)(err == OS_NO_ERR);
  98. // ret = (int)(xSemaphoreTake(sobj, _FS_TIMEOUT) == pdTRUE); /* FreeRTOS */
  99. return ret;
  100. }
  101. /*------------------------------------------------------------------------*/
  102. /* Release Grant to Access the Volume */
  103. /*------------------------------------------------------------------------*/
  104. /* This function is called on leaving file functions to unlock the volume.
  105. */
  106. void ff_rel_grant (
  107. _SYNC_t sobj /* Sync object to be signaled */
  108. )
  109. {
  110. if (sobj != 0 && (int)sobj != INVALID_HANDLE_VALUE) {
  111. k_mutex_unlock(sobj);
  112. }
  113. // ReleaseMutex(sobj); /* Win32 */
  114. // sig_sem(sobj); /* uITRON */
  115. // OSMutexPost(sobj); /* uC/OS-II */
  116. // xSemaphoreGive(sobj); /* FreeRTOS */
  117. }
  118. #endif
  119. #if _USE_LFN == 3 /* LFN with a working buffer on the heap */
  120. /*------------------------------------------------------------------------*/
  121. /* Allocate a memory block */
  122. /*------------------------------------------------------------------------*/
  123. /* If a NULL is returned, the file function fails with FR_NOT_ENOUGH_CORE.
  124. */
  125. extern void * mem_malloc(unsigned int num_bytes);
  126. extern void mem_free(void *ptr);
  127. void* ff_memalloc ( /* Returns pointer to the allocated memory block */
  128. UINT msize /* Number of bytes to allocate */
  129. )
  130. {
  131. return k_malloc(msize);
  132. //return mem_malloc(msize); /* Allocate a new memory block with POSIX API */
  133. }
  134. /*------------------------------------------------------------------------*/
  135. /* Free a memory block */
  136. /*------------------------------------------------------------------------*/
  137. void ff_memfree (
  138. void* mblock /* Pointer to the memory block to free */
  139. )
  140. {
  141. k_free(mblock);
  142. //mem_free(mblock); /* Discard the memory block with POSIX API */
  143. }
  144. #endif