diskio.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*-----------------------------------------------------------------------*/
  2. /* Low level disk I/O module skeleton for FatFs (C)ChaN, 2007 */
  3. /*-----------------------------------------------------------------------*/
  4. /* This is a stub disk I/O module that acts as front end of the existing */
  5. /* disk I/O modules and attach it to FatFs module with common interface. */
  6. /*-----------------------------------------------------------------------*/
  7. #include "diskio.h"
  8. #include "at32f403a_407.h"
  9. #include "ffconf.h"
  10. //#include "systick.h"
  11. //#include "usbh_mcs_fatfs.h"
  12. #include "drivers/fatfs_spi_flash.h"
  13. /*-----------------------------------------------------------------------*/
  14. /* Correspondence between physical drive number and physical drive. */
  15. /*-----------------------------------------------------------------------*/
  16. #define SECTOR_SIZE 512
  17. /*-----------------------------------------------------------------------*/
  18. /* Inidialize a Drive */
  19. /*-----------------------------------------------------------------------*/
  20. DSTATUS disk_initialize (
  21. BYTE drv /* Physical drive nmuber (0..) */
  22. )
  23. {
  24. /* Set NOINIT bit */
  25. int res = STA_NOINIT;
  26. switch (drv) {
  27. case ATA:
  28. break;
  29. case MMC:
  30. break;
  31. case SD:
  32. break;
  33. case SPI_FLASH:
  34. res = ff_spi_flash_init();
  35. break;
  36. }
  37. return (DSTATUS)res;
  38. }
  39. /*-----------------------------------------------------------------------*/
  40. /* Return Disk Status */
  41. /*-----------------------------------------------------------------------*/
  42. DSTATUS disk_status (
  43. BYTE drv /* Physical drive nmuber (0) */
  44. )
  45. {
  46. // if (drv) return STA_NOINIT; /* Supports only single drive */
  47. // return 0;
  48. DSTATUS stat = 0;
  49. //int result;
  50. switch (drv) {
  51. case ATA :
  52. //result = ATA_disk_status();
  53. // translate the reslut code here
  54. return stat;
  55. case MMC :
  56. //result = MMC_disk_status();
  57. // translate the reslut code here
  58. return stat;
  59. case SD :
  60. //result = SD_disk_status();
  61. // translate the reslut code here
  62. return stat;
  63. case SPI_FLASH :
  64. //result = USB_disk_status();
  65. // translate the reslut code here
  66. return stat;
  67. }
  68. return STA_NOINIT;
  69. }
  70. /*-----------------------------------------------------------------------*/
  71. /* Read Sector(s) */
  72. /*-----------------------------------------------------------------------*/
  73. DRESULT disk_read (
  74. BYTE drv, /* Physical drive nmuber (0..) */
  75. BYTE *buff, /* Data buffer to store read data */
  76. DWORD sector, /* Sector address (LBA) */
  77. UINT count /* Number of sectors to read (1..255) */
  78. )
  79. {
  80. DRESULT res = RES_NOTRDY;
  81. switch (drv) {
  82. case ATA:
  83. break;
  84. case MMC:
  85. break;
  86. case SD:
  87. break;
  88. case SPI_FLASH:
  89. //DBG start_time_meas(); /* Debug measure of function calls duration */
  90. res = (DRESULT)ff_spi_flash_read(buff, sector, count);
  91. //DBG printf("\tSPI read sector: %dus\r\n", stop_time_meas());
  92. break;
  93. }
  94. return res;
  95. }
  96. /*-----------------------------------------------------------------------*/
  97. /* Write Sector(s) */
  98. /*-----------------------------------------------------------------------*/
  99. /* The FatFs module will issue multiple sector transfer request
  100. / (count > 1) to the disk I/O layer. The disk function should process
  101. / the multiple sector transfer properly Do. not translate it into
  102. / multiple single sector transfers to the media, or the data read/write
  103. / performance may be drasticaly decreased. */
  104. #if _READONLY == 0
  105. DRESULT disk_write (
  106. BYTE drv, /* Physical drive nmuber (0..) */
  107. const BYTE *buff, /* Data to be written */
  108. DWORD sector, /* Sector address (LBA) */
  109. UINT count /* Number of sectors to write (1..255) */
  110. )
  111. {
  112. DRESULT res = RES_NOTRDY;
  113. switch (drv) {
  114. case ATA:
  115. break;
  116. case MMC:
  117. break;
  118. case SD:
  119. break;
  120. case SPI_FLASH:
  121. //DBG start_time_meas(); /* Debug measure of function calls duration */
  122. res = (DRESULT)ff_spi_flash_write(buff, sector, count);
  123. //DBG printf("\tSPI write sector: %dus\r\n", stop_time_meas());
  124. break;
  125. }
  126. return res;
  127. }
  128. #endif /* _READONLY */
  129. /*-----------------------------------------------------------------------*/
  130. /* Get current time */
  131. /*-----------------------------------------------------------------------*/
  132. DWORD get_fattime ()
  133. {
  134. return ((2006UL-1980) << 25) // Year = 2006
  135. | (2UL << 21) // Month = Feb
  136. | (9UL << 16) // Day = 9
  137. | (22U << 11) // Hour = 22
  138. | (30U << 5) // Min = 30
  139. | (0U >> 1) // Sec = 0
  140. ;
  141. }
  142. /*-----------------------------------------------------------------------*/
  143. /* Miscellaneous Functions */
  144. /*-----------------------------------------------------------------------*/
  145. DRESULT disk_ioctl (
  146. BYTE drv, /* Physical drive nmuber (0..) */
  147. BYTE ctrl, /* Control code */
  148. void *buff /* Buffer to send/receive control data */
  149. )
  150. {
  151. DRESULT res = RES_OK;
  152. switch (drv) {
  153. case ATA:
  154. break;
  155. case MMC:
  156. break;
  157. case SD:
  158. break;
  159. case SPI_FLASH:
  160. switch (ctrl) {
  161. case GET_SECTOR_COUNT : // Get number of sectors on the disk (DWORD)
  162. *(DWORD*)buff = SPI_FLASH_BLOCK_SIZE * SPI_FLASH_BLOCK_NUMBER;
  163. res = RES_OK;
  164. break;
  165. case GET_SECTOR_SIZE : // Get R/W sector size (WORD)
  166. *(WORD*)buff = SPI_FLASH_SECTOR_SIZE;
  167. res = RES_OK;
  168. break;
  169. case GET_BLOCK_SIZE : // Get erase block size in unit of sector (DWORD)
  170. *(DWORD*)buff = SPI_FLASH_SECTORS_IN_BLOCK_NUMBER;
  171. res = RES_OK;
  172. break;
  173. }
  174. break;
  175. }
  176. return res;
  177. }