1 /**
2 ******************************************************************************
3 * @file stm32l4xx_hal_sd.c
4 * @author MCD Application Team
5 * @brief SD card HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the Secure Digital (SD) peripheral:
8 * + Initialization and de-initialization functions
9 * + IO operation functions
10 * + Peripheral Control functions
11 * + Peripheral State functions
12 *
13 ******************************************************************************
14 * @attention
15 *
16 * Copyright (c) 2017 STMicroelectronics.
17 * All rights reserved.
18 *
19 * This software is licensed under terms that can be found in the LICENSE file
20 * in the root directory of this software component.
21 * If no LICENSE file comes with this software, it is provided AS-IS.
22 *
23 ******************************************************************************
24 @verbatim
25 ==============================================================================
26 ##### How to use this driver #####
27 ==============================================================================
28 [..]
29 This driver implements a high level communication layer for read and write from/to
30 this memory. The needed STM32 hardware resources (SDMMC and GPIO) are performed by
31 the user in HAL_SD_MspInit() function (MSP layer).
32 Basically, the MSP layer configuration should be the same as we provide in the
33 examples.
34 You can easily tailor this configuration according to hardware resources.
35
36 [..]
37 This driver is a generic layered driver for SDMMC memories which uses the HAL
38 SDMMC driver functions to interface with SD and uSD cards devices.
39 It is used as follows:
40
41 (#)Initialize the SDMMC low level resources by implementing the HAL_SD_MspInit() API:
42 (##) Call the function HAL_RCCEx_PeriphCLKConfig with RCC_PERIPHCLK_SDMMC1 for
43 PeriphClockSelection and select SDMMC1 clock source (MSI, main PLL or PLLSAI1)
44 (##) Enable the SDMMC interface clock using __HAL_RCC_SDMMC1_CLK_ENABLE();
45 (##) SDMMC pins configuration for SD card
46 (+++) Enable the clock for the SDMMC GPIOs using the functions __HAL_RCC_GPIOx_CLK_ENABLE();
47 (+++) Configure these SDMMC pins as alternate function pull-up using HAL_GPIO_Init()
48 and according to your pin assignment;
49 (##) On STM32L4Rx/STM32L4Sxx devices, no DMA configuration is need, an internal DMA for SDMMC Peripheral is used.
50 (##) On other devices, perform DMA configuration if you need to use DMA process (HAL_SD_ReadBlocks_DMA()
51 and HAL_SD_WriteBlocks_DMA() APIs).
52 (+++) Enable the DMAx interface clock using __HAL_RCC_DMAx_CLK_ENABLE();
53 (+++) Configure the DMA using the function HAL_DMA_Init() with predeclared and filled.
54 (##) NVIC configuration if you need to use interrupt process when using DMA transfer.
55 (+++) Configure the SDMMC and DMA interrupt priorities using functions
56 HAL_NVIC_SetPriority(); DMA priority is superior to SDMMC's priority
57 (+++) Enable the NVIC DMA and SDMMC IRQs using function HAL_NVIC_EnableIRQ()
58 (+++) SDMMC interrupts are managed using the macros __HAL_SD_ENABLE_IT()
59 and __HAL_SD_DISABLE_IT() inside the communication process.
60 (+++) SDMMC interrupts pending bits are managed using the macros __HAL_SD_GET_IT()
61 and __HAL_SD_CLEAR_IT()
62 (##) NVIC configuration if you need to use interrupt process (HAL_SD_ReadBlocks_IT()
63 and HAL_SD_WriteBlocks_IT() APIs).
64 (+++) Configure the SDMMC interrupt priorities using function HAL_NVIC_SetPriority();
65 (+++) Enable the NVIC SDMMC IRQs using function HAL_NVIC_EnableIRQ()
66 (+++) SDMMC interrupts are managed using the macros __HAL_SD_ENABLE_IT()
67 and __HAL_SD_DISABLE_IT() inside the communication process.
68 (+++) SDMMC interrupts pending bits are managed using the macros __HAL_SD_GET_IT()
69 and __HAL_SD_CLEAR_IT()
70 (#) At this stage, you can perform SD read/write/erase operations after SD card initialization
71
72
73 *** SD Card Initialization and configuration ***
74 ================================================
75 [..]
76 To initialize the SD Card, use the HAL_SD_Init() function. It Initializes
77 SDMMC Peripheral(STM32 side) and the SD Card, and put it into StandBy State (Ready for data transfer).
78 This function provide the following operations:
79
80 (#) Apply the SD Card initialization process at 400KHz and check the SD Card
81 type (Standard Capacity or High Capacity). You can change or adapt this
82 frequency by adjusting the "ClockDiv" field.
83 The SD Card frequency (SDMMC_CK) is computed as follows:
84
85 SDMMC_CK = SDMMCCLK / (2 * ClockDiv) on STM32L4Rx/STM32L4Sxx devices
86 SDMMC_CK = SDMMCCLK / (ClockDiv + 2) on other devices
87
88 In initialization mode and according to the SD Card standard,
89 make sure that the SDMMC_CK frequency doesn't exceed 400KHz.
90
91 This phase of initialization is done through SDMMC_Init() and
92 SDMMC_PowerState_ON() SDMMC low level APIs.
93
94 (#) Initialize the SD card. The API used is HAL_SD_InitCard().
95 This phase allows the card initialization and identification
96 and check the SD Card type (Standard Capacity or High Capacity)
97 The initialization flow is compatible with SD standard.
98
99 This API (HAL_SD_InitCard()) could be used also to reinitialize the card in case
100 of plug-off plug-in.
101
102 (#) Configure the SD Card Data transfer frequency. You can change or adapt this
103 frequency by adjusting the "ClockDiv" field.
104 In transfer mode and according to the SD Card standard, make sure that the
105 SDMMC_CK frequency doesn't exceed 25MHz and 100MHz in High-speed mode switch.
106
107 (#) Select the corresponding SD Card according to the address read with the step 2.
108
109 (#) Configure the SD Card in wide bus mode: 4-bits data.
110
111 *** SD Card Read operation ***
112 ==============================
113 [..]
114 (+) You can read from SD card in polling mode by using function HAL_SD_ReadBlocks().
115 This function support only 512-bytes block length (the block size should be
116 chosen as 512 bytes).
117 You can choose either one block read operation or multiple block read operation
118 by adjusting the "NumberOfBlocks" parameter.
119 After this, you have to ensure that the transfer is done correctly. The check is done
120 through HAL_SD_GetCardState() function for SD card state.
121
122 (+) You can read from SD card in DMA mode by using function HAL_SD_ReadBlocks_DMA().
123 This function support only 512-bytes block length (the block size should be
124 chosen as 512 bytes).
125 You can choose either one block read operation or multiple block read operation
126 by adjusting the "NumberOfBlocks" parameter.
127 After this, you have to ensure that the transfer is done correctly. The check is done
128 through HAL_SD_GetCardState() function for SD card state.
129 You could also check the DMA transfer process through the SD Rx interrupt event.
130
131 (+) You can read from SD card in Interrupt mode by using function HAL_SD_ReadBlocks_IT().
132 This function support only 512-bytes block length (the block size should be
133 chosen as 512 bytes).
134 You can choose either one block read operation or multiple block read operation
135 by adjusting the "NumberOfBlocks" parameter.
136 After this, you have to ensure that the transfer is done correctly. The check is done
137 through HAL_SD_GetCardState() function for SD card state.
138 You could also check the IT transfer process through the SD Rx interrupt event.
139
140 *** SD Card Write operation ***
141 ===============================
142 [..]
143 (+) You can write to SD card in polling mode by using function HAL_SD_WriteBlocks().
144 This function support only 512-bytes block length (the block size should be
145 chosen as 512 bytes).
146 You can choose either one block read operation or multiple block read operation
147 by adjusting the "NumberOfBlocks" parameter.
148 After this, you have to ensure that the transfer is done correctly. The check is done
149 through HAL_SD_GetCardState() function for SD card state.
150
151 (+) You can write to SD card in DMA mode by using function HAL_SD_WriteBlocks_DMA().
152 This function support only 512-bytes block length (the block size should be
153 chosen as 512 bytes).
154 You can choose either one block read operation or multiple block read operation
155 by adjusting the "NumberOfBlocks" parameter.
156 After this, you have to ensure that the transfer is done correctly. The check is done
157 through HAL_SD_GetCardState() function for SD card state.
158 You could also check the DMA transfer process through the SD Tx interrupt event.
159
160 (+) You can write to SD card in Interrupt mode by using function HAL_SD_WriteBlocks_IT().
161 This function support only 512-bytes block length (the block size should be
162 chosen as 512 bytes).
163 You can choose either one block read operation or multiple block read operation
164 by adjusting the "NumberOfBlocks" parameter.
165 After this, you have to ensure that the transfer is done correctly. The check is done
166 through HAL_SD_GetCardState() function for SD card state.
167 You could also check the IT transfer process through the SD Tx interrupt event.
168
169 *** SD card status ***
170 ======================
171 [..]
172 (+) The SD Status contains status bits that are related to the SD Memory
173 Card proprietary features. To get SD card status use the HAL_SD_GetCardStatus().
174
175 *** SD card information ***
176 ===========================
177 [..]
178 (+) To get SD card information, you can use the function HAL_SD_GetCardInfo().
179 It returns useful information about the SD card such as block size, card type,
180 block number ...
181
182 *** SD card CSD register ***
183 ============================
184 (+) The HAL_SD_GetCardCSD() API allows to get the parameters of the CSD register.
185 Some of the CSD parameters are useful for card initialization and identification.
186
187 *** SD card CID register ***
188 ============================
189 (+) The HAL_SD_GetCardCID() API allows to get the parameters of the CID register.
190 Some of the CSD parameters are useful for card initialization and identification.
191
192 *** SD HAL driver macros list ***
193 ==================================
194 [..]
195 Below the list of most used macros in SD HAL driver.
196
197 (+) __HAL_SD_ENABLE : Enable the SD device
198 (+) __HAL_SD_DISABLE : Disable the SD device
199 (+) __HAL_SD_DMA_ENABLE: Enable the SDMMC DMA transfer
200 (+) __HAL_SD_DMA_DISABLE: Disable the SDMMC DMA transfer
201 (+) __HAL_SD_ENABLE_IT: Enable the SD device interrupt
202 (+) __HAL_SD_DISABLE_IT: Disable the SD device interrupt
203 (+) __HAL_SD_GET_FLAG:Check whether the specified SD flag is set or not
204 (+) __HAL_SD_CLEAR_FLAG: Clear the SD's pending flags
205
206 (@) You can refer to the SD HAL driver header file for more useful macros
207
208 *** Callback registration ***
209 =============================================
210 [..]
211 The compilation define USE_HAL_SD_REGISTER_CALLBACKS when set to 1
212 allows the user to configure dynamically the driver callbacks.
213
214 Use Functions HAL_SD_RegisterCallback() to register a user callback,
215 it allows to register following callbacks:
216 (+) TxCpltCallback : callback when a transmission transfer is completed.
217 (+) RxCpltCallback : callback when a reception transfer is completed.
218 (+) ErrorCallback : callback when error occurs.
219 (+) AbortCpltCallback : callback when abort is completed.
220 (+) Read_DMADblBuf0CpltCallback : callback when the DMA reception of first buffer is completed.
221 (+) Read_DMADblBuf1CpltCallback : callback when the DMA reception of second buffer is completed.
222 (+) Write_DMADblBuf0CpltCallback : callback when the DMA transmission of first buffer is completed.
223 (+) Write_DMADblBuf1CpltCallback : callback when the DMA transmission of second buffer is completed.
224 (+) MspInitCallback : SD MspInit.
225 (+) MspDeInitCallback : SD MspDeInit.
226 This function takes as parameters the HAL peripheral handle, the Callback ID
227 and a pointer to the user callback function.
228 For specific callbacks TransceiverCallback use dedicated register callbacks:
229 respectively HAL_SD_RegisterTransceiverCallback().
230
231 Use function HAL_SD_UnRegisterCallback() to reset a callback to the default
232 weak (surcharged) function. It allows to reset following callbacks:
233 (+) TxCpltCallback : callback when a transmission transfer is completed.
234 (+) RxCpltCallback : callback when a reception transfer is completed.
235 (+) ErrorCallback : callback when error occurs.
236 (+) AbortCpltCallback : callback when abort is completed.
237 (+) Read_DMADblBuf0CpltCallback : callback when the DMA reception of first buffer is completed.
238 (+) Read_DMADblBuf1CpltCallback : callback when the DMA reception of second buffer is completed.
239 (+) Write_DMADblBuf0CpltCallback : callback when the DMA transmission of first buffer is completed.
240 (+) Write_DMADblBuf1CpltCallback : callback when the DMA transmission of second buffer is completed.
241 (+) MspInitCallback : SD MspInit.
242 (+) MspDeInitCallback : SD MspDeInit.
243 This function) takes as parameters the HAL peripheral handle and the Callback ID.
244 For specific callbacks TransceiverCallback use dedicated unregister callbacks:
245 respectively HAL_SD_UnRegisterTransceiverCallback().
246
247 By default, after the HAL_SD_Init and if the state is HAL_SD_STATE_RESET
248 all callbacks are reset to the corresponding legacy weak (surcharged) functions.
249 Exception done for MspInit and MspDeInit callbacks that are respectively
250 reset to the legacy weak (surcharged) functions in the HAL_SD_Init
251 and HAL_SD_DeInit only when these callbacks are null (not registered beforehand).
252 If not, MspInit or MspDeInit are not null, the HAL_SD_Init and HAL_SD_DeInit
253 keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
254
255 Callbacks can be registered/unregistered in READY state only.
256 Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
257 in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
258 during the Init/DeInit.
259 In that case first register the MspInit/MspDeInit user callbacks
260 using HAL_SD_RegisterCallback before calling HAL_SD_DeInit
261 or HAL_SD_Init function.
262
263 When The compilation define USE_HAL_SD_REGISTER_CALLBACKS is set to 0 or
264 not defined, the callback registering feature is not available
265 and weak (surcharged) callbacks are used.
266
267 @endverbatim
268 ******************************************************************************
269 */
270
271 /* Includes ------------------------------------------------------------------*/
272 #include "stm32l4xx_hal.h"
273
274 #if defined(SDMMC1)
275
276 /** @addtogroup STM32L4xx_HAL_Driver
277 * @{
278 */
279
280 /** @addtogroup SD
281 * @{
282 */
283
284 #ifdef HAL_SD_MODULE_ENABLED
285
286 /* Private typedef -----------------------------------------------------------*/
287 /* Private define ------------------------------------------------------------*/
288 /** @addtogroup SD_Private_Defines
289 * @{
290 */
291 /* Frequencies used in the driver for clock divider calculation */
292 #define SD_INIT_FREQ 400000U /* Initialization phase : 400 kHz max */
293 #define SD_NORMAL_SPEED_FREQ 25000000U /* Normal speed phase : 25 MHz max */
294 #define SD_HIGH_SPEED_FREQ 50000000U /* High speed phase : 50 MHz max */
295 /**
296 * @}
297 */
298
299 /* Private macro -------------------------------------------------------------*/
300 /* Private variables ---------------------------------------------------------*/
301 /* Private function prototypes -----------------------------------------------*/
302 /* Private functions ---------------------------------------------------------*/
303 /** @defgroup SD_Private_Functions SD Private Functions
304 * @{
305 */
306 static uint32_t SD_InitCard (SD_HandleTypeDef *hsd);
307 static uint32_t SD_PowerON (SD_HandleTypeDef *hsd);
308 static uint32_t SD_SendSDStatus (SD_HandleTypeDef *hsd, uint32_t *pSDstatus);
309 static uint32_t SD_SendStatus (SD_HandleTypeDef *hsd, uint32_t *pCardStatus);
310 static uint32_t SD_WideBus_Enable (SD_HandleTypeDef *hsd);
311 static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd);
312 static uint32_t SD_FindSCR (SD_HandleTypeDef *hsd, uint32_t *pSCR);
313 static void SD_PowerOFF (SD_HandleTypeDef *hsd);
314 static void SD_Write_IT (SD_HandleTypeDef *hsd);
315 static void SD_Read_IT (SD_HandleTypeDef *hsd);
316 #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
317 static void SD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
318 static void SD_DMAReceiveCplt (DMA_HandleTypeDef *hdma);
319 static void SD_DMAError (DMA_HandleTypeDef *hdma);
320 static void SD_DMATxAbort (DMA_HandleTypeDef *hdma);
321 static void SD_DMARxAbort (DMA_HandleTypeDef *hdma);
322 #else
323 static uint32_t SD_UltraHighSpeed (SD_HandleTypeDef *hsd);
324 static uint32_t SD_DDR_Mode (SD_HandleTypeDef *hsd);
325 #endif /* !STM32L4P5xx && !STM32L4Q5xx && !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
326 /**
327 * @}
328 */
329
330 /* Exported functions --------------------------------------------------------*/
331 /** @addtogroup SD_Exported_Functions
332 * @{
333 */
334
335 /** @addtogroup SD_Exported_Functions_Group1
336 * @brief Initialization and de-initialization functions
337 *
338 @verbatim
339 ==============================================================================
340 ##### Initialization and de-initialization functions #####
341 ==============================================================================
342 [..]
343 This section provides functions allowing to initialize/de-initialize the SD
344 card device to be ready for use.
345
346 @endverbatim
347 * @{
348 */
349
350 /**
351 * @brief Initializes the SD according to the specified parameters in the
352 SD_HandleTypeDef and create the associated handle.
353 * @param hsd Pointer to the SD handle
354 * @retval HAL status
355 */
HAL_SD_Init(SD_HandleTypeDef * hsd)356 HAL_StatusTypeDef HAL_SD_Init(SD_HandleTypeDef *hsd)
357 {
358 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
359 HAL_SD_CardStatusTypeDef CardStatus;
360 uint32_t speedgrade, unitsize;
361 uint32_t tickstart;
362 #endif
363
364 /* Check the SD handle allocation */
365 if(hsd == NULL)
366 {
367 return HAL_ERROR;
368 }
369
370 /* Check the parameters */
371 assert_param(IS_SDMMC_ALL_INSTANCE(hsd->Instance));
372 assert_param(IS_SDMMC_CLOCK_EDGE(hsd->Init.ClockEdge));
373 #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
374 assert_param(IS_SDMMC_CLOCK_BYPASS(hsd->Init.ClockBypass));
375 #endif /* !STM32L4P5xx && !STM32L4Q5xx && !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
376 assert_param(IS_SDMMC_CLOCK_POWER_SAVE(hsd->Init.ClockPowerSave));
377 assert_param(IS_SDMMC_BUS_WIDE(hsd->Init.BusWide));
378 assert_param(IS_SDMMC_HARDWARE_FLOW_CONTROL(hsd->Init.HardwareFlowControl));
379 assert_param(IS_SDMMC_CLKDIV(hsd->Init.ClockDiv));
380
381 if(hsd->State == HAL_SD_STATE_RESET)
382 {
383 /* Allocate lock resource and initialize it */
384 hsd->Lock = HAL_UNLOCKED;
385 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
386 /* Reset Callback pointers in HAL_SD_STATE_RESET only */
387 hsd->TxCpltCallback = HAL_SD_TxCpltCallback;
388 hsd->RxCpltCallback = HAL_SD_RxCpltCallback;
389 hsd->ErrorCallback = HAL_SD_ErrorCallback;
390 hsd->AbortCpltCallback = HAL_SD_AbortCallback;
391 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
392 hsd->Read_DMADblBuf0CpltCallback = HAL_SDEx_Read_DMADoubleBuffer0CpltCallback;
393 hsd->Read_DMADblBuf1CpltCallback = HAL_SDEx_Read_DMADoubleBuffer1CpltCallback;
394 hsd->Write_DMADblBuf0CpltCallback = HAL_SDEx_Write_DMADoubleBuffer0CpltCallback;
395 hsd->Write_DMADblBuf1CpltCallback = HAL_SDEx_Write_DMADoubleBuffer1CpltCallback;
396 hsd->DriveTransceiver_1_8V_Callback = HAL_SDEx_DriveTransceiver_1_8V_Callback;
397 #endif
398
399 if(hsd->MspInitCallback == NULL)
400 {
401 hsd->MspInitCallback = HAL_SD_MspInit;
402 }
403
404 /* Init the low level hardware */
405 hsd->MspInitCallback(hsd);
406 #else
407 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
408 HAL_SD_MspInit(hsd);
409 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
410 }
411
412 hsd->State = HAL_SD_STATE_BUSY;
413
414 /* Initialize the Card parameters */
415 if (HAL_SD_InitCard(hsd) != HAL_OK)
416 {
417 return HAL_ERROR;
418 }
419
420 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
421 if( HAL_SD_GetCardStatus(hsd, &CardStatus) != HAL_OK)
422 {
423 return HAL_ERROR;
424 }
425 /* Get Initial Card Speed from Card Status*/
426 speedgrade = CardStatus.UhsSpeedGrade;
427 unitsize = CardStatus.UhsAllocationUnitSize;
428 if ((hsd->SdCard.CardType == CARD_SDHC_SDXC) && ((speedgrade != 0U) || (unitsize != 0U)))
429 {
430 hsd->SdCard.CardSpeed = CARD_ULTRA_HIGH_SPEED;
431 }
432 else
433 {
434 if (hsd->SdCard.CardType == CARD_SDHC_SDXC)
435 {
436 hsd->SdCard.CardSpeed = CARD_HIGH_SPEED;
437 }
438 else
439 {
440 hsd->SdCard.CardSpeed = CARD_NORMAL_SPEED;
441 }
442
443 }
444 /* Configure the bus wide */
445 if(HAL_SD_ConfigWideBusOperation(hsd, hsd->Init.BusWide) != HAL_OK)
446 {
447 return HAL_ERROR;
448 }
449
450 /* Verify that SD card is ready to use after Initialization */
451 tickstart = HAL_GetTick();
452 while((HAL_SD_GetCardState(hsd) != HAL_SD_CARD_TRANSFER))
453 {
454 if((HAL_GetTick()-tickstart) >= SDMMC_DATATIMEOUT)
455 {
456 hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
457 hsd->State= HAL_SD_STATE_READY;
458 hsd->Context = SD_CONTEXT_NONE;
459 return HAL_TIMEOUT;
460 }
461 }
462 #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
463
464 /* Configure the bus wide with the specified value in the SD_HandleTypeDef */
465 if(HAL_SD_ConfigWideBusOperation(hsd, hsd->Init.BusWide) != HAL_OK)
466 {
467 return HAL_ERROR;
468 }
469
470 /* Initialize the error code */
471 hsd->ErrorCode = HAL_SD_ERROR_NONE;
472
473 /* Initialize the SD operation */
474 hsd->Context = SD_CONTEXT_NONE;
475
476 /* Initialize the SD state */
477 hsd->State = HAL_SD_STATE_READY;
478
479 return HAL_OK;
480 }
481
482 /**
483 * @brief Initializes the SD Card.
484 * @param hsd Pointer to SD handle
485 * @note This function initializes the SD card. It could be used when a card
486 re-initialization is needed.
487 * @retval HAL status
488 */
HAL_SD_InitCard(SD_HandleTypeDef * hsd)489 HAL_StatusTypeDef HAL_SD_InitCard(SD_HandleTypeDef *hsd)
490 {
491 uint32_t errorstate;
492 SD_InitTypeDef Init;
493 uint32_t sdmmc_clk;
494
495 /* Default SDMMC peripheral configuration for SD card initialization */
496 Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
497 #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
498 Init.ClockBypass = SDMMC_CLOCK_BYPASS_DISABLE;
499 #endif /* !STM32L4P5xx && !STM32L4Q5xx && !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
500 Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
501 Init.BusWide = SDMMC_BUS_WIDE_1B;
502 Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE;
503
504 /* Init Clock should be less or equal to 400Khz*/
505 sdmmc_clk = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_SDMMC1);
506 if (sdmmc_clk == 0U)
507 {
508 hsd->State = HAL_SD_STATE_READY;
509 hsd->ErrorCode = SDMMC_ERROR_INVALID_PARAMETER;
510 return HAL_ERROR;
511 }
512 #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
513 Init.ClockDiv = ((sdmmc_clk / SD_INIT_FREQ) - 2U);
514 #else
515 Init.ClockDiv = sdmmc_clk / (2U * SD_INIT_FREQ);
516 #endif
517
518 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
519 Init.Transceiver = hsd->Init.Transceiver;
520 if(hsd->Init.Transceiver == SDMMC_TRANSCEIVER_ENABLE)
521 {
522 /* Set Transceiver polarity */
523 hsd->Instance->POWER |= SDMMC_POWER_DIRPOL;
524 }
525 #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
526
527 /* Initialize SDMMC peripheral interface with default configuration */
528 (void)SDMMC_Init(hsd->Instance, Init);
529
530 #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
531 /* Disable SDMMC Clock */
532 __HAL_SD_DISABLE(hsd);
533 #endif /* !STM32L4P5xx && !STM32L4Q5xx && !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
534
535 /* Set Power State to ON */
536 (void)SDMMC_PowerState_ON(hsd->Instance);
537
538 #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
539 /* Enable SDMMC Clock */
540 __HAL_SD_ENABLE(hsd);
541 #endif /* !STM32L4P5xx && !STM32L4Q5xx && !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
542
543 /* wait 74 Cycles: required power up waiting time before starting
544 the SD initialization sequence */
545 #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
546 sdmmc_clk = sdmmc_clk/(Init.ClockDiv + 2U);
547 #else
548 sdmmc_clk = sdmmc_clk/(2U*Init.ClockDiv);
549 #endif
550 HAL_Delay(1U+ (74U*1000U/(sdmmc_clk)));
551
552 /* Identify card operating voltage */
553 errorstate = SD_PowerON(hsd);
554 if(errorstate != HAL_SD_ERROR_NONE)
555 {
556 hsd->State = HAL_SD_STATE_READY;
557 hsd->ErrorCode |= errorstate;
558 return HAL_ERROR;
559 }
560
561 /* Card initialization */
562 errorstate = SD_InitCard(hsd);
563 if(errorstate != HAL_SD_ERROR_NONE)
564 {
565 hsd->State = HAL_SD_STATE_READY;
566 hsd->ErrorCode |= errorstate;
567 return HAL_ERROR;
568 }
569
570 /* Set Block Size for Card */
571 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
572 if(errorstate != HAL_SD_ERROR_NONE)
573 {
574 /* Clear all the static flags */
575 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
576 hsd->ErrorCode |= errorstate;
577 hsd->State = HAL_SD_STATE_READY;
578 return HAL_ERROR;
579 }
580
581 return HAL_OK;
582 }
583
584 /**
585 * @brief De-Initializes the SD card.
586 * @param hsd Pointer to SD handle
587 * @retval HAL status
588 */
HAL_SD_DeInit(SD_HandleTypeDef * hsd)589 HAL_StatusTypeDef HAL_SD_DeInit(SD_HandleTypeDef *hsd)
590 {
591 /* Check the SD handle allocation */
592 if(hsd == NULL)
593 {
594 return HAL_ERROR;
595 }
596
597 /* Check the parameters */
598 assert_param(IS_SDMMC_ALL_INSTANCE(hsd->Instance));
599
600 hsd->State = HAL_SD_STATE_BUSY;
601
602 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
603 /* Deactivate the 1.8V Mode */
604 if(hsd->Init.Transceiver == SDMMC_TRANSCEIVER_ENABLE)
605 {
606 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
607 if(hsd->DriveTransceiver_1_8V_Callback == NULL)
608 {
609 hsd->DriveTransceiver_1_8V_Callback = HAL_SDEx_DriveTransceiver_1_8V_Callback;
610 }
611 hsd->DriveTransceiver_1_8V_Callback(RESET);
612 #else
613 HAL_SDEx_DriveTransceiver_1_8V_Callback(RESET);
614 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
615 }
616 #endif
617
618 /* Set SD power state to off */
619 SD_PowerOFF(hsd);
620
621 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
622 if(hsd->MspDeInitCallback == NULL)
623 {
624 hsd->MspDeInitCallback = HAL_SD_MspDeInit;
625 }
626
627 /* DeInit the low level hardware */
628 hsd->MspDeInitCallback(hsd);
629 #else
630 /* De-Initialize the MSP layer */
631 HAL_SD_MspDeInit(hsd);
632 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
633
634 hsd->ErrorCode = HAL_SD_ERROR_NONE;
635 hsd->State = HAL_SD_STATE_RESET;
636
637 return HAL_OK;
638 }
639
640
641 /**
642 * @brief Initializes the SD MSP.
643 * @param hsd Pointer to SD handle
644 * @retval None
645 */
HAL_SD_MspInit(SD_HandleTypeDef * hsd)646 __weak void HAL_SD_MspInit(SD_HandleTypeDef *hsd)
647 {
648 /* Prevent unused argument(s) compilation warning */
649 UNUSED(hsd);
650
651 /* NOTE : This function should not be modified, when the callback is needed,
652 the HAL_SD_MspInit could be implemented in the user file
653 */
654 }
655
656 /**
657 * @brief De-Initialize SD MSP.
658 * @param hsd Pointer to SD handle
659 * @retval None
660 */
HAL_SD_MspDeInit(SD_HandleTypeDef * hsd)661 __weak void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd)
662 {
663 /* Prevent unused argument(s) compilation warning */
664 UNUSED(hsd);
665
666 /* NOTE : This function should not be modified, when the callback is needed,
667 the HAL_SD_MspDeInit could be implemented in the user file
668 */
669 }
670
671 /**
672 * @}
673 */
674
675 /** @addtogroup SD_Exported_Functions_Group2
676 * @brief Data transfer functions
677 *
678 @verbatim
679 ==============================================================================
680 ##### IO operation functions #####
681 ==============================================================================
682 [..]
683 This subsection provides a set of functions allowing to manage the data
684 transfer from/to SD card.
685
686 @endverbatim
687 * @{
688 */
689
690 /**
691 * @brief Reads block(s) from a specified address in a card. The Data transfer
692 * is managed by polling mode.
693 * @note This API should be followed by a check on the card state through
694 * HAL_SD_GetCardState().
695 * @param hsd Pointer to SD handle
696 * @param pData pointer to the buffer that will contain the received data
697 * @param BlockAdd Block Address from where data is to be read
698 * @param NumberOfBlocks Number of SD blocks to read
699 * @param Timeout Specify timeout value
700 * @retval HAL status
701 */
HAL_SD_ReadBlocks(SD_HandleTypeDef * hsd,uint8_t * pData,uint32_t BlockAdd,uint32_t NumberOfBlocks,uint32_t Timeout)702 HAL_StatusTypeDef HAL_SD_ReadBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
703 {
704 SDMMC_DataInitTypeDef config;
705 uint32_t errorstate;
706 uint32_t tickstart = HAL_GetTick();
707 uint32_t count, data, dataremaining;
708 uint32_t add = BlockAdd;
709 uint8_t *tempbuff = pData;
710
711 if(NULL == pData)
712 {
713 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
714 return HAL_ERROR;
715 }
716
717 if(hsd->State == HAL_SD_STATE_READY)
718 {
719 hsd->ErrorCode = HAL_SD_ERROR_NONE;
720
721 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
722 {
723 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
724 return HAL_ERROR;
725 }
726
727 hsd->State = HAL_SD_STATE_BUSY;
728
729 /* Initialize data control register */
730 hsd->Instance->DCTRL = 0U;
731
732 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
733 {
734 add *= 512U;
735 }
736
737 /* Configure the SD DPSM (Data Path State Machine) */
738 config.DataTimeOut = SDMMC_DATATIMEOUT;
739 config.DataLength = NumberOfBlocks * BLOCKSIZE;
740 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
741 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
742 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
743 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
744 config.DPSM = SDMMC_DPSM_DISABLE;
745 #else
746 config.DPSM = SDMMC_DPSM_ENABLE;
747 #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
748 (void)SDMMC_ConfigData(hsd->Instance, &config);
749 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
750 __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
751 #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
752
753 /* Read block(s) in polling mode */
754 if(NumberOfBlocks > 1U)
755 {
756 hsd->Context = SD_CONTEXT_READ_MULTIPLE_BLOCK;
757
758 /* Read Multi Block command */
759 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
760 }
761 else
762 {
763 hsd->Context = SD_CONTEXT_READ_SINGLE_BLOCK;
764
765 /* Read Single Block command */
766 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
767 }
768 if(errorstate != HAL_SD_ERROR_NONE)
769 {
770 /* Clear all the static flags */
771 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
772 hsd->ErrorCode |= errorstate;
773 hsd->State = HAL_SD_STATE_READY;
774 hsd->Context = SD_CONTEXT_NONE;
775 return HAL_ERROR;
776 }
777
778 /* Poll on SDMMC flags */
779 dataremaining = config.DataLength;
780 while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
781 {
782 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF) && (dataremaining > 0U))
783 {
784 /* Read data from SDMMC Rx FIFO */
785 for(count = 0U; count < 8U; count++)
786 {
787 data = SDMMC_ReadFIFO(hsd->Instance);
788 *tempbuff = (uint8_t)(data & 0xFFU);
789 tempbuff++;
790 dataremaining--;
791 *tempbuff = (uint8_t)((data >> 8U) & 0xFFU);
792 tempbuff++;
793 dataremaining--;
794 *tempbuff = (uint8_t)((data >> 16U) & 0xFFU);
795 tempbuff++;
796 dataremaining--;
797 *tempbuff = (uint8_t)((data >> 24U) & 0xFFU);
798 tempbuff++;
799 dataremaining--;
800 }
801 }
802
803 if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
804 {
805 /* Clear all the static flags */
806 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
807 hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT;
808 hsd->State= HAL_SD_STATE_READY;
809 hsd->Context = SD_CONTEXT_NONE;
810 return HAL_TIMEOUT;
811 }
812 }
813 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
814 __SDMMC_CMDTRANS_DISABLE( hsd->Instance);
815 #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
816
817 /* Send stop transmission command in case of multiblock read */
818 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1U))
819 {
820 if(hsd->SdCard.CardType != CARD_SECURED)
821 {
822 /* Send stop transmission command */
823 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
824 if(errorstate != HAL_SD_ERROR_NONE)
825 {
826 /* Clear all the static flags */
827 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
828 hsd->ErrorCode |= errorstate;
829 hsd->State = HAL_SD_STATE_READY;
830 hsd->Context = SD_CONTEXT_NONE;
831 return HAL_ERROR;
832 }
833 }
834 }
835
836 /* Get error state */
837 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
838 {
839 /* Clear all the static flags */
840 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
841 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
842 hsd->State = HAL_SD_STATE_READY;
843 hsd->Context = SD_CONTEXT_NONE;
844 return HAL_ERROR;
845 }
846 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
847 {
848 /* Clear all the static flags */
849 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
850 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
851 hsd->State = HAL_SD_STATE_READY;
852 hsd->Context = SD_CONTEXT_NONE;
853 return HAL_ERROR;
854 }
855 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
856 {
857 /* Clear all the static flags */
858 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
859 hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
860 hsd->State = HAL_SD_STATE_READY;
861 hsd->Context = SD_CONTEXT_NONE;
862 return HAL_ERROR;
863 }
864 else
865 {
866 /* Nothing to do */
867 }
868
869 #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
870 /* Empty FIFO if there is still any data */
871 while ((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL)) && (dataremaining > 0U))
872 {
873 data = SDMMC_ReadFIFO(hsd->Instance);
874 *tempbuff = (uint8_t)(data & 0xFFU);
875 tempbuff++;
876 dataremaining--;
877 *tempbuff = (uint8_t)((data >> 8U) & 0xFFU);
878 tempbuff++;
879 dataremaining--;
880 *tempbuff = (uint8_t)((data >> 16U) & 0xFFU);
881 tempbuff++;
882 dataremaining--;
883 *tempbuff = (uint8_t)((data >> 24U) & 0xFFU);
884 tempbuff++;
885 dataremaining--;
886
887 if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
888 {
889 /* Clear all the static flags */
890 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
891 hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT;
892 hsd->State= HAL_SD_STATE_READY;
893 hsd->Context = SD_CONTEXT_NONE;
894 return HAL_ERROR;
895 }
896 }
897 #endif /* !STM32L4P5xx && !STM32L4Q5xx && !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
898
899 /* Clear all the static flags */
900 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
901
902 hsd->State = HAL_SD_STATE_READY;
903
904 return HAL_OK;
905 }
906 else
907 {
908 hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
909 return HAL_ERROR;
910 }
911 }
912
913 /**
914 * @brief Allows to write block(s) to a specified address in a card. The Data
915 * transfer is managed by polling mode.
916 * @note This API should be followed by a check on the card state through
917 * HAL_SD_GetCardState().
918 * @param hsd Pointer to SD handle
919 * @param pData pointer to the buffer that will contain the data to transmit
920 * @param BlockAdd Block Address where data will be written
921 * @param NumberOfBlocks Number of SD blocks to write
922 * @param Timeout Specify timeout value
923 * @retval HAL status
924 */
HAL_SD_WriteBlocks(SD_HandleTypeDef * hsd,uint8_t * pData,uint32_t BlockAdd,uint32_t NumberOfBlocks,uint32_t Timeout)925 HAL_StatusTypeDef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
926 {
927 SDMMC_DataInitTypeDef config;
928 uint32_t errorstate;
929 uint32_t tickstart = HAL_GetTick();
930 uint32_t count, data, dataremaining;
931 uint32_t add = BlockAdd;
932 uint8_t *tempbuff = pData;
933
934 if(NULL == pData)
935 {
936 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
937 return HAL_ERROR;
938 }
939
940 if(hsd->State == HAL_SD_STATE_READY)
941 {
942 hsd->ErrorCode = HAL_SD_ERROR_NONE;
943
944 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
945 {
946 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
947 return HAL_ERROR;
948 }
949
950 hsd->State = HAL_SD_STATE_BUSY;
951
952 /* Initialize data control register */
953 hsd->Instance->DCTRL = 0U;
954
955 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
956 {
957 add *= 512U;
958 }
959
960 /* Configure the SD DPSM (Data Path State Machine) */
961 config.DataTimeOut = SDMMC_DATATIMEOUT;
962 config.DataLength = NumberOfBlocks * BLOCKSIZE;
963 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
964 config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
965 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
966 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
967 config.DPSM = SDMMC_DPSM_DISABLE;
968 #else
969 config.DPSM = SDMMC_DPSM_ENABLE;
970 #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
971 (void)SDMMC_ConfigData(hsd->Instance, &config);
972 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
973 __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
974 #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
975
976 /* Write Blocks in Polling mode */
977 if(NumberOfBlocks > 1U)
978 {
979 hsd->Context = SD_CONTEXT_WRITE_MULTIPLE_BLOCK;
980
981 /* Write Multi Block command */
982 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
983 }
984 else
985 {
986 hsd->Context = SD_CONTEXT_WRITE_SINGLE_BLOCK;
987
988 /* Write Single Block command */
989 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
990 }
991 if(errorstate != HAL_SD_ERROR_NONE)
992 {
993 /* Clear all the static flags */
994 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
995 hsd->ErrorCode |= errorstate;
996 hsd->State = HAL_SD_STATE_READY;
997 hsd->Context = SD_CONTEXT_NONE;
998 return HAL_ERROR;
999 }
1000
1001 /* Write block(s) in polling mode */
1002 dataremaining = config.DataLength;
1003 while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
1004 {
1005 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXFIFOHE) && (dataremaining > 0U))
1006 {
1007 /* Write data to SDMMC Tx FIFO */
1008 for(count = 0U; count < 8U; count++)
1009 {
1010 data = (uint32_t)(*tempbuff);
1011 tempbuff++;
1012 dataremaining--;
1013 data |= ((uint32_t)(*tempbuff) << 8U);
1014 tempbuff++;
1015 dataremaining--;
1016 data |= ((uint32_t)(*tempbuff) << 16U);
1017 tempbuff++;
1018 dataremaining--;
1019 data |= ((uint32_t)(*tempbuff) << 24U);
1020 tempbuff++;
1021 dataremaining--;
1022 (void)SDMMC_WriteFIFO(hsd->Instance, &data);
1023 }
1024 }
1025
1026 if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
1027 {
1028 /* Clear all the static flags */
1029 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1030 hsd->ErrorCode |= errorstate;
1031 hsd->State = HAL_SD_STATE_READY;
1032 hsd->Context = SD_CONTEXT_NONE;
1033 return HAL_TIMEOUT;
1034 }
1035 }
1036 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1037 __SDMMC_CMDTRANS_DISABLE( hsd->Instance);
1038 #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
1039
1040 /* Send stop transmission command in case of multiblock write */
1041 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1U))
1042 {
1043 if(hsd->SdCard.CardType != CARD_SECURED)
1044 {
1045 /* Send stop transmission command */
1046 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
1047 if(errorstate != HAL_SD_ERROR_NONE)
1048 {
1049 /* Clear all the static flags */
1050 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1051 hsd->ErrorCode |= errorstate;
1052 hsd->State = HAL_SD_STATE_READY;
1053 hsd->Context = SD_CONTEXT_NONE;
1054 return HAL_ERROR;
1055 }
1056 }
1057 }
1058
1059 /* Get error state */
1060 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
1061 {
1062 /* Clear all the static flags */
1063 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1064 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
1065 hsd->State = HAL_SD_STATE_READY;
1066 hsd->Context = SD_CONTEXT_NONE;
1067 return HAL_ERROR;
1068 }
1069 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
1070 {
1071 /* Clear all the static flags */
1072 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1073 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
1074 hsd->State = HAL_SD_STATE_READY;
1075 hsd->Context = SD_CONTEXT_NONE;
1076 return HAL_ERROR;
1077 }
1078 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR))
1079 {
1080 /* Clear all the static flags */
1081 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1082 hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
1083 hsd->State = HAL_SD_STATE_READY;
1084 hsd->Context = SD_CONTEXT_NONE;
1085 return HAL_ERROR;
1086 }
1087 else
1088 {
1089 /* Nothing to do */
1090 }
1091
1092 /* Clear all the static flags */
1093 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
1094
1095 hsd->State = HAL_SD_STATE_READY;
1096
1097 return HAL_OK;
1098 }
1099 else
1100 {
1101 hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
1102 return HAL_ERROR;
1103 }
1104 }
1105
1106 /**
1107 * @brief Reads block(s) from a specified address in a card. The Data transfer
1108 * is managed in interrupt mode.
1109 * @note This API should be followed by a check on the card state through
1110 * HAL_SD_GetCardState().
1111 * @note You could also check the IT transfer process through the SD Rx
1112 * interrupt event.
1113 * @param hsd Pointer to SD handle
1114 * @param pData Pointer to the buffer that will contain the received data
1115 * @param BlockAdd Block Address from where data is to be read
1116 * @param NumberOfBlocks Number of blocks to read.
1117 * @retval HAL status
1118 */
HAL_SD_ReadBlocks_IT(SD_HandleTypeDef * hsd,uint8_t * pData,uint32_t BlockAdd,uint32_t NumberOfBlocks)1119 HAL_StatusTypeDef HAL_SD_ReadBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1120 {
1121 SDMMC_DataInitTypeDef config;
1122 uint32_t errorstate;
1123 uint32_t add = BlockAdd;
1124
1125 if(NULL == pData)
1126 {
1127 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1128 return HAL_ERROR;
1129 }
1130
1131 if(hsd->State == HAL_SD_STATE_READY)
1132 {
1133 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1134
1135 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1136 {
1137 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1138 return HAL_ERROR;
1139 }
1140
1141 hsd->State = HAL_SD_STATE_BUSY;
1142
1143 /* Initialize data control register */
1144 hsd->Instance->DCTRL = 0U;
1145
1146 hsd->pRxBuffPtr = pData;
1147 hsd->RxXferSize = BLOCKSIZE * NumberOfBlocks;
1148
1149 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1150 {
1151 add *= 512U;
1152 }
1153
1154 /* Configure the SD DPSM (Data Path State Machine) */
1155 config.DataTimeOut = SDMMC_DATATIMEOUT;
1156 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1157 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1158 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
1159 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1160 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1161 config.DPSM = SDMMC_DPSM_DISABLE;
1162 #else
1163 config.DPSM = SDMMC_DPSM_ENABLE;
1164 #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
1165 (void)SDMMC_ConfigData(hsd->Instance, &config);
1166 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1167 __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
1168 #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
1169
1170 /* Read Blocks in IT mode */
1171 if(NumberOfBlocks > 1U)
1172 {
1173 hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_IT);
1174
1175 /* Read Multi Block command */
1176 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
1177 }
1178 else
1179 {
1180 hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_IT);
1181
1182 /* Read Single Block command */
1183 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
1184 }
1185 if(errorstate != HAL_SD_ERROR_NONE)
1186 {
1187 /* Clear all the static flags */
1188 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1189 hsd->ErrorCode |= errorstate;
1190 hsd->State = HAL_SD_STATE_READY;
1191 hsd->Context = SD_CONTEXT_NONE;
1192 return HAL_ERROR;
1193 }
1194
1195 __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND | SDMMC_FLAG_RXFIFOHF));
1196
1197 return HAL_OK;
1198 }
1199 else
1200 {
1201 return HAL_BUSY;
1202 }
1203 }
1204
1205 /**
1206 * @brief Writes block(s) to a specified address in a card. The Data transfer
1207 * is managed in interrupt mode.
1208 * @note This API should be followed by a check on the card state through
1209 * HAL_SD_GetCardState().
1210 * @note You could also check the IT transfer process through the SD Tx
1211 * interrupt event.
1212 * @param hsd Pointer to SD handle
1213 * @param pData Pointer to the buffer that will contain the data to transmit
1214 * @param BlockAdd Block Address where data will be written
1215 * @param NumberOfBlocks Number of blocks to write
1216 * @retval HAL status
1217 */
HAL_SD_WriteBlocks_IT(SD_HandleTypeDef * hsd,uint8_t * pData,uint32_t BlockAdd,uint32_t NumberOfBlocks)1218 HAL_StatusTypeDef HAL_SD_WriteBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1219 {
1220 SDMMC_DataInitTypeDef config;
1221 uint32_t errorstate;
1222 uint32_t add = BlockAdd;
1223
1224 if(NULL == pData)
1225 {
1226 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1227 return HAL_ERROR;
1228 }
1229
1230 if(hsd->State == HAL_SD_STATE_READY)
1231 {
1232 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1233
1234 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1235 {
1236 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1237 return HAL_ERROR;
1238 }
1239
1240 hsd->State = HAL_SD_STATE_BUSY;
1241
1242 /* Initialize data control register */
1243 hsd->Instance->DCTRL = 0U;
1244
1245 hsd->pTxBuffPtr = pData;
1246 hsd->TxXferSize = BLOCKSIZE * NumberOfBlocks;
1247
1248 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1249 {
1250 add *= 512U;
1251 }
1252
1253 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1254 /* Configure the SD DPSM (Data Path State Machine) */
1255 config.DataTimeOut = SDMMC_DATATIMEOUT;
1256 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1257 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1258 config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
1259 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1260 config.DPSM = SDMMC_DPSM_DISABLE;
1261 (void)SDMMC_ConfigData(hsd->Instance, &config);
1262
1263 __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
1264 #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
1265
1266 /* Write Blocks in Polling mode */
1267 if(NumberOfBlocks > 1U)
1268 {
1269 hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK| SD_CONTEXT_IT);
1270
1271 /* Write Multi Block command */
1272 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
1273 }
1274 else
1275 {
1276 hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_IT);
1277
1278 /* Write Single Block command */
1279 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
1280 }
1281 if(errorstate != HAL_SD_ERROR_NONE)
1282 {
1283 /* Clear all the static flags */
1284 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1285 hsd->ErrorCode |= errorstate;
1286 hsd->State = HAL_SD_STATE_READY;
1287 hsd->Context = SD_CONTEXT_NONE;
1288 return HAL_ERROR;
1289 }
1290
1291 #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
1292 /* Configure the SD DPSM (Data Path State Machine) */
1293 config.DataTimeOut = SDMMC_DATATIMEOUT;
1294 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1295 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1296 config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
1297 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1298 config.DPSM = SDMMC_DPSM_ENABLE;
1299 (void)SDMMC_ConfigData(hsd->Instance, &config);
1300 #endif /* !STM32L4P5xx && !STM32L4Q5xx && !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
1301
1302 /* Enable transfer interrupts */
1303 __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND | SDMMC_FLAG_TXFIFOHE));
1304
1305 return HAL_OK;
1306 }
1307 else
1308 {
1309 return HAL_BUSY;
1310 }
1311 }
1312
1313 /**
1314 * @brief Reads block(s) from a specified address in a card. The Data transfer
1315 * is managed by DMA mode.
1316 * @note This API should be followed by a check on the card state through
1317 * HAL_SD_GetCardState().
1318 * @note You could also check the DMA transfer process through the SD Rx
1319 * interrupt event.
1320 * @param hsd Pointer SD handle
1321 * @param pData Pointer to the buffer that will contain the received data
1322 * @param BlockAdd Block Address from where data is to be read
1323 * @param NumberOfBlocks Number of blocks to read.
1324 * @retval HAL status
1325 */
HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef * hsd,uint8_t * pData,uint32_t BlockAdd,uint32_t NumberOfBlocks)1326 HAL_StatusTypeDef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1327 {
1328 SDMMC_DataInitTypeDef config;
1329 uint32_t errorstate;
1330 uint32_t add = BlockAdd;
1331
1332 if(NULL == pData)
1333 {
1334 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1335 return HAL_ERROR;
1336 }
1337
1338 if(hsd->State == HAL_SD_STATE_READY)
1339 {
1340 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1341
1342 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1343 {
1344 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1345 return HAL_ERROR;
1346 }
1347
1348 hsd->State = HAL_SD_STATE_BUSY;
1349
1350 /* Initialize data control register */
1351 hsd->Instance->DCTRL = 0U;
1352
1353 #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
1354 /* Set the DMA transfer complete callback */
1355 hsd->hdmarx->XferCpltCallback = SD_DMAReceiveCplt;
1356
1357 /* Set the DMA error callback */
1358 hsd->hdmarx->XferErrorCallback = SD_DMAError;
1359
1360 /* Set the DMA Abort callback */
1361 hsd->hdmarx->XferAbortCallback = NULL;
1362
1363 /* Enable the DMA Channel */
1364 if(HAL_DMA_Start_IT(hsd->hdmarx, (uint32_t)&hsd->Instance->FIFO, (uint32_t)pData, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4U) != HAL_OK)
1365 {
1366 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1367 hsd->ErrorCode |= HAL_SD_ERROR_DMA;
1368 hsd->State = HAL_SD_STATE_READY;
1369 return HAL_ERROR;
1370 }
1371 else
1372 {
1373 /* Enable SD DMA transfer */
1374 __HAL_SD_DMA_ENABLE(hsd);
1375 #else
1376 hsd->pRxBuffPtr = pData;
1377 hsd->RxXferSize = BLOCKSIZE * NumberOfBlocks;
1378 #endif /* !STM32L4P5xx && !STM32L4Q5xx && !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
1379
1380 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1381 {
1382 add *= 512U;
1383 }
1384
1385 /* Configure the SD DPSM (Data Path State Machine) */
1386 config.DataTimeOut = SDMMC_DATATIMEOUT;
1387 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1388 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1389 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
1390 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1391 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1392 config.DPSM = SDMMC_DPSM_DISABLE;
1393 #else
1394 config.DPSM = SDMMC_DPSM_ENABLE;
1395 #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
1396 (void)SDMMC_ConfigData(hsd->Instance, &config);
1397
1398 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1399
1400 __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
1401 hsd->Instance->IDMABASE0 = (uint32_t) pData ;
1402 hsd->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_SINGLE_BUFF;
1403 #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
1404
1405 /* Read Blocks in DMA mode */
1406 if(NumberOfBlocks > 1U)
1407 {
1408 hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
1409
1410 /* Read Multi Block command */
1411 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
1412 }
1413 else
1414 {
1415 hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_DMA);
1416
1417 /* Read Single Block command */
1418 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
1419 }
1420 if(errorstate != HAL_SD_ERROR_NONE)
1421 {
1422 /* Clear all the static flags */
1423 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1424 hsd->ErrorCode |= errorstate;
1425 hsd->State = HAL_SD_STATE_READY;
1426 hsd->Context = SD_CONTEXT_NONE;
1427 return HAL_ERROR;
1428 }
1429
1430 /* Enable transfer interrupts */
1431 __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND));
1432
1433 return HAL_OK;
1434 #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
1435 }
1436 #endif
1437 }
1438 else
1439 {
1440 return HAL_BUSY;
1441 }
1442 }
1443
1444 /**
1445 * @brief Writes block(s) to a specified address in a card. The Data transfer
1446 * is managed by DMA mode.
1447 * @note This API should be followed by a check on the card state through
1448 * HAL_SD_GetCardState().
1449 * @note You could also check the DMA transfer process through the SD Tx
1450 * interrupt event.
1451 * @param hsd Pointer to SD handle
1452 * @param pData Pointer to the buffer that will contain the data to transmit
1453 * @param BlockAdd Block Address where data will be written
1454 * @param NumberOfBlocks Number of blocks to write
1455 * @retval HAL status
1456 */
HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef * hsd,uint8_t * pData,uint32_t BlockAdd,uint32_t NumberOfBlocks)1457 HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1458 {
1459 SDMMC_DataInitTypeDef config;
1460 uint32_t errorstate;
1461 uint32_t add = BlockAdd;
1462
1463 if(NULL == pData)
1464 {
1465 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1466 return HAL_ERROR;
1467 }
1468
1469 if(hsd->State == HAL_SD_STATE_READY)
1470 {
1471 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1472
1473 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1474 {
1475 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1476 return HAL_ERROR;
1477 }
1478
1479 hsd->State = HAL_SD_STATE_BUSY;
1480
1481 /* Initialize data control register */
1482 hsd->Instance->DCTRL = 0U;
1483
1484 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1485 hsd->pTxBuffPtr = pData;
1486 hsd->TxXferSize = BLOCKSIZE * NumberOfBlocks;
1487 #else
1488 /* Set the DMA transfer complete callback */
1489 hsd->hdmatx->XferCpltCallback = SD_DMATransmitCplt;
1490
1491 /* Set the DMA error callback */
1492 hsd->hdmatx->XferErrorCallback = SD_DMAError;
1493
1494 /* Set the DMA Abort callback */
1495 hsd->hdmatx->XferAbortCallback = NULL;
1496 #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
1497
1498 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1499 {
1500 add *= 512U;
1501 }
1502
1503 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1504 /* Configure the SD DPSM (Data Path State Machine) */
1505 config.DataTimeOut = SDMMC_DATATIMEOUT;
1506 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1507 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1508 config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
1509 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1510 config.DPSM = SDMMC_DPSM_DISABLE;
1511 (void)SDMMC_ConfigData(hsd->Instance, &config);
1512
1513 __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
1514
1515 hsd->Instance->IDMABASE0 = (uint32_t) pData ;
1516 hsd->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_SINGLE_BUFF;
1517 #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
1518
1519 /* Write Blocks in Polling mode */
1520 if(NumberOfBlocks > 1U)
1521 {
1522 hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
1523
1524 /* Write Multi Block command */
1525 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
1526 }
1527 else
1528 {
1529 hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_DMA);
1530
1531 /* Write Single Block command */
1532 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
1533 }
1534 if(errorstate != HAL_SD_ERROR_NONE)
1535 {
1536 /* Clear all the static flags */
1537 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1538 hsd->ErrorCode |= errorstate;
1539 hsd->State = HAL_SD_STATE_READY;
1540 hsd->Context = SD_CONTEXT_NONE;
1541 return HAL_ERROR;
1542 }
1543
1544 #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
1545 /* Enable SDMMC DMA transfer */
1546 __HAL_SD_DMA_ENABLE(hsd);
1547
1548 /* Enable the DMA Channel */
1549 if(HAL_DMA_Start_IT(hsd->hdmatx, (uint32_t)pData, (uint32_t)&hsd->Instance->FIFO, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4U) != HAL_OK)
1550 {
1551 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1552 hsd->ErrorCode |= HAL_SD_ERROR_DMA;
1553 hsd->State = HAL_SD_STATE_READY;
1554 hsd->Context = SD_CONTEXT_NONE;
1555 return HAL_ERROR;
1556 }
1557 else
1558 {
1559 /* Configure the SD DPSM (Data Path State Machine) */
1560 config.DataTimeOut = SDMMC_DATATIMEOUT;
1561 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1562 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1563 config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
1564 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1565 config.DPSM = SDMMC_DPSM_ENABLE;
1566 (void)SDMMC_ConfigData(hsd->Instance, &config);
1567
1568 /* Enable SD Error interrupts */
1569 __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR));
1570 #else
1571 /* Enable transfer interrupts */
1572 __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND));
1573 #endif /* !STM32L4P5xx && !STM32L4Q5xx && !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
1574
1575 return HAL_OK;
1576 #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
1577 }
1578 #endif
1579 }
1580 else
1581 {
1582 return HAL_BUSY;
1583 }
1584 }
1585
1586 /**
1587 * @brief Erases the specified memory area of the given SD card.
1588 * @note This API should be followed by a check on the card state through
1589 * HAL_SD_GetCardState().
1590 * @param hsd Pointer to SD handle
1591 * @param BlockStartAdd Start Block address
1592 * @param BlockEndAdd End Block address
1593 * @retval HAL status
1594 */
HAL_SD_Erase(SD_HandleTypeDef * hsd,uint32_t BlockStartAdd,uint32_t BlockEndAdd)1595 HAL_StatusTypeDef HAL_SD_Erase(SD_HandleTypeDef *hsd, uint32_t BlockStartAdd, uint32_t BlockEndAdd)
1596 {
1597 uint32_t errorstate;
1598 uint32_t start_add = BlockStartAdd;
1599 uint32_t end_add = BlockEndAdd;
1600
1601 if(hsd->State == HAL_SD_STATE_READY)
1602 {
1603 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1604
1605 if(end_add < start_add)
1606 {
1607 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1608 return HAL_ERROR;
1609 }
1610
1611 if(end_add > (hsd->SdCard.LogBlockNbr))
1612 {
1613 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1614 return HAL_ERROR;
1615 }
1616
1617 hsd->State = HAL_SD_STATE_BUSY;
1618
1619 /* Check if the card command class supports erase command */
1620 if(((hsd->SdCard.Class) & SDMMC_CCCC_ERASE) == 0U)
1621 {
1622 /* Clear all the static flags */
1623 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1624 hsd->ErrorCode |= HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
1625 hsd->State = HAL_SD_STATE_READY;
1626 return HAL_ERROR;
1627 }
1628
1629 if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
1630 {
1631 /* Clear all the static flags */
1632 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1633 hsd->ErrorCode |= HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
1634 hsd->State = HAL_SD_STATE_READY;
1635 return HAL_ERROR;
1636 }
1637
1638 /* Get start and end block for high capacity cards */
1639 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1640 {
1641 start_add *= 512U;
1642 end_add *= 512U;
1643 }
1644
1645 /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
1646 if(hsd->SdCard.CardType != CARD_SECURED)
1647 {
1648 /* Send CMD32 SD_ERASE_GRP_START with argument as addr */
1649 errorstate = SDMMC_CmdSDEraseStartAdd(hsd->Instance, start_add);
1650 if(errorstate != HAL_SD_ERROR_NONE)
1651 {
1652 /* Clear all the static flags */
1653 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1654 hsd->ErrorCode |= errorstate;
1655 hsd->State = HAL_SD_STATE_READY;
1656 return HAL_ERROR;
1657 }
1658
1659 /* Send CMD33 SD_ERASE_GRP_END with argument as addr */
1660 errorstate = SDMMC_CmdSDEraseEndAdd(hsd->Instance, end_add);
1661 if(errorstate != HAL_SD_ERROR_NONE)
1662 {
1663 /* Clear all the static flags */
1664 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1665 hsd->ErrorCode |= errorstate;
1666 hsd->State = HAL_SD_STATE_READY;
1667 return HAL_ERROR;
1668 }
1669 }
1670
1671 /* Send CMD38 ERASE */
1672 errorstate = SDMMC_CmdErase(hsd->Instance, 0UL);
1673 if(errorstate != HAL_SD_ERROR_NONE)
1674 {
1675 /* Clear all the static flags */
1676 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1677 hsd->ErrorCode |= errorstate;
1678 hsd->State = HAL_SD_STATE_READY;
1679 return HAL_ERROR;
1680 }
1681
1682 hsd->State = HAL_SD_STATE_READY;
1683
1684 return HAL_OK;
1685 }
1686 else
1687 {
1688 return HAL_BUSY;
1689 }
1690 }
1691
1692 /**
1693 * @brief This function handles SD card interrupt request.
1694 * @param hsd Pointer to SD handle
1695 * @retval None
1696 */
HAL_SD_IRQHandler(SD_HandleTypeDef * hsd)1697 void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd)
1698 {
1699 uint32_t errorstate;
1700 uint32_t context = hsd->Context;
1701
1702 /* Check for SDMMC interrupt flags */
1703 if((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF) != RESET) && ((context & SD_CONTEXT_IT) != 0U))
1704 {
1705 SD_Read_IT(hsd);
1706 }
1707
1708 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) != RESET)
1709 {
1710 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DATAEND);
1711
1712 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT |\
1713 SDMMC_IT_TXUNDERR | SDMMC_IT_RXOVERR | SDMMC_IT_TXFIFOHE |\
1714 SDMMC_IT_RXFIFOHF);
1715
1716 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1717 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_IDMABTC);
1718 __SDMMC_CMDTRANS_DISABLE( hsd->Instance);
1719 #else
1720 hsd->Instance->DCTRL &= ~(SDMMC_DCTRL_DTEN);
1721 #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
1722
1723 if((context & SD_CONTEXT_IT) != 0U)
1724 {
1725 if(((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1726 {
1727 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
1728 if(errorstate != HAL_SD_ERROR_NONE)
1729 {
1730 hsd->ErrorCode |= errorstate;
1731 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1732 hsd->ErrorCallback(hsd);
1733 #else
1734 HAL_SD_ErrorCallback(hsd);
1735 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1736 }
1737 }
1738
1739 /* Clear all the static flags */
1740 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
1741
1742 hsd->State = HAL_SD_STATE_READY;
1743 hsd->Context = SD_CONTEXT_NONE;
1744 if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
1745 {
1746 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1747 hsd->RxCpltCallback(hsd);
1748 #else
1749 HAL_SD_RxCpltCallback(hsd);
1750 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1751 }
1752 else
1753 {
1754 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1755 hsd->TxCpltCallback(hsd);
1756 #else
1757 HAL_SD_TxCpltCallback(hsd);
1758 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1759 }
1760 }
1761 else if((context & SD_CONTEXT_DMA) != 0U)
1762 {
1763 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1764 hsd->Instance->DLEN = 0;
1765 hsd->Instance->DCTRL = 0;
1766 hsd->Instance->IDMACTRL = SDMMC_DISABLE_IDMA;
1767
1768 /* Stop Transfer for Write Multi blocks or Read Multi blocks */
1769 if(((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1770 {
1771 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
1772 if(errorstate != HAL_SD_ERROR_NONE)
1773 {
1774 hsd->ErrorCode |= errorstate;
1775 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1776 hsd->ErrorCallback(hsd);
1777 #else
1778 HAL_SD_ErrorCallback(hsd);
1779 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1780 }
1781 }
1782
1783 hsd->State = HAL_SD_STATE_READY;
1784 hsd->Context = SD_CONTEXT_NONE;
1785 if(((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1786 {
1787 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1788 hsd->TxCpltCallback(hsd);
1789 #else
1790 HAL_SD_TxCpltCallback(hsd);
1791 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1792 }
1793 if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
1794 {
1795 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1796 hsd->RxCpltCallback(hsd);
1797 #else
1798 HAL_SD_RxCpltCallback(hsd);
1799 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1800 }
1801 #else
1802 if((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
1803 {
1804 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
1805 if(errorstate != HAL_SD_ERROR_NONE)
1806 {
1807 hsd->ErrorCode |= errorstate;
1808 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1809 hsd->ErrorCallback(hsd);
1810 #else
1811 HAL_SD_ErrorCallback(hsd);
1812 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1813 }
1814 }
1815 if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) == 0U) && ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) == 0U))
1816 {
1817 /* Disable the DMA transfer for transmit request by setting the DMAEN bit
1818 in the SD DCTRL register */
1819 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
1820
1821 hsd->State = HAL_SD_STATE_READY;
1822
1823 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1824 hsd->TxCpltCallback(hsd);
1825 #else
1826 HAL_SD_TxCpltCallback(hsd);
1827 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1828 }
1829 #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
1830 }
1831 else
1832 {
1833 /* Nothing to do */
1834 }
1835 }
1836
1837 else if((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXFIFOHE) != RESET) && ((context & SD_CONTEXT_IT) != 0U))
1838 {
1839 SD_Write_IT(hsd);
1840 }
1841
1842 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_RXOVERR | SDMMC_FLAG_TXUNDERR) != RESET)
1843 {
1844 /* Set Error code */
1845 if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_DCRCFAIL) != RESET)
1846 {
1847 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
1848 }
1849 if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_DTIMEOUT) != RESET)
1850 {
1851 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
1852 }
1853 if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_RXOVERR) != RESET)
1854 {
1855 hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
1856 }
1857 if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_TXUNDERR) != RESET)
1858 {
1859 hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
1860 }
1861
1862 /* Clear All flags */
1863 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
1864
1865 /* Disable all interrupts */
1866 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
1867 SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
1868
1869 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1870 __SDMMC_CMDTRANS_DISABLE( hsd->Instance);
1871 hsd->Instance->DCTRL |= SDMMC_DCTRL_FIFORST;
1872 hsd->Instance->CMD |= SDMMC_CMD_CMDSTOP;
1873 #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
1874 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
1875 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1876 hsd->Instance->CMD &= ~(SDMMC_CMD_CMDSTOP);
1877 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DABORT);
1878 #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
1879
1880 if((context & SD_CONTEXT_IT) != 0U)
1881 {
1882 /* Set the SD state to ready to be able to start again the process */
1883 hsd->State = HAL_SD_STATE_READY;
1884 hsd->Context = SD_CONTEXT_NONE;
1885 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1886 hsd->ErrorCallback(hsd);
1887 #else
1888 HAL_SD_ErrorCallback(hsd);
1889 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1890 }
1891 else if((context & SD_CONTEXT_DMA) != 0U)
1892 {
1893 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1894 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
1895 {
1896 /* Disable Internal DMA */
1897 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_IDMABTC);
1898 hsd->Instance->IDMACTRL = SDMMC_DISABLE_IDMA;
1899
1900 /* Set the SD state to ready to be able to start again the process */
1901 hsd->State = HAL_SD_STATE_READY;
1902 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1903 hsd->ErrorCallback(hsd);
1904 #else
1905 HAL_SD_ErrorCallback(hsd);
1906 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1907 }
1908 #else
1909 /* Abort the SD DMA channel */
1910 if(((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1911 {
1912 /* Set the DMA Tx abort callback */
1913 hsd->hdmatx->XferAbortCallback = SD_DMATxAbort;
1914 /* Abort DMA in IT mode */
1915 if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK)
1916 {
1917 SD_DMATxAbort(hsd->hdmatx);
1918 }
1919 }
1920 else if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
1921 {
1922 /* Set the DMA Rx abort callback */
1923 hsd->hdmarx->XferAbortCallback = SD_DMARxAbort;
1924 /* Abort DMA in IT mode */
1925 if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK)
1926 {
1927 SD_DMARxAbort(hsd->hdmarx);
1928 }
1929 }
1930 else
1931 {
1932 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1933 hsd->State = HAL_SD_STATE_READY;
1934 hsd->Context = SD_CONTEXT_NONE;
1935 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1936 hsd->AbortCpltCallback(hsd);
1937 #else
1938 HAL_SD_AbortCallback(hsd);
1939 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1940 }
1941 #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
1942 }
1943 else
1944 {
1945 /* Nothing to do */
1946 }
1947 }
1948
1949 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1950 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_IDMABTC) != RESET)
1951 {
1952 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_IDMABTC);
1953 if(READ_BIT(hsd->Instance->IDMACTRL, SDMMC_IDMA_IDMABACT) == 0U)
1954 {
1955 /* Current buffer is buffer0, Transfer complete for buffer1 */
1956 if((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
1957 {
1958 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1959 hsd->Write_DMADblBuf1CpltCallback(hsd);
1960 #else
1961 HAL_SDEx_Write_DMADoubleBuffer1CpltCallback(hsd);
1962 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1963 }
1964 else /* SD_CONTEXT_READ_MULTIPLE_BLOCK */
1965 {
1966 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1967 hsd->Read_DMADblBuf1CpltCallback(hsd);
1968 #else
1969 HAL_SDEx_Read_DMADoubleBuffer1CpltCallback(hsd);
1970 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1971 }
1972 }
1973 else /* SD_DMA_BUFFER1 */
1974 {
1975 /* Current buffer is buffer1, Transfer complete for buffer0 */
1976 if((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
1977 {
1978 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1979 hsd->Write_DMADblBuf0CpltCallback(hsd);
1980 #else
1981 HAL_SDEx_Write_DMADoubleBuffer0CpltCallback(hsd);
1982 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1983 }
1984 else /* SD_CONTEXT_READ_MULTIPLE_BLOCK */
1985 {
1986 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1987 hsd->Read_DMADblBuf0CpltCallback(hsd);
1988 #else
1989 HAL_SDEx_Read_DMADoubleBuffer0CpltCallback(hsd);
1990 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1991 }
1992 }
1993 }
1994 #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
1995 else
1996 {
1997 /* Nothing to do */
1998 }
1999 }
2000
2001 /**
2002 * @brief return the SD state
2003 * @param hsd Pointer to sd handle
2004 * @retval HAL state
2005 */
HAL_SD_GetState(SD_HandleTypeDef * hsd)2006 HAL_SD_StateTypeDef HAL_SD_GetState(SD_HandleTypeDef *hsd)
2007 {
2008 return hsd->State;
2009 }
2010
2011 /**
2012 * @brief Return the SD error code
2013 * @param hsd : Pointer to a SD_HandleTypeDef structure that contains
2014 * the configuration information.
2015 * @retval SD Error Code
2016 */
HAL_SD_GetError(SD_HandleTypeDef * hsd)2017 uint32_t HAL_SD_GetError(SD_HandleTypeDef *hsd)
2018 {
2019 return hsd->ErrorCode;
2020 }
2021
2022 /**
2023 * @brief Tx Transfer completed callbacks
2024 * @param hsd Pointer to SD handle
2025 * @retval None
2026 */
HAL_SD_TxCpltCallback(SD_HandleTypeDef * hsd)2027 __weak void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)
2028 {
2029 /* Prevent unused argument(s) compilation warning */
2030 UNUSED(hsd);
2031
2032 /* NOTE : This function should not be modified, when the callback is needed,
2033 the HAL_SD_TxCpltCallback can be implemented in the user file
2034 */
2035 }
2036
2037 /**
2038 * @brief Rx Transfer completed callbacks
2039 * @param hsd Pointer SD handle
2040 * @retval None
2041 */
HAL_SD_RxCpltCallback(SD_HandleTypeDef * hsd)2042 __weak void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
2043 {
2044 /* Prevent unused argument(s) compilation warning */
2045 UNUSED(hsd);
2046
2047 /* NOTE : This function should not be modified, when the callback is needed,
2048 the HAL_SD_RxCpltCallback can be implemented in the user file
2049 */
2050 }
2051
2052 /**
2053 * @brief SD error callbacks
2054 * @param hsd Pointer SD handle
2055 * @retval None
2056 */
HAL_SD_ErrorCallback(SD_HandleTypeDef * hsd)2057 __weak void HAL_SD_ErrorCallback(SD_HandleTypeDef *hsd)
2058 {
2059 /* Prevent unused argument(s) compilation warning */
2060 UNUSED(hsd);
2061
2062 /* NOTE : This function should not be modified, when the callback is needed,
2063 the HAL_SD_ErrorCallback can be implemented in the user file
2064 */
2065 }
2066
2067 /**
2068 * @brief SD Abort callbacks
2069 * @param hsd Pointer SD handle
2070 * @retval None
2071 */
HAL_SD_AbortCallback(SD_HandleTypeDef * hsd)2072 __weak void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd)
2073 {
2074 /* Prevent unused argument(s) compilation warning */
2075 UNUSED(hsd);
2076
2077 /* NOTE : This function should not be modified, when the callback is needed,
2078 the HAL_SD_AbortCallback can be implemented in the user file
2079 */
2080 }
2081
2082 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
2083 /**
2084 * @brief Register a User SD Callback
2085 * To be used instead of the weak (surcharged) predefined callback
2086 * @param hsd : SD handle
2087 * @param CallbackID : ID of the callback to be registered
2088 * This parameter can be one of the following values:
2089 * @arg @ref HAL_SD_TX_CPLT_CB_ID SD Tx Complete Callback ID
2090 * @arg @ref HAL_SD_RX_CPLT_CB_ID SD Rx Complete Callback ID
2091 * @arg @ref HAL_SD_ERROR_CB_ID SD Error Callback ID
2092 * @arg @ref HAL_SD_ABORT_CB_ID SD Abort Callback ID
2093 * @arg @ref HAL_SD_READ_DMA_DBL_BUF0_CPLT_CB_ID SD DMA Rx Double buffer 0 Callback ID
2094 * @arg @ref HAL_SD_READ_DMA_DBL_BUF1_CPLT_CB_ID SD DMA Rx Double buffer 1 Callback ID
2095 * @arg @ref HAL_SD_WRITE_DMA_DBL_BUF0_CPLT_CB_ID SD DMA Tx Double buffer 0 Callback ID
2096 * @arg @ref HAL_SD_WRITE_DMA_DBL_BUF1_CPLT_CB_ID SD DMA Tx Double buffer 1 Callback ID
2097 * @arg @ref HAL_SD_MSP_INIT_CB_ID SD MspInit Callback ID
2098 * @arg @ref HAL_SD_MSP_DEINIT_CB_ID SD MspDeInit Callback ID
2099 * @param pCallback : pointer to the Callback function
2100 * @retval status
2101 */
HAL_SD_RegisterCallback(SD_HandleTypeDef * hsd,HAL_SD_CallbackIDTypeDef CallbackID,pSD_CallbackTypeDef pCallback)2102 HAL_StatusTypeDef HAL_SD_RegisterCallback(SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackID, pSD_CallbackTypeDef pCallback)
2103 {
2104 HAL_StatusTypeDef status = HAL_OK;
2105
2106 if(pCallback == NULL)
2107 {
2108 /* Update the error code */
2109 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2110 return HAL_ERROR;
2111 }
2112
2113 /* Process locked */
2114 __HAL_LOCK(hsd);
2115
2116 if(hsd->State == HAL_SD_STATE_READY)
2117 {
2118 switch (CallbackID)
2119 {
2120 case HAL_SD_TX_CPLT_CB_ID :
2121 hsd->TxCpltCallback = pCallback;
2122 break;
2123 case HAL_SD_RX_CPLT_CB_ID :
2124 hsd->RxCpltCallback = pCallback;
2125 break;
2126 case HAL_SD_ERROR_CB_ID :
2127 hsd->ErrorCallback = pCallback;
2128 break;
2129 case HAL_SD_ABORT_CB_ID :
2130 hsd->AbortCpltCallback = pCallback;
2131 break;
2132 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
2133 case HAL_SD_READ_DMA_DBL_BUF0_CPLT_CB_ID :
2134 hsd->Read_DMADblBuf0CpltCallback = pCallback;
2135 break;
2136 case HAL_SD_READ_DMA_DBL_BUF1_CPLT_CB_ID :
2137 hsd->Read_DMADblBuf1CpltCallback = pCallback;
2138 break;
2139 case HAL_SD_WRITE_DMA_DBL_BUF0_CPLT_CB_ID :
2140 hsd->Write_DMADblBuf0CpltCallback = pCallback;
2141 break;
2142 case HAL_SD_WRITE_DMA_DBL_BUF1_CPLT_CB_ID :
2143 hsd->Write_DMADblBuf1CpltCallback = pCallback;
2144 break;
2145 #endif
2146 case HAL_SD_MSP_INIT_CB_ID :
2147 hsd->MspInitCallback = pCallback;
2148 break;
2149 case HAL_SD_MSP_DEINIT_CB_ID :
2150 hsd->MspDeInitCallback = pCallback;
2151 break;
2152 default :
2153 /* Update the error code */
2154 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2155 /* update return status */
2156 status = HAL_ERROR;
2157 break;
2158 }
2159 }
2160 else if (hsd->State == HAL_SD_STATE_RESET)
2161 {
2162 switch (CallbackID)
2163 {
2164 case HAL_SD_MSP_INIT_CB_ID :
2165 hsd->MspInitCallback = pCallback;
2166 break;
2167 case HAL_SD_MSP_DEINIT_CB_ID :
2168 hsd->MspDeInitCallback = pCallback;
2169 break;
2170 default :
2171 /* Update the error code */
2172 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2173 /* update return status */
2174 status = HAL_ERROR;
2175 break;
2176 }
2177 }
2178 else
2179 {
2180 /* Update the error code */
2181 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2182 /* update return status */
2183 status = HAL_ERROR;
2184 }
2185
2186 /* Release Lock */
2187 __HAL_UNLOCK(hsd);
2188 return status;
2189 }
2190
2191 /**
2192 * @brief Unregister a User SD Callback
2193 * SD Callback is redirected to the weak (surcharged) predefined callback
2194 * @param hsd : SD handle
2195 * @param CallbackID : ID of the callback to be unregistered
2196 * This parameter can be one of the following values:
2197 * @arg @ref HAL_SD_TX_CPLT_CB_ID SD Tx Complete Callback ID
2198 * @arg @ref HAL_SD_RX_CPLT_CB_ID SD Rx Complete Callback ID
2199 * @arg @ref HAL_SD_ERROR_CB_ID SD Error Callback ID
2200 * @arg @ref HAL_SD_ABORT_CB_ID SD Abort Callback ID
2201 * @arg @ref HAL_SD_READ_DMA_DBL_BUF0_CPLT_CB_ID SD DMA Rx Double buffer 0 Callback ID
2202 * @arg @ref HAL_SD_READ_DMA_DBL_BUF1_CPLT_CB_ID SD DMA Rx Double buffer 1 Callback ID
2203 * @arg @ref HAL_SD_WRITE_DMA_DBL_BUF0_CPLT_CB_ID SD DMA Tx Double buffer 0 Callback ID
2204 * @arg @ref HAL_SD_WRITE_DMA_DBL_BUF1_CPLT_CB_ID SD DMA Tx Double buffer 1 Callback ID
2205 * @arg @ref HAL_SD_MSP_INIT_CB_ID SD MspInit Callback ID
2206 * @arg @ref HAL_SD_MSP_DEINIT_CB_ID SD MspDeInit Callback ID
2207 * @retval status
2208 */
HAL_SD_UnRegisterCallback(SD_HandleTypeDef * hsd,HAL_SD_CallbackIDTypeDef CallbackID)2209 HAL_StatusTypeDef HAL_SD_UnRegisterCallback(SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackID)
2210 {
2211 HAL_StatusTypeDef status = HAL_OK;
2212
2213 /* Process locked */
2214 __HAL_LOCK(hsd);
2215
2216 if(hsd->State == HAL_SD_STATE_READY)
2217 {
2218 switch (CallbackID)
2219 {
2220 case HAL_SD_TX_CPLT_CB_ID :
2221 hsd->TxCpltCallback = HAL_SD_TxCpltCallback;
2222 break;
2223 case HAL_SD_RX_CPLT_CB_ID :
2224 hsd->RxCpltCallback = HAL_SD_RxCpltCallback;
2225 break;
2226 case HAL_SD_ERROR_CB_ID :
2227 hsd->ErrorCallback = HAL_SD_ErrorCallback;
2228 break;
2229 case HAL_SD_ABORT_CB_ID :
2230 hsd->AbortCpltCallback = HAL_SD_AbortCallback;
2231 break;
2232 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
2233 case HAL_SD_READ_DMA_DBL_BUF0_CPLT_CB_ID :
2234 hsd->Read_DMADblBuf0CpltCallback = HAL_SDEx_Read_DMADoubleBuffer0CpltCallback;
2235 break;
2236 case HAL_SD_READ_DMA_DBL_BUF1_CPLT_CB_ID :
2237 hsd->Read_DMADblBuf1CpltCallback = HAL_SDEx_Read_DMADoubleBuffer1CpltCallback;
2238 break;
2239 case HAL_SD_WRITE_DMA_DBL_BUF0_CPLT_CB_ID :
2240 hsd->Write_DMADblBuf0CpltCallback = HAL_SDEx_Write_DMADoubleBuffer0CpltCallback;
2241 break;
2242 case HAL_SD_WRITE_DMA_DBL_BUF1_CPLT_CB_ID :
2243 hsd->Write_DMADblBuf1CpltCallback = HAL_SDEx_Write_DMADoubleBuffer1CpltCallback;
2244 break;
2245 #endif
2246 case HAL_SD_MSP_INIT_CB_ID :
2247 hsd->MspInitCallback = HAL_SD_MspInit;
2248 break;
2249 case HAL_SD_MSP_DEINIT_CB_ID :
2250 hsd->MspDeInitCallback = HAL_SD_MspDeInit;
2251 break;
2252 default :
2253 /* Update the error code */
2254 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2255 /* update return status */
2256 status = HAL_ERROR;
2257 break;
2258 }
2259 }
2260 else if (hsd->State == HAL_SD_STATE_RESET)
2261 {
2262 switch (CallbackID)
2263 {
2264 case HAL_SD_MSP_INIT_CB_ID :
2265 hsd->MspInitCallback = HAL_SD_MspInit;
2266 break;
2267 case HAL_SD_MSP_DEINIT_CB_ID :
2268 hsd->MspDeInitCallback = HAL_SD_MspDeInit;
2269 break;
2270 default :
2271 /* Update the error code */
2272 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2273 /* update return status */
2274 status = HAL_ERROR;
2275 break;
2276 }
2277 }
2278 else
2279 {
2280 /* Update the error code */
2281 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2282 /* update return status */
2283 status = HAL_ERROR;
2284 }
2285
2286 /* Release Lock */
2287 __HAL_UNLOCK(hsd);
2288 return status;
2289 }
2290
2291 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
2292 /**
2293 * @brief Register a User SD Transceiver Callback
2294 * To be used instead of the weak (surcharged) predefined callback
2295 * @param hsd : SD handle
2296 * @param pCallback : pointer to the Callback function
2297 * @retval status
2298 */
HAL_SD_RegisterTransceiverCallback(SD_HandleTypeDef * hsd,pSD_TransceiverCallbackTypeDef pCallback)2299 HAL_StatusTypeDef HAL_SD_RegisterTransceiverCallback(SD_HandleTypeDef *hsd, pSD_TransceiverCallbackTypeDef pCallback)
2300 {
2301 HAL_StatusTypeDef status = HAL_OK;
2302
2303 if(pCallback == NULL)
2304 {
2305 /* Update the error code */
2306 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2307 return HAL_ERROR;
2308 }
2309
2310 /* Process locked */
2311 __HAL_LOCK(hsd);
2312
2313 if(hsd->State == HAL_SD_STATE_READY)
2314 {
2315 hsd->DriveTransceiver_1_8V_Callback = pCallback;
2316 }
2317 else
2318 {
2319 /* Update the error code */
2320 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2321 /* update return status */
2322 status = HAL_ERROR;
2323 }
2324
2325 /* Release Lock */
2326 __HAL_UNLOCK(hsd);
2327 return status;
2328 }
2329
2330 /**
2331 * @brief Unregister a User SD Transceiver Callback
2332 * SD Callback is redirected to the weak (surcharged) predefined callback
2333 * @param hsd : SD handle
2334 * @retval status
2335 */
HAL_SD_UnRegisterTransceiverCallback(SD_HandleTypeDef * hsd)2336 HAL_StatusTypeDef HAL_SD_UnRegisterTransceiverCallback(SD_HandleTypeDef *hsd)
2337 {
2338 HAL_StatusTypeDef status = HAL_OK;
2339
2340 /* Process locked */
2341 __HAL_LOCK(hsd);
2342
2343 if(hsd->State == HAL_SD_STATE_READY)
2344 {
2345 hsd->DriveTransceiver_1_8V_Callback = HAL_SDEx_DriveTransceiver_1_8V_Callback;
2346 }
2347 else
2348 {
2349 /* Update the error code */
2350 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2351 /* update return status */
2352 status = HAL_ERROR;
2353 }
2354
2355 /* Release Lock */
2356 __HAL_UNLOCK(hsd);
2357 return status;
2358 }
2359 #endif
2360 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
2361
2362 /**
2363 * @}
2364 */
2365
2366 /** @addtogroup SD_Exported_Functions_Group3
2367 * @brief management functions
2368 *
2369 @verbatim
2370 ==============================================================================
2371 ##### Peripheral Control functions #####
2372 ==============================================================================
2373 [..]
2374 This subsection provides a set of functions allowing to control the SD card
2375 operations and get the related information
2376
2377 @endverbatim
2378 * @{
2379 */
2380
2381 /**
2382 * @brief Returns information the information of the card which are stored on
2383 * the CID register.
2384 * @param hsd Pointer to SD handle
2385 * @param pCID Pointer to a HAL_SD_CardCIDTypeDef structure that
2386 * contains all CID register parameters
2387 * @retval HAL status
2388 */
HAL_SD_GetCardCID(SD_HandleTypeDef * hsd,HAL_SD_CardCIDTypeDef * pCID)2389 HAL_StatusTypeDef HAL_SD_GetCardCID(SD_HandleTypeDef *hsd, HAL_SD_CardCIDTypeDef *pCID)
2390 {
2391 pCID->ManufacturerID = (uint8_t)((hsd->CID[0] & 0xFF000000U) >> 24U);
2392
2393 pCID->OEM_AppliID = (uint16_t)((hsd->CID[0] & 0x00FFFF00U) >> 8U);
2394
2395 pCID->ProdName1 = (((hsd->CID[0] & 0x000000FFU) << 24U) | ((hsd->CID[1] & 0xFFFFFF00U) >> 8U));
2396
2397 pCID->ProdName2 = (uint8_t)(hsd->CID[1] & 0x000000FFU);
2398
2399 pCID->ProdRev = (uint8_t)((hsd->CID[2] & 0xFF000000U) >> 24U);
2400
2401 pCID->ProdSN = (((hsd->CID[2] & 0x00FFFFFFU) << 8U) | ((hsd->CID[3] & 0xFF000000U) >> 24U));
2402
2403 pCID->Reserved1 = (uint8_t)((hsd->CID[3] & 0x00F00000U) >> 20U);
2404
2405 pCID->ManufactDate = (uint16_t)((hsd->CID[3] & 0x000FFF00U) >> 8U);
2406
2407 pCID->CID_CRC = (uint8_t)((hsd->CID[3] & 0x000000FEU) >> 1U);
2408
2409 pCID->Reserved2 = 1U;
2410
2411 return HAL_OK;
2412 }
2413
2414 /**
2415 * @brief Returns information the information of the card which are stored on
2416 * the CSD register.
2417 * @param hsd Pointer to SD handle
2418 * @param pCSD Pointer to a HAL_SD_CardCSDTypeDef structure that
2419 * contains all CSD register parameters
2420 * @retval HAL status
2421 */
HAL_SD_GetCardCSD(SD_HandleTypeDef * hsd,HAL_SD_CardCSDTypeDef * pCSD)2422 HAL_StatusTypeDef HAL_SD_GetCardCSD(SD_HandleTypeDef *hsd, HAL_SD_CardCSDTypeDef *pCSD)
2423 {
2424 pCSD->CSDStruct = (uint8_t)((hsd->CSD[0] & 0xC0000000U) >> 30U);
2425
2426 pCSD->SysSpecVersion = (uint8_t)((hsd->CSD[0] & 0x3C000000U) >> 26U);
2427
2428 pCSD->Reserved1 = (uint8_t)((hsd->CSD[0] & 0x03000000U) >> 24U);
2429
2430 pCSD->TAAC = (uint8_t)((hsd->CSD[0] & 0x00FF0000U) >> 16U);
2431
2432 pCSD->NSAC = (uint8_t)((hsd->CSD[0] & 0x0000FF00U) >> 8U);
2433
2434 pCSD->MaxBusClkFrec = (uint8_t)(hsd->CSD[0] & 0x000000FFU);
2435
2436 pCSD->CardComdClasses = (uint16_t)((hsd->CSD[1] & 0xFFF00000U) >> 20U);
2437
2438 pCSD->RdBlockLen = (uint8_t)((hsd->CSD[1] & 0x000F0000U) >> 16U);
2439
2440 pCSD->PartBlockRead = (uint8_t)((hsd->CSD[1] & 0x00008000U) >> 15U);
2441
2442 pCSD->WrBlockMisalign = (uint8_t)((hsd->CSD[1] & 0x00004000U) >> 14U);
2443
2444 pCSD->RdBlockMisalign = (uint8_t)((hsd->CSD[1] & 0x00002000U) >> 13U);
2445
2446 pCSD->DSRImpl = (uint8_t)((hsd->CSD[1] & 0x00001000U) >> 12U);
2447
2448 pCSD->Reserved2 = 0U; /*!< Reserved */
2449
2450 if(hsd->SdCard.CardType == CARD_SDSC)
2451 {
2452 pCSD->DeviceSize = (((hsd->CSD[1] & 0x000003FFU) << 2U) | ((hsd->CSD[2] & 0xC0000000U) >> 30U));
2453
2454 pCSD->MaxRdCurrentVDDMin = (uint8_t)((hsd->CSD[2] & 0x38000000U) >> 27U);
2455
2456 pCSD->MaxRdCurrentVDDMax = (uint8_t)((hsd->CSD[2] & 0x07000000U) >> 24U);
2457
2458 pCSD->MaxWrCurrentVDDMin = (uint8_t)((hsd->CSD[2] & 0x00E00000U) >> 21U);
2459
2460 pCSD->MaxWrCurrentVDDMax = (uint8_t)((hsd->CSD[2] & 0x001C0000U) >> 18U);
2461
2462 pCSD->DeviceSizeMul = (uint8_t)((hsd->CSD[2] & 0x00038000U) >> 15U);
2463
2464 hsd->SdCard.BlockNbr = (pCSD->DeviceSize + 1U) ;
2465 hsd->SdCard.BlockNbr *= (1UL << ((pCSD->DeviceSizeMul & 0x07U) + 2U));
2466 hsd->SdCard.BlockSize = (1UL << (pCSD->RdBlockLen & 0x0FU));
2467
2468 hsd->SdCard.LogBlockNbr = (hsd->SdCard.BlockNbr) * ((hsd->SdCard.BlockSize) / 512U);
2469 hsd->SdCard.LogBlockSize = 512U;
2470 }
2471 else if(hsd->SdCard.CardType == CARD_SDHC_SDXC)
2472 {
2473 /* Byte 7 */
2474 pCSD->DeviceSize = (((hsd->CSD[1] & 0x0000003FU) << 16U) | ((hsd->CSD[2] & 0xFFFF0000U) >> 16U));
2475
2476 hsd->SdCard.BlockNbr = ((pCSD->DeviceSize + 1U) * 1024U);
2477 hsd->SdCard.LogBlockNbr = hsd->SdCard.BlockNbr;
2478 hsd->SdCard.BlockSize = 512U;
2479 hsd->SdCard.LogBlockSize = hsd->SdCard.BlockSize;
2480 }
2481 else
2482 {
2483 /* Clear all the static flags */
2484 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2485 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2486 hsd->State = HAL_SD_STATE_READY;
2487 return HAL_ERROR;
2488 }
2489
2490 pCSD->EraseGrSize = (uint8_t)((hsd->CSD[2] & 0x00004000U) >> 14U);
2491
2492 pCSD->EraseGrMul = (uint8_t)((hsd->CSD[2] & 0x00003F80U) >> 7U);
2493
2494 pCSD->WrProtectGrSize = (uint8_t)(hsd->CSD[2] & 0x0000007FU);
2495
2496 pCSD->WrProtectGrEnable = (uint8_t)((hsd->CSD[3] & 0x80000000U) >> 31U);
2497
2498 pCSD->ManDeflECC = (uint8_t)((hsd->CSD[3] & 0x60000000U) >> 29U);
2499
2500 pCSD->WrSpeedFact = (uint8_t)((hsd->CSD[3] & 0x1C000000U) >> 26U);
2501
2502 pCSD->MaxWrBlockLen= (uint8_t)((hsd->CSD[3] & 0x03C00000U) >> 22U);
2503
2504 pCSD->WriteBlockPaPartial = (uint8_t)((hsd->CSD[3] & 0x00200000U) >> 21U);
2505
2506 pCSD->Reserved3 = 0;
2507
2508 pCSD->ContentProtectAppli = (uint8_t)((hsd->CSD[3] & 0x00010000U) >> 16U);
2509
2510 pCSD->FileFormatGroup = (uint8_t)((hsd->CSD[3] & 0x00008000U) >> 15U);
2511
2512 pCSD->CopyFlag = (uint8_t)((hsd->CSD[3] & 0x00004000U) >> 14U);
2513
2514 pCSD->PermWrProtect = (uint8_t)((hsd->CSD[3] & 0x00002000U) >> 13U);
2515
2516 pCSD->TempWrProtect = (uint8_t)((hsd->CSD[3] & 0x00001000U) >> 12U);
2517
2518 pCSD->FileFormat = (uint8_t)((hsd->CSD[3] & 0x00000C00U) >> 10U);
2519
2520 pCSD->ECC= (uint8_t)((hsd->CSD[3] & 0x00000300U) >> 8U);
2521
2522 pCSD->CSD_CRC = (uint8_t)((hsd->CSD[3] & 0x000000FEU) >> 1U);
2523
2524 pCSD->Reserved4 = 1;
2525
2526 return HAL_OK;
2527 }
2528
2529 /**
2530 * @brief Gets the SD status info.
2531 * @param hsd Pointer to SD handle
2532 * @param pStatus Pointer to the HAL_SD_CardStatusTypeDef structure that
2533 * will contain the SD card status information
2534 * @retval HAL status
2535 */
HAL_SD_GetCardStatus(SD_HandleTypeDef * hsd,HAL_SD_CardStatusTypeDef * pStatus)2536 HAL_StatusTypeDef HAL_SD_GetCardStatus(SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypeDef *pStatus)
2537 {
2538 uint32_t sd_status[16];
2539 uint32_t errorstate;
2540 HAL_StatusTypeDef status = HAL_OK;
2541
2542 errorstate = SD_SendSDStatus(hsd, sd_status);
2543 if(errorstate != HAL_SD_ERROR_NONE)
2544 {
2545 /* Clear all the static flags */
2546 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2547 hsd->ErrorCode |= errorstate;
2548 hsd->State = HAL_SD_STATE_READY;
2549 status = HAL_ERROR;
2550 }
2551 else
2552 {
2553 pStatus->DataBusWidth = (uint8_t)((sd_status[0] & 0xC0U) >> 6U);
2554
2555 pStatus->SecuredMode = (uint8_t)((sd_status[0] & 0x20U) >> 5U);
2556
2557 pStatus->CardType = (uint16_t)(((sd_status[0] & 0x00FF0000U) >> 8U) | ((sd_status[0] & 0xFF000000U) >> 24U));
2558
2559 pStatus->ProtectedAreaSize = (((sd_status[1] & 0xFFU) << 24U) | ((sd_status[1] & 0xFF00U) << 8U) |
2560 ((sd_status[1] & 0xFF0000U) >> 8U) | ((sd_status[1] & 0xFF000000U) >> 24U));
2561
2562 pStatus->SpeedClass = (uint8_t)(sd_status[2] & 0xFFU);
2563
2564 pStatus->PerformanceMove = (uint8_t)((sd_status[2] & 0xFF00U) >> 8U);
2565
2566 pStatus->AllocationUnitSize = (uint8_t)((sd_status[2] & 0xF00000U) >> 20U);
2567
2568 pStatus->EraseSize = (uint16_t)(((sd_status[2] & 0xFF000000U) >> 16U) | (sd_status[3] & 0xFFU));
2569
2570 pStatus->EraseTimeout = (uint8_t)((sd_status[3] & 0xFC00U) >> 10U);
2571
2572 pStatus->EraseOffset = (uint8_t)((sd_status[3] & 0x0300U) >> 8U);
2573
2574 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
2575 pStatus->UhsSpeedGrade = (uint8_t)((sd_status[3] & 0x00F0U) >> 4U);
2576 pStatus->UhsAllocationUnitSize = (uint8_t)(sd_status[3] & 0x000FU) ;
2577 pStatus->VideoSpeedClass = (uint8_t)((sd_status[4] & 0xFF000000U) >> 24U);
2578 #endif
2579 }
2580
2581 /* Set Block Size for Card */
2582 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
2583 if(errorstate != HAL_SD_ERROR_NONE)
2584 {
2585 /* Clear all the static flags */
2586 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2587 hsd->ErrorCode = errorstate;
2588 hsd->State = HAL_SD_STATE_READY;
2589 status = HAL_ERROR;
2590 }
2591
2592 return status;
2593 }
2594
2595 /**
2596 * @brief Gets the SD card info.
2597 * @param hsd Pointer to SD handle
2598 * @param pCardInfo Pointer to the HAL_SD_CardInfoTypeDef structure that
2599 * will contain the SD card status information
2600 * @retval HAL status
2601 */
HAL_SD_GetCardInfo(SD_HandleTypeDef * hsd,HAL_SD_CardInfoTypeDef * pCardInfo)2602 HAL_StatusTypeDef HAL_SD_GetCardInfo(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypeDef *pCardInfo)
2603 {
2604 pCardInfo->CardType = (uint32_t)(hsd->SdCard.CardType);
2605 pCardInfo->CardVersion = (uint32_t)(hsd->SdCard.CardVersion);
2606 pCardInfo->Class = (uint32_t)(hsd->SdCard.Class);
2607 pCardInfo->RelCardAdd = (uint32_t)(hsd->SdCard.RelCardAdd);
2608 pCardInfo->BlockNbr = (uint32_t)(hsd->SdCard.BlockNbr);
2609 pCardInfo->BlockSize = (uint32_t)(hsd->SdCard.BlockSize);
2610 pCardInfo->LogBlockNbr = (uint32_t)(hsd->SdCard.LogBlockNbr);
2611 pCardInfo->LogBlockSize = (uint32_t)(hsd->SdCard.LogBlockSize);
2612
2613 return HAL_OK;
2614 }
2615
2616 /**
2617 * @brief Enables wide bus operation for the requested card if supported by
2618 * card.
2619 * @param hsd Pointer to SD handle
2620 * @param WideMode Specifies the SD card wide bus mode
2621 * This parameter can be one of the following values:
2622 * @arg SDMMC_BUS_WIDE_8B: 8-bit data transfer
2623 * @arg SDMMC_BUS_WIDE_4B: 4-bit data transfer
2624 * @arg SDMMC_BUS_WIDE_1B: 1-bit data transfer
2625 * @retval HAL status
2626 */
HAL_SD_ConfigWideBusOperation(SD_HandleTypeDef * hsd,uint32_t WideMode)2627 HAL_StatusTypeDef HAL_SD_ConfigWideBusOperation(SD_HandleTypeDef *hsd, uint32_t WideMode)
2628 {
2629 SDMMC_InitTypeDef Init;
2630 uint32_t errorstate;
2631 uint32_t sdmmc_clk;
2632 HAL_StatusTypeDef status = HAL_OK;
2633
2634 /* Check the parameters */
2635 assert_param(IS_SDMMC_BUS_WIDE(WideMode));
2636
2637 /* Change State */
2638 hsd->State = HAL_SD_STATE_BUSY;
2639
2640 if(hsd->SdCard.CardType != CARD_SECURED)
2641 {
2642 if(WideMode == SDMMC_BUS_WIDE_8B)
2643 {
2644 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2645 }
2646 else if(WideMode == SDMMC_BUS_WIDE_4B)
2647 {
2648 errorstate = SD_WideBus_Enable(hsd);
2649
2650 hsd->ErrorCode |= errorstate;
2651 }
2652 else if(WideMode == SDMMC_BUS_WIDE_1B)
2653 {
2654 errorstate = SD_WideBus_Disable(hsd);
2655
2656 hsd->ErrorCode |= errorstate;
2657 }
2658 else
2659 {
2660 /* WideMode is not a valid argument*/
2661 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
2662 }
2663 }
2664 else
2665 {
2666 /* MMC Card does not support this feature */
2667 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2668 }
2669
2670 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2671 {
2672 /* Clear all the static flags */
2673 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2674 status = HAL_ERROR;
2675 }
2676 else
2677 {
2678 sdmmc_clk = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_SDMMC1);
2679 if (sdmmc_clk != 0U)
2680 {
2681 /* Configure the SDMMC peripheral */
2682 Init.ClockEdge = hsd->Init.ClockEdge;
2683 #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
2684 Init.ClockBypass = hsd->Init.ClockBypass;
2685 #endif /* !STM32L4P5xx && !STM32L4Q5xx && !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
2686 Init.ClockPowerSave = hsd->Init.ClockPowerSave;
2687 Init.BusWide = WideMode;
2688 Init.HardwareFlowControl = hsd->Init.HardwareFlowControl;
2689
2690 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
2691 /* Check if user Clock div < Normal speed 25Mhz, no change in Clockdiv */
2692 if (hsd->Init.ClockDiv >= (sdmmc_clk / (2U * SD_NORMAL_SPEED_FREQ)))
2693 {
2694 Init.ClockDiv = hsd->Init.ClockDiv;
2695 }
2696 else if (hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED)
2697 {
2698 /* UltraHigh speed SD card,user Clock div */
2699 Init.ClockDiv = hsd->Init.ClockDiv;
2700 }
2701 else if (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED)
2702 {
2703 /* High speed SD card, Max Frequency = 50Mhz */
2704 if (hsd->Init.ClockDiv == 0U)
2705 {
2706 if (sdmmc_clk > SD_HIGH_SPEED_FREQ)
2707 {
2708 Init.ClockDiv = sdmmc_clk / (2U * SD_HIGH_SPEED_FREQ);
2709 }
2710 else
2711 {
2712 Init.ClockDiv = hsd->Init.ClockDiv;
2713 }
2714 }
2715 else
2716 {
2717 if ((sdmmc_clk/(2U * hsd->Init.ClockDiv)) > SD_HIGH_SPEED_FREQ)
2718 {
2719 Init.ClockDiv = sdmmc_clk / (2U * SD_HIGH_SPEED_FREQ);
2720 }
2721 else
2722 {
2723 Init.ClockDiv = hsd->Init.ClockDiv;
2724 }
2725 }
2726 }
2727 else
2728 {
2729 /* No High speed SD card, Max Frequency = 25Mhz */
2730 if (hsd->Init.ClockDiv == 0U)
2731 {
2732 if (sdmmc_clk > SD_NORMAL_SPEED_FREQ)
2733 {
2734 Init.ClockDiv = sdmmc_clk / (2U * SD_NORMAL_SPEED_FREQ);
2735 }
2736 else
2737 {
2738 Init.ClockDiv = hsd->Init.ClockDiv;
2739 }
2740 }
2741 else
2742 {
2743 if ((sdmmc_clk/(2U * hsd->Init.ClockDiv)) > SD_NORMAL_SPEED_FREQ)
2744 {
2745 Init.ClockDiv = sdmmc_clk / (2U * SD_NORMAL_SPEED_FREQ);
2746 }
2747 else
2748 {
2749 Init.ClockDiv = hsd->Init.ClockDiv;
2750 }
2751 }
2752 }
2753
2754 Init.Transceiver = hsd->Init.Transceiver;
2755 #else
2756 if ((sdmmc_clk / (hsd->Init.ClockDiv + 2U)) > SD_NORMAL_SPEED_FREQ)
2757 {
2758 Init.ClockDiv = ((sdmmc_clk / SD_NORMAL_SPEED_FREQ) - 2U);
2759 }
2760 else
2761 {
2762 Init.ClockDiv = hsd->Init.ClockDiv;
2763 }
2764 #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
2765
2766 (void)SDMMC_Init(hsd->Instance, Init);
2767 }
2768 else
2769 {
2770 hsd->ErrorCode |= SDMMC_ERROR_INVALID_PARAMETER;
2771 status = HAL_ERROR;
2772 }
2773 }
2774
2775 /* Set Block Size for Card */
2776 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
2777 if(errorstate != HAL_SD_ERROR_NONE)
2778 {
2779 /* Clear all the static flags */
2780 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2781 hsd->ErrorCode |= errorstate;
2782 status = HAL_ERROR;
2783 }
2784
2785 /* Change State */
2786 hsd->State = HAL_SD_STATE_READY;
2787
2788 return status;
2789 }
2790
2791 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
2792 /**
2793 * @brief Configure the speed bus mode
2794 * @param hsd Pointer to the SD handle
2795 * @param SpeedMode Specifies the SD card speed bus mode
2796 * This parameter can be one of the following values:
2797 * @arg SDMMC_SPEED_MODE_AUTO: Max speed mode supported by the card
2798 * @arg SDMMC_SPEED_MODE_DEFAULT: Default Speed/SDR12 mode
2799 * @arg SDMMC_SPEED_MODE_HIGH: High Speed/SDR25 mode
2800 * @arg SDMMC_SPEED_MODE_ULTRA: Ultra high speed mode
2801 * @retval HAL status
2802 */
2803
HAL_SD_ConfigSpeedBusOperation(SD_HandleTypeDef * hsd,uint32_t SpeedMode)2804 HAL_StatusTypeDef HAL_SD_ConfigSpeedBusOperation(SD_HandleTypeDef *hsd, uint32_t SpeedMode)
2805 {
2806 uint32_t tickstart;
2807 uint32_t errorstate;
2808 HAL_StatusTypeDef status = HAL_OK;
2809
2810 /* Check the parameters */
2811 assert_param(IS_SDMMC_SPEED_MODE(SpeedMode));
2812 /* Change State */
2813 hsd->State = HAL_SD_STATE_BUSY;
2814
2815 if(hsd->Init.Transceiver == SDMMC_TRANSCEIVER_ENABLE)
2816 {
2817 switch (SpeedMode)
2818 {
2819 case SDMMC_SPEED_MODE_AUTO:
2820 {
2821 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
2822 (hsd->SdCard.CardType == CARD_SDHC_SDXC))
2823 {
2824 hsd->Instance->CLKCR |= 0x00100000U;
2825 /* Enable Ultra High Speed */
2826 if (SD_UltraHighSpeed(hsd) != HAL_SD_ERROR_NONE)
2827 {
2828 if (SD_HighSpeed(hsd) != HAL_SD_ERROR_NONE)
2829 {
2830 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2831 status = HAL_ERROR;
2832 }
2833 }
2834 }
2835 else if (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED)
2836 {
2837 /* Enable High Speed */
2838 if (SD_HighSpeed(hsd) != HAL_SD_ERROR_NONE)
2839 {
2840 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2841 status = HAL_ERROR;
2842 }
2843 }
2844 else
2845 {
2846 /*Nothing to do, Use defaultSpeed */
2847 }
2848 break;
2849 }
2850
2851 case SDMMC_SPEED_MODE_ULTRA:
2852 {
2853 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
2854 (hsd->SdCard.CardType == CARD_SDHC_SDXC))
2855 {
2856 hsd->Instance->CLKCR |= 0x00100000U;
2857 /* Enable UltraHigh Speed */
2858 if (SD_UltraHighSpeed(hsd) != HAL_SD_ERROR_NONE)
2859 {
2860 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2861 status = HAL_ERROR;
2862 }
2863 }
2864 else
2865 {
2866 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2867 status = HAL_ERROR;
2868 }
2869 break;
2870 }
2871
2872 case SDMMC_SPEED_MODE_DDR:
2873 {
2874 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
2875 (hsd->SdCard.CardType == CARD_SDHC_SDXC))
2876 {
2877 hsd->Instance->CLKCR |= 0x00100000U;
2878 /* Enable DDR Mode*/
2879 if (SD_DDR_Mode(hsd) != HAL_SD_ERROR_NONE)
2880 {
2881 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2882 status = HAL_ERROR;
2883 }
2884 }
2885 else
2886 {
2887 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2888 status = HAL_ERROR;
2889 }
2890 break;
2891 }
2892
2893 case SDMMC_SPEED_MODE_HIGH:
2894 {
2895 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
2896 (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED) ||
2897 (hsd->SdCard.CardType == CARD_SDHC_SDXC))
2898 {
2899 /* Enable High Speed */
2900 if (SD_HighSpeed(hsd) != HAL_SD_ERROR_NONE)
2901 {
2902 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2903 status = HAL_ERROR;
2904 }
2905 }
2906 else
2907 {
2908 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2909 status = HAL_ERROR;
2910 }
2911 break;
2912 }
2913
2914 case SDMMC_SPEED_MODE_DEFAULT:
2915 break;
2916
2917 default:
2918 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
2919 status = HAL_ERROR;
2920 break;
2921 }
2922 }
2923 else
2924 {
2925 switch (SpeedMode)
2926 {
2927 case SDMMC_SPEED_MODE_AUTO:
2928 {
2929 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
2930 (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED) ||
2931 (hsd->SdCard.CardType == CARD_SDHC_SDXC))
2932 {
2933 /* Enable High Speed */
2934 if (SD_HighSpeed(hsd) != HAL_SD_ERROR_NONE)
2935 {
2936 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2937 status = HAL_ERROR;
2938 }
2939 }
2940 else
2941 {
2942 /*Nothing to do, Use defaultSpeed */
2943 }
2944 break;
2945 }
2946
2947 case SDMMC_SPEED_MODE_HIGH:
2948 {
2949 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
2950 (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED) ||
2951 (hsd->SdCard.CardType == CARD_SDHC_SDXC))
2952 {
2953 /* Enable High Speed */
2954 if (SD_HighSpeed(hsd) != HAL_SD_ERROR_NONE)
2955 {
2956 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2957 status = HAL_ERROR;
2958 }
2959 }
2960 else
2961 {
2962 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2963 status = HAL_ERROR;
2964 }
2965 break;
2966 }
2967
2968 case SDMMC_SPEED_MODE_DEFAULT:
2969 break;
2970
2971 case SDMMC_SPEED_MODE_ULTRA: /*not valid without transceiver*/
2972 default:
2973 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
2974 status = HAL_ERROR;
2975 break;
2976 }
2977 }
2978
2979 /* Verify that SD card is ready to use after Speed mode switch*/
2980 tickstart = HAL_GetTick();
2981 while ((HAL_SD_GetCardState(hsd) != HAL_SD_CARD_TRANSFER))
2982 {
2983 if ((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
2984 {
2985 hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
2986 hsd->State = HAL_SD_STATE_READY;
2987 return HAL_TIMEOUT;
2988 }
2989 }
2990
2991 /* Set Block Size for Card */
2992 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
2993 if(errorstate != HAL_SD_ERROR_NONE)
2994 {
2995 /* Clear all the static flags */
2996 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2997 hsd->ErrorCode |= errorstate;
2998 status = HAL_ERROR;
2999 }
3000
3001 /* Change State */
3002 hsd->State = HAL_SD_STATE_READY;
3003 return status;
3004 }
3005 #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
3006
3007 /**
3008 * @brief Gets the current sd card data state.
3009 * @param hsd pointer to SD handle
3010 * @retval Card state
3011 */
HAL_SD_GetCardState(SD_HandleTypeDef * hsd)3012 HAL_SD_CardStateTypeDef HAL_SD_GetCardState(SD_HandleTypeDef *hsd)
3013 {
3014 uint32_t cardstate;
3015 uint32_t errorstate;
3016 uint32_t resp1 = 0;
3017
3018 errorstate = SD_SendStatus(hsd, &resp1);
3019 if(errorstate != HAL_SD_ERROR_NONE)
3020 {
3021 hsd->ErrorCode |= errorstate;
3022 }
3023
3024 cardstate = ((resp1 >> 9U) & 0x0FU);
3025
3026 return (HAL_SD_CardStateTypeDef)cardstate;
3027 }
3028
3029 /**
3030 * @brief Abort the current transfer and disable the SD.
3031 * @param hsd pointer to a SD_HandleTypeDef structure that contains
3032 * the configuration information for SD module.
3033 * @retval HAL status
3034 */
HAL_SD_Abort(SD_HandleTypeDef * hsd)3035 HAL_StatusTypeDef HAL_SD_Abort(SD_HandleTypeDef *hsd)
3036 {
3037 HAL_SD_CardStateTypeDef CardState;
3038 #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
3039 uint32_t context = hsd->Context;
3040 #endif
3041
3042 /* DIsable All interrupts */
3043 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
3044 SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
3045
3046 /* Clear All flags */
3047 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
3048
3049 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
3050 /* If IDMA Context, disable Internal DMA */
3051 hsd->Instance->IDMACTRL = SDMMC_DISABLE_IDMA;
3052 #else
3053 CLEAR_BIT(hsd->Instance->DCTRL, SDMMC_DCTRL_DTEN);
3054
3055 if ((context & SD_CONTEXT_DMA) != 0U)
3056 {
3057 /* Disable the SD DMA request */
3058 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
3059
3060 /* Abort the SD DMA Tx channel */
3061 if (((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
3062 {
3063 if(HAL_DMA_Abort(hsd->hdmatx) != HAL_OK)
3064 {
3065 hsd->ErrorCode |= HAL_SD_ERROR_DMA;
3066 }
3067 }
3068 /* Abort the SD DMA Rx channel */
3069 else if (((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
3070 {
3071 if(HAL_DMA_Abort(hsd->hdmarx) != HAL_OK)
3072 {
3073 hsd->ErrorCode |= HAL_SD_ERROR_DMA;
3074 }
3075 }
3076 else
3077 {
3078 /* Nothing to do */
3079 }
3080 }
3081 #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
3082
3083 hsd->State = HAL_SD_STATE_READY;
3084
3085 /* Initialize the SD operation */
3086 hsd->Context = SD_CONTEXT_NONE;
3087
3088 CardState = HAL_SD_GetCardState(hsd);
3089 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
3090 {
3091 hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
3092 }
3093 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
3094 {
3095 return HAL_ERROR;
3096 }
3097 return HAL_OK;
3098 }
3099
3100 /**
3101 * @brief Abort the current transfer and disable the SD (IT mode).
3102 * @param hsd pointer to a SD_HandleTypeDef structure that contains
3103 * the configuration information for SD module.
3104 * @retval HAL status
3105 */
HAL_SD_Abort_IT(SD_HandleTypeDef * hsd)3106 HAL_StatusTypeDef HAL_SD_Abort_IT(SD_HandleTypeDef *hsd)
3107 {
3108 HAL_SD_CardStateTypeDef CardState;
3109 #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
3110 uint32_t context = hsd->Context;
3111 #endif
3112
3113 /* Disable All interrupts */
3114 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
3115 SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
3116
3117 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
3118 /* If IDMA Context, disable Internal DMA */
3119 hsd->Instance->IDMACTRL = SDMMC_DISABLE_IDMA;
3120
3121 /* Clear All flags */
3122 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
3123
3124 CardState = HAL_SD_GetCardState(hsd);
3125 hsd->State = HAL_SD_STATE_READY;
3126
3127 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
3128 {
3129 hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
3130 }
3131
3132 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
3133 {
3134 return HAL_ERROR;
3135 }
3136 else
3137 {
3138 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
3139 hsd->AbortCpltCallback(hsd);
3140 #else
3141 HAL_SD_AbortCallback(hsd);
3142 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
3143 }
3144 #else
3145 CLEAR_BIT(hsd->Instance->DCTRL, SDMMC_DCTRL_DTEN);
3146
3147 if ((context & SD_CONTEXT_DMA) != 0U)
3148 {
3149 /* Disable the SD DMA request */
3150 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
3151
3152 /* Abort the SD DMA Tx channel */
3153 if (((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
3154 {
3155 hsd->hdmatx->XferAbortCallback = SD_DMATxAbort;
3156 if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK)
3157 {
3158 hsd->hdmatx = NULL;
3159 }
3160 }
3161 /* Abort the SD DMA Rx channel */
3162 else if (((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
3163 {
3164 hsd->hdmarx->XferAbortCallback = SD_DMARxAbort;
3165 if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK)
3166 {
3167 hsd->hdmarx = NULL;
3168 }
3169 }
3170 else
3171 {
3172 /* Nothing to do */
3173 }
3174 }
3175 /* No transfer ongoing on both DMA channels*/
3176 else
3177 {
3178 /* Clear All flags */
3179 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
3180
3181 CardState = HAL_SD_GetCardState(hsd);
3182 hsd->State = HAL_SD_STATE_READY;
3183 hsd->Context = SD_CONTEXT_NONE;
3184 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
3185 {
3186 hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
3187 }
3188 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
3189 {
3190 return HAL_ERROR;
3191 }
3192 else
3193 {
3194 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
3195 hsd->AbortCpltCallback(hsd);
3196 #else
3197 HAL_SD_AbortCallback(hsd);
3198 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
3199 }
3200 }
3201 #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
3202
3203 return HAL_OK;
3204 }
3205
3206 /**
3207 * @}
3208 */
3209
3210 /**
3211 * @}
3212 */
3213
3214 /* Private function ----------------------------------------------------------*/
3215 /** @addtogroup SD_Private_Functions
3216 * @{
3217 */
3218
3219 #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
3220 /**
3221 * @brief DMA SD transmit process complete callback
3222 * @param hdma DMA handle
3223 * @retval None
3224 */
SD_DMATransmitCplt(DMA_HandleTypeDef * hdma)3225 static void SD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
3226 {
3227 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
3228
3229 /* Enable DATAEND Interrupt */
3230 __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DATAEND));
3231 }
3232
3233 /**
3234 * @brief DMA SD receive process complete callback
3235 * @param hdma DMA handle
3236 * @retval None
3237 */
SD_DMAReceiveCplt(DMA_HandleTypeDef * hdma)3238 static void SD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
3239 {
3240 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
3241 uint32_t errorstate;
3242
3243 /* Send stop command in multiblock write */
3244 if(hsd->Context == (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA))
3245 {
3246 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
3247 if(errorstate != HAL_SD_ERROR_NONE)
3248 {
3249 hsd->ErrorCode |= errorstate;
3250 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
3251 hsd->ErrorCallback(hsd);
3252 #else
3253 HAL_SD_ErrorCallback(hsd);
3254 #endif
3255 }
3256 }
3257
3258 /* Disable the DMA transfer for transmit request by setting the DMAEN bit
3259 in the SD DCTRL register */
3260 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
3261
3262 /* Clear all the static flags */
3263 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
3264
3265 hsd->State = HAL_SD_STATE_READY;
3266 hsd->Context = SD_CONTEXT_NONE;
3267
3268 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
3269 hsd->RxCpltCallback(hsd);
3270 #else
3271 HAL_SD_RxCpltCallback(hsd);
3272 #endif
3273 }
3274
3275 /**
3276 * @brief DMA SD communication error callback
3277 * @param hdma DMA handle
3278 * @retval None
3279 */
SD_DMAError(DMA_HandleTypeDef * hdma)3280 static void SD_DMAError(DMA_HandleTypeDef *hdma)
3281 {
3282 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
3283 HAL_SD_CardStateTypeDef CardState;
3284 uint32_t RxErrorCode, TxErrorCode;
3285
3286 RxErrorCode = hsd->hdmarx->ErrorCode;
3287 TxErrorCode = hsd->hdmatx->ErrorCode;
3288 if((RxErrorCode == HAL_DMA_ERROR_TE) || (TxErrorCode == HAL_DMA_ERROR_TE))
3289 {
3290 /* Clear All flags */
3291 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
3292
3293 /* Disable All interrupts */
3294 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
3295 SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
3296
3297 hsd->ErrorCode |= HAL_SD_ERROR_DMA;
3298 CardState = HAL_SD_GetCardState(hsd);
3299 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
3300 {
3301 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
3302 }
3303
3304 hsd->State= HAL_SD_STATE_READY;
3305 hsd->Context = SD_CONTEXT_NONE;
3306 }
3307
3308 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
3309 hsd->ErrorCallback(hsd);
3310 #else
3311 HAL_SD_ErrorCallback(hsd);
3312 #endif
3313 }
3314
3315 /**
3316 * @brief DMA SD Tx Abort callback
3317 * @param hdma DMA handle
3318 * @retval None
3319 */
SD_DMATxAbort(DMA_HandleTypeDef * hdma)3320 static void SD_DMATxAbort(DMA_HandleTypeDef *hdma)
3321 {
3322 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
3323 HAL_SD_CardStateTypeDef CardState;
3324
3325 /* Clear All flags */
3326 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
3327
3328 CardState = HAL_SD_GetCardState(hsd);
3329 hsd->State = HAL_SD_STATE_READY;
3330 hsd->Context = SD_CONTEXT_NONE;
3331 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
3332 {
3333 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
3334 }
3335
3336 if(hsd->ErrorCode == HAL_SD_ERROR_NONE)
3337 {
3338 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
3339 hsd->AbortCpltCallback(hsd);
3340 #else
3341 HAL_SD_AbortCallback(hsd);
3342 #endif
3343 }
3344 else
3345 {
3346 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
3347 hsd->ErrorCallback(hsd);
3348 #else
3349 HAL_SD_ErrorCallback(hsd);
3350 #endif
3351 }
3352 }
3353
3354 /**
3355 * @brief DMA SD Rx Abort callback
3356 * @param hdma DMA handle
3357 * @retval None
3358 */
SD_DMARxAbort(DMA_HandleTypeDef * hdma)3359 static void SD_DMARxAbort(DMA_HandleTypeDef *hdma)
3360 {
3361 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
3362 HAL_SD_CardStateTypeDef CardState;
3363
3364 /* Clear All flags */
3365 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
3366
3367 CardState = HAL_SD_GetCardState(hsd);
3368 hsd->State = HAL_SD_STATE_READY;
3369 hsd->Context = SD_CONTEXT_NONE;
3370 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
3371 {
3372 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
3373 }
3374
3375 if(hsd->ErrorCode == HAL_SD_ERROR_NONE)
3376 {
3377 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
3378 hsd->AbortCpltCallback(hsd);
3379 #else
3380 HAL_SD_AbortCallback(hsd);
3381 #endif
3382 }
3383 else
3384 {
3385 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
3386 hsd->ErrorCallback(hsd);
3387 #else
3388 HAL_SD_ErrorCallback(hsd);
3389 #endif
3390 }
3391 }
3392 #endif /* !STM32L4P5xx && !STM32L4Q5xx && !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
3393
3394 /**
3395 * @brief Initializes the sd card.
3396 * @param hsd Pointer to SD handle
3397 * @retval SD Card error state
3398 */
SD_InitCard(SD_HandleTypeDef * hsd)3399 static uint32_t SD_InitCard(SD_HandleTypeDef *hsd)
3400 {
3401 HAL_SD_CardCSDTypeDef CSD;
3402 uint32_t errorstate;
3403 uint16_t sd_rca = 1U;
3404
3405 /* Check the power State */
3406 if(SDMMC_GetPowerState(hsd->Instance) == 0U)
3407 {
3408 /* Power off */
3409 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3410 }
3411
3412 if(hsd->SdCard.CardType != CARD_SECURED)
3413 {
3414 /* Send CMD2 ALL_SEND_CID */
3415 errorstate = SDMMC_CmdSendCID(hsd->Instance);
3416 if(errorstate != HAL_SD_ERROR_NONE)
3417 {
3418 return errorstate;
3419 }
3420 else
3421 {
3422 /* Get Card identification number data */
3423 hsd->CID[0U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
3424 hsd->CID[1U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2);
3425 hsd->CID[2U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP3);
3426 hsd->CID[3U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP4);
3427 }
3428 }
3429
3430 if(hsd->SdCard.CardType != CARD_SECURED)
3431 {
3432 /* Send CMD3 SET_REL_ADDR with argument 0 */
3433 /* SD Card publishes its RCA. */
3434 errorstate = SDMMC_CmdSetRelAdd(hsd->Instance, &sd_rca);
3435 if(errorstate != HAL_SD_ERROR_NONE)
3436 {
3437 return errorstate;
3438 }
3439 }
3440 if(hsd->SdCard.CardType != CARD_SECURED)
3441 {
3442 /* Get the SD card RCA */
3443 hsd->SdCard.RelCardAdd = sd_rca;
3444
3445 /* Send CMD9 SEND_CSD with argument as card's RCA */
3446 errorstate = SDMMC_CmdSendCSD(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
3447 if(errorstate != HAL_SD_ERROR_NONE)
3448 {
3449 return errorstate;
3450 }
3451 else
3452 {
3453 /* Get Card Specific Data */
3454 hsd->CSD[0U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
3455 hsd->CSD[1U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2);
3456 hsd->CSD[2U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP3);
3457 hsd->CSD[3U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP4);
3458 }
3459 }
3460
3461 /* Get the Card Class */
3462 hsd->SdCard.Class = (SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2) >> 20U);
3463
3464 /* Get CSD parameters */
3465 if (HAL_SD_GetCardCSD(hsd, &CSD) != HAL_OK)
3466 {
3467 return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
3468 }
3469
3470 /* Select the Card */
3471 errorstate = SDMMC_CmdSelDesel(hsd->Instance, (uint32_t)(((uint32_t)hsd->SdCard.RelCardAdd) << 16U));
3472 if(errorstate != HAL_SD_ERROR_NONE)
3473 {
3474 return errorstate;
3475 }
3476
3477 /* All cards are initialized */
3478 return HAL_SD_ERROR_NONE;
3479 }
3480
3481 /**
3482 * @brief Enquires cards about their operating voltage and configures clock
3483 * controls and stores SD information that will be needed in future
3484 * in the SD handle.
3485 * @param hsd Pointer to SD handle
3486 * @retval error state
3487 */
SD_PowerON(SD_HandleTypeDef * hsd)3488 static uint32_t SD_PowerON(SD_HandleTypeDef *hsd)
3489 {
3490 __IO uint32_t count = 0U;
3491 uint32_t response = 0U, validvoltage = 0U;
3492 uint32_t errorstate;
3493 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
3494 uint32_t tickstart = HAL_GetTick();
3495 #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
3496
3497 /* CMD0: GO_IDLE_STATE */
3498 errorstate = SDMMC_CmdGoIdleState(hsd->Instance);
3499 if(errorstate != HAL_SD_ERROR_NONE)
3500 {
3501 return errorstate;
3502 }
3503
3504 /* CMD8: SEND_IF_COND: Command available only on V2.0 cards */
3505 errorstate = SDMMC_CmdOperCond(hsd->Instance);
3506 if(errorstate != HAL_SD_ERROR_NONE)
3507 {
3508 hsd->SdCard.CardVersion = CARD_V1_X;
3509 /* CMD0: GO_IDLE_STATE */
3510 errorstate = SDMMC_CmdGoIdleState(hsd->Instance);
3511 if(errorstate != HAL_SD_ERROR_NONE)
3512 {
3513 return errorstate;
3514 }
3515
3516 }
3517 else
3518 {
3519 hsd->SdCard.CardVersion = CARD_V2_X;
3520 }
3521
3522 if( hsd->SdCard.CardVersion == CARD_V2_X)
3523 {
3524 /* SEND CMD55 APP_CMD with RCA as 0 */
3525 errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
3526 if(errorstate != HAL_SD_ERROR_NONE)
3527 {
3528 return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
3529 }
3530 }
3531 /* SD CARD */
3532 /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
3533 while((count < SDMMC_MAX_VOLT_TRIAL) && (validvoltage == 0U))
3534 {
3535 /* SEND CMD55 APP_CMD with RCA as 0 */
3536 errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
3537 if(errorstate != HAL_SD_ERROR_NONE)
3538 {
3539 return errorstate;
3540 }
3541
3542 /* Send CMD41 */
3543 errorstate = SDMMC_CmdAppOperCommand(hsd->Instance, SDMMC_VOLTAGE_WINDOW_SD | SDMMC_HIGH_CAPACITY | SD_SWITCH_1_8V_CAPACITY);
3544 if(errorstate != HAL_SD_ERROR_NONE)
3545 {
3546 return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
3547 }
3548
3549 /* Get command response */
3550 response = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
3551
3552 /* Get operating voltage*/
3553 validvoltage = (((response >> 31U) == 1U) ? 1U : 0U);
3554
3555 count++;
3556 }
3557
3558 if(count >= SDMMC_MAX_VOLT_TRIAL)
3559 {
3560 return HAL_SD_ERROR_INVALID_VOLTRANGE;
3561 }
3562
3563 if((response & SDMMC_HIGH_CAPACITY) == SDMMC_HIGH_CAPACITY) /* (response &= SD_HIGH_CAPACITY) */
3564 {
3565 hsd->SdCard.CardType = CARD_SDHC_SDXC;
3566 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
3567 if(hsd->Init.Transceiver == SDMMC_TRANSCEIVER_ENABLE)
3568 {
3569 if((response & SD_SWITCH_1_8V_CAPACITY) == SD_SWITCH_1_8V_CAPACITY)
3570 {
3571 hsd->SdCard.CardSpeed = CARD_ULTRA_HIGH_SPEED;
3572
3573 /* Start switching procedue */
3574 hsd->Instance->POWER |= SDMMC_POWER_VSWITCHEN;
3575
3576 /* Send CMD11 to switch 1.8V mode */
3577 errorstate = SDMMC_CmdVoltageSwitch(hsd->Instance);
3578 if(errorstate != HAL_SD_ERROR_NONE)
3579 {
3580 return errorstate;
3581 }
3582
3583 /* Check to CKSTOP */
3584 while(( hsd->Instance->STA & SDMMC_FLAG_CKSTOP) != SDMMC_FLAG_CKSTOP)
3585 {
3586 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
3587 {
3588 return HAL_SD_ERROR_TIMEOUT;
3589 }
3590 }
3591
3592 /* Clear CKSTOP Flag */
3593 hsd->Instance->ICR = SDMMC_FLAG_CKSTOP;
3594
3595 /* Check to BusyD0 */
3596 if(( hsd->Instance->STA & SDMMC_FLAG_BUSYD0) != SDMMC_FLAG_BUSYD0)
3597 {
3598 /* Error when activate Voltage Switch in SDMMC Peripheral */
3599 return SDMMC_ERROR_UNSUPPORTED_FEATURE;
3600 }
3601 else
3602 {
3603 /* Enable Transceiver Switch PIN */
3604 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
3605 hsd->DriveTransceiver_1_8V_Callback(SET);
3606 #else
3607 HAL_SDEx_DriveTransceiver_1_8V_Callback(SET);
3608 #endif
3609
3610 /* Switch ready */
3611 hsd->Instance->POWER |= SDMMC_POWER_VSWITCH;
3612
3613 /* Check VSWEND Flag */
3614 while(( hsd->Instance->STA & SDMMC_FLAG_VSWEND) != SDMMC_FLAG_VSWEND)
3615 {
3616 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
3617 {
3618 return HAL_SD_ERROR_TIMEOUT;
3619 }
3620 }
3621
3622 /* Clear VSWEND Flag */
3623 hsd->Instance->ICR = SDMMC_FLAG_VSWEND;
3624
3625 /* Check BusyD0 status */
3626 if(( hsd->Instance->STA & SDMMC_FLAG_BUSYD0) == SDMMC_FLAG_BUSYD0)
3627 {
3628 /* Error when enabling 1.8V mode */
3629 return HAL_SD_ERROR_INVALID_VOLTRANGE;
3630 }
3631 /* Switch to 1.8V OK */
3632
3633 /* Disable VSWITCH FLAG from SDMMC Peripheral */
3634 hsd->Instance->POWER = 0x13U;
3635
3636 /* Clean Status flags */
3637 hsd->Instance->ICR = 0xFFFFFFFFU;
3638 }
3639 }
3640 }
3641 #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
3642 }
3643 else
3644 {
3645 hsd->SdCard.CardType = CARD_SDSC;
3646 }
3647
3648
3649 return HAL_SD_ERROR_NONE;
3650 }
3651
3652 /**
3653 * @brief Turns the SDMMC output signals off.
3654 * @param hsd Pointer to SD handle
3655 * @retval None
3656 */
SD_PowerOFF(SD_HandleTypeDef * hsd)3657 static void SD_PowerOFF(SD_HandleTypeDef *hsd)
3658 {
3659 /* Set Power State to OFF */
3660 (void)SDMMC_PowerState_OFF(hsd->Instance);
3661 }
3662
3663 /**
3664 * @brief Send Status info command.
3665 * @param hsd pointer to SD handle
3666 * @param pSDstatus Pointer to the buffer that will contain the SD card status
3667 * SD Status register)
3668 * @retval error state
3669 */
SD_SendSDStatus(SD_HandleTypeDef * hsd,uint32_t * pSDstatus)3670 static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus)
3671 {
3672 SDMMC_DataInitTypeDef config;
3673 uint32_t errorstate;
3674 uint32_t tickstart = HAL_GetTick();
3675 uint32_t count;
3676 uint32_t *pData = pSDstatus;
3677
3678 /* Check SD response */
3679 if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
3680 {
3681 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
3682 }
3683
3684 /* Set block size for card if it is not equal to current block size for card */
3685 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U);
3686 if(errorstate != HAL_SD_ERROR_NONE)
3687 {
3688 hsd->ErrorCode |= HAL_SD_ERROR_NONE;
3689 return errorstate;
3690 }
3691
3692 /* Send CMD55 */
3693 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
3694 if(errorstate != HAL_SD_ERROR_NONE)
3695 {
3696 hsd->ErrorCode |= HAL_SD_ERROR_NONE;
3697 return errorstate;
3698 }
3699
3700 /* Configure the SD DPSM (Data Path State Machine) */
3701 config.DataTimeOut = SDMMC_DATATIMEOUT;
3702 config.DataLength = 64U;
3703 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B;
3704 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
3705 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
3706 config.DPSM = SDMMC_DPSM_ENABLE;
3707 (void)SDMMC_ConfigData(hsd->Instance, &config);
3708
3709 /* Send ACMD13 (SD_APP_STAUS) with argument as card's RCA */
3710 errorstate = SDMMC_CmdStatusRegister(hsd->Instance);
3711 if(errorstate != HAL_SD_ERROR_NONE)
3712 {
3713 hsd->ErrorCode |= HAL_SD_ERROR_NONE;
3714 return errorstate;
3715 }
3716
3717 /* Get status data */
3718 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
3719 while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
3720 #else
3721 while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND))
3722 #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
3723 {
3724 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
3725 {
3726 for(count = 0U; count < 8U; count++)
3727 {
3728 *pData = SDMMC_ReadFIFO(hsd->Instance);
3729 pData++;
3730 }
3731 }
3732
3733 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
3734 {
3735 return HAL_SD_ERROR_TIMEOUT;
3736 }
3737 }
3738
3739 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
3740 {
3741 return HAL_SD_ERROR_DATA_TIMEOUT;
3742 }
3743 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
3744 {
3745 return HAL_SD_ERROR_DATA_CRC_FAIL;
3746 }
3747 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
3748 {
3749 return HAL_SD_ERROR_RX_OVERRUN;
3750 }
3751 else
3752 {
3753 /* Nothing to do */
3754 }
3755
3756 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
3757 while ((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DPSMACT)))
3758 #else
3759 while ((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL)))
3760 #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
3761 {
3762 *pData = SDMMC_ReadFIFO(hsd->Instance);
3763 pData++;
3764
3765 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
3766 {
3767 return HAL_SD_ERROR_TIMEOUT;
3768 }
3769 }
3770
3771 /* Clear all the static status flags*/
3772 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
3773
3774 return HAL_SD_ERROR_NONE;
3775 }
3776
3777 /**
3778 * @brief Returns the current card's status.
3779 * @param hsd Pointer to SD handle
3780 * @param pCardStatus pointer to the buffer that will contain the SD card
3781 * status (Card Status register)
3782 * @retval error state
3783 */
SD_SendStatus(SD_HandleTypeDef * hsd,uint32_t * pCardStatus)3784 static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus)
3785 {
3786 uint32_t errorstate;
3787
3788 if(pCardStatus == NULL)
3789 {
3790 return HAL_SD_ERROR_PARAM;
3791 }
3792
3793 /* Send Status command */
3794 errorstate = SDMMC_CmdSendStatus(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
3795 if(errorstate != HAL_SD_ERROR_NONE)
3796 {
3797 return errorstate;
3798 }
3799
3800 /* Get SD card status */
3801 *pCardStatus = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
3802
3803 return HAL_SD_ERROR_NONE;
3804 }
3805
3806 /**
3807 * @brief Enables the SDMMC wide bus mode.
3808 * @param hsd pointer to SD handle
3809 * @retval error state
3810 */
SD_WideBus_Enable(SD_HandleTypeDef * hsd)3811 static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd)
3812 {
3813 uint32_t scr[2U] = {0UL, 0UL};
3814 uint32_t errorstate;
3815
3816 if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
3817 {
3818 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
3819 }
3820
3821 /* Get SCR Register */
3822 errorstate = SD_FindSCR(hsd, scr);
3823 if(errorstate != HAL_SD_ERROR_NONE)
3824 {
3825 return errorstate;
3826 }
3827
3828 /* If requested card supports wide bus operation */
3829 if((scr[1U] & SDMMC_WIDE_BUS_SUPPORT) != SDMMC_ALLZERO)
3830 {
3831 /* Send CMD55 APP_CMD with argument as card's RCA.*/
3832 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
3833 if(errorstate != HAL_SD_ERROR_NONE)
3834 {
3835 return errorstate;
3836 }
3837
3838 /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
3839 errorstate = SDMMC_CmdBusWidth(hsd->Instance, 2U);
3840 if(errorstate != HAL_SD_ERROR_NONE)
3841 {
3842 return errorstate;
3843 }
3844
3845 return HAL_SD_ERROR_NONE;
3846 }
3847 else
3848 {
3849 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3850 }
3851 }
3852
3853 /**
3854 * @brief Disables the SDMMC wide bus mode.
3855 * @param hsd Pointer to SD handle
3856 * @retval error state
3857 */
SD_WideBus_Disable(SD_HandleTypeDef * hsd)3858 static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd)
3859 {
3860 uint32_t scr[2U] = {0UL, 0UL};
3861 uint32_t errorstate;
3862
3863 if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
3864 {
3865 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
3866 }
3867
3868 /* Get SCR Register */
3869 errorstate = SD_FindSCR(hsd, scr);
3870 if(errorstate != HAL_SD_ERROR_NONE)
3871 {
3872 return errorstate;
3873 }
3874
3875 /* If requested card supports 1 bit mode operation */
3876 if((scr[1U] & SDMMC_SINGLE_BUS_SUPPORT) != SDMMC_ALLZERO)
3877 {
3878 /* Send CMD55 APP_CMD with argument as card's RCA */
3879 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
3880 if(errorstate != HAL_SD_ERROR_NONE)
3881 {
3882 return errorstate;
3883 }
3884
3885 /* Send ACMD6 APP_CMD with argument as 0 for single bus mode */
3886 errorstate = SDMMC_CmdBusWidth(hsd->Instance, 0U);
3887 if(errorstate != HAL_SD_ERROR_NONE)
3888 {
3889 return errorstate;
3890 }
3891
3892 return HAL_SD_ERROR_NONE;
3893 }
3894 else
3895 {
3896 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3897 }
3898 }
3899
3900
3901 /**
3902 * @brief Finds the SD card SCR register value.
3903 * @param hsd Pointer to SD handle
3904 * @param pSCR pointer to the buffer that will contain the SCR value
3905 * @retval error state
3906 */
SD_FindSCR(SD_HandleTypeDef * hsd,uint32_t * pSCR)3907 static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR)
3908 {
3909 SDMMC_DataInitTypeDef config;
3910 uint32_t errorstate;
3911 uint32_t tickstart = HAL_GetTick();
3912 uint32_t index = 0U;
3913 uint32_t tempscr[2U] = {0UL, 0UL};
3914 uint32_t *scr = pSCR;
3915
3916 /* Set Block Size To 8 Bytes */
3917 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 8U);
3918 if(errorstate != HAL_SD_ERROR_NONE)
3919 {
3920 return errorstate;
3921 }
3922
3923 /* Send CMD55 APP_CMD with argument as card's RCA */
3924 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)((hsd->SdCard.RelCardAdd) << 16U));
3925 if(errorstate != HAL_SD_ERROR_NONE)
3926 {
3927 return errorstate;
3928 }
3929
3930 config.DataTimeOut = SDMMC_DATATIMEOUT;
3931 config.DataLength = 8U;
3932 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_8B;
3933 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
3934 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
3935 config.DPSM = SDMMC_DPSM_ENABLE;
3936 (void)SDMMC_ConfigData(hsd->Instance, &config);
3937
3938 /* Send ACMD51 SD_APP_SEND_SCR with argument as 0 */
3939 errorstate = SDMMC_CmdSendSCR(hsd->Instance);
3940 if(errorstate != HAL_SD_ERROR_NONE)
3941 {
3942 return errorstate;
3943 }
3944
3945 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
3946 while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND | SDMMC_FLAG_DATAEND))
3947 {
3948 if((!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOE)) && (index == 0U))
3949 {
3950 tempscr[0] = SDMMC_ReadFIFO(hsd->Instance);
3951 tempscr[1] = SDMMC_ReadFIFO(hsd->Instance);
3952 index++;
3953 }
3954
3955
3956 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
3957 {
3958 return HAL_SD_ERROR_TIMEOUT;
3959 }
3960 }
3961 #else
3962 while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND))
3963 {
3964 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL))
3965 {
3966 *(tempscr + index) = SDMMC_ReadFIFO(hsd->Instance);
3967 index++;
3968 }
3969
3970 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
3971 {
3972 return HAL_SD_ERROR_TIMEOUT;
3973 }
3974 }
3975 #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
3976
3977 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
3978 {
3979 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
3980
3981 return HAL_SD_ERROR_DATA_TIMEOUT;
3982 }
3983 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
3984 {
3985 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
3986
3987 return HAL_SD_ERROR_DATA_CRC_FAIL;
3988 }
3989 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
3990 {
3991 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
3992
3993 return HAL_SD_ERROR_RX_OVERRUN;
3994 }
3995 else
3996 {
3997 /* No error flag set */
3998 /* Clear all the static flags */
3999 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
4000
4001 *scr = (((tempscr[1] & SDMMC_0TO7BITS) << 24) | ((tempscr[1] & SDMMC_8TO15BITS) << 8) |\
4002 ((tempscr[1] & SDMMC_16TO23BITS) >> 8) | ((tempscr[1] & SDMMC_24TO31BITS) >> 24));
4003 scr++;
4004 *scr = (((tempscr[0] & SDMMC_0TO7BITS) << 24) | ((tempscr[0] & SDMMC_8TO15BITS) << 8) |\
4005 ((tempscr[0] & SDMMC_16TO23BITS) >> 8) | ((tempscr[0] & SDMMC_24TO31BITS) >> 24));
4006
4007 }
4008
4009 return HAL_SD_ERROR_NONE;
4010 }
4011
4012 /**
4013 * @brief Wrap up reading in non-blocking mode.
4014 * @param hsd pointer to a SD_HandleTypeDef structure that contains
4015 * the configuration information.
4016 * @retval None
4017 */
SD_Read_IT(SD_HandleTypeDef * hsd)4018 static void SD_Read_IT(SD_HandleTypeDef *hsd)
4019 {
4020 uint32_t count, data, dataremaining;
4021 uint8_t* tmp;
4022
4023 tmp = hsd->pRxBuffPtr;
4024 dataremaining = hsd->RxXferSize;
4025
4026 if (dataremaining > 0U)
4027 {
4028 /* Read data from SDMMC Rx FIFO */
4029 for(count = 0U; count < 8U; count++)
4030 {
4031 data = SDMMC_ReadFIFO(hsd->Instance);
4032 *tmp = (uint8_t)(data & 0xFFU);
4033 tmp++;
4034 dataremaining--;
4035 *tmp = (uint8_t)((data >> 8U) & 0xFFU);
4036 tmp++;
4037 dataremaining--;
4038 *tmp = (uint8_t)((data >> 16U) & 0xFFU);
4039 tmp++;
4040 dataremaining--;
4041 *tmp = (uint8_t)((data >> 24U) & 0xFFU);
4042 tmp++;
4043 dataremaining--;
4044 }
4045
4046 hsd->pRxBuffPtr = tmp;
4047 hsd->RxXferSize = dataremaining;
4048 }
4049 }
4050
4051 /**
4052 * @brief Wrap up writing in non-blocking mode.
4053 * @param hsd pointer to a SD_HandleTypeDef structure that contains
4054 * the configuration information.
4055 * @retval None
4056 */
SD_Write_IT(SD_HandleTypeDef * hsd)4057 static void SD_Write_IT(SD_HandleTypeDef *hsd)
4058 {
4059 uint32_t count, data, dataremaining;
4060 uint8_t* tmp;
4061
4062 tmp = hsd->pTxBuffPtr;
4063 dataremaining = hsd->TxXferSize;
4064
4065 if (dataremaining > 0U)
4066 {
4067 /* Write data to SDMMC Tx FIFO */
4068 for(count = 0U; count < 8U; count++)
4069 {
4070 data = (uint32_t)(*tmp);
4071 tmp++;
4072 dataremaining--;
4073 data |= ((uint32_t)(*tmp) << 8U);
4074 tmp++;
4075 dataremaining--;
4076 data |= ((uint32_t)(*tmp) << 16U);
4077 tmp++;
4078 dataremaining--;
4079 data |= ((uint32_t)(*tmp) << 24U);
4080 tmp++;
4081 dataremaining--;
4082 (void)SDMMC_WriteFIFO(hsd->Instance, &data);
4083 }
4084
4085 hsd->pTxBuffPtr = tmp;
4086 hsd->TxXferSize = dataremaining;
4087 }
4088 }
4089
4090 #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
4091 /**
4092 * @brief Switches the SD card to High Speed mode.
4093 * This API must be used after "Transfer State"
4094 * @note This operation should be followed by the configuration
4095 * of PLL to have SDMMCCK clock between 50 and 120 MHz
4096 * @param hsd SD handle
4097 * @retval SD Card error state
4098 */
SD_HighSpeed(SD_HandleTypeDef * hsd)4099 uint32_t SD_HighSpeed(SD_HandleTypeDef *hsd)
4100 {
4101 uint32_t errorstate = HAL_SD_ERROR_NONE;
4102 SDMMC_DataInitTypeDef sdmmc_datainitstructure;
4103 uint32_t SD_hs[16] = {0};
4104 uint32_t count, loop = 0 ;
4105 uint32_t Timeout = HAL_GetTick();
4106
4107 if(hsd->SdCard.CardSpeed == CARD_NORMAL_SPEED)
4108 {
4109 /* Standard Speed Card <= 12.5Mhz */
4110 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
4111 }
4112
4113 if(hsd->SdCard.CardSpeed == CARD_HIGH_SPEED)
4114 {
4115 /* Initialize the Data control register */
4116 hsd->Instance->DCTRL = 0;
4117 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U);
4118
4119 if (errorstate != HAL_SD_ERROR_NONE)
4120 {
4121 return errorstate;
4122 }
4123
4124 /* Configure the SD DPSM (Data Path State Machine) */
4125 sdmmc_datainitstructure.DataTimeOut = SDMMC_DATATIMEOUT;
4126 sdmmc_datainitstructure.DataLength = 64U;
4127 sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B ;
4128 sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
4129 sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
4130 sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE;
4131
4132 (void)SDMMC_ConfigData(hsd->Instance, &sdmmc_datainitstructure);
4133
4134 errorstate = SDMMC_CmdSwitch(hsd->Instance,SDMMC_SDR25_SWITCH_PATTERN);
4135 if(errorstate != HAL_SD_ERROR_NONE)
4136 {
4137 return errorstate;
4138 }
4139
4140 while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND| SDMMC_FLAG_DATAEND ))
4141 {
4142 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
4143 {
4144 for (count = 0U; count < 8U; count++)
4145 {
4146 SD_hs[(8U*loop)+count] = SDMMC_ReadFIFO(hsd->Instance);
4147 }
4148 loop ++;
4149 }
4150
4151 if((HAL_GetTick()-Timeout) >= SDMMC_DATATIMEOUT)
4152 {
4153 hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
4154 hsd->State= HAL_SD_STATE_READY;
4155 return HAL_SD_ERROR_TIMEOUT;
4156 }
4157 }
4158
4159 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
4160 {
4161 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
4162
4163 return errorstate;
4164 }
4165 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
4166 {
4167 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
4168
4169 errorstate = SDMMC_ERROR_DATA_CRC_FAIL;
4170
4171 return errorstate;
4172 }
4173 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
4174 {
4175 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
4176
4177 errorstate = SDMMC_ERROR_RX_OVERRUN;
4178
4179 return errorstate;
4180 }
4181 else
4182 {
4183 /* No error flag set */
4184 }
4185
4186 /* Clear all the static flags */
4187 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
4188
4189 /* Test if the switch mode HS is ok */
4190 if ((((uint8_t*)SD_hs)[13] & 2U) != 2U)
4191 {
4192 errorstate = SDMMC_ERROR_UNSUPPORTED_FEATURE;
4193 }
4194
4195 }
4196
4197 return errorstate;
4198 }
4199
4200 /**
4201 * @brief Switches the SD card to Ultra High Speed mode.
4202 * This API must be used after "Transfer State"
4203 * @note This operation should be followed by the configuration
4204 * of PLL to have SDMMCCK clock between 50 and 120 MHz
4205 * @param hsd SD handle
4206 * @retval SD Card error state
4207 */
SD_UltraHighSpeed(SD_HandleTypeDef * hsd)4208 static uint32_t SD_UltraHighSpeed(SD_HandleTypeDef *hsd)
4209 {
4210 uint32_t errorstate = HAL_SD_ERROR_NONE;
4211 SDMMC_DataInitTypeDef sdmmc_datainitstructure;
4212 uint32_t SD_hs[16] = {0};
4213 uint32_t count, loop = 0 ;
4214 uint32_t Timeout = HAL_GetTick();
4215
4216 if(hsd->SdCard.CardSpeed == CARD_NORMAL_SPEED)
4217 {
4218 /* Standard Speed Card <= 12.5Mhz */
4219 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
4220 }
4221
4222 if((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) &&
4223 (hsd->Init.Transceiver == SDMMC_TRANSCEIVER_ENABLE))
4224 {
4225 /* Initialize the Data control register */
4226 hsd->Instance->DCTRL = 0;
4227 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U);
4228
4229 if (errorstate != HAL_SD_ERROR_NONE)
4230 {
4231 return errorstate;
4232 }
4233
4234 /* Configure the SD DPSM (Data Path State Machine) */
4235 sdmmc_datainitstructure.DataTimeOut = SDMMC_DATATIMEOUT;
4236 sdmmc_datainitstructure.DataLength = 64U;
4237 sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B ;
4238 sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
4239 sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
4240 sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE;
4241
4242 (void)SDMMC_ConfigData(hsd->Instance, &sdmmc_datainitstructure);
4243
4244 errorstate = SDMMC_CmdSwitch(hsd->Instance, SDMMC_SDR104_SWITCH_PATTERN);
4245 if(errorstate != HAL_SD_ERROR_NONE)
4246 {
4247 return errorstate;
4248 }
4249
4250 while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND| SDMMC_FLAG_DATAEND ))
4251 {
4252 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
4253 {
4254 for (count = 0U; count < 8U; count++)
4255 {
4256 SD_hs[(8U*loop)+count] = SDMMC_ReadFIFO(hsd->Instance);
4257 }
4258 loop ++;
4259 }
4260
4261 if((HAL_GetTick()-Timeout) >= SDMMC_DATATIMEOUT)
4262 {
4263 hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
4264 hsd->State= HAL_SD_STATE_READY;
4265 return HAL_SD_ERROR_TIMEOUT;
4266 }
4267 }
4268
4269 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
4270 {
4271 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
4272
4273 return errorstate;
4274 }
4275 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
4276 {
4277 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
4278
4279 errorstate = SDMMC_ERROR_DATA_CRC_FAIL;
4280
4281 return errorstate;
4282 }
4283 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
4284 {
4285 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
4286
4287 errorstate = SDMMC_ERROR_RX_OVERRUN;
4288
4289 return errorstate;
4290 }
4291 else
4292 {
4293 /* No error flag set */
4294 }
4295
4296 /* Clear all the static flags */
4297 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
4298
4299 /* Test if the switch mode HS is ok */
4300 if ((((uint8_t*)SD_hs)[13] & 2U) != 2U)
4301 {
4302 errorstate = SDMMC_ERROR_UNSUPPORTED_FEATURE;
4303 }
4304 else
4305 {
4306 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
4307 hsd->DriveTransceiver_1_8V_Callback(SET);
4308 #else
4309 HAL_SDEx_DriveTransceiver_1_8V_Callback(SET);
4310 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
4311 #if defined (DLYB_SDMMC1) || defined (DLYB_SDMMC2)
4312 /* Enable DelayBlock Peripheral */
4313 /* SDMMC_FB_CLK tuned feedback clock selected as receive clock, for SDR104 */
4314 MODIFY_REG(hsd->Instance->CLKCR, SDMMC_CLKCR_SELCLKRX,SDMMC_CLKCR_SELCLKRX_1);
4315 if (DelayBlock_Enable(SD_GET_DLYB_INSTANCE(hsd->Instance)) != HAL_OK)
4316 {
4317 return (HAL_SD_ERROR_GENERAL_UNKNOWN_ERR);
4318 }
4319 #endif /* (DLYB_SDMMC1) || (DLYB_SDMMC2) */
4320 }
4321 }
4322
4323 return errorstate;
4324 }
4325
4326 /**
4327 * @brief Switches the SD card to Double Data Rate (DDR) mode.
4328 * This API must be used after "Transfer State"
4329 * @note This operation should be followed by the configuration
4330 * of PLL to have SDMMCCK clock less than 50MHz
4331 * @param hsd SD handle
4332 * @retval SD Card error state
4333 */
SD_DDR_Mode(SD_HandleTypeDef * hsd)4334 static uint32_t SD_DDR_Mode(SD_HandleTypeDef *hsd)
4335 {
4336 uint32_t errorstate = HAL_SD_ERROR_NONE;
4337 SDMMC_DataInitTypeDef sdmmc_datainitstructure;
4338 uint32_t SD_hs[16] = {0};
4339 uint32_t count, loop = 0 ;
4340 uint32_t Timeout = HAL_GetTick();
4341
4342 if(hsd->SdCard.CardSpeed == CARD_NORMAL_SPEED)
4343 {
4344 /* Standard Speed Card <= 12.5Mhz */
4345 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
4346 }
4347
4348 if((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) &&
4349 (hsd->Init.Transceiver == SDMMC_TRANSCEIVER_ENABLE))
4350 {
4351 /* Initialize the Data control register */
4352 hsd->Instance->DCTRL = 0;
4353 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U);
4354
4355 if (errorstate != HAL_SD_ERROR_NONE)
4356 {
4357 return errorstate;
4358 }
4359
4360 /* Configure the SD DPSM (Data Path State Machine) */
4361 sdmmc_datainitstructure.DataTimeOut = SDMMC_DATATIMEOUT;
4362 sdmmc_datainitstructure.DataLength = 64U;
4363 sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B ;
4364 sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
4365 sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
4366 sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE;
4367
4368 (void)SDMMC_ConfigData(hsd->Instance, &sdmmc_datainitstructure);
4369
4370 errorstate = SDMMC_CmdSwitch(hsd->Instance, SDMMC_DDR50_SWITCH_PATTERN);
4371 if(errorstate != HAL_SD_ERROR_NONE)
4372 {
4373 return errorstate;
4374 }
4375
4376 while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND| SDMMC_FLAG_DATAEND ))
4377 {
4378 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
4379 {
4380 for (count = 0U; count < 8U; count++)
4381 {
4382 SD_hs[(8U*loop)+count] = SDMMC_ReadFIFO(hsd->Instance);
4383 }
4384 loop ++;
4385 }
4386
4387 if((HAL_GetTick()-Timeout) >= SDMMC_DATATIMEOUT)
4388 {
4389 hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
4390 hsd->State= HAL_SD_STATE_READY;
4391 return HAL_SD_ERROR_TIMEOUT;
4392 }
4393 }
4394
4395 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
4396 {
4397 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
4398
4399 return errorstate;
4400 }
4401 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
4402 {
4403 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
4404
4405 errorstate = SDMMC_ERROR_DATA_CRC_FAIL;
4406
4407 return errorstate;
4408 }
4409 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
4410 {
4411 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
4412
4413 errorstate = SDMMC_ERROR_RX_OVERRUN;
4414
4415 return errorstate;
4416 }
4417 else
4418 {
4419 /* No error flag set */
4420 }
4421
4422 /* Clear all the static flags */
4423 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
4424
4425 /* Test if the switch mode is ok */
4426 if ((((uint8_t*)SD_hs)[13] & 2U) != 2U)
4427 {
4428 errorstate = SDMMC_ERROR_UNSUPPORTED_FEATURE;
4429 }
4430 else
4431 {
4432 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
4433 hsd->DriveTransceiver_1_8V_Callback(SET);
4434 #else
4435 HAL_SDEx_DriveTransceiver_1_8V_Callback(SET);
4436 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
4437 #if defined (DLYB_SDMMC1) || defined (DLYB_SDMMC2)
4438 /* Enable DelayBlock Peripheral */
4439 /* SDMMC_FB_CLK tuned feedback clock selected as receive clock, for SDR104 */
4440 MODIFY_REG(hsd->Instance->CLKCR, SDMMC_CLKCR_SELCLKRX,SDMMC_CLKCR_SELCLKRX_1);
4441 if (DelayBlock_Enable(SD_GET_DLYB_INSTANCE(hsd->Instance)) != HAL_OK)
4442 {
4443 return (HAL_SD_ERROR_GENERAL_UNKNOWN_ERR);
4444 }
4445 #endif /* (DLYB_SDMMC1) || (DLYB_SDMMC2) */
4446 }
4447 }
4448
4449 return errorstate;
4450 }
4451
4452 #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
4453 /**
4454 * @}
4455 */
4456
4457 #endif /* HAL_SD_MODULE_ENABLED */
4458
4459 /**
4460 * @}
4461 */
4462
4463 /**
4464 * @}
4465 */
4466
4467 #endif /* SDMMC1 */
4468