sd_diskio.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /**
  2. ******************************************************************************
  3. * @file sd_diskio.c (based on sd_diskio_template.c v2.0.2 as "Use dma template" is disabled)
  4. * @brief SD Disk I/O driver
  5. ******************************************************************************
  6. * This notice applies to any and all portions of this file
  7. * that are not between comment pairs USER CODE BEGIN and
  8. * USER CODE END. Other portions of this file, whether
  9. * inserted by the user or by software development tools
  10. * are owned by their respective copyright owners.
  11. *
  12. * Copyright (c) 2018 STMicroelectronics International N.V.
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted, provided that the following conditions are met:
  17. *
  18. * 1. Redistribution of source code must retain the above copyright notice,
  19. * this list of conditions and the following disclaimer.
  20. * 2. Redistributions in binary form must reproduce the above copyright notice,
  21. * this list of conditions and the following disclaimer in the documentation
  22. * and/or other materials provided with the distribution.
  23. * 3. Neither the name of STMicroelectronics nor the names of other
  24. * contributors to this software may be used to endorse or promote products
  25. * derived from this software without specific written permission.
  26. * 4. This software, including modifications and/or derivative works of this
  27. * software, must execute solely and exclusively on microcontroller or
  28. * microprocessor devices manufactured by or for STMicroelectronics.
  29. * 5. Redistribution and use of this software other than as permitted under
  30. * this license is void and will automatically terminate your rights under
  31. * this license.
  32. *
  33. * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  34. * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  35. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  36. * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  37. * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  38. * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  39. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  41. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  42. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  43. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  44. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. ******************************************************************************
  47. */
  48. /* USER CODE BEGIN firstSection */
  49. /* can be used to modify / undefine following code or add new definitions */
  50. /* USER CODE END firstSection*/
  51. /* Includes ------------------------------------------------------------------*/
  52. #include "ff_gen_drv.h"
  53. #include "sd_diskio.h"
  54. /* Private typedef -----------------------------------------------------------*/
  55. /* Private define ------------------------------------------------------------*/
  56. /* use the default SD timout as defined in the platform BSP driver*/
  57. #if defined(SDMMC_DATATIMEOUT)
  58. #define SD_TIMEOUT SDMMC_DATATIMEOUT
  59. #elif defined(SD_DATATIMEOUT)
  60. #define SD_TIMEOUT SD_DATATIMEOUT
  61. #else
  62. #define SD_TIMEOUT 30 * 1000
  63. #endif
  64. #define SD_DEFAULT_BLOCK_SIZE 512
  65. /*
  66. * Depending on the use case, the SD card initialization could be done at the
  67. * application level: if it is the case define the flag below to disable
  68. * the BSP_SD_Init() call in the SD_Initialize() and add a call to
  69. * BSP_SD_Init() elsewhere in the application.
  70. */
  71. /* USER CODE BEGIN disableSDInit */
  72. /* #define DISABLE_SD_INIT */
  73. /* USER CODE END disableSDInit */
  74. /* Private variables ---------------------------------------------------------*/
  75. /* Disk status */
  76. static volatile DSTATUS Stat = STA_NOINIT;
  77. /* Private function prototypes -----------------------------------------------*/
  78. static DSTATUS SD_CheckStatus(BYTE lun);
  79. DSTATUS SD_initialize (BYTE);
  80. DSTATUS SD_status (BYTE);
  81. DRESULT SD_read (BYTE, BYTE*, DWORD, UINT);
  82. #if _USE_WRITE == 1
  83. DRESULT SD_write (BYTE, const BYTE*, DWORD, UINT);
  84. #endif /* _USE_WRITE == 1 */
  85. #if _USE_IOCTL == 1
  86. DRESULT SD_ioctl (BYTE, BYTE, void*);
  87. #endif /* _USE_IOCTL == 1 */
  88. const Diskio_drvTypeDef SD_Driver =
  89. {
  90. SD_initialize,
  91. SD_status,
  92. SD_read,
  93. #if _USE_WRITE == 1
  94. SD_write,
  95. #endif /* _USE_WRITE == 1 */
  96. #if _USE_IOCTL == 1
  97. SD_ioctl,
  98. #endif /* _USE_IOCTL == 1 */
  99. };
  100. /* USER CODE BEGIN beforeFunctionSection */
  101. /* can be used to modify / undefine following code or add new code */
  102. /* USER CODE END beforeFunctionSection */
  103. /* Private functions ---------------------------------------------------------*/
  104. static DSTATUS SD_CheckStatus(BYTE lun)
  105. {
  106. Stat = STA_NOINIT;
  107. if(BSP_SD_GetCardState() == MSD_OK)
  108. {
  109. Stat &= ~STA_NOINIT;
  110. }
  111. return Stat;
  112. }
  113. /**
  114. * @brief Initializes a Drive
  115. * @param lun : not used
  116. * @retval DSTATUS: Operation status
  117. */
  118. DSTATUS SD_initialize(BYTE lun)
  119. {
  120. Stat = STA_NOINIT;
  121. #if !defined(DISABLE_SD_INIT)
  122. if(BSP_SD_Init() == MSD_OK)
  123. {
  124. Stat = SD_CheckStatus(lun);
  125. }
  126. #else
  127. Stat = SD_CheckStatus(lun);
  128. #endif
  129. return Stat;
  130. }
  131. /**
  132. * @brief Gets Disk Status
  133. * @param lun : not used
  134. * @retval DSTATUS: Operation status
  135. */
  136. DSTATUS SD_status(BYTE lun)
  137. {
  138. return SD_CheckStatus(lun);
  139. }
  140. /* USER CODE BEGIN beforeReadSection */
  141. /* can be used to modify previous code / undefine following code / add new code */
  142. /* USER CODE END beforeReadSection */
  143. /**
  144. * @brief Reads Sector(s)
  145. * @param lun : not used
  146. * @param *buff: Data buffer to store read data
  147. * @param sector: Sector address (LBA)
  148. * @param count: Number of sectors to read (1..128)
  149. * @retval DRESULT: Operation result
  150. */
  151. DRESULT SD_read(BYTE lun, BYTE *buff, DWORD sector, UINT count)
  152. {
  153. DRESULT res = RES_ERROR;
  154. if(BSP_SD_ReadBlocks((uint32_t*)buff,
  155. (uint32_t) (sector),
  156. count, SD_TIMEOUT) == MSD_OK)
  157. {
  158. /* wait until the read operation is finished */
  159. while(BSP_SD_GetCardState()!= MSD_OK)
  160. {
  161. }
  162. res = RES_OK;
  163. }
  164. return res;
  165. }
  166. /* USER CODE BEGIN beforeWriteSection */
  167. /* can be used to modify previous code / undefine following code / add new code */
  168. /* USER CODE END beforeWriteSection */
  169. /**
  170. * @brief Writes Sector(s)
  171. * @param lun : not used
  172. * @param *buff: Data to be written
  173. * @param sector: Sector address (LBA)
  174. * @param count: Number of sectors to write (1..128)
  175. * @retval DRESULT: Operation result
  176. */
  177. #if _USE_WRITE == 1
  178. DRESULT SD_write(BYTE lun, const BYTE *buff, DWORD sector, UINT count)
  179. {
  180. DRESULT res = RES_ERROR;
  181. if(BSP_SD_WriteBlocks((uint32_t*)buff,
  182. (uint32_t)(sector),
  183. count, SD_TIMEOUT) == MSD_OK)
  184. {
  185. /* wait until the Write operation is finished */
  186. while(BSP_SD_GetCardState() != MSD_OK)
  187. {
  188. }
  189. res = RES_OK;
  190. }
  191. return res;
  192. }
  193. #endif /* _USE_WRITE == 1 */
  194. /* USER CODE BEGIN beforeIoctlSection */
  195. /* can be used to modify previous code / undefine following code / add new code */
  196. /* USER CODE END beforeIoctlSection */
  197. /**
  198. * @brief I/O control operation
  199. * @param lun : not used
  200. * @param cmd: Control code
  201. * @param *buff: Buffer to send/receive control data
  202. * @retval DRESULT: Operation result
  203. */
  204. #if _USE_IOCTL == 1
  205. DRESULT SD_ioctl(BYTE lun, BYTE cmd, void *buff)
  206. {
  207. DRESULT res = RES_ERROR;
  208. BSP_SD_CardInfo CardInfo;
  209. if (Stat & STA_NOINIT) return RES_NOTRDY;
  210. switch (cmd)
  211. {
  212. /* Make sure that no pending write process */
  213. case CTRL_SYNC :
  214. res = RES_OK;
  215. break;
  216. /* Get number of sectors on the disk (DWORD) */
  217. case GET_SECTOR_COUNT :
  218. BSP_SD_GetCardInfo(&CardInfo);
  219. *(DWORD*)buff = CardInfo.LogBlockNbr;
  220. res = RES_OK;
  221. break;
  222. /* Get R/W sector size (WORD) */
  223. case GET_SECTOR_SIZE :
  224. BSP_SD_GetCardInfo(&CardInfo);
  225. *(WORD*)buff = CardInfo.LogBlockSize;
  226. res = RES_OK;
  227. break;
  228. /* Get erase block size in unit of sector (DWORD) */
  229. case GET_BLOCK_SIZE :
  230. BSP_SD_GetCardInfo(&CardInfo);
  231. *(DWORD*)buff = CardInfo.LogBlockSize / SD_DEFAULT_BLOCK_SIZE;
  232. res = RES_OK;
  233. break;
  234. default:
  235. res = RES_PARERR;
  236. }
  237. return res;
  238. }
  239. #endif /* _USE_IOCTL == 1 */
  240. /* USER CODE BEGIN afterIoctlSection */
  241. /* can be used to modify previous code / undefine following code / add new code */
  242. /* USER CODE END afterIoctlSection */
  243. /* USER CODE BEGIN lastSection */
  244. /* can be used to modify / undefine previous code or add new code */
  245. /* USER CODE END lastSection */
  246. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/