comparison Common/Drivers/STM32F4xx_HAL_DRIVER_v120/Src/stm32f4xx_hal_sd.c @ 38:5f11787b4f42

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