user_diskio.c 6.3 KB

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