diskio.c 11 KB

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