You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

328 lines
10 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_hal_sd_ex.c
  4. * @author MCD Application Team
  5. * @brief SD card Extended HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Secure Digital (SD) peripheral:
  8. * + Extended features functions
  9. *
  10. @verbatim
  11. ==============================================================================
  12. ##### How to use this driver #####
  13. ==============================================================================
  14. [..]
  15. The SD Extension HAL driver can be used as follows:
  16. (+) Configure Buffer0 and Buffer1 start address and Buffer size using HAL_SDEx_ConfigDMAMultiBuffer() function.
  17. (+) Start Read and Write for multibuffer mode using HAL_SDEx_ReadBlocksDMAMultiBuffer() and HAL_SDEx_WriteBlocksDMAMultiBuffer() functions.
  18. @endverbatim
  19. ******************************************************************************
  20. * @attention
  21. *
  22. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  23. * All rights reserved.</center></h2>
  24. *
  25. * This software component is licensed by ST under BSD 3-Clause license,
  26. * the "License"; You may not use this file except in compliance with the
  27. * License. You may obtain a copy of the License at:
  28. * opensource.org/licenses/BSD-3-Clause
  29. *
  30. ******************************************************************************
  31. */
  32. /* Includes ------------------------------------------------------------------*/
  33. #include "stm32h7xx_hal.h"
  34. /** @addtogroup STM32H7xx_HAL_Driver
  35. * @{
  36. */
  37. /** @defgroup SDEx SDEx
  38. * @brief SD Extended HAL module driver
  39. * @{
  40. */
  41. #ifdef HAL_SD_MODULE_ENABLED
  42. /* Private typedef -----------------------------------------------------------*/
  43. /* Private define ------------------------------------------------------------*/
  44. /* Private macro -------------------------------------------------------------*/
  45. /* Private variables ---------------------------------------------------------*/
  46. /* Private function prototypes -----------------------------------------------*/
  47. /* Private functions ---------------------------------------------------------*/
  48. /* Exported functions --------------------------------------------------------*/
  49. /** @addtogroup SDEx_Exported_Functions
  50. * @{
  51. */
  52. /** @addtogroup SDEx_Exported_Functions_Group1
  53. * @brief Multibuffer functions
  54. *
  55. @verbatim
  56. ==============================================================================
  57. ##### Multibuffer functions #####
  58. ==============================================================================
  59. [..]
  60. This section provides functions allowing to configure the multibuffer mode and start read and write
  61. multibuffer mode for SD HAL driver.
  62. @endverbatim
  63. * @{
  64. */
  65. /**
  66. * @brief Configure DMA Dual Buffer mode. The Data transfer is managed by an Internal DMA.
  67. * @param hsd: SD handle
  68. * @param pDataBuffer0: Pointer to the buffer0 that will contain/receive the transfered data
  69. * @param pDataBuffer1: Pointer to the buffer1 that will contain/receive the transfered data
  70. * @param BufferSize: Size of Buffer0 in Blocks. Buffer0 and Buffer1 must have the same size.
  71. * @retval HAL status
  72. */
  73. HAL_StatusTypeDef HAL_SDEx_ConfigDMAMultiBuffer(SD_HandleTypeDef *hsd, uint32_t *pDataBuffer0, uint32_t *pDataBuffer1, uint32_t BufferSize)
  74. {
  75. if(hsd->State == HAL_SD_STATE_READY)
  76. {
  77. hsd->Instance->IDMABASE0= (uint32_t) pDataBuffer0;
  78. hsd->Instance->IDMABASE1= (uint32_t) pDataBuffer1;
  79. hsd->Instance->IDMABSIZE= (uint32_t) (BLOCKSIZE * BufferSize);
  80. return HAL_OK;
  81. }
  82. else
  83. {
  84. return HAL_BUSY;
  85. }
  86. }
  87. /**
  88. * @brief Reads block(s) from a specified address in a card. The received Data will be stored in Buffer0 and Buffer1.
  89. * Buffer0, Buffer1 and BufferSize need to be configured by function HAL_SDEx_ConfigDMAMultiBuffer before call this function.
  90. * @param hsd: SD handle
  91. * @param BlockAdd: Block Address from where data is to be read
  92. * @param NumberOfBlocks: Total number of blocks to read
  93. * @retval HAL status
  94. */
  95. HAL_StatusTypeDef HAL_SDEx_ReadBlocksDMAMultiBuffer(SD_HandleTypeDef *hsd, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  96. {
  97. SDMMC_DataInitTypeDef config;
  98. uint32_t errorstate;
  99. uint32_t DmaBase0_reg, DmaBase1_reg;
  100. uint32_t add = BlockAdd;
  101. if(hsd->State == HAL_SD_STATE_READY)
  102. {
  103. if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
  104. {
  105. hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  106. return HAL_ERROR;
  107. }
  108. DmaBase0_reg = hsd->Instance->IDMABASE0;
  109. DmaBase1_reg = hsd->Instance->IDMABASE1;
  110. if ((hsd->Instance->IDMABSIZE == 0U) || (DmaBase0_reg == 0U) || (DmaBase1_reg == 0U))
  111. {
  112. hsd->ErrorCode = HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  113. return HAL_ERROR;
  114. }
  115. /* Initialize data control register */
  116. hsd->Instance->DCTRL = 0;
  117. /* Clear old Flags*/
  118. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
  119. hsd->ErrorCode = HAL_SD_ERROR_NONE;
  120. hsd->State = HAL_SD_STATE_BUSY;
  121. if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  122. {
  123. add *= 512U;
  124. }
  125. /* Set Block Size for Card */
  126. errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  127. if(errorstate != HAL_SD_ERROR_NONE)
  128. {
  129. /* Clear all the static flags */
  130. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  131. hsd->ErrorCode |= errorstate;
  132. hsd->State = HAL_SD_STATE_READY;
  133. return HAL_ERROR;
  134. }
  135. /* Configure the SD DPSM (Data Path State Machine) */
  136. config.DataTimeOut = SDMMC_DATATIMEOUT;
  137. config.DataLength = BLOCKSIZE * NumberOfBlocks;
  138. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  139. config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  140. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  141. config.DPSM = SDMMC_DPSM_DISABLE;
  142. (void)SDMMC_ConfigData(hsd->Instance, &config);
  143. hsd->Instance->DCTRL |= SDMMC_DCTRL_FIFORST;
  144. __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
  145. hsd->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_DOUBLE_BUFF0;
  146. /* Read Blocks in DMA mode */
  147. hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
  148. /* Read Multi Block command */
  149. errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
  150. if(errorstate != HAL_SD_ERROR_NONE)
  151. {
  152. hsd->State = HAL_SD_STATE_READY;
  153. hsd->ErrorCode |= errorstate;
  154. return HAL_ERROR;
  155. }
  156. __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND | SDMMC_IT_IDMABTC));
  157. return HAL_OK;
  158. }
  159. else
  160. {
  161. return HAL_BUSY;
  162. }
  163. }
  164. /**
  165. * @brief Write block(s) to a specified address in a card. The transfered Data are stored in Buffer0 and Buffer1.
  166. * Buffer0, Buffer1 and BufferSize need to be configured by function HAL_SDEx_ConfigDMAMultiBuffer before call this function.
  167. * @param hsd: SD handle
  168. * @param BlockAdd: Block Address from where data is to be read
  169. * @param NumberOfBlocks: Total number of blocks to read
  170. * @retval HAL status
  171. */
  172. HAL_StatusTypeDef HAL_SDEx_WriteBlocksDMAMultiBuffer(SD_HandleTypeDef *hsd, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  173. {
  174. SDMMC_DataInitTypeDef config;
  175. uint32_t errorstate;
  176. uint32_t DmaBase0_reg, DmaBase1_reg;
  177. uint32_t add = BlockAdd;
  178. if(hsd->State == HAL_SD_STATE_READY)
  179. {
  180. if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
  181. {
  182. hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  183. return HAL_ERROR;
  184. }
  185. DmaBase0_reg = hsd->Instance->IDMABASE0;
  186. DmaBase1_reg = hsd->Instance->IDMABASE1;
  187. if ((hsd->Instance->IDMABSIZE == 0U) || (DmaBase0_reg == 0U) || (DmaBase1_reg == 0U))
  188. {
  189. hsd->ErrorCode = HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  190. return HAL_ERROR;
  191. }
  192. /* Initialize data control register */
  193. hsd->Instance->DCTRL = 0;
  194. hsd->ErrorCode = HAL_SD_ERROR_NONE;
  195. hsd->State = HAL_SD_STATE_BUSY;
  196. if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  197. {
  198. add *= 512U;
  199. }
  200. /* Set Block Size for Card */
  201. errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  202. if(errorstate != HAL_SD_ERROR_NONE)
  203. {
  204. /* Clear all the static flags */
  205. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  206. hsd->ErrorCode |= errorstate;
  207. hsd->State = HAL_SD_STATE_READY;
  208. return HAL_ERROR;
  209. }
  210. /* Configure the SD DPSM (Data Path State Machine) */
  211. config.DataTimeOut = SDMMC_DATATIMEOUT;
  212. config.DataLength = BLOCKSIZE * NumberOfBlocks;
  213. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  214. config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
  215. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  216. config.DPSM = SDMMC_DPSM_DISABLE;
  217. (void)SDMMC_ConfigData(hsd->Instance, &config);
  218. __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
  219. hsd->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_DOUBLE_BUFF0;
  220. /* Write Blocks in DMA mode */
  221. hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
  222. /* Write Multi Block command */
  223. errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
  224. if(errorstate != HAL_SD_ERROR_NONE)
  225. {
  226. hsd->State = HAL_SD_STATE_READY;
  227. hsd->ErrorCode |= errorstate;
  228. return HAL_ERROR;
  229. }
  230. __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND | SDMMC_IT_IDMABTC));
  231. return HAL_OK;
  232. }
  233. else
  234. {
  235. return HAL_BUSY;
  236. }
  237. }
  238. /**
  239. * @brief Change the DMA Buffer0 or Buffer1 address on the fly.
  240. * @param hsd: pointer to a SD_HandleTypeDef structure.
  241. * @param Buffer: the buffer to be changed, This parameter can be one of
  242. * the following values: SD_DMA_BUFFER0 or SD_DMA_BUFFER1
  243. * @param pDataBuffer: The new address
  244. * @note The BUFFER0 address can be changed only when the current transfer use
  245. * BUFFER1 and the BUFFER1 address can be changed only when the current
  246. * transfer use BUFFER0.
  247. * @retval HAL status
  248. */
  249. HAL_StatusTypeDef HAL_SDEx_ChangeDMABuffer(SD_HandleTypeDef *hsd, HAL_SDEx_DMABuffer_MemoryTypeDef Buffer, uint32_t *pDataBuffer)
  250. {
  251. if(Buffer == SD_DMA_BUFFER0)
  252. {
  253. /* change the buffer0 address */
  254. hsd->Instance->IDMABASE0 = (uint32_t)pDataBuffer;
  255. }
  256. else
  257. {
  258. /* change the memory1 address */
  259. hsd->Instance->IDMABASE1 = (uint32_t)pDataBuffer;
  260. }
  261. return HAL_OK;
  262. }
  263. /**
  264. * @}
  265. */
  266. /**
  267. * @}
  268. */
  269. #endif /* HAL_SD_MODULE_ENABLED */
  270. /**
  271. * @}
  272. */
  273. /**
  274. * @}
  275. */
  276. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/