sidestep
[mTask.git] / int / com / lib / STM32F7xx_HAL_Driver / Src / stm32f7xx_hal_sd.c
1 /**
2 ******************************************************************************
3 * @file stm32f7xx_hal_sd.c
4 * @author MCD Application Team
5 * @version V1.1.0
6 * @date 22-April-2016
7 * @brief SD card HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the Secure Digital (SD) peripheral:
10 * + Initialization and de-initialization functions
11 * + IO operation functions
12 * + Peripheral Control functions
13 * + Peripheral State functions
14 *
15 @verbatim
16 ==============================================================================
17 ##### How to use this driver #####
18 ==============================================================================
19 [..]
20 This driver implements a high level communication layer for read and write from/to
21 this memory. The needed STM32 hardware resources (SDMMC and GPIO) are performed by
22 the user in HAL_SD_MspInit() function (MSP layer).
23 Basically, the MSP layer configuration should be the same as we provide in the
24 examples.
25 You can easily tailor this configuration according to hardware resources.
26
27 [..]
28 This driver is a generic layered driver for SDMMC memories which uses the HAL
29 SDMMC driver functions to interface with SD and uSD cards devices.
30 It is used as follows:
31
32 (#)Initialize the SDMMC low level resources by implement the HAL_SD_MspInit() API:
33 (##) Enable the SDMMC interface clock using __HAL_RCC_SDMMC_CLK_ENABLE();
34 (##) SDMMC pins configuration for SD card
35 (+++) Enable the clock for the SDMMC GPIOs using the functions __HAL_RCC_GPIOx_CLK_ENABLE();
36 (+++) Configure these SDMMC pins as alternate function pull-up using HAL_GPIO_Init()
37 and according to your pin assignment;
38 (##) DMA Configuration if you need to use DMA process (HAL_SD_ReadBlocks_DMA()
39 and HAL_SD_WriteBlocks_DMA() APIs).
40 (+++) Enable the DMAx interface clock using __HAL_RCC_DMAx_CLK_ENABLE();
41 (+++) Configure the DMA using the function HAL_DMA_Init() with predeclared and filled.
42 (##) NVIC configuration if you need to use interrupt process when using DMA transfer.
43 (+++) Configure the SDMMC and DMA interrupt priorities using functions
44 HAL_NVIC_SetPriority(); DMA priority is superior to SDMMC's priority
45 (+++) Enable the NVIC DMA and SDMMC IRQs using function HAL_NVIC_EnableIRQ()
46 (+++) SDMMC interrupts are managed using the macros __HAL_SD_SDMMC_ENABLE_IT()
47 and __HAL_SD_SDMMC_DISABLE_IT() inside the communication process.
48 (+++) SDMMC interrupts pending bits are managed using the macros __HAL_SD_SDMMC_GET_IT()
49 and __HAL_SD_SDMMC_CLEAR_IT()
50 (#) At this stage, you can perform SD read/write/erase operations after SD card initialization
51
52
53 *** SD Card Initialization and configuration ***
54 ================================================
55 [..]
56 To initialize the SD Card, use the HAL_SD_Init() function. It Initializes
57 the SD Card and put it into StandBy State (Ready for data transfer).
58 This function provide the following operations:
59
60 (#) Apply the SD Card initialization process at 400KHz and check the SD Card
61 type (Standard Capacity or High Capacity). You can change or adapt this
62 frequency by adjusting the "ClockDiv" field.
63 The SD Card frequency (SDMMC_CK) is computed as follows:
64
65 SDMMC_CK = SDMMCCLK / (ClockDiv + 2)
66
67 In initialization mode and according to the SD Card standard,
68 make sure that the SDMMC_CK frequency doesn't exceed 400KHz.
69
70 (#) Get the SD CID and CSD data. All these information are managed by the SDCardInfo
71 structure. This structure provide also ready computed SD Card capacity
72 and Block size.
73
74 -@- These information are stored in SD handle structure in case of future use.
75
76 (#) Configure the SD Card Data transfer frequency. By Default, the card transfer
77 frequency is set to 24MHz. You can change or adapt this frequency by adjusting
78 the "ClockDiv" field.
79 In transfer mode and according to the SD Card standard, make sure that the
80 SDMMC_CK frequency doesn't exceed 25MHz and 50MHz in High-speed mode switch.
81 To be able to use a frequency higher than 24MHz, you should use the SDMMC
82 peripheral in bypass mode. Refer to the corresponding reference manual
83 for more details.
84
85 (#) Select the corresponding SD Card according to the address read with the step 2.
86
87 (#) Configure the SD Card in wide bus mode: 4-bits data.
88
89 *** SD Card Read operation ***
90 ==============================
91 [..]
92 (+) You can read from SD card in polling mode by using function HAL_SD_ReadBlocks().
93 This function support only 512-bytes block length (the block size should be
94 chosen as 512 bytes).
95 You can choose either one block read operation or multiple block read operation
96 by adjusting the "NumberOfBlocks" parameter.
97
98 (+) You can read from SD card in DMA mode by using function HAL_SD_ReadBlocks_DMA().
99 This function support only 512-bytes block length (the block size should be
100 chosen as 512 bytes).
101 You can choose either one block read operation or multiple block read operation
102 by adjusting the "NumberOfBlocks" parameter.
103 After this, you have to call the function HAL_SD_CheckReadOperation(), to insure
104 that the read transfer is done correctly in both DMA and SD sides.
105
106 *** SD Card Write operation ***
107 ===============================
108 [..]
109 (+) You can write to SD card in polling mode by using function HAL_SD_WriteBlocks().
110 This function support only 512-bytes block length (the block size should be
111 chosen as 512 bytes).
112 You can choose either one block read operation or multiple block read operation
113 by adjusting the "NumberOfBlocks" parameter.
114
115 (+) You can write to SD card in DMA mode by using function HAL_SD_WriteBlocks_DMA().
116 This function support only 512-bytes block length (the block size should be
117 chosen as 512 byte).
118 You can choose either one block read operation or multiple block read operation
119 by adjusting the "NumberOfBlocks" parameter.
120 After this, you have to call the function HAL_SD_CheckWriteOperation(), to insure
121 that the write transfer is done correctly in both DMA and SD sides.
122
123 *** SD card status ***
124 ======================
125 [..]
126 (+) At any time, you can check the SD Card status and get the SD card state
127 by using the HAL_SD_GetStatus() function. This function checks first if the
128 SD card is still connected and then get the internal SD Card transfer state.
129 (+) You can also get the SD card SD Status register by using the HAL_SD_SendSDStatus()
130 function.
131
132 *** SD HAL driver macros list ***
133 ==================================
134 [..]
135 Below the list of most used macros in SD HAL driver.
136
137 (+) __HAL_SD_SDMMC_ENABLE : Enable the SD device
138 (+) __HAL_SD_SDMMC_DISABLE : Disable the SD device
139 (+) __HAL_SD_SDMMC_DMA_ENABLE: Enable the SDMMC DMA transfer
140 (+) __HAL_SD_SDMMC_DMA_DISABLE: Disable the SDMMC DMA transfer
141 (+) __HAL_SD_SDMMC_ENABLE_IT: Enable the SD device interrupt
142 (+) __HAL_SD_SDMMC_DISABLE_IT: Disable the SD device interrupt
143 (+) __HAL_SD_SDMMC_GET_FLAG:Check whether the specified SD flag is set or not
144 (+) __HAL_SD_SDMMC_CLEAR_FLAG: Clear the SD's pending flags
145
146 (@) You can refer to the SD HAL driver header file for more useful macros
147
148 @endverbatim
149 ******************************************************************************
150 * @attention
151 *
152 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
153 *
154 * Redistribution and use in source and binary forms, with or without modification,
155 * are permitted provided that the following conditions are met:
156 * 1. Redistributions of source code must retain the above copyright notice,
157 * this list of conditions and the following disclaimer.
158 * 2. Redistributions in binary form must reproduce the above copyright notice,
159 * this list of conditions and the following disclaimer in the documentation
160 * and/or other materials provided with the distribution.
161 * 3. Neither the name of STMicroelectronics nor the names of its contributors
162 * may be used to endorse or promote products derived from this software
163 * without specific prior written permission.
164 *
165 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
166 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
167 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
168 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
169 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
170 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
171 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
172 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
173 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
174 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
175 *
176 ******************************************************************************
177 */
178
179 /* Includes ------------------------------------------------------------------*/
180 #include "stm32f7xx_hal.h"
181
182 /** @addtogroup STM32F7xx_HAL_Driver
183 * @{
184 */
185
186 /** @addtogroup SD
187 * @{
188 */
189
190 #ifdef HAL_SD_MODULE_ENABLED
191
192 /* Private typedef -----------------------------------------------------------*/
193 /* Private define ------------------------------------------------------------*/
194 /** @addtogroup SD_Private_Defines
195 * @{
196 */
197 /**
198 * @brief SDMMC Data block size
199 */
200 #define DATA_BLOCK_SIZE ((uint32_t)(9 << 4))
201 /**
202 * @brief SDMMC Static flags, Timeout, FIFO Address
203 */
204 #define SDMMC_STATIC_FLAGS ((uint32_t)(SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_CTIMEOUT |\
205 SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_TXUNDERR | SDMMC_FLAG_RXOVERR |\
206 SDMMC_FLAG_CMDREND | SDMMC_FLAG_CMDSENT | SDMMC_FLAG_DATAEND |\
207 SDMMC_FLAG_DBCKEND))
208
209 #define SDMMC_CMD0TIMEOUT ((uint32_t)0x00010000U)
210
211 /**
212 * @brief Mask for errors Card Status R1 (OCR Register)
213 */
214 #define SD_OCR_ADDR_OUT_OF_RANGE ((uint32_t)0x80000000U)
215 #define SD_OCR_ADDR_MISALIGNED ((uint32_t)0x40000000U)
216 #define SD_OCR_BLOCK_LEN_ERR ((uint32_t)0x20000000U)
217 #define SD_OCR_ERASE_SEQ_ERR ((uint32_t)0x10000000U)
218 #define SD_OCR_BAD_ERASE_PARAM ((uint32_t)0x08000000U)
219 #define SD_OCR_WRITE_PROT_VIOLATION ((uint32_t)0x04000000U)
220 #define SD_OCR_LOCK_UNLOCK_FAILED ((uint32_t)0x01000000U)
221 #define SD_OCR_COM_CRC_FAILED ((uint32_t)0x00800000U)
222 #define SD_OCR_ILLEGAL_CMD ((uint32_t)0x00400000U)
223 #define SD_OCR_CARD_ECC_FAILED ((uint32_t)0x00200000U)
224 #define SD_OCR_CC_ERROR ((uint32_t)0x00100000U)
225 #define SD_OCR_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00080000U)
226 #define SD_OCR_STREAM_READ_UNDERRUN ((uint32_t)0x00040000U)
227 #define SD_OCR_STREAM_WRITE_OVERRUN ((uint32_t)0x00020000U)
228 #define SD_OCR_CID_CSD_OVERWRITE ((uint32_t)0x00010000U)
229 #define SD_OCR_WP_ERASE_SKIP ((uint32_t)0x00008000U)
230 #define SD_OCR_CARD_ECC_DISABLED ((uint32_t)0x00004000U)
231 #define SD_OCR_ERASE_RESET ((uint32_t)0x00002000U)
232 #define SD_OCR_AKE_SEQ_ERROR ((uint32_t)0x00000008U)
233 #define SD_OCR_ERRORBITS ((uint32_t)0xFDFFE008U)
234
235 /**
236 * @brief Masks for R6 Response
237 */
238 #define SD_R6_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00002000U)
239 #define SD_R6_ILLEGAL_CMD ((uint32_t)0x00004000U)
240 #define SD_R6_COM_CRC_FAILED ((uint32_t)0x00008000U)
241
242 #define SD_VOLTAGE_WINDOW_SD ((uint32_t)0x80100000U)
243 #define SD_HIGH_CAPACITY ((uint32_t)0x40000000U)
244 #define SD_STD_CAPACITY ((uint32_t)0x00000000U)
245 #define SD_CHECK_PATTERN ((uint32_t)0x000001AAU)
246
247 #define SD_MAX_VOLT_TRIAL ((uint32_t)0x0000FFFFU)
248 #define SD_ALLZERO ((uint32_t)0x00000000U)
249
250 #define SD_WIDE_BUS_SUPPORT ((uint32_t)0x00040000U)
251 #define SD_SINGLE_BUS_SUPPORT ((uint32_t)0x00010000U)
252 #define SD_CARD_LOCKED ((uint32_t)0x02000000U)
253
254 #define SD_DATATIMEOUT ((uint32_t)0xFFFFFFFFU)
255 #define SD_0TO7BITS ((uint32_t)0x000000FFU)
256 #define SD_8TO15BITS ((uint32_t)0x0000FF00U)
257 #define SD_16TO23BITS ((uint32_t)0x00FF0000U)
258 #define SD_24TO31BITS ((uint32_t)0xFF000000U)
259 #define SD_MAX_DATA_LENGTH ((uint32_t)0x01FFFFFFU)
260
261 #define SD_HALFFIFO ((uint32_t)0x00000008U)
262 #define SD_HALFFIFOBYTES ((uint32_t)0x00000020U)
263
264 /**
265 * @brief Command Class Supported
266 */
267 #define SD_CCCC_LOCK_UNLOCK ((uint32_t)0x00000080U)
268 #define SD_CCCC_WRITE_PROT ((uint32_t)0x00000040U)
269 #define SD_CCCC_ERASE ((uint32_t)0x00000020U)
270
271 /**
272 * @brief Following commands are SD Card Specific commands.
273 * SDMMC_APP_CMD should be sent before sending these commands.
274 */
275 #define SD_SDMMC_SEND_IF_COND ((uint32_t)SD_CMD_HS_SEND_EXT_CSD)
276 /**
277 * @}
278 */
279
280 /* Private macro -------------------------------------------------------------*/
281 /* Private variables ---------------------------------------------------------*/
282 /* Private function prototypes -----------------------------------------------*/
283 /* Private functions ---------------------------------------------------------*/
284 /** @defgroup SD_Private_Functions SD Private Functions
285 * @{
286 */
287 static HAL_SD_ErrorTypedef SD_Initialize_Cards(SD_HandleTypeDef *hsd);
288 static HAL_SD_ErrorTypedef SD_Select_Deselect(SD_HandleTypeDef *hsd, uint64_t addr);
289 static HAL_SD_ErrorTypedef SD_PowerON(SD_HandleTypeDef *hsd);
290 static HAL_SD_ErrorTypedef SD_PowerOFF(SD_HandleTypeDef *hsd);
291 static HAL_SD_ErrorTypedef SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus);
292 static HAL_SD_CardStateTypedef SD_GetState(SD_HandleTypeDef *hsd);
293 static HAL_SD_ErrorTypedef SD_IsCardProgramming(SD_HandleTypeDef *hsd, uint8_t *pStatus);
294 static HAL_SD_ErrorTypedef SD_CmdError(SD_HandleTypeDef *hsd);
295 static HAL_SD_ErrorTypedef SD_CmdResp1Error(SD_HandleTypeDef *hsd, uint8_t SD_CMD);
296 static HAL_SD_ErrorTypedef SD_CmdResp7Error(SD_HandleTypeDef *hsd);
297 static HAL_SD_ErrorTypedef SD_CmdResp3Error(SD_HandleTypeDef *hsd);
298 static HAL_SD_ErrorTypedef SD_CmdResp2Error(SD_HandleTypeDef *hsd);
299 static HAL_SD_ErrorTypedef SD_CmdResp6Error(SD_HandleTypeDef *hsd, uint8_t SD_CMD, uint16_t *pRCA);
300 static HAL_SD_ErrorTypedef SD_WideBus_Enable(SD_HandleTypeDef *hsd);
301 static HAL_SD_ErrorTypedef SD_WideBus_Disable(SD_HandleTypeDef *hsd);
302 static HAL_SD_ErrorTypedef SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR);
303 static void SD_DMA_RxCplt(DMA_HandleTypeDef *hdma);
304 static void SD_DMA_RxError(DMA_HandleTypeDef *hdma);
305 static void SD_DMA_TxCplt(DMA_HandleTypeDef *hdma);
306 static void SD_DMA_TxError(DMA_HandleTypeDef *hdma);
307 /**
308 * @}
309 */
310 /* Exported functions --------------------------------------------------------*/
311 /** @addtogroup SD_Exported_Functions
312 * @{
313 */
314
315 /** @addtogroup SD_Exported_Functions_Group1
316 * @brief Initialization and de-initialization functions
317 *
318 @verbatim
319 ==============================================================================
320 ##### Initialization and de-initialization functions #####
321 ==============================================================================
322 [..]
323 This section provides functions allowing to initialize/de-initialize the SD
324 card device to be ready for use.
325
326
327 @endverbatim
328 * @{
329 */
330
331 /**
332 * @brief Initializes the SD card according to the specified parameters in the
333 SD_HandleTypeDef and create the associated handle.
334 * @param hsd: SD handle
335 * @param SDCardInfo: HAL_SD_CardInfoTypedef structure for SD card information
336 * @retval HAL SD error state
337 */
338 HAL_SD_ErrorTypedef HAL_SD_Init(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypedef *SDCardInfo)
339 {
340 __IO HAL_SD_ErrorTypedef errorstate = SD_OK;
341 SD_InitTypeDef tmpinit;
342
343 /* Allocate lock resource and initialize it */
344 hsd->Lock = HAL_UNLOCKED;
345
346 /* Initialize the low level hardware (MSP) */
347 HAL_SD_MspInit(hsd);
348
349 /* Default SDMMC peripheral configuration for SD card initialization */
350 tmpinit.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
351 tmpinit.ClockBypass = SDMMC_CLOCK_BYPASS_DISABLE;
352 tmpinit.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
353 tmpinit.BusWide = SDMMC_BUS_WIDE_1B;
354 tmpinit.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE;
355 tmpinit.ClockDiv = SDMMC_INIT_CLK_DIV;
356
357 /* Initialize SDMMC peripheral interface with default configuration */
358 SDMMC_Init(hsd->Instance, tmpinit);
359
360 /* Identify card operating voltage */
361 errorstate = SD_PowerON(hsd);
362
363 if(errorstate != SD_OK)
364 {
365 return errorstate;
366 }
367
368 /* Initialize the present SDMMC card(s) and put them in idle state */
369 errorstate = SD_Initialize_Cards(hsd);
370
371 if (errorstate != SD_OK)
372 {
373 return errorstate;
374 }
375
376 /* Read CSD/CID MSD registers */
377 errorstate = HAL_SD_Get_CardInfo(hsd, SDCardInfo);
378
379 if (errorstate == SD_OK)
380 {
381 /* Select the Card */
382 errorstate = SD_Select_Deselect(hsd, (uint32_t)(((uint32_t)SDCardInfo->RCA) << 16));
383 }
384
385 /* Configure SDMMC peripheral interface */
386 SDMMC_Init(hsd->Instance, hsd->Init);
387
388 return errorstate;
389 }
390
391 /**
392 * @brief De-Initializes the SD card.
393 * @param hsd: SD handle
394 * @retval HAL status
395 */
396 HAL_StatusTypeDef HAL_SD_DeInit(SD_HandleTypeDef *hsd)
397 {
398
399 /* Set SD power state to off */
400 SD_PowerOFF(hsd);
401
402 /* De-Initialize the MSP layer */
403 HAL_SD_MspDeInit(hsd);
404
405 return HAL_OK;
406 }
407
408
409 /**
410 * @brief Initializes the SD MSP.
411 * @param hsd: SD handle
412 * @retval None
413 */
414 __weak void HAL_SD_MspInit(SD_HandleTypeDef *hsd)
415 {
416 /* Prevent unused argument(s) compilation warning */
417 UNUSED(hsd);
418
419 /* NOTE : This function Should not be modified, when the callback is needed,
420 the HAL_SD_MspInit could be implemented in the user file
421 */
422 }
423
424 /**
425 * @brief De-Initialize SD MSP.
426 * @param hsd: SD handle
427 * @retval None
428 */
429 __weak void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd)
430 {
431 /* Prevent unused argument(s) compilation warning */
432 UNUSED(hsd);
433
434 /* NOTE : This function Should not be modified, when the callback is needed,
435 the HAL_SD_MspDeInit could be implemented in the user file
436 */
437 }
438
439 /**
440 * @}
441 */
442
443 /** @addtogroup SD_Exported_Functions_Group2
444 * @brief Data transfer functions
445 *
446 @verbatim
447 ==============================================================================
448 ##### IO operation functions #####
449 ==============================================================================
450 [..]
451 This subsection provides a set of functions allowing to manage the data
452 transfer from/to SD card.
453
454 @endverbatim
455 * @{
456 */
457
458 /**
459 * @brief Reads block(s) from a specified address in a card. The Data transfer
460 * is managed by polling mode.
461 * @param hsd: SD handle
462 * @param pReadBuffer: pointer to the buffer that will contain the received data
463 * @param ReadAddr: Address from where data is to be read
464 * @param BlockSize: SD card Data block size
465 * @note BlockSize must be 512 bytes.
466 * @param NumberOfBlocks: Number of SD blocks to read
467 * @retval SD Card error state
468 */
469 HAL_SD_ErrorTypedef HAL_SD_ReadBlocks(SD_HandleTypeDef *hsd, uint32_t *pReadBuffer, uint64_t ReadAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
470 {
471 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure;
472 SDMMC_DataInitTypeDef sdmmc_datainitstructure;
473 HAL_SD_ErrorTypedef errorstate = SD_OK;
474 uint32_t count = 0, *tempbuff = (uint32_t *)pReadBuffer;
475
476 /* Initialize data control register */
477 hsd->Instance->DCTRL = 0;
478
479 if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
480 {
481 BlockSize = 512;
482 ReadAddr /= 512;
483 }
484
485 /* Set Block Size for Card */
486 sdmmc_cmdinitstructure.Argument = (uint32_t) BlockSize;
487 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
488 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
489 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
490 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
491 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
492
493 /* Check for error conditions */
494 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
495
496 if (errorstate != SD_OK)
497 {
498 return errorstate;
499 }
500
501 /* Configure the SD DPSM (Data Path State Machine) */
502 sdmmc_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
503 sdmmc_datainitstructure.DataLength = NumberOfBlocks * BlockSize;
504 sdmmc_datainitstructure.DataBlockSize = DATA_BLOCK_SIZE;
505 sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
506 sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
507 sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE;
508 SDMMC_DataConfig(hsd->Instance, &sdmmc_datainitstructure);
509
510 if(NumberOfBlocks > 1)
511 {
512 /* Send CMD18 READ_MULT_BLOCK with argument data address */
513 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_READ_MULT_BLOCK;
514 }
515 else
516 {
517 /* Send CMD17 READ_SINGLE_BLOCK */
518 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_READ_SINGLE_BLOCK;
519 }
520
521 sdmmc_cmdinitstructure.Argument = (uint32_t)ReadAddr;
522 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
523
524 /* Read block(s) in polling mode */
525 if(NumberOfBlocks > 1)
526 {
527 /* Check for error conditions */
528 errorstate = SD_CmdResp1Error(hsd, SD_CMD_READ_MULT_BLOCK);
529
530 if (errorstate != SD_OK)
531 {
532 return errorstate;
533 }
534
535 /* Poll on SDMMC flags */
536 while(!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
537 {
538 if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
539 {
540 /* Read data from SDMMC Rx FIFO */
541 for (count = 0; count < 8; count++)
542 {
543 *(tempbuff + count) = SDMMC_ReadFIFO(hsd->Instance);
544 }
545
546 tempbuff += 8;
547 }
548 }
549 }
550 else
551 {
552 /* Check for error conditions */
553 errorstate = SD_CmdResp1Error(hsd, SD_CMD_READ_SINGLE_BLOCK);
554
555 if (errorstate != SD_OK)
556 {
557 return errorstate;
558 }
559
560 /* In case of single block transfer, no need of stop transfer at all */
561 while(!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND))
562 {
563 if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
564 {
565 /* Read data from SDMMC Rx FIFO */
566 for (count = 0; count < 8; count++)
567 {
568 *(tempbuff + count) = SDMMC_ReadFIFO(hsd->Instance);
569 }
570
571 tempbuff += 8;
572 }
573 }
574 }
575
576 /* Send stop transmission command in case of multiblock read */
577 if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1))
578 {
579 if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) ||\
580 (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\
581 (hsd->CardType == HIGH_CAPACITY_SD_CARD))
582 {
583 /* Send stop transmission command */
584 errorstate = HAL_SD_StopTransfer(hsd);
585 }
586 }
587
588 /* Get error state */
589 if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
590 {
591 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
592
593 errorstate = SD_DATA_TIMEOUT;
594
595 return errorstate;
596 }
597 else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
598 {
599 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
600
601 errorstate = SD_DATA_CRC_FAIL;
602
603 return errorstate;
604 }
605 else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
606 {
607 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
608
609 errorstate = SD_RX_OVERRUN;
610
611 return errorstate;
612 }
613 else
614 {
615 /* No error flag set */
616 }
617
618 count = SD_DATATIMEOUT;
619
620 /* Empty FIFO if there is still any data */
621 while ((__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL)) && (count > 0))
622 {
623 *tempbuff = SDMMC_ReadFIFO(hsd->Instance);
624 tempbuff++;
625 count--;
626 }
627
628 /* Clear all the static flags */
629 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
630
631 return errorstate;
632 }
633
634 /**
635 * @brief Allows to write block(s) to a specified address in a card. The Data
636 * transfer is managed by polling mode.
637 * @param hsd: SD handle
638 * @param pWriteBuffer: pointer to the buffer that will contain the data to transmit
639 * @param WriteAddr: Address from where data is to be written
640 * @param BlockSize: SD card Data block size
641 * @note BlockSize must be 512 bytes.
642 * @param NumberOfBlocks: Number of SD blocks to write
643 * @retval SD Card error state
644 */
645 HAL_SD_ErrorTypedef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, uint32_t *pWriteBuffer, uint64_t WriteAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
646 {
647 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure;
648 SDMMC_DataInitTypeDef sdmmc_datainitstructure;
649 HAL_SD_ErrorTypedef errorstate = SD_OK;
650 uint32_t totalnumberofbytes = 0, bytestransferred = 0, count = 0, restwords = 0;
651 uint32_t *tempbuff = (uint32_t *)pWriteBuffer;
652 uint8_t cardstate = 0;
653
654 /* Initialize data control register */
655 hsd->Instance->DCTRL = 0;
656
657 if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
658 {
659 BlockSize = 512;
660 WriteAddr /= 512;
661 }
662
663 /* Set Block Size for Card */
664 sdmmc_cmdinitstructure.Argument = (uint32_t)BlockSize;
665 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
666 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
667 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
668 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
669 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
670
671 /* Check for error conditions */
672 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
673
674 if (errorstate != SD_OK)
675 {
676 return errorstate;
677 }
678
679 if(NumberOfBlocks > 1)
680 {
681 /* Send CMD25 WRITE_MULT_BLOCK with argument data address */
682 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_WRITE_MULT_BLOCK;
683 }
684 else
685 {
686 /* Send CMD24 WRITE_SINGLE_BLOCK */
687 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_WRITE_SINGLE_BLOCK;
688 }
689
690 sdmmc_cmdinitstructure.Argument = (uint32_t)WriteAddr;
691 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
692
693 /* Check for error conditions */
694 if(NumberOfBlocks > 1)
695 {
696 errorstate = SD_CmdResp1Error(hsd, SD_CMD_WRITE_MULT_BLOCK);
697 }
698 else
699 {
700 errorstate = SD_CmdResp1Error(hsd, SD_CMD_WRITE_SINGLE_BLOCK);
701 }
702
703 if (errorstate != SD_OK)
704 {
705 return errorstate;
706 }
707
708 /* Set total number of bytes to write */
709 totalnumberofbytes = NumberOfBlocks * BlockSize;
710
711 /* Configure the SD DPSM (Data Path State Machine) */
712 sdmmc_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
713 sdmmc_datainitstructure.DataLength = NumberOfBlocks * BlockSize;
714 sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
715 sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
716 sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
717 sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE;
718 SDMMC_DataConfig(hsd->Instance, &sdmmc_datainitstructure);
719
720 /* Write block(s) in polling mode */
721 if(NumberOfBlocks > 1)
722 {
723 while(!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
724 {
725 if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_TXFIFOHE))
726 {
727 if ((totalnumberofbytes - bytestransferred) < 32)
728 {
729 restwords = ((totalnumberofbytes - bytestransferred) % 4 == 0) ? ((totalnumberofbytes - bytestransferred) / 4) : (( totalnumberofbytes - bytestransferred) / 4 + 1);
730
731 /* Write data to SDMMC Tx FIFO */
732 for (count = 0; count < restwords; count++)
733 {
734 SDMMC_WriteFIFO(hsd->Instance, tempbuff);
735 tempbuff++;
736 bytestransferred += 4;
737 }
738 }
739 else
740 {
741 /* Write data to SDMMC Tx FIFO */
742 for (count = 0; count < 8; count++)
743 {
744 SDMMC_WriteFIFO(hsd->Instance, (tempbuff + count));
745 }
746
747 tempbuff += 8;
748 bytestransferred += 32;
749 }
750 }
751 }
752 }
753 else
754 {
755 /* In case of single data block transfer no need of stop command at all */
756 while(!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND))
757 {
758 if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_TXFIFOHE))
759 {
760 if ((totalnumberofbytes - bytestransferred) < 32)
761 {
762 restwords = ((totalnumberofbytes - bytestransferred) % 4 == 0) ? ((totalnumberofbytes - bytestransferred) / 4) : (( totalnumberofbytes - bytestransferred) / 4 + 1);
763
764 /* Write data to SDMMC Tx FIFO */
765 for (count = 0; count < restwords; count++)
766 {
767 SDMMC_WriteFIFO(hsd->Instance, tempbuff);
768 tempbuff++;
769 bytestransferred += 4;
770 }
771 }
772 else
773 {
774 /* Write data to SDMMC Tx FIFO */
775 for (count = 0; count < 8; count++)
776 {
777 SDMMC_WriteFIFO(hsd->Instance, (tempbuff + count));
778 }
779
780 tempbuff += 8;
781 bytestransferred += 32;
782 }
783 }
784 }
785 }
786
787 /* Send stop transmission command in case of multiblock write */
788 if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1))
789 {
790 if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\
791 (hsd->CardType == HIGH_CAPACITY_SD_CARD))
792 {
793 /* Send stop transmission command */
794 errorstate = HAL_SD_StopTransfer(hsd);
795 }
796 }
797
798 /* Get error state */
799 if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
800 {
801 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
802
803 errorstate = SD_DATA_TIMEOUT;
804
805 return errorstate;
806 }
807 else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
808 {
809 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
810
811 errorstate = SD_DATA_CRC_FAIL;
812
813 return errorstate;
814 }
815 else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR))
816 {
817 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_TXUNDERR);
818
819 errorstate = SD_TX_UNDERRUN;
820
821 return errorstate;
822 }
823 else
824 {
825 /* No error flag set */
826 }
827
828 /* Clear all the static flags */
829 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
830
831 /* Wait till the card is in programming state */
832 errorstate = SD_IsCardProgramming(hsd, &cardstate);
833
834 while ((errorstate == SD_OK) && ((cardstate == SD_CARD_PROGRAMMING) || (cardstate == SD_CARD_RECEIVING)))
835 {
836 errorstate = SD_IsCardProgramming(hsd, &cardstate);
837 }
838
839 return errorstate;
840 }
841
842 /**
843 * @brief Reads block(s) from a specified address in a card. The Data transfer
844 * is managed by DMA mode.
845 * @note This API should be followed by the function HAL_SD_CheckReadOperation()
846 * to check the completion of the read process
847 * @param hsd: SD handle
848 * @param pReadBuffer: Pointer to the buffer that will contain the received data
849 * @param ReadAddr: Address from where data is to be read
850 * @param BlockSize: SD card Data block size
851 * @note BlockSize must be 512 bytes.
852 * @param NumberOfBlocks: Number of blocks to read.
853 * @retval SD Card error state
854 */
855 HAL_SD_ErrorTypedef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint32_t *pReadBuffer, uint64_t ReadAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
856 {
857 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure;
858 SDMMC_DataInitTypeDef sdmmc_datainitstructure;
859 HAL_SD_ErrorTypedef errorstate = SD_OK;
860
861 /* Initialize data control register */
862 hsd->Instance->DCTRL = 0;
863
864 /* Initialize handle flags */
865 hsd->SdTransferCplt = 0;
866 hsd->DmaTransferCplt = 0;
867 hsd->SdTransferErr = SD_OK;
868
869 /* Initialize SD Read operation */
870 if(NumberOfBlocks > 1)
871 {
872 hsd->SdOperation = SD_READ_MULTIPLE_BLOCK;
873 }
874 else
875 {
876 hsd->SdOperation = SD_READ_SINGLE_BLOCK;
877 }
878
879 /* Enable transfer interrupts */
880 __HAL_SD_SDMMC_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL |\
881 SDMMC_IT_DTIMEOUT |\
882 SDMMC_IT_DATAEND |\
883 SDMMC_IT_RXOVERR));
884
885 /* Enable SDMMC DMA transfer */
886 __HAL_SD_SDMMC_DMA_ENABLE(hsd);
887
888 /* Configure DMA user callbacks */
889 hsd->hdmarx->XferCpltCallback = SD_DMA_RxCplt;
890 hsd->hdmarx->XferErrorCallback = SD_DMA_RxError;
891
892 /* Enable the DMA Channel */
893 HAL_DMA_Start_IT(hsd->hdmarx, (uint32_t)&hsd->Instance->FIFO, (uint32_t)pReadBuffer, (uint32_t)(BlockSize * NumberOfBlocks)/4);
894
895 if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
896 {
897 BlockSize = 512;
898 ReadAddr /= 512;
899 }
900
901 /* Set Block Size for Card */
902 sdmmc_cmdinitstructure.Argument = (uint32_t)BlockSize;
903 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
904 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
905 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
906 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
907 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
908
909 /* Check for error conditions */
910 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
911
912 if (errorstate != SD_OK)
913 {
914 return errorstate;
915 }
916
917 /* Configure the SD DPSM (Data Path State Machine) */
918 sdmmc_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
919 sdmmc_datainitstructure.DataLength = BlockSize * NumberOfBlocks;
920 sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
921 sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
922 sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
923 sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE;
924 SDMMC_DataConfig(hsd->Instance, &sdmmc_datainitstructure);
925
926 /* Check number of blocks command */
927 if(NumberOfBlocks > 1)
928 {
929 /* Send CMD18 READ_MULT_BLOCK with argument data address */
930 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_READ_MULT_BLOCK;
931 }
932 else
933 {
934 /* Send CMD17 READ_SINGLE_BLOCK */
935 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_READ_SINGLE_BLOCK;
936 }
937
938 sdmmc_cmdinitstructure.Argument = (uint32_t)ReadAddr;
939 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
940
941 /* Check for error conditions */
942 if(NumberOfBlocks > 1)
943 {
944 errorstate = SD_CmdResp1Error(hsd, SD_CMD_READ_MULT_BLOCK);
945 }
946 else
947 {
948 errorstate = SD_CmdResp1Error(hsd, SD_CMD_READ_SINGLE_BLOCK);
949 }
950
951 /* Update the SD transfer error in SD handle */
952 hsd->SdTransferErr = errorstate;
953
954 return errorstate;
955 }
956
957
958 /**
959 * @brief Writes block(s) to a specified address in a card. The Data transfer
960 * is managed by DMA mode.
961 * @note This API should be followed by the function HAL_SD_CheckWriteOperation()
962 * to check the completion of the write process (by SD current status polling).
963 * @param hsd: SD handle
964 * @param pWriteBuffer: pointer to the buffer that will contain the data to transmit
965 * @param WriteAddr: Address from where data is to be read
966 * @param BlockSize: the SD card Data block size
967 * @note BlockSize must be 512 bytes.
968 * @param NumberOfBlocks: Number of blocks to write
969 * @retval SD Card error state
970 */
971 HAL_SD_ErrorTypedef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint32_t *pWriteBuffer, uint64_t WriteAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
972 {
973 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure;
974 SDMMC_DataInitTypeDef sdmmc_datainitstructure;
975 HAL_SD_ErrorTypedef errorstate = SD_OK;
976
977 /* Initialize data control register */
978 hsd->Instance->DCTRL = 0;
979
980 /* Initialize handle flags */
981 hsd->SdTransferCplt = 0;
982 hsd->DmaTransferCplt = 0;
983 hsd->SdTransferErr = SD_OK;
984
985 /* Initialize SD Write operation */
986 if(NumberOfBlocks > 1)
987 {
988 hsd->SdOperation = SD_WRITE_MULTIPLE_BLOCK;
989 }
990 else
991 {
992 hsd->SdOperation = SD_WRITE_SINGLE_BLOCK;
993 }
994
995 /* Enable transfer interrupts */
996 __HAL_SD_SDMMC_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL |\
997 SDMMC_IT_DTIMEOUT |\
998 SDMMC_IT_DATAEND |\
999 SDMMC_IT_TXUNDERR));
1000
1001 /* Configure DMA user callbacks */
1002 hsd->hdmatx->XferCpltCallback = SD_DMA_TxCplt;
1003 hsd->hdmatx->XferErrorCallback = SD_DMA_TxError;
1004
1005 /* Enable the DMA Channel */
1006 HAL_DMA_Start_IT(hsd->hdmatx, (uint32_t)pWriteBuffer, (uint32_t)&hsd->Instance->FIFO, (uint32_t)(BlockSize * NumberOfBlocks)/4);
1007
1008 /* Enable SDMMC DMA transfer */
1009 __HAL_SD_SDMMC_DMA_ENABLE(hsd);
1010
1011 if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
1012 {
1013 BlockSize = 512;
1014 WriteAddr /= 512;
1015 }
1016
1017 /* Set Block Size for Card */
1018 sdmmc_cmdinitstructure.Argument = (uint32_t)BlockSize;
1019 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
1020 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
1021 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
1022 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
1023 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
1024
1025 /* Check for error conditions */
1026 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
1027
1028 if (errorstate != SD_OK)
1029 {
1030 return errorstate;
1031 }
1032
1033 /* Check number of blocks command */
1034 if(NumberOfBlocks <= 1)
1035 {
1036 /* Send CMD24 WRITE_SINGLE_BLOCK */
1037 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_WRITE_SINGLE_BLOCK;
1038 }
1039 else
1040 {
1041 /* Send CMD25 WRITE_MULT_BLOCK with argument data address */
1042 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_WRITE_MULT_BLOCK;
1043 }
1044
1045 sdmmc_cmdinitstructure.Argument = (uint32_t)WriteAddr;
1046 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
1047
1048 /* Check for error conditions */
1049 if(NumberOfBlocks > 1)
1050 {
1051 errorstate = SD_CmdResp1Error(hsd, SD_CMD_WRITE_MULT_BLOCK);
1052 }
1053 else
1054 {
1055 errorstate = SD_CmdResp1Error(hsd, SD_CMD_WRITE_SINGLE_BLOCK);
1056 }
1057
1058 if (errorstate != SD_OK)
1059 {
1060 return errorstate;
1061 }
1062
1063 /* Configure the SD DPSM (Data Path State Machine) */
1064 sdmmc_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
1065 sdmmc_datainitstructure.DataLength = BlockSize * NumberOfBlocks;
1066 sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1067 sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
1068 sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1069 sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE;
1070 SDMMC_DataConfig(hsd->Instance, &sdmmc_datainitstructure);
1071
1072 hsd->SdTransferErr = errorstate;
1073
1074 return errorstate;
1075 }
1076
1077 /**
1078 * @brief This function waits until the SD DMA data read transfer is finished.
1079 * This API should be called after HAL_SD_ReadBlocks_DMA() function
1080 * to insure that all data sent by the card is already transferred by the
1081 * DMA controller.
1082 * @param hsd: SD handle
1083 * @param Timeout: Timeout duration
1084 * @retval SD Card error state
1085 */
1086 HAL_SD_ErrorTypedef HAL_SD_CheckReadOperation(SD_HandleTypeDef *hsd, uint32_t Timeout)
1087 {
1088 HAL_SD_ErrorTypedef errorstate = SD_OK;
1089 uint32_t timeout = Timeout;
1090 uint32_t tmp1, tmp2;
1091 HAL_SD_ErrorTypedef tmp3;
1092
1093 /* Wait for DMA/SD transfer end or SD error variables to be in SD handle */
1094 tmp1 = hsd->DmaTransferCplt;
1095 tmp2 = hsd->SdTransferCplt;
1096 tmp3 = (HAL_SD_ErrorTypedef)hsd->SdTransferErr;
1097
1098 while (((tmp1 & tmp2) == 0) && (tmp3 == SD_OK) && (timeout > 0))
1099 {
1100 tmp1 = hsd->DmaTransferCplt;
1101 tmp2 = hsd->SdTransferCplt;
1102 tmp3 = (HAL_SD_ErrorTypedef)hsd->SdTransferErr;
1103 timeout--;
1104 }
1105
1106 timeout = Timeout;
1107
1108 /* Wait until the Rx transfer is no longer active */
1109 while((__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXACT)) && (timeout > 0))
1110 {
1111 timeout--;
1112 }
1113
1114 /* Send stop command in multiblock read */
1115 if (hsd->SdOperation == SD_READ_MULTIPLE_BLOCK)
1116 {
1117 errorstate = HAL_SD_StopTransfer(hsd);
1118 }
1119
1120 if ((timeout == 0) && (errorstate == SD_OK))
1121 {
1122 errorstate = SD_DATA_TIMEOUT;
1123 }
1124
1125 /* Clear all the static flags */
1126 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1127
1128 /* Return error state */
1129 if (hsd->SdTransferErr != SD_OK)
1130 {
1131 return (HAL_SD_ErrorTypedef)(hsd->SdTransferErr);
1132 }
1133
1134 return errorstate;
1135 }
1136
1137 /**
1138 * @brief This function waits until the SD DMA data write transfer is finished.
1139 * This API should be called after HAL_SD_WriteBlocks_DMA() function
1140 * to insure that all data sent by the card is already transferred by the
1141 * DMA controller.
1142 * @param hsd: SD handle
1143 * @param Timeout: Timeout duration
1144 * @retval SD Card error state
1145 */
1146 HAL_SD_ErrorTypedef HAL_SD_CheckWriteOperation(SD_HandleTypeDef *hsd, uint32_t Timeout)
1147 {
1148 HAL_SD_ErrorTypedef errorstate = SD_OK;
1149 uint32_t timeout = Timeout;
1150 uint32_t tmp1, tmp2;
1151 HAL_SD_ErrorTypedef tmp3;
1152
1153 /* Wait for DMA/SD transfer end or SD error variables to be in SD handle */
1154 tmp1 = hsd->DmaTransferCplt;
1155 tmp2 = hsd->SdTransferCplt;
1156 tmp3 = (HAL_SD_ErrorTypedef)hsd->SdTransferErr;
1157
1158 while (((tmp1 & tmp2) == 0) && (tmp3 == SD_OK) && (timeout > 0))
1159 {
1160 tmp1 = hsd->DmaTransferCplt;
1161 tmp2 = hsd->SdTransferCplt;
1162 tmp3 = (HAL_SD_ErrorTypedef)hsd->SdTransferErr;
1163 timeout--;
1164 }
1165
1166 timeout = Timeout;
1167
1168 /* Wait until the Tx transfer is no longer active */
1169 while((__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_TXACT)) && (timeout > 0))
1170 {
1171 timeout--;
1172 }
1173
1174 /* Send stop command in multiblock write */
1175 if (hsd->SdOperation == SD_WRITE_MULTIPLE_BLOCK)
1176 {
1177 errorstate = HAL_SD_StopTransfer(hsd);
1178 }
1179
1180 if ((timeout == 0) && (errorstate == SD_OK))
1181 {
1182 errorstate = SD_DATA_TIMEOUT;
1183 }
1184
1185 /* Clear all the static flags */
1186 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1187
1188 /* Return error state */
1189 if (hsd->SdTransferErr != SD_OK)
1190 {
1191 return (HAL_SD_ErrorTypedef)(hsd->SdTransferErr);
1192 }
1193
1194 /* Wait until write is complete */
1195 while(HAL_SD_GetStatus(hsd) != SD_TRANSFER_OK)
1196 {
1197 }
1198
1199 return errorstate;
1200 }
1201
1202 /**
1203 * @brief Erases the specified memory area of the given SD card.
1204 * @param hsd: SD handle
1205 * @param startaddr: Start byte address
1206 * @param endaddr: End byte address
1207 * @retval SD Card error state
1208 */
1209 HAL_SD_ErrorTypedef HAL_SD_Erase(SD_HandleTypeDef *hsd, uint64_t startaddr, uint64_t endaddr)
1210 {
1211 HAL_SD_ErrorTypedef errorstate = SD_OK;
1212 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure;
1213
1214 uint32_t delay = 0;
1215 __IO uint32_t maxdelay = 0;
1216 uint8_t cardstate = 0;
1217
1218 /* Check if the card command class supports erase command */
1219 if (((hsd->CSD[1] >> 20) & SD_CCCC_ERASE) == 0)
1220 {
1221 errorstate = SD_REQUEST_NOT_APPLICABLE;
1222
1223 return errorstate;
1224 }
1225
1226 /* Get max delay value */
1227 maxdelay = 120000 / (((hsd->Instance->CLKCR) & 0xFF) + 2);
1228
1229 if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SD_CARD_LOCKED) == SD_CARD_LOCKED)
1230 {
1231 errorstate = SD_LOCK_UNLOCK_FAILED;
1232
1233 return errorstate;
1234 }
1235
1236 /* Get start and end block for high capacity cards */
1237 if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
1238 {
1239 startaddr /= 512;
1240 endaddr /= 512;
1241 }
1242
1243 /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
1244 if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\
1245 (hsd->CardType == HIGH_CAPACITY_SD_CARD))
1246 {
1247 /* Send CMD32 SD_ERASE_GRP_START with argument as addr */
1248 sdmmc_cmdinitstructure.Argument =(uint32_t)startaddr;
1249 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SD_ERASE_GRP_START;
1250 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
1251 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
1252 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
1253 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
1254
1255 /* Check for error conditions */
1256 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SD_ERASE_GRP_START);
1257
1258 if (errorstate != SD_OK)
1259 {
1260 return errorstate;
1261 }
1262
1263 /* Send CMD33 SD_ERASE_GRP_END with argument as addr */
1264 sdmmc_cmdinitstructure.Argument = (uint32_t)endaddr;
1265 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SD_ERASE_GRP_END;
1266 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
1267
1268 /* Check for error conditions */
1269 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SD_ERASE_GRP_END);
1270
1271 if (errorstate != SD_OK)
1272 {
1273 return errorstate;
1274 }
1275 }
1276
1277 /* Send CMD38 ERASE */
1278 sdmmc_cmdinitstructure.Argument = 0;
1279 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_ERASE;
1280 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
1281
1282 /* Check for error conditions */
1283 errorstate = SD_CmdResp1Error(hsd, SD_CMD_ERASE);
1284
1285 if (errorstate != SD_OK)
1286 {
1287 return errorstate;
1288 }
1289
1290 for (; delay < maxdelay; delay++)
1291 {
1292 }
1293
1294 /* Wait until the card is in programming state */
1295 errorstate = SD_IsCardProgramming(hsd, &cardstate);
1296
1297 delay = SD_DATATIMEOUT;
1298
1299 while ((delay > 0) && (errorstate == SD_OK) && ((cardstate == SD_CARD_PROGRAMMING) || (cardstate == SD_CARD_RECEIVING)))
1300 {
1301 errorstate = SD_IsCardProgramming(hsd, &cardstate);
1302 delay--;
1303 }
1304
1305 return errorstate;
1306 }
1307
1308 /**
1309 * @brief This function handles SD card interrupt request.
1310 * @param hsd: SD handle
1311 * @retval None
1312 */
1313 void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd)
1314 {
1315 /* Check for SDMMC interrupt flags */
1316 if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_IT_DATAEND))
1317 {
1318 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_IT_DATAEND);
1319
1320 /* SD transfer is complete */
1321 hsd->SdTransferCplt = 1;
1322
1323 /* No transfer error */
1324 hsd->SdTransferErr = SD_OK;
1325
1326 HAL_SD_XferCpltCallback(hsd);
1327 }
1328 else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_IT_DCRCFAIL))
1329 {
1330 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
1331
1332 hsd->SdTransferErr = SD_DATA_CRC_FAIL;
1333
1334 HAL_SD_XferErrorCallback(hsd);
1335
1336 }
1337 else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_IT_DTIMEOUT))
1338 {
1339 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
1340
1341 hsd->SdTransferErr = SD_DATA_TIMEOUT;
1342
1343 HAL_SD_XferErrorCallback(hsd);
1344 }
1345 else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_IT_RXOVERR))
1346 {
1347 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
1348
1349 hsd->SdTransferErr = SD_RX_OVERRUN;
1350
1351 HAL_SD_XferErrorCallback(hsd);
1352 }
1353 else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_IT_TXUNDERR))
1354 {
1355 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_TXUNDERR);
1356
1357 hsd->SdTransferErr = SD_TX_UNDERRUN;
1358
1359 HAL_SD_XferErrorCallback(hsd);
1360 }
1361 else
1362 {
1363 /* No error flag set */
1364 }
1365
1366 /* Disable all SDMMC peripheral interrupt sources */
1367 __HAL_SD_SDMMC_DISABLE_IT(hsd, SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_DATAEND |\
1368 SDMMC_IT_TXFIFOHE | SDMMC_IT_RXFIFOHF | SDMMC_IT_TXUNDERR |\
1369 SDMMC_IT_RXOVERR);
1370 }
1371
1372
1373 /**
1374 * @brief SD end of transfer callback.
1375 * @param hsd: SD handle
1376 * @retval None
1377 */
1378 __weak void HAL_SD_XferCpltCallback(SD_HandleTypeDef *hsd)
1379 {
1380 /* Prevent unused argument(s) compilation warning */
1381 UNUSED(hsd);
1382
1383 /* NOTE : This function Should not be modified, when the callback is needed,
1384 the HAL_SD_XferCpltCallback could be implemented in the user file
1385 */
1386 }
1387
1388 /**
1389 * @brief SD Transfer Error callback.
1390 * @param hsd: SD handle
1391 * @retval None
1392 */
1393 __weak void HAL_SD_XferErrorCallback(SD_HandleTypeDef *hsd)
1394 {
1395 /* Prevent unused argument(s) compilation warning */
1396 UNUSED(hsd);
1397
1398 /* NOTE : This function Should not be modified, when the callback is needed,
1399 the HAL_SD_XferErrorCallback could be implemented in the user file
1400 */
1401 }
1402
1403 /**
1404 * @brief SD Transfer complete Rx callback in non blocking mode.
1405 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
1406 * the configuration information for the specified DMA module.
1407 * @retval None
1408 */
1409 __weak void HAL_SD_DMA_RxCpltCallback(DMA_HandleTypeDef *hdma)
1410 {
1411 /* Prevent unused argument(s) compilation warning */
1412 UNUSED(hdma);
1413
1414 /* NOTE : This function Should not be modified, when the callback is needed,
1415 the HAL_SD_DMA_RxCpltCallback could be implemented in the user file
1416 */
1417 }
1418
1419 /**
1420 * @brief SD DMA transfer complete Rx error callback.
1421 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
1422 * the configuration information for the specified DMA module.
1423 * @retval None
1424 */
1425 __weak void HAL_SD_DMA_RxErrorCallback(DMA_HandleTypeDef *hdma)
1426 {
1427 /* Prevent unused argument(s) compilation warning */
1428 UNUSED(hdma);
1429
1430 /* NOTE : This function Should not be modified, when the callback is needed,
1431 the HAL_SD_DMA_RxErrorCallback could be implemented in the user file
1432 */
1433 }
1434
1435 /**
1436 * @brief SD Transfer complete Tx callback in non blocking mode.
1437 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
1438 * the configuration information for the specified DMA module.
1439 * @retval None
1440 */
1441 __weak void HAL_SD_DMA_TxCpltCallback(DMA_HandleTypeDef *hdma)
1442 {
1443 /* Prevent unused argument(s) compilation warning */
1444 UNUSED(hdma);
1445
1446 /* NOTE : This function Should not be modified, when the callback is needed,
1447 the HAL_SD_DMA_TxCpltCallback could be implemented in the user file
1448 */
1449 }
1450
1451 /**
1452 * @brief SD DMA transfer complete error Tx callback.
1453 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
1454 * the configuration information for the specified DMA module.
1455 * @retval None
1456 */
1457 __weak void HAL_SD_DMA_TxErrorCallback(DMA_HandleTypeDef *hdma)
1458 {
1459 /* Prevent unused argument(s) compilation warning */
1460 UNUSED(hdma);
1461
1462 /* NOTE : This function Should not be modified, when the callback is needed,
1463 the HAL_SD_DMA_TxErrorCallback could be implemented in the user file
1464 */
1465 }
1466
1467 /**
1468 * @}
1469 */
1470
1471 /** @addtogroup SD_Exported_Functions_Group3
1472 * @brief management functions
1473 *
1474 @verbatim
1475 ==============================================================================
1476 ##### Peripheral Control functions #####
1477 ==============================================================================
1478 [..]
1479 This subsection provides a set of functions allowing to control the SD card
1480 operations.
1481
1482 @endverbatim
1483 * @{
1484 */
1485
1486 /**
1487 * @brief Returns information about specific card.
1488 * @param hsd: SD handle
1489 * @param pCardInfo: Pointer to a HAL_SD_CardInfoTypedef structure that
1490 * contains all SD cardinformation
1491 * @retval SD Card error state
1492 */
1493 HAL_SD_ErrorTypedef HAL_SD_Get_CardInfo(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypedef *pCardInfo)
1494 {
1495 HAL_SD_ErrorTypedef errorstate = SD_OK;
1496 uint32_t tmp = 0;
1497
1498 pCardInfo->CardType = (uint8_t)(hsd->CardType);
1499 pCardInfo->RCA = (uint16_t)(hsd->RCA);
1500
1501 /* Byte 0 */
1502 tmp = (hsd->CSD[0] & 0xFF000000U) >> 24;
1503 pCardInfo->SD_csd.CSDStruct = (uint8_t)((tmp & 0xC0) >> 6);
1504 pCardInfo->SD_csd.SysSpecVersion = (uint8_t)((tmp & 0x3C) >> 2);
1505 pCardInfo->SD_csd.Reserved1 = tmp & 0x03;
1506
1507 /* Byte 1 */
1508 tmp = (hsd->CSD[0] & 0x00FF0000) >> 16;
1509 pCardInfo->SD_csd.TAAC = (uint8_t)tmp;
1510
1511 /* Byte 2 */
1512 tmp = (hsd->CSD[0] & 0x0000FF00) >> 8;
1513 pCardInfo->SD_csd.NSAC = (uint8_t)tmp;
1514
1515 /* Byte 3 */
1516 tmp = hsd->CSD[0] & 0x000000FF;
1517 pCardInfo->SD_csd.MaxBusClkFrec = (uint8_t)tmp;
1518
1519 /* Byte 4 */
1520 tmp = (hsd->CSD[1] & 0xFF000000U) >> 24;
1521 pCardInfo->SD_csd.CardComdClasses = (uint16_t)(tmp << 4);
1522
1523 /* Byte 5 */
1524 tmp = (hsd->CSD[1] & 0x00FF0000U) >> 16;
1525 pCardInfo->SD_csd.CardComdClasses |= (uint16_t)((tmp & 0xF0) >> 4);
1526 pCardInfo->SD_csd.RdBlockLen = (uint8_t)(tmp & 0x0F);
1527
1528 /* Byte 6 */
1529 tmp = (hsd->CSD[1] & 0x0000FF00U) >> 8;
1530 pCardInfo->SD_csd.PartBlockRead = (uint8_t)((tmp & 0x80) >> 7);
1531 pCardInfo->SD_csd.WrBlockMisalign = (uint8_t)((tmp & 0x40) >> 6);
1532 pCardInfo->SD_csd.RdBlockMisalign = (uint8_t)((tmp & 0x20) >> 5);
1533 pCardInfo->SD_csd.DSRImpl = (uint8_t)((tmp & 0x10) >> 4);
1534 pCardInfo->SD_csd.Reserved2 = 0; /*!< Reserved */
1535
1536 if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0))
1537 {
1538 pCardInfo->SD_csd.DeviceSize = (tmp & 0x03) << 10;
1539
1540 /* Byte 7 */
1541 tmp = (uint8_t)(hsd->CSD[1] & 0x000000FFU);
1542 pCardInfo->SD_csd.DeviceSize |= (tmp) << 2;
1543
1544 /* Byte 8 */
1545 tmp = (uint8_t)((hsd->CSD[2] & 0xFF000000U) >> 24);
1546 pCardInfo->SD_csd.DeviceSize |= (tmp & 0xC0) >> 6;
1547
1548 pCardInfo->SD_csd.MaxRdCurrentVDDMin = (tmp & 0x38) >> 3;
1549 pCardInfo->SD_csd.MaxRdCurrentVDDMax = (tmp & 0x07);
1550
1551 /* Byte 9 */
1552 tmp = (uint8_t)((hsd->CSD[2] & 0x00FF0000U) >> 16);
1553 pCardInfo->SD_csd.MaxWrCurrentVDDMin = (tmp & 0xE0) >> 5;
1554 pCardInfo->SD_csd.MaxWrCurrentVDDMax = (tmp & 0x1C) >> 2;
1555 pCardInfo->SD_csd.DeviceSizeMul = (tmp & 0x03) << 1;
1556 /* Byte 10 */
1557 tmp = (uint8_t)((hsd->CSD[2] & 0x0000FF00U) >> 8);
1558 pCardInfo->SD_csd.DeviceSizeMul |= (tmp & 0x80) >> 7;
1559
1560 pCardInfo->CardCapacity = (pCardInfo->SD_csd.DeviceSize + 1) ;
1561 pCardInfo->CardCapacity *= (1 << (pCardInfo->SD_csd.DeviceSizeMul + 2));
1562 pCardInfo->CardBlockSize = 1 << (pCardInfo->SD_csd.RdBlockLen);
1563 pCardInfo->CardCapacity *= pCardInfo->CardBlockSize;
1564 }
1565 else if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
1566 {
1567 /* Byte 7 */
1568 tmp = (uint8_t)(hsd->CSD[1] & 0x000000FFU);
1569 pCardInfo->SD_csd.DeviceSize = (tmp & 0x3F) << 16;
1570
1571 /* Byte 8 */
1572 tmp = (uint8_t)((hsd->CSD[2] & 0xFF000000U) >> 24);
1573
1574 pCardInfo->SD_csd.DeviceSize |= (tmp << 8);
1575
1576 /* Byte 9 */
1577 tmp = (uint8_t)((hsd->CSD[2] & 0x00FF0000U) >> 16);
1578
1579 pCardInfo->SD_csd.DeviceSize |= (tmp);
1580
1581 /* Byte 10 */
1582 tmp = (uint8_t)((hsd->CSD[2] & 0x0000FF00U) >> 8);
1583
1584 pCardInfo->CardCapacity = (uint64_t)(((uint64_t)pCardInfo->SD_csd.DeviceSize + 1) * 512 * 1024);
1585 pCardInfo->CardBlockSize = 512;
1586 }
1587 else
1588 {
1589 /* Not supported card type */
1590 errorstate = SD_ERROR;
1591 }
1592
1593 pCardInfo->SD_csd.EraseGrSize = (tmp & 0x40) >> 6;
1594 pCardInfo->SD_csd.EraseGrMul = (tmp & 0x3F) << 1;
1595
1596 /* Byte 11 */
1597 tmp = (uint8_t)(hsd->CSD[2] & 0x000000FF);
1598 pCardInfo->SD_csd.EraseGrMul |= (tmp & 0x80) >> 7;
1599 pCardInfo->SD_csd.WrProtectGrSize = (tmp & 0x7F);
1600
1601 /* Byte 12 */
1602 tmp = (uint8_t)((hsd->CSD[3] & 0xFF000000U) >> 24);
1603 pCardInfo->SD_csd.WrProtectGrEnable = (tmp & 0x80) >> 7;
1604 pCardInfo->SD_csd.ManDeflECC = (tmp & 0x60) >> 5;
1605 pCardInfo->SD_csd.WrSpeedFact = (tmp & 0x1C) >> 2;
1606 pCardInfo->SD_csd.MaxWrBlockLen = (tmp & 0x03) << 2;
1607
1608 /* Byte 13 */
1609 tmp = (uint8_t)((hsd->CSD[3] & 0x00FF0000) >> 16);
1610 pCardInfo->SD_csd.MaxWrBlockLen |= (tmp & 0xC0) >> 6;
1611 pCardInfo->SD_csd.WriteBlockPaPartial = (tmp & 0x20) >> 5;
1612 pCardInfo->SD_csd.Reserved3 = 0;
1613 pCardInfo->SD_csd.ContentProtectAppli = (tmp & 0x01);
1614
1615 /* Byte 14 */
1616 tmp = (uint8_t)((hsd->CSD[3] & 0x0000FF00) >> 8);
1617 pCardInfo->SD_csd.FileFormatGrouop = (tmp & 0x80) >> 7;
1618 pCardInfo->SD_csd.CopyFlag = (tmp & 0x40) >> 6;
1619 pCardInfo->SD_csd.PermWrProtect = (tmp & 0x20) >> 5;
1620 pCardInfo->SD_csd.TempWrProtect = (tmp & 0x10) >> 4;
1621 pCardInfo->SD_csd.FileFormat = (tmp & 0x0C) >> 2;
1622 pCardInfo->SD_csd.ECC = (tmp & 0x03);
1623
1624 /* Byte 15 */
1625 tmp = (uint8_t)(hsd->CSD[3] & 0x000000FF);
1626 pCardInfo->SD_csd.CSD_CRC = (tmp & 0xFE) >> 1;
1627 pCardInfo->SD_csd.Reserved4 = 1;
1628
1629 /* Byte 0 */
1630 tmp = (uint8_t)((hsd->CID[0] & 0xFF000000U) >> 24);
1631 pCardInfo->SD_cid.ManufacturerID = tmp;
1632
1633 /* Byte 1 */
1634 tmp = (uint8_t)((hsd->CID[0] & 0x00FF0000) >> 16);
1635 pCardInfo->SD_cid.OEM_AppliID = tmp << 8;
1636
1637 /* Byte 2 */
1638 tmp = (uint8_t)((hsd->CID[0] & 0x000000FF00) >> 8);
1639 pCardInfo->SD_cid.OEM_AppliID |= tmp;
1640
1641 /* Byte 3 */
1642 tmp = (uint8_t)(hsd->CID[0] & 0x000000FF);
1643 pCardInfo->SD_cid.ProdName1 = tmp << 24;
1644
1645 /* Byte 4 */
1646 tmp = (uint8_t)((hsd->CID[1] & 0xFF000000U) >> 24);
1647 pCardInfo->SD_cid.ProdName1 |= tmp << 16;
1648
1649 /* Byte 5 */
1650 tmp = (uint8_t)((hsd->CID[1] & 0x00FF0000) >> 16);
1651 pCardInfo->SD_cid.ProdName1 |= tmp << 8;
1652
1653 /* Byte 6 */
1654 tmp = (uint8_t)((hsd->CID[1] & 0x0000FF00) >> 8);
1655 pCardInfo->SD_cid.ProdName1 |= tmp;
1656
1657 /* Byte 7 */
1658 tmp = (uint8_t)(hsd->CID[1] & 0x000000FF);
1659 pCardInfo->SD_cid.ProdName2 = tmp;
1660
1661 /* Byte 8 */
1662 tmp = (uint8_t)((hsd->CID[2] & 0xFF000000U) >> 24);
1663 pCardInfo->SD_cid.ProdRev = tmp;
1664
1665 /* Byte 9 */
1666 tmp = (uint8_t)((hsd->CID[2] & 0x00FF0000) >> 16);
1667 pCardInfo->SD_cid.ProdSN = tmp << 24;
1668
1669 /* Byte 10 */
1670 tmp = (uint8_t)((hsd->CID[2] & 0x0000FF00) >> 8);
1671 pCardInfo->SD_cid.ProdSN |= tmp << 16;
1672
1673 /* Byte 11 */
1674 tmp = (uint8_t)(hsd->CID[2] & 0x000000FF);
1675 pCardInfo->SD_cid.ProdSN |= tmp << 8;
1676
1677 /* Byte 12 */
1678 tmp = (uint8_t)((hsd->CID[3] & 0xFF000000U) >> 24);
1679 pCardInfo->SD_cid.ProdSN |= tmp;
1680
1681 /* Byte 13 */
1682 tmp = (uint8_t)((hsd->CID[3] & 0x00FF0000) >> 16);
1683 pCardInfo->SD_cid.Reserved1 |= (tmp & 0xF0) >> 4;
1684 pCardInfo->SD_cid.ManufactDate = (tmp & 0x0F) << 8;
1685
1686 /* Byte 14 */
1687 tmp = (uint8_t)((hsd->CID[3] & 0x0000FF00) >> 8);
1688 pCardInfo->SD_cid.ManufactDate |= tmp;
1689
1690 /* Byte 15 */
1691 tmp = (uint8_t)(hsd->CID[3] & 0x000000FF);
1692 pCardInfo->SD_cid.CID_CRC = (tmp & 0xFE) >> 1;
1693 pCardInfo->SD_cid.Reserved2 = 1;
1694
1695 return errorstate;
1696 }
1697
1698 /**
1699 * @brief Enables wide bus operation for the requested card if supported by
1700 * card.
1701 * @param hsd: SD handle
1702 * @param WideMode: Specifies the SD card wide bus mode
1703 * This parameter can be one of the following values:
1704 * @arg SDMMC_BUS_WIDE_8B: 8-bit data transfer (Only for MMC)
1705 * @arg SDMMC_BUS_WIDE_4B: 4-bit data transfer
1706 * @arg SDMMC_BUS_WIDE_1B: 1-bit data transfer
1707 * @retval SD Card error state
1708 */
1709 HAL_SD_ErrorTypedef HAL_SD_WideBusOperation_Config(SD_HandleTypeDef *hsd, uint32_t WideMode)
1710 {
1711 HAL_SD_ErrorTypedef errorstate = SD_OK;
1712 SDMMC_InitTypeDef tmpinit;
1713
1714 /* MMC Card does not support this feature */
1715 if (hsd->CardType == MULTIMEDIA_CARD)
1716 {
1717 errorstate = SD_UNSUPPORTED_FEATURE;
1718
1719 return errorstate;
1720 }
1721 else if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\
1722 (hsd->CardType == HIGH_CAPACITY_SD_CARD))
1723 {
1724 if (WideMode == SDMMC_BUS_WIDE_8B)
1725 {
1726 errorstate = SD_UNSUPPORTED_FEATURE;
1727 }
1728 else if (WideMode == SDMMC_BUS_WIDE_4B)
1729 {
1730 errorstate = SD_WideBus_Enable(hsd);
1731 }
1732 else if (WideMode == SDMMC_BUS_WIDE_1B)
1733 {
1734 errorstate = SD_WideBus_Disable(hsd);
1735 }
1736 else
1737 {
1738 /* WideMode is not a valid argument*/
1739 errorstate = SD_INVALID_PARAMETER;
1740 }
1741
1742 if (errorstate == SD_OK)
1743 {
1744 /* Configure the SDMMC peripheral */
1745 tmpinit.ClockEdge = hsd->Init.ClockEdge;
1746 tmpinit.ClockBypass = hsd->Init.ClockBypass;
1747 tmpinit.ClockPowerSave = hsd->Init.ClockPowerSave;
1748 tmpinit.BusWide = WideMode;
1749 tmpinit.HardwareFlowControl = hsd->Init.HardwareFlowControl;
1750 tmpinit.ClockDiv = hsd->Init.ClockDiv;
1751 SDMMC_Init(hsd->Instance, tmpinit);
1752 }
1753 }
1754
1755 return errorstate;
1756 }
1757
1758 /**
1759 * @brief Aborts an ongoing data transfer.
1760 * @param hsd: SD handle
1761 * @retval SD Card error state
1762 */
1763 HAL_SD_ErrorTypedef HAL_SD_StopTransfer(SD_HandleTypeDef *hsd)
1764 {
1765 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure;
1766 HAL_SD_ErrorTypedef errorstate = SD_OK;
1767
1768 /* Send CMD12 STOP_TRANSMISSION */
1769 sdmmc_cmdinitstructure.Argument = 0;
1770 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_STOP_TRANSMISSION;
1771 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
1772 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
1773 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
1774 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
1775
1776 /* Check for error conditions */
1777 errorstate = SD_CmdResp1Error(hsd, SD_CMD_STOP_TRANSMISSION);
1778
1779 return errorstate;
1780 }
1781
1782 /**
1783 * @brief Switches the SD card to High Speed mode.
1784 * This API must be used after "Transfer State"
1785 * @note This operation should be followed by the configuration
1786 * of PLL to have SDMMCCK clock between 67 and 75 MHz
1787 * @param hsd: SD handle
1788 * @retval SD Card error state
1789 */
1790 HAL_SD_ErrorTypedef HAL_SD_HighSpeed (SD_HandleTypeDef *hsd)
1791 {
1792 HAL_SD_ErrorTypedef errorstate = SD_OK;
1793 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure;
1794 SDMMC_DataInitTypeDef sdmmc_datainitstructure;
1795
1796 uint8_t SD_hs[64] = {0};
1797 uint32_t SD_scr[2] = {0, 0};
1798 uint32_t SD_SPEC = 0 ;
1799 uint32_t count = 0, *tempbuff = (uint32_t *)SD_hs;
1800
1801 /* Initialize the Data control register */
1802 hsd->Instance->DCTRL = 0;
1803
1804 /* Get SCR Register */
1805 errorstate = SD_FindSCR(hsd, SD_scr);
1806
1807 if (errorstate != SD_OK)
1808 {
1809 return errorstate;
1810 }
1811
1812 /* Test the Version supported by the card*/
1813 SD_SPEC = (SD_scr[1] & 0x01000000) | (SD_scr[1] & 0x02000000);
1814
1815 if (SD_SPEC != SD_ALLZERO)
1816 {
1817 /* Set Block Size for Card */
1818 sdmmc_cmdinitstructure.Argument = (uint32_t)64;
1819 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
1820 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
1821 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
1822 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
1823 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
1824
1825 /* Check for error conditions */
1826 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
1827
1828 if (errorstate != SD_OK)
1829 {
1830 return errorstate;
1831 }
1832
1833 /* Configure the SD DPSM (Data Path State Machine) */
1834 sdmmc_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
1835 sdmmc_datainitstructure.DataLength = 64;
1836 sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B ;
1837 sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
1838 sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1839 sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE;
1840 SDMMC_DataConfig(hsd->Instance, &sdmmc_datainitstructure);
1841
1842 /* Send CMD6 switch mode */
1843 sdmmc_cmdinitstructure.Argument = 0x80FFFF01U;
1844 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_HS_SWITCH;
1845 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
1846
1847 /* Check for error conditions */
1848 errorstate = SD_CmdResp1Error(hsd, SD_CMD_HS_SWITCH);
1849
1850 if (errorstate != SD_OK)
1851 {
1852 return errorstate;
1853 }
1854
1855 while(!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND))
1856 {
1857 if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
1858 {
1859 for (count = 0; count < 8; count++)
1860 {
1861 *(tempbuff + count) = SDMMC_ReadFIFO(hsd->Instance);
1862 }
1863
1864 tempbuff += 8;
1865 }
1866 }
1867
1868 if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
1869 {
1870 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
1871
1872 errorstate = SD_DATA_TIMEOUT;
1873
1874 return errorstate;
1875 }
1876 else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
1877 {
1878 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
1879
1880 errorstate = SD_DATA_CRC_FAIL;
1881
1882 return errorstate;
1883 }
1884 else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
1885 {
1886 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
1887
1888 errorstate = SD_RX_OVERRUN;
1889
1890 return errorstate;
1891 }
1892 else
1893 {
1894 /* No error flag set */
1895 }
1896
1897 count = SD_DATATIMEOUT;
1898
1899 while ((__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL)) && (count > 0))
1900 {
1901 *tempbuff = SDMMC_ReadFIFO(hsd->Instance);
1902 tempbuff++;
1903 count--;
1904 }
1905
1906 /* Clear all the static flags */
1907 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1908
1909 /* Test if the switch mode HS is ok */
1910 if ((SD_hs[13]& 2) != 2)
1911 {
1912 errorstate = SD_UNSUPPORTED_FEATURE;
1913 }
1914 }
1915
1916 return errorstate;
1917 }
1918
1919 /**
1920 * @}
1921 */
1922
1923 /** @addtogroup SD_Exported_Functions_Group4
1924 * @brief Peripheral State functions
1925 *
1926 @verbatim
1927 ==============================================================================
1928 ##### Peripheral State functions #####
1929 ==============================================================================
1930 [..]
1931 This subsection permits to get in runtime the status of the peripheral
1932 and the data flow.
1933
1934 @endverbatim
1935 * @{
1936 */
1937
1938 /**
1939 * @brief Returns the current SD card's status.
1940 * @param hsd: SD handle
1941 * @param pSDstatus: Pointer to the buffer that will contain the SD card status
1942 * SD Status register)
1943 * @retval SD Card error state
1944 */
1945 HAL_SD_ErrorTypedef HAL_SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus)
1946 {
1947 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure;
1948 SDMMC_DataInitTypeDef sdmmc_datainitstructure;
1949 HAL_SD_ErrorTypedef errorstate = SD_OK;
1950 uint32_t count = 0;
1951
1952 /* Check SD response */
1953 if ((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SD_CARD_LOCKED) == SD_CARD_LOCKED)
1954 {
1955 errorstate = SD_LOCK_UNLOCK_FAILED;
1956
1957 return errorstate;
1958 }
1959
1960 /* Set block size for card if it is not equal to current block size for card */
1961 sdmmc_cmdinitstructure.Argument = 64;
1962 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
1963 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
1964 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
1965 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
1966 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
1967
1968 /* Check for error conditions */
1969 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
1970
1971 if (errorstate != SD_OK)
1972 {
1973 return errorstate;
1974 }
1975
1976 /* Send CMD55 */
1977 sdmmc_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16);
1978 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD;
1979 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
1980
1981 /* Check for error conditions */
1982 errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
1983
1984 if (errorstate != SD_OK)
1985 {
1986 return errorstate;
1987 }
1988
1989 /* Configure the SD DPSM (Data Path State Machine) */
1990 sdmmc_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
1991 sdmmc_datainitstructure.DataLength = 64;
1992 sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B;
1993 sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
1994 sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1995 sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE;
1996 SDMMC_DataConfig(hsd->Instance, &sdmmc_datainitstructure);
1997
1998 /* Send ACMD13 (SD_APP_STAUS) with argument as card's RCA */
1999 sdmmc_cmdinitstructure.Argument = 0;
2000 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SD_APP_STATUS;
2001 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
2002
2003 /* Check for error conditions */
2004 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SD_APP_STATUS);
2005
2006 if (errorstate != SD_OK)
2007 {
2008 return errorstate;
2009 }
2010
2011 /* Get status data */
2012 while(!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND))
2013 {
2014 if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
2015 {
2016 for (count = 0; count < 8; count++)
2017 {
2018 *(pSDstatus + count) = SDMMC_ReadFIFO(hsd->Instance);
2019 }
2020
2021 pSDstatus += 8;
2022 }
2023 }
2024
2025 if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
2026 {
2027 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
2028
2029 errorstate = SD_DATA_TIMEOUT;
2030
2031 return errorstate;
2032 }
2033 else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
2034 {
2035 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
2036
2037 errorstate = SD_DATA_CRC_FAIL;
2038
2039 return errorstate;
2040 }
2041 else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
2042 {
2043 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
2044
2045 errorstate = SD_RX_OVERRUN;
2046
2047 return errorstate;
2048 }
2049 else
2050 {
2051 /* No error flag set */
2052 }
2053
2054 count = SD_DATATIMEOUT;
2055 while ((__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL)) && (count > 0))
2056 {
2057 *pSDstatus = SDMMC_ReadFIFO(hsd->Instance);
2058 pSDstatus++;
2059 count--;
2060 }
2061
2062 /* Clear all the static status flags*/
2063 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2064
2065 return errorstate;
2066 }
2067
2068 /**
2069 * @brief Gets the current sd card data status.
2070 * @param hsd: SD handle
2071 * @retval Data Transfer state
2072 */
2073 HAL_SD_TransferStateTypedef HAL_SD_GetStatus(SD_HandleTypeDef *hsd)
2074 {
2075 HAL_SD_CardStateTypedef cardstate = SD_CARD_TRANSFER;
2076
2077 /* Get SD card state */
2078 cardstate = SD_GetState(hsd);
2079
2080 /* Find SD status according to card state*/
2081 if (cardstate == SD_CARD_TRANSFER)
2082 {
2083 return SD_TRANSFER_OK;
2084 }
2085 else if(cardstate == SD_CARD_ERROR)
2086 {
2087 return SD_TRANSFER_ERROR;
2088 }
2089 else
2090 {
2091 return SD_TRANSFER_BUSY;
2092 }
2093 }
2094
2095 /**
2096 * @brief Gets the SD card status.
2097 * @param hsd: SD handle
2098 * @param pCardStatus: Pointer to the HAL_SD_CardStatusTypedef structure that
2099 * will contain the SD card status information
2100 * @retval SD Card error state
2101 */
2102 HAL_SD_ErrorTypedef HAL_SD_GetCardStatus(SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypedef *pCardStatus)
2103 {
2104 HAL_SD_ErrorTypedef errorstate = SD_OK;
2105 uint32_t tmp = 0;
2106 uint32_t sd_status[16];
2107
2108 errorstate = HAL_SD_SendSDStatus(hsd, sd_status);
2109
2110 if (errorstate != SD_OK)
2111 {
2112 return errorstate;
2113 }
2114
2115 /* Byte 0 */
2116 tmp = (sd_status[0] & 0xC0) >> 6;
2117 pCardStatus->DAT_BUS_WIDTH = (uint8_t)tmp;
2118
2119 /* Byte 0 */
2120 tmp = (sd_status[0] & 0x20) >> 5;
2121 pCardStatus->SECURED_MODE = (uint8_t)tmp;
2122
2123 /* Byte 2 */
2124 tmp = (sd_status[0] & 0x00FF0000) >> 16;
2125 pCardStatus->SD_CARD_TYPE = (uint16_t)(tmp << 8);
2126
2127 /* Byte 3 */
2128 tmp = (sd_status[0] & 0xFF000000) >> 24;
2129 pCardStatus->SD_CARD_TYPE |= (uint16_t)tmp;
2130
2131 /* Byte 4 */
2132 tmp = (sd_status[1] & 0xFF);
2133 pCardStatus->SIZE_OF_PROTECTED_AREA = (uint32_t)(tmp << 24);
2134
2135 /* Byte 5 */
2136 tmp = (sd_status[1] & 0xFF00) >> 8;
2137 pCardStatus->SIZE_OF_PROTECTED_AREA |= (uint32_t)(tmp << 16);
2138
2139 /* Byte 6 */
2140 tmp = (sd_status[1] & 0xFF0000) >> 16;
2141 pCardStatus->SIZE_OF_PROTECTED_AREA |= (uint32_t)(tmp << 8);
2142
2143 /* Byte 7 */
2144 tmp = (sd_status[1] & 0xFF000000) >> 24;
2145 pCardStatus->SIZE_OF_PROTECTED_AREA |= (uint32_t)tmp;
2146
2147 /* Byte 8 */
2148 tmp = (sd_status[2] & 0xFF);
2149 pCardStatus->SPEED_CLASS = (uint8_t)tmp;
2150
2151 /* Byte 9 */
2152 tmp = (sd_status[2] & 0xFF00) >> 8;
2153 pCardStatus->PERFORMANCE_MOVE = (uint8_t)tmp;
2154
2155 /* Byte 10 */
2156 tmp = (sd_status[2] & 0xF00000) >> 20;
2157 pCardStatus->AU_SIZE = (uint8_t)tmp;
2158
2159 /* Byte 11 */
2160 tmp = (sd_status[2] & 0xFF000000) >> 24;
2161 pCardStatus->ERASE_SIZE = (uint16_t)(tmp << 8);
2162
2163 /* Byte 12 */
2164 tmp = (sd_status[3] & 0xFF);
2165 pCardStatus->ERASE_SIZE |= (uint16_t)tmp;
2166
2167 /* Byte 13 */
2168 tmp = (sd_status[3] & 0xFC00) >> 10;
2169 pCardStatus->ERASE_TIMEOUT = (uint8_t)tmp;
2170
2171 /* Byte 13 */
2172 tmp = (sd_status[3] & 0x0300) >> 8;
2173 pCardStatus->ERASE_OFFSET = (uint8_t)tmp;
2174
2175 return errorstate;
2176 }
2177
2178 /**
2179 * @}
2180 */
2181
2182 /**
2183 * @}
2184 */
2185
2186 /* Private function ----------------------------------------------------------*/
2187 /** @addtogroup SD_Private_Functions
2188 * @{
2189 */
2190
2191 /**
2192 * @brief SD DMA transfer complete Rx callback.
2193 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
2194 * the configuration information for the specified DMA module.
2195 * @retval None
2196 */
2197 static void SD_DMA_RxCplt(DMA_HandleTypeDef *hdma)
2198 {
2199 SD_HandleTypeDef *hsd = (SD_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
2200
2201 /* DMA transfer is complete */
2202 hsd->DmaTransferCplt = 1;
2203
2204 /* Wait until SD transfer is complete */
2205 while(hsd->SdTransferCplt == 0)
2206 {
2207 }
2208
2209 /* Disable the DMA channel */
2210 HAL_DMA_Abort(hdma);
2211
2212 /* Transfer complete user callback */
2213 HAL_SD_DMA_RxCpltCallback(hsd->hdmarx);
2214 }
2215
2216 /**
2217 * @brief SD DMA transfer Error Rx callback.
2218 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
2219 * the configuration information for the specified DMA module.
2220 * @retval None
2221 */
2222 static void SD_DMA_RxError(DMA_HandleTypeDef *hdma)
2223 {
2224 SD_HandleTypeDef *hsd = (SD_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
2225
2226 /* Transfer complete user callback */
2227 HAL_SD_DMA_RxErrorCallback(hsd->hdmarx);
2228 }
2229
2230 /**
2231 * @brief SD DMA transfer complete Tx callback.
2232 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
2233 * the configuration information for the specified DMA module.
2234 * @retval None
2235 */
2236 static void SD_DMA_TxCplt(DMA_HandleTypeDef *hdma)
2237 {
2238 SD_HandleTypeDef *hsd = (SD_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
2239
2240 /* DMA transfer is complete */
2241 hsd->DmaTransferCplt = 1;
2242
2243 /* Wait until SD transfer is complete */
2244 while(hsd->SdTransferCplt == 0)
2245 {
2246 }
2247
2248 /* Disable the DMA channel */
2249 HAL_DMA_Abort(hdma);
2250
2251 /* Transfer complete user callback */
2252 HAL_SD_DMA_TxCpltCallback(hsd->hdmatx);
2253 }
2254
2255 /**
2256 * @brief SD DMA transfer Error Tx callback.
2257 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
2258 * the configuration information for the specified DMA module.
2259 * @retval None
2260 */
2261 static void SD_DMA_TxError(DMA_HandleTypeDef *hdma)
2262 {
2263 SD_HandleTypeDef *hsd = ( SD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
2264
2265 /* Transfer complete user callback */
2266 HAL_SD_DMA_TxErrorCallback(hsd->hdmatx);
2267 }
2268
2269 /**
2270 * @brief Returns the SD current state.
2271 * @param hsd: SD handle
2272 * @retval SD card current state
2273 */
2274 static HAL_SD_CardStateTypedef SD_GetState(SD_HandleTypeDef *hsd)
2275 {
2276 uint32_t resp1 = 0;
2277
2278 if (SD_SendStatus(hsd, &resp1) != SD_OK)
2279 {
2280 return SD_CARD_ERROR;
2281 }
2282 else
2283 {
2284 return (HAL_SD_CardStateTypedef)((resp1 >> 9) & 0x0F);
2285 }
2286 }
2287
2288 /**
2289 * @brief Initializes all cards or single card as the case may be Card(s) come
2290 * into standby state.
2291 * @param hsd: SD handle
2292 * @retval SD Card error state
2293 */
2294 static HAL_SD_ErrorTypedef SD_Initialize_Cards(SD_HandleTypeDef *hsd)
2295 {
2296 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure;
2297 HAL_SD_ErrorTypedef errorstate = SD_OK;
2298 uint16_t sd_rca = 1;
2299
2300 if(SDMMC_GetPowerState(hsd->Instance) == 0) /* Power off */
2301 {
2302 errorstate = SD_REQUEST_NOT_APPLICABLE;
2303
2304 return errorstate;
2305 }
2306
2307 if(hsd->CardType != SECURE_DIGITAL_IO_CARD)
2308 {
2309 /* Send CMD2 ALL_SEND_CID */
2310 sdmmc_cmdinitstructure.Argument = 0;
2311 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_ALL_SEND_CID;
2312 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_LONG;
2313 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
2314 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
2315 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
2316
2317 /* Check for error conditions */
2318 errorstate = SD_CmdResp2Error(hsd);
2319
2320 if(errorstate != SD_OK)
2321 {
2322 return errorstate;
2323 }
2324
2325 /* Get Card identification number data */
2326 hsd->CID[0] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
2327 hsd->CID[1] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2);
2328 hsd->CID[2] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP3);
2329 hsd->CID[3] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP4);
2330 }
2331
2332 if((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\
2333 (hsd->CardType == SECURE_DIGITAL_IO_COMBO_CARD) || (hsd->CardType == HIGH_CAPACITY_SD_CARD))
2334 {
2335 /* Send CMD3 SET_REL_ADDR with argument 0 */
2336 /* SD Card publishes its RCA. */
2337 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SET_REL_ADDR;
2338 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
2339 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
2340
2341 /* Check for error conditions */
2342 errorstate = SD_CmdResp6Error(hsd, SD_CMD_SET_REL_ADDR, &sd_rca);
2343
2344 if(errorstate != SD_OK)
2345 {
2346 return errorstate;
2347 }
2348 }
2349
2350 if (hsd->CardType != SECURE_DIGITAL_IO_CARD)
2351 {
2352 /* Get the SD card RCA */
2353 hsd->RCA = sd_rca;
2354
2355 /* Send CMD9 SEND_CSD with argument as card's RCA */
2356 sdmmc_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16);
2357 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SEND_CSD;
2358 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_LONG;
2359 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
2360
2361 /* Check for error conditions */
2362 errorstate = SD_CmdResp2Error(hsd);
2363
2364 if(errorstate != SD_OK)
2365 {
2366 return errorstate;
2367 }
2368
2369 /* Get Card Specific Data */
2370 hsd->CSD[0] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
2371 hsd->CSD[1] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2);
2372 hsd->CSD[2] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP3);
2373 hsd->CSD[3] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP4);
2374 }
2375
2376 /* All cards are initialized */
2377 return errorstate;
2378 }
2379
2380 /**
2381 * @brief Selects od Deselects the corresponding card.
2382 * @param hsd: SD handle
2383 * @param addr: Address of the card to be selected
2384 * @retval SD Card error state
2385 */
2386 static HAL_SD_ErrorTypedef SD_Select_Deselect(SD_HandleTypeDef *hsd, uint64_t addr)
2387 {
2388 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure;
2389 HAL_SD_ErrorTypedef errorstate = SD_OK;
2390
2391 /* Send CMD7 SDMMC_SEL_DESEL_CARD */
2392 sdmmc_cmdinitstructure.Argument = (uint32_t)addr;
2393 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SEL_DESEL_CARD;
2394 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
2395 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
2396 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
2397 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
2398
2399 /* Check for error conditions */
2400 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SEL_DESEL_CARD);
2401
2402 return errorstate;
2403 }
2404
2405 /**
2406 * @brief Enquires cards about their operating voltage and configures clock
2407 * controls and stores SD information that will be needed in future
2408 * in the SD handle.
2409 * @param hsd: SD handle
2410 * @retval SD Card error state
2411 */
2412 static HAL_SD_ErrorTypedef SD_PowerON(SD_HandleTypeDef *hsd)
2413 {
2414 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure;
2415 __IO HAL_SD_ErrorTypedef errorstate = SD_OK;
2416 uint32_t response = 0, count = 0, validvoltage = 0;
2417 uint32_t sdtype = SD_STD_CAPACITY;
2418
2419 /* Power ON Sequence -------------------------------------------------------*/
2420 /* Disable SDMMC Clock */
2421 __HAL_SD_SDMMC_DISABLE(hsd);
2422
2423 /* Set Power State to ON */
2424 SDMMC_PowerState_ON(hsd->Instance);
2425
2426 /* 1ms: required power up waiting time before starting the SD initialization
2427 sequence */
2428 HAL_Delay(1);
2429
2430 /* Enable SDMMC Clock */
2431 __HAL_SD_SDMMC_ENABLE(hsd);
2432
2433 /* CMD0: GO_IDLE_STATE -----------------------------------------------------*/
2434 /* No CMD response required */
2435 sdmmc_cmdinitstructure.Argument = 0;
2436 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_GO_IDLE_STATE;
2437 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_NO;
2438 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
2439 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
2440 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
2441
2442 /* Check for error conditions */
2443 errorstate = SD_CmdError(hsd);
2444
2445 if(errorstate != SD_OK)
2446 {
2447 /* CMD Response Timeout (wait for CMDSENT flag) */
2448 return errorstate;
2449 }
2450
2451 /* CMD8: SEND_IF_COND ------------------------------------------------------*/
2452 /* Send CMD8 to verify SD card interface operating condition */
2453 /* Argument: - [31:12]: Reserved (shall be set to '0')
2454 - [11:8]: Supply Voltage (VHS) 0x1 (Range: 2.7-3.6 V)
2455 - [7:0]: Check Pattern (recommended 0xAA) */
2456 /* CMD Response: R7 */
2457 sdmmc_cmdinitstructure.Argument = SD_CHECK_PATTERN;
2458 sdmmc_cmdinitstructure.CmdIndex = SD_SDMMC_SEND_IF_COND;
2459 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
2460 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
2461
2462 /* Check for error conditions */
2463 errorstate = SD_CmdResp7Error(hsd);
2464
2465 if (errorstate == SD_OK)
2466 {
2467 /* SD Card 2.0 */
2468 hsd->CardType = STD_CAPACITY_SD_CARD_V2_0;
2469 sdtype = SD_HIGH_CAPACITY;
2470 }
2471
2472 /* Send CMD55 */
2473 sdmmc_cmdinitstructure.Argument = 0;
2474 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD;
2475 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
2476
2477 /* Check for error conditions */
2478 errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
2479
2480 /* If errorstate is Command Timeout, it is a MMC card */
2481 /* If errorstate is SD_OK it is a SD card: SD card 2.0 (voltage range mismatch)
2482 or SD card 1.x */
2483 if(errorstate == SD_OK)
2484 {
2485 /* SD CARD */
2486 /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
2487 while((!validvoltage) && (count < SD_MAX_VOLT_TRIAL))
2488 {
2489
2490 /* SEND CMD55 APP_CMD with RCA as 0 */
2491 sdmmc_cmdinitstructure.Argument = 0;
2492 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD;
2493 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
2494 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
2495 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
2496 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
2497
2498 /* Check for error conditions */
2499 errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
2500
2501 if(errorstate != SD_OK)
2502 {
2503 return errorstate;
2504 }
2505
2506 /* Send CMD41 */
2507 sdmmc_cmdinitstructure.Argument = SD_VOLTAGE_WINDOW_SD | sdtype;
2508 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SD_APP_OP_COND;
2509 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
2510 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
2511 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
2512 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
2513
2514 /* Check for error conditions */
2515 errorstate = SD_CmdResp3Error(hsd);
2516
2517 if(errorstate != SD_OK)
2518 {
2519 return errorstate;
2520 }
2521
2522 /* Get command response */
2523 response = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
2524
2525 /* Get operating voltage*/
2526 validvoltage = (((response >> 31) == 1) ? 1 : 0);
2527
2528 count++;
2529 }
2530
2531 if(count >= SD_MAX_VOLT_TRIAL)
2532 {
2533 errorstate = SD_INVALID_VOLTRANGE;
2534
2535 return errorstate;
2536 }
2537
2538 if((response & SD_HIGH_CAPACITY) == SD_HIGH_CAPACITY) /* (response &= SD_HIGH_CAPACITY) */
2539 {
2540 hsd->CardType = HIGH_CAPACITY_SD_CARD;
2541 }
2542
2543 } /* else MMC Card */
2544
2545 return errorstate;
2546 }
2547
2548 /**
2549 * @brief Turns the SDMMC output signals off.
2550 * @param hsd: SD handle
2551 * @retval SD Card error state
2552 */
2553 static HAL_SD_ErrorTypedef SD_PowerOFF(SD_HandleTypeDef *hsd)
2554 {
2555 HAL_SD_ErrorTypedef errorstate = SD_OK;
2556
2557 /* Set Power State to OFF */
2558 SDMMC_PowerState_OFF(hsd->Instance);
2559
2560 return errorstate;
2561 }
2562
2563 /**
2564 * @brief Returns the current card's status.
2565 * @param hsd: SD handle
2566 * @param pCardStatus: pointer to the buffer that will contain the SD card
2567 * status (Card Status register)
2568 * @retval SD Card error state
2569 */
2570 static HAL_SD_ErrorTypedef SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus)
2571 {
2572 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure;
2573 HAL_SD_ErrorTypedef errorstate = SD_OK;
2574
2575 if(pCardStatus == NULL)
2576 {
2577 errorstate = SD_INVALID_PARAMETER;
2578
2579 return errorstate;
2580 }
2581
2582 /* Send Status command */
2583 sdmmc_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16);
2584 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SEND_STATUS;
2585 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
2586 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
2587 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
2588 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
2589
2590 /* Check for error conditions */
2591 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SEND_STATUS);
2592
2593 if(errorstate != SD_OK)
2594 {
2595 return errorstate;
2596 }
2597
2598 /* Get SD card status */
2599 *pCardStatus = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
2600
2601 return errorstate;
2602 }
2603
2604 /**
2605 * @brief Checks for error conditions for CMD0.
2606 * @param hsd: SD handle
2607 * @retval SD Card error state
2608 */
2609 static HAL_SD_ErrorTypedef SD_CmdError(SD_HandleTypeDef *hsd)
2610 {
2611 HAL_SD_ErrorTypedef errorstate = SD_OK;
2612 uint32_t timeout, tmp;
2613
2614 timeout = SDMMC_CMD0TIMEOUT;
2615
2616 tmp = __HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CMDSENT);
2617
2618 while((timeout > 0) && (!tmp))
2619 {
2620 tmp = __HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CMDSENT);
2621 timeout--;
2622 }
2623
2624 if(timeout == 0)
2625 {
2626 errorstate = SD_CMD_RSP_TIMEOUT;
2627 return errorstate;
2628 }
2629
2630 /* Clear all the static flags */
2631 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2632
2633 return errorstate;
2634 }
2635
2636 /**
2637 * @brief Checks for error conditions for R7 response.
2638 * @param hsd: SD handle
2639 * @retval SD Card error state
2640 */
2641 static HAL_SD_ErrorTypedef SD_CmdResp7Error(SD_HandleTypeDef *hsd)
2642 {
2643 HAL_SD_ErrorTypedef errorstate = SD_ERROR;
2644 uint32_t timeout = SDMMC_CMD0TIMEOUT, tmp;
2645
2646 tmp = __HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT);
2647
2648 while((!tmp) && (timeout > 0))
2649 {
2650 tmp = __HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT);
2651 timeout--;
2652 }
2653
2654 tmp = __HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CTIMEOUT);
2655
2656 if((timeout == 0) || tmp)
2657 {
2658 /* Card is not V2.0 compliant or card does not support the set voltage range */
2659 errorstate = SD_CMD_RSP_TIMEOUT;
2660
2661 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_CTIMEOUT);
2662
2663 return errorstate;
2664 }
2665
2666 if(__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CMDREND))
2667 {
2668 /* Card is SD V2.0 compliant */
2669 errorstate = SD_OK;
2670
2671 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_CMDREND);
2672
2673 return errorstate;
2674 }
2675
2676 return errorstate;
2677 }
2678
2679 /**
2680 * @brief Checks for error conditions for R1 response.
2681 * @param hsd: SD handle
2682 * @param SD_CMD: The sent command index
2683 * @retval SD Card error state
2684 */
2685 static HAL_SD_ErrorTypedef SD_CmdResp1Error(SD_HandleTypeDef *hsd, uint8_t SD_CMD)
2686 {
2687 HAL_SD_ErrorTypedef errorstate = SD_OK;
2688 uint32_t response_r1;
2689
2690 while(!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT))
2691 {
2692 }
2693
2694 if(__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CTIMEOUT))
2695 {
2696 errorstate = SD_CMD_RSP_TIMEOUT;
2697
2698 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_CTIMEOUT);
2699
2700 return errorstate;
2701 }
2702 else if(__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CCRCFAIL))
2703 {
2704 errorstate = SD_CMD_CRC_FAIL;
2705
2706 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_CCRCFAIL);
2707
2708 return errorstate;
2709 }
2710
2711 /* Check response received is of desired command */
2712 if(SDMMC_GetCommandResponse(hsd->Instance) != SD_CMD)
2713 {
2714 errorstate = SD_ILLEGAL_CMD;
2715
2716 return errorstate;
2717 }
2718
2719 /* Clear all the static flags */
2720 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2721
2722 /* We have received response, retrieve it for analysis */
2723 response_r1 = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
2724
2725 if((response_r1 & SD_OCR_ERRORBITS) == SD_ALLZERO)
2726 {
2727 return errorstate;
2728 }
2729
2730 if((response_r1 & SD_OCR_ADDR_OUT_OF_RANGE) == SD_OCR_ADDR_OUT_OF_RANGE)
2731 {
2732 return(SD_ADDR_OUT_OF_RANGE);
2733 }
2734
2735 if((response_r1 & SD_OCR_ADDR_MISALIGNED) == SD_OCR_ADDR_MISALIGNED)
2736 {
2737 return(SD_ADDR_MISALIGNED);
2738 }
2739
2740 if((response_r1 & SD_OCR_BLOCK_LEN_ERR) == SD_OCR_BLOCK_LEN_ERR)
2741 {
2742 return(SD_BLOCK_LEN_ERR);
2743 }
2744
2745 if((response_r1 & SD_OCR_ERASE_SEQ_ERR) == SD_OCR_ERASE_SEQ_ERR)
2746 {
2747 return(SD_ERASE_SEQ_ERR);
2748 }
2749
2750 if((response_r1 & SD_OCR_BAD_ERASE_PARAM) == SD_OCR_BAD_ERASE_PARAM)
2751 {
2752 return(SD_BAD_ERASE_PARAM);
2753 }
2754
2755 if((response_r1 & SD_OCR_WRITE_PROT_VIOLATION) == SD_OCR_WRITE_PROT_VIOLATION)
2756 {
2757 return(SD_WRITE_PROT_VIOLATION);
2758 }
2759
2760 if((response_r1 & SD_OCR_LOCK_UNLOCK_FAILED) == SD_OCR_LOCK_UNLOCK_FAILED)
2761 {
2762 return(SD_LOCK_UNLOCK_FAILED);
2763 }
2764
2765 if((response_r1 & SD_OCR_COM_CRC_FAILED) == SD_OCR_COM_CRC_FAILED)
2766 {
2767 return(SD_COM_CRC_FAILED);
2768 }
2769
2770 if((response_r1 & SD_OCR_ILLEGAL_CMD) == SD_OCR_ILLEGAL_CMD)
2771 {
2772 return(SD_ILLEGAL_CMD);
2773 }
2774
2775 if((response_r1 & SD_OCR_CARD_ECC_FAILED) == SD_OCR_CARD_ECC_FAILED)
2776 {
2777 return(SD_CARD_ECC_FAILED);
2778 }
2779
2780 if((response_r1 & SD_OCR_CC_ERROR) == SD_OCR_CC_ERROR)
2781 {
2782 return(SD_CC_ERROR);
2783 }
2784
2785 if((response_r1 & SD_OCR_GENERAL_UNKNOWN_ERROR) == SD_OCR_GENERAL_UNKNOWN_ERROR)
2786 {
2787 return(SD_GENERAL_UNKNOWN_ERROR);
2788 }
2789
2790 if((response_r1 & SD_OCR_STREAM_READ_UNDERRUN) == SD_OCR_STREAM_READ_UNDERRUN)
2791 {
2792 return(SD_STREAM_READ_UNDERRUN);
2793 }
2794
2795 if((response_r1 & SD_OCR_STREAM_WRITE_OVERRUN) == SD_OCR_STREAM_WRITE_OVERRUN)
2796 {
2797 return(SD_STREAM_WRITE_OVERRUN);
2798 }
2799
2800 if((response_r1 & SD_OCR_CID_CSD_OVERWRITE) == SD_OCR_CID_CSD_OVERWRITE)
2801 {
2802 return(SD_CID_CSD_OVERWRITE);
2803 }
2804
2805 if((response_r1 & SD_OCR_WP_ERASE_SKIP) == SD_OCR_WP_ERASE_SKIP)
2806 {
2807 return(SD_WP_ERASE_SKIP);
2808 }
2809
2810 if((response_r1 & SD_OCR_CARD_ECC_DISABLED) == SD_OCR_CARD_ECC_DISABLED)
2811 {
2812 return(SD_CARD_ECC_DISABLED);
2813 }
2814
2815 if((response_r1 & SD_OCR_ERASE_RESET) == SD_OCR_ERASE_RESET)
2816 {
2817 return(SD_ERASE_RESET);
2818 }
2819
2820 if((response_r1 & SD_OCR_AKE_SEQ_ERROR) == SD_OCR_AKE_SEQ_ERROR)
2821 {
2822 return(SD_AKE_SEQ_ERROR);
2823 }
2824
2825 return errorstate;
2826 }
2827
2828 /**
2829 * @brief Checks for error conditions for R3 (OCR) response.
2830 * @param hsd: SD handle
2831 * @retval SD Card error state
2832 */
2833 static HAL_SD_ErrorTypedef SD_CmdResp3Error(SD_HandleTypeDef *hsd)
2834 {
2835 HAL_SD_ErrorTypedef errorstate = SD_OK;
2836
2837 while (!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT))
2838 {
2839 }
2840
2841 if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CTIMEOUT))
2842 {
2843 errorstate = SD_CMD_RSP_TIMEOUT;
2844
2845 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_CTIMEOUT);
2846
2847 return errorstate;
2848 }
2849
2850 /* Clear all the static flags */
2851 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2852
2853 return errorstate;
2854 }
2855
2856 /**
2857 * @brief Checks for error conditions for R2 (CID or CSD) response.
2858 * @param hsd: SD handle
2859 * @retval SD Card error state
2860 */
2861 static HAL_SD_ErrorTypedef SD_CmdResp2Error(SD_HandleTypeDef *hsd)
2862 {
2863 HAL_SD_ErrorTypedef errorstate = SD_OK;
2864
2865 while (!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT))
2866 {
2867 }
2868
2869 if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CTIMEOUT))
2870 {
2871 errorstate = SD_CMD_RSP_TIMEOUT;
2872
2873 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_CTIMEOUT);
2874
2875 return errorstate;
2876 }
2877 else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CCRCFAIL))
2878 {
2879 errorstate = SD_CMD_CRC_FAIL;
2880
2881 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_CCRCFAIL);
2882
2883 return errorstate;
2884 }
2885 else
2886 {
2887 /* No error flag set */
2888 }
2889
2890 /* Clear all the static flags */
2891 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2892
2893 return errorstate;
2894 }
2895
2896 /**
2897 * @brief Checks for error conditions for R6 (RCA) response.
2898 * @param hsd: SD handle
2899 * @param SD_CMD: The sent command index
2900 * @param pRCA: Pointer to the variable that will contain the SD card relative
2901 * address RCA
2902 * @retval SD Card error state
2903 */
2904 static HAL_SD_ErrorTypedef SD_CmdResp6Error(SD_HandleTypeDef *hsd, uint8_t SD_CMD, uint16_t *pRCA)
2905 {
2906 HAL_SD_ErrorTypedef errorstate = SD_OK;
2907 uint32_t response_r1;
2908
2909 while(!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT))
2910 {
2911 }
2912
2913 if(__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CTIMEOUT))
2914 {
2915 errorstate = SD_CMD_RSP_TIMEOUT;
2916
2917 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_CTIMEOUT);
2918
2919 return errorstate;
2920 }
2921 else if(__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CCRCFAIL))
2922 {
2923 errorstate = SD_CMD_CRC_FAIL;
2924
2925 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_CCRCFAIL);
2926
2927 return errorstate;
2928 }
2929 else
2930 {
2931 /* No error flag set */
2932 }
2933
2934 /* Check response received is of desired command */
2935 if(SDMMC_GetCommandResponse(hsd->Instance) != SD_CMD)
2936 {
2937 errorstate = SD_ILLEGAL_CMD;
2938
2939 return errorstate;
2940 }
2941
2942 /* Clear all the static flags */
2943 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2944
2945 /* We have received response, retrieve it. */
2946 response_r1 = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
2947
2948 if((response_r1 & (SD_R6_GENERAL_UNKNOWN_ERROR | SD_R6_ILLEGAL_CMD | SD_R6_COM_CRC_FAILED)) == SD_ALLZERO)
2949 {
2950 *pRCA = (uint16_t) (response_r1 >> 16);
2951
2952 return errorstate;
2953 }
2954
2955 if((response_r1 & SD_R6_GENERAL_UNKNOWN_ERROR) == SD_R6_GENERAL_UNKNOWN_ERROR)
2956 {
2957 return(SD_GENERAL_UNKNOWN_ERROR);
2958 }
2959
2960 if((response_r1 & SD_R6_ILLEGAL_CMD) == SD_R6_ILLEGAL_CMD)
2961 {
2962 return(SD_ILLEGAL_CMD);
2963 }
2964
2965 if((response_r1 & SD_R6_COM_CRC_FAILED) == SD_R6_COM_CRC_FAILED)
2966 {
2967 return(SD_COM_CRC_FAILED);
2968 }
2969
2970 return errorstate;
2971 }
2972
2973 /**
2974 * @brief Enables the SDMMC wide bus mode.
2975 * @param hsd: SD handle
2976 * @retval SD Card error state
2977 */
2978 static HAL_SD_ErrorTypedef SD_WideBus_Enable(SD_HandleTypeDef *hsd)
2979 {
2980 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure;
2981 HAL_SD_ErrorTypedef errorstate = SD_OK;
2982
2983 uint32_t scr[2] = {0, 0};
2984
2985 if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SD_CARD_LOCKED) == SD_CARD_LOCKED)
2986 {
2987 errorstate = SD_LOCK_UNLOCK_FAILED;
2988
2989 return errorstate;
2990 }
2991
2992 /* Get SCR Register */
2993 errorstate = SD_FindSCR(hsd, scr);
2994
2995 if(errorstate != SD_OK)
2996 {
2997 return errorstate;
2998 }
2999
3000 /* If requested card supports wide bus operation */
3001 if((scr[1] & SD_WIDE_BUS_SUPPORT) != SD_ALLZERO)
3002 {
3003 /* Send CMD55 APP_CMD with argument as card's RCA.*/
3004 sdmmc_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16);
3005 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD;
3006 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
3007 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
3008 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
3009 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
3010
3011 /* Check for error conditions */
3012 errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
3013
3014 if(errorstate != SD_OK)
3015 {
3016 return errorstate;
3017 }
3018
3019 /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
3020 sdmmc_cmdinitstructure.Argument = 2;
3021 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH;
3022 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
3023
3024 /* Check for error conditions */
3025 errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_SD_SET_BUSWIDTH);
3026
3027 if(errorstate != SD_OK)
3028 {
3029 return errorstate;
3030 }
3031
3032 return errorstate;
3033 }
3034 else
3035 {
3036 errorstate = SD_REQUEST_NOT_APPLICABLE;
3037
3038 return errorstate;
3039 }
3040 }
3041
3042 /**
3043 * @brief Disables the SDMMC wide bus mode.
3044 * @param hsd: SD handle
3045 * @retval SD Card error state
3046 */
3047 static HAL_SD_ErrorTypedef SD_WideBus_Disable(SD_HandleTypeDef *hsd)
3048 {
3049 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure;
3050 HAL_SD_ErrorTypedef errorstate = SD_OK;
3051
3052 uint32_t scr[2] = {0, 0};
3053
3054 if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SD_CARD_LOCKED) == SD_CARD_LOCKED)
3055 {
3056 errorstate = SD_LOCK_UNLOCK_FAILED;
3057
3058 return errorstate;
3059 }
3060
3061 /* Get SCR Register */
3062 errorstate = SD_FindSCR(hsd, scr);
3063
3064 if(errorstate != SD_OK)
3065 {
3066 return errorstate;
3067 }
3068
3069 /* If requested card supports 1 bit mode operation */
3070 if((scr[1] & SD_SINGLE_BUS_SUPPORT) != SD_ALLZERO)
3071 {
3072 /* Send CMD55 APP_CMD with argument as card's RCA */
3073 sdmmc_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16);
3074 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD;
3075 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
3076 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
3077 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
3078 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
3079
3080 /* Check for error conditions */
3081 errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
3082
3083 if(errorstate != SD_OK)
3084 {
3085 return errorstate;
3086 }
3087
3088 /* Send ACMD6 APP_CMD with argument as 0 for single bus mode */
3089 sdmmc_cmdinitstructure.Argument = 0;
3090 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH;
3091 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
3092
3093 /* Check for error conditions */
3094 errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_SD_SET_BUSWIDTH);
3095
3096 if(errorstate != SD_OK)
3097 {
3098 return errorstate;
3099 }
3100
3101 return errorstate;
3102 }
3103 else
3104 {
3105 errorstate = SD_REQUEST_NOT_APPLICABLE;
3106
3107 return errorstate;
3108 }
3109 }
3110
3111
3112 /**
3113 * @brief Finds the SD card SCR register value.
3114 * @param hsd: SD handle
3115 * @param pSCR: pointer to the buffer that will contain the SCR value
3116 * @retval SD Card error state
3117 */
3118 static HAL_SD_ErrorTypedef SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR)
3119 {
3120 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure;
3121 SDMMC_DataInitTypeDef sdmmc_datainitstructure;
3122 HAL_SD_ErrorTypedef errorstate = SD_OK;
3123 uint32_t index = 0;
3124 uint32_t tempscr[2] = {0, 0};
3125
3126 /* Set Block Size To 8 Bytes */
3127 /* Send CMD55 APP_CMD with argument as card's RCA */
3128 sdmmc_cmdinitstructure.Argument = (uint32_t)8;
3129 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
3130 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
3131 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
3132 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
3133 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
3134
3135 /* Check for error conditions */
3136 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
3137
3138 if(errorstate != SD_OK)
3139 {
3140 return errorstate;
3141 }
3142
3143 /* Send CMD55 APP_CMD with argument as card's RCA */
3144 sdmmc_cmdinitstructure.Argument = (uint32_t)((hsd->RCA) << 16);
3145 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD;
3146 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
3147
3148 /* Check for error conditions */
3149 errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
3150
3151 if(errorstate != SD_OK)
3152 {
3153 return errorstate;
3154 }
3155 sdmmc_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
3156 sdmmc_datainitstructure.DataLength = 8;
3157 sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_8B;
3158 sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
3159 sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
3160 sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE;
3161 SDMMC_DataConfig(hsd->Instance, &sdmmc_datainitstructure);
3162
3163 /* Send ACMD51 SD_APP_SEND_SCR with argument as 0 */
3164 sdmmc_cmdinitstructure.Argument = 0;
3165 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SD_APP_SEND_SCR;
3166 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
3167
3168 /* Check for error conditions */
3169 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SD_APP_SEND_SCR);
3170
3171 if(errorstate != SD_OK)
3172 {
3173 return errorstate;
3174 }
3175
3176 while(!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND))
3177 {
3178 if(__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL))
3179 {
3180 *(tempscr + index) = SDMMC_ReadFIFO(hsd->Instance);
3181 index++;
3182 }
3183 }
3184
3185 if(__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
3186 {
3187 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
3188
3189 errorstate = SD_DATA_TIMEOUT;
3190
3191 return errorstate;
3192 }
3193 else if(__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
3194 {
3195 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
3196
3197 errorstate = SD_DATA_CRC_FAIL;
3198
3199 return errorstate;
3200 }
3201 else if(__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
3202 {
3203 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
3204
3205 errorstate = SD_RX_OVERRUN;
3206
3207 return errorstate;
3208 }
3209 else
3210 {
3211 /* No error flag set */
3212 }
3213
3214 /* Clear all the static flags */
3215 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
3216
3217 *(pSCR + 1) = ((tempscr[0] & SD_0TO7BITS) << 24) | ((tempscr[0] & SD_8TO15BITS) << 8) |\
3218 ((tempscr[0] & SD_16TO23BITS) >> 8) | ((tempscr[0] & SD_24TO31BITS) >> 24);
3219
3220 *(pSCR) = ((tempscr[1] & SD_0TO7BITS) << 24) | ((tempscr[1] & SD_8TO15BITS) << 8) |\
3221 ((tempscr[1] & SD_16TO23BITS) >> 8) | ((tempscr[1] & SD_24TO31BITS) >> 24);
3222
3223 return errorstate;
3224 }
3225
3226 /**
3227 * @brief Checks if the SD card is in programming state.
3228 * @param hsd: SD handle
3229 * @param pStatus: pointer to the variable that will contain the SD card state
3230 * @retval SD Card error state
3231 */
3232 static HAL_SD_ErrorTypedef SD_IsCardProgramming(SD_HandleTypeDef *hsd, uint8_t *pStatus)
3233 {
3234 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure;
3235 HAL_SD_ErrorTypedef errorstate = SD_OK;
3236 __IO uint32_t responseR1 = 0;
3237
3238 sdmmc_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16);
3239 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SEND_STATUS;
3240 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
3241 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
3242 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
3243 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
3244
3245 while(!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT))
3246 {
3247 }
3248
3249 if(__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CTIMEOUT))
3250 {
3251 errorstate = SD_CMD_RSP_TIMEOUT;
3252
3253 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_CTIMEOUT);
3254
3255 return errorstate;
3256 }
3257 else if(__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CCRCFAIL))
3258 {
3259 errorstate = SD_CMD_CRC_FAIL;
3260
3261 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_CCRCFAIL);
3262
3263 return errorstate;
3264 }
3265 else
3266 {
3267 /* No error flag set */
3268 }
3269
3270 /* Check response received is of desired command */
3271 if((uint32_t)SDMMC_GetCommandResponse(hsd->Instance) != SD_CMD_SEND_STATUS)
3272 {
3273 errorstate = SD_ILLEGAL_CMD;
3274
3275 return errorstate;
3276 }
3277
3278 /* Clear all the static flags */
3279 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
3280
3281
3282 /* We have received response, retrieve it for analysis */
3283 responseR1 = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
3284
3285 /* Find out card status */
3286 *pStatus = (uint8_t)((responseR1 >> 9) & 0x0000000F);
3287
3288 if((responseR1 & SD_OCR_ERRORBITS) == SD_ALLZERO)
3289 {
3290 return errorstate;
3291 }
3292
3293 if((responseR1 & SD_OCR_ADDR_OUT_OF_RANGE) == SD_OCR_ADDR_OUT_OF_RANGE)
3294 {
3295 return(SD_ADDR_OUT_OF_RANGE);
3296 }
3297
3298 if((responseR1 & SD_OCR_ADDR_MISALIGNED) == SD_OCR_ADDR_MISALIGNED)
3299 {
3300 return(SD_ADDR_MISALIGNED);
3301 }
3302
3303 if((responseR1 & SD_OCR_BLOCK_LEN_ERR) == SD_OCR_BLOCK_LEN_ERR)
3304 {
3305 return(SD_BLOCK_LEN_ERR);
3306 }
3307
3308 if((responseR1 & SD_OCR_ERASE_SEQ_ERR) == SD_OCR_ERASE_SEQ_ERR)
3309 {
3310 return(SD_ERASE_SEQ_ERR);
3311 }
3312
3313 if((responseR1 & SD_OCR_BAD_ERASE_PARAM) == SD_OCR_BAD_ERASE_PARAM)
3314 {
3315 return(SD_BAD_ERASE_PARAM);
3316 }
3317
3318 if((responseR1 & SD_OCR_WRITE_PROT_VIOLATION) == SD_OCR_WRITE_PROT_VIOLATION)
3319 {
3320 return(SD_WRITE_PROT_VIOLATION);
3321 }
3322
3323 if((responseR1 & SD_OCR_LOCK_UNLOCK_FAILED) == SD_OCR_LOCK_UNLOCK_FAILED)
3324 {
3325 return(SD_LOCK_UNLOCK_FAILED);
3326 }
3327
3328 if((responseR1 & SD_OCR_COM_CRC_FAILED) == SD_OCR_COM_CRC_FAILED)
3329 {
3330 return(SD_COM_CRC_FAILED);
3331 }
3332
3333 if((responseR1 & SD_OCR_ILLEGAL_CMD) == SD_OCR_ILLEGAL_CMD)
3334 {
3335 return(SD_ILLEGAL_CMD);
3336 }
3337
3338 if((responseR1 & SD_OCR_CARD_ECC_FAILED) == SD_OCR_CARD_ECC_FAILED)
3339 {
3340 return(SD_CARD_ECC_FAILED);
3341 }
3342
3343 if((responseR1 & SD_OCR_CC_ERROR) == SD_OCR_CC_ERROR)
3344 {
3345 return(SD_CC_ERROR);
3346 }
3347
3348 if((responseR1 & SD_OCR_GENERAL_UNKNOWN_ERROR) == SD_OCR_GENERAL_UNKNOWN_ERROR)
3349 {
3350 return(SD_GENERAL_UNKNOWN_ERROR);
3351 }
3352
3353 if((responseR1 & SD_OCR_STREAM_READ_UNDERRUN) == SD_OCR_STREAM_READ_UNDERRUN)
3354 {
3355 return(SD_STREAM_READ_UNDERRUN);
3356 }
3357
3358 if((responseR1 & SD_OCR_STREAM_WRITE_OVERRUN) == SD_OCR_STREAM_WRITE_OVERRUN)
3359 {
3360 return(SD_STREAM_WRITE_OVERRUN);
3361 }
3362
3363 if((responseR1 & SD_OCR_CID_CSD_OVERWRITE) == SD_OCR_CID_CSD_OVERWRITE)
3364 {
3365 return(SD_CID_CSD_OVERWRITE);
3366 }
3367
3368 if((responseR1 & SD_OCR_WP_ERASE_SKIP) == SD_OCR_WP_ERASE_SKIP)
3369 {
3370 return(SD_WP_ERASE_SKIP);
3371 }
3372
3373 if((responseR1 & SD_OCR_CARD_ECC_DISABLED) == SD_OCR_CARD_ECC_DISABLED)
3374 {
3375 return(SD_CARD_ECC_DISABLED);
3376 }
3377
3378 if((responseR1 & SD_OCR_ERASE_RESET) == SD_OCR_ERASE_RESET)
3379 {
3380 return(SD_ERASE_RESET);
3381 }
3382
3383 if((responseR1 & SD_OCR_AKE_SEQ_ERROR) == SD_OCR_AKE_SEQ_ERROR)
3384 {
3385 return(SD_AKE_SEQ_ERROR);
3386 }
3387
3388 return errorstate;
3389 }
3390
3391 /**
3392 * @}
3393 */
3394
3395 #endif /* HAL_SD_MODULE_ENABLED */
3396
3397 /**
3398 * @}
3399 */
3400
3401 /**
3402 * @}
3403 */
3404
3405 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/