w25qxx_diskio.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file user_diskio.c
  5. * @brief This file includes a diskio driver skeleton to be completed by the user.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under Ultimate Liberty license
  13. * SLA0044, the "License"; You may not use this file except in compliance with
  14. * the License. You may obtain a copy of the License at:
  15. * www.st.com/SLA0044
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. #ifdef USE_OBSOLETE_W25Q_CODE_SECTION_0
  21. /*
  22. * Warning: the user section 0 is no more in use (starting from CubeMx version 4.16.0)
  23. * To be suppressed in the future.
  24. * Kept to ensure backward compatibility with previous CubeMx versions when
  25. * migrating projects.
  26. * User code previously added there should be copied in the new user sections before
  27. * the section contents can be deleted.
  28. */
  29. /* USER CODE BEGIN 0 */
  30. /* USER CODE END 0 */
  31. #endif
  32. /* USER CODE BEGIN DECL */
  33. /* Includes ------------------------------------------------------------------*/
  34. #include <string.h>
  35. #include "ff_gen_drv.h"
  36. #include "obj_spi_sd_stm32.h"
  37. #include "obj_spi_w25qxx.h"
  38. #include "obj_hal_w25qxx.h"
  39. #include "obj_soft_w25qxx.h"
  40. #include "func_spi_w25qxx.h"
  41. /* Private typedef -----------------------------------------------------------*/
  42. /* Private define ------------------------------------------------------------*/
  43. #define SD_CARD 0 //SD卡,卷标为0
  44. #define EX_FLASH 1 //外部spi flash,卷标为1
  45. #define EX_NAND 2 //外部nand flash,卷标为2
  46. //对于W25Q128
  47. //#define FLASH_SECTOR_SIZE 512
  48. //#define FLASH_SECTOR_COUNT 1024*16*2 //W25Q256,前25M字节给FATFS占用
  49. //#define FLASH_BLOCK_SIZE 8 //每个BLOCK有8个扇区
  50. /* Private variables ---------------------------------------------------------*/
  51. /* Disk status */
  52. static volatile DSTATUS Stat = STA_NOINIT;
  53. /* USER CODE END DECL */
  54. /* Private function prototypes -----------------------------------------------*/
  55. DSTATUS W25Q_initialize (BYTE pdrv);
  56. DSTATUS W25Q_status (BYTE pdrv);
  57. DRESULT W25Q_read (BYTE pdrv, BYTE *buff, DWORD sector, UINT count);
  58. #if _USE_WRITE == 1
  59. DRESULT W25Q_write (BYTE pdrv, const BYTE *buff, DWORD sector, UINT count);
  60. #endif /* _USE_WRITE == 1 */
  61. #if _USE_IOCTL == 1
  62. DRESULT W25Q_ioctl (BYTE pdrv, BYTE cmd, void *buff);
  63. #endif /* _USE_IOCTL == 1 */
  64. Diskio_drvTypeDef W25Q_Driver =
  65. {
  66. W25Q_initialize,
  67. W25Q_status,
  68. W25Q_read,
  69. #if _USE_WRITE
  70. W25Q_write,
  71. #endif /* _USE_WRITE == 1 */
  72. #if _USE_IOCTL == 1
  73. W25Q_ioctl,
  74. #endif /* _USE_IOCTL == 1 */
  75. };
  76. /* Private functions ---------------------------------------------------------*/
  77. /**
  78. * @brief Initializes a Drive
  79. * @param pdrv: Physical drive number (0..)
  80. * @retval DSTATUS: Operation status
  81. */
  82. DSTATUS W25Q_initialize (
  83. BYTE pdrv /* Physical drive nmuber to identify the drive */
  84. )
  85. {
  86. /* USER CODE BEGIN INIT */
  87. uint8_t res=0;
  88. func_w25q_init(); //W25QXX初始化
  89. if(res)
  90. {
  91. return STA_NOINIT;
  92. }
  93. else
  94. {
  95. return 0; //初始化成功
  96. }
  97. /* USER CODE END INIT */
  98. }
  99. /**
  100. * @brief Gets Disk Status
  101. * @param pdrv: Physical drive number (0..)
  102. * @retval DSTATUS: Operation status
  103. */
  104. DSTATUS W25Q_status (
  105. BYTE pdrv /* Physical drive number to identify the drive */
  106. )
  107. {
  108. /* USER CODE BEGIN STATUS */
  109. uint8_t res=0;
  110. if(res)
  111. {
  112. return STA_NOINIT;
  113. }
  114. else
  115. {
  116. return 0;
  117. }
  118. /* USER CODE END STATUS */
  119. }
  120. //uint8_t data_buf[3][0x1000] = {0};
  121. /**
  122. * @brief Reads Sector(s)
  123. * @param pdrv: Physical drive number (0..)
  124. * @param *buff: Data buffer to store read data
  125. * @param sector: Sector address (LBA)
  126. * @param count: Number of sectors to read (1..128)
  127. * @retval DRESULT: Operation result
  128. */
  129. DRESULT W25Q_read (
  130. BYTE pdrv, /* Physical drive nmuber to identify the drive */
  131. BYTE *buff, /* Data buffer to store read data */
  132. DWORD sector, /* Sector address in LBA */
  133. UINT count /* Number of sectors to read */
  134. )
  135. {
  136. uint8_t res=0;
  137. if (!count)return RES_PARERR;//count不能等于0,否则返回参数错误
  138. W25QXX_Read(buff, sector * SPI_FLASH_SECTOR_SIZE, count * SPI_FLASH_SECTOR_SIZE);
  139. //处理返回值,将SPI_SD_driver.c的返回值转成ff.c的返回值
  140. if(res==0x00)return RES_OK;
  141. else return RES_ERROR;
  142. }
  143. /**
  144. * @brief Writes Sector(s)
  145. * @param pdrv: Physical drive number (0..)
  146. * @param *buff: Data to be written
  147. * @param sector: Sector address (LBA)
  148. * @param count: Number of sectors to write (1..128)
  149. * @retval DRESULT: Operation result
  150. */
  151. #if _USE_WRITE == 1
  152. DRESULT W25Q_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. /* USER CODE BEGIN WRITE */
  160. /* USER CODE HERE */
  161. uint8_t res=0;
  162. if (!count)return RES_PARERR;//count不能等于0,否则返回参数错误
  163. W25QXX_Write((uint8_t*)buff, sector * SPI_FLASH_SECTOR_SIZE, count * SPI_FLASH_SECTOR_SIZE);
  164. //处理返回值,将SPI_SD_driver.c的返回值转成ff.c的返回值
  165. if(res == 0x00)
  166. {
  167. return RES_OK;
  168. }
  169. else
  170. {
  171. return RES_ERROR;
  172. }
  173. /* USER CODE END WRITE */
  174. }
  175. #endif /* _USE_WRITE == 1 */
  176. /**
  177. * @brief I/O control operation
  178. * @param pdrv: Physical drive number (0..)
  179. * @param cmd: Control code
  180. * @param *buff: Buffer to send/receive control data
  181. * @retval DRESULT: Operation result
  182. */
  183. #if _USE_IOCTL == 1
  184. DRESULT W25Q_ioctl (
  185. BYTE pdrv, /* Physical drive nmuber (0..) */
  186. BYTE cmd, /* Control code */
  187. void *buff /* Buffer to send/receive control data */
  188. )
  189. {
  190. /* USER CODE BEGIN IOCTL */
  191. uint8_t res=RES_OK;
  192. res = func_w25q_disk_ioctl(pdrv, cmd, buff);
  193. //处理返回值,将SPI_SD_driver.c的返回值转成ff.c的返回值
  194. if(res == 0x00)
  195. {
  196. return RES_OK;
  197. }
  198. else
  199. {
  200. return RES_ERROR;
  201. }
  202. /* USER CODE END IOCTL */
  203. }
  204. #endif /* _USE_IOCTL == 1 */
  205. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/