diskio.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*-----------------------------------------------------------------------*/
  2. /* Low level disk I/O module skeleton for FatFs (C)ChaN, 2014 */
  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 "atadrive.h" /* Example: ATA drive control */
  11. #include "nand_flash.h"
  12. #include "sdcard.h" /* Example: MMC/SDC contorl */
  13. // #include "usbdisk.h" /* Example: USB drive control */
  14. #include "string.h"
  15. /* Definitions of physical drive number for each drive */
  16. /* 为每个设备定义一个物理编号 */
  17. #define FS_SD 0 // SD卡
  18. #define FS_NAND 1 // 预留外部SPI Flash使用
  19. // #define ATA 0 /* Example: Map ATA drive to drive number 0 */
  20. // #define MMC 1 /* Example: Map MMC/SD card to drive number 1 */
  21. #define FS_USB 2 /* Example: Map USB drive to drive number 2 */
  22. #define SD_BLOCKSIZE 512
  23. extern SD_CardInfo SDCardInfo;
  24. /*-----------------------------------------------------------------------*/
  25. /* 获取设备状态 */
  26. /*-----------------------------------------------------------------------*/
  27. DSTATUS disk_status(
  28. BYTE pdrv /* Physical drive nmuber to identify the drive */
  29. )
  30. {
  31. DSTATUS status = STA_NOINIT;
  32. switch (pdrv)
  33. {
  34. case FS_SD:
  35. status &= ~STA_NOINIT;
  36. break;
  37. case FS_NAND:
  38. status = 0;
  39. break;
  40. case FS_USB:
  41. status = STA_NOINIT;
  42. break;
  43. }
  44. return status;
  45. }
  46. /*-----------------------------------------------------------------------*/
  47. /* Inidialize a Drive */
  48. /*-----------------------------------------------------------------------*/
  49. DSTATUS disk_initialize(
  50. BYTE pdrv /* Physical drive nmuber to identify the drive */
  51. )
  52. {
  53. DSTATUS status = STA_NOINIT;
  54. switch (pdrv)
  55. {
  56. case FS_SD:
  57. if (SD_Init() == SD_OK)
  58. {
  59. status &= ~STA_NOINIT;
  60. }
  61. else
  62. {
  63. status = STA_NOINIT;
  64. }
  65. break;
  66. case FS_NAND:
  67. if (nand_flash_init() == NAND_OK)
  68. {
  69. status = RES_OK;
  70. }
  71. else
  72. {
  73. /* 如果初始化失败,请执行低级格式化 */
  74. printf("NAND_Init() Error! \r\n");
  75. status = RES_ERROR;
  76. }
  77. break;
  78. case FS_USB:
  79. // result = USB_disk_initialize();
  80. // translate the reslut code here
  81. status = STA_NOINIT;
  82. }
  83. return status;
  84. }
  85. /*-----------------------------------------------------------------------*/
  86. /* Read Sector(s) */
  87. /*-----------------------------------------------------------------------*/
  88. DRESULT disk_read(
  89. BYTE pdrv, /* Physical drive nmuber to identify the drive */
  90. BYTE *buff, /* Data buffer to store read data */
  91. DWORD sector, /* Sector address in LBA */
  92. UINT count /* Number of sectors to read */
  93. )
  94. {
  95. DRESULT status = RES_PARERR;
  96. SD_Error SD_state = SD_OK;
  97. switch (pdrv)
  98. {
  99. case FS_SD:
  100. if ((DWORD)buff & 3)
  101. {
  102. DRESULT res = RES_OK;
  103. DWORD scratch[SD_BLOCKSIZE / 4];
  104. while (count--)
  105. {
  106. res = disk_read(FS_SD, (void *)scratch, sector++, 1);
  107. if (res != RES_OK)
  108. {
  109. break;
  110. }
  111. memcpy(buff, scratch, SD_BLOCKSIZE);
  112. buff += SD_BLOCKSIZE;
  113. }
  114. return res;
  115. }
  116. SD_state = SD_ReadMultiBlocks(buff, sector * SD_BLOCKSIZE, SD_BLOCKSIZE, count);
  117. if (SD_state == SD_OK)
  118. {
  119. /* Check if the Transfer is finished */
  120. SD_state = SD_WaitReadOperation();
  121. while (SD_GetStatus() != SD_TRANSFER_OK)
  122. ;
  123. }
  124. if (SD_state != SD_OK)
  125. status = RES_PARERR;
  126. else
  127. status = RES_OK;
  128. break;
  129. case FS_NAND:
  130. if (NAND_OK == NAND_ReadMultiSectors(buff, sector, 512, count))
  131. {
  132. status = RES_OK;
  133. }
  134. else
  135. {
  136. printf("NAND_ReadMultiSectors() Error! sector = %d, count = %d \r\n", sector, count);
  137. status = RES_ERROR;
  138. }
  139. break;
  140. case FS_USB:
  141. // translate the arguments here
  142. // result = USB_disk_read(buff, sector, count);
  143. // translate the reslut code here
  144. status = RES_PARERR;
  145. }
  146. return status;
  147. }
  148. /*-----------------------------------------------------------------------*/
  149. /* Write Sector(s) */
  150. /*-----------------------------------------------------------------------*/
  151. #if _USE_WRITE
  152. DRESULT disk_write(
  153. BYTE pdrv, /* Physical drive nmuber to identify the drive */
  154. const BYTE *buff, /* Data to be written */
  155. DWORD sector, /* Sector address in LBA */
  156. UINT count /* Number of sectors to write */
  157. )
  158. {
  159. DRESULT status = RES_PARERR;
  160. SD_Error SD_state = SD_OK;
  161. if (!count)
  162. {
  163. return RES_PARERR; /* Check parameter */
  164. }
  165. switch (pdrv)
  166. {
  167. case FS_SD: /* SD CARD */
  168. if ((DWORD)buff & 3)
  169. {
  170. DRESULT res = RES_OK;
  171. DWORD scratch[SD_BLOCKSIZE / 4];
  172. while (count--)
  173. {
  174. memcpy(scratch, buff, SD_BLOCKSIZE);
  175. res = disk_write(FS_SD, (void *)scratch, sector++, 1);
  176. if (res != RES_OK)
  177. {
  178. break;
  179. }
  180. buff += SD_BLOCKSIZE;
  181. }
  182. return res;
  183. }
  184. SD_state = SD_WriteMultiBlocks((uint8_t *)buff, sector * SD_BLOCKSIZE, SD_BLOCKSIZE, count);
  185. if (SD_state == SD_OK)
  186. {
  187. /* Check if the Transfer is finished */
  188. SD_state = SD_WaitWriteOperation();
  189. /* Wait until end of DMA transfer */
  190. while (SD_GetStatus() != SD_TRANSFER_OK)
  191. ;
  192. }
  193. if (SD_state != SD_OK)
  194. status = RES_PARERR;
  195. else
  196. status = RES_OK;
  197. break;
  198. case FS_NAND:
  199. if (NAND_OK == NAND_WriteMultiSectors((uint8_t *)buff, sector, 512, count))
  200. {
  201. status = RES_OK;
  202. }
  203. else
  204. {
  205. printf("NAND_ReadMultiSectors() Error! sector = %d, count = %d \r\n", sector, count);
  206. status = RES_ERROR;
  207. }
  208. break;
  209. case FS_USB:
  210. // translate the arguments here
  211. // result = USB_disk_write(buff, sector, count);
  212. // translate the reslut code here
  213. status = RES_PARERR;
  214. break;
  215. }
  216. return status;
  217. }
  218. #endif
  219. /*-----------------------------------------------------------------------*/
  220. /* Miscellaneous Functions */
  221. /*-----------------------------------------------------------------------*/
  222. #if _USE_IOCTL
  223. DRESULT disk_ioctl(
  224. BYTE pdrv, /* Physical drive nmuber (0..) */
  225. BYTE cmd, /* Control code */
  226. void *buff /* Buffer to send/receive control data */
  227. )
  228. {
  229. DRESULT status = RES_PARERR;
  230. switch (pdrv)
  231. {
  232. case FS_SD: /* SD CARD */
  233. switch (cmd)
  234. {
  235. // Get R/W sector size (WORD)
  236. case GET_SECTOR_SIZE:
  237. *(WORD *)buff = SD_BLOCKSIZE;
  238. break;
  239. // Get erase block size in unit of sector (DWORD)
  240. case GET_BLOCK_SIZE:
  241. *(DWORD *)buff = 1; // SDCardInfo.CardBlockSize;
  242. break;
  243. case GET_SECTOR_COUNT:
  244. *(DWORD *)buff = SDCardInfo.CardCapacity / SDCardInfo.CardBlockSize;
  245. break;
  246. case CTRL_SYNC:
  247. break;
  248. }
  249. status = RES_OK;
  250. break;
  251. case FS_NAND:
  252. status = RES_OK;
  253. break;
  254. case FS_USB:
  255. // Process of the command the USB drive
  256. status = RES_PARERR;
  257. break;
  258. }
  259. return status;
  260. }
  261. /*
  262. *********************************************************************************************************
  263. * 函 数 名: get_fattime
  264. * 功能说明: 获得系统时间,用于改写文件的创建和修改时间。
  265. * 形 参:无
  266. * 返 回 值: 无
  267. *********************************************************************************************************
  268. */
  269. // __weak DWORD get_fattime(void)
  270. // {
  271. // /* 如果有全局时钟,可按下面的格式进行时钟转换. 这个例子是2013-01-01 00:00:00 */
  272. // return ((DWORD)(2013 - 1980) << 25) /* Year = 2013 */
  273. // | ((DWORD)1 << 21) /* Month = 1 */
  274. // | ((DWORD)1 << 16) /* Day_m = 1*/
  275. // | ((DWORD)0 << 11) /* Hour = 0 */
  276. // | ((DWORD)0 << 5) /* Min = 0 */
  277. // | ((DWORD)0 >> 1); /* Sec = 0 */
  278. // }
  279. #endif