user_diskio.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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(sd_get_card_state() == SdSpiStatusOK) {
  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(sd_read_blocks((uint32_t*)buff, (uint32_t)(sector), count, SD_TIMEOUT_MS) ==
  113. SdSpiStatusOK) {
  114. FuriHalCortexTimer timer = furi_hal_cortex_timer_get(SD_TIMEOUT_MS * 1000);
  115. /* wait until the read operation is finished */
  116. res = RES_OK;
  117. while(sd_get_card_state() != SdSpiStatusOK) {
  118. if(furi_hal_cortex_timer_is_expired(timer)) {
  119. res = RES_ERROR;
  120. break;
  121. }
  122. }
  123. }
  124. furi_hal_sd_spi_handle = NULL;
  125. furi_hal_spi_release(&furi_hal_spi_bus_handle_sd_fast);
  126. return res;
  127. /* USER CODE END READ */
  128. }
  129. /**
  130. * @brief Writes Sector(s)
  131. * @param pdrv: Physical drive number (0..)
  132. * @param *buff: Data to be written
  133. * @param sector: Sector address (LBA)
  134. * @param count: Number of sectors to write (1..128)
  135. * @retval DRESULT: Operation result
  136. */
  137. #if _USE_WRITE == 1
  138. DRESULT USER_write(BYTE pdrv, const BYTE* buff, DWORD sector, UINT count) {
  139. /* USER CODE BEGIN WRITE */
  140. /* USER CODE HERE */
  141. UNUSED(pdrv);
  142. DRESULT res = RES_ERROR;
  143. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_sd_fast);
  144. furi_hal_sd_spi_handle = &furi_hal_spi_bus_handle_sd_fast;
  145. if(sd_write_blocks((uint32_t*)buff, (uint32_t)(sector), count, SD_TIMEOUT_MS) ==
  146. SdSpiStatusOK) {
  147. FuriHalCortexTimer timer = furi_hal_cortex_timer_get(SD_TIMEOUT_MS * 1000);
  148. /* wait until the Write operation is finished */
  149. res = RES_OK;
  150. while(sd_get_card_state() != SdSpiStatusOK) {
  151. if(furi_hal_cortex_timer_is_expired(timer)) {
  152. res = RES_ERROR;
  153. break;
  154. }
  155. }
  156. }
  157. furi_hal_sd_spi_handle = NULL;
  158. furi_hal_spi_release(&furi_hal_spi_bus_handle_sd_fast);
  159. return res;
  160. /* USER CODE END WRITE */
  161. }
  162. #endif /* _USE_WRITE == 1 */
  163. /**
  164. * @brief I/O control operation
  165. * @param pdrv: Physical drive number (0..)
  166. * @param cmd: Control code
  167. * @param *buff: Buffer to send/receive control data
  168. * @retval DRESULT: Operation result
  169. */
  170. #if _USE_IOCTL == 1
  171. DRESULT USER_ioctl(BYTE pdrv, BYTE cmd, void* buff) {
  172. /* USER CODE BEGIN IOCTL */
  173. UNUSED(pdrv);
  174. DRESULT res = RES_ERROR;
  175. SD_CardInfo CardInfo;
  176. if(Stat & STA_NOINIT) return RES_NOTRDY;
  177. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_sd_fast);
  178. furi_hal_sd_spi_handle = &furi_hal_spi_bus_handle_sd_fast;
  179. switch(cmd) {
  180. /* Make sure that no pending write process */
  181. case CTRL_SYNC:
  182. res = RES_OK;
  183. break;
  184. /* Get number of sectors on the disk (DWORD) */
  185. case GET_SECTOR_COUNT:
  186. sd_get_card_info(&CardInfo);
  187. *(DWORD*)buff = CardInfo.LogBlockNbr;
  188. res = RES_OK;
  189. break;
  190. /* Get R/W sector size (WORD) */
  191. case GET_SECTOR_SIZE:
  192. sd_get_card_info(&CardInfo);
  193. *(WORD*)buff = CardInfo.LogBlockSize;
  194. res = RES_OK;
  195. break;
  196. /* Get erase block size in unit of sector (DWORD) */
  197. case GET_BLOCK_SIZE:
  198. sd_get_card_info(&CardInfo);
  199. *(DWORD*)buff = CardInfo.LogBlockSize;
  200. res = RES_OK;
  201. break;
  202. default:
  203. res = RES_PARERR;
  204. }
  205. furi_hal_sd_spi_handle = NULL;
  206. furi_hal_spi_release(&furi_hal_spi_bus_handle_sd_fast);
  207. return res;
  208. /* USER CODE END IOCTL */
  209. }
  210. #endif /* _USE_IOCTL == 1 */
  211. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/