2 ******************************************************************************
3 * @file stm32f7xx_hal_sd.c
4 * @author MCD Application Team
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
16 ==============================================================================
17 ##### How to use this driver #####
18 ==============================================================================
20 This driver implements a high level communication layer for read and write from/to
21 this memory. The needed STM32 hardware resources (SDMMC and GPIO) are performed by
22 the user in HAL_SD_MspInit() function (MSP layer).
23 Basically, the MSP layer configuration should be the same as we provide in the
25 You can easily tailor this configuration according to hardware resources.
28 This driver is a generic layered driver for SDMMC memories which uses the HAL
29 SDMMC driver functions to interface with SD and uSD cards devices.
30 It is used as follows:
32 (#)Initialize the SDMMC low level resources by implement the HAL_SD_MspInit() API:
33 (##) Enable the SDMMC interface clock using __HAL_RCC_SDMMC_CLK_ENABLE();
34 (##) SDMMC pins configuration for SD card
35 (+++) Enable the clock for the SDMMC GPIOs using the functions __HAL_RCC_GPIOx_CLK_ENABLE();
36 (+++) Configure these SDMMC pins as alternate function pull-up using HAL_GPIO_Init()
37 and according to your pin assignment;
38 (##) DMA Configuration if you need to use DMA process (HAL_SD_ReadBlocks_DMA()
39 and HAL_SD_WriteBlocks_DMA() APIs).
40 (+++) Enable the DMAx interface clock using __HAL_RCC_DMAx_CLK_ENABLE();
41 (+++) Configure the DMA using the function HAL_DMA_Init() with predeclared and filled.
42 (##) NVIC configuration if you need to use interrupt process when using DMA transfer.
43 (+++) Configure the SDMMC and DMA interrupt priorities using functions
44 HAL_NVIC_SetPriority(); DMA priority is superior to SDMMC's priority
45 (+++) Enable the NVIC DMA and SDMMC IRQs using function HAL_NVIC_EnableIRQ()
46 (+++) SDMMC interrupts are managed using the macros __HAL_SD_SDMMC_ENABLE_IT()
47 and __HAL_SD_SDMMC_DISABLE_IT() inside the communication process.
48 (+++) SDMMC interrupts pending bits are managed using the macros __HAL_SD_SDMMC_GET_IT()
49 and __HAL_SD_SDMMC_CLEAR_IT()
50 (#) At this stage, you can perform SD read/write/erase operations after SD card initialization
53 *** SD Card Initialization and configuration ***
54 ================================================
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:
60 (#) Apply the SD Card initialization process at 400KHz and check the SD Card
61 type (Standard Capacity or High Capacity). You can change or adapt this
62 frequency by adjusting the "ClockDiv" field.
63 The SD Card frequency (SDMMC_CK) is computed as follows:
65 SDMMC_CK = SDMMCCLK / (ClockDiv + 2)
67 In initialization mode and according to the SD Card standard,
68 make sure that the SDMMC_CK frequency doesn't exceed 400KHz.
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
74 -@- These information are stored in SD handle structure in case of future use.
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
79 In transfer mode and according to the SD Card standard, make sure that the
80 SDMMC_CK frequency doesn't exceed 25MHz and 50MHz in High-speed mode switch.
81 To be able to use a frequency higher than 24MHz, you should use the SDMMC
82 peripheral in bypass mode. Refer to the corresponding reference manual
85 (#) Select the corresponding SD Card according to the address read with the step 2.
87 (#) Configure the SD Card in wide bus mode: 4-bits data.
89 *** SD Card Read operation ***
90 ==============================
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
95 You can choose either one block read operation or multiple block read operation
96 by adjusting the "NumberOfBlocks" parameter.
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.
106 *** SD Card Write operation ***
107 ===============================
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.
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
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.
123 *** SD card status ***
124 ======================
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()
132 *** SD HAL driver macros list ***
133 ==================================
135 Below the list of most used macros in SD HAL driver.
137 (+) __HAL_SD_SDMMC_ENABLE : Enable the SD device
138 (+) __HAL_SD_SDMMC_DISABLE : Disable the SD device
139 (+) __HAL_SD_SDMMC_DMA_ENABLE: Enable the SDMMC DMA transfer
140 (+) __HAL_SD_SDMMC_DMA_DISABLE: Disable the SDMMC DMA transfer
141 (+) __HAL_SD_SDMMC_ENABLE_IT: Enable the SD device interrupt
142 (+) __HAL_SD_SDMMC_DISABLE_IT: Disable the SD device interrupt
143 (+) __HAL_SD_SDMMC_GET_FLAG:Check whether the specified SD flag is set or not
144 (+) __HAL_SD_SDMMC_CLEAR_FLAG: Clear the SD's pending flags
146 (@) You can refer to the SD HAL driver header file for more useful macros
149 ******************************************************************************
152 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
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.
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.
176 ******************************************************************************
179 /* Includes ------------------------------------------------------------------*/
180 #include "stm32f7xx_hal.h"
182 /** @addtogroup STM32F7xx_HAL_Driver
190 #ifdef HAL_SD_MODULE_ENABLED
192 /* Private typedef -----------------------------------------------------------*/
193 /* Private define ------------------------------------------------------------*/
194 /** @addtogroup SD_Private_Defines
198 * @brief SDMMC Data block size
200 #define DATA_BLOCK_SIZE ((uint32_t)(9 << 4))
202 * @brief SDMMC Static flags, Timeout, FIFO Address
204 #define SDMMC_STATIC_FLAGS ((uint32_t)(SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_CTIMEOUT |\
205 SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_TXUNDERR | SDMMC_FLAG_RXOVERR |\
206 SDMMC_FLAG_CMDREND | SDMMC_FLAG_CMDSENT | SDMMC_FLAG_DATAEND |\
209 #define SDMMC_CMD0TIMEOUT ((uint32_t)0x00010000U)
212 * @brief Mask for errors Card Status R1 (OCR Register)
214 #define SD_OCR_ADDR_OUT_OF_RANGE ((uint32_t)0x80000000U)
215 #define SD_OCR_ADDR_MISALIGNED ((uint32_t)0x40000000U)
216 #define SD_OCR_BLOCK_LEN_ERR ((uint32_t)0x20000000U)
217 #define SD_OCR_ERASE_SEQ_ERR ((uint32_t)0x10000000U)
218 #define SD_OCR_BAD_ERASE_PARAM ((uint32_t)0x08000000U)
219 #define SD_OCR_WRITE_PROT_VIOLATION ((uint32_t)0x04000000U)
220 #define SD_OCR_LOCK_UNLOCK_FAILED ((uint32_t)0x01000000U)
221 #define SD_OCR_COM_CRC_FAILED ((uint32_t)0x00800000U)
222 #define SD_OCR_ILLEGAL_CMD ((uint32_t)0x00400000U)
223 #define SD_OCR_CARD_ECC_FAILED ((uint32_t)0x00200000U)
224 #define SD_OCR_CC_ERROR ((uint32_t)0x00100000U)
225 #define SD_OCR_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00080000U)
226 #define SD_OCR_STREAM_READ_UNDERRUN ((uint32_t)0x00040000U)
227 #define SD_OCR_STREAM_WRITE_OVERRUN ((uint32_t)0x00020000U)
228 #define SD_OCR_CID_CSD_OVERWRITE ((uint32_t)0x00010000U)
229 #define SD_OCR_WP_ERASE_SKIP ((uint32_t)0x00008000U)
230 #define SD_OCR_CARD_ECC_DISABLED ((uint32_t)0x00004000U)
231 #define SD_OCR_ERASE_RESET ((uint32_t)0x00002000U)
232 #define SD_OCR_AKE_SEQ_ERROR ((uint32_t)0x00000008U)
233 #define SD_OCR_ERRORBITS ((uint32_t)0xFDFFE008U)
236 * @brief Masks for R6 Response
238 #define SD_R6_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00002000U)
239 #define SD_R6_ILLEGAL_CMD ((uint32_t)0x00004000U)
240 #define SD_R6_COM_CRC_FAILED ((uint32_t)0x00008000U)
242 #define SD_VOLTAGE_WINDOW_SD ((uint32_t)0x80100000U)
243 #define SD_HIGH_CAPACITY ((uint32_t)0x40000000U)
244 #define SD_STD_CAPACITY ((uint32_t)0x00000000U)
245 #define SD_CHECK_PATTERN ((uint32_t)0x000001AAU)
247 #define SD_MAX_VOLT_TRIAL ((uint32_t)0x0000FFFFU)
248 #define SD_ALLZERO ((uint32_t)0x00000000U)
250 #define SD_WIDE_BUS_SUPPORT ((uint32_t)0x00040000U)
251 #define SD_SINGLE_BUS_SUPPORT ((uint32_t)0x00010000U)
252 #define SD_CARD_LOCKED ((uint32_t)0x02000000U)
254 #define SD_DATATIMEOUT ((uint32_t)0xFFFFFFFFU)
255 #define SD_0TO7BITS ((uint32_t)0x000000FFU)
256 #define SD_8TO15BITS ((uint32_t)0x0000FF00U)
257 #define SD_16TO23BITS ((uint32_t)0x00FF0000U)
258 #define SD_24TO31BITS ((uint32_t)0xFF000000U)
259 #define SD_MAX_DATA_LENGTH ((uint32_t)0x01FFFFFFU)
261 #define SD_HALFFIFO ((uint32_t)0x00000008U)
262 #define SD_HALFFIFOBYTES ((uint32_t)0x00000020U)
265 * @brief Command Class Supported
267 #define SD_CCCC_LOCK_UNLOCK ((uint32_t)0x00000080U)
268 #define SD_CCCC_WRITE_PROT ((uint32_t)0x00000040U)
269 #define SD_CCCC_ERASE ((uint32_t)0x00000020U)
272 * @brief Following commands are SD Card Specific commands.
273 * SDMMC_APP_CMD should be sent before sending these commands.
275 #define SD_SDMMC_SEND_IF_COND ((uint32_t)SD_CMD_HS_SEND_EXT_CSD)
280 /* Private macro -------------------------------------------------------------*/
281 /* Private variables ---------------------------------------------------------*/
282 /* Private function prototypes -----------------------------------------------*/
283 /* Private functions ---------------------------------------------------------*/
284 /** @defgroup SD_Private_Functions SD Private Functions
287 static HAL_SD_ErrorTypedef
SD_Initialize_Cards(SD_HandleTypeDef
*hsd
);
288 static HAL_SD_ErrorTypedef
SD_Select_Deselect(SD_HandleTypeDef
*hsd
, uint64_t addr
);
289 static HAL_SD_ErrorTypedef
SD_PowerON(SD_HandleTypeDef
*hsd
);
290 static HAL_SD_ErrorTypedef
SD_PowerOFF(SD_HandleTypeDef
*hsd
);
291 static HAL_SD_ErrorTypedef
SD_SendStatus(SD_HandleTypeDef
*hsd
, uint32_t *pCardStatus
);
292 static HAL_SD_CardStateTypedef
SD_GetState(SD_HandleTypeDef
*hsd
);
293 static HAL_SD_ErrorTypedef
SD_IsCardProgramming(SD_HandleTypeDef
*hsd
, uint8_t *pStatus
);
294 static HAL_SD_ErrorTypedef
SD_CmdError(SD_HandleTypeDef
*hsd
);
295 static HAL_SD_ErrorTypedef
SD_CmdResp1Error(SD_HandleTypeDef
*hsd
, uint8_t SD_CMD
);
296 static HAL_SD_ErrorTypedef
SD_CmdResp7Error(SD_HandleTypeDef
*hsd
);
297 static HAL_SD_ErrorTypedef
SD_CmdResp3Error(SD_HandleTypeDef
*hsd
);
298 static HAL_SD_ErrorTypedef
SD_CmdResp2Error(SD_HandleTypeDef
*hsd
);
299 static HAL_SD_ErrorTypedef
SD_CmdResp6Error(SD_HandleTypeDef
*hsd
, uint8_t SD_CMD
, uint16_t *pRCA
);
300 static HAL_SD_ErrorTypedef
SD_WideBus_Enable(SD_HandleTypeDef
*hsd
);
301 static HAL_SD_ErrorTypedef
SD_WideBus_Disable(SD_HandleTypeDef
*hsd
);
302 static HAL_SD_ErrorTypedef
SD_FindSCR(SD_HandleTypeDef
*hsd
, uint32_t *pSCR
);
303 static void SD_DMA_RxCplt(DMA_HandleTypeDef
*hdma
);
304 static void SD_DMA_RxError(DMA_HandleTypeDef
*hdma
);
305 static void SD_DMA_TxCplt(DMA_HandleTypeDef
*hdma
);
306 static void SD_DMA_TxError(DMA_HandleTypeDef
*hdma
);
310 /* Exported functions --------------------------------------------------------*/
311 /** @addtogroup SD_Exported_Functions
315 /** @addtogroup SD_Exported_Functions_Group1
316 * @brief Initialization and de-initialization functions
319 ==============================================================================
320 ##### Initialization and de-initialization functions #####
321 ==============================================================================
323 This section provides functions allowing to initialize/de-initialize the SD
324 card device to be ready for use.
332 * @brief Initializes the SD card according to the specified parameters in the
333 SD_HandleTypeDef and create the associated handle.
334 * @param hsd: SD handle
335 * @param SDCardInfo: HAL_SD_CardInfoTypedef structure for SD card information
336 * @retval HAL SD error state
338 HAL_SD_ErrorTypedef
HAL_SD_Init(SD_HandleTypeDef
*hsd
, HAL_SD_CardInfoTypedef
*SDCardInfo
)
340 __IO HAL_SD_ErrorTypedef errorstate
= SD_OK
;
341 SD_InitTypeDef tmpinit
;
343 /* Allocate lock resource and initialize it */
344 hsd
->Lock
= HAL_UNLOCKED
;
346 /* Initialize the low level hardware (MSP) */
349 /* Default SDMMC peripheral configuration for SD card initialization */
350 tmpinit
.ClockEdge
= SDMMC_CLOCK_EDGE_RISING
;
351 tmpinit
.ClockBypass
= SDMMC_CLOCK_BYPASS_DISABLE
;
352 tmpinit
.ClockPowerSave
= SDMMC_CLOCK_POWER_SAVE_DISABLE
;
353 tmpinit
.BusWide
= SDMMC_BUS_WIDE_1B
;
354 tmpinit
.HardwareFlowControl
= SDMMC_HARDWARE_FLOW_CONTROL_DISABLE
;
355 tmpinit
.ClockDiv
= SDMMC_INIT_CLK_DIV
;
357 /* Initialize SDMMC peripheral interface with default configuration */
358 SDMMC_Init(hsd
->Instance
, tmpinit
);
360 /* Identify card operating voltage */
361 errorstate
= SD_PowerON(hsd
);
363 if(errorstate
!= SD_OK
)
368 /* Initialize the present SDMMC card(s) and put them in idle state */
369 errorstate
= SD_Initialize_Cards(hsd
);
371 if (errorstate
!= SD_OK
)
376 /* Read CSD/CID MSD registers */
377 errorstate
= HAL_SD_Get_CardInfo(hsd
, SDCardInfo
);
379 if (errorstate
== SD_OK
)
381 /* Select the Card */
382 errorstate
= SD_Select_Deselect(hsd
, (uint32_t)(((uint32_t)SDCardInfo
->RCA
) << 16));
385 /* Configure SDMMC peripheral interface */
386 SDMMC_Init(hsd
->Instance
, hsd
->Init
);
392 * @brief De-Initializes the SD card.
393 * @param hsd: SD handle
396 HAL_StatusTypeDef
HAL_SD_DeInit(SD_HandleTypeDef
*hsd
)
399 /* Set SD power state to off */
402 /* De-Initialize the MSP layer */
403 HAL_SD_MspDeInit(hsd
);
410 * @brief Initializes the SD MSP.
411 * @param hsd: SD handle
414 __weak
void HAL_SD_MspInit(SD_HandleTypeDef
*hsd
)
416 /* Prevent unused argument(s) compilation warning */
419 /* NOTE : This function Should not be modified, when the callback is needed,
420 the HAL_SD_MspInit could be implemented in the user file
425 * @brief De-Initialize SD MSP.
426 * @param hsd: SD handle
429 __weak
void HAL_SD_MspDeInit(SD_HandleTypeDef
*hsd
)
431 /* Prevent unused argument(s) compilation warning */
434 /* NOTE : This function Should not be modified, when the callback is needed,
435 the HAL_SD_MspDeInit could be implemented in the user file
443 /** @addtogroup SD_Exported_Functions_Group2
444 * @brief Data transfer functions
447 ==============================================================================
448 ##### IO operation functions #####
449 ==============================================================================
451 This subsection provides a set of functions allowing to manage the data
452 transfer from/to SD card.
459 * @brief Reads block(s) from a specified address in a card. The Data transfer
460 * is managed by polling mode.
461 * @param hsd: SD handle
462 * @param pReadBuffer: pointer to the buffer that will contain the received data
463 * @param ReadAddr: Address from where data is to be read
464 * @param BlockSize: SD card Data block size
465 * @note BlockSize must be 512 bytes.
466 * @param NumberOfBlocks: Number of SD blocks to read
467 * @retval SD Card error state
469 HAL_SD_ErrorTypedef
HAL_SD_ReadBlocks(SD_HandleTypeDef
*hsd
, uint32_t *pReadBuffer
, uint64_t ReadAddr
, uint32_t BlockSize
, uint32_t NumberOfBlocks
)
471 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure
;
472 SDMMC_DataInitTypeDef sdmmc_datainitstructure
;
473 HAL_SD_ErrorTypedef errorstate
= SD_OK
;
474 uint32_t count
= 0, *tempbuff
= (uint32_t *)pReadBuffer
;
476 /* Initialize data control register */
477 hsd
->Instance
->DCTRL
= 0;
479 if (hsd
->CardType
== HIGH_CAPACITY_SD_CARD
)
485 /* Set Block Size for Card */
486 sdmmc_cmdinitstructure
.Argument
= (uint32_t) BlockSize
;
487 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_SET_BLOCKLEN
;
488 sdmmc_cmdinitstructure
.Response
= SDMMC_RESPONSE_SHORT
;
489 sdmmc_cmdinitstructure
.WaitForInterrupt
= SDMMC_WAIT_NO
;
490 sdmmc_cmdinitstructure
.CPSM
= SDMMC_CPSM_ENABLE
;
491 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
493 /* Check for error conditions */
494 errorstate
= SD_CmdResp1Error(hsd
, SD_CMD_SET_BLOCKLEN
);
496 if (errorstate
!= SD_OK
)
501 /* Configure the SD DPSM (Data Path State Machine) */
502 sdmmc_datainitstructure
.DataTimeOut
= SD_DATATIMEOUT
;
503 sdmmc_datainitstructure
.DataLength
= NumberOfBlocks
* BlockSize
;
504 sdmmc_datainitstructure
.DataBlockSize
= DATA_BLOCK_SIZE
;
505 sdmmc_datainitstructure
.TransferDir
= SDMMC_TRANSFER_DIR_TO_SDMMC
;
506 sdmmc_datainitstructure
.TransferMode
= SDMMC_TRANSFER_MODE_BLOCK
;
507 sdmmc_datainitstructure
.DPSM
= SDMMC_DPSM_ENABLE
;
508 SDMMC_DataConfig(hsd
->Instance
, &sdmmc_datainitstructure
);
510 if(NumberOfBlocks
> 1)
512 /* Send CMD18 READ_MULT_BLOCK with argument data address */
513 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_READ_MULT_BLOCK
;
517 /* Send CMD17 READ_SINGLE_BLOCK */
518 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_READ_SINGLE_BLOCK
;
521 sdmmc_cmdinitstructure
.Argument
= (uint32_t)ReadAddr
;
522 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
524 /* Read block(s) in polling mode */
525 if(NumberOfBlocks
> 1)
527 /* Check for error conditions */
528 errorstate
= SD_CmdResp1Error(hsd
, SD_CMD_READ_MULT_BLOCK
);
530 if (errorstate
!= SD_OK
)
535 /* Poll on SDMMC flags */
536 while(!__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_RXOVERR
| SDMMC_FLAG_DCRCFAIL
| SDMMC_FLAG_DTIMEOUT
| SDMMC_FLAG_DATAEND
))
538 if (__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_RXFIFOHF
))
540 /* Read data from SDMMC Rx FIFO */
541 for (count
= 0; count
< 8; count
++)
543 *(tempbuff
+ count
) = SDMMC_ReadFIFO(hsd
->Instance
);
552 /* Check for error conditions */
553 errorstate
= SD_CmdResp1Error(hsd
, SD_CMD_READ_SINGLE_BLOCK
);
555 if (errorstate
!= SD_OK
)
560 /* In case of single block transfer, no need of stop transfer at all */
561 while(!__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_RXOVERR
| SDMMC_FLAG_DCRCFAIL
| SDMMC_FLAG_DTIMEOUT
| SDMMC_FLAG_DBCKEND
))
563 if (__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_RXFIFOHF
))
565 /* Read data from SDMMC Rx FIFO */
566 for (count
= 0; count
< 8; count
++)
568 *(tempbuff
+ count
) = SDMMC_ReadFIFO(hsd
->Instance
);
576 /* Send stop transmission command in case of multiblock read */
577 if (__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_DATAEND
) && (NumberOfBlocks
> 1))
579 if ((hsd
->CardType
== STD_CAPACITY_SD_CARD_V1_1
) ||\
580 (hsd
->CardType
== STD_CAPACITY_SD_CARD_V2_0
) ||\
581 (hsd
->CardType
== HIGH_CAPACITY_SD_CARD
))
583 /* Send stop transmission command */
584 errorstate
= HAL_SD_StopTransfer(hsd
);
588 /* Get error state */
589 if (__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_DTIMEOUT
))
591 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_FLAG_DTIMEOUT
);
593 errorstate
= SD_DATA_TIMEOUT
;
597 else if (__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_DCRCFAIL
))
599 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_FLAG_DCRCFAIL
);
601 errorstate
= SD_DATA_CRC_FAIL
;
605 else if (__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_RXOVERR
))
607 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_FLAG_RXOVERR
);
609 errorstate
= SD_RX_OVERRUN
;
615 /* No error flag set */
618 count
= SD_DATATIMEOUT
;
620 /* Empty FIFO if there is still any data */
621 while ((__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_RXDAVL
)) && (count
> 0))
623 *tempbuff
= SDMMC_ReadFIFO(hsd
->Instance
);
628 /* Clear all the static flags */
629 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
635 * @brief Allows to write block(s) to a specified address in a card. The Data
636 * transfer is managed by polling mode.
637 * @param hsd: SD handle
638 * @param pWriteBuffer: pointer to the buffer that will contain the data to transmit
639 * @param WriteAddr: Address from where data is to be written
640 * @param BlockSize: SD card Data block size
641 * @note BlockSize must be 512 bytes.
642 * @param NumberOfBlocks: Number of SD blocks to write
643 * @retval SD Card error state
645 HAL_SD_ErrorTypedef
HAL_SD_WriteBlocks(SD_HandleTypeDef
*hsd
, uint32_t *pWriteBuffer
, uint64_t WriteAddr
, uint32_t BlockSize
, uint32_t NumberOfBlocks
)
647 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure
;
648 SDMMC_DataInitTypeDef sdmmc_datainitstructure
;
649 HAL_SD_ErrorTypedef errorstate
= SD_OK
;
650 uint32_t totalnumberofbytes
= 0, bytestransferred
= 0, count
= 0, restwords
= 0;
651 uint32_t *tempbuff
= (uint32_t *)pWriteBuffer
;
652 uint8_t cardstate
= 0;
654 /* Initialize data control register */
655 hsd
->Instance
->DCTRL
= 0;
657 if (hsd
->CardType
== HIGH_CAPACITY_SD_CARD
)
663 /* Set Block Size for Card */
664 sdmmc_cmdinitstructure
.Argument
= (uint32_t)BlockSize
;
665 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_SET_BLOCKLEN
;
666 sdmmc_cmdinitstructure
.Response
= SDMMC_RESPONSE_SHORT
;
667 sdmmc_cmdinitstructure
.WaitForInterrupt
= SDMMC_WAIT_NO
;
668 sdmmc_cmdinitstructure
.CPSM
= SDMMC_CPSM_ENABLE
;
669 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
671 /* Check for error conditions */
672 errorstate
= SD_CmdResp1Error(hsd
, SD_CMD_SET_BLOCKLEN
);
674 if (errorstate
!= SD_OK
)
679 if(NumberOfBlocks
> 1)
681 /* Send CMD25 WRITE_MULT_BLOCK with argument data address */
682 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_WRITE_MULT_BLOCK
;
686 /* Send CMD24 WRITE_SINGLE_BLOCK */
687 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_WRITE_SINGLE_BLOCK
;
690 sdmmc_cmdinitstructure
.Argument
= (uint32_t)WriteAddr
;
691 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
693 /* Check for error conditions */
694 if(NumberOfBlocks
> 1)
696 errorstate
= SD_CmdResp1Error(hsd
, SD_CMD_WRITE_MULT_BLOCK
);
700 errorstate
= SD_CmdResp1Error(hsd
, SD_CMD_WRITE_SINGLE_BLOCK
);
703 if (errorstate
!= SD_OK
)
708 /* Set total number of bytes to write */
709 totalnumberofbytes
= NumberOfBlocks
* BlockSize
;
711 /* Configure the SD DPSM (Data Path State Machine) */
712 sdmmc_datainitstructure
.DataTimeOut
= SD_DATATIMEOUT
;
713 sdmmc_datainitstructure
.DataLength
= NumberOfBlocks
* BlockSize
;
714 sdmmc_datainitstructure
.DataBlockSize
= SDMMC_DATABLOCK_SIZE_512B
;
715 sdmmc_datainitstructure
.TransferDir
= SDMMC_TRANSFER_DIR_TO_CARD
;
716 sdmmc_datainitstructure
.TransferMode
= SDMMC_TRANSFER_MODE_BLOCK
;
717 sdmmc_datainitstructure
.DPSM
= SDMMC_DPSM_ENABLE
;
718 SDMMC_DataConfig(hsd
->Instance
, &sdmmc_datainitstructure
);
720 /* Write block(s) in polling mode */
721 if(NumberOfBlocks
> 1)
723 while(!__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_TXUNDERR
| SDMMC_FLAG_DCRCFAIL
| SDMMC_FLAG_DTIMEOUT
| SDMMC_FLAG_DATAEND
))
725 if (__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_TXFIFOHE
))
727 if ((totalnumberofbytes
- bytestransferred
) < 32)
729 restwords
= ((totalnumberofbytes
- bytestransferred
) % 4 == 0) ? ((totalnumberofbytes
- bytestransferred
) / 4) : (( totalnumberofbytes
- bytestransferred
) / 4 + 1);
731 /* Write data to SDMMC Tx FIFO */
732 for (count
= 0; count
< restwords
; count
++)
734 SDMMC_WriteFIFO(hsd
->Instance
, tempbuff
);
736 bytestransferred
+= 4;
741 /* Write data to SDMMC Tx FIFO */
742 for (count
= 0; count
< 8; count
++)
744 SDMMC_WriteFIFO(hsd
->Instance
, (tempbuff
+ count
));
748 bytestransferred
+= 32;
755 /* In case of single data block transfer no need of stop command at all */
756 while(!__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_TXUNDERR
| SDMMC_FLAG_DCRCFAIL
| SDMMC_FLAG_DTIMEOUT
| SDMMC_FLAG_DBCKEND
))
758 if (__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_TXFIFOHE
))
760 if ((totalnumberofbytes
- bytestransferred
) < 32)
762 restwords
= ((totalnumberofbytes
- bytestransferred
) % 4 == 0) ? ((totalnumberofbytes
- bytestransferred
) / 4) : (( totalnumberofbytes
- bytestransferred
) / 4 + 1);
764 /* Write data to SDMMC Tx FIFO */
765 for (count
= 0; count
< restwords
; count
++)
767 SDMMC_WriteFIFO(hsd
->Instance
, tempbuff
);
769 bytestransferred
+= 4;
774 /* Write data to SDMMC Tx FIFO */
775 for (count
= 0; count
< 8; count
++)
777 SDMMC_WriteFIFO(hsd
->Instance
, (tempbuff
+ count
));
781 bytestransferred
+= 32;
787 /* Send stop transmission command in case of multiblock write */
788 if (__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_DATAEND
) && (NumberOfBlocks
> 1))
790 if ((hsd
->CardType
== STD_CAPACITY_SD_CARD_V1_1
) || (hsd
->CardType
== STD_CAPACITY_SD_CARD_V2_0
) ||\
791 (hsd
->CardType
== HIGH_CAPACITY_SD_CARD
))
793 /* Send stop transmission command */
794 errorstate
= HAL_SD_StopTransfer(hsd
);
798 /* Get error state */
799 if (__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_DTIMEOUT
))
801 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_FLAG_DTIMEOUT
);
803 errorstate
= SD_DATA_TIMEOUT
;
807 else if (__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_DCRCFAIL
))
809 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_FLAG_DCRCFAIL
);
811 errorstate
= SD_DATA_CRC_FAIL
;
815 else if (__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_TXUNDERR
))
817 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_FLAG_TXUNDERR
);
819 errorstate
= SD_TX_UNDERRUN
;
825 /* No error flag set */
828 /* Clear all the static flags */
829 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
831 /* Wait till the card is in programming state */
832 errorstate
= SD_IsCardProgramming(hsd
, &cardstate
);
834 while ((errorstate
== SD_OK
) && ((cardstate
== SD_CARD_PROGRAMMING
) || (cardstate
== SD_CARD_RECEIVING
)))
836 errorstate
= SD_IsCardProgramming(hsd
, &cardstate
);
843 * @brief Reads block(s) from a specified address in a card. The Data transfer
844 * is managed by DMA mode.
845 * @note This API should be followed by the function HAL_SD_CheckReadOperation()
846 * to check the completion of the read process
847 * @param hsd: SD handle
848 * @param pReadBuffer: Pointer to the buffer that will contain the received data
849 * @param ReadAddr: Address from where data is to be read
850 * @param BlockSize: SD card Data block size
851 * @note BlockSize must be 512 bytes.
852 * @param NumberOfBlocks: Number of blocks to read.
853 * @retval SD Card error state
855 HAL_SD_ErrorTypedef
HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef
*hsd
, uint32_t *pReadBuffer
, uint64_t ReadAddr
, uint32_t BlockSize
, uint32_t NumberOfBlocks
)
857 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure
;
858 SDMMC_DataInitTypeDef sdmmc_datainitstructure
;
859 HAL_SD_ErrorTypedef errorstate
= SD_OK
;
861 /* Initialize data control register */
862 hsd
->Instance
->DCTRL
= 0;
864 /* Initialize handle flags */
865 hsd
->SdTransferCplt
= 0;
866 hsd
->DmaTransferCplt
= 0;
867 hsd
->SdTransferErr
= SD_OK
;
869 /* Initialize SD Read operation */
870 if(NumberOfBlocks
> 1)
872 hsd
->SdOperation
= SD_READ_MULTIPLE_BLOCK
;
876 hsd
->SdOperation
= SD_READ_SINGLE_BLOCK
;
879 /* Enable transfer interrupts */
880 __HAL_SD_SDMMC_ENABLE_IT(hsd
, (SDMMC_IT_DCRCFAIL
|\
885 /* Enable SDMMC DMA transfer */
886 __HAL_SD_SDMMC_DMA_ENABLE(hsd
);
888 /* Configure DMA user callbacks */
889 hsd
->hdmarx
->XferCpltCallback
= SD_DMA_RxCplt
;
890 hsd
->hdmarx
->XferErrorCallback
= SD_DMA_RxError
;
892 /* Enable the DMA Channel */
893 HAL_DMA_Start_IT(hsd
->hdmarx
, (uint32_t)&hsd
->Instance
->FIFO
, (uint32_t)pReadBuffer
, (uint32_t)(BlockSize
* NumberOfBlocks
)/4);
895 if (hsd
->CardType
== HIGH_CAPACITY_SD_CARD
)
901 /* Set Block Size for Card */
902 sdmmc_cmdinitstructure
.Argument
= (uint32_t)BlockSize
;
903 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_SET_BLOCKLEN
;
904 sdmmc_cmdinitstructure
.Response
= SDMMC_RESPONSE_SHORT
;
905 sdmmc_cmdinitstructure
.WaitForInterrupt
= SDMMC_WAIT_NO
;
906 sdmmc_cmdinitstructure
.CPSM
= SDMMC_CPSM_ENABLE
;
907 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
909 /* Check for error conditions */
910 errorstate
= SD_CmdResp1Error(hsd
, SD_CMD_SET_BLOCKLEN
);
912 if (errorstate
!= SD_OK
)
917 /* Configure the SD DPSM (Data Path State Machine) */
918 sdmmc_datainitstructure
.DataTimeOut
= SD_DATATIMEOUT
;
919 sdmmc_datainitstructure
.DataLength
= BlockSize
* NumberOfBlocks
;
920 sdmmc_datainitstructure
.DataBlockSize
= SDMMC_DATABLOCK_SIZE_512B
;
921 sdmmc_datainitstructure
.TransferDir
= SDMMC_TRANSFER_DIR_TO_SDMMC
;
922 sdmmc_datainitstructure
.TransferMode
= SDMMC_TRANSFER_MODE_BLOCK
;
923 sdmmc_datainitstructure
.DPSM
= SDMMC_DPSM_ENABLE
;
924 SDMMC_DataConfig(hsd
->Instance
, &sdmmc_datainitstructure
);
926 /* Check number of blocks command */
927 if(NumberOfBlocks
> 1)
929 /* Send CMD18 READ_MULT_BLOCK with argument data address */
930 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_READ_MULT_BLOCK
;
934 /* Send CMD17 READ_SINGLE_BLOCK */
935 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_READ_SINGLE_BLOCK
;
938 sdmmc_cmdinitstructure
.Argument
= (uint32_t)ReadAddr
;
939 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
941 /* Check for error conditions */
942 if(NumberOfBlocks
> 1)
944 errorstate
= SD_CmdResp1Error(hsd
, SD_CMD_READ_MULT_BLOCK
);
948 errorstate
= SD_CmdResp1Error(hsd
, SD_CMD_READ_SINGLE_BLOCK
);
951 /* Update the SD transfer error in SD handle */
952 hsd
->SdTransferErr
= errorstate
;
959 * @brief Writes block(s) to a specified address in a card. The Data transfer
960 * is managed by DMA mode.
961 * @note This API should be followed by the function HAL_SD_CheckWriteOperation()
962 * to check the completion of the write process (by SD current status polling).
963 * @param hsd: SD handle
964 * @param pWriteBuffer: pointer to the buffer that will contain the data to transmit
965 * @param WriteAddr: Address from where data is to be read
966 * @param BlockSize: the SD card Data block size
967 * @note BlockSize must be 512 bytes.
968 * @param NumberOfBlocks: Number of blocks to write
969 * @retval SD Card error state
971 HAL_SD_ErrorTypedef
HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef
*hsd
, uint32_t *pWriteBuffer
, uint64_t WriteAddr
, uint32_t BlockSize
, uint32_t NumberOfBlocks
)
973 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure
;
974 SDMMC_DataInitTypeDef sdmmc_datainitstructure
;
975 HAL_SD_ErrorTypedef errorstate
= SD_OK
;
977 /* Initialize data control register */
978 hsd
->Instance
->DCTRL
= 0;
980 /* Initialize handle flags */
981 hsd
->SdTransferCplt
= 0;
982 hsd
->DmaTransferCplt
= 0;
983 hsd
->SdTransferErr
= SD_OK
;
985 /* Initialize SD Write operation */
986 if(NumberOfBlocks
> 1)
988 hsd
->SdOperation
= SD_WRITE_MULTIPLE_BLOCK
;
992 hsd
->SdOperation
= SD_WRITE_SINGLE_BLOCK
;
995 /* Enable transfer interrupts */
996 __HAL_SD_SDMMC_ENABLE_IT(hsd
, (SDMMC_IT_DCRCFAIL
|\
1001 /* Configure DMA user callbacks */
1002 hsd
->hdmatx
->XferCpltCallback
= SD_DMA_TxCplt
;
1003 hsd
->hdmatx
->XferErrorCallback
= SD_DMA_TxError
;
1005 /* Enable the DMA Channel */
1006 HAL_DMA_Start_IT(hsd
->hdmatx
, (uint32_t)pWriteBuffer
, (uint32_t)&hsd
->Instance
->FIFO
, (uint32_t)(BlockSize
* NumberOfBlocks
)/4);
1008 /* Enable SDMMC DMA transfer */
1009 __HAL_SD_SDMMC_DMA_ENABLE(hsd
);
1011 if (hsd
->CardType
== HIGH_CAPACITY_SD_CARD
)
1017 /* Set Block Size for Card */
1018 sdmmc_cmdinitstructure
.Argument
= (uint32_t)BlockSize
;
1019 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_SET_BLOCKLEN
;
1020 sdmmc_cmdinitstructure
.Response
= SDMMC_RESPONSE_SHORT
;
1021 sdmmc_cmdinitstructure
.WaitForInterrupt
= SDMMC_WAIT_NO
;
1022 sdmmc_cmdinitstructure
.CPSM
= SDMMC_CPSM_ENABLE
;
1023 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
1025 /* Check for error conditions */
1026 errorstate
= SD_CmdResp1Error(hsd
, SD_CMD_SET_BLOCKLEN
);
1028 if (errorstate
!= SD_OK
)
1033 /* Check number of blocks command */
1034 if(NumberOfBlocks
<= 1)
1036 /* Send CMD24 WRITE_SINGLE_BLOCK */
1037 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_WRITE_SINGLE_BLOCK
;
1041 /* Send CMD25 WRITE_MULT_BLOCK with argument data address */
1042 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_WRITE_MULT_BLOCK
;
1045 sdmmc_cmdinitstructure
.Argument
= (uint32_t)WriteAddr
;
1046 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
1048 /* Check for error conditions */
1049 if(NumberOfBlocks
> 1)
1051 errorstate
= SD_CmdResp1Error(hsd
, SD_CMD_WRITE_MULT_BLOCK
);
1055 errorstate
= SD_CmdResp1Error(hsd
, SD_CMD_WRITE_SINGLE_BLOCK
);
1058 if (errorstate
!= SD_OK
)
1063 /* Configure the SD DPSM (Data Path State Machine) */
1064 sdmmc_datainitstructure
.DataTimeOut
= SD_DATATIMEOUT
;
1065 sdmmc_datainitstructure
.DataLength
= BlockSize
* NumberOfBlocks
;
1066 sdmmc_datainitstructure
.DataBlockSize
= SDMMC_DATABLOCK_SIZE_512B
;
1067 sdmmc_datainitstructure
.TransferDir
= SDMMC_TRANSFER_DIR_TO_CARD
;
1068 sdmmc_datainitstructure
.TransferMode
= SDMMC_TRANSFER_MODE_BLOCK
;
1069 sdmmc_datainitstructure
.DPSM
= SDMMC_DPSM_ENABLE
;
1070 SDMMC_DataConfig(hsd
->Instance
, &sdmmc_datainitstructure
);
1072 hsd
->SdTransferErr
= errorstate
;
1078 * @brief This function waits until the SD DMA data read transfer is finished.
1079 * This API should be called after HAL_SD_ReadBlocks_DMA() function
1080 * to insure that all data sent by the card is already transferred by the
1082 * @param hsd: SD handle
1083 * @param Timeout: Timeout duration
1084 * @retval SD Card error state
1086 HAL_SD_ErrorTypedef
HAL_SD_CheckReadOperation(SD_HandleTypeDef
*hsd
, uint32_t Timeout
)
1088 HAL_SD_ErrorTypedef errorstate
= SD_OK
;
1089 uint32_t timeout
= Timeout
;
1090 uint32_t tmp1
, tmp2
;
1091 HAL_SD_ErrorTypedef tmp3
;
1093 /* Wait for DMA/SD transfer end or SD error variables to be in SD handle */
1094 tmp1
= hsd
->DmaTransferCplt
;
1095 tmp2
= hsd
->SdTransferCplt
;
1096 tmp3
= (HAL_SD_ErrorTypedef
)hsd
->SdTransferErr
;
1098 while (((tmp1
& tmp2
) == 0) && (tmp3
== SD_OK
) && (timeout
> 0))
1100 tmp1
= hsd
->DmaTransferCplt
;
1101 tmp2
= hsd
->SdTransferCplt
;
1102 tmp3
= (HAL_SD_ErrorTypedef
)hsd
->SdTransferErr
;
1108 /* Wait until the Rx transfer is no longer active */
1109 while((__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_RXACT
)) && (timeout
> 0))
1114 /* Send stop command in multiblock read */
1115 if (hsd
->SdOperation
== SD_READ_MULTIPLE_BLOCK
)
1117 errorstate
= HAL_SD_StopTransfer(hsd
);
1120 if ((timeout
== 0) && (errorstate
== SD_OK
))
1122 errorstate
= SD_DATA_TIMEOUT
;
1125 /* Clear all the static flags */
1126 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
1128 /* Return error state */
1129 if (hsd
->SdTransferErr
!= SD_OK
)
1131 return (HAL_SD_ErrorTypedef
)(hsd
->SdTransferErr
);
1138 * @brief This function waits until the SD DMA data write transfer is finished.
1139 * This API should be called after HAL_SD_WriteBlocks_DMA() function
1140 * to insure that all data sent by the card is already transferred by the
1142 * @param hsd: SD handle
1143 * @param Timeout: Timeout duration
1144 * @retval SD Card error state
1146 HAL_SD_ErrorTypedef
HAL_SD_CheckWriteOperation(SD_HandleTypeDef
*hsd
, uint32_t Timeout
)
1148 HAL_SD_ErrorTypedef errorstate
= SD_OK
;
1149 uint32_t timeout
= Timeout
;
1150 uint32_t tmp1
, tmp2
;
1151 HAL_SD_ErrorTypedef tmp3
;
1153 /* Wait for DMA/SD transfer end or SD error variables to be in SD handle */
1154 tmp1
= hsd
->DmaTransferCplt
;
1155 tmp2
= hsd
->SdTransferCplt
;
1156 tmp3
= (HAL_SD_ErrorTypedef
)hsd
->SdTransferErr
;
1158 while (((tmp1
& tmp2
) == 0) && (tmp3
== SD_OK
) && (timeout
> 0))
1160 tmp1
= hsd
->DmaTransferCplt
;
1161 tmp2
= hsd
->SdTransferCplt
;
1162 tmp3
= (HAL_SD_ErrorTypedef
)hsd
->SdTransferErr
;
1168 /* Wait until the Tx transfer is no longer active */
1169 while((__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_TXACT
)) && (timeout
> 0))
1174 /* Send stop command in multiblock write */
1175 if (hsd
->SdOperation
== SD_WRITE_MULTIPLE_BLOCK
)
1177 errorstate
= HAL_SD_StopTransfer(hsd
);
1180 if ((timeout
== 0) && (errorstate
== SD_OK
))
1182 errorstate
= SD_DATA_TIMEOUT
;
1185 /* Clear all the static flags */
1186 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
1188 /* Return error state */
1189 if (hsd
->SdTransferErr
!= SD_OK
)
1191 return (HAL_SD_ErrorTypedef
)(hsd
->SdTransferErr
);
1194 /* Wait until write is complete */
1195 while(HAL_SD_GetStatus(hsd
) != SD_TRANSFER_OK
)
1203 * @brief Erases the specified memory area of the given SD card.
1204 * @param hsd: SD handle
1205 * @param startaddr: Start byte address
1206 * @param endaddr: End byte address
1207 * @retval SD Card error state
1209 HAL_SD_ErrorTypedef
HAL_SD_Erase(SD_HandleTypeDef
*hsd
, uint64_t startaddr
, uint64_t endaddr
)
1211 HAL_SD_ErrorTypedef errorstate
= SD_OK
;
1212 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure
;
1215 __IO
uint32_t maxdelay
= 0;
1216 uint8_t cardstate
= 0;
1218 /* Check if the card command class supports erase command */
1219 if (((hsd
->CSD
[1] >> 20) & SD_CCCC_ERASE
) == 0)
1221 errorstate
= SD_REQUEST_NOT_APPLICABLE
;
1226 /* Get max delay value */
1227 maxdelay
= 120000 / (((hsd
->Instance
->CLKCR
) & 0xFF) + 2);
1229 if((SDMMC_GetResponse(hsd
->Instance
, SDMMC_RESP1
) & SD_CARD_LOCKED
) == SD_CARD_LOCKED
)
1231 errorstate
= SD_LOCK_UNLOCK_FAILED
;
1236 /* Get start and end block for high capacity cards */
1237 if (hsd
->CardType
== HIGH_CAPACITY_SD_CARD
)
1243 /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
1244 if ((hsd
->CardType
== STD_CAPACITY_SD_CARD_V1_1
) || (hsd
->CardType
== STD_CAPACITY_SD_CARD_V2_0
) ||\
1245 (hsd
->CardType
== HIGH_CAPACITY_SD_CARD
))
1247 /* Send CMD32 SD_ERASE_GRP_START with argument as addr */
1248 sdmmc_cmdinitstructure
.Argument
=(uint32_t)startaddr
;
1249 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_SD_ERASE_GRP_START
;
1250 sdmmc_cmdinitstructure
.Response
= SDMMC_RESPONSE_SHORT
;
1251 sdmmc_cmdinitstructure
.WaitForInterrupt
= SDMMC_WAIT_NO
;
1252 sdmmc_cmdinitstructure
.CPSM
= SDMMC_CPSM_ENABLE
;
1253 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
1255 /* Check for error conditions */
1256 errorstate
= SD_CmdResp1Error(hsd
, SD_CMD_SD_ERASE_GRP_START
);
1258 if (errorstate
!= SD_OK
)
1263 /* Send CMD33 SD_ERASE_GRP_END with argument as addr */
1264 sdmmc_cmdinitstructure
.Argument
= (uint32_t)endaddr
;
1265 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_SD_ERASE_GRP_END
;
1266 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
1268 /* Check for error conditions */
1269 errorstate
= SD_CmdResp1Error(hsd
, SD_CMD_SD_ERASE_GRP_END
);
1271 if (errorstate
!= SD_OK
)
1277 /* Send CMD38 ERASE */
1278 sdmmc_cmdinitstructure
.Argument
= 0;
1279 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_ERASE
;
1280 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
1282 /* Check for error conditions */
1283 errorstate
= SD_CmdResp1Error(hsd
, SD_CMD_ERASE
);
1285 if (errorstate
!= SD_OK
)
1290 for (; delay
< maxdelay
; delay
++)
1294 /* Wait until the card is in programming state */
1295 errorstate
= SD_IsCardProgramming(hsd
, &cardstate
);
1297 delay
= SD_DATATIMEOUT
;
1299 while ((delay
> 0) && (errorstate
== SD_OK
) && ((cardstate
== SD_CARD_PROGRAMMING
) || (cardstate
== SD_CARD_RECEIVING
)))
1301 errorstate
= SD_IsCardProgramming(hsd
, &cardstate
);
1309 * @brief This function handles SD card interrupt request.
1310 * @param hsd: SD handle
1313 void HAL_SD_IRQHandler(SD_HandleTypeDef
*hsd
)
1315 /* Check for SDMMC interrupt flags */
1316 if (__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_IT_DATAEND
))
1318 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_IT_DATAEND
);
1320 /* SD transfer is complete */
1321 hsd
->SdTransferCplt
= 1;
1323 /* No transfer error */
1324 hsd
->SdTransferErr
= SD_OK
;
1326 HAL_SD_XferCpltCallback(hsd
);
1328 else if (__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_IT_DCRCFAIL
))
1330 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_FLAG_DCRCFAIL
);
1332 hsd
->SdTransferErr
= SD_DATA_CRC_FAIL
;
1334 HAL_SD_XferErrorCallback(hsd
);
1337 else if (__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_IT_DTIMEOUT
))
1339 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_FLAG_DTIMEOUT
);
1341 hsd
->SdTransferErr
= SD_DATA_TIMEOUT
;
1343 HAL_SD_XferErrorCallback(hsd
);
1345 else if (__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_IT_RXOVERR
))
1347 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_FLAG_RXOVERR
);
1349 hsd
->SdTransferErr
= SD_RX_OVERRUN
;
1351 HAL_SD_XferErrorCallback(hsd
);
1353 else if (__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_IT_TXUNDERR
))
1355 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_FLAG_TXUNDERR
);
1357 hsd
->SdTransferErr
= SD_TX_UNDERRUN
;
1359 HAL_SD_XferErrorCallback(hsd
);
1363 /* No error flag set */
1366 /* Disable all SDMMC peripheral interrupt sources */
1367 __HAL_SD_SDMMC_DISABLE_IT(hsd
, SDMMC_IT_DCRCFAIL
| SDMMC_IT_DTIMEOUT
| SDMMC_IT_DATAEND
|\
1368 SDMMC_IT_TXFIFOHE
| SDMMC_IT_RXFIFOHF
| SDMMC_IT_TXUNDERR
|\
1374 * @brief SD end of transfer callback.
1375 * @param hsd: SD handle
1378 __weak
void HAL_SD_XferCpltCallback(SD_HandleTypeDef
*hsd
)
1380 /* Prevent unused argument(s) compilation warning */
1383 /* NOTE : This function Should not be modified, when the callback is needed,
1384 the HAL_SD_XferCpltCallback could be implemented in the user file
1389 * @brief SD Transfer Error callback.
1390 * @param hsd: SD handle
1393 __weak
void HAL_SD_XferErrorCallback(SD_HandleTypeDef
*hsd
)
1395 /* Prevent unused argument(s) compilation warning */
1398 /* NOTE : This function Should not be modified, when the callback is needed,
1399 the HAL_SD_XferErrorCallback could be implemented in the user file
1404 * @brief SD Transfer complete Rx callback in non blocking mode.
1405 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
1406 * the configuration information for the specified DMA module.
1409 __weak
void HAL_SD_DMA_RxCpltCallback(DMA_HandleTypeDef
*hdma
)
1411 /* Prevent unused argument(s) compilation warning */
1414 /* NOTE : This function Should not be modified, when the callback is needed,
1415 the HAL_SD_DMA_RxCpltCallback could be implemented in the user file
1420 * @brief SD DMA transfer complete Rx error callback.
1421 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
1422 * the configuration information for the specified DMA module.
1425 __weak
void HAL_SD_DMA_RxErrorCallback(DMA_HandleTypeDef
*hdma
)
1427 /* Prevent unused argument(s) compilation warning */
1430 /* NOTE : This function Should not be modified, when the callback is needed,
1431 the HAL_SD_DMA_RxErrorCallback could be implemented in the user file
1436 * @brief SD Transfer complete Tx callback in non blocking mode.
1437 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
1438 * the configuration information for the specified DMA module.
1441 __weak
void HAL_SD_DMA_TxCpltCallback(DMA_HandleTypeDef
*hdma
)
1443 /* Prevent unused argument(s) compilation warning */
1446 /* NOTE : This function Should not be modified, when the callback is needed,
1447 the HAL_SD_DMA_TxCpltCallback could be implemented in the user file
1452 * @brief SD DMA transfer complete error Tx callback.
1453 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
1454 * the configuration information for the specified DMA module.
1457 __weak
void HAL_SD_DMA_TxErrorCallback(DMA_HandleTypeDef
*hdma
)
1459 /* Prevent unused argument(s) compilation warning */
1462 /* NOTE : This function Should not be modified, when the callback is needed,
1463 the HAL_SD_DMA_TxErrorCallback could be implemented in the user file
1471 /** @addtogroup SD_Exported_Functions_Group3
1472 * @brief management functions
1475 ==============================================================================
1476 ##### Peripheral Control functions #####
1477 ==============================================================================
1479 This subsection provides a set of functions allowing to control the SD card
1487 * @brief Returns information about specific card.
1488 * @param hsd: SD handle
1489 * @param pCardInfo: Pointer to a HAL_SD_CardInfoTypedef structure that
1490 * contains all SD cardinformation
1491 * @retval SD Card error state
1493 HAL_SD_ErrorTypedef
HAL_SD_Get_CardInfo(SD_HandleTypeDef
*hsd
, HAL_SD_CardInfoTypedef
*pCardInfo
)
1495 HAL_SD_ErrorTypedef errorstate
= SD_OK
;
1498 pCardInfo
->CardType
= (uint8_t)(hsd
->CardType
);
1499 pCardInfo
->RCA
= (uint16_t)(hsd
->RCA
);
1502 tmp
= (hsd
->CSD
[0] & 0xFF000000U
) >> 24;
1503 pCardInfo
->SD_csd
.CSDStruct
= (uint8_t)((tmp
& 0xC0) >> 6);
1504 pCardInfo
->SD_csd
.SysSpecVersion
= (uint8_t)((tmp
& 0x3C) >> 2);
1505 pCardInfo
->SD_csd
.Reserved1
= tmp
& 0x03;
1508 tmp
= (hsd
->CSD
[0] & 0x00FF0000) >> 16;
1509 pCardInfo
->SD_csd
.TAAC
= (uint8_t)tmp
;
1512 tmp
= (hsd
->CSD
[0] & 0x0000FF00) >> 8;
1513 pCardInfo
->SD_csd
.NSAC
= (uint8_t)tmp
;
1516 tmp
= hsd
->CSD
[0] & 0x000000FF;
1517 pCardInfo
->SD_csd
.MaxBusClkFrec
= (uint8_t)tmp
;
1520 tmp
= (hsd
->CSD
[1] & 0xFF000000U
) >> 24;
1521 pCardInfo
->SD_csd
.CardComdClasses
= (uint16_t)(tmp
<< 4);
1524 tmp
= (hsd
->CSD
[1] & 0x00FF0000U
) >> 16;
1525 pCardInfo
->SD_csd
.CardComdClasses
|= (uint16_t)((tmp
& 0xF0) >> 4);
1526 pCardInfo
->SD_csd
.RdBlockLen
= (uint8_t)(tmp
& 0x0F);
1529 tmp
= (hsd
->CSD
[1] & 0x0000FF00U
) >> 8;
1530 pCardInfo
->SD_csd
.PartBlockRead
= (uint8_t)((tmp
& 0x80) >> 7);
1531 pCardInfo
->SD_csd
.WrBlockMisalign
= (uint8_t)((tmp
& 0x40) >> 6);
1532 pCardInfo
->SD_csd
.RdBlockMisalign
= (uint8_t)((tmp
& 0x20) >> 5);
1533 pCardInfo
->SD_csd
.DSRImpl
= (uint8_t)((tmp
& 0x10) >> 4);
1534 pCardInfo
->SD_csd
.Reserved2
= 0; /*!< Reserved */
1536 if ((hsd
->CardType
== STD_CAPACITY_SD_CARD_V1_1
) || (hsd
->CardType
== STD_CAPACITY_SD_CARD_V2_0
))
1538 pCardInfo
->SD_csd
.DeviceSize
= (tmp
& 0x03) << 10;
1541 tmp
= (uint8_t)(hsd
->CSD
[1] & 0x000000FFU
);
1542 pCardInfo
->SD_csd
.DeviceSize
|= (tmp
) << 2;
1545 tmp
= (uint8_t)((hsd
->CSD
[2] & 0xFF000000U
) >> 24);
1546 pCardInfo
->SD_csd
.DeviceSize
|= (tmp
& 0xC0) >> 6;
1548 pCardInfo
->SD_csd
.MaxRdCurrentVDDMin
= (tmp
& 0x38) >> 3;
1549 pCardInfo
->SD_csd
.MaxRdCurrentVDDMax
= (tmp
& 0x07);
1552 tmp
= (uint8_t)((hsd
->CSD
[2] & 0x00FF0000U
) >> 16);
1553 pCardInfo
->SD_csd
.MaxWrCurrentVDDMin
= (tmp
& 0xE0) >> 5;
1554 pCardInfo
->SD_csd
.MaxWrCurrentVDDMax
= (tmp
& 0x1C) >> 2;
1555 pCardInfo
->SD_csd
.DeviceSizeMul
= (tmp
& 0x03) << 1;
1557 tmp
= (uint8_t)((hsd
->CSD
[2] & 0x0000FF00U
) >> 8);
1558 pCardInfo
->SD_csd
.DeviceSizeMul
|= (tmp
& 0x80) >> 7;
1560 pCardInfo
->CardCapacity
= (pCardInfo
->SD_csd
.DeviceSize
+ 1) ;
1561 pCardInfo
->CardCapacity
*= (1 << (pCardInfo
->SD_csd
.DeviceSizeMul
+ 2));
1562 pCardInfo
->CardBlockSize
= 1 << (pCardInfo
->SD_csd
.RdBlockLen
);
1563 pCardInfo
->CardCapacity
*= pCardInfo
->CardBlockSize
;
1565 else if (hsd
->CardType
== HIGH_CAPACITY_SD_CARD
)
1568 tmp
= (uint8_t)(hsd
->CSD
[1] & 0x000000FFU
);
1569 pCardInfo
->SD_csd
.DeviceSize
= (tmp
& 0x3F) << 16;
1572 tmp
= (uint8_t)((hsd
->CSD
[2] & 0xFF000000U
) >> 24);
1574 pCardInfo
->SD_csd
.DeviceSize
|= (tmp
<< 8);
1577 tmp
= (uint8_t)((hsd
->CSD
[2] & 0x00FF0000U
) >> 16);
1579 pCardInfo
->SD_csd
.DeviceSize
|= (tmp
);
1582 tmp
= (uint8_t)((hsd
->CSD
[2] & 0x0000FF00U
) >> 8);
1584 pCardInfo
->CardCapacity
= (uint64_t)(((uint64_t)pCardInfo
->SD_csd
.DeviceSize
+ 1) * 512 * 1024);
1585 pCardInfo
->CardBlockSize
= 512;
1589 /* Not supported card type */
1590 errorstate
= SD_ERROR
;
1593 pCardInfo
->SD_csd
.EraseGrSize
= (tmp
& 0x40) >> 6;
1594 pCardInfo
->SD_csd
.EraseGrMul
= (tmp
& 0x3F) << 1;
1597 tmp
= (uint8_t)(hsd
->CSD
[2] & 0x000000FF);
1598 pCardInfo
->SD_csd
.EraseGrMul
|= (tmp
& 0x80) >> 7;
1599 pCardInfo
->SD_csd
.WrProtectGrSize
= (tmp
& 0x7F);
1602 tmp
= (uint8_t)((hsd
->CSD
[3] & 0xFF000000U
) >> 24);
1603 pCardInfo
->SD_csd
.WrProtectGrEnable
= (tmp
& 0x80) >> 7;
1604 pCardInfo
->SD_csd
.ManDeflECC
= (tmp
& 0x60) >> 5;
1605 pCardInfo
->SD_csd
.WrSpeedFact
= (tmp
& 0x1C) >> 2;
1606 pCardInfo
->SD_csd
.MaxWrBlockLen
= (tmp
& 0x03) << 2;
1609 tmp
= (uint8_t)((hsd
->CSD
[3] & 0x00FF0000) >> 16);
1610 pCardInfo
->SD_csd
.MaxWrBlockLen
|= (tmp
& 0xC0) >> 6;
1611 pCardInfo
->SD_csd
.WriteBlockPaPartial
= (tmp
& 0x20) >> 5;
1612 pCardInfo
->SD_csd
.Reserved3
= 0;
1613 pCardInfo
->SD_csd
.ContentProtectAppli
= (tmp
& 0x01);
1616 tmp
= (uint8_t)((hsd
->CSD
[3] & 0x0000FF00) >> 8);
1617 pCardInfo
->SD_csd
.FileFormatGrouop
= (tmp
& 0x80) >> 7;
1618 pCardInfo
->SD_csd
.CopyFlag
= (tmp
& 0x40) >> 6;
1619 pCardInfo
->SD_csd
.PermWrProtect
= (tmp
& 0x20) >> 5;
1620 pCardInfo
->SD_csd
.TempWrProtect
= (tmp
& 0x10) >> 4;
1621 pCardInfo
->SD_csd
.FileFormat
= (tmp
& 0x0C) >> 2;
1622 pCardInfo
->SD_csd
.ECC
= (tmp
& 0x03);
1625 tmp
= (uint8_t)(hsd
->CSD
[3] & 0x000000FF);
1626 pCardInfo
->SD_csd
.CSD_CRC
= (tmp
& 0xFE) >> 1;
1627 pCardInfo
->SD_csd
.Reserved4
= 1;
1630 tmp
= (uint8_t)((hsd
->CID
[0] & 0xFF000000U
) >> 24);
1631 pCardInfo
->SD_cid
.ManufacturerID
= tmp
;
1634 tmp
= (uint8_t)((hsd
->CID
[0] & 0x00FF0000) >> 16);
1635 pCardInfo
->SD_cid
.OEM_AppliID
= tmp
<< 8;
1638 tmp
= (uint8_t)((hsd
->CID
[0] & 0x000000FF00) >> 8);
1639 pCardInfo
->SD_cid
.OEM_AppliID
|= tmp
;
1642 tmp
= (uint8_t)(hsd
->CID
[0] & 0x000000FF);
1643 pCardInfo
->SD_cid
.ProdName1
= tmp
<< 24;
1646 tmp
= (uint8_t)((hsd
->CID
[1] & 0xFF000000U
) >> 24);
1647 pCardInfo
->SD_cid
.ProdName1
|= tmp
<< 16;
1650 tmp
= (uint8_t)((hsd
->CID
[1] & 0x00FF0000) >> 16);
1651 pCardInfo
->SD_cid
.ProdName1
|= tmp
<< 8;
1654 tmp
= (uint8_t)((hsd
->CID
[1] & 0x0000FF00) >> 8);
1655 pCardInfo
->SD_cid
.ProdName1
|= tmp
;
1658 tmp
= (uint8_t)(hsd
->CID
[1] & 0x000000FF);
1659 pCardInfo
->SD_cid
.ProdName2
= tmp
;
1662 tmp
= (uint8_t)((hsd
->CID
[2] & 0xFF000000U
) >> 24);
1663 pCardInfo
->SD_cid
.ProdRev
= tmp
;
1666 tmp
= (uint8_t)((hsd
->CID
[2] & 0x00FF0000) >> 16);
1667 pCardInfo
->SD_cid
.ProdSN
= tmp
<< 24;
1670 tmp
= (uint8_t)((hsd
->CID
[2] & 0x0000FF00) >> 8);
1671 pCardInfo
->SD_cid
.ProdSN
|= tmp
<< 16;
1674 tmp
= (uint8_t)(hsd
->CID
[2] & 0x000000FF);
1675 pCardInfo
->SD_cid
.ProdSN
|= tmp
<< 8;
1678 tmp
= (uint8_t)((hsd
->CID
[3] & 0xFF000000U
) >> 24);
1679 pCardInfo
->SD_cid
.ProdSN
|= tmp
;
1682 tmp
= (uint8_t)((hsd
->CID
[3] & 0x00FF0000) >> 16);
1683 pCardInfo
->SD_cid
.Reserved1
|= (tmp
& 0xF0) >> 4;
1684 pCardInfo
->SD_cid
.ManufactDate
= (tmp
& 0x0F) << 8;
1687 tmp
= (uint8_t)((hsd
->CID
[3] & 0x0000FF00) >> 8);
1688 pCardInfo
->SD_cid
.ManufactDate
|= tmp
;
1691 tmp
= (uint8_t)(hsd
->CID
[3] & 0x000000FF);
1692 pCardInfo
->SD_cid
.CID_CRC
= (tmp
& 0xFE) >> 1;
1693 pCardInfo
->SD_cid
.Reserved2
= 1;
1699 * @brief Enables wide bus operation for the requested card if supported by
1701 * @param hsd: SD handle
1702 * @param WideMode: Specifies the SD card wide bus mode
1703 * This parameter can be one of the following values:
1704 * @arg SDMMC_BUS_WIDE_8B: 8-bit data transfer (Only for MMC)
1705 * @arg SDMMC_BUS_WIDE_4B: 4-bit data transfer
1706 * @arg SDMMC_BUS_WIDE_1B: 1-bit data transfer
1707 * @retval SD Card error state
1709 HAL_SD_ErrorTypedef
HAL_SD_WideBusOperation_Config(SD_HandleTypeDef
*hsd
, uint32_t WideMode
)
1711 HAL_SD_ErrorTypedef errorstate
= SD_OK
;
1712 SDMMC_InitTypeDef tmpinit
;
1714 /* MMC Card does not support this feature */
1715 if (hsd
->CardType
== MULTIMEDIA_CARD
)
1717 errorstate
= SD_UNSUPPORTED_FEATURE
;
1721 else if ((hsd
->CardType
== STD_CAPACITY_SD_CARD_V1_1
) || (hsd
->CardType
== STD_CAPACITY_SD_CARD_V2_0
) ||\
1722 (hsd
->CardType
== HIGH_CAPACITY_SD_CARD
))
1724 if (WideMode
== SDMMC_BUS_WIDE_8B
)
1726 errorstate
= SD_UNSUPPORTED_FEATURE
;
1728 else if (WideMode
== SDMMC_BUS_WIDE_4B
)
1730 errorstate
= SD_WideBus_Enable(hsd
);
1732 else if (WideMode
== SDMMC_BUS_WIDE_1B
)
1734 errorstate
= SD_WideBus_Disable(hsd
);
1738 /* WideMode is not a valid argument*/
1739 errorstate
= SD_INVALID_PARAMETER
;
1742 if (errorstate
== SD_OK
)
1744 /* Configure the SDMMC peripheral */
1745 tmpinit
.ClockEdge
= hsd
->Init
.ClockEdge
;
1746 tmpinit
.ClockBypass
= hsd
->Init
.ClockBypass
;
1747 tmpinit
.ClockPowerSave
= hsd
->Init
.ClockPowerSave
;
1748 tmpinit
.BusWide
= WideMode
;
1749 tmpinit
.HardwareFlowControl
= hsd
->Init
.HardwareFlowControl
;
1750 tmpinit
.ClockDiv
= hsd
->Init
.ClockDiv
;
1751 SDMMC_Init(hsd
->Instance
, tmpinit
);
1759 * @brief Aborts an ongoing data transfer.
1760 * @param hsd: SD handle
1761 * @retval SD Card error state
1763 HAL_SD_ErrorTypedef
HAL_SD_StopTransfer(SD_HandleTypeDef
*hsd
)
1765 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure
;
1766 HAL_SD_ErrorTypedef errorstate
= SD_OK
;
1768 /* Send CMD12 STOP_TRANSMISSION */
1769 sdmmc_cmdinitstructure
.Argument
= 0;
1770 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_STOP_TRANSMISSION
;
1771 sdmmc_cmdinitstructure
.Response
= SDMMC_RESPONSE_SHORT
;
1772 sdmmc_cmdinitstructure
.WaitForInterrupt
= SDMMC_WAIT_NO
;
1773 sdmmc_cmdinitstructure
.CPSM
= SDMMC_CPSM_ENABLE
;
1774 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
1776 /* Check for error conditions */
1777 errorstate
= SD_CmdResp1Error(hsd
, SD_CMD_STOP_TRANSMISSION
);
1783 * @brief Switches the SD card to High Speed mode.
1784 * This API must be used after "Transfer State"
1785 * @note This operation should be followed by the configuration
1786 * of PLL to have SDMMCCK clock between 67 and 75 MHz
1787 * @param hsd: SD handle
1788 * @retval SD Card error state
1790 HAL_SD_ErrorTypedef
HAL_SD_HighSpeed (SD_HandleTypeDef
*hsd
)
1792 HAL_SD_ErrorTypedef errorstate
= SD_OK
;
1793 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure
;
1794 SDMMC_DataInitTypeDef sdmmc_datainitstructure
;
1796 uint8_t SD_hs
[64] = {0};
1797 uint32_t SD_scr
[2] = {0, 0};
1798 uint32_t SD_SPEC
= 0 ;
1799 uint32_t count
= 0, *tempbuff
= (uint32_t *)SD_hs
;
1801 /* Initialize the Data control register */
1802 hsd
->Instance
->DCTRL
= 0;
1804 /* Get SCR Register */
1805 errorstate
= SD_FindSCR(hsd
, SD_scr
);
1807 if (errorstate
!= SD_OK
)
1812 /* Test the Version supported by the card*/
1813 SD_SPEC
= (SD_scr
[1] & 0x01000000) | (SD_scr
[1] & 0x02000000);
1815 if (SD_SPEC
!= SD_ALLZERO
)
1817 /* Set Block Size for Card */
1818 sdmmc_cmdinitstructure
.Argument
= (uint32_t)64;
1819 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_SET_BLOCKLEN
;
1820 sdmmc_cmdinitstructure
.Response
= SDMMC_RESPONSE_SHORT
;
1821 sdmmc_cmdinitstructure
.WaitForInterrupt
= SDMMC_WAIT_NO
;
1822 sdmmc_cmdinitstructure
.CPSM
= SDMMC_CPSM_ENABLE
;
1823 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
1825 /* Check for error conditions */
1826 errorstate
= SD_CmdResp1Error(hsd
, SD_CMD_SET_BLOCKLEN
);
1828 if (errorstate
!= SD_OK
)
1833 /* Configure the SD DPSM (Data Path State Machine) */
1834 sdmmc_datainitstructure
.DataTimeOut
= SD_DATATIMEOUT
;
1835 sdmmc_datainitstructure
.DataLength
= 64;
1836 sdmmc_datainitstructure
.DataBlockSize
= SDMMC_DATABLOCK_SIZE_64B
;
1837 sdmmc_datainitstructure
.TransferDir
= SDMMC_TRANSFER_DIR_TO_SDMMC
;
1838 sdmmc_datainitstructure
.TransferMode
= SDMMC_TRANSFER_MODE_BLOCK
;
1839 sdmmc_datainitstructure
.DPSM
= SDMMC_DPSM_ENABLE
;
1840 SDMMC_DataConfig(hsd
->Instance
, &sdmmc_datainitstructure
);
1842 /* Send CMD6 switch mode */
1843 sdmmc_cmdinitstructure
.Argument
= 0x80FFFF01U
;
1844 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_HS_SWITCH
;
1845 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
1847 /* Check for error conditions */
1848 errorstate
= SD_CmdResp1Error(hsd
, SD_CMD_HS_SWITCH
);
1850 if (errorstate
!= SD_OK
)
1855 while(!__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_RXOVERR
| SDMMC_FLAG_DCRCFAIL
| SDMMC_FLAG_DTIMEOUT
| SDMMC_FLAG_DBCKEND
))
1857 if (__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_RXFIFOHF
))
1859 for (count
= 0; count
< 8; count
++)
1861 *(tempbuff
+ count
) = SDMMC_ReadFIFO(hsd
->Instance
);
1868 if (__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_DTIMEOUT
))
1870 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_FLAG_DTIMEOUT
);
1872 errorstate
= SD_DATA_TIMEOUT
;
1876 else if (__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_DCRCFAIL
))
1878 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_FLAG_DCRCFAIL
);
1880 errorstate
= SD_DATA_CRC_FAIL
;
1884 else if (__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_RXOVERR
))
1886 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_FLAG_RXOVERR
);
1888 errorstate
= SD_RX_OVERRUN
;
1894 /* No error flag set */
1897 count
= SD_DATATIMEOUT
;
1899 while ((__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_RXDAVL
)) && (count
> 0))
1901 *tempbuff
= SDMMC_ReadFIFO(hsd
->Instance
);
1906 /* Clear all the static flags */
1907 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
1909 /* Test if the switch mode HS is ok */
1910 if ((SD_hs
[13]& 2) != 2)
1912 errorstate
= SD_UNSUPPORTED_FEATURE
;
1923 /** @addtogroup SD_Exported_Functions_Group4
1924 * @brief Peripheral State functions
1927 ==============================================================================
1928 ##### Peripheral State functions #####
1929 ==============================================================================
1931 This subsection permits to get in runtime the status of the peripheral
1939 * @brief Returns the current SD card's status.
1940 * @param hsd: SD handle
1941 * @param pSDstatus: Pointer to the buffer that will contain the SD card status
1942 * SD Status register)
1943 * @retval SD Card error state
1945 HAL_SD_ErrorTypedef
HAL_SD_SendSDStatus(SD_HandleTypeDef
*hsd
, uint32_t *pSDstatus
)
1947 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure
;
1948 SDMMC_DataInitTypeDef sdmmc_datainitstructure
;
1949 HAL_SD_ErrorTypedef errorstate
= SD_OK
;
1952 /* Check SD response */
1953 if ((SDMMC_GetResponse(hsd
->Instance
, SDMMC_RESP1
) & SD_CARD_LOCKED
) == SD_CARD_LOCKED
)
1955 errorstate
= SD_LOCK_UNLOCK_FAILED
;
1960 /* Set block size for card if it is not equal to current block size for card */
1961 sdmmc_cmdinitstructure
.Argument
= 64;
1962 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_SET_BLOCKLEN
;
1963 sdmmc_cmdinitstructure
.Response
= SDMMC_RESPONSE_SHORT
;
1964 sdmmc_cmdinitstructure
.WaitForInterrupt
= SDMMC_WAIT_NO
;
1965 sdmmc_cmdinitstructure
.CPSM
= SDMMC_CPSM_ENABLE
;
1966 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
1968 /* Check for error conditions */
1969 errorstate
= SD_CmdResp1Error(hsd
, SD_CMD_SET_BLOCKLEN
);
1971 if (errorstate
!= SD_OK
)
1977 sdmmc_cmdinitstructure
.Argument
= (uint32_t)(hsd
->RCA
<< 16);
1978 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_APP_CMD
;
1979 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
1981 /* Check for error conditions */
1982 errorstate
= SD_CmdResp1Error(hsd
, SD_CMD_APP_CMD
);
1984 if (errorstate
!= SD_OK
)
1989 /* Configure the SD DPSM (Data Path State Machine) */
1990 sdmmc_datainitstructure
.DataTimeOut
= SD_DATATIMEOUT
;
1991 sdmmc_datainitstructure
.DataLength
= 64;
1992 sdmmc_datainitstructure
.DataBlockSize
= SDMMC_DATABLOCK_SIZE_64B
;
1993 sdmmc_datainitstructure
.TransferDir
= SDMMC_TRANSFER_DIR_TO_SDMMC
;
1994 sdmmc_datainitstructure
.TransferMode
= SDMMC_TRANSFER_MODE_BLOCK
;
1995 sdmmc_datainitstructure
.DPSM
= SDMMC_DPSM_ENABLE
;
1996 SDMMC_DataConfig(hsd
->Instance
, &sdmmc_datainitstructure
);
1998 /* Send ACMD13 (SD_APP_STAUS) with argument as card's RCA */
1999 sdmmc_cmdinitstructure
.Argument
= 0;
2000 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_SD_APP_STATUS
;
2001 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
2003 /* Check for error conditions */
2004 errorstate
= SD_CmdResp1Error(hsd
, SD_CMD_SD_APP_STATUS
);
2006 if (errorstate
!= SD_OK
)
2011 /* Get status data */
2012 while(!__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_RXOVERR
| SDMMC_FLAG_DCRCFAIL
| SDMMC_FLAG_DTIMEOUT
| SDMMC_FLAG_DBCKEND
))
2014 if (__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_RXFIFOHF
))
2016 for (count
= 0; count
< 8; count
++)
2018 *(pSDstatus
+ count
) = SDMMC_ReadFIFO(hsd
->Instance
);
2025 if (__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_DTIMEOUT
))
2027 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_FLAG_DTIMEOUT
);
2029 errorstate
= SD_DATA_TIMEOUT
;
2033 else if (__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_DCRCFAIL
))
2035 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_FLAG_DCRCFAIL
);
2037 errorstate
= SD_DATA_CRC_FAIL
;
2041 else if (__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_RXOVERR
))
2043 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_FLAG_RXOVERR
);
2045 errorstate
= SD_RX_OVERRUN
;
2051 /* No error flag set */
2054 count
= SD_DATATIMEOUT
;
2055 while ((__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_RXDAVL
)) && (count
> 0))
2057 *pSDstatus
= SDMMC_ReadFIFO(hsd
->Instance
);
2062 /* Clear all the static status flags*/
2063 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
2069 * @brief Gets the current sd card data status.
2070 * @param hsd: SD handle
2071 * @retval Data Transfer state
2073 HAL_SD_TransferStateTypedef
HAL_SD_GetStatus(SD_HandleTypeDef
*hsd
)
2075 HAL_SD_CardStateTypedef cardstate
= SD_CARD_TRANSFER
;
2077 /* Get SD card state */
2078 cardstate
= SD_GetState(hsd
);
2080 /* Find SD status according to card state*/
2081 if (cardstate
== SD_CARD_TRANSFER
)
2083 return SD_TRANSFER_OK
;
2085 else if(cardstate
== SD_CARD_ERROR
)
2087 return SD_TRANSFER_ERROR
;
2091 return SD_TRANSFER_BUSY
;
2096 * @brief Gets the SD card status.
2097 * @param hsd: SD handle
2098 * @param pCardStatus: Pointer to the HAL_SD_CardStatusTypedef structure that
2099 * will contain the SD card status information
2100 * @retval SD Card error state
2102 HAL_SD_ErrorTypedef
HAL_SD_GetCardStatus(SD_HandleTypeDef
*hsd
, HAL_SD_CardStatusTypedef
*pCardStatus
)
2104 HAL_SD_ErrorTypedef errorstate
= SD_OK
;
2106 uint32_t sd_status
[16];
2108 errorstate
= HAL_SD_SendSDStatus(hsd
, sd_status
);
2110 if (errorstate
!= SD_OK
)
2116 tmp
= (sd_status
[0] & 0xC0) >> 6;
2117 pCardStatus
->DAT_BUS_WIDTH
= (uint8_t)tmp
;
2120 tmp
= (sd_status
[0] & 0x20) >> 5;
2121 pCardStatus
->SECURED_MODE
= (uint8_t)tmp
;
2124 tmp
= (sd_status
[0] & 0x00FF0000) >> 16;
2125 pCardStatus
->SD_CARD_TYPE
= (uint16_t)(tmp
<< 8);
2128 tmp
= (sd_status
[0] & 0xFF000000) >> 24;
2129 pCardStatus
->SD_CARD_TYPE
|= (uint16_t)tmp
;
2132 tmp
= (sd_status
[1] & 0xFF);
2133 pCardStatus
->SIZE_OF_PROTECTED_AREA
= (uint32_t)(tmp
<< 24);
2136 tmp
= (sd_status
[1] & 0xFF00) >> 8;
2137 pCardStatus
->SIZE_OF_PROTECTED_AREA
|= (uint32_t)(tmp
<< 16);
2140 tmp
= (sd_status
[1] & 0xFF0000) >> 16;
2141 pCardStatus
->SIZE_OF_PROTECTED_AREA
|= (uint32_t)(tmp
<< 8);
2144 tmp
= (sd_status
[1] & 0xFF000000) >> 24;
2145 pCardStatus
->SIZE_OF_PROTECTED_AREA
|= (uint32_t)tmp
;
2148 tmp
= (sd_status
[2] & 0xFF);
2149 pCardStatus
->SPEED_CLASS
= (uint8_t)tmp
;
2152 tmp
= (sd_status
[2] & 0xFF00) >> 8;
2153 pCardStatus
->PERFORMANCE_MOVE
= (uint8_t)tmp
;
2156 tmp
= (sd_status
[2] & 0xF00000) >> 20;
2157 pCardStatus
->AU_SIZE
= (uint8_t)tmp
;
2160 tmp
= (sd_status
[2] & 0xFF000000) >> 24;
2161 pCardStatus
->ERASE_SIZE
= (uint16_t)(tmp
<< 8);
2164 tmp
= (sd_status
[3] & 0xFF);
2165 pCardStatus
->ERASE_SIZE
|= (uint16_t)tmp
;
2168 tmp
= (sd_status
[3] & 0xFC00) >> 10;
2169 pCardStatus
->ERASE_TIMEOUT
= (uint8_t)tmp
;
2172 tmp
= (sd_status
[3] & 0x0300) >> 8;
2173 pCardStatus
->ERASE_OFFSET
= (uint8_t)tmp
;
2186 /* Private function ----------------------------------------------------------*/
2187 /** @addtogroup SD_Private_Functions
2192 * @brief SD DMA transfer complete Rx callback.
2193 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
2194 * the configuration information for the specified DMA module.
2197 static void SD_DMA_RxCplt(DMA_HandleTypeDef
*hdma
)
2199 SD_HandleTypeDef
*hsd
= (SD_HandleTypeDef
*)((DMA_HandleTypeDef
*)hdma
)->Parent
;
2201 /* DMA transfer is complete */
2202 hsd
->DmaTransferCplt
= 1;
2204 /* Wait until SD transfer is complete */
2205 while(hsd
->SdTransferCplt
== 0)
2209 /* Disable the DMA channel */
2210 HAL_DMA_Abort(hdma
);
2212 /* Transfer complete user callback */
2213 HAL_SD_DMA_RxCpltCallback(hsd
->hdmarx
);
2217 * @brief SD DMA transfer Error Rx callback.
2218 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
2219 * the configuration information for the specified DMA module.
2222 static void SD_DMA_RxError(DMA_HandleTypeDef
*hdma
)
2224 SD_HandleTypeDef
*hsd
= (SD_HandleTypeDef
*)((DMA_HandleTypeDef
*)hdma
)->Parent
;
2226 /* Transfer complete user callback */
2227 HAL_SD_DMA_RxErrorCallback(hsd
->hdmarx
);
2231 * @brief SD DMA transfer complete Tx callback.
2232 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
2233 * the configuration information for the specified DMA module.
2236 static void SD_DMA_TxCplt(DMA_HandleTypeDef
*hdma
)
2238 SD_HandleTypeDef
*hsd
= (SD_HandleTypeDef
*)((DMA_HandleTypeDef
*)hdma
)->Parent
;
2240 /* DMA transfer is complete */
2241 hsd
->DmaTransferCplt
= 1;
2243 /* Wait until SD transfer is complete */
2244 while(hsd
->SdTransferCplt
== 0)
2248 /* Disable the DMA channel */
2249 HAL_DMA_Abort(hdma
);
2251 /* Transfer complete user callback */
2252 HAL_SD_DMA_TxCpltCallback(hsd
->hdmatx
);
2256 * @brief SD DMA transfer Error Tx callback.
2257 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
2258 * the configuration information for the specified DMA module.
2261 static void SD_DMA_TxError(DMA_HandleTypeDef
*hdma
)
2263 SD_HandleTypeDef
*hsd
= ( SD_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
2265 /* Transfer complete user callback */
2266 HAL_SD_DMA_TxErrorCallback(hsd
->hdmatx
);
2270 * @brief Returns the SD current state.
2271 * @param hsd: SD handle
2272 * @retval SD card current state
2274 static HAL_SD_CardStateTypedef
SD_GetState(SD_HandleTypeDef
*hsd
)
2278 if (SD_SendStatus(hsd
, &resp1
) != SD_OK
)
2280 return SD_CARD_ERROR
;
2284 return (HAL_SD_CardStateTypedef
)((resp1
>> 9) & 0x0F);
2289 * @brief Initializes all cards or single card as the case may be Card(s) come
2290 * into standby state.
2291 * @param hsd: SD handle
2292 * @retval SD Card error state
2294 static HAL_SD_ErrorTypedef
SD_Initialize_Cards(SD_HandleTypeDef
*hsd
)
2296 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure
;
2297 HAL_SD_ErrorTypedef errorstate
= SD_OK
;
2298 uint16_t sd_rca
= 1;
2300 if(SDMMC_GetPowerState(hsd
->Instance
) == 0) /* Power off */
2302 errorstate
= SD_REQUEST_NOT_APPLICABLE
;
2307 if(hsd
->CardType
!= SECURE_DIGITAL_IO_CARD
)
2309 /* Send CMD2 ALL_SEND_CID */
2310 sdmmc_cmdinitstructure
.Argument
= 0;
2311 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_ALL_SEND_CID
;
2312 sdmmc_cmdinitstructure
.Response
= SDMMC_RESPONSE_LONG
;
2313 sdmmc_cmdinitstructure
.WaitForInterrupt
= SDMMC_WAIT_NO
;
2314 sdmmc_cmdinitstructure
.CPSM
= SDMMC_CPSM_ENABLE
;
2315 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
2317 /* Check for error conditions */
2318 errorstate
= SD_CmdResp2Error(hsd
);
2320 if(errorstate
!= SD_OK
)
2325 /* Get Card identification number data */
2326 hsd
->CID
[0] = SDMMC_GetResponse(hsd
->Instance
, SDMMC_RESP1
);
2327 hsd
->CID
[1] = SDMMC_GetResponse(hsd
->Instance
, SDMMC_RESP2
);
2328 hsd
->CID
[2] = SDMMC_GetResponse(hsd
->Instance
, SDMMC_RESP3
);
2329 hsd
->CID
[3] = SDMMC_GetResponse(hsd
->Instance
, SDMMC_RESP4
);
2332 if((hsd
->CardType
== STD_CAPACITY_SD_CARD_V1_1
) || (hsd
->CardType
== STD_CAPACITY_SD_CARD_V2_0
) ||\
2333 (hsd
->CardType
== SECURE_DIGITAL_IO_COMBO_CARD
) || (hsd
->CardType
== HIGH_CAPACITY_SD_CARD
))
2335 /* Send CMD3 SET_REL_ADDR with argument 0 */
2336 /* SD Card publishes its RCA. */
2337 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_SET_REL_ADDR
;
2338 sdmmc_cmdinitstructure
.Response
= SDMMC_RESPONSE_SHORT
;
2339 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
2341 /* Check for error conditions */
2342 errorstate
= SD_CmdResp6Error(hsd
, SD_CMD_SET_REL_ADDR
, &sd_rca
);
2344 if(errorstate
!= SD_OK
)
2350 if (hsd
->CardType
!= SECURE_DIGITAL_IO_CARD
)
2352 /* Get the SD card RCA */
2355 /* Send CMD9 SEND_CSD with argument as card's RCA */
2356 sdmmc_cmdinitstructure
.Argument
= (uint32_t)(hsd
->RCA
<< 16);
2357 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_SEND_CSD
;
2358 sdmmc_cmdinitstructure
.Response
= SDMMC_RESPONSE_LONG
;
2359 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
2361 /* Check for error conditions */
2362 errorstate
= SD_CmdResp2Error(hsd
);
2364 if(errorstate
!= SD_OK
)
2369 /* Get Card Specific Data */
2370 hsd
->CSD
[0] = SDMMC_GetResponse(hsd
->Instance
, SDMMC_RESP1
);
2371 hsd
->CSD
[1] = SDMMC_GetResponse(hsd
->Instance
, SDMMC_RESP2
);
2372 hsd
->CSD
[2] = SDMMC_GetResponse(hsd
->Instance
, SDMMC_RESP3
);
2373 hsd
->CSD
[3] = SDMMC_GetResponse(hsd
->Instance
, SDMMC_RESP4
);
2376 /* All cards are initialized */
2381 * @brief Selects od Deselects the corresponding card.
2382 * @param hsd: SD handle
2383 * @param addr: Address of the card to be selected
2384 * @retval SD Card error state
2386 static HAL_SD_ErrorTypedef
SD_Select_Deselect(SD_HandleTypeDef
*hsd
, uint64_t addr
)
2388 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure
;
2389 HAL_SD_ErrorTypedef errorstate
= SD_OK
;
2391 /* Send CMD7 SDMMC_SEL_DESEL_CARD */
2392 sdmmc_cmdinitstructure
.Argument
= (uint32_t)addr
;
2393 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_SEL_DESEL_CARD
;
2394 sdmmc_cmdinitstructure
.Response
= SDMMC_RESPONSE_SHORT
;
2395 sdmmc_cmdinitstructure
.WaitForInterrupt
= SDMMC_WAIT_NO
;
2396 sdmmc_cmdinitstructure
.CPSM
= SDMMC_CPSM_ENABLE
;
2397 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
2399 /* Check for error conditions */
2400 errorstate
= SD_CmdResp1Error(hsd
, SD_CMD_SEL_DESEL_CARD
);
2406 * @brief Enquires cards about their operating voltage and configures clock
2407 * controls and stores SD information that will be needed in future
2409 * @param hsd: SD handle
2410 * @retval SD Card error state
2412 static HAL_SD_ErrorTypedef
SD_PowerON(SD_HandleTypeDef
*hsd
)
2414 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure
;
2415 __IO HAL_SD_ErrorTypedef errorstate
= SD_OK
;
2416 uint32_t response
= 0, count
= 0, validvoltage
= 0;
2417 uint32_t sdtype
= SD_STD_CAPACITY
;
2419 /* Power ON Sequence -------------------------------------------------------*/
2420 /* Disable SDMMC Clock */
2421 __HAL_SD_SDMMC_DISABLE(hsd
);
2423 /* Set Power State to ON */
2424 SDMMC_PowerState_ON(hsd
->Instance
);
2426 /* 1ms: required power up waiting time before starting the SD initialization
2430 /* Enable SDMMC Clock */
2431 __HAL_SD_SDMMC_ENABLE(hsd
);
2433 /* CMD0: GO_IDLE_STATE -----------------------------------------------------*/
2434 /* No CMD response required */
2435 sdmmc_cmdinitstructure
.Argument
= 0;
2436 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_GO_IDLE_STATE
;
2437 sdmmc_cmdinitstructure
.Response
= SDMMC_RESPONSE_NO
;
2438 sdmmc_cmdinitstructure
.WaitForInterrupt
= SDMMC_WAIT_NO
;
2439 sdmmc_cmdinitstructure
.CPSM
= SDMMC_CPSM_ENABLE
;
2440 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
2442 /* Check for error conditions */
2443 errorstate
= SD_CmdError(hsd
);
2445 if(errorstate
!= SD_OK
)
2447 /* CMD Response Timeout (wait for CMDSENT flag) */
2451 /* CMD8: SEND_IF_COND ------------------------------------------------------*/
2452 /* Send CMD8 to verify SD card interface operating condition */
2453 /* Argument: - [31:12]: Reserved (shall be set to '0')
2454 - [11:8]: Supply Voltage (VHS) 0x1 (Range: 2.7-3.6 V)
2455 - [7:0]: Check Pattern (recommended 0xAA) */
2456 /* CMD Response: R7 */
2457 sdmmc_cmdinitstructure
.Argument
= SD_CHECK_PATTERN
;
2458 sdmmc_cmdinitstructure
.CmdIndex
= SD_SDMMC_SEND_IF_COND
;
2459 sdmmc_cmdinitstructure
.Response
= SDMMC_RESPONSE_SHORT
;
2460 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
2462 /* Check for error conditions */
2463 errorstate
= SD_CmdResp7Error(hsd
);
2465 if (errorstate
== SD_OK
)
2468 hsd
->CardType
= STD_CAPACITY_SD_CARD_V2_0
;
2469 sdtype
= SD_HIGH_CAPACITY
;
2473 sdmmc_cmdinitstructure
.Argument
= 0;
2474 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_APP_CMD
;
2475 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
2477 /* Check for error conditions */
2478 errorstate
= SD_CmdResp1Error(hsd
, SD_CMD_APP_CMD
);
2480 /* If errorstate is Command Timeout, it is a MMC card */
2481 /* If errorstate is SD_OK it is a SD card: SD card 2.0 (voltage range mismatch)
2483 if(errorstate
== SD_OK
)
2486 /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
2487 while((!validvoltage
) && (count
< SD_MAX_VOLT_TRIAL
))
2490 /* SEND CMD55 APP_CMD with RCA as 0 */
2491 sdmmc_cmdinitstructure
.Argument
= 0;
2492 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_APP_CMD
;
2493 sdmmc_cmdinitstructure
.Response
= SDMMC_RESPONSE_SHORT
;
2494 sdmmc_cmdinitstructure
.WaitForInterrupt
= SDMMC_WAIT_NO
;
2495 sdmmc_cmdinitstructure
.CPSM
= SDMMC_CPSM_ENABLE
;
2496 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
2498 /* Check for error conditions */
2499 errorstate
= SD_CmdResp1Error(hsd
, SD_CMD_APP_CMD
);
2501 if(errorstate
!= SD_OK
)
2507 sdmmc_cmdinitstructure
.Argument
= SD_VOLTAGE_WINDOW_SD
| sdtype
;
2508 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_SD_APP_OP_COND
;
2509 sdmmc_cmdinitstructure
.Response
= SDMMC_RESPONSE_SHORT
;
2510 sdmmc_cmdinitstructure
.WaitForInterrupt
= SDMMC_WAIT_NO
;
2511 sdmmc_cmdinitstructure
.CPSM
= SDMMC_CPSM_ENABLE
;
2512 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
2514 /* Check for error conditions */
2515 errorstate
= SD_CmdResp3Error(hsd
);
2517 if(errorstate
!= SD_OK
)
2522 /* Get command response */
2523 response
= SDMMC_GetResponse(hsd
->Instance
, SDMMC_RESP1
);
2525 /* Get operating voltage*/
2526 validvoltage
= (((response
>> 31) == 1) ? 1 : 0);
2531 if(count
>= SD_MAX_VOLT_TRIAL
)
2533 errorstate
= SD_INVALID_VOLTRANGE
;
2538 if((response
& SD_HIGH_CAPACITY
) == SD_HIGH_CAPACITY
) /* (response &= SD_HIGH_CAPACITY) */
2540 hsd
->CardType
= HIGH_CAPACITY_SD_CARD
;
2543 } /* else MMC Card */
2549 * @brief Turns the SDMMC output signals off.
2550 * @param hsd: SD handle
2551 * @retval SD Card error state
2553 static HAL_SD_ErrorTypedef
SD_PowerOFF(SD_HandleTypeDef
*hsd
)
2555 HAL_SD_ErrorTypedef errorstate
= SD_OK
;
2557 /* Set Power State to OFF */
2558 SDMMC_PowerState_OFF(hsd
->Instance
);
2564 * @brief Returns the current card's status.
2565 * @param hsd: SD handle
2566 * @param pCardStatus: pointer to the buffer that will contain the SD card
2567 * status (Card Status register)
2568 * @retval SD Card error state
2570 static HAL_SD_ErrorTypedef
SD_SendStatus(SD_HandleTypeDef
*hsd
, uint32_t *pCardStatus
)
2572 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure
;
2573 HAL_SD_ErrorTypedef errorstate
= SD_OK
;
2575 if(pCardStatus
== NULL
)
2577 errorstate
= SD_INVALID_PARAMETER
;
2582 /* Send Status command */
2583 sdmmc_cmdinitstructure
.Argument
= (uint32_t)(hsd
->RCA
<< 16);
2584 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_SEND_STATUS
;
2585 sdmmc_cmdinitstructure
.Response
= SDMMC_RESPONSE_SHORT
;
2586 sdmmc_cmdinitstructure
.WaitForInterrupt
= SDMMC_WAIT_NO
;
2587 sdmmc_cmdinitstructure
.CPSM
= SDMMC_CPSM_ENABLE
;
2588 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
2590 /* Check for error conditions */
2591 errorstate
= SD_CmdResp1Error(hsd
, SD_CMD_SEND_STATUS
);
2593 if(errorstate
!= SD_OK
)
2598 /* Get SD card status */
2599 *pCardStatus
= SDMMC_GetResponse(hsd
->Instance
, SDMMC_RESP1
);
2605 * @brief Checks for error conditions for CMD0.
2606 * @param hsd: SD handle
2607 * @retval SD Card error state
2609 static HAL_SD_ErrorTypedef
SD_CmdError(SD_HandleTypeDef
*hsd
)
2611 HAL_SD_ErrorTypedef errorstate
= SD_OK
;
2612 uint32_t timeout
, tmp
;
2614 timeout
= SDMMC_CMD0TIMEOUT
;
2616 tmp
= __HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_CMDSENT
);
2618 while((timeout
> 0) && (!tmp
))
2620 tmp
= __HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_CMDSENT
);
2626 errorstate
= SD_CMD_RSP_TIMEOUT
;
2630 /* Clear all the static flags */
2631 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
2637 * @brief Checks for error conditions for R7 response.
2638 * @param hsd: SD handle
2639 * @retval SD Card error state
2641 static HAL_SD_ErrorTypedef
SD_CmdResp7Error(SD_HandleTypeDef
*hsd
)
2643 HAL_SD_ErrorTypedef errorstate
= SD_ERROR
;
2644 uint32_t timeout
= SDMMC_CMD0TIMEOUT
, tmp
;
2646 tmp
= __HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_CCRCFAIL
| SDMMC_FLAG_CMDREND
| SDMMC_FLAG_CTIMEOUT
);
2648 while((!tmp
) && (timeout
> 0))
2650 tmp
= __HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_CCRCFAIL
| SDMMC_FLAG_CMDREND
| SDMMC_FLAG_CTIMEOUT
);
2654 tmp
= __HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_CTIMEOUT
);
2656 if((timeout
== 0) || tmp
)
2658 /* Card is not V2.0 compliant or card does not support the set voltage range */
2659 errorstate
= SD_CMD_RSP_TIMEOUT
;
2661 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_FLAG_CTIMEOUT
);
2666 if(__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_CMDREND
))
2668 /* Card is SD V2.0 compliant */
2671 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_FLAG_CMDREND
);
2680 * @brief Checks for error conditions for R1 response.
2681 * @param hsd: SD handle
2682 * @param SD_CMD: The sent command index
2683 * @retval SD Card error state
2685 static HAL_SD_ErrorTypedef
SD_CmdResp1Error(SD_HandleTypeDef
*hsd
, uint8_t SD_CMD
)
2687 HAL_SD_ErrorTypedef errorstate
= SD_OK
;
2688 uint32_t response_r1
;
2690 while(!__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_CCRCFAIL
| SDMMC_FLAG_CMDREND
| SDMMC_FLAG_CTIMEOUT
))
2694 if(__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_CTIMEOUT
))
2696 errorstate
= SD_CMD_RSP_TIMEOUT
;
2698 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_FLAG_CTIMEOUT
);
2702 else if(__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_CCRCFAIL
))
2704 errorstate
= SD_CMD_CRC_FAIL
;
2706 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_FLAG_CCRCFAIL
);
2711 /* Check response received is of desired command */
2712 if(SDMMC_GetCommandResponse(hsd
->Instance
) != SD_CMD
)
2714 errorstate
= SD_ILLEGAL_CMD
;
2719 /* Clear all the static flags */
2720 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
2722 /* We have received response, retrieve it for analysis */
2723 response_r1
= SDMMC_GetResponse(hsd
->Instance
, SDMMC_RESP1
);
2725 if((response_r1
& SD_OCR_ERRORBITS
) == SD_ALLZERO
)
2730 if((response_r1
& SD_OCR_ADDR_OUT_OF_RANGE
) == SD_OCR_ADDR_OUT_OF_RANGE
)
2732 return(SD_ADDR_OUT_OF_RANGE
);
2735 if((response_r1
& SD_OCR_ADDR_MISALIGNED
) == SD_OCR_ADDR_MISALIGNED
)
2737 return(SD_ADDR_MISALIGNED
);
2740 if((response_r1
& SD_OCR_BLOCK_LEN_ERR
) == SD_OCR_BLOCK_LEN_ERR
)
2742 return(SD_BLOCK_LEN_ERR
);
2745 if((response_r1
& SD_OCR_ERASE_SEQ_ERR
) == SD_OCR_ERASE_SEQ_ERR
)
2747 return(SD_ERASE_SEQ_ERR
);
2750 if((response_r1
& SD_OCR_BAD_ERASE_PARAM
) == SD_OCR_BAD_ERASE_PARAM
)
2752 return(SD_BAD_ERASE_PARAM
);
2755 if((response_r1
& SD_OCR_WRITE_PROT_VIOLATION
) == SD_OCR_WRITE_PROT_VIOLATION
)
2757 return(SD_WRITE_PROT_VIOLATION
);
2760 if((response_r1
& SD_OCR_LOCK_UNLOCK_FAILED
) == SD_OCR_LOCK_UNLOCK_FAILED
)
2762 return(SD_LOCK_UNLOCK_FAILED
);
2765 if((response_r1
& SD_OCR_COM_CRC_FAILED
) == SD_OCR_COM_CRC_FAILED
)
2767 return(SD_COM_CRC_FAILED
);
2770 if((response_r1
& SD_OCR_ILLEGAL_CMD
) == SD_OCR_ILLEGAL_CMD
)
2772 return(SD_ILLEGAL_CMD
);
2775 if((response_r1
& SD_OCR_CARD_ECC_FAILED
) == SD_OCR_CARD_ECC_FAILED
)
2777 return(SD_CARD_ECC_FAILED
);
2780 if((response_r1
& SD_OCR_CC_ERROR
) == SD_OCR_CC_ERROR
)
2782 return(SD_CC_ERROR
);
2785 if((response_r1
& SD_OCR_GENERAL_UNKNOWN_ERROR
) == SD_OCR_GENERAL_UNKNOWN_ERROR
)
2787 return(SD_GENERAL_UNKNOWN_ERROR
);
2790 if((response_r1
& SD_OCR_STREAM_READ_UNDERRUN
) == SD_OCR_STREAM_READ_UNDERRUN
)
2792 return(SD_STREAM_READ_UNDERRUN
);
2795 if((response_r1
& SD_OCR_STREAM_WRITE_OVERRUN
) == SD_OCR_STREAM_WRITE_OVERRUN
)
2797 return(SD_STREAM_WRITE_OVERRUN
);
2800 if((response_r1
& SD_OCR_CID_CSD_OVERWRITE
) == SD_OCR_CID_CSD_OVERWRITE
)
2802 return(SD_CID_CSD_OVERWRITE
);
2805 if((response_r1
& SD_OCR_WP_ERASE_SKIP
) == SD_OCR_WP_ERASE_SKIP
)
2807 return(SD_WP_ERASE_SKIP
);
2810 if((response_r1
& SD_OCR_CARD_ECC_DISABLED
) == SD_OCR_CARD_ECC_DISABLED
)
2812 return(SD_CARD_ECC_DISABLED
);
2815 if((response_r1
& SD_OCR_ERASE_RESET
) == SD_OCR_ERASE_RESET
)
2817 return(SD_ERASE_RESET
);
2820 if((response_r1
& SD_OCR_AKE_SEQ_ERROR
) == SD_OCR_AKE_SEQ_ERROR
)
2822 return(SD_AKE_SEQ_ERROR
);
2829 * @brief Checks for error conditions for R3 (OCR) response.
2830 * @param hsd: SD handle
2831 * @retval SD Card error state
2833 static HAL_SD_ErrorTypedef
SD_CmdResp3Error(SD_HandleTypeDef
*hsd
)
2835 HAL_SD_ErrorTypedef errorstate
= SD_OK
;
2837 while (!__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_CCRCFAIL
| SDMMC_FLAG_CMDREND
| SDMMC_FLAG_CTIMEOUT
))
2841 if (__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_CTIMEOUT
))
2843 errorstate
= SD_CMD_RSP_TIMEOUT
;
2845 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_FLAG_CTIMEOUT
);
2850 /* Clear all the static flags */
2851 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
2857 * @brief Checks for error conditions for R2 (CID or CSD) response.
2858 * @param hsd: SD handle
2859 * @retval SD Card error state
2861 static HAL_SD_ErrorTypedef
SD_CmdResp2Error(SD_HandleTypeDef
*hsd
)
2863 HAL_SD_ErrorTypedef errorstate
= SD_OK
;
2865 while (!__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_CCRCFAIL
| SDMMC_FLAG_CMDREND
| SDMMC_FLAG_CTIMEOUT
))
2869 if (__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_CTIMEOUT
))
2871 errorstate
= SD_CMD_RSP_TIMEOUT
;
2873 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_FLAG_CTIMEOUT
);
2877 else if (__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_CCRCFAIL
))
2879 errorstate
= SD_CMD_CRC_FAIL
;
2881 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_FLAG_CCRCFAIL
);
2887 /* No error flag set */
2890 /* Clear all the static flags */
2891 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
2897 * @brief Checks for error conditions for R6 (RCA) response.
2898 * @param hsd: SD handle
2899 * @param SD_CMD: The sent command index
2900 * @param pRCA: Pointer to the variable that will contain the SD card relative
2902 * @retval SD Card error state
2904 static HAL_SD_ErrorTypedef
SD_CmdResp6Error(SD_HandleTypeDef
*hsd
, uint8_t SD_CMD
, uint16_t *pRCA
)
2906 HAL_SD_ErrorTypedef errorstate
= SD_OK
;
2907 uint32_t response_r1
;
2909 while(!__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_CCRCFAIL
| SDMMC_FLAG_CMDREND
| SDMMC_FLAG_CTIMEOUT
))
2913 if(__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_CTIMEOUT
))
2915 errorstate
= SD_CMD_RSP_TIMEOUT
;
2917 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_FLAG_CTIMEOUT
);
2921 else if(__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_CCRCFAIL
))
2923 errorstate
= SD_CMD_CRC_FAIL
;
2925 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_FLAG_CCRCFAIL
);
2931 /* No error flag set */
2934 /* Check response received is of desired command */
2935 if(SDMMC_GetCommandResponse(hsd
->Instance
) != SD_CMD
)
2937 errorstate
= SD_ILLEGAL_CMD
;
2942 /* Clear all the static flags */
2943 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
2945 /* We have received response, retrieve it. */
2946 response_r1
= SDMMC_GetResponse(hsd
->Instance
, SDMMC_RESP1
);
2948 if((response_r1
& (SD_R6_GENERAL_UNKNOWN_ERROR
| SD_R6_ILLEGAL_CMD
| SD_R6_COM_CRC_FAILED
)) == SD_ALLZERO
)
2950 *pRCA
= (uint16_t) (response_r1
>> 16);
2955 if((response_r1
& SD_R6_GENERAL_UNKNOWN_ERROR
) == SD_R6_GENERAL_UNKNOWN_ERROR
)
2957 return(SD_GENERAL_UNKNOWN_ERROR
);
2960 if((response_r1
& SD_R6_ILLEGAL_CMD
) == SD_R6_ILLEGAL_CMD
)
2962 return(SD_ILLEGAL_CMD
);
2965 if((response_r1
& SD_R6_COM_CRC_FAILED
) == SD_R6_COM_CRC_FAILED
)
2967 return(SD_COM_CRC_FAILED
);
2974 * @brief Enables the SDMMC wide bus mode.
2975 * @param hsd: SD handle
2976 * @retval SD Card error state
2978 static HAL_SD_ErrorTypedef
SD_WideBus_Enable(SD_HandleTypeDef
*hsd
)
2980 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure
;
2981 HAL_SD_ErrorTypedef errorstate
= SD_OK
;
2983 uint32_t scr
[2] = {0, 0};
2985 if((SDMMC_GetResponse(hsd
->Instance
, SDMMC_RESP1
) & SD_CARD_LOCKED
) == SD_CARD_LOCKED
)
2987 errorstate
= SD_LOCK_UNLOCK_FAILED
;
2992 /* Get SCR Register */
2993 errorstate
= SD_FindSCR(hsd
, scr
);
2995 if(errorstate
!= SD_OK
)
3000 /* If requested card supports wide bus operation */
3001 if((scr
[1] & SD_WIDE_BUS_SUPPORT
) != SD_ALLZERO
)
3003 /* Send CMD55 APP_CMD with argument as card's RCA.*/
3004 sdmmc_cmdinitstructure
.Argument
= (uint32_t)(hsd
->RCA
<< 16);
3005 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_APP_CMD
;
3006 sdmmc_cmdinitstructure
.Response
= SDMMC_RESPONSE_SHORT
;
3007 sdmmc_cmdinitstructure
.WaitForInterrupt
= SDMMC_WAIT_NO
;
3008 sdmmc_cmdinitstructure
.CPSM
= SDMMC_CPSM_ENABLE
;
3009 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
3011 /* Check for error conditions */
3012 errorstate
= SD_CmdResp1Error(hsd
, SD_CMD_APP_CMD
);
3014 if(errorstate
!= SD_OK
)
3019 /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
3020 sdmmc_cmdinitstructure
.Argument
= 2;
3021 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_APP_SD_SET_BUSWIDTH
;
3022 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
3024 /* Check for error conditions */
3025 errorstate
= SD_CmdResp1Error(hsd
, SD_CMD_APP_SD_SET_BUSWIDTH
);
3027 if(errorstate
!= SD_OK
)
3036 errorstate
= SD_REQUEST_NOT_APPLICABLE
;
3043 * @brief Disables the SDMMC wide bus mode.
3044 * @param hsd: SD handle
3045 * @retval SD Card error state
3047 static HAL_SD_ErrorTypedef
SD_WideBus_Disable(SD_HandleTypeDef
*hsd
)
3049 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure
;
3050 HAL_SD_ErrorTypedef errorstate
= SD_OK
;
3052 uint32_t scr
[2] = {0, 0};
3054 if((SDMMC_GetResponse(hsd
->Instance
, SDMMC_RESP1
) & SD_CARD_LOCKED
) == SD_CARD_LOCKED
)
3056 errorstate
= SD_LOCK_UNLOCK_FAILED
;
3061 /* Get SCR Register */
3062 errorstate
= SD_FindSCR(hsd
, scr
);
3064 if(errorstate
!= SD_OK
)
3069 /* If requested card supports 1 bit mode operation */
3070 if((scr
[1] & SD_SINGLE_BUS_SUPPORT
) != SD_ALLZERO
)
3072 /* Send CMD55 APP_CMD with argument as card's RCA */
3073 sdmmc_cmdinitstructure
.Argument
= (uint32_t)(hsd
->RCA
<< 16);
3074 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_APP_CMD
;
3075 sdmmc_cmdinitstructure
.Response
= SDMMC_RESPONSE_SHORT
;
3076 sdmmc_cmdinitstructure
.WaitForInterrupt
= SDMMC_WAIT_NO
;
3077 sdmmc_cmdinitstructure
.CPSM
= SDMMC_CPSM_ENABLE
;
3078 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
3080 /* Check for error conditions */
3081 errorstate
= SD_CmdResp1Error(hsd
, SD_CMD_APP_CMD
);
3083 if(errorstate
!= SD_OK
)
3088 /* Send ACMD6 APP_CMD with argument as 0 for single bus mode */
3089 sdmmc_cmdinitstructure
.Argument
= 0;
3090 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_APP_SD_SET_BUSWIDTH
;
3091 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
3093 /* Check for error conditions */
3094 errorstate
= SD_CmdResp1Error(hsd
, SD_CMD_APP_SD_SET_BUSWIDTH
);
3096 if(errorstate
!= SD_OK
)
3105 errorstate
= SD_REQUEST_NOT_APPLICABLE
;
3113 * @brief Finds the SD card SCR register value.
3114 * @param hsd: SD handle
3115 * @param pSCR: pointer to the buffer that will contain the SCR value
3116 * @retval SD Card error state
3118 static HAL_SD_ErrorTypedef
SD_FindSCR(SD_HandleTypeDef
*hsd
, uint32_t *pSCR
)
3120 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure
;
3121 SDMMC_DataInitTypeDef sdmmc_datainitstructure
;
3122 HAL_SD_ErrorTypedef errorstate
= SD_OK
;
3124 uint32_t tempscr
[2] = {0, 0};
3126 /* Set Block Size To 8 Bytes */
3127 /* Send CMD55 APP_CMD with argument as card's RCA */
3128 sdmmc_cmdinitstructure
.Argument
= (uint32_t)8;
3129 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_SET_BLOCKLEN
;
3130 sdmmc_cmdinitstructure
.Response
= SDMMC_RESPONSE_SHORT
;
3131 sdmmc_cmdinitstructure
.WaitForInterrupt
= SDMMC_WAIT_NO
;
3132 sdmmc_cmdinitstructure
.CPSM
= SDMMC_CPSM_ENABLE
;
3133 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
3135 /* Check for error conditions */
3136 errorstate
= SD_CmdResp1Error(hsd
, SD_CMD_SET_BLOCKLEN
);
3138 if(errorstate
!= SD_OK
)
3143 /* Send CMD55 APP_CMD with argument as card's RCA */
3144 sdmmc_cmdinitstructure
.Argument
= (uint32_t)((hsd
->RCA
) << 16);
3145 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_APP_CMD
;
3146 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
3148 /* Check for error conditions */
3149 errorstate
= SD_CmdResp1Error(hsd
, SD_CMD_APP_CMD
);
3151 if(errorstate
!= SD_OK
)
3155 sdmmc_datainitstructure
.DataTimeOut
= SD_DATATIMEOUT
;
3156 sdmmc_datainitstructure
.DataLength
= 8;
3157 sdmmc_datainitstructure
.DataBlockSize
= SDMMC_DATABLOCK_SIZE_8B
;
3158 sdmmc_datainitstructure
.TransferDir
= SDMMC_TRANSFER_DIR_TO_SDMMC
;
3159 sdmmc_datainitstructure
.TransferMode
= SDMMC_TRANSFER_MODE_BLOCK
;
3160 sdmmc_datainitstructure
.DPSM
= SDMMC_DPSM_ENABLE
;
3161 SDMMC_DataConfig(hsd
->Instance
, &sdmmc_datainitstructure
);
3163 /* Send ACMD51 SD_APP_SEND_SCR with argument as 0 */
3164 sdmmc_cmdinitstructure
.Argument
= 0;
3165 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_SD_APP_SEND_SCR
;
3166 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
3168 /* Check for error conditions */
3169 errorstate
= SD_CmdResp1Error(hsd
, SD_CMD_SD_APP_SEND_SCR
);
3171 if(errorstate
!= SD_OK
)
3176 while(!__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_RXOVERR
| SDMMC_FLAG_DCRCFAIL
| SDMMC_FLAG_DTIMEOUT
| SDMMC_FLAG_DBCKEND
))
3178 if(__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_RXDAVL
))
3180 *(tempscr
+ index
) = SDMMC_ReadFIFO(hsd
->Instance
);
3185 if(__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_DTIMEOUT
))
3187 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_FLAG_DTIMEOUT
);
3189 errorstate
= SD_DATA_TIMEOUT
;
3193 else if(__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_DCRCFAIL
))
3195 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_FLAG_DCRCFAIL
);
3197 errorstate
= SD_DATA_CRC_FAIL
;
3201 else if(__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_RXOVERR
))
3203 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_FLAG_RXOVERR
);
3205 errorstate
= SD_RX_OVERRUN
;
3211 /* No error flag set */
3214 /* Clear all the static flags */
3215 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
3217 *(pSCR
+ 1) = ((tempscr
[0] & SD_0TO7BITS
) << 24) | ((tempscr
[0] & SD_8TO15BITS
) << 8) |\
3218 ((tempscr
[0] & SD_16TO23BITS
) >> 8) | ((tempscr
[0] & SD_24TO31BITS
) >> 24);
3220 *(pSCR
) = ((tempscr
[1] & SD_0TO7BITS
) << 24) | ((tempscr
[1] & SD_8TO15BITS
) << 8) |\
3221 ((tempscr
[1] & SD_16TO23BITS
) >> 8) | ((tempscr
[1] & SD_24TO31BITS
) >> 24);
3227 * @brief Checks if the SD card is in programming state.
3228 * @param hsd: SD handle
3229 * @param pStatus: pointer to the variable that will contain the SD card state
3230 * @retval SD Card error state
3232 static HAL_SD_ErrorTypedef
SD_IsCardProgramming(SD_HandleTypeDef
*hsd
, uint8_t *pStatus
)
3234 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure
;
3235 HAL_SD_ErrorTypedef errorstate
= SD_OK
;
3236 __IO
uint32_t responseR1
= 0;
3238 sdmmc_cmdinitstructure
.Argument
= (uint32_t)(hsd
->RCA
<< 16);
3239 sdmmc_cmdinitstructure
.CmdIndex
= SD_CMD_SEND_STATUS
;
3240 sdmmc_cmdinitstructure
.Response
= SDMMC_RESPONSE_SHORT
;
3241 sdmmc_cmdinitstructure
.WaitForInterrupt
= SDMMC_WAIT_NO
;
3242 sdmmc_cmdinitstructure
.CPSM
= SDMMC_CPSM_ENABLE
;
3243 SDMMC_SendCommand(hsd
->Instance
, &sdmmc_cmdinitstructure
);
3245 while(!__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_CCRCFAIL
| SDMMC_FLAG_CMDREND
| SDMMC_FLAG_CTIMEOUT
))
3249 if(__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_CTIMEOUT
))
3251 errorstate
= SD_CMD_RSP_TIMEOUT
;
3253 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_FLAG_CTIMEOUT
);
3257 else if(__HAL_SD_SDMMC_GET_FLAG(hsd
, SDMMC_FLAG_CCRCFAIL
))
3259 errorstate
= SD_CMD_CRC_FAIL
;
3261 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_FLAG_CCRCFAIL
);
3267 /* No error flag set */
3270 /* Check response received is of desired command */
3271 if((uint32_t)SDMMC_GetCommandResponse(hsd
->Instance
) != SD_CMD_SEND_STATUS
)
3273 errorstate
= SD_ILLEGAL_CMD
;
3278 /* Clear all the static flags */
3279 __HAL_SD_SDMMC_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
3282 /* We have received response, retrieve it for analysis */
3283 responseR1
= SDMMC_GetResponse(hsd
->Instance
, SDMMC_RESP1
);
3285 /* Find out card status */
3286 *pStatus
= (uint8_t)((responseR1
>> 9) & 0x0000000F);
3288 if((responseR1
& SD_OCR_ERRORBITS
) == SD_ALLZERO
)
3293 if((responseR1
& SD_OCR_ADDR_OUT_OF_RANGE
) == SD_OCR_ADDR_OUT_OF_RANGE
)
3295 return(SD_ADDR_OUT_OF_RANGE
);
3298 if((responseR1
& SD_OCR_ADDR_MISALIGNED
) == SD_OCR_ADDR_MISALIGNED
)
3300 return(SD_ADDR_MISALIGNED
);
3303 if((responseR1
& SD_OCR_BLOCK_LEN_ERR
) == SD_OCR_BLOCK_LEN_ERR
)
3305 return(SD_BLOCK_LEN_ERR
);
3308 if((responseR1
& SD_OCR_ERASE_SEQ_ERR
) == SD_OCR_ERASE_SEQ_ERR
)
3310 return(SD_ERASE_SEQ_ERR
);
3313 if((responseR1
& SD_OCR_BAD_ERASE_PARAM
) == SD_OCR_BAD_ERASE_PARAM
)
3315 return(SD_BAD_ERASE_PARAM
);
3318 if((responseR1
& SD_OCR_WRITE_PROT_VIOLATION
) == SD_OCR_WRITE_PROT_VIOLATION
)
3320 return(SD_WRITE_PROT_VIOLATION
);
3323 if((responseR1
& SD_OCR_LOCK_UNLOCK_FAILED
) == SD_OCR_LOCK_UNLOCK_FAILED
)
3325 return(SD_LOCK_UNLOCK_FAILED
);
3328 if((responseR1
& SD_OCR_COM_CRC_FAILED
) == SD_OCR_COM_CRC_FAILED
)
3330 return(SD_COM_CRC_FAILED
);
3333 if((responseR1
& SD_OCR_ILLEGAL_CMD
) == SD_OCR_ILLEGAL_CMD
)
3335 return(SD_ILLEGAL_CMD
);
3338 if((responseR1
& SD_OCR_CARD_ECC_FAILED
) == SD_OCR_CARD_ECC_FAILED
)
3340 return(SD_CARD_ECC_FAILED
);
3343 if((responseR1
& SD_OCR_CC_ERROR
) == SD_OCR_CC_ERROR
)
3345 return(SD_CC_ERROR
);
3348 if((responseR1
& SD_OCR_GENERAL_UNKNOWN_ERROR
) == SD_OCR_GENERAL_UNKNOWN_ERROR
)
3350 return(SD_GENERAL_UNKNOWN_ERROR
);
3353 if((responseR1
& SD_OCR_STREAM_READ_UNDERRUN
) == SD_OCR_STREAM_READ_UNDERRUN
)
3355 return(SD_STREAM_READ_UNDERRUN
);
3358 if((responseR1
& SD_OCR_STREAM_WRITE_OVERRUN
) == SD_OCR_STREAM_WRITE_OVERRUN
)
3360 return(SD_STREAM_WRITE_OVERRUN
);
3363 if((responseR1
& SD_OCR_CID_CSD_OVERWRITE
) == SD_OCR_CID_CSD_OVERWRITE
)
3365 return(SD_CID_CSD_OVERWRITE
);
3368 if((responseR1
& SD_OCR_WP_ERASE_SKIP
) == SD_OCR_WP_ERASE_SKIP
)
3370 return(SD_WP_ERASE_SKIP
);
3373 if((responseR1
& SD_OCR_CARD_ECC_DISABLED
) == SD_OCR_CARD_ECC_DISABLED
)
3375 return(SD_CARD_ECC_DISABLED
);
3378 if((responseR1
& SD_OCR_ERASE_RESET
) == SD_OCR_ERASE_RESET
)
3380 return(SD_ERASE_RESET
);
3383 if((responseR1
& SD_OCR_AKE_SEQ_ERROR
) == SD_OCR_AKE_SEQ_ERROR
)
3385 return(SD_AKE_SEQ_ERROR
);
3395 #endif /* HAL_SD_MODULE_ENABLED */
3405 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/