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