diskio.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 "stdio.h"
  10. #include "diskio.h" /* FatFs lower layer API */
  11. #include "nandflash.h"
  12. //#define ff_printf printf
  13. #define ff_printf(...)
  14. #define SECTOR_SIZE 512 /* SD卡扇区大小必须为512 */
  15. /*-----------------------------------------------------------------------*/
  16. /* Get Drive Status */
  17. /*-----------------------------------------------------------------------*/
  18. DSTATUS disk_status (
  19. BYTE pdrv /* Physical drive nmuber to identify the drive */
  20. )
  21. {
  22. DSTATUS stat = STA_NOINIT;
  23. switch (pdrv)
  24. {
  25. case FS_NAND :
  26. stat = 0;
  27. break;
  28. default:
  29. break;
  30. }
  31. return stat;
  32. }
  33. /*-----------------------------------------------------------------------*/
  34. /* Inidialize a Drive */
  35. /*-----------------------------------------------------------------------*/
  36. DSTATUS disk_initialize (
  37. BYTE pdrv /* Physical drive nmuber to identify the drive */
  38. )
  39. {
  40. DSTATUS stat = STA_NOINIT;
  41. switch (pdrv)
  42. {
  43. case FS_NAND : /* NAND Flash */
  44. if (1) //修改为无需判断
  45. {
  46. stat = RES_OK;
  47. }
  48. else
  49. {
  50. /* 如果初始化失败,请执行低级格式化 */
  51. ff_printf("NAND_Init() Error! \r\n");
  52. stat = RES_ERROR;
  53. }
  54. break;
  55. default :
  56. break;
  57. }
  58. return stat;
  59. }
  60. /*-----------------------------------------------------------------------*/
  61. /* Read Sector(s) */
  62. /*-----------------------------------------------------------------------*/
  63. DRESULT disk_read (
  64. BYTE pdrv, /* Physical drive nmuber to identify the drive */
  65. BYTE *buff, /* Data buffer to store read data */
  66. DWORD sector, /* Sector address in LBA */
  67. UINT count /* Number of sectors to read */
  68. )
  69. {
  70. DRESULT res = RES_OK;
  71. switch (pdrv)
  72. {
  73. case FS_NAND :
  74. if (NAND_OK == NAND_ReadMultiSectors(buff, sector, 512, count))
  75. {
  76. res = RES_OK;
  77. }
  78. else
  79. {
  80. ff_printf("NAND_ReadMultiSectors() Error! sector = %d, count = %d \r\n", sector, count);
  81. res = RES_ERROR;
  82. }
  83. break;
  84. default:
  85. res = RES_PARERR;
  86. break;
  87. }
  88. return res;
  89. }
  90. /*-----------------------------------------------------------------------*/
  91. /* Write Sector(s) */
  92. /*-----------------------------------------------------------------------*/
  93. #if _USE_WRITE
  94. DRESULT disk_write (
  95. BYTE pdrv, /* Physical drive nmuber to identify the drive */
  96. const BYTE *buff, /* Data to be written */
  97. DWORD sector, /* Sector address in LBA */
  98. UINT count /* Number of sectors to write */
  99. )
  100. {
  101. DRESULT res = RES_OK;
  102. switch (pdrv)
  103. {
  104. case FS_NAND :
  105. if (NAND_OK == NAND_WriteMultiSectors((uint8_t *)buff, sector, 512, count))
  106. {
  107. res = RES_OK;
  108. }
  109. else
  110. {
  111. printf("NAND_WriteMultiSectors() Error! sector = %d, count = %d \r\n", sector, count);
  112. res = RES_ERROR;
  113. }
  114. break;
  115. default:
  116. res = RES_PARERR;
  117. break;
  118. }
  119. return res;
  120. }
  121. #endif
  122. /*-----------------------------------------------------------------------*/
  123. /* Miscellaneous Functions */
  124. /*-----------------------------------------------------------------------*/
  125. #if _USE_IOCTL
  126. DRESULT disk_ioctl (
  127. BYTE pdrv, /* Physical drive nmuber (0..) */
  128. BYTE cmd, /* Control code */
  129. void *buff /* Buffer to send/receive control data */
  130. )
  131. {
  132. DRESULT res = RES_PARERR;
  133. switch (pdrv)
  134. {
  135. case FS_NAND :
  136. res = RES_ERROR;
  137. switch (cmd)
  138. {
  139. case CTRL_SYNC : /* Make sure that no pending write process */
  140. res = RES_OK;
  141. break;
  142. case GET_SECTOR_COUNT : /* Get number of sectors on the disk (DWORD) */
  143. //*(DWORD*)buff = 262144;
  144. //*block_num = (NAND_PAGE_SIZE / 512) * NAND_BLOCK_SIZE * NAND_ZONE_SIZE * NAND_MAX_ZONE;
  145. *(DWORD*)buff = NAND_FormatCapacity() / 512; /* 必须为可用的扇区个数,不是芯片的理论容量 */
  146. res = RES_OK;
  147. break;
  148. case GET_SECTOR_SIZE : /* Get R/W sector size (WORD) */
  149. *(WORD*)buff = 512;
  150. res = RES_OK;
  151. break;
  152. case GET_BLOCK_SIZE : /* Get erase block size in unit of sector (DWORD) */\
  153. *(DWORD*)buff = 512;
  154. res = RES_OK;
  155. break;
  156. default:
  157. res = RES_PARERR;
  158. break;
  159. }
  160. break;
  161. default:
  162. res = RES_PARERR;
  163. break;
  164. }
  165. return res;
  166. }
  167. #endif
  168. /*
  169. *********************************************************************************************************
  170. * 函 数 名: get_fattime
  171. * 功能说明: 获得系统时间,用于改写文件的创建和修改时间。客户可以自行移植和系统的RTC关联起来
  172. * 形 参: 无
  173. * 返 回 值: 无
  174. *********************************************************************************************************
  175. */
  176. DWORD get_fattime (void)
  177. {
  178. /* 如果有全局时钟,可按下面的格式进行时钟转换. 这个例子是2014-07-02 00:00:00 */
  179. #if 0
  180. RTC_ReadClock();
  181. return ((DWORD)(g_tRTC.Year - 1980) << 25) /* Year */
  182. | ((DWORD)g_tRTC.Mon << 21) /* Month */
  183. | ((DWORD)g_tRTC.Day << 16) /* Day_m 1*/
  184. | ((DWORD)g_tRTC.Hour << 11) /* Hour */
  185. | ((DWORD)g_tRTC.Min << 5) /* Min */
  186. | ((DWORD)g_tRTC.Sec >> 1); /* Sec */
  187. #else
  188. return ((DWORD)(2014 - 1980) << 25) /* Year = 2014 */
  189. | ((DWORD)7 << 21) /* Month = 7 */
  190. | ((DWORD)2 << 16) /* Day_m = 2*/
  191. | ((DWORD)0 << 11) /* Hour = 0 */
  192. | ((DWORD)0 << 5) /* Min = 0 */
  193. | ((DWORD)0 >> 1); /* Sec = 0 */
  194. #endif
  195. }