1 /**
2 ******************************************************************************
3 * @file stm32l4xx_hal_mmc_ex.c
4 * @author MCD Application Team
5 * @brief MMC card Extended HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the Secure Digital (MMC) peripheral:
8 * + Extended features functions
9 *
10 ******************************************************************************
11 * @attention
12 *
13 * Copyright (c) 2017 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 MMC Extension HAL driver can be used as follows:
27 (+) Configure Buffer0 and Buffer1 start address and Buffer size using HAL_MMCEx_ConfigDMAMultiBuffer() function.
28
29 (+) Start Read and Write for multibuffer mode using HAL_MMCEx_ReadBlocksDMAMultiBuffer() and HAL_MMCEx_WriteBlocksDMAMultiBuffer() functions.
30
31 @endverbatim
32 ******************************************************************************
33 */
34
35 /* Includes ------------------------------------------------------------------*/
36 #include "stm32l4xx_hal.h"
37
38 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
39
40 /** @addtogroup STM32L4xx_HAL_Driver
41 * @{
42 */
43
44 /** @defgroup MMCEx MMCEx
45 * @brief MMC Extended HAL module driver
46 * @{
47 */
48
49 #ifdef HAL_MMC_MODULE_ENABLED
50
51 /* Private typedef -----------------------------------------------------------*/
52 /* Private define ------------------------------------------------------------*/
53 /* Private macro -------------------------------------------------------------*/
54 /* Private variables ---------------------------------------------------------*/
55 /* Private function prototypes -----------------------------------------------*/
56 /* Private functions ---------------------------------------------------------*/
57 /* Exported functions --------------------------------------------------------*/
58 /** @defgroup MMCEx_Exported_Types MMCEx Exported Types
59 * @{
60 */
61
62 /** @defgroup MMCEx_Exported_Types_Group1 MMC Internal DMA Buffer structure
63 * @brief Multibuffer functions
64 *
65 @verbatim
66 ==============================================================================
67 ##### Multibuffer functions #####
68 ==============================================================================
69 [..]
70 This section provides functions allowing to configure the multibuffer mode and start read and write
71 multibuffer mode for MMC HAL driver.
72
73 @endverbatim
74 * @{
75 */
76
77 /**
78 * @brief Configure DMA Dual Buffer mode. The Data transfer is managed by an Internal DMA.
79 * @param hmmc MMC handle
80 * @param pDataBuffer0 Pointer to the buffer0 that will contain/receive the transferred data
81 * @param pDataBuffer1 Pointer to the buffer1 that will contain/receive the transferred data
82 * @param BufferSize Size of Buffer0 in Blocks. Buffer0 and Buffer1 must have the same size.
83 * @retval HAL status
84 */
HAL_MMCEx_ConfigDMAMultiBuffer(MMC_HandleTypeDef * hmmc,uint32_t * pDataBuffer0,uint32_t * pDataBuffer1,uint32_t BufferSize)85 HAL_StatusTypeDef HAL_MMCEx_ConfigDMAMultiBuffer(MMC_HandleTypeDef *hmmc, uint32_t * pDataBuffer0, uint32_t * pDataBuffer1, uint32_t BufferSize)
86 {
87 if(hmmc->State == HAL_MMC_STATE_READY)
88 {
89 hmmc->Instance->IDMABASE0= (uint32_t) pDataBuffer0 ;
90 hmmc->Instance->IDMABASE1= (uint32_t) pDataBuffer1 ;
91 hmmc->Instance->IDMABSIZE= (uint32_t) (MMC_BLOCKSIZE * BufferSize);
92
93 return HAL_OK;
94 }
95 else
96 {
97 return HAL_BUSY;
98 }
99 }
100
101 /**
102 * @brief Reads block(s) from a specified address in a card. The received Data will be stored in Buffer0 and Buffer1.
103 * Buffer0, Buffer1 and BufferSize need to be configured by function HAL_MMCEx_ConfigDMAMultiBuffer before call this function.
104 * @param hmmc MMC 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_MMCEx_ReadBlocksDMAMultiBuffer(MMC_HandleTypeDef * hmmc,uint32_t BlockAdd,uint32_t NumberOfBlocks)109 HAL_StatusTypeDef HAL_MMCEx_ReadBlocksDMAMultiBuffer(MMC_HandleTypeDef *hmmc, uint32_t BlockAdd, uint32_t NumberOfBlocks)
110 {
111 SDMMC_DataInitTypeDef config;
112 uint32_t DmaBase0_reg, DmaBase1_reg;
113 uint32_t errorstate;
114 uint32_t add = BlockAdd;
115
116 if(hmmc->State == HAL_MMC_STATE_READY)
117 {
118 if((BlockAdd + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
119 {
120 hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
121 return HAL_ERROR;
122 }
123
124 DmaBase0_reg = hmmc->Instance->IDMABASE0;
125 DmaBase1_reg = hmmc->Instance->IDMABASE1;
126 if ((hmmc->Instance->IDMABSIZE == 0U) || (DmaBase0_reg == 0U) || (DmaBase1_reg == 0U))
127 {
128 hmmc->ErrorCode = HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
129 return HAL_ERROR;
130 }
131
132 /* Initialize data control register */
133 hmmc->Instance->DCTRL = 0;
134
135 hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
136 hmmc->State = HAL_MMC_STATE_BUSY;
137
138 if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
139 {
140 add *= 512U;
141 }
142
143 /* Configure the MMC DPSM (Data Path State Machine) */
144 config.DataTimeOut = SDMMC_DATATIMEOUT;
145 config.DataLength = MMC_BLOCKSIZE * NumberOfBlocks;
146 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
147 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
148 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
149 config.DPSM = SDMMC_DPSM_DISABLE;
150 (void)SDMMC_ConfigData(hmmc->Instance, &config);
151
152 hmmc->Instance->DCTRL |= SDMMC_DCTRL_FIFORST;
153
154 __SDMMC_CMDTRANS_ENABLE( hmmc->Instance);
155
156 hmmc->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_DOUBLE_BUFF0;
157
158 /* Read Blocks in DMA mode */
159 hmmc->Context = (MMC_CONTEXT_READ_MULTIPLE_BLOCK | MMC_CONTEXT_DMA);
160
161 /* Read Multi Block command */
162 errorstate = SDMMC_CmdReadMultiBlock(hmmc->Instance, add);
163 if(errorstate != HAL_MMC_ERROR_NONE)
164 {
165 hmmc->State = HAL_MMC_STATE_READY;
166 hmmc->ErrorCode |= errorstate;
167 return HAL_ERROR;
168 }
169
170 __HAL_MMC_ENABLE_IT(hmmc, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND | SDMMC_FLAG_IDMATE | SDMMC_FLAG_IDMABTC));
171
172 return HAL_OK;
173 }
174 else
175 {
176 return HAL_BUSY;
177 }
178
179 }
180
181 /**
182 * @brief Write block(s) to a specified address in a card. The transferred Data are stored in Buffer0 and Buffer1.
183 * Buffer0, Buffer1 and BufferSize need to be configured by function HAL_MMCEx_ConfigDMAMultiBuffer before call this function.
184 * @param hmmc MMC handle
185 * @param BlockAdd Block Address from where data is to be read
186 * @param NumberOfBlocks Total number of blocks to read
187 * @retval HAL status
188 */
HAL_MMCEx_WriteBlocksDMAMultiBuffer(MMC_HandleTypeDef * hmmc,uint32_t BlockAdd,uint32_t NumberOfBlocks)189 HAL_StatusTypeDef HAL_MMCEx_WriteBlocksDMAMultiBuffer(MMC_HandleTypeDef *hmmc, uint32_t BlockAdd, uint32_t NumberOfBlocks)
190 {
191 SDMMC_DataInitTypeDef config;
192 uint32_t errorstate;
193 uint32_t DmaBase0_reg, DmaBase1_reg;
194 uint32_t add = BlockAdd;
195
196 if(hmmc->State == HAL_MMC_STATE_READY)
197 {
198 if((BlockAdd + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
199 {
200 hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
201 return HAL_ERROR;
202 }
203
204 DmaBase0_reg = hmmc->Instance->IDMABASE0;
205 DmaBase1_reg = hmmc->Instance->IDMABASE1;
206 if ((hmmc->Instance->IDMABSIZE == 0U) || (DmaBase0_reg == 0U) || (DmaBase1_reg == 0U))
207 {
208 hmmc->ErrorCode = HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
209 return HAL_ERROR;
210 }
211
212 /* Initialize data control register */
213 hmmc->Instance->DCTRL = 0;
214
215 hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
216
217 hmmc->State = HAL_MMC_STATE_BUSY;
218
219 if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
220 {
221 add *= 512U;
222 }
223
224 /* Configure the MMC DPSM (Data Path State Machine) */
225 config.DataTimeOut = SDMMC_DATATIMEOUT;
226 config.DataLength = MMC_BLOCKSIZE * NumberOfBlocks;
227 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
228 config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
229 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
230 config.DPSM = SDMMC_DPSM_DISABLE;
231 (void)SDMMC_ConfigData(hmmc->Instance, &config);
232
233 __SDMMC_CMDTRANS_ENABLE( hmmc->Instance);
234
235 hmmc->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_DOUBLE_BUFF0;
236
237 /* Write Blocks in DMA mode */
238 hmmc->Context = (MMC_CONTEXT_WRITE_MULTIPLE_BLOCK | MMC_CONTEXT_DMA);
239
240 /* Write Multi Block command */
241 errorstate = SDMMC_CmdWriteMultiBlock(hmmc->Instance, add);
242 if(errorstate != HAL_MMC_ERROR_NONE)
243 {
244 hmmc->State = HAL_MMC_STATE_READY;
245 hmmc->ErrorCode |= errorstate;
246 return HAL_ERROR;
247 }
248
249 __HAL_MMC_ENABLE_IT(hmmc, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND | SDMMC_FLAG_IDMATE | SDMMC_FLAG_IDMABTC));
250
251 return HAL_OK;
252 }
253 else
254 {
255 return HAL_BUSY;
256 }
257 }
258
259
260 /**
261 * @brief Change the DMA Buffer0 or Buffer1 address on the fly.
262 * @param hmmc pointer to a MMC_HandleTypeDef structure.
263 * @param Buffer the buffer to be changed, This parameter can be one of
264 * the following values: MMC_DMA_BUFFER0 or MMC_DMA_BUFFER1
265 * @param pDataBuffer The new address
266 * @note The BUFFER0 address can be changed only when the current transfer use
267 * BUFFER1 and the BUFFER1 address can be changed only when the current
268 * transfer use BUFFER0.
269 * @retval HAL status
270 */
HAL_MMCEx_ChangeDMABuffer(MMC_HandleTypeDef * hmmc,HAL_MMCEx_DMABuffer_MemoryTypeDef Buffer,uint32_t * pDataBuffer)271 HAL_StatusTypeDef HAL_MMCEx_ChangeDMABuffer(MMC_HandleTypeDef *hmmc, HAL_MMCEx_DMABuffer_MemoryTypeDef Buffer, uint32_t *pDataBuffer)
272 {
273 if(Buffer == MMC_DMA_BUFFER0)
274 {
275 /* change the buffer0 address */
276 hmmc->Instance->IDMABASE0 = (uint32_t)pDataBuffer;
277 }
278 else
279 {
280 /* change the memory1 address */
281 hmmc->Instance->IDMABASE1 = (uint32_t)pDataBuffer;
282 }
283
284 return HAL_OK;
285 }
286
287 /**
288 * @brief Read DMA Buffer 0 Transfer completed callbacks
289 * @param hmmc MMC handle
290 * @retval None
291 */
HAL_MMCEx_Read_DMADoubleBuffer0CpltCallback(MMC_HandleTypeDef * hmmc)292 __weak void HAL_MMCEx_Read_DMADoubleBuffer0CpltCallback(MMC_HandleTypeDef *hmmc)
293 {
294 /* Prevent unused argument(s) compilation warning */
295 UNUSED(hmmc);
296
297 /* NOTE : This function should not be modified, when the callback is needed,
298 the HAL_MMCEx_Read_DMADoubleBuffer0CpltCallback can be implemented in the user file
299 */
300 }
301
302 /**
303 * @brief Read DMA Buffer 1 Transfer completed callbacks
304 * @param hmmc MMC handle
305 * @retval None
306 */
HAL_MMCEx_Read_DMADoubleBuffer1CpltCallback(MMC_HandleTypeDef * hmmc)307 __weak void HAL_MMCEx_Read_DMADoubleBuffer1CpltCallback(MMC_HandleTypeDef *hmmc)
308 {
309 /* Prevent unused argument(s) compilation warning */
310 UNUSED(hmmc);
311
312 /* NOTE : This function should not be modified, when the callback is needed,
313 the HAL_MMCEx_Read_DMADoubleBuffer1CpltCallback can be implemented in the user file
314 */
315 }
316
317 /**
318 * @brief Write DMA Buffer 0 Transfer completed callbacks
319 * @param hmmc MMC handle
320 * @retval None
321 */
HAL_MMCEx_Write_DMADoubleBuffer0CpltCallback(MMC_HandleTypeDef * hmmc)322 __weak void HAL_MMCEx_Write_DMADoubleBuffer0CpltCallback(MMC_HandleTypeDef *hmmc)
323 {
324 /* Prevent unused argument(s) compilation warning */
325 UNUSED(hmmc);
326
327 /* NOTE : This function should not be modified, when the callback is needed,
328 the HAL_MMCEx_Write_DMADoubleBuffer0CpltCallback can be implemented in the user file
329 */
330 }
331
332 /**
333 * @brief Write DMA Buffer 1 Transfer completed callbacks
334 * @param hmmc MMC handle
335 * @retval None
336 */
HAL_MMCEx_Write_DMADoubleBuffer1CpltCallback(MMC_HandleTypeDef * hmmc)337 __weak void HAL_MMCEx_Write_DMADoubleBuffer1CpltCallback(MMC_HandleTypeDef *hmmc)
338 {
339 /* Prevent unused argument(s) compilation warning */
340 UNUSED(hmmc);
341
342 /* NOTE : This function should not be modified, when the callback is needed,
343 the HAL_MMCEx_Write_DMADoubleBuffer0CpltCallback can be implemented in the user file
344 */
345 }
346
347 /**
348 * @}
349 */
350
351 /**
352 * @}
353 */
354
355 #endif /* HAL_MMC_MODULE_ENABLED */
356
357 /**
358 * @}
359 */
360
361 /**
362 * @}
363 */
364
365 #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
366