diskio.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*-----------------------------------------------------------------------*/
  2. /* Low level disk I/O module skeleton for FatFs (C)ChaN, 2013 */
  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 module to the FatFs module with a defined API. */
  8. /*-----------------------------------------------------------------------*/
  9. #include "diskio.h" /* FatFs lower layer API */
  10. #include <string.h> // memcpy
  11. #include "stm32f4xx.h"
  12. #include "../SD_Card/sdio_sd.h"
  13. #define BLOCK_SIZE 512 /* Block Size in Bytes */
  14. /* Definitions of physical drive number for each media */
  15. #define ATA 0
  16. #define MMC 1
  17. #define USB 2
  18. /*-----------------------------------------------------------------------*/
  19. /* Inidialize a Drive */
  20. /*-----------------------------------------------------------------------*/
  21. DSTATUS disk_initialize (
  22. BYTE pdrv /* Physical drive nmuber (0..) */
  23. )
  24. {
  25. SD_Error Status;
  26. /* Supports only single drive */
  27. if (pdrv) return STA_NOINIT;
  28. /*-------------------------- SD Init ----------------------------- */
  29. Status = SD_Init();
  30. if (Status!=SD_OK ) return STA_NOINIT;
  31. else return RES_OK;
  32. }
  33. /*-----------------------------------------------------------------------*/
  34. /* Get Disk Status */
  35. /*-----------------------------------------------------------------------*/
  36. DSTATUS disk_status (
  37. BYTE pdrv /* Physical drive nmuber (0..) */
  38. )
  39. {
  40. DSTATUS stat = 0;
  41. if (SD_Detect() != SD_PRESENT)
  42. stat |= STA_NODISK;
  43. // STA_NOTINIT - Subsystem not initailized
  44. // STA_PROTECTED - Write protected, MMC/SD switch if available
  45. return(stat);
  46. }
  47. /*-----------------------------------------------------------------------*/
  48. /* Read Sector(s) */
  49. /*-----------------------------------------------------------------------*/
  50. DRESULT disk_read (
  51. BYTE pdrv, /* Physical drive nmuber (0..) */
  52. BYTE *buff, /* Data buffer to store read data */
  53. DWORD sector, /* Sector address (LBA) */
  54. UINT count /* Number of sectors to read (1..128) */
  55. )
  56. {
  57. SDTransferState state;
  58. DRESULT res = RES_OK;
  59. if (SD_Detect() != SD_PRESENT) return RES_NOTRDY;
  60. if (count == 0) return RES_PARERR;
  61. //SD_ReadMultiBlocks ( buff, sector, 512, 1 );
  62. SD_ReadMultiBlocksFIXED (buff, sector, 512, count);
  63. SD_WaitReadOperation();
  64. do {
  65. state = SD_GetStatus();
  66. if(state == SD_TRANSFER_ERROR) {
  67. res = RES_ERROR;
  68. break;
  69. }
  70. } while (state != SD_TRANSFER_OK);
  71. return res;
  72. }
  73. /*-----------------------------------------------------------------------*/
  74. /* Write Sector(s) */
  75. /*-----------------------------------------------------------------------*/
  76. #if _USE_WRITE
  77. DRESULT disk_write (
  78. BYTE pdrv, /* Physical drive nmuber (0..) */
  79. const BYTE *buff, /* Data to be written */
  80. DWORD sector, /* Sector address (LBA) */
  81. UINT count /* Number of sectors to write (1..128) */
  82. )
  83. {
  84. //SDTransferState state;
  85. // DRESULT res = RES_OK;
  86. if (SD_Detect( ) != SD_PRESENT) return RES_NOTRDY;
  87. if (count == 0) return RES_PARERR;
  88. //SD_WriteMultiBlocks ( (uint8_t *)buff, sector, 512, count );
  89. SD_WriteMultiBlocksFIXED ((uint8_t *)buff, sector, 512, count);
  90. SD_WaitWriteOperation();
  91. while (SD_GetStatus() != SD_TRANSFER_OK);
  92. return RES_OK;
  93. }
  94. #endif
  95. /*-----------------------------------------------------------------------*/
  96. /* Miscellaneous Functions */
  97. /*-----------------------------------------------------------------------*/
  98. #if _USE_IOCTL
  99. DRESULT disk_ioctl (
  100. BYTE pdrv, /* Physical drive nmuber (0..) */
  101. BYTE cmd, /* Control code */
  102. void *buff /* Buffer to send/receive control data */
  103. )
  104. {
  105. return RES_OK;
  106. }
  107. #endif