diskio.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 "Config.h"
  8. #include "diskio.h"
  9. #include "SPI-SD/SD/sddriver.h"
  10. /*-----------------------------------------------------------------------*/
  11. /* Correspondence between physical drive number and physical drive. */
  12. #define BlockSize 512 /* Block Size in Bytes [这里的block含义是“扇区”,SD卡的每个扇区为512字节]*/
  13. #define BufferWordsSize (BlockSize >> 2) //128,含义参考MAIN中的解释,是128个32位的缓冲区,存放512个字节
  14. /* Inidialize a Drive */
  15. DSTATUS disk_initialize (
  16. BYTE drv /* Physical drive nmuber (0..) */
  17. )
  18. {
  19. BYTE state;
  20. u8 i=0;
  21. if(drv)
  22. {
  23. return STA_NOINIT; //仅支持磁盘0的操作
  24. }
  25. state = SD_Init();
  26. do {
  27. state = SD_Init();
  28. }while(state&&(i++<20)) ;
  29. if(state == STA_NODISK)
  30. {
  31. return STA_NODISK;
  32. }
  33. else if(state != 0)
  34. {
  35. return STA_NOINIT; //其他错误:初始化失败
  36. }
  37. else
  38. {
  39. return RES_OK; //初始化成功
  40. }
  41. }
  42. /*-----------------------------------------------------------------------*/
  43. /* Return Disk Status */
  44. DSTATUS disk_status (
  45. BYTE drv /* Physical drive nmuber (0..) */
  46. )
  47. {
  48. if(drv)
  49. {
  50. return STA_NOINIT; //仅支持磁盘0操作
  51. }
  52. return RES_OK;
  53. }
  54. /*-----------------------------------------------------------------------*/
  55. /* Read Sector(s) */
  56. DRESULT disk_read (
  57. BYTE drv, /* Physical drive nmuber (0..) */
  58. BYTE *buff, /* Data buffer to store read data */
  59. DWORD sector, /* Sector address (LBA) */
  60. BYTE count /* Number of sectors to read (1..255) */
  61. )
  62. {
  63. u8 res=0;
  64. if (drv || !count)
  65. {
  66. return RES_PARERR; /*仅支持单磁盘操作,count不能等于0,否则返回参数错误*/
  67. }
  68. if(count==1) /*1个sector的读操作 */
  69. {
  70. res = SD_ReadSingleBlock(sector<<9 ,(&buff[0]));;
  71. }
  72. else /*多个sector的读操作 */
  73. {
  74. res = SD_ReadMultiBlock(sector<<9,(&buff[0]),count);
  75. }
  76. /* 处理返回值,将sddriver.c的返回值转成ff.c的返回值 */
  77. if(res == 0x00)
  78. {
  79. return RES_OK;
  80. }
  81. else
  82. {
  83. return RES_ERROR;
  84. }
  85. }
  86. /*-----------------------------------------------------------------------*/
  87. /* Write Sector(s) */
  88. #if _READONLY == 0
  89. DRESULT disk_write (
  90. BYTE drv, /* Physical drive nmuber (0..) */
  91. const BYTE *buff, /* Data to be written */
  92. DWORD sector, /* Sector address (LBA) */
  93. BYTE count /* Number of sectors to write (1..255) */
  94. )
  95. {
  96. u8 res;
  97. if (drv || !count)
  98. {
  99. return RES_PARERR; /* 仅支持单磁盘操作,count不能等于0,否则返回参数错误 */
  100. }
  101. // 读写操作
  102. if(count == 1)
  103. {
  104. res = SD_WriteSingleBlock(sector<<9, buff);
  105. }
  106. else
  107. {
  108. res = SD_WriteMultiBlock(sector<<9, buff, count);
  109. }
  110. // 返回值转换
  111. if(res == 0)
  112. {
  113. return RES_OK;
  114. }
  115. else
  116. {
  117. return RES_ERROR;
  118. }
  119. }
  120. #endif /* _READONLY */
  121. /*-----------------------------------------------------------------------*/
  122. /* Miscellaneous Functions */
  123. DRESULT disk_ioctl (
  124. BYTE drv, /* Physical drive nmuber (0..) */
  125. BYTE ctrl, /* Control code */
  126. void *buff /* Buffer to send/receive control data */
  127. )
  128. {
  129. DRESULT res;
  130. if (drv)
  131. {
  132. return RES_PARERR; /*仅支持单磁盘操作,参数错误 */
  133. }
  134. /*FATFS目前版本仅需处理CTRL_SYNC,GET_SECTOR_COUNT,GET_BLOCK_SIZ三个命令*/
  135. switch(ctrl)
  136. {
  137. case CTRL_SYNC:
  138. SD_CS_ENABLE();
  139. if(SD_WaitReady()==0)
  140. {
  141. res = RES_OK;
  142. }
  143. else
  144. {
  145. res = RES_ERROR;
  146. }
  147. SD_CS_DISABLE();
  148. break;
  149. case GET_BLOCK_SIZE:
  150. *(WORD*)buff = 512;
  151. res = RES_OK;
  152. break;
  153. case GET_SECTOR_COUNT:
  154. *(DWORD*)buff = SD_GetCapacity();
  155. res = RES_OK;
  156. break;
  157. default:
  158. res = RES_PARERR;
  159. break;
  160. }
  161. return res;
  162. }
  163. /*-----------------------------------------------------------------------*/
  164. /* User defined function to give a current time to fatfs module */
  165. /* 31-25: Year(0-127 org.1980), 24-21: Month(1-12), 20-16: Day(1-31) */
  166. /* 15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */
  167. DWORD get_fattime (void)
  168. {
  169. return 0;
  170. }
  171. //得到文件Calendar格式的建立日期,是DWORD get_fattime (void) 逆变换