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