user_diskio.c 6.8 KB

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