1 /**
2   ******************************************************************************
3   * @file    stm32mp1xx_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 
29    (+) Start Read and Write for multibuffer mode using HAL_SDEx_ReadBlocksDMAMultiBuffer() and HAL_SDEx_WriteBlocksDMAMultiBuffer() functions.
30 
31   @endverbatim
32   ******************************************************************************
33   */
34 
35 /* Includes ------------------------------------------------------------------*/
36 #include "stm32mp1xx_hal.h"
37 
38 /** @addtogroup STM32MP1xx_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 transfered data
79   * @param  pDataBuffer1: Pointer to the buffer1 that will contain/receive the transfered 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, uint32_t BufferSize)
84 {
85   if(hsd->State == HAL_SD_STATE_READY)
86   {
87     hsd->Instance->IDMABASE0= (uint32_t) pDataBuffer0;
88     hsd->Instance->IDMABASE1= (uint32_t) pDataBuffer1;
89     hsd->Instance->IDMABSIZE= (uint32_t) (BLOCKSIZE * BufferSize);
90 
91     return HAL_OK;
92   }
93   else
94   {
95     return HAL_BUSY;
96   }
97 }
98 
99 /**
100   * @brief  Reads block(s) from a specified address in a card. The received Data will be stored in Buffer0 and Buffer1.
101   *         Buffer0, Buffer1 and BufferSize need to be configured by function HAL_SDEx_ConfigDMAMultiBuffer before call this function.
102   * @param  hsd: SD handle
103   * @param  BlockAdd: Block Address from where data is to be read
104   * @param  NumberOfBlocks: Total number of blocks to read
105   * @retval HAL status
106   */
HAL_SDEx_ReadBlocksDMAMultiBuffer(SD_HandleTypeDef * hsd,uint32_t BlockAdd,uint32_t NumberOfBlocks)107 HAL_StatusTypeDef HAL_SDEx_ReadBlocksDMAMultiBuffer(SD_HandleTypeDef *hsd, uint32_t BlockAdd, uint32_t NumberOfBlocks)
108 {
109   SDMMC_DataInitTypeDef config;
110   uint32_t errorstate;
111   uint32_t DmaBase0_reg, DmaBase1_reg;
112   uint32_t add = BlockAdd;
113 
114   if(hsd->State == HAL_SD_STATE_READY)
115   {
116     if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
117     {
118       hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
119       return HAL_ERROR;
120     }
121 
122     DmaBase0_reg = hsd->Instance->IDMABASE0;
123     DmaBase1_reg = hsd->Instance->IDMABASE1;
124     if ((hsd->Instance->IDMABSIZE == 0U) || (DmaBase0_reg == 0U) || (DmaBase1_reg == 0U))
125     {
126       hsd->ErrorCode = HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
127       return HAL_ERROR;
128     }
129 
130     /* Initialize data control register */
131     hsd->Instance->DCTRL = 0;
132     /* Clear old Flags*/
133     __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
134 
135     hsd->ErrorCode = HAL_SD_ERROR_NONE;
136     hsd->State = HAL_SD_STATE_BUSY;
137 
138     if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
139     {
140       add *= 512U;
141     }
142 
143     /* Configure the SD DPSM (Data Path State Machine) */
144     config.DataTimeOut   = SDMMC_DATATIMEOUT;
145     config.DataLength    = 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(hsd->Instance, &config);
151 
152     hsd->Instance->DCTRL |= SDMMC_DCTRL_FIFORST;
153 
154     __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
155 
156     hsd->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_DOUBLE_BUFF0;
157 
158     __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND | SDMMC_IT_IDMABTC));
159 
160     /* Read Blocks in DMA mode */
161     hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
162 
163     /* Read Multi Block command */
164     errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
165     if(errorstate != HAL_SD_ERROR_NONE)
166     {
167       hsd->State = HAL_SD_STATE_READY;
168       hsd->ErrorCode |= errorstate;
169       return HAL_ERROR;
170     }
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 transfered Data are stored in Buffer0 and Buffer1.
183   *         Buffer0, Buffer1 and BufferSize need to be configured by function HAL_SDEx_ConfigDMAMultiBuffer before call this function.
184   * @param  hsd: SD 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_SDEx_WriteBlocksDMAMultiBuffer(SD_HandleTypeDef * hsd,uint32_t BlockAdd,uint32_t NumberOfBlocks)189 HAL_StatusTypeDef HAL_SDEx_WriteBlocksDMAMultiBuffer(SD_HandleTypeDef *hsd, 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(hsd->State == HAL_SD_STATE_READY)
197   {
198     if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
199     {
200       hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
201       return HAL_ERROR;
202     }
203 
204     DmaBase0_reg = hsd->Instance->IDMABASE0;
205     DmaBase1_reg = hsd->Instance->IDMABASE1;
206     if ((hsd->Instance->IDMABSIZE == 0U) || (DmaBase0_reg == 0U) || (DmaBase1_reg == 0U))
207     {
208       hsd->ErrorCode = HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
209       return HAL_ERROR;
210     }
211 
212     /* Initialize data control register */
213     hsd->Instance->DCTRL = 0;
214 
215     hsd->ErrorCode = HAL_SD_ERROR_NONE;
216 
217     hsd->State = HAL_SD_STATE_BUSY;
218 
219     if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
220     {
221       add *= 512U;
222     }
223 
224     /* Configure the SD DPSM (Data Path State Machine) */
225     config.DataTimeOut   = SDMMC_DATATIMEOUT;
226     config.DataLength    = 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(hsd->Instance, &config);
232 
233     __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
234 
235     hsd->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_DOUBLE_BUFF0;
236 
237     __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND | SDMMC_IT_IDMABTC));
238 
239     /* Write Blocks in DMA mode */
240     hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
241 
242     /* Write Multi Block command */
243     errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
244     if(errorstate != HAL_SD_ERROR_NONE)
245     {
246       hsd->State = HAL_SD_STATE_READY;
247       hsd->ErrorCode |= errorstate;
248       return HAL_ERROR;
249     }
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  hsd:           pointer to a SD_HandleTypeDef structure.
263   * @param  Buffer:        the buffer to be changed, This parameter can be one of
264   *                        the following values: SD_DMA_BUFFER0 or SD_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_SDEx_ChangeDMABuffer(SD_HandleTypeDef * hsd,HAL_SDEx_DMABuffer_MemoryTypeDef Buffer,uint32_t * pDataBuffer)271 HAL_StatusTypeDef HAL_SDEx_ChangeDMABuffer(SD_HandleTypeDef *hsd, HAL_SDEx_DMABuffer_MemoryTypeDef Buffer, uint32_t *pDataBuffer)
272 {
273   if(Buffer == SD_DMA_BUFFER0)
274   {
275     /* change the buffer0 address */
276     hsd->Instance->IDMABASE0 = (uint32_t)pDataBuffer;
277   }
278   else
279   {
280     /* change the memory1 address */
281     hsd->Instance->IDMABASE1 = (uint32_t)pDataBuffer;
282   }
283 
284   return HAL_OK;
285 }
286 
287 
288 /**
289   * @}
290   */
291 
292   /**
293   * @}
294   */
295 
296 #endif /* HAL_SD_MODULE_ENABLED */
297 
298 /**
299   * @}
300   */
301 
302 /**
303   * @}
304   */
305