diskio.c 8.9 KB

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