source: asp3_wo_tecs/trunk/arch/arm_m_gcc/stm32f4xx_stm32cube/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sd.c@ 303

Last change on this file since 303 was 303, checked in by ertl-honda, 7 years ago

nucleo_f401re依存部の追加

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