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