user_diskio.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 <furi_hal.h>
  36. /* Private typedef -----------------------------------------------------------*/
  37. /* Private define ------------------------------------------------------------*/
  38. /* Private variables ---------------------------------------------------------*/
  39. /* Disk status */
  40. static volatile DSTATUS Stat = STA_NOINIT;
  41. static DSTATUS User_CheckStatus(BYTE lun) {
  42. Stat = STA_NOINIT;
  43. if(BSP_SD_GetCardState() == MSD_OK) {
  44. Stat &= ~STA_NOINIT;
  45. }
  46. return Stat;
  47. }
  48. /* USER CODE END DECL */
  49. /* Private function prototypes -----------------------------------------------*/
  50. DSTATUS USER_initialize(BYTE pdrv);
  51. DSTATUS USER_status(BYTE pdrv);
  52. DRESULT USER_read(BYTE pdrv, BYTE* buff, DWORD sector, UINT count);
  53. #if _USE_WRITE == 1
  54. DRESULT USER_write(BYTE pdrv, const BYTE* buff, DWORD sector, UINT count);
  55. #endif /* _USE_WRITE == 1 */
  56. #if _USE_IOCTL == 1
  57. DRESULT USER_ioctl(BYTE pdrv, BYTE cmd, void* buff);
  58. #endif /* _USE_IOCTL == 1 */
  59. Diskio_drvTypeDef USER_Driver = {
  60. USER_initialize,
  61. USER_status,
  62. USER_read,
  63. #if _USE_WRITE
  64. USER_write,
  65. #endif /* _USE_WRITE == 1 */
  66. #if _USE_IOCTL == 1
  67. USER_ioctl,
  68. #endif /* _USE_IOCTL == 1 */
  69. };
  70. /* Private functions ---------------------------------------------------------*/
  71. /**
  72. * @brief Initializes a Drive
  73. * @param pdrv: Physical drive number (0..)
  74. * @retval DSTATUS: Operation status
  75. */
  76. DSTATUS USER_initialize(BYTE pdrv) {
  77. /* USER CODE BEGIN INIT */
  78. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_sd_fast);
  79. furi_hal_sd_spi_handle = &furi_hal_spi_bus_handle_sd_fast;
  80. DSTATUS status = User_CheckStatus(pdrv);
  81. furi_hal_sd_spi_handle = NULL;
  82. furi_hal_spi_release(&furi_hal_spi_bus_handle_sd_fast);
  83. return status;
  84. /* USER CODE END INIT */
  85. }
  86. /**
  87. * @brief Gets Disk Status
  88. * @param pdrv: Physical drive number (0..)
  89. * @retval DSTATUS: Operation status
  90. */
  91. DSTATUS USER_status(BYTE pdrv) {
  92. /* USER CODE BEGIN STATUS */
  93. return Stat;
  94. /* USER CODE END STATUS */
  95. }
  96. /**
  97. * @brief Reads Sector(s)
  98. * @param pdrv: Physical drive number (0..)
  99. * @param *buff: Data buffer to store read data
  100. * @param sector: Sector address (LBA)
  101. * @param count: Number of sectors to read (1..128)
  102. * @retval DRESULT: Operation result
  103. */
  104. DRESULT USER_read(BYTE pdrv, BYTE* buff, DWORD sector, UINT count) {
  105. /* USER CODE BEGIN READ */
  106. DRESULT res = RES_ERROR;
  107. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_sd_fast);
  108. furi_hal_sd_spi_handle = &furi_hal_spi_bus_handle_sd_fast;
  109. if(BSP_SD_ReadBlocks((uint32_t*)buff, (uint32_t)(sector), count, SD_DATATIMEOUT) == MSD_OK) {
  110. /* wait until the read operation is finished */
  111. while(BSP_SD_GetCardState() != MSD_OK) {
  112. }
  113. res = RES_OK;
  114. }
  115. furi_hal_sd_spi_handle = NULL;
  116. furi_hal_spi_release(&furi_hal_spi_bus_handle_sd_fast);
  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. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_sd_fast);
  134. furi_hal_sd_spi_handle = &furi_hal_spi_bus_handle_sd_fast;
  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. furi_hal_sd_spi_handle = NULL;
  142. furi_hal_spi_release(&furi_hal_spi_bus_handle_sd_fast);
  143. return res;
  144. /* USER CODE END WRITE */
  145. }
  146. #endif /* _USE_WRITE == 1 */
  147. /**
  148. * @brief I/O control operation
  149. * @param pdrv: Physical drive number (0..)
  150. * @param cmd: Control code
  151. * @param *buff: Buffer to send/receive control data
  152. * @retval DRESULT: Operation result
  153. */
  154. #if _USE_IOCTL == 1
  155. DRESULT USER_ioctl(BYTE pdrv, BYTE cmd, void* buff) {
  156. /* USER CODE BEGIN IOCTL */
  157. DRESULT res = RES_ERROR;
  158. BSP_SD_CardInfo CardInfo;
  159. if(Stat & STA_NOINIT) return RES_NOTRDY;
  160. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_sd_fast);
  161. furi_hal_sd_spi_handle = &furi_hal_spi_bus_handle_sd_fast;
  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. furi_hal_sd_spi_handle = NULL;
  189. furi_hal_spi_release(&furi_hal_spi_bus_handle_sd_fast);
  190. return res;
  191. /* USER CODE END IOCTL */
  192. }
  193. #endif /* _USE_IOCTL == 1 */
  194. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/