diskio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. #ifdef FS_USB
  16. #include "usbh_bsp_msc.h" /* 底层驱动 */
  17. #endif
  18. /* Definitions of physical drive number for each drive */
  19. /* 为每个设备定义一个物理编号 */
  20. #define FS_SD 0 // SD卡
  21. #define FS_NAND 1 // 预留外部SPI Flash使用
  22. // #define ATA 0 /* Example: Map ATA drive to drive number 0 */
  23. // #define MMC 1 /* Example: Map MMC/SD card to drive number 1 */
  24. // #define FS_USB 2 /* Example: Map USB drive to drive number 2 */
  25. #define SD_BLOCKSIZE 512
  26. extern SD_CardInfo SDCardInfo;
  27. /*-----------------------------------------------------------------------*/
  28. /* 获取设备状态 */
  29. /*-----------------------------------------------------------------------*/
  30. DSTATUS disk_status(
  31. BYTE pdrv /* Physical drive nmuber to identify the drive */
  32. )
  33. {
  34. DSTATUS status = STA_NOINIT;
  35. switch (pdrv)
  36. {
  37. case FS_SD:
  38. status &= ~STA_NOINIT;
  39. break;
  40. case FS_NAND:
  41. status = 0;
  42. break;
  43. #ifdef FS_USB
  44. case FS_USB:
  45. status = 0;
  46. break;
  47. #endif
  48. }
  49. return status;
  50. }
  51. /*-----------------------------------------------------------------------*/
  52. /* Inidialize a Drive */
  53. /*-----------------------------------------------------------------------*/
  54. DSTATUS disk_initialize(
  55. BYTE pdrv /* Physical drive nmuber to identify the drive */
  56. )
  57. {
  58. DSTATUS status = STA_NOINIT;
  59. switch (pdrv)
  60. {
  61. case FS_SD:
  62. if (SD_Init() == SD_OK)
  63. {
  64. status &= ~STA_NOINIT;
  65. }
  66. else
  67. {
  68. status = STA_NOINIT;
  69. }
  70. break;
  71. case FS_NAND:
  72. if (nand_flash_init() == NAND_OK)
  73. {
  74. status = RES_OK;
  75. }
  76. else
  77. {
  78. /* 如果初始化失败,请执行低级格式化 */
  79. printf("NAND_Init() Error! \r\n");
  80. status = RES_ERROR;
  81. }
  82. break;
  83. #ifdef FS_USB
  84. case FS_USB:
  85. if (HCD_IsDeviceConnected(&USB_OTG_Core))
  86. {
  87. status &= ~STA_NOINIT;
  88. }
  89. break;
  90. #endif
  91. }
  92. return status;
  93. }
  94. /*-----------------------------------------------------------------------*/
  95. /* Read Sector(s) */
  96. /*-----------------------------------------------------------------------*/
  97. DRESULT disk_read(
  98. BYTE pdrv, /* Physical drive nmuber to identify the drive */
  99. BYTE *buff, /* Data buffer to store read data */
  100. DWORD sector, /* Sector address in LBA */
  101. UINT count /* Number of sectors to read */
  102. )
  103. {
  104. DRESULT status = RES_PARERR;
  105. SD_Error SD_state = SD_OK;
  106. switch (pdrv)
  107. {
  108. case FS_SD:
  109. if ((DWORD)buff & 3)
  110. {
  111. DRESULT res = RES_OK;
  112. DWORD scratch[SD_BLOCKSIZE / 4];
  113. while (count--)
  114. {
  115. res = disk_read(FS_SD, (void *)scratch, sector++, 1);
  116. if (res != RES_OK)
  117. {
  118. break;
  119. }
  120. memcpy(buff, scratch, SD_BLOCKSIZE);
  121. buff += SD_BLOCKSIZE;
  122. }
  123. return res;
  124. }
  125. SD_state = SD_ReadMultiBlocks(buff, sector * SD_BLOCKSIZE, SD_BLOCKSIZE, count);
  126. if (SD_state == SD_OK)
  127. {
  128. /* Check if the Transfer is finished */
  129. SD_state = SD_WaitReadOperation();
  130. while (SD_GetStatus() != SD_TRANSFER_OK)
  131. ;
  132. }
  133. if (SD_state != SD_OK)
  134. status = RES_PARERR;
  135. else
  136. status = RES_OK;
  137. break;
  138. case FS_NAND:
  139. if (NAND_OK == NAND_ReadMultiSectors(buff, sector, 512, count))
  140. {
  141. status = RES_OK;
  142. }
  143. else
  144. {
  145. printf("NAND_ReadMultiSectors() Error! sector = %d, count = %d \r\n", sector, count);
  146. status = RES_ERROR;
  147. }
  148. break;
  149. #ifdef FS_USB
  150. case FS_USB:
  151. {
  152. BYTE res = USBH_MSC_OK;
  153. if (HCD_IsDeviceConnected(&USB_OTG_Core))
  154. {
  155. do
  156. {
  157. res = USBH_MSC_Read10(&USB_OTG_Core, buff, sector, 512 * count);
  158. USBH_MSC_HandleBOTXfer(&USB_OTG_Core, &USB_Host);
  159. if (!HCD_IsDeviceConnected(&USB_OTG_Core))
  160. {
  161. break;
  162. }
  163. } while (res == USBH_MSC_BUSY);
  164. }
  165. if (res == USBH_MSC_OK)
  166. {
  167. status = RES_OK;
  168. }
  169. else
  170. {
  171. status = RES_ERROR;
  172. }
  173. }
  174. #endif
  175. break;
  176. }
  177. return status;
  178. }
  179. /*-----------------------------------------------------------------------*/
  180. /* Write Sector(s) */
  181. /*-----------------------------------------------------------------------*/
  182. #if _USE_WRITE
  183. DRESULT disk_write(
  184. BYTE pdrv, /* Physical drive nmuber to identify the drive */
  185. const BYTE *buff, /* Data to be written */
  186. DWORD sector, /* Sector address in LBA */
  187. UINT count /* Number of sectors to write */
  188. )
  189. {
  190. DRESULT status = RES_PARERR;
  191. SD_Error SD_state = SD_OK;
  192. if (!count)
  193. {
  194. return RES_PARERR; /* Check parameter */
  195. }
  196. switch (pdrv)
  197. {
  198. case FS_SD: /* SD CARD */
  199. if ((DWORD)buff & 3)
  200. {
  201. DRESULT res = RES_OK;
  202. DWORD scratch[SD_BLOCKSIZE / 4];
  203. while (count--)
  204. {
  205. memcpy(scratch, buff, SD_BLOCKSIZE);
  206. res = disk_write(FS_SD, (void *)scratch, sector++, 1);
  207. if (res != RES_OK)
  208. {
  209. break;
  210. }
  211. buff += SD_BLOCKSIZE;
  212. }
  213. return res;
  214. }
  215. SD_state = SD_WriteMultiBlocks((uint8_t *)buff, sector * SD_BLOCKSIZE, SD_BLOCKSIZE, count);
  216. if (SD_state == SD_OK)
  217. {
  218. /* Check if the Transfer is finished */
  219. SD_state = SD_WaitWriteOperation();
  220. /* Wait until end of DMA transfer */
  221. while (SD_GetStatus() != SD_TRANSFER_OK)
  222. ;
  223. }
  224. if (SD_state != SD_OK)
  225. status = RES_PARERR;
  226. else
  227. status = RES_OK;
  228. break;
  229. case FS_NAND:
  230. if (NAND_OK == NAND_WriteMultiSectors((uint8_t *)buff, sector, 512, count))
  231. {
  232. status = RES_OK;
  233. }
  234. else
  235. {
  236. printf("NAND_ReadMultiSectors() Error! sector = %d, count = %d \r\n", sector, count);
  237. status = RES_ERROR;
  238. }
  239. break;
  240. #ifdef FS_USB
  241. case FS_USB:
  242. {
  243. BYTE res = USBH_MSC_OK;
  244. if (HCD_IsDeviceConnected(&USB_OTG_Core))
  245. {
  246. do
  247. {
  248. res = USBH_MSC_Write10(&USB_OTG_Core, (BYTE *)buff, sector, 512 * count);
  249. USBH_MSC_HandleBOTXfer(&USB_OTG_Core, &USB_Host);
  250. if (!HCD_IsDeviceConnected(&USB_OTG_Core))
  251. {
  252. break;
  253. }
  254. } while (res == USBH_MSC_BUSY);
  255. }
  256. if (res == USBH_MSC_OK)
  257. {
  258. status = RES_OK;
  259. }
  260. else
  261. {
  262. status = RES_ERROR;
  263. }
  264. }
  265. #endif
  266. break;
  267. }
  268. return status;
  269. }
  270. #endif
  271. /*-----------------------------------------------------------------------*/
  272. /* Miscellaneous Functions */
  273. /*-----------------------------------------------------------------------*/
  274. #if _USE_IOCTL
  275. DRESULT disk_ioctl(
  276. BYTE pdrv, /* Physical drive nmuber (0..) */
  277. BYTE cmd, /* Control code */
  278. void *buff /* Buffer to send/receive control data */
  279. )
  280. {
  281. DRESULT status = RES_PARERR;
  282. switch (pdrv)
  283. {
  284. case FS_SD: /* SD CARD */
  285. switch (cmd)
  286. {
  287. // Get R/W sector size (WORD)
  288. case GET_SECTOR_SIZE:
  289. *(WORD *)buff = SD_BLOCKSIZE;
  290. break;
  291. // Get erase block size in unit of sector (DWORD)
  292. case GET_BLOCK_SIZE:
  293. *(DWORD *)buff = 1; // SDCardInfo.CardBlockSize;
  294. break;
  295. case GET_SECTOR_COUNT:
  296. *(DWORD *)buff = SDCardInfo.CardCapacity / SDCardInfo.CardBlockSize;
  297. break;
  298. case CTRL_SYNC:
  299. break;
  300. }
  301. status = RES_OK;
  302. break;
  303. case FS_NAND:
  304. status = RES_OK;
  305. break;
  306. #ifdef FS_USB
  307. case FS_USB:
  308. // if (Stat & STA_NOINIT) return RES_NOTRDY;
  309. switch (cmd)
  310. {
  311. case CTRL_SYNC: /* Make sure that no pending write process */
  312. status = RES_OK;
  313. break;
  314. case GET_SECTOR_COUNT: /* Get number of sectors on the disk (DWORD) */
  315. *(DWORD *)buff = (DWORD)USBH_MSC_Param.MSCapacity;
  316. status = RES_OK;
  317. break;
  318. case GET_SECTOR_SIZE: /* Get R/W sector size (WORD) */
  319. *(WORD *)buff = 512;
  320. status = RES_OK;
  321. break;
  322. case GET_BLOCK_SIZE: /* Get erase block size in unit of sector (DWORD) */
  323. *(DWORD *)buff = 512;
  324. status = RES_OK;
  325. break;
  326. default:
  327. status = RES_PARERR;
  328. break;
  329. }
  330. #endif
  331. break;
  332. }
  333. return status;
  334. }
  335. /*
  336. *********************************************************************************************************
  337. * 函 数 名: get_fattime
  338. * 功能说明: 获得系统时间,用于改写文件的创建和修改时间。
  339. * 形 参:无
  340. * 返 回 值: 无
  341. *********************************************************************************************************
  342. */
  343. // __weak DWORD get_fattime(void)
  344. // {
  345. // /* 如果有全局时钟,可按下面的格式进行时钟转换. 这个例子是2013-01-01 00:00:00 */
  346. // return ((DWORD)(2013 - 1980) << 25) /* Year = 2013 */
  347. // | ((DWORD)1 << 21) /* Month = 1 */
  348. // | ((DWORD)1 << 16) /* Day_m = 1*/
  349. // | ((DWORD)0 << 11) /* Hour = 0 */
  350. // | ((DWORD)0 << 5) /* Min = 0 */
  351. // | ((DWORD)0 >> 1); /* Sec = 0 */
  352. // }
  353. #endif