ffsystem_mh.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*------------------------------------------------------------------------*/
  2. /* Sample Code of OS Dependent Functions for FatFs */
  3. /* (C)ChaN, 2017 */
  4. /*------------------------------------------------------------------------*/
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include "ff_mh.h"
  8. #include "sdkconfig.h"
  9. #ifdef CONFIG_FATFS_ALLOC_PREFER_EXTRAM
  10. #include "esp_heap_caps.h"
  11. #endif
  12. void* ff_memalloc ( /* Returns pointer to the allocated memory block (null on not enough core) */
  13. unsigned msize /* Number of bytes to allocate */
  14. )
  15. {
  16. #ifdef CONFIG_FATFS_ALLOC_PREFER_EXTRAM
  17. return heap_caps_malloc_prefer(msize, 2, MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM,
  18. MALLOC_CAP_DEFAULT | MALLOC_CAP_INTERNAL);
  19. #else
  20. return malloc(msize);
  21. #endif
  22. }
  23. /*------------------------------------------------------------------------*/
  24. /* Free a memory block */
  25. /*------------------------------------------------------------------------*/
  26. void ff_memfree (
  27. void* mblock /* Pointer to the memory block to free (nothing to do for null) */
  28. )
  29. {
  30. free(mblock); /* Free the memory block with POSIX API */
  31. }
  32. #if FF_FS_REENTRANT /* Mutal exclusion */
  33. /*------------------------------------------------------------------------*/
  34. /* Create a Synchronization Object */
  35. /*------------------------------------------------------------------------*/
  36. /* This function is called in f_mount() function to create a new
  37. / synchronization object for the volume, such as semaphore and mutex.
  38. / When a 0 is returned, the f_mount() function fails with FR_INT_ERR.
  39. */
  40. int ff_cre_syncobj ( /* 1:Function succeeded, 0:Could not create the sync object */
  41. BYTE vol, /* Corresponding volume (logical drive number) */
  42. FF_SYNC_t *sobj /* Pointer to return the created sync object */
  43. )
  44. {
  45. *sobj = xSemaphoreCreateMutex();
  46. return (*sobj != NULL) ? 1 : 0;
  47. }
  48. /*------------------------------------------------------------------------*/
  49. /* Delete a Synchronization Object */
  50. /*------------------------------------------------------------------------*/
  51. /* This function is called in f_mount() function to delete a synchronization
  52. / object that created with ff_cre_syncobj() function. When a 0 is returned,
  53. / the f_mount() function fails with FR_INT_ERR.
  54. */
  55. int ff_del_syncobj ( /* 1:Function succeeded, 0:Could not delete due to an error */
  56. FF_SYNC_t sobj /* Sync object tied to the logical drive to be deleted */
  57. )
  58. {
  59. vSemaphoreDelete(sobj);
  60. return 1;
  61. }
  62. /*------------------------------------------------------------------------*/
  63. /* Request Grant to Access the Volume */
  64. /*------------------------------------------------------------------------*/
  65. /* This function is called on entering file functions to lock the volume.
  66. / When a 0 is returned, the file function fails with FR_TIMEOUT.
  67. */
  68. int ff_req_grant ( /* 1:Got a grant to access the volume, 0:Could not get a grant */
  69. FF_SYNC_t sobj /* Sync object to wait */
  70. )
  71. {
  72. return (xSemaphoreTake(sobj, FF_FS_TIMEOUT) == pdTRUE) ? 1 : 0;
  73. }
  74. /*------------------------------------------------------------------------*/
  75. /* Release Grant to Access the Volume */
  76. /*------------------------------------------------------------------------*/
  77. /* This function is called on leaving file functions to unlock the volume.
  78. */
  79. void ff_rel_grant (
  80. FF_SYNC_t sobj /* Sync object to be signaled */
  81. )
  82. {
  83. xSemaphoreGive(sobj);
  84. }
  85. #endif // FF_FS_REENTRANT