diskio.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*-----------------------------------------------------------------------*/
  2. /* Low level disk I/O module skeleton for FatFs (C)ChaN, 2016 */
  3. /*-----------------------------------------------------------------------*/
  4. /* If a working storage control module is available, it should be */
  5. /* attached to the FatFs via a glue function rather than modifying it. */
  6. /* This is an example of glue functions to attach various exsisting */
  7. /* storage control modules to the FatFs module with a defined API. */
  8. /*-----------------------------------------------------------------------*/
  9. #include "diskio.h" /* FatFs lower layer API */
  10. #include <ffconf.h> /* import ff config */
  11. #define DISK_MAX_PHY_DRV _VOLUMES
  12. /* Number of volumes (logical drives) to be used. */
  13. const char* const disk_volume_strs[] = {
  14. _VOLUME_STRS
  15. };
  16. /*-----------------------------------------------------------------------*/
  17. /* Get Drive Status */
  18. /*-----------------------------------------------------------------------*/
  19. static DSTATUS translate_error(int error)
  20. {
  21. switch (error) {
  22. case 0:
  23. return RES_OK;
  24. case EINVAL:
  25. return RES_PARERR;
  26. case ENODEV:
  27. return RES_NOTRDY;
  28. default:
  29. return RES_ERROR;
  30. }
  31. }
  32. DSTATUS disk_status (
  33. BYTE pdrv /* Physical drive nmuber to identify the drive */
  34. )
  35. {
  36. int result;
  37. if (pdrv >= DISK_MAX_PHY_DRV)
  38. return STA_NODISK;
  39. result = disk_access_status(disk_volume_strs[pdrv]);
  40. return translate_error(result);
  41. }
  42. /*-----------------------------------------------------------------------*/
  43. /* Inidialize a Drive */
  44. /*-----------------------------------------------------------------------*/
  45. DSTATUS disk_initialize (
  46. BYTE pdrv /* Physical drive nmuber to identify the drive */
  47. )
  48. {
  49. int result;
  50. if (pdrv >= DISK_MAX_PHY_DRV)
  51. return STA_NODISK;
  52. #ifdef CONFIG_DISKIO_CACHE
  53. diskio_cache_invalid(disk_volume_strs[pdrv]);
  54. #endif
  55. result = disk_access_init(disk_volume_strs[pdrv]);
  56. return translate_error(result);
  57. }
  58. /*-----------------------------------------------------------------------*/
  59. /* Read Sector(s) */
  60. /*-----------------------------------------------------------------------*/
  61. DRESULT disk_read (
  62. BYTE pdrv, /* Physical drive nmuber to identify the drive */
  63. BYTE *buff, /* Data buffer to store read data */
  64. DWORD sector, /* Start sector in LBA */
  65. UINT count /* Number of sectors to read */
  66. )
  67. {
  68. int result;
  69. if (pdrv >= DISK_MAX_PHY_DRV)
  70. return STA_NODISK;
  71. #ifdef CONFIG_DISKIO_CACHE
  72. result = diskio_cache_read(disk_volume_strs[pdrv], buff, sector, count);
  73. #else
  74. result = disk_access_read(disk_volume_strs[pdrv], buff, sector, count);
  75. #endif
  76. return translate_error(result);
  77. }
  78. /*-----------------------------------------------------------------------*/
  79. /* Write Sector(s) */
  80. /*-----------------------------------------------------------------------*/
  81. DRESULT disk_write (
  82. BYTE pdrv, /* Physical drive nmuber to identify the drive */
  83. const BYTE *buff, /* Data to be written */
  84. DWORD sector, /* Start sector in LBA */
  85. UINT count /* Number of sectors to write */
  86. )
  87. {
  88. int result;
  89. if (pdrv >= DISK_MAX_PHY_DRV)
  90. return STA_NODISK;
  91. #ifdef CONFIG_DISKIO_CACHE
  92. result = diskio_cache_write(disk_volume_strs[pdrv], buff, sector, count);
  93. #else
  94. result = disk_access_write(disk_volume_strs[pdrv], buff, sector, count);
  95. #endif
  96. return translate_error(result);
  97. }
  98. /*-----------------------------------------------------------------------*/
  99. /* Miscellaneous Functions */
  100. /*-----------------------------------------------------------------------*/
  101. DRESULT disk_ioctl (
  102. BYTE pdrv, /* Physical drive nmuber (0..) */
  103. BYTE cmd, /* Control code */
  104. void *buff /* Buffer to send/receive control data */
  105. )
  106. {
  107. int result = RES_OK;
  108. if (pdrv >= DISK_MAX_PHY_DRV)
  109. return STA_NODISK;
  110. switch (cmd) {
  111. case CTRL_SYNC:
  112. #ifdef CONFIG_DISKIO_CACHE
  113. diskio_cache_flush(disk_volume_strs[pdrv]);
  114. #endif
  115. if(disk_access_ioctl(disk_volume_strs[pdrv], DISK_IOCTL_CTRL_SYNC, buff) != 0) {
  116. result = RES_ERROR;
  117. }
  118. break;
  119. case GET_SECTOR_SIZE:
  120. if(disk_access_ioctl(disk_volume_strs[pdrv], DISK_IOCTL_GET_SECTOR_SIZE, buff) != 0) {
  121. result = RES_ERROR;
  122. }
  123. break;
  124. case GET_SECTOR_COUNT:
  125. if(disk_access_ioctl(disk_volume_strs[pdrv], DISK_IOCTL_GET_SECTOR_COUNT, buff) != 0) {
  126. result = RES_ERROR;
  127. }
  128. break;
  129. case GET_BLOCK_SIZE:
  130. if (disk_access_ioctl(disk_volume_strs[pdrv], DISK_IOCTL_GET_ERASE_BLOCK_SZ, buff) != 0) {
  131. result = RES_ERROR;
  132. }
  133. break;
  134. case DISK_HW_DETECT:
  135. if (disk_access_ioctl(disk_volume_strs[pdrv], DISK_IOCTL_HW_DETECT, buff) != 0) {
  136. result = RES_ERROR;
  137. }
  138. break;
  139. default:
  140. result = RES_PARERR;
  141. break;
  142. }
  143. return result;
  144. }