user_diskio.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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) 2020 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_USER_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 "user_diskio.h"
  35. #include "spi.h"
  36. #include "api-hal-spi.h"
  37. /* Private typedef -----------------------------------------------------------*/
  38. /* Private define ------------------------------------------------------------*/
  39. /* Private variables ---------------------------------------------------------*/
  40. /* Disk status */
  41. static volatile DSTATUS Stat = STA_NOINIT;
  42. static DSTATUS User_CheckStatus(BYTE lun) {
  43. Stat = STA_NOINIT;
  44. if(BSP_SD_GetCardState() == MSD_OK) {
  45. Stat &= ~STA_NOINIT;
  46. }
  47. return Stat;
  48. }
  49. static const ApiHalSpiDevice* sd_spi_fast_dev = &api_hal_spi_devices[ApiHalSpiDeviceIdSdCardFast];
  50. /* USER CODE END DECL */
  51. /* Private function prototypes -----------------------------------------------*/
  52. DSTATUS USER_initialize(BYTE pdrv);
  53. DSTATUS USER_status(BYTE pdrv);
  54. DRESULT USER_read(BYTE pdrv, BYTE* buff, DWORD sector, UINT count);
  55. #if _USE_WRITE == 1
  56. DRESULT USER_write(BYTE pdrv, const BYTE* buff, DWORD sector, UINT count);
  57. #endif /* _USE_WRITE == 1 */
  58. #if _USE_IOCTL == 1
  59. DRESULT USER_ioctl(BYTE pdrv, BYTE cmd, void* buff);
  60. #endif /* _USE_IOCTL == 1 */
  61. Diskio_drvTypeDef USER_Driver = {
  62. USER_initialize,
  63. USER_status,
  64. USER_read,
  65. #if _USE_WRITE
  66. USER_write,
  67. #endif /* _USE_WRITE == 1 */
  68. #if _USE_IOCTL == 1
  69. USER_ioctl,
  70. #endif /* _USE_IOCTL == 1 */
  71. };
  72. /* Private functions ---------------------------------------------------------*/
  73. /**
  74. * @brief Initializes a Drive
  75. * @param pdrv: Physical drive number (0..)
  76. * @retval DSTATUS: Operation status
  77. */
  78. DSTATUS USER_initialize(BYTE pdrv) {
  79. /* USER CODE BEGIN INIT */
  80. api_hal_spi_bus_lock(sd_spi_fast_dev->bus);
  81. api_hal_spi_bus_configure(sd_spi_fast_dev->bus, sd_spi_fast_dev->config);
  82. DSTATUS status = User_CheckStatus(pdrv);
  83. api_hal_spi_bus_unlock(sd_spi_fast_dev->bus);
  84. return status;
  85. /* USER CODE END INIT */
  86. }
  87. /**
  88. * @brief Gets Disk Status
  89. * @param pdrv: Physical drive number (0..)
  90. * @retval DSTATUS: Operation status
  91. */
  92. DSTATUS USER_status(BYTE pdrv) {
  93. /* USER CODE BEGIN STATUS */
  94. return Stat;
  95. /* USER CODE END STATUS */
  96. }
  97. /**
  98. * @brief Reads Sector(s)
  99. * @param pdrv: Physical drive number (0..)
  100. * @param *buff: Data buffer to store read data
  101. * @param sector: Sector address (LBA)
  102. * @param count: Number of sectors to read (1..128)
  103. * @retval DRESULT: Operation result
  104. */
  105. DRESULT USER_read(BYTE pdrv, BYTE* buff, DWORD sector, UINT count) {
  106. /* USER CODE BEGIN READ */
  107. DRESULT res = RES_ERROR;
  108. api_hal_spi_bus_lock(sd_spi_fast_dev->bus);
  109. api_hal_spi_bus_configure(sd_spi_fast_dev->bus, sd_spi_fast_dev->config);
  110. if(BSP_SD_ReadBlocks((uint32_t*)buff, (uint32_t)(sector), count, SD_DATATIMEOUT) == MSD_OK) {
  111. /* wait until the read operation is finished */
  112. while(BSP_SD_GetCardState() != MSD_OK) {
  113. }
  114. res = RES_OK;
  115. }
  116. api_hal_spi_bus_unlock(sd_spi_fast_dev->bus);
  117. return res;
  118. /* USER CODE END READ */
  119. }
  120. /**
  121. * @brief Writes Sector(s)
  122. * @param pdrv: Physical drive number (0..)
  123. * @param *buff: Data to be written
  124. * @param sector: Sector address (LBA)
  125. * @param count: Number of sectors to write (1..128)
  126. * @retval DRESULT: Operation result
  127. */
  128. #if _USE_WRITE == 1
  129. DRESULT USER_write(BYTE pdrv, const BYTE* buff, DWORD sector, UINT count) {
  130. /* USER CODE BEGIN WRITE */
  131. /* USER CODE HERE */
  132. DRESULT res = RES_ERROR;
  133. api_hal_spi_bus_lock(sd_spi_fast_dev->bus);
  134. api_hal_spi_bus_configure(sd_spi_fast_dev->bus, sd_spi_fast_dev->config);
  135. if(BSP_SD_WriteBlocks((uint32_t*)buff, (uint32_t)(sector), count, SD_DATATIMEOUT) == MSD_OK) {
  136. /* wait until the Write operation is finished */
  137. while(BSP_SD_GetCardState() != MSD_OK) {
  138. }
  139. res = RES_OK;
  140. }
  141. api_hal_spi_bus_unlock(sd_spi_fast_dev->bus);
  142. return res;
  143. /* USER CODE END WRITE */
  144. }
  145. #endif /* _USE_WRITE == 1 */
  146. /**
  147. * @brief I/O control operation
  148. * @param pdrv: Physical drive number (0..)
  149. * @param cmd: Control code
  150. * @param *buff: Buffer to send/receive control data
  151. * @retval DRESULT: Operation result
  152. */
  153. #if _USE_IOCTL == 1
  154. DRESULT USER_ioctl(BYTE pdrv, BYTE cmd, void* buff) {
  155. /* USER CODE BEGIN IOCTL */
  156. DRESULT res = RES_ERROR;
  157. BSP_SD_CardInfo CardInfo;
  158. if(Stat & STA_NOINIT) return RES_NOTRDY;
  159. api_hal_spi_bus_lock(sd_spi_fast_dev->bus);
  160. api_hal_spi_bus_configure(sd_spi_fast_dev->bus, sd_spi_fast_dev->config);
  161. switch(cmd) {
  162. /* Make sure that no pending write process */
  163. case CTRL_SYNC:
  164. res = RES_OK;
  165. break;
  166. /* Get number of sectors on the disk (DWORD) */
  167. case GET_SECTOR_COUNT:
  168. BSP_SD_GetCardInfo(&CardInfo);
  169. *(DWORD*)buff = CardInfo.LogBlockNbr;
  170. res = RES_OK;
  171. break;
  172. /* Get R/W sector size (WORD) */
  173. case GET_SECTOR_SIZE:
  174. BSP_SD_GetCardInfo(&CardInfo);
  175. *(WORD*)buff = CardInfo.LogBlockSize;
  176. res = RES_OK;
  177. break;
  178. /* Get erase block size in unit of sector (DWORD) */
  179. case GET_BLOCK_SIZE:
  180. BSP_SD_GetCardInfo(&CardInfo);
  181. *(DWORD*)buff = CardInfo.LogBlockSize;
  182. res = RES_OK;
  183. break;
  184. default:
  185. res = RES_PARERR;
  186. }
  187. api_hal_spi_bus_unlock(sd_spi_fast_dev->bus);
  188. return res;
  189. /* USER CODE END IOCTL */
  190. }
  191. #endif /* _USE_IOCTL == 1 */
  192. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/