Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
stm32l4xx_hal_sd.c
00001 /** 00002 ****************************************************************************** 00003 * @file stm32l4xx_hal_sd.c 00004 * @author MCD Application Team 00005 * @version V1.5.1 00006 * @date 31-May-2016 00007 * @brief SD card HAL module driver. 00008 * This file provides firmware functions to manage the following 00009 * functionalities of the Secure Digital (SD) peripheral: 00010 * + Initialization and de-initialization functions 00011 * + IO operation functions 00012 * + Peripheral Control functions 00013 * + Peripheral State functions 00014 * 00015 @verbatim 00016 ============================================================================== 00017 ##### How to use this driver ##### 00018 ============================================================================== 00019 [..] 00020 This driver implements a high level communication layer for read and write from/to 00021 this memory. The needed STM32 hardware resources (SDMMC1 and GPIO) are performed by 00022 the user in HAL_SD_MspInit() function (MSP layer). 00023 Basically, the MSP layer configuration should be the same as we provide in the 00024 examples. 00025 You can easily tailor this configuration according to hardware resources. 00026 00027 [..] 00028 This driver is a generic layered driver for SDMMC memories which uses the HAL 00029 SDMMC driver functions to interface with SD and uSD cards devices. 00030 It is used as follows: 00031 00032 (#)Initialize the SDMMC1 low level resources by implementing the HAL_SD_MspInit() API: 00033 (##) Call the function HAL_RCCEx_PeriphCLKConfig with RCC_PERIPHCLK_SDMMC1 for 00034 PeriphClockSelection and select SDMMC1 clock source (MSI, main PLL or PLLSAI1) 00035 (##) Enable the SDMMC1 interface clock using __HAL_RCC_SDMMC1_CLK_ENABLE(); 00036 (##) SDMMC pins configuration for SD card 00037 (+++) Enable the clock for the SDMMC GPIOs using the functions __HAL_RCC_GPIOx_CLK_ENABLE(); 00038 (+++) Configure these SDMMC pins as alternate function pull-up using HAL_GPIO_Init() 00039 and according to your pin assignment; 00040 (##) DMA Configuration if you need to use DMA process (HAL_SD_ReadBlocks_DMA() 00041 and HAL_SD_WriteBlocks_DMA() APIs). 00042 (+++) Enable the DMAx interface clock using __HAL_RCC_DMAx_CLK_ENABLE(); 00043 (+++) Configure the DMA using the function HAL_DMA_Init() with predeclared and filled. 00044 (##) NVIC configuration if you need to use interrupt process when using DMA transfer. 00045 (+++) Configure the SDMMC and DMA interrupt priorities using functions 00046 HAL_NVIC_SetPriority(); DMA priority is superior to SDMMC's priority 00047 (+++) Enable the NVIC DMA and SDMMC IRQs using function HAL_NVIC_EnableIRQ() 00048 (+++) SDMMC interrupts are managed using the macros __HAL_SD_SDMMC_ENABLE_IT() 00049 and __HAL_SD_SDMMC_DISABLE_IT() inside the communication process. 00050 (+++) SDMMC interrupts pending bits are managed using the macros __HAL_SD_SDMMC_GET_IT() 00051 and __HAL_SD_SDMMC_CLEAR_IT() 00052 (#) At this stage, you can perform SD read/write/erase operations after SD card initialization 00053 00054 00055 *** SD Card Initialization and configuration *** 00056 ================================================ 00057 [..] 00058 To initialize the SD Card, use the HAL_SD_Init() function. It Initializes 00059 the SD Card and put it into StandBy State (Ready for data transfer). 00060 This function provide the following operations: 00061 00062 (#) Apply the SD Card initialization process at 400KHz and check the SD Card 00063 type (Standard Capacity or High Capacity). You can change or adapt this 00064 frequency by adjusting the "ClockDiv" field. 00065 The SD Card frequency (SDMMC_CK) is computed as follows: 00066 (++) 00067 00068 SDMMC_CK = SDMMCCLK / (ClockDiv + 2) 00069 00070 -@@- In initialization mode and according to the SD Card standard, 00071 make sure that the SDMMC_CK frequency doesn't exceed 400KHz. 00072 00073 (#) Get the SD CID and CSD data. All these information are managed by the SDCardInfo 00074 structure. This structure provide also ready computed SD Card capacity 00075 and Block size. 00076 00077 -@- These information are stored in SD handle structure in case of future use. 00078 00079 (#) Configure the SD Card Data transfer frequency. By Default, the card transfer 00080 frequency is set to 24MHz. You can change or adapt this frequency by adjusting 00081 the "ClockDiv" field. 00082 In transfer mode and according to the SD Card standard, make sure that the 00083 SDMMC_CK frequency doesn't exceed 25MHz and 50MHz in High-speed mode switch. 00084 To be able to use a frequency higher than 24MHz, you should use the SDMMC 00085 peripheral in bypass mode. Refer to the corresponding reference manual 00086 for more details. 00087 00088 (#) Select the corresponding SD Card according to the address read with the step 2. 00089 00090 (#) Configure the SD Card in wide bus mode: 4-bits data. 00091 00092 *** SD Card Read operation *** 00093 ============================== 00094 [..] 00095 (+) You can read from SD card in polling mode by using function HAL_SD_ReadBlocks(). 00096 This function support only 512-bytes block length (the block size should be 00097 chosen as 512 bytes). 00098 You can choose either one block read operation or multiple block read operation 00099 by adjusting the "NumberOfBlocks" parameter. 00100 00101 (+) You can read from SD card in DMA mode by using function HAL_SD_ReadBlocks_DMA(). 00102 This function support only 512-bytes block length (the block size should be 00103 chosen as 512 bytes). 00104 You can choose either one block read operation or multiple block read operation 00105 by adjusting the "NumberOfBlocks" parameter. 00106 After this, you have to call the function HAL_SD_CheckReadOperation(), to insure 00107 that the read transfer is done correctly in both DMA and SD sides. 00108 00109 *** SD Card Write operation *** 00110 =============================== 00111 [..] 00112 (+) You can write to SD card in polling mode by using function HAL_SD_WriteBlocks(). 00113 This function support only 512-bytes block length (the block size should be 00114 chosen as 512 bytes). 00115 You can choose either one block read operation or multiple block read operation 00116 by adjusting the "NumberOfBlocks" parameter. 00117 00118 (+) You can write to SD card in DMA mode by using function HAL_SD_WriteBlocks_DMA(). 00119 This function support only 512-bytes block length (the block size should be 00120 chosen as 512 byte). 00121 You can choose either one block read operation or multiple block read operation 00122 by adjusting the "NumberOfBlocks" parameter. 00123 After this, you have to call the function HAL_SD_CheckWriteOperation(), to insure 00124 that the write transfer is done correctly in both DMA and SD sides. 00125 00126 *** SD card status *** 00127 ====================== 00128 [..] 00129 (+) At any time, you can check the SD Card status and get the SD card state 00130 by using the HAL_SD_GetStatus() function. This function checks first if the 00131 SD card is still connected and then get the internal SD Card transfer state. 00132 (+) You can also get the SD card SD Status register by using the HAL_SD_SendSDStatus() 00133 function. 00134 00135 *** SD HAL driver macros list *** 00136 ================================== 00137 [..] 00138 Below the list of most used macros in SD HAL driver. 00139 00140 (+) __HAL_SD_SDMMC_ENABLE : Enable the SD device 00141 (+) __HAL_SD_SDMMC_DISABLE : Disable the SD device 00142 (+) __HAL_SD_SDMMC_DMA_ENABLE: Enable the SDMMC DMA transfer 00143 (+) __HAL_SD_SDMMC_DMA_DISABLE: Disable the SDMMC DMA transfer 00144 (+) __HAL_SD_SDMMC_ENABLE_IT: Enable the SD device interrupt 00145 (+) __HAL_SD_SDMMC_DISABLE_IT: Disable the SD device interrupt 00146 (+) __HAL_SD_SDMMC_GET_FLAG:Check whether the specified SD flag is set or not 00147 (+) __HAL_SD_SDMMC_CLEAR_FLAG: Clear the SD's pending flags 00148 [..] 00149 (@) You can refer to the SD HAL driver header file for more useful macros 00150 00151 @endverbatim 00152 ****************************************************************************** 00153 * @attention 00154 * 00155 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> 00156 * 00157 * Redistribution and use in source and binary forms, with or without modification, 00158 * are permitted provided that the following conditions are met: 00159 * 1. Redistributions of source code must retain the above copyright notice, 00160 * this list of conditions and the following disclaimer. 00161 * 2. Redistributions in binary form must reproduce the above copyright notice, 00162 * this list of conditions and the following disclaimer in the documentation 00163 * and/or other materials provided with the distribution. 00164 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00165 * may be used to endorse or promote products derived from this software 00166 * without specific prior written permission. 00167 * 00168 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00169 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00170 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00171 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00172 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00173 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00174 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00175 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00176 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00177 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00178 * 00179 ****************************************************************************** 00180 */ 00181 00182 /* Includes ------------------------------------------------------------------*/ 00183 #include "stm32l4xx_hal.h" 00184 00185 #if defined(SDMMC1) 00186 00187 /** @addtogroup STM32L4xx_HAL_Driver 00188 * @{ 00189 */ 00190 00191 /** @addtogroup SD 00192 * @{ 00193 */ 00194 00195 #ifdef HAL_SD_MODULE_ENABLED 00196 00197 /* Private typedef -----------------------------------------------------------*/ 00198 /* Private define ------------------------------------------------------------*/ 00199 /** @addtogroup SD_Private_Defines 00200 * @{ 00201 */ 00202 /** 00203 * @brief SDMMC Data block size 00204 */ 00205 #define DATA_BLOCK_SIZE ((uint32_t)(9 << 4)) 00206 /** 00207 * @brief SDMMC Static flags, Timeout, FIFO Address 00208 */ 00209 #define SDMMC_STATIC_FLAGS ((uint32_t)(SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_CTIMEOUT |\ 00210 SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_TXUNDERR | SDMMC_FLAG_RXOVERR |\ 00211 SDMMC_FLAG_CMDREND | SDMMC_FLAG_CMDSENT | SDMMC_FLAG_DATAEND |\ 00212 SDMMC_FLAG_DBCKEND)) 00213 00214 #define SDMMC_CMD0TIMEOUT ((uint32_t)0x00010000) 00215 00216 /** 00217 * @brief Mask for errors Card Status R1 (OCR Register) 00218 */ 00219 #define SD_OCR_ADDR_OUT_OF_RANGE ((uint32_t)0x80000000) 00220 #define SD_OCR_ADDR_MISALIGNED ((uint32_t)0x40000000) 00221 #define SD_OCR_BLOCK_LEN_ERR ((uint32_t)0x20000000) 00222 #define SD_OCR_ERASE_SEQ_ERR ((uint32_t)0x10000000) 00223 #define SD_OCR_BAD_ERASE_PARAM ((uint32_t)0x08000000) 00224 #define SD_OCR_WRITE_PROT_VIOLATION ((uint32_t)0x04000000) 00225 #define SD_OCR_LOCK_UNLOCK_FAILED ((uint32_t)0x01000000) 00226 #define SD_OCR_COM_CRC_FAILED ((uint32_t)0x00800000) 00227 #define SD_OCR_ILLEGAL_CMD ((uint32_t)0x00400000) 00228 #define SD_OCR_CARD_ECC_FAILED ((uint32_t)0x00200000) 00229 #define SD_OCR_CC_ERROR ((uint32_t)0x00100000) 00230 #define SD_OCR_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00080000) 00231 #define SD_OCR_STREAM_READ_UNDERRUN ((uint32_t)0x00040000) 00232 #define SD_OCR_STREAM_WRITE_OVERRUN ((uint32_t)0x00020000) 00233 #define SD_OCR_CID_CSD_OVERWRITE ((uint32_t)0x00010000) 00234 #define SD_OCR_WP_ERASE_SKIP ((uint32_t)0x00008000) 00235 #define SD_OCR_CARD_ECC_DISABLED ((uint32_t)0x00004000) 00236 #define SD_OCR_ERASE_RESET ((uint32_t)0x00002000) 00237 #define SD_OCR_AKE_SEQ_ERROR ((uint32_t)0x00000008) 00238 #define SD_OCR_ERRORBITS ((uint32_t)0xFDFFE008) 00239 00240 /** 00241 * @brief Masks for R6 Response 00242 */ 00243 #define SD_R6_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00002000) 00244 #define SD_R6_ILLEGAL_CMD ((uint32_t)0x00004000) 00245 #define SD_R6_COM_CRC_FAILED ((uint32_t)0x00008000) 00246 00247 #define SD_VOLTAGE_WINDOW_SD ((uint32_t)0x80100000) 00248 #define SD_HIGH_CAPACITY ((uint32_t)0x40000000) 00249 #define SD_STD_CAPACITY ((uint32_t)0x00000000) 00250 #define SD_CHECK_PATTERN ((uint32_t)0x000001AA) 00251 00252 #define SD_MAX_VOLT_TRIAL ((uint32_t)0x0000FFFF) 00253 #define SD_ALLZERO ((uint32_t)0x00000000) 00254 00255 #define SD_WIDE_BUS_SUPPORT ((uint32_t)0x00040000) 00256 #define SD_SINGLE_BUS_SUPPORT ((uint32_t)0x00010000) 00257 #define SD_CARD_LOCKED ((uint32_t)0x02000000) 00258 00259 #define SD_DATATIMEOUT ((uint32_t)0xFFFFFFFF) 00260 #define SD_0TO7BITS ((uint32_t)0x000000FF) 00261 #define SD_8TO15BITS ((uint32_t)0x0000FF00) 00262 #define SD_16TO23BITS ((uint32_t)0x00FF0000) 00263 #define SD_24TO31BITS ((uint32_t)0xFF000000) 00264 #define SD_MAX_DATA_LENGTH ((uint32_t)0x01FFFFFF) 00265 00266 #define SD_HALFFIFO ((uint32_t)0x00000008) 00267 #define SD_HALFFIFOBYTES ((uint32_t)0x00000020) 00268 00269 /** 00270 * @brief Command Class Supported 00271 */ 00272 #define SD_CCCC_LOCK_UNLOCK ((uint32_t)0x00000080) 00273 #define SD_CCCC_WRITE_PROT ((uint32_t)0x00000040) 00274 #define SD_CCCC_ERASE ((uint32_t)0x00000020) 00275 00276 /** 00277 * @brief Following commands are SD Card Specific commands. 00278 * SDMMC_APP_CMD should be sent before sending these commands. 00279 */ 00280 #define SD_SDMMC_SEND_IF_COND ((uint32_t)SD_CMD_HS_SEND_EXT_CSD) 00281 /** 00282 * @} 00283 */ 00284 00285 /* Private macro -------------------------------------------------------------*/ 00286 /* Private variables ---------------------------------------------------------*/ 00287 /* Private function prototypes -----------------------------------------------*/ 00288 /** @addtogroup SD_Private_Functions_Prototypes 00289 * @{ 00290 */ 00291 static HAL_SD_ErrorTypedef SD_Initialize_Cards(SD_HandleTypeDef *hsd); 00292 static HAL_SD_ErrorTypedef SD_Select_Deselect(SD_HandleTypeDef *hsd, uint64_t addr); 00293 static HAL_SD_ErrorTypedef SD_PowerON(SD_HandleTypeDef *hsd); 00294 static HAL_SD_ErrorTypedef SD_PowerOFF(SD_HandleTypeDef *hsd); 00295 static HAL_SD_ErrorTypedef SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus); 00296 static HAL_SD_CardStateTypedef SD_GetState(SD_HandleTypeDef *hsd); 00297 static HAL_SD_ErrorTypedef SD_IsCardProgramming(SD_HandleTypeDef *hsd, uint8_t *pStatus); 00298 static HAL_SD_ErrorTypedef SD_CmdError(SD_HandleTypeDef *hsd); 00299 static HAL_SD_ErrorTypedef SD_CmdResp1Error(SD_HandleTypeDef *hsd, uint8_t SD_CMD); 00300 static HAL_SD_ErrorTypedef SD_CmdResp7Error(SD_HandleTypeDef *hsd); 00301 static HAL_SD_ErrorTypedef SD_CmdResp3Error(SD_HandleTypeDef *hsd); 00302 static HAL_SD_ErrorTypedef SD_CmdResp2Error(SD_HandleTypeDef *hsd); 00303 static HAL_SD_ErrorTypedef SD_CmdResp6Error(SD_HandleTypeDef *hsd, uint8_t SD_CMD, uint16_t *pRCA); 00304 static HAL_SD_ErrorTypedef SD_WideBus_Enable(SD_HandleTypeDef *hsd); 00305 static HAL_SD_ErrorTypedef SD_WideBus_Disable(SD_HandleTypeDef *hsd); 00306 static HAL_SD_ErrorTypedef SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR); 00307 static void SD_DMA_RxCplt(DMA_HandleTypeDef *hdma); 00308 static void SD_DMA_RxError(DMA_HandleTypeDef *hdma); 00309 static void SD_DMA_TxCplt(DMA_HandleTypeDef *hdma); 00310 static void SD_DMA_TxError(DMA_HandleTypeDef *hdma); 00311 /** 00312 * @} 00313 */ 00314 /* Exported functions --------------------------------------------------------*/ 00315 /** @addtogroup SD_Exported_Functions 00316 * @{ 00317 */ 00318 00319 /** @addtogroup SD_Exported_Functions_Group1 00320 * @brief Initialization and de-initialization functions 00321 * 00322 @verbatim 00323 ============================================================================== 00324 ##### Initialization and de-initialization functions ##### 00325 ============================================================================== 00326 [..] 00327 This section provides functions allowing to initialize/de-initialize the SD 00328 card device to be ready for use. 00329 00330 00331 @endverbatim 00332 * @{ 00333 */ 00334 00335 /** 00336 * @brief Initializes the SD card according to the specified parameters in the 00337 SD_HandleTypeDef and initialize the associated handle. 00338 * @param hsd: SD handle 00339 * @param SDCardInfo: HAL_SD_CardInfoTypedef structure for SD card information 00340 * @retval HAL SD error state 00341 */ 00342 HAL_SD_ErrorTypedef HAL_SD_Init(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypedef *SDCardInfo) 00343 { 00344 __IO HAL_SD_ErrorTypedef errorstate = SD_OK; 00345 SD_InitTypeDef tmpinit; 00346 00347 /* Initialize the low level hardware (MSP) */ 00348 HAL_SD_MspInit(hsd); 00349 00350 /* Default SDMMC peripheral configuration for SD card initialization */ 00351 tmpinit.ClockEdge = SDMMC_CLOCK_EDGE_RISING; 00352 tmpinit.ClockBypass = SDMMC_CLOCK_BYPASS_DISABLE; 00353 tmpinit.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE; 00354 tmpinit.BusWide = SDMMC_BUS_WIDE_1B; 00355 tmpinit.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE; 00356 tmpinit.ClockDiv = SDMMC_INIT_CLK_DIV; 00357 00358 /* Initialize SDMMC peripheral interface with default configuration */ 00359 SDMMC_Init(hsd->Instance, tmpinit); 00360 00361 /* Identify card operating voltage */ 00362 errorstate = SD_PowerON(hsd); 00363 00364 if(errorstate != SD_OK) 00365 { 00366 return errorstate; 00367 } 00368 00369 /* Initialize the present SDMMC card(s) and put them in idle state */ 00370 errorstate = SD_Initialize_Cards(hsd); 00371 00372 if (errorstate != SD_OK) 00373 { 00374 return errorstate; 00375 } 00376 00377 /* Read CSD/CID MSD registers */ 00378 errorstate = HAL_SD_Get_CardInfo(hsd, SDCardInfo); 00379 00380 if (errorstate == SD_OK) 00381 { 00382 /* Select the Card */ 00383 errorstate = SD_Select_Deselect(hsd, (uint32_t)(((uint32_t)SDCardInfo->RCA) << 16)); 00384 } 00385 00386 /* Configure SDMMC peripheral interface */ 00387 SDMMC_Init(hsd->Instance, hsd->Init); 00388 00389 return errorstate; 00390 } 00391 00392 /** 00393 * @brief De-Initializes the SD card. 00394 * @param hsd: SD handle 00395 * @retval HAL status 00396 */ 00397 HAL_StatusTypeDef HAL_SD_DeInit(SD_HandleTypeDef *hsd) 00398 { 00399 00400 /* Set SD power state to off */ 00401 SD_PowerOFF(hsd); 00402 00403 /* De-Initialize the MSP layer */ 00404 HAL_SD_MspDeInit(hsd); 00405 00406 return HAL_OK; 00407 } 00408 00409 00410 /** 00411 * @brief Initializes the SD MSP. 00412 * @param hsd: SD handle 00413 * @retval None 00414 */ 00415 __weak void HAL_SD_MspInit(SD_HandleTypeDef *hsd) 00416 { 00417 /* Prevent unused argument(s) compilation warning */ 00418 UNUSED(hsd); 00419 00420 /* NOTE : This function should not be modified, when the callback is needed, 00421 the HAL_SD_MspInit could be implemented in the user file 00422 */ 00423 } 00424 00425 /** 00426 * @brief De-Initialize SD MSP. 00427 * @param hsd: SD handle 00428 * @retval None 00429 */ 00430 __weak void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd) 00431 { 00432 /* Prevent unused argument(s) compilation warning */ 00433 UNUSED(hsd); 00434 00435 /* NOTE : This function should not be modified, when the callback is needed, 00436 the HAL_SD_MspDeInit could be implemented in the user file 00437 */ 00438 } 00439 00440 /** 00441 * @} 00442 */ 00443 00444 /** @addtogroup SD_Exported_Functions_Group2 00445 * @brief Data transfer functions 00446 * 00447 @verbatim 00448 ============================================================================== 00449 ##### IO operation functions ##### 00450 ============================================================================== 00451 [..] 00452 This subsection provides a set of functions allowing to manage the data 00453 transfer from/to SD card. 00454 00455 @endverbatim 00456 * @{ 00457 */ 00458 00459 /** 00460 * @brief Reads block(s) from a specified address in a card. The Data transfer 00461 * is managed by polling mode. 00462 * @param hsd: SD handle 00463 * @param pReadBuffer: pointer to the buffer that will contain the received data 00464 * @param ReadAddr: Address from where data is to be read 00465 * @param BlockSize: SD card Data block size 00466 * @note BlockSize must be 512 bytes. 00467 * @param NumberOfBlocks: Number of SD blocks to read 00468 * @retval SD Card error state 00469 */ 00470 HAL_SD_ErrorTypedef HAL_SD_ReadBlocks(SD_HandleTypeDef *hsd, uint32_t *pReadBuffer, uint64_t ReadAddr, uint32_t BlockSize, uint32_t NumberOfBlocks) 00471 { 00472 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure; 00473 SDMMC_DataInitTypeDef sdmmc_datainitstructure; 00474 HAL_SD_ErrorTypedef errorstate = SD_OK; 00475 uint32_t count = 0, *tempbuff = (uint32_t *)pReadBuffer; 00476 00477 /* Initialize data control register */ 00478 hsd->Instance->DCTRL = 0; 00479 00480 if (hsd->CardType == HIGH_CAPACITY_SD_CARD) 00481 { 00482 BlockSize = 512; 00483 ReadAddr /= 512; 00484 } 00485 00486 /* Set Block Size for Card */ 00487 sdmmc_cmdinitstructure.Argument = (uint32_t) BlockSize; 00488 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN; 00489 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT; 00490 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO; 00491 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE; 00492 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 00493 00494 /* Check for error conditions */ 00495 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN); 00496 00497 if (errorstate != SD_OK) 00498 { 00499 return errorstate; 00500 } 00501 00502 /* Configure the SD DPSM (Data Path State Machine) */ 00503 sdmmc_datainitstructure.DataTimeOut = SD_DATATIMEOUT; 00504 sdmmc_datainitstructure.DataLength = NumberOfBlocks * BlockSize; 00505 sdmmc_datainitstructure.DataBlockSize = DATA_BLOCK_SIZE; 00506 sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC; 00507 sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK; 00508 sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE; 00509 SDMMC_DataConfig(hsd->Instance, &sdmmc_datainitstructure); 00510 00511 if(NumberOfBlocks > 1) 00512 { 00513 /* Send CMD18 READ_MULT_BLOCK with argument data address */ 00514 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_READ_MULT_BLOCK; 00515 } 00516 else 00517 { 00518 /* Send CMD17 READ_SINGLE_BLOCK */ 00519 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_READ_SINGLE_BLOCK; 00520 } 00521 00522 sdmmc_cmdinitstructure.Argument = (uint32_t)ReadAddr; 00523 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 00524 00525 /* Read block(s) in polling mode */ 00526 if(NumberOfBlocks > 1) 00527 { 00528 /* Check for error conditions */ 00529 errorstate = SD_CmdResp1Error(hsd, SD_CMD_READ_MULT_BLOCK); 00530 00531 if (errorstate != SD_OK) 00532 { 00533 return errorstate; 00534 } 00535 00536 /* Poll on SDMMC flags */ 00537 while(!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND)) 00538 { 00539 if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF)) 00540 { 00541 /* Read data from SDMMC Rx FIFO */ 00542 for (count = 0; count < 8; count++) 00543 { 00544 *(tempbuff + count) = SDMMC_ReadFIFO(hsd->Instance); 00545 } 00546 00547 tempbuff += 8; 00548 } 00549 } 00550 } 00551 else 00552 { 00553 /* Check for error conditions */ 00554 errorstate = SD_CmdResp1Error(hsd, SD_CMD_READ_SINGLE_BLOCK); 00555 00556 if (errorstate != SD_OK) 00557 { 00558 return errorstate; 00559 } 00560 00561 /* In case of single block transfer, no need of stop transfer at all */ 00562 while(!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND)) 00563 { 00564 if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF)) 00565 { 00566 /* Read data from SDMMC Rx FIFO */ 00567 for (count = 0; count < 8; count++) 00568 { 00569 *(tempbuff + count) = SDMMC_ReadFIFO(hsd->Instance); 00570 } 00571 00572 tempbuff += 8; 00573 } 00574 } 00575 } 00576 00577 /* Send stop transmission command in case of multiblock read */ 00578 if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1)) 00579 { 00580 if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) ||\ 00581 (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\ 00582 (hsd->CardType == HIGH_CAPACITY_SD_CARD)) 00583 { 00584 /* Send stop transmission command */ 00585 errorstate = HAL_SD_StopTransfer(hsd); 00586 } 00587 } 00588 00589 /* Get error state */ 00590 if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT)) 00591 { 00592 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT); 00593 00594 errorstate = SD_DATA_TIMEOUT; 00595 00596 return errorstate; 00597 } 00598 else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL)) 00599 { 00600 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL); 00601 00602 errorstate = SD_DATA_CRC_FAIL; 00603 00604 return errorstate; 00605 } 00606 else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR)) 00607 { 00608 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR); 00609 00610 errorstate = SD_RX_OVERRUN; 00611 00612 return errorstate; 00613 } 00614 else 00615 { 00616 /* No error flag set */ 00617 } 00618 00619 count = SD_DATATIMEOUT; 00620 00621 /* Empty FIFO if there is still any data */ 00622 while ((__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL)) && (count > 0)) 00623 { 00624 *tempbuff = SDMMC_ReadFIFO(hsd->Instance); 00625 tempbuff++; 00626 count--; 00627 } 00628 00629 /* Clear all the static flags */ 00630 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 00631 00632 return errorstate; 00633 } 00634 00635 /** 00636 * @brief Allows to write block(s) to a specified address in a card. The Data 00637 * transfer is managed by polling mode. 00638 * @param hsd: SD handle 00639 * @param pWriteBuffer: pointer to the buffer that will contain the data to transmit 00640 * @param WriteAddr: Address from where data is to be written 00641 * @param BlockSize: SD card Data block size 00642 * @note BlockSize must be 512 bytes. 00643 * @param NumberOfBlocks: Number of SD blocks to write 00644 * @retval SD Card error state 00645 */ 00646 HAL_SD_ErrorTypedef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, uint32_t *pWriteBuffer, uint64_t WriteAddr, uint32_t BlockSize, uint32_t NumberOfBlocks) 00647 { 00648 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure; 00649 SDMMC_DataInitTypeDef sdmmc_datainitstructure; 00650 HAL_SD_ErrorTypedef errorstate = SD_OK; 00651 uint32_t totalnumberofbytes = 0, bytestransferred = 0, count = 0, restwords = 0; 00652 uint32_t *tempbuff = (uint32_t *)pWriteBuffer; 00653 uint8_t cardstate = 0; 00654 00655 /* Initialize data control register */ 00656 hsd->Instance->DCTRL = 0; 00657 00658 if (hsd->CardType == HIGH_CAPACITY_SD_CARD) 00659 { 00660 BlockSize = 512; 00661 WriteAddr /= 512; 00662 } 00663 00664 /* Set Block Size for Card */ 00665 sdmmc_cmdinitstructure.Argument = (uint32_t)BlockSize; 00666 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN; 00667 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT; 00668 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO; 00669 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE; 00670 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 00671 00672 /* Check for error conditions */ 00673 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN); 00674 00675 if (errorstate != SD_OK) 00676 { 00677 return errorstate; 00678 } 00679 00680 if(NumberOfBlocks > 1) 00681 { 00682 /* Send CMD25 WRITE_MULT_BLOCK with argument data address */ 00683 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_WRITE_MULT_BLOCK; 00684 } 00685 else 00686 { 00687 /* Send CMD24 WRITE_SINGLE_BLOCK */ 00688 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_WRITE_SINGLE_BLOCK; 00689 } 00690 00691 sdmmc_cmdinitstructure.Argument = (uint32_t)WriteAddr; 00692 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 00693 00694 /* Check for error conditions */ 00695 if(NumberOfBlocks > 1) 00696 { 00697 errorstate = SD_CmdResp1Error(hsd, SD_CMD_WRITE_MULT_BLOCK); 00698 } 00699 else 00700 { 00701 errorstate = SD_CmdResp1Error(hsd, SD_CMD_WRITE_SINGLE_BLOCK); 00702 } 00703 00704 if (errorstate != SD_OK) 00705 { 00706 return errorstate; 00707 } 00708 00709 /* Set total number of bytes to write */ 00710 totalnumberofbytes = NumberOfBlocks * BlockSize; 00711 00712 /* Configure the SD DPSM (Data Path State Machine) */ 00713 sdmmc_datainitstructure.DataTimeOut = SD_DATATIMEOUT; 00714 sdmmc_datainitstructure.DataLength = NumberOfBlocks * BlockSize; 00715 sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B; 00716 sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD; 00717 sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK; 00718 sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE; 00719 SDMMC_DataConfig(hsd->Instance, &sdmmc_datainitstructure); 00720 00721 /* Write block(s) in polling mode */ 00722 if(NumberOfBlocks > 1) 00723 { 00724 while(!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND)) 00725 { 00726 if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_TXFIFOHE)) 00727 { 00728 if ((totalnumberofbytes - bytestransferred) < 32) 00729 { 00730 restwords = ((totalnumberofbytes - bytestransferred) % 4 == 0) ? ((totalnumberofbytes - bytestransferred) / 4) : (( totalnumberofbytes - bytestransferred) / 4 + 1); 00731 00732 /* Write data to SDMMC Tx FIFO */ 00733 for (count = 0; count < restwords; count++) 00734 { 00735 SDMMC_WriteFIFO(hsd->Instance, tempbuff); 00736 tempbuff++; 00737 bytestransferred += 4; 00738 } 00739 } 00740 else 00741 { 00742 /* Write data to SDMMC Tx FIFO */ 00743 for (count = 0; count < 8; count++) 00744 { 00745 SDMMC_WriteFIFO(hsd->Instance, (tempbuff + count)); 00746 } 00747 00748 tempbuff += 8; 00749 bytestransferred += 32; 00750 } 00751 } 00752 } 00753 } 00754 else 00755 { 00756 /* In case of single data block transfer no need of stop command at all */ 00757 while(!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND)) 00758 { 00759 if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_TXFIFOHE)) 00760 { 00761 if ((totalnumberofbytes - bytestransferred) < 32) 00762 { 00763 restwords = ((totalnumberofbytes - bytestransferred) % 4 == 0) ? ((totalnumberofbytes - bytestransferred) / 4) : (( totalnumberofbytes - bytestransferred) / 4 + 1); 00764 00765 /* Write data to SDMMC Tx FIFO */ 00766 for (count = 0; count < restwords; count++) 00767 { 00768 SDMMC_WriteFIFO(hsd->Instance, tempbuff); 00769 tempbuff++; 00770 bytestransferred += 4; 00771 } 00772 } 00773 else 00774 { 00775 /* Write data to SDMMC Tx FIFO */ 00776 for (count = 0; count < 8; count++) 00777 { 00778 SDMMC_WriteFIFO(hsd->Instance, (tempbuff + count)); 00779 } 00780 00781 tempbuff += 8; 00782 bytestransferred += 32; 00783 } 00784 } 00785 } 00786 } 00787 00788 /* Send stop transmission command in case of multiblock write */ 00789 if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1)) 00790 { 00791 if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\ 00792 (hsd->CardType == HIGH_CAPACITY_SD_CARD)) 00793 { 00794 /* Send stop transmission command */ 00795 errorstate = HAL_SD_StopTransfer(hsd); 00796 } 00797 } 00798 00799 /* Get error state */ 00800 if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT)) 00801 { 00802 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT); 00803 00804 errorstate = SD_DATA_TIMEOUT; 00805 00806 return errorstate; 00807 } 00808 else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL)) 00809 { 00810 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL); 00811 00812 errorstate = SD_DATA_CRC_FAIL; 00813 00814 return errorstate; 00815 } 00816 else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR)) 00817 { 00818 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_TXUNDERR); 00819 00820 errorstate = SD_TX_UNDERRUN; 00821 00822 return errorstate; 00823 } 00824 else 00825 { 00826 /* No error flag set */ 00827 } 00828 00829 /* Clear all the static flags */ 00830 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 00831 00832 /* Wait till the card is in programming state */ 00833 errorstate = SD_IsCardProgramming(hsd, &cardstate); 00834 00835 while ((errorstate == SD_OK) && ((cardstate == SD_CARD_PROGRAMMING) || (cardstate == SD_CARD_RECEIVING))) 00836 { 00837 errorstate = SD_IsCardProgramming(hsd, &cardstate); 00838 } 00839 00840 return errorstate; 00841 } 00842 00843 /** 00844 * @brief Reads block(s) from a specified address in a card. The Data transfer 00845 * is managed by DMA mode. 00846 * @note This API should be followed by the function HAL_SD_CheckReadOperation() 00847 * to check the completion of the read process 00848 * @param hsd: SD handle 00849 * @param pReadBuffer: Pointer to the buffer that will contain the received data 00850 * @param ReadAddr: Address from where data is to be read 00851 * @param BlockSize: SD card Data block size 00852 * @note BlockSize must be 512 bytes. 00853 * @param NumberOfBlocks: Number of blocks to read. 00854 * @retval SD Card error state 00855 */ 00856 HAL_SD_ErrorTypedef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint32_t *pReadBuffer, uint64_t ReadAddr, uint32_t BlockSize, uint32_t NumberOfBlocks) 00857 { 00858 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure; 00859 SDMMC_DataInitTypeDef sdmmc_datainitstructure; 00860 HAL_SD_ErrorTypedef errorstate = SD_OK; 00861 00862 /* Initialize data control register */ 00863 hsd->Instance->DCTRL = 0; 00864 00865 /* Initialize handle flags */ 00866 hsd->SdTransferCplt = 0; 00867 hsd->DmaTransferCplt = 0; 00868 hsd->SdTransferErr = SD_OK; 00869 00870 /* Initialize SD Read operation */ 00871 if(NumberOfBlocks > 1) 00872 { 00873 hsd->SdOperation = SD_READ_MULTIPLE_BLOCK; 00874 } 00875 else 00876 { 00877 hsd->SdOperation = SD_READ_SINGLE_BLOCK; 00878 } 00879 00880 /* Enable transfer interrupts */ 00881 __HAL_SD_SDMMC_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL |\ 00882 SDMMC_IT_DTIMEOUT |\ 00883 SDMMC_IT_DATAEND |\ 00884 SDMMC_IT_RXOVERR)); 00885 00886 /* Enable SDMMC DMA transfer */ 00887 __HAL_SD_SDMMC_DMA_ENABLE(hsd); 00888 00889 /* Configure DMA user callbacks */ 00890 hsd->hdmarx->XferCpltCallback = SD_DMA_RxCplt; 00891 hsd->hdmarx->XferErrorCallback = SD_DMA_RxError; 00892 00893 /* Change DMA direction Periph to Memory */ 00894 hsd->hdmarx->Init.Direction = DMA_PERIPH_TO_MEMORY; 00895 hsd->hdmarx->Instance->CCR &= ~DMA_MEMORY_TO_PERIPH; 00896 00897 /* Enable the DMA Channel */ 00898 HAL_DMA_Start_IT(hsd->hdmarx, (uint32_t)&hsd->Instance->FIFO, (uint32_t)pReadBuffer, (uint32_t)(BlockSize * NumberOfBlocks)/4); 00899 00900 if (hsd->CardType == HIGH_CAPACITY_SD_CARD) 00901 { 00902 BlockSize = 512; 00903 ReadAddr /= 512; 00904 } 00905 00906 /* Set Block Size for Card */ 00907 sdmmc_cmdinitstructure.Argument = (uint32_t)BlockSize; 00908 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN; 00909 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT; 00910 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO; 00911 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE; 00912 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 00913 00914 /* Check for error conditions */ 00915 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN); 00916 00917 if (errorstate != SD_OK) 00918 { 00919 return errorstate; 00920 } 00921 00922 /* Configure the SD DPSM (Data Path State Machine) */ 00923 sdmmc_datainitstructure.DataTimeOut = SD_DATATIMEOUT; 00924 sdmmc_datainitstructure.DataLength = BlockSize * NumberOfBlocks; 00925 sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B; 00926 sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC; 00927 sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK; 00928 sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE; 00929 SDMMC_DataConfig(hsd->Instance, &sdmmc_datainitstructure); 00930 00931 /* Check number of blocks command */ 00932 if(NumberOfBlocks > 1) 00933 { 00934 /* Send CMD18 READ_MULT_BLOCK with argument data address */ 00935 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_READ_MULT_BLOCK; 00936 } 00937 else 00938 { 00939 /* Send CMD17 READ_SINGLE_BLOCK */ 00940 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_READ_SINGLE_BLOCK; 00941 } 00942 00943 sdmmc_cmdinitstructure.Argument = (uint32_t)ReadAddr; 00944 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 00945 00946 /* Check for error conditions */ 00947 if(NumberOfBlocks > 1) 00948 { 00949 errorstate = SD_CmdResp1Error(hsd, SD_CMD_READ_MULT_BLOCK); 00950 } 00951 else 00952 { 00953 errorstate = SD_CmdResp1Error(hsd, SD_CMD_READ_SINGLE_BLOCK); 00954 } 00955 00956 /* Update the SD transfer error in SD handle */ 00957 hsd->SdTransferErr = errorstate; 00958 00959 return errorstate; 00960 } 00961 00962 00963 /** 00964 * @brief Writes block(s) to a specified address in a card. The Data transfer 00965 * is managed by DMA mode. 00966 * @note This API should be followed by the function HAL_SD_CheckWriteOperation() 00967 * to check the completion of the write process (by SD current status polling). 00968 * @param hsd: SD handle 00969 * @param pWriteBuffer: pointer to the buffer that will contain the data to transmit 00970 * @param WriteAddr: Address from where data is to be read 00971 * @param BlockSize: the SD card Data block size 00972 * @note BlockSize must be 512 bytes. 00973 * @param NumberOfBlocks: Number of blocks to write 00974 * @retval SD Card error state 00975 */ 00976 HAL_SD_ErrorTypedef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint32_t *pWriteBuffer, uint64_t WriteAddr, uint32_t BlockSize, uint32_t NumberOfBlocks) 00977 { 00978 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure; 00979 SDMMC_DataInitTypeDef sdmmc_datainitstructure; 00980 HAL_SD_ErrorTypedef errorstate = SD_OK; 00981 00982 /* Initialize data control register */ 00983 hsd->Instance->DCTRL = 0; 00984 00985 /* Initialize handle flags */ 00986 hsd->SdTransferCplt = 0; 00987 hsd->DmaTransferCplt = 0; 00988 hsd->SdTransferErr = SD_OK; 00989 00990 /* Initialize SD Write operation */ 00991 if(NumberOfBlocks > 1) 00992 { 00993 hsd->SdOperation = SD_WRITE_MULTIPLE_BLOCK; 00994 } 00995 else 00996 { 00997 hsd->SdOperation = SD_WRITE_SINGLE_BLOCK; 00998 } 00999 01000 /* Enable transfer interrupts */ 01001 __HAL_SD_SDMMC_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL |\ 01002 SDMMC_IT_DTIMEOUT |\ 01003 SDMMC_IT_DATAEND |\ 01004 SDMMC_IT_TXUNDERR)); 01005 01006 /* Configure DMA user callbacks */ 01007 hsd->hdmatx->XferCpltCallback = SD_DMA_TxCplt; 01008 hsd->hdmatx->XferErrorCallback = SD_DMA_TxError; 01009 01010 /* Change DMA direction Memory to Periph */ 01011 hsd->hdmatx->Init.Direction = DMA_MEMORY_TO_PERIPH; 01012 hsd->hdmatx->Instance->CCR |= DMA_MEMORY_TO_PERIPH; 01013 01014 /* Enable the DMA Channel */ 01015 HAL_DMA_Start_IT(hsd->hdmatx, (uint32_t)pWriteBuffer, (uint32_t)&hsd->Instance->FIFO, (uint32_t)(BlockSize * NumberOfBlocks)/4); 01016 01017 /* Enable SDMMC DMA transfer */ 01018 __HAL_SD_SDMMC_DMA_ENABLE(hsd); 01019 01020 if (hsd->CardType == HIGH_CAPACITY_SD_CARD) 01021 { 01022 BlockSize = 512; 01023 WriteAddr /= 512; 01024 } 01025 01026 /* Set Block Size for Card */ 01027 sdmmc_cmdinitstructure.Argument = (uint32_t)BlockSize; 01028 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN; 01029 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT; 01030 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO; 01031 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE; 01032 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 01033 01034 /* Check for error conditions */ 01035 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN); 01036 01037 if (errorstate != SD_OK) 01038 { 01039 return errorstate; 01040 } 01041 01042 /* Check number of blocks command */ 01043 if(NumberOfBlocks <= 1) 01044 { 01045 /* Send CMD24 WRITE_SINGLE_BLOCK */ 01046 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_WRITE_SINGLE_BLOCK; 01047 } 01048 else 01049 { 01050 /* Send CMD25 WRITE_MULT_BLOCK with argument data address */ 01051 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_WRITE_MULT_BLOCK; 01052 } 01053 01054 sdmmc_cmdinitstructure.Argument = (uint32_t)WriteAddr; 01055 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 01056 01057 /* Check for error conditions */ 01058 if(NumberOfBlocks > 1) 01059 { 01060 errorstate = SD_CmdResp1Error(hsd, SD_CMD_WRITE_MULT_BLOCK); 01061 } 01062 else 01063 { 01064 errorstate = SD_CmdResp1Error(hsd, SD_CMD_WRITE_SINGLE_BLOCK); 01065 } 01066 01067 if (errorstate != SD_OK) 01068 { 01069 return errorstate; 01070 } 01071 01072 /* Configure the SD DPSM (Data Path State Machine) */ 01073 sdmmc_datainitstructure.DataTimeOut = SD_DATATIMEOUT; 01074 sdmmc_datainitstructure.DataLength = BlockSize * NumberOfBlocks; 01075 sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B; 01076 sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD; 01077 sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK; 01078 sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE; 01079 SDMMC_DataConfig(hsd->Instance, &sdmmc_datainitstructure); 01080 01081 hsd->SdTransferErr = errorstate; 01082 01083 return errorstate; 01084 } 01085 01086 /** 01087 * @brief This function waits until the SD DMA data read transfer is finished. 01088 * This API should be called after HAL_SD_ReadBlocks_DMA() function 01089 * to insure that all data sent by the card is already transferred by the 01090 * DMA controller. 01091 * @param hsd: SD handle 01092 * @param Timeout: Timeout duration 01093 * @retval SD Card error state 01094 */ 01095 HAL_SD_ErrorTypedef HAL_SD_CheckReadOperation(SD_HandleTypeDef *hsd, uint32_t Timeout) 01096 { 01097 HAL_SD_ErrorTypedef errorstate = SD_OK; 01098 uint32_t timeout = Timeout; 01099 uint32_t tmp1, tmp2; 01100 HAL_SD_ErrorTypedef tmp3; 01101 01102 /* Wait for DMA/SD transfer end or SD error variables to be in SD handle */ 01103 tmp1 = hsd->DmaTransferCplt; 01104 tmp2 = hsd->SdTransferCplt; 01105 tmp3 = (HAL_SD_ErrorTypedef)hsd->SdTransferErr; 01106 01107 while (((tmp1 & tmp2) == 0) && (tmp3 == SD_OK) && (timeout > 0)) 01108 { 01109 tmp1 = hsd->DmaTransferCplt; 01110 tmp2 = hsd->SdTransferCplt; 01111 tmp3 = (HAL_SD_ErrorTypedef)hsd->SdTransferErr; 01112 timeout--; 01113 } 01114 01115 timeout = Timeout; 01116 01117 /* Wait until the Rx transfer is no longer active */ 01118 while((__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXACT)) && (timeout > 0)) 01119 { 01120 timeout--; 01121 } 01122 01123 /* Send stop command in multiblock read */ 01124 if (hsd->SdOperation == SD_READ_MULTIPLE_BLOCK) 01125 { 01126 errorstate = HAL_SD_StopTransfer(hsd); 01127 } 01128 01129 if ((timeout == 0) && (errorstate == SD_OK)) 01130 { 01131 errorstate = SD_DATA_TIMEOUT; 01132 } 01133 01134 /* Clear all the static flags */ 01135 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 01136 01137 /* Return error state */ 01138 if (hsd->SdTransferErr != SD_OK) 01139 { 01140 return (HAL_SD_ErrorTypedef)(hsd->SdTransferErr); 01141 } 01142 01143 return errorstate; 01144 } 01145 01146 /** 01147 * @brief This function waits until the SD DMA data write transfer is finished. 01148 * This API should be called after HAL_SD_WriteBlocks_DMA() function 01149 * to insure that all data sent by the card is already transferred by the 01150 * DMA controller. 01151 * @param hsd: SD handle 01152 * @param Timeout: Timeout duration 01153 * @retval SD Card error state 01154 */ 01155 HAL_SD_ErrorTypedef HAL_SD_CheckWriteOperation(SD_HandleTypeDef *hsd, uint32_t Timeout) 01156 { 01157 HAL_SD_ErrorTypedef errorstate = SD_OK; 01158 uint32_t timeout = Timeout; 01159 uint32_t tmp1, tmp2; 01160 HAL_SD_ErrorTypedef tmp3; 01161 01162 /* Wait for DMA/SD transfer end or SD error variables to be in SD handle */ 01163 tmp1 = hsd->DmaTransferCplt; 01164 tmp2 = hsd->SdTransferCplt; 01165 tmp3 = (HAL_SD_ErrorTypedef)hsd->SdTransferErr; 01166 01167 while (((tmp1 & tmp2) == 0) && (tmp3 == SD_OK) && (timeout > 0)) 01168 { 01169 tmp1 = hsd->DmaTransferCplt; 01170 tmp2 = hsd->SdTransferCplt; 01171 tmp3 = (HAL_SD_ErrorTypedef)hsd->SdTransferErr; 01172 timeout--; 01173 } 01174 01175 timeout = Timeout; 01176 01177 /* Wait until the Tx transfer is no longer active */ 01178 while((__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_TXACT)) && (timeout > 0)) 01179 { 01180 timeout--; 01181 } 01182 01183 /* Send stop command in multiblock write */ 01184 if (hsd->SdOperation == SD_WRITE_MULTIPLE_BLOCK) 01185 { 01186 errorstate = HAL_SD_StopTransfer(hsd); 01187 } 01188 01189 if ((timeout == 0) && (errorstate == SD_OK)) 01190 { 01191 errorstate = SD_DATA_TIMEOUT; 01192 } 01193 01194 /* Clear all the static flags */ 01195 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 01196 01197 /* Return error state */ 01198 if (hsd->SdTransferErr != SD_OK) 01199 { 01200 return (HAL_SD_ErrorTypedef)(hsd->SdTransferErr); 01201 } 01202 01203 /* Wait until write is complete */ 01204 while(HAL_SD_GetStatus(hsd) != SD_TRANSFER_OK) 01205 { 01206 } 01207 01208 return errorstate; 01209 } 01210 01211 /** 01212 * @brief Erases the specified memory area of the given SD card. 01213 * @param hsd: SD handle 01214 * @param startaddr: Start byte address 01215 * @param endaddr: End byte address 01216 * @retval SD Card error state 01217 */ 01218 HAL_SD_ErrorTypedef HAL_SD_Erase(SD_HandleTypeDef *hsd, uint64_t startaddr, uint64_t endaddr) 01219 { 01220 HAL_SD_ErrorTypedef errorstate = SD_OK; 01221 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure; 01222 01223 uint32_t delay = 0; 01224 __IO uint32_t maxdelay = 0; 01225 uint8_t cardstate = 0; 01226 01227 /* Check if the card command class supports erase command */ 01228 if (((hsd->CSD[1] >> 20) & SD_CCCC_ERASE) == 0) 01229 { 01230 errorstate = SD_REQUEST_NOT_APPLICABLE; 01231 01232 return errorstate; 01233 } 01234 01235 /* Get max delay value */ 01236 maxdelay = 120000 / (((hsd->Instance->CLKCR) & 0xFF) + 2); 01237 01238 if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SD_CARD_LOCKED) == SD_CARD_LOCKED) 01239 { 01240 errorstate = SD_LOCK_UNLOCK_FAILED; 01241 01242 return errorstate; 01243 } 01244 01245 /* Get start and end block for high capacity cards */ 01246 if (hsd->CardType == HIGH_CAPACITY_SD_CARD) 01247 { 01248 startaddr /= 512; 01249 endaddr /= 512; 01250 } 01251 01252 /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */ 01253 if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\ 01254 (hsd->CardType == HIGH_CAPACITY_SD_CARD)) 01255 { 01256 /* Send CMD32 SD_ERASE_GRP_START with argument as addr */ 01257 sdmmc_cmdinitstructure.Argument =(uint32_t)startaddr; 01258 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SD_ERASE_GRP_START; 01259 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT; 01260 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO; 01261 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE; 01262 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 01263 01264 /* Check for error conditions */ 01265 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SD_ERASE_GRP_START); 01266 01267 if (errorstate != SD_OK) 01268 { 01269 return errorstate; 01270 } 01271 01272 /* Send CMD33 SD_ERASE_GRP_END with argument as addr */ 01273 sdmmc_cmdinitstructure.Argument = (uint32_t)endaddr; 01274 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SD_ERASE_GRP_END; 01275 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 01276 01277 /* Check for error conditions */ 01278 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SD_ERASE_GRP_END); 01279 01280 if (errorstate != SD_OK) 01281 { 01282 return errorstate; 01283 } 01284 } 01285 01286 /* Send CMD38 ERASE */ 01287 sdmmc_cmdinitstructure.Argument = 0; 01288 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_ERASE; 01289 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 01290 01291 /* Check for error conditions */ 01292 errorstate = SD_CmdResp1Error(hsd, SD_CMD_ERASE); 01293 01294 if (errorstate != SD_OK) 01295 { 01296 return errorstate; 01297 } 01298 01299 for (; delay < maxdelay; delay++) 01300 { 01301 } 01302 01303 /* Wait until the card is in programming state */ 01304 errorstate = SD_IsCardProgramming(hsd, &cardstate); 01305 01306 delay = SD_DATATIMEOUT; 01307 01308 while ((delay > 0) && (errorstate == SD_OK) && ((cardstate == SD_CARD_PROGRAMMING) || (cardstate == SD_CARD_RECEIVING))) 01309 { 01310 errorstate = SD_IsCardProgramming(hsd, &cardstate); 01311 delay--; 01312 } 01313 01314 return errorstate; 01315 } 01316 01317 /** 01318 * @brief This function handles SD card interrupt request. 01319 * @param hsd: SD handle 01320 * @retval None 01321 */ 01322 void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd) 01323 { 01324 /* Check for SDMMC interrupt flags */ 01325 if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_IT_DATAEND)) 01326 { 01327 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_IT_DATAEND); 01328 01329 /* SD transfer is complete */ 01330 hsd->SdTransferCplt = 1; 01331 01332 /* No transfer error */ 01333 hsd->SdTransferErr = SD_OK; 01334 01335 HAL_SD_XferCpltCallback(hsd); 01336 } 01337 else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_IT_DCRCFAIL)) 01338 { 01339 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL); 01340 01341 hsd->SdTransferErr = SD_DATA_CRC_FAIL; 01342 01343 HAL_SD_XferErrorCallback(hsd); 01344 01345 } 01346 else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_IT_DTIMEOUT)) 01347 { 01348 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT); 01349 01350 hsd->SdTransferErr = SD_DATA_TIMEOUT; 01351 01352 HAL_SD_XferErrorCallback(hsd); 01353 } 01354 else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_IT_RXOVERR)) 01355 { 01356 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR); 01357 01358 hsd->SdTransferErr = SD_RX_OVERRUN; 01359 01360 HAL_SD_XferErrorCallback(hsd); 01361 } 01362 else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_IT_TXUNDERR)) 01363 { 01364 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_TXUNDERR); 01365 01366 hsd->SdTransferErr = SD_TX_UNDERRUN; 01367 01368 HAL_SD_XferErrorCallback(hsd); 01369 } 01370 else 01371 { 01372 /* No error flag set */ 01373 } 01374 01375 /* Disable all SDMMC peripheral interrupt sources */ 01376 __HAL_SD_SDMMC_DISABLE_IT(hsd, SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_DATAEND |\ 01377 SDMMC_IT_TXFIFOHE | SDMMC_IT_RXFIFOHF | SDMMC_IT_TXUNDERR |\ 01378 SDMMC_IT_RXOVERR); 01379 } 01380 01381 01382 /** 01383 * @brief SD end of transfer callback. 01384 * @param hsd: SD handle 01385 * @retval None 01386 */ 01387 __weak void HAL_SD_XferCpltCallback(SD_HandleTypeDef *hsd) 01388 { 01389 /* Prevent unused argument(s) compilation warning */ 01390 UNUSED(hsd); 01391 01392 /* NOTE : This function should not be modified, when the callback is needed, 01393 the HAL_SD_XferCpltCallback could be implemented in the user file 01394 */ 01395 } 01396 01397 /** 01398 * @brief SD Transfer Error callback. 01399 * @param hsd: SD handle 01400 * @retval None 01401 */ 01402 __weak void HAL_SD_XferErrorCallback(SD_HandleTypeDef *hsd) 01403 { 01404 /* Prevent unused argument(s) compilation warning */ 01405 UNUSED(hsd); 01406 01407 /* NOTE : This function should not be modified, when the callback is needed, 01408 the HAL_SD_XferErrorCallback could be implemented in the user file 01409 */ 01410 } 01411 01412 /** 01413 * @brief SD Transfer complete Rx callback in non-blocking mode. 01414 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains 01415 * the configuration information for the specified DMA module. 01416 * @retval None 01417 */ 01418 __weak void HAL_SD_DMA_RxCpltCallback(DMA_HandleTypeDef *hdma) 01419 { 01420 /* Prevent unused argument(s) compilation warning */ 01421 UNUSED(hdma); 01422 01423 /* NOTE : This function should not be modified, when the callback is needed, 01424 the HAL_SD_DMA_RxCpltCallback could be implemented in the user file 01425 */ 01426 } 01427 01428 /** 01429 * @brief SD DMA transfer complete Rx error callback. 01430 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains 01431 * the configuration information for the specified DMA module. 01432 * @retval None 01433 */ 01434 __weak void HAL_SD_DMA_RxErrorCallback(DMA_HandleTypeDef *hdma) 01435 { 01436 /* Prevent unused argument(s) compilation warning */ 01437 UNUSED(hdma); 01438 01439 /* NOTE : This function should not be modified, when the callback is needed, 01440 the HAL_SD_DMA_RxErrorCallback could be implemented in the user file 01441 */ 01442 } 01443 01444 /** 01445 * @brief SD Transfer complete Tx callback in non-blocking mode. 01446 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains 01447 * the configuration information for the specified DMA module. 01448 * @retval None 01449 */ 01450 __weak void HAL_SD_DMA_TxCpltCallback(DMA_HandleTypeDef *hdma) 01451 { 01452 /* Prevent unused argument(s) compilation warning */ 01453 UNUSED(hdma); 01454 01455 /* NOTE : This function should not be modified, when the callback is needed, 01456 the HAL_SD_DMA_TxCpltCallback could be implemented in the user file 01457 */ 01458 } 01459 01460 /** 01461 * @brief SD DMA transfer complete error Tx callback. 01462 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains 01463 * the configuration information for the specified DMA module. 01464 * @retval None 01465 */ 01466 __weak void HAL_SD_DMA_TxErrorCallback(DMA_HandleTypeDef *hdma) 01467 { 01468 /* Prevent unused argument(s) compilation warning */ 01469 UNUSED(hdma); 01470 01471 /* NOTE : This function should not be modified, when the callback is needed, 01472 the HAL_SD_DMA_TxErrorCallback could be implemented in the user file 01473 */ 01474 } 01475 01476 /** 01477 * @} 01478 */ 01479 01480 /** @addtogroup SD_Exported_Functions_Group3 01481 * @brief management functions 01482 * 01483 @verbatim 01484 ============================================================================== 01485 ##### Peripheral Control functions ##### 01486 ============================================================================== 01487 [..] 01488 This subsection provides a set of functions allowing to control the SD card 01489 operations. 01490 01491 @endverbatim 01492 * @{ 01493 */ 01494 01495 /** 01496 * @brief Returns information about specific card. 01497 * @param hsd: SD handle 01498 * @param pCardInfo: Pointer to a HAL_SD_CardInfoTypedef structure that 01499 * contains all SD cardinformation 01500 * @retval SD Card error state 01501 */ 01502 HAL_SD_ErrorTypedef HAL_SD_Get_CardInfo(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypedef *pCardInfo) 01503 { 01504 HAL_SD_ErrorTypedef errorstate = SD_OK; 01505 uint32_t tmp = 0; 01506 01507 pCardInfo->CardType = (uint8_t)(hsd->CardType); 01508 pCardInfo->RCA = (uint16_t)(hsd->RCA); 01509 01510 /* Byte 0 */ 01511 tmp = (hsd->CSD[0] & 0xFF000000) >> 24; 01512 pCardInfo->SD_csd.CSDStruct = (uint8_t)((tmp & 0xC0) >> 6); 01513 pCardInfo->SD_csd.SysSpecVersion = (uint8_t)((tmp & 0x3C) >> 2); 01514 pCardInfo->SD_csd.Reserved1 = tmp & 0x03; 01515 01516 /* Byte 1 */ 01517 tmp = (hsd->CSD[0] & 0x00FF0000) >> 16; 01518 pCardInfo->SD_csd.TAAC = (uint8_t)tmp; 01519 01520 /* Byte 2 */ 01521 tmp = (hsd->CSD[0] & 0x0000FF00) >> 8; 01522 pCardInfo->SD_csd.NSAC = (uint8_t)tmp; 01523 01524 /* Byte 3 */ 01525 tmp = hsd->CSD[0] & 0x000000FF; 01526 pCardInfo->SD_csd.MaxBusClkFrec = (uint8_t)tmp; 01527 01528 /* Byte 4 */ 01529 tmp = (hsd->CSD[1] & 0xFF000000) >> 24; 01530 pCardInfo->SD_csd.CardComdClasses = (uint16_t)(tmp << 4); 01531 01532 /* Byte 5 */ 01533 tmp = (hsd->CSD[1] & 0x00FF0000) >> 16; 01534 pCardInfo->SD_csd.CardComdClasses |= (uint16_t)((tmp & 0xF0) >> 4); 01535 pCardInfo->SD_csd.RdBlockLen = (uint8_t)(tmp & 0x0F); 01536 01537 /* Byte 6 */ 01538 tmp = (hsd->CSD[1] & 0x0000FF00) >> 8; 01539 pCardInfo->SD_csd.PartBlockRead = (uint8_t)((tmp & 0x80) >> 7); 01540 pCardInfo->SD_csd.WrBlockMisalign = (uint8_t)((tmp & 0x40) >> 6); 01541 pCardInfo->SD_csd.RdBlockMisalign = (uint8_t)((tmp & 0x20) >> 5); 01542 pCardInfo->SD_csd.DSRImpl = (uint8_t)((tmp & 0x10) >> 4); 01543 pCardInfo->SD_csd.Reserved2 = 0; /*!< Reserved */ 01544 01545 if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0)) 01546 { 01547 pCardInfo->SD_csd.DeviceSize = (tmp & 0x03) << 10; 01548 01549 /* Byte 7 */ 01550 tmp = (uint8_t)(hsd->CSD[1] & 0x000000FF); 01551 pCardInfo->SD_csd.DeviceSize |= (tmp) << 2; 01552 01553 /* Byte 8 */ 01554 tmp = (uint8_t)((hsd->CSD[2] & 0xFF000000) >> 24); 01555 pCardInfo->SD_csd.DeviceSize |= (tmp & 0xC0) >> 6; 01556 01557 pCardInfo->SD_csd.MaxRdCurrentVDDMin = (tmp & 0x38) >> 3; 01558 pCardInfo->SD_csd.MaxRdCurrentVDDMax = (tmp & 0x07); 01559 01560 /* Byte 9 */ 01561 tmp = (uint8_t)((hsd->CSD[2] & 0x00FF0000) >> 16); 01562 pCardInfo->SD_csd.MaxWrCurrentVDDMin = (tmp & 0xE0) >> 5; 01563 pCardInfo->SD_csd.MaxWrCurrentVDDMax = (tmp & 0x1C) >> 2; 01564 pCardInfo->SD_csd.DeviceSizeMul = (tmp & 0x03) << 1; 01565 /* Byte 10 */ 01566 tmp = (uint8_t)((hsd->CSD[2] & 0x0000FF00) >> 8); 01567 pCardInfo->SD_csd.DeviceSizeMul |= (tmp & 0x80) >> 7; 01568 01569 pCardInfo->CardCapacity = (pCardInfo->SD_csd.DeviceSize + 1) ; 01570 pCardInfo->CardCapacity *= (1 << (pCardInfo->SD_csd.DeviceSizeMul + 2)); 01571 pCardInfo->CardBlockSize = 1 << (pCardInfo->SD_csd.RdBlockLen); 01572 pCardInfo->CardCapacity *= pCardInfo->CardBlockSize; 01573 } 01574 else if (hsd->CardType == HIGH_CAPACITY_SD_CARD) 01575 { 01576 /* Byte 7 */ 01577 tmp = (uint8_t)(hsd->CSD[1] & 0x000000FF); 01578 pCardInfo->SD_csd.DeviceSize = (tmp & 0x3F) << 16; 01579 01580 /* Byte 8 */ 01581 tmp = (uint8_t)((hsd->CSD[2] & 0xFF000000) >> 24); 01582 01583 pCardInfo->SD_csd.DeviceSize |= (tmp << 8); 01584 01585 /* Byte 9 */ 01586 tmp = (uint8_t)((hsd->CSD[2] & 0x00FF0000) >> 16); 01587 01588 pCardInfo->SD_csd.DeviceSize |= (tmp); 01589 01590 /* Byte 10 */ 01591 tmp = (uint8_t)((hsd->CSD[2] & 0x0000FF00) >> 8); 01592 01593 pCardInfo->CardCapacity = (uint64_t)(((uint64_t)pCardInfo->SD_csd.DeviceSize + 1) * 512 * 1024); 01594 pCardInfo->CardBlockSize = 512; 01595 } 01596 else 01597 { 01598 /* Not supported card type */ 01599 errorstate = SD_ERROR; 01600 } 01601 01602 pCardInfo->SD_csd.EraseGrSize = (tmp & 0x40) >> 6; 01603 pCardInfo->SD_csd.EraseGrMul = (tmp & 0x3F) << 1; 01604 01605 /* Byte 11 */ 01606 tmp = (uint8_t)(hsd->CSD[2] & 0x000000FF); 01607 pCardInfo->SD_csd.EraseGrMul |= (tmp & 0x80) >> 7; 01608 pCardInfo->SD_csd.WrProtectGrSize = (tmp & 0x7F); 01609 01610 /* Byte 12 */ 01611 tmp = (uint8_t)((hsd->CSD[3] & 0xFF000000) >> 24); 01612 pCardInfo->SD_csd.WrProtectGrEnable = (tmp & 0x80) >> 7; 01613 pCardInfo->SD_csd.ManDeflECC = (tmp & 0x60) >> 5; 01614 pCardInfo->SD_csd.WrSpeedFact = (tmp & 0x1C) >> 2; 01615 pCardInfo->SD_csd.MaxWrBlockLen = (tmp & 0x03) << 2; 01616 01617 /* Byte 13 */ 01618 tmp = (uint8_t)((hsd->CSD[3] & 0x00FF0000) >> 16); 01619 pCardInfo->SD_csd.MaxWrBlockLen |= (tmp & 0xC0) >> 6; 01620 pCardInfo->SD_csd.WriteBlockPaPartial = (tmp & 0x20) >> 5; 01621 pCardInfo->SD_csd.Reserved3 = 0; 01622 pCardInfo->SD_csd.ContentProtectAppli = (tmp & 0x01); 01623 01624 /* Byte 14 */ 01625 tmp = (uint8_t)((hsd->CSD[3] & 0x0000FF00) >> 8); 01626 pCardInfo->SD_csd.FileFormatGrouop = (tmp & 0x80) >> 7; 01627 pCardInfo->SD_csd.CopyFlag = (tmp & 0x40) >> 6; 01628 pCardInfo->SD_csd.PermWrProtect = (tmp & 0x20) >> 5; 01629 pCardInfo->SD_csd.TempWrProtect = (tmp & 0x10) >> 4; 01630 pCardInfo->SD_csd.FileFormat = (tmp & 0x0C) >> 2; 01631 pCardInfo->SD_csd.ECC = (tmp & 0x03); 01632 01633 /* Byte 15 */ 01634 tmp = (uint8_t)(hsd->CSD[3] & 0x000000FF); 01635 pCardInfo->SD_csd.CSD_CRC = (tmp & 0xFE) >> 1; 01636 pCardInfo->SD_csd.Reserved4 = 1; 01637 01638 /* Byte 0 */ 01639 tmp = (uint8_t)((hsd->CID[0] & 0xFF000000) >> 24); 01640 pCardInfo->SD_cid.ManufacturerID = tmp; 01641 01642 /* Byte 1 */ 01643 tmp = (uint8_t)((hsd->CID[0] & 0x00FF0000) >> 16); 01644 pCardInfo->SD_cid.OEM_AppliID = tmp << 8; 01645 01646 /* Byte 2 */ 01647 tmp = (uint8_t)((hsd->CID[0] & 0x000000FF00) >> 8); 01648 pCardInfo->SD_cid.OEM_AppliID |= tmp; 01649 01650 /* Byte 3 */ 01651 tmp = (uint8_t)(hsd->CID[0] & 0x000000FF); 01652 pCardInfo->SD_cid.ProdName1 = tmp << 24; 01653 01654 /* Byte 4 */ 01655 tmp = (uint8_t)((hsd->CID[1] & 0xFF000000) >> 24); 01656 pCardInfo->SD_cid.ProdName1 |= tmp << 16; 01657 01658 /* Byte 5 */ 01659 tmp = (uint8_t)((hsd->CID[1] & 0x00FF0000) >> 16); 01660 pCardInfo->SD_cid.ProdName1 |= tmp << 8; 01661 01662 /* Byte 6 */ 01663 tmp = (uint8_t)((hsd->CID[1] & 0x0000FF00) >> 8); 01664 pCardInfo->SD_cid.ProdName1 |= tmp; 01665 01666 /* Byte 7 */ 01667 tmp = (uint8_t)(hsd->CID[1] & 0x000000FF); 01668 pCardInfo->SD_cid.ProdName2 = tmp; 01669 01670 /* Byte 8 */ 01671 tmp = (uint8_t)((hsd->CID[2] & 0xFF000000) >> 24); 01672 pCardInfo->SD_cid.ProdRev = tmp; 01673 01674 /* Byte 9 */ 01675 tmp = (uint8_t)((hsd->CID[2] & 0x00FF0000) >> 16); 01676 pCardInfo->SD_cid.ProdSN = tmp << 24; 01677 01678 /* Byte 10 */ 01679 tmp = (uint8_t)((hsd->CID[2] & 0x0000FF00) >> 8); 01680 pCardInfo->SD_cid.ProdSN |= tmp << 16; 01681 01682 /* Byte 11 */ 01683 tmp = (uint8_t)(hsd->CID[2] & 0x000000FF); 01684 pCardInfo->SD_cid.ProdSN |= tmp << 8; 01685 01686 /* Byte 12 */ 01687 tmp = (uint8_t)((hsd->CID[3] & 0xFF000000) >> 24); 01688 pCardInfo->SD_cid.ProdSN |= tmp; 01689 01690 /* Byte 13 */ 01691 tmp = (uint8_t)((hsd->CID[3] & 0x00FF0000) >> 16); 01692 pCardInfo->SD_cid.Reserved1 |= (tmp & 0xF0) >> 4; 01693 pCardInfo->SD_cid.ManufactDate = (tmp & 0x0F) << 8; 01694 01695 /* Byte 14 */ 01696 tmp = (uint8_t)((hsd->CID[3] & 0x0000FF00) >> 8); 01697 pCardInfo->SD_cid.ManufactDate |= tmp; 01698 01699 /* Byte 15 */ 01700 tmp = (uint8_t)(hsd->CID[3] & 0x000000FF); 01701 pCardInfo->SD_cid.CID_CRC = (tmp & 0xFE) >> 1; 01702 pCardInfo->SD_cid.Reserved2 = 1; 01703 01704 return errorstate; 01705 } 01706 01707 /** 01708 * @brief Enables wide bus operation for the requested card if supported by 01709 * card. 01710 * @param hsd: SD handle 01711 * @param WideMode: Specifies the SD card wide bus mode 01712 * This parameter can be one of the following values: 01713 * @arg SDMMC_BUS_WIDE_8B: 8-bit data transfer (Only for MMC) 01714 * @arg SDMMC_BUS_WIDE_4B: 4-bit data transfer 01715 * @arg SDMMC_BUS_WIDE_1B: 1-bit data transfer 01716 * @retval SD Card error state 01717 */ 01718 HAL_SD_ErrorTypedef HAL_SD_WideBusOperation_Config(SD_HandleTypeDef *hsd, uint32_t WideMode) 01719 { 01720 HAL_SD_ErrorTypedef errorstate = SD_OK; 01721 SDMMC_InitTypeDef tmpinit; 01722 01723 /* MMC Card does not support this feature */ 01724 if (hsd->CardType == MULTIMEDIA_CARD) 01725 { 01726 errorstate = SD_UNSUPPORTED_FEATURE; 01727 01728 return errorstate; 01729 } 01730 else if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\ 01731 (hsd->CardType == HIGH_CAPACITY_SD_CARD)) 01732 { 01733 if (WideMode == SDMMC_BUS_WIDE_8B) 01734 { 01735 errorstate = SD_UNSUPPORTED_FEATURE; 01736 } 01737 else if (WideMode == SDMMC_BUS_WIDE_4B) 01738 { 01739 errorstate = SD_WideBus_Enable(hsd); 01740 } 01741 else if (WideMode == SDMMC_BUS_WIDE_1B) 01742 { 01743 errorstate = SD_WideBus_Disable(hsd); 01744 } 01745 else 01746 { 01747 /* WideMode is not a valid argument*/ 01748 errorstate = SD_INVALID_PARAMETER; 01749 } 01750 01751 if (errorstate == SD_OK) 01752 { 01753 /* Configure the SDMMC peripheral */ 01754 tmpinit.ClockEdge = hsd->Init.ClockEdge; 01755 tmpinit.ClockBypass = hsd->Init.ClockBypass; 01756 tmpinit.ClockPowerSave = hsd->Init.ClockPowerSave; 01757 tmpinit.BusWide = WideMode; 01758 tmpinit.HardwareFlowControl = hsd->Init.HardwareFlowControl; 01759 tmpinit.ClockDiv = hsd->Init.ClockDiv; 01760 SDMMC_Init(hsd->Instance, tmpinit); 01761 } 01762 } 01763 01764 return errorstate; 01765 } 01766 01767 /** 01768 * @brief Aborts an ongoing data transfer. 01769 * @param hsd: SD handle 01770 * @retval SD Card error state 01771 */ 01772 HAL_SD_ErrorTypedef HAL_SD_StopTransfer(SD_HandleTypeDef *hsd) 01773 { 01774 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure; 01775 HAL_SD_ErrorTypedef errorstate = SD_OK; 01776 01777 /* Send CMD12 STOP_TRANSMISSION */ 01778 sdmmc_cmdinitstructure.Argument = 0; 01779 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_STOP_TRANSMISSION; 01780 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT; 01781 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO; 01782 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE; 01783 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 01784 01785 /* Check for error conditions */ 01786 errorstate = SD_CmdResp1Error(hsd, SD_CMD_STOP_TRANSMISSION); 01787 01788 return errorstate; 01789 } 01790 01791 /** 01792 * @brief Switches the SD card to High Speed mode. 01793 * This API must be used after "Transfer State" 01794 * @note This operation should be followed by the configuration 01795 * of PLL to have SDMMCCK clock between 67 and 75 MHz 01796 * @param hsd: SD handle 01797 * @retval SD Card error state 01798 */ 01799 HAL_SD_ErrorTypedef HAL_SD_HighSpeed (SD_HandleTypeDef *hsd) 01800 { 01801 HAL_SD_ErrorTypedef errorstate = SD_OK; 01802 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure; 01803 SDMMC_DataInitTypeDef sdmmc_datainitstructure; 01804 01805 uint8_t SD_hs[64] = {0}; 01806 uint32_t SD_scr[2] = {0, 0}; 01807 uint32_t SD_SPEC = 0 ; 01808 uint32_t count = 0, *tempbuff = (uint32_t *)SD_hs; 01809 01810 /* Initialize the Data control register */ 01811 hsd->Instance->DCTRL = 0; 01812 01813 /* Get SCR Register */ 01814 errorstate = SD_FindSCR(hsd, SD_scr); 01815 01816 if (errorstate != SD_OK) 01817 { 01818 return errorstate; 01819 } 01820 01821 /* Test the Version supported by the card*/ 01822 SD_SPEC = (SD_scr[1] & 0x01000000) | (SD_scr[1] & 0x02000000); 01823 01824 if (SD_SPEC != SD_ALLZERO) 01825 { 01826 /* Set Block Size for Card */ 01827 sdmmc_cmdinitstructure.Argument = (uint32_t)64; 01828 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN; 01829 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT; 01830 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO; 01831 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE; 01832 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 01833 01834 /* Check for error conditions */ 01835 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN); 01836 01837 if (errorstate != SD_OK) 01838 { 01839 return errorstate; 01840 } 01841 01842 /* Configure the SD DPSM (Data Path State Machine) */ 01843 sdmmc_datainitstructure.DataTimeOut = SD_DATATIMEOUT; 01844 sdmmc_datainitstructure.DataLength = 64; 01845 sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B ; 01846 sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC; 01847 sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK; 01848 sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE; 01849 SDMMC_DataConfig(hsd->Instance, &sdmmc_datainitstructure); 01850 01851 /* Send CMD6 switch mode */ 01852 sdmmc_cmdinitstructure.Argument = 0x80FFFF01; 01853 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_HS_SWITCH; 01854 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 01855 01856 /* Check for error conditions */ 01857 errorstate = SD_CmdResp1Error(hsd, SD_CMD_HS_SWITCH); 01858 01859 if (errorstate != SD_OK) 01860 { 01861 return errorstate; 01862 } 01863 01864 while(!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND)) 01865 { 01866 if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF)) 01867 { 01868 for (count = 0; count < 8; count++) 01869 { 01870 *(tempbuff + count) = SDMMC_ReadFIFO(hsd->Instance); 01871 } 01872 01873 tempbuff += 8; 01874 } 01875 } 01876 01877 if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT)) 01878 { 01879 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT); 01880 01881 errorstate = SD_DATA_TIMEOUT; 01882 01883 return errorstate; 01884 } 01885 else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL)) 01886 { 01887 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL); 01888 01889 errorstate = SD_DATA_CRC_FAIL; 01890 01891 return errorstate; 01892 } 01893 else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR)) 01894 { 01895 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR); 01896 01897 errorstate = SD_RX_OVERRUN; 01898 01899 return errorstate; 01900 } 01901 else 01902 { 01903 /* No error flag set */ 01904 } 01905 01906 count = SD_DATATIMEOUT; 01907 01908 while ((__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL)) && (count > 0)) 01909 { 01910 *tempbuff = SDMMC_ReadFIFO(hsd->Instance); 01911 tempbuff++; 01912 count--; 01913 } 01914 01915 /* Clear all the static flags */ 01916 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 01917 01918 /* Test if the switch mode HS is ok */ 01919 if ((SD_hs[13]& 2) != 2) 01920 { 01921 errorstate = SD_UNSUPPORTED_FEATURE; 01922 } 01923 } 01924 01925 return errorstate; 01926 } 01927 01928 /** 01929 * @} 01930 */ 01931 01932 /** @addtogroup SD_Exported_Functions_Group4 01933 * @brief Peripheral State functions 01934 * 01935 @verbatim 01936 ============================================================================== 01937 ##### Peripheral State functions ##### 01938 ============================================================================== 01939 [..] 01940 This subsection permits to get in runtime the status of the peripheral 01941 and the data flow. 01942 01943 @endverbatim 01944 * @{ 01945 */ 01946 01947 /** 01948 * @brief Returns the current SD card's status. 01949 * @param hsd: SD handle 01950 * @param pSDstatus: Pointer to the buffer that will contain the SD card status 01951 * SD Status register) 01952 * @retval SD Card error state 01953 */ 01954 HAL_SD_ErrorTypedef HAL_SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus) 01955 { 01956 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure; 01957 SDMMC_DataInitTypeDef sdmmc_datainitstructure; 01958 HAL_SD_ErrorTypedef errorstate = SD_OK; 01959 uint32_t count = 0; 01960 01961 /* Check SD response */ 01962 if ((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SD_CARD_LOCKED) == SD_CARD_LOCKED) 01963 { 01964 errorstate = SD_LOCK_UNLOCK_FAILED; 01965 01966 return errorstate; 01967 } 01968 01969 /* Set block size for card if it is not equal to current block size for card */ 01970 sdmmc_cmdinitstructure.Argument = 64; 01971 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN; 01972 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT; 01973 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO; 01974 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE; 01975 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 01976 01977 /* Check for error conditions */ 01978 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN); 01979 01980 if (errorstate != SD_OK) 01981 { 01982 return errorstate; 01983 } 01984 01985 /* Send CMD55 */ 01986 sdmmc_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16); 01987 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD; 01988 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 01989 01990 /* Check for error conditions */ 01991 errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD); 01992 01993 if (errorstate != SD_OK) 01994 { 01995 return errorstate; 01996 } 01997 01998 /* Configure the SD DPSM (Data Path State Machine) */ 01999 sdmmc_datainitstructure.DataTimeOut = SD_DATATIMEOUT; 02000 sdmmc_datainitstructure.DataLength = 64; 02001 sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B; 02002 sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC; 02003 sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK; 02004 sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE; 02005 SDMMC_DataConfig(hsd->Instance, &sdmmc_datainitstructure); 02006 02007 /* Send ACMD13 (SD_APP_STAUS) with argument as card's RCA */ 02008 sdmmc_cmdinitstructure.Argument = 0; 02009 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SD_APP_STATUS; 02010 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 02011 02012 /* Check for error conditions */ 02013 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SD_APP_STATUS); 02014 02015 if (errorstate != SD_OK) 02016 { 02017 return errorstate; 02018 } 02019 02020 /* Get status data */ 02021 while(!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND)) 02022 { 02023 if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF)) 02024 { 02025 for (count = 0; count < 8; count++) 02026 { 02027 *(pSDstatus + count) = SDMMC_ReadFIFO(hsd->Instance); 02028 } 02029 02030 pSDstatus += 8; 02031 } 02032 } 02033 02034 if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT)) 02035 { 02036 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT); 02037 02038 errorstate = SD_DATA_TIMEOUT; 02039 02040 return errorstate; 02041 } 02042 else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL)) 02043 { 02044 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL); 02045 02046 errorstate = SD_DATA_CRC_FAIL; 02047 02048 return errorstate; 02049 } 02050 else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR)) 02051 { 02052 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR); 02053 02054 errorstate = SD_RX_OVERRUN; 02055 02056 return errorstate; 02057 } 02058 else 02059 { 02060 /* No error flag set */ 02061 } 02062 02063 count = SD_DATATIMEOUT; 02064 while ((__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL)) && (count > 0)) 02065 { 02066 *pSDstatus = SDMMC_ReadFIFO(hsd->Instance); 02067 pSDstatus++; 02068 count--; 02069 } 02070 02071 /* Clear all the static status flags*/ 02072 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 02073 02074 return errorstate; 02075 } 02076 02077 /** 02078 * @brief Gets the current sd card data status. 02079 * @param hsd: SD handle 02080 * @retval Data Transfer state 02081 */ 02082 HAL_SD_TransferStateTypedef HAL_SD_GetStatus(SD_HandleTypeDef *hsd) 02083 { 02084 HAL_SD_CardStateTypedef cardstate = SD_CARD_TRANSFER; 02085 02086 /* Get SD card state */ 02087 cardstate = SD_GetState(hsd); 02088 02089 /* Find SD status according to card state*/ 02090 if (cardstate == SD_CARD_TRANSFER) 02091 { 02092 return SD_TRANSFER_OK; 02093 } 02094 else if(cardstate == SD_CARD_ERROR) 02095 { 02096 return SD_TRANSFER_ERROR; 02097 } 02098 else 02099 { 02100 return SD_TRANSFER_BUSY; 02101 } 02102 } 02103 02104 /** 02105 * @brief Gets the SD card status. 02106 * @param hsd: SD handle 02107 * @param pCardStatus: Pointer to the HAL_SD_CardStatusTypedef structure that 02108 * will contain the SD card status information 02109 * @retval SD Card error state 02110 */ 02111 HAL_SD_ErrorTypedef HAL_SD_GetCardStatus(SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypedef *pCardStatus) 02112 { 02113 HAL_SD_ErrorTypedef errorstate = SD_OK; 02114 uint32_t tmp = 0; 02115 uint32_t sd_status[16]; 02116 02117 errorstate = HAL_SD_SendSDStatus(hsd, sd_status); 02118 02119 if (errorstate != SD_OK) 02120 { 02121 return errorstate; 02122 } 02123 02124 /* Byte 0 */ 02125 tmp = (sd_status[0] & 0xC0) >> 6; 02126 pCardStatus->DAT_BUS_WIDTH = (uint8_t)tmp; 02127 02128 /* Byte 0 */ 02129 tmp = (sd_status[0] & 0x20) >> 5; 02130 pCardStatus->SECURED_MODE = (uint8_t)tmp; 02131 02132 /* Byte 2 */ 02133 tmp = (sd_status[2] & 0xFF); 02134 pCardStatus->SD_CARD_TYPE = (uint8_t)(tmp << 8); 02135 02136 /* Byte 3 */ 02137 tmp = (sd_status[3] & 0xFF); 02138 pCardStatus->SD_CARD_TYPE |= (uint8_t)tmp; 02139 02140 /* Byte 4 */ 02141 tmp = (sd_status[4] & 0xFF); 02142 pCardStatus->SIZE_OF_PROTECTED_AREA = (uint8_t)(tmp << 24); 02143 02144 /* Byte 5 */ 02145 tmp = (sd_status[5] & 0xFF); 02146 pCardStatus->SIZE_OF_PROTECTED_AREA |= (uint8_t)(tmp << 16); 02147 02148 /* Byte 6 */ 02149 tmp = (sd_status[6] & 0xFF); 02150 pCardStatus->SIZE_OF_PROTECTED_AREA |= (uint8_t)(tmp << 8); 02151 02152 /* Byte 7 */ 02153 tmp = (sd_status[7] & 0xFF); 02154 pCardStatus->SIZE_OF_PROTECTED_AREA |= (uint8_t)tmp; 02155 02156 /* Byte 8 */ 02157 tmp = (sd_status[8] & 0xFF); 02158 pCardStatus->SPEED_CLASS = (uint8_t)tmp; 02159 02160 /* Byte 9 */ 02161 tmp = (sd_status[9] & 0xFF); 02162 pCardStatus->PERFORMANCE_MOVE = (uint8_t)tmp; 02163 02164 /* Byte 10 */ 02165 tmp = (sd_status[10] & 0xF0) >> 4; 02166 pCardStatus->AU_SIZE = (uint8_t)tmp; 02167 02168 /* Byte 11 */ 02169 tmp = (sd_status[11] & 0xFF); 02170 pCardStatus->ERASE_SIZE = (uint8_t)(tmp << 8); 02171 02172 /* Byte 12 */ 02173 tmp = (sd_status[12] & 0xFF); 02174 pCardStatus->ERASE_SIZE |= (uint8_t)tmp; 02175 02176 /* Byte 13 */ 02177 tmp = (sd_status[13] & 0xFC) >> 2; 02178 pCardStatus->ERASE_TIMEOUT = (uint8_t)tmp; 02179 02180 /* Byte 13 */ 02181 tmp = (sd_status[13] & 0x3); 02182 pCardStatus->ERASE_OFFSET = (uint8_t)tmp; 02183 02184 return errorstate; 02185 } 02186 02187 /** 02188 * @} 02189 */ 02190 02191 /** 02192 * @} 02193 */ 02194 02195 /* Private function ----------------------------------------------------------*/ 02196 /** @addtogroup SD_Private_Functions 02197 * @{ 02198 */ 02199 02200 /** 02201 * @brief SD DMA transfer complete Rx callback. 02202 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains 02203 * the configuration information for the specified DMA module. 02204 * @retval None 02205 */ 02206 static void SD_DMA_RxCplt(DMA_HandleTypeDef *hdma) 02207 { 02208 SD_HandleTypeDef *hsd = (SD_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; 02209 02210 /* DMA transfer is complete */ 02211 hsd->DmaTransferCplt = 1; 02212 02213 /* Wait until SD transfer is complete */ 02214 while(hsd->SdTransferCplt == 0) 02215 { 02216 } 02217 02218 /* Disable the DMA channel */ 02219 HAL_DMA_Abort(hdma); 02220 02221 /* Transfer complete user callback */ 02222 HAL_SD_DMA_RxCpltCallback(hsd->hdmarx); 02223 } 02224 02225 /** 02226 * @brief SD DMA transfer Error Rx callback. 02227 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains 02228 * the configuration information for the specified DMA module. 02229 * @retval None 02230 */ 02231 static void SD_DMA_RxError(DMA_HandleTypeDef *hdma) 02232 { 02233 SD_HandleTypeDef *hsd = (SD_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; 02234 02235 /* Transfer complete user callback */ 02236 HAL_SD_DMA_RxErrorCallback(hsd->hdmarx); 02237 } 02238 02239 /** 02240 * @brief SD DMA transfer complete Tx callback. 02241 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains 02242 * the configuration information for the specified DMA module. 02243 * @retval None 02244 */ 02245 static void SD_DMA_TxCplt(DMA_HandleTypeDef *hdma) 02246 { 02247 SD_HandleTypeDef *hsd = (SD_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; 02248 02249 /* DMA transfer is complete */ 02250 hsd->DmaTransferCplt = 1; 02251 02252 /* Wait until SD transfer is complete */ 02253 while(hsd->SdTransferCplt == 0) 02254 { 02255 } 02256 02257 /* Disable the DMA channel */ 02258 HAL_DMA_Abort(hdma); 02259 02260 /* Transfer complete user callback */ 02261 HAL_SD_DMA_TxCpltCallback(hsd->hdmatx); 02262 } 02263 02264 /** 02265 * @brief SD DMA transfer Error Tx callback. 02266 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains 02267 * the configuration information for the specified DMA module. 02268 * @retval None 02269 */ 02270 static void SD_DMA_TxError(DMA_HandleTypeDef *hdma) 02271 { 02272 SD_HandleTypeDef *hsd = ( SD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; 02273 02274 /* Transfer complete user callback */ 02275 HAL_SD_DMA_TxErrorCallback(hsd->hdmatx); 02276 } 02277 02278 /** 02279 * @brief Returns the SD current state. 02280 * @param hsd: SD handle 02281 * @retval SD card current state 02282 */ 02283 static HAL_SD_CardStateTypedef SD_GetState(SD_HandleTypeDef *hsd) 02284 { 02285 uint32_t resp1 = 0; 02286 02287 if (SD_SendStatus(hsd, &resp1) != SD_OK) 02288 { 02289 return SD_CARD_ERROR; 02290 } 02291 else 02292 { 02293 return (HAL_SD_CardStateTypedef)((resp1 >> 9) & 0x0F); 02294 } 02295 } 02296 02297 /** 02298 * @brief Initializes all cards or single card as the case may be Card(s) come 02299 * into standby state. 02300 * @param hsd: SD handle 02301 * @retval SD Card error state 02302 */ 02303 static HAL_SD_ErrorTypedef SD_Initialize_Cards(SD_HandleTypeDef *hsd) 02304 { 02305 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure; 02306 HAL_SD_ErrorTypedef errorstate = SD_OK; 02307 uint16_t sd_rca = 1; 02308 02309 if(SDMMC_GetPowerState(hsd->Instance) == 0) /* Power off */ 02310 { 02311 errorstate = SD_REQUEST_NOT_APPLICABLE; 02312 02313 return errorstate; 02314 } 02315 02316 if(hsd->CardType != SECURE_DIGITAL_IO_CARD) 02317 { 02318 /* Send CMD2 ALL_SEND_CID */ 02319 sdmmc_cmdinitstructure.Argument = 0; 02320 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_ALL_SEND_CID; 02321 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_LONG; 02322 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO; 02323 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE; 02324 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 02325 02326 /* Check for error conditions */ 02327 errorstate = SD_CmdResp2Error(hsd); 02328 02329 if(errorstate != SD_OK) 02330 { 02331 return errorstate; 02332 } 02333 02334 /* Get Card identification number data */ 02335 hsd->CID[0] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1); 02336 hsd->CID[1] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2); 02337 hsd->CID[2] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP3); 02338 hsd->CID[3] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP4); 02339 } 02340 02341 if((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\ 02342 (hsd->CardType == SECURE_DIGITAL_IO_COMBO_CARD) || (hsd->CardType == HIGH_CAPACITY_SD_CARD)) 02343 { 02344 /* Send CMD3 SET_REL_ADDR with argument 0 */ 02345 /* SD Card publishes its RCA. */ 02346 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SET_REL_ADDR; 02347 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT; 02348 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 02349 02350 /* Check for error conditions */ 02351 errorstate = SD_CmdResp6Error(hsd, SD_CMD_SET_REL_ADDR, &sd_rca); 02352 02353 if(errorstate != SD_OK) 02354 { 02355 return errorstate; 02356 } 02357 } 02358 02359 if (hsd->CardType != SECURE_DIGITAL_IO_CARD) 02360 { 02361 /* Get the SD card RCA */ 02362 hsd->RCA = sd_rca; 02363 02364 /* Send CMD9 SEND_CSD with argument as card's RCA */ 02365 sdmmc_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16); 02366 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SEND_CSD; 02367 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_LONG; 02368 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 02369 02370 /* Check for error conditions */ 02371 errorstate = SD_CmdResp2Error(hsd); 02372 02373 if(errorstate != SD_OK) 02374 { 02375 return errorstate; 02376 } 02377 02378 /* Get Card Specific Data */ 02379 hsd->CSD[0] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1); 02380 hsd->CSD[1] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2); 02381 hsd->CSD[2] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP3); 02382 hsd->CSD[3] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP4); 02383 } 02384 02385 /* All cards are initialized */ 02386 return errorstate; 02387 } 02388 02389 /** 02390 * @brief Selects or Deselects the corresponding card. 02391 * @param hsd: SD handle 02392 * @param addr: Address of the card to be selected 02393 * @retval SD Card error state 02394 */ 02395 static HAL_SD_ErrorTypedef SD_Select_Deselect(SD_HandleTypeDef *hsd, uint64_t addr) 02396 { 02397 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure; 02398 HAL_SD_ErrorTypedef errorstate = SD_OK; 02399 02400 /* Send CMD7 SDMMC_SEL_DESEL_CARD */ 02401 sdmmc_cmdinitstructure.Argument = (uint32_t)addr; 02402 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SEL_DESEL_CARD; 02403 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT; 02404 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO; 02405 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE; 02406 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 02407 02408 /* Check for error conditions */ 02409 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SEL_DESEL_CARD); 02410 02411 return errorstate; 02412 } 02413 02414 /** 02415 * @brief Enquires cards about their operating voltage and configures clock 02416 * controls and stores SD information that will be needed in future 02417 * in the SD handle. 02418 * @param hsd: SD handle 02419 * @retval SD Card error state 02420 */ 02421 static HAL_SD_ErrorTypedef SD_PowerON(SD_HandleTypeDef *hsd) 02422 { 02423 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure; 02424 __IO HAL_SD_ErrorTypedef errorstate = SD_OK; 02425 uint32_t response = 0, count = 0, validvoltage = 0; 02426 uint32_t sdtype = SD_STD_CAPACITY; 02427 02428 /* Power ON Sequence -------------------------------------------------------*/ 02429 /* Disable SDMMC Clock */ 02430 __HAL_SD_SDMMC_DISABLE(hsd); 02431 02432 /* Set Power State to ON */ 02433 SDMMC_PowerState_ON(hsd->Instance); 02434 02435 /* 1ms: required power up waiting time before starting the SD initialization 02436 sequence */ 02437 HAL_Delay(1); 02438 02439 /* Enable SDMMC Clock */ 02440 __HAL_SD_SDMMC_ENABLE(hsd); 02441 02442 /* CMD0: GO_IDLE_STATE -----------------------------------------------------*/ 02443 /* No CMD response required */ 02444 sdmmc_cmdinitstructure.Argument = 0; 02445 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_GO_IDLE_STATE; 02446 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_NO; 02447 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO; 02448 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE; 02449 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 02450 02451 /* Check for error conditions */ 02452 errorstate = SD_CmdError(hsd); 02453 02454 if(errorstate != SD_OK) 02455 { 02456 /* CMD Response Timeout (wait for CMDSENT flag) */ 02457 return errorstate; 02458 } 02459 02460 /* CMD8: SEND_IF_COND ------------------------------------------------------*/ 02461 /* Send CMD8 to verify SD card interface operating condition */ 02462 /* Argument: - [31:12]: Reserved (shall be set to '0') 02463 - [11:8]: Supply Voltage (VHS) 0x1 (Range: 2.7-3.6 V) 02464 - [7:0]: Check Pattern (recommended 0xAA) */ 02465 /* CMD Response: R7 */ 02466 sdmmc_cmdinitstructure.Argument = SD_CHECK_PATTERN; 02467 sdmmc_cmdinitstructure.CmdIndex = SD_SDMMC_SEND_IF_COND; 02468 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT; 02469 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 02470 02471 /* Check for error conditions */ 02472 errorstate = SD_CmdResp7Error(hsd); 02473 02474 if (errorstate == SD_OK) 02475 { 02476 /* SD Card 2.0 */ 02477 hsd->CardType = STD_CAPACITY_SD_CARD_V2_0; 02478 sdtype = SD_HIGH_CAPACITY; 02479 } 02480 02481 /* Send CMD55 */ 02482 sdmmc_cmdinitstructure.Argument = 0; 02483 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD; 02484 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 02485 02486 /* Check for error conditions */ 02487 errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD); 02488 02489 /* If errorstate is Command Timeout, it is a MMC card */ 02490 /* If errorstate is SD_OK it is a SD card: SD card 2.0 (voltage range mismatch) 02491 or SD card 1.x */ 02492 if(errorstate == SD_OK) 02493 { 02494 /* SD CARD */ 02495 /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */ 02496 while((!validvoltage) && (count < SD_MAX_VOLT_TRIAL)) 02497 { 02498 02499 /* SEND CMD55 APP_CMD with RCA as 0 */ 02500 sdmmc_cmdinitstructure.Argument = 0; 02501 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD; 02502 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT; 02503 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO; 02504 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE; 02505 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 02506 02507 /* Check for error conditions */ 02508 errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD); 02509 02510 if(errorstate != SD_OK) 02511 { 02512 return errorstate; 02513 } 02514 02515 /* Send CMD41 */ 02516 sdmmc_cmdinitstructure.Argument = SD_VOLTAGE_WINDOW_SD | sdtype; 02517 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SD_APP_OP_COND; 02518 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT; 02519 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO; 02520 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE; 02521 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 02522 02523 /* Check for error conditions */ 02524 errorstate = SD_CmdResp3Error(hsd); 02525 02526 if(errorstate != SD_OK) 02527 { 02528 return errorstate; 02529 } 02530 02531 /* Get command response */ 02532 response = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1); 02533 02534 /* Get operating voltage*/ 02535 validvoltage = (((response >> 31) == 1) ? 1 : 0); 02536 02537 count++; 02538 } 02539 02540 if(count >= SD_MAX_VOLT_TRIAL) 02541 { 02542 errorstate = SD_INVALID_VOLTRANGE; 02543 02544 return errorstate; 02545 } 02546 02547 if((response & SD_HIGH_CAPACITY) == SD_HIGH_CAPACITY) /* (response &= SD_HIGH_CAPACITY) */ 02548 { 02549 hsd->CardType = HIGH_CAPACITY_SD_CARD; 02550 } 02551 02552 } /* else MMC Card */ 02553 02554 return errorstate; 02555 } 02556 02557 /** 02558 * @brief Turns the SDMMC output signals off. 02559 * @param hsd: SD handle 02560 * @retval SD Card error state 02561 */ 02562 static HAL_SD_ErrorTypedef SD_PowerOFF(SD_HandleTypeDef *hsd) 02563 { 02564 HAL_SD_ErrorTypedef errorstate = SD_OK; 02565 02566 /* Set Power State to OFF */ 02567 SDMMC_PowerState_OFF(hsd->Instance); 02568 02569 return errorstate; 02570 } 02571 02572 /** 02573 * @brief Returns the current card's status. 02574 * @param hsd: SD handle 02575 * @param pCardStatus: pointer to the buffer that will contain the SD card 02576 * status (Card Status register) 02577 * @retval SD Card error state 02578 */ 02579 static HAL_SD_ErrorTypedef SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus) 02580 { 02581 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure; 02582 HAL_SD_ErrorTypedef errorstate = SD_OK; 02583 02584 if(pCardStatus == NULL) 02585 { 02586 errorstate = SD_INVALID_PARAMETER; 02587 02588 return errorstate; 02589 } 02590 02591 /* Send Status command */ 02592 sdmmc_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16); 02593 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SEND_STATUS; 02594 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT; 02595 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO; 02596 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE; 02597 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 02598 02599 /* Check for error conditions */ 02600 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SEND_STATUS); 02601 02602 if(errorstate != SD_OK) 02603 { 02604 return errorstate; 02605 } 02606 02607 /* Get SD card status */ 02608 *pCardStatus = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1); 02609 02610 return errorstate; 02611 } 02612 02613 /** 02614 * @brief Checks for error conditions for CMD0. 02615 * @param hsd: SD handle 02616 * @retval SD Card error state 02617 */ 02618 static HAL_SD_ErrorTypedef SD_CmdError(SD_HandleTypeDef *hsd) 02619 { 02620 HAL_SD_ErrorTypedef errorstate = SD_OK; 02621 uint32_t timeout, tmp; 02622 02623 timeout = SDMMC_CMD0TIMEOUT; 02624 02625 tmp = __HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CMDSENT); 02626 02627 while((timeout > 0) && (!tmp)) 02628 { 02629 tmp = __HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CMDSENT); 02630 timeout--; 02631 } 02632 02633 if(timeout == 0) 02634 { 02635 errorstate = SD_CMD_RSP_TIMEOUT; 02636 return errorstate; 02637 } 02638 02639 /* Clear all the static flags */ 02640 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 02641 02642 return errorstate; 02643 } 02644 02645 /** 02646 * @brief Checks for error conditions for R7 response. 02647 * @param hsd: SD handle 02648 * @retval SD Card error state 02649 */ 02650 static HAL_SD_ErrorTypedef SD_CmdResp7Error(SD_HandleTypeDef *hsd) 02651 { 02652 HAL_SD_ErrorTypedef errorstate = SD_ERROR; 02653 uint32_t timeout = SDMMC_CMD0TIMEOUT, tmp; 02654 02655 tmp = __HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT); 02656 02657 while((!tmp) && (timeout > 0)) 02658 { 02659 tmp = __HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT); 02660 timeout--; 02661 } 02662 02663 tmp = __HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CTIMEOUT); 02664 02665 if((timeout == 0) || tmp) 02666 { 02667 /* Card is not V2.0 compliant or card does not support the set voltage range */ 02668 errorstate = SD_CMD_RSP_TIMEOUT; 02669 02670 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_CTIMEOUT); 02671 02672 return errorstate; 02673 } 02674 02675 if(__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CMDREND)) 02676 { 02677 /* Card is SD V2.0 compliant */ 02678 errorstate = SD_OK; 02679 02680 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_CMDREND); 02681 02682 return errorstate; 02683 } 02684 02685 return errorstate; 02686 } 02687 02688 /** 02689 * @brief Checks for error conditions for R1 response. 02690 * @param hsd: SD handle 02691 * @param SD_CMD: The sent command index 02692 * @retval SD Card error state 02693 */ 02694 static HAL_SD_ErrorTypedef SD_CmdResp1Error(SD_HandleTypeDef *hsd, uint8_t SD_CMD) 02695 { 02696 HAL_SD_ErrorTypedef errorstate = SD_OK; 02697 uint32_t response_r1; 02698 02699 while(!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT)) 02700 { 02701 } 02702 02703 if(__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CTIMEOUT)) 02704 { 02705 errorstate = SD_CMD_RSP_TIMEOUT; 02706 02707 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_CTIMEOUT); 02708 02709 return errorstate; 02710 } 02711 else if(__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CCRCFAIL)) 02712 { 02713 errorstate = SD_CMD_CRC_FAIL; 02714 02715 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_CCRCFAIL); 02716 02717 return errorstate; 02718 } 02719 02720 /* Check response received is of desired command */ 02721 if(SDMMC_GetCommandResponse(hsd->Instance) != SD_CMD) 02722 { 02723 errorstate = SD_ILLEGAL_CMD; 02724 02725 return errorstate; 02726 } 02727 02728 /* Clear all the static flags */ 02729 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 02730 02731 /* We have received response, retrieve it for analysis */ 02732 response_r1 = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1); 02733 02734 if((response_r1 & SD_OCR_ERRORBITS) == SD_ALLZERO) 02735 { 02736 return errorstate; 02737 } 02738 02739 if((response_r1 & SD_OCR_ADDR_OUT_OF_RANGE) == SD_OCR_ADDR_OUT_OF_RANGE) 02740 { 02741 return(SD_ADDR_OUT_OF_RANGE); 02742 } 02743 02744 if((response_r1 & SD_OCR_ADDR_MISALIGNED) == SD_OCR_ADDR_MISALIGNED) 02745 { 02746 return(SD_ADDR_MISALIGNED); 02747 } 02748 02749 if((response_r1 & SD_OCR_BLOCK_LEN_ERR) == SD_OCR_BLOCK_LEN_ERR) 02750 { 02751 return(SD_BLOCK_LEN_ERR); 02752 } 02753 02754 if((response_r1 & SD_OCR_ERASE_SEQ_ERR) == SD_OCR_ERASE_SEQ_ERR) 02755 { 02756 return(SD_ERASE_SEQ_ERR); 02757 } 02758 02759 if((response_r1 & SD_OCR_BAD_ERASE_PARAM) == SD_OCR_BAD_ERASE_PARAM) 02760 { 02761 return(SD_BAD_ERASE_PARAM); 02762 } 02763 02764 if((response_r1 & SD_OCR_WRITE_PROT_VIOLATION) == SD_OCR_WRITE_PROT_VIOLATION) 02765 { 02766 return(SD_WRITE_PROT_VIOLATION); 02767 } 02768 02769 if((response_r1 & SD_OCR_LOCK_UNLOCK_FAILED) == SD_OCR_LOCK_UNLOCK_FAILED) 02770 { 02771 return(SD_LOCK_UNLOCK_FAILED); 02772 } 02773 02774 if((response_r1 & SD_OCR_COM_CRC_FAILED) == SD_OCR_COM_CRC_FAILED) 02775 { 02776 return(SD_COM_CRC_FAILED); 02777 } 02778 02779 if((response_r1 & SD_OCR_ILLEGAL_CMD) == SD_OCR_ILLEGAL_CMD) 02780 { 02781 return(SD_ILLEGAL_CMD); 02782 } 02783 02784 if((response_r1 & SD_OCR_CARD_ECC_FAILED) == SD_OCR_CARD_ECC_FAILED) 02785 { 02786 return(SD_CARD_ECC_FAILED); 02787 } 02788 02789 if((response_r1 & SD_OCR_CC_ERROR) == SD_OCR_CC_ERROR) 02790 { 02791 return(SD_CC_ERROR); 02792 } 02793 02794 if((response_r1 & SD_OCR_GENERAL_UNKNOWN_ERROR) == SD_OCR_GENERAL_UNKNOWN_ERROR) 02795 { 02796 return(SD_GENERAL_UNKNOWN_ERROR); 02797 } 02798 02799 if((response_r1 & SD_OCR_STREAM_READ_UNDERRUN) == SD_OCR_STREAM_READ_UNDERRUN) 02800 { 02801 return(SD_STREAM_READ_UNDERRUN); 02802 } 02803 02804 if((response_r1 & SD_OCR_STREAM_WRITE_OVERRUN) == SD_OCR_STREAM_WRITE_OVERRUN) 02805 { 02806 return(SD_STREAM_WRITE_OVERRUN); 02807 } 02808 02809 if((response_r1 & SD_OCR_CID_CSD_OVERWRITE) == SD_OCR_CID_CSD_OVERWRITE) 02810 { 02811 return(SD_CID_CSD_OVERWRITE); 02812 } 02813 02814 if((response_r1 & SD_OCR_WP_ERASE_SKIP) == SD_OCR_WP_ERASE_SKIP) 02815 { 02816 return(SD_WP_ERASE_SKIP); 02817 } 02818 02819 if((response_r1 & SD_OCR_CARD_ECC_DISABLED) == SD_OCR_CARD_ECC_DISABLED) 02820 { 02821 return(SD_CARD_ECC_DISABLED); 02822 } 02823 02824 if((response_r1 & SD_OCR_ERASE_RESET) == SD_OCR_ERASE_RESET) 02825 { 02826 return(SD_ERASE_RESET); 02827 } 02828 02829 if((response_r1 & SD_OCR_AKE_SEQ_ERROR) == SD_OCR_AKE_SEQ_ERROR) 02830 { 02831 return(SD_AKE_SEQ_ERROR); 02832 } 02833 02834 return errorstate; 02835 } 02836 02837 /** 02838 * @brief Checks for error conditions for R3 (OCR) response. 02839 * @param hsd: SD handle 02840 * @retval SD Card error state 02841 */ 02842 static HAL_SD_ErrorTypedef SD_CmdResp3Error(SD_HandleTypeDef *hsd) 02843 { 02844 HAL_SD_ErrorTypedef errorstate = SD_OK; 02845 02846 while (!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT)) 02847 { 02848 } 02849 02850 if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CTIMEOUT)) 02851 { 02852 errorstate = SD_CMD_RSP_TIMEOUT; 02853 02854 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_CTIMEOUT); 02855 02856 return errorstate; 02857 } 02858 02859 /* Clear all the static flags */ 02860 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 02861 02862 return errorstate; 02863 } 02864 02865 /** 02866 * @brief Checks for error conditions for R2 (CID or CSD) response. 02867 * @param hsd: SD handle 02868 * @retval SD Card error state 02869 */ 02870 static HAL_SD_ErrorTypedef SD_CmdResp2Error(SD_HandleTypeDef *hsd) 02871 { 02872 HAL_SD_ErrorTypedef errorstate = SD_OK; 02873 02874 while (!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT)) 02875 { 02876 } 02877 02878 if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CTIMEOUT)) 02879 { 02880 errorstate = SD_CMD_RSP_TIMEOUT; 02881 02882 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_CTIMEOUT); 02883 02884 return errorstate; 02885 } 02886 else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CCRCFAIL)) 02887 { 02888 errorstate = SD_CMD_CRC_FAIL; 02889 02890 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_CCRCFAIL); 02891 02892 return errorstate; 02893 } 02894 else 02895 { 02896 /* No error flag set */ 02897 } 02898 02899 /* Clear all the static flags */ 02900 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 02901 02902 return errorstate; 02903 } 02904 02905 /** 02906 * @brief Checks for error conditions for R6 (RCA) response. 02907 * @param hsd: SD handle 02908 * @param SD_CMD: The sent command index 02909 * @param pRCA: Pointer to the variable that will contain the SD card relative 02910 * address RCA 02911 * @retval SD Card error state 02912 */ 02913 static HAL_SD_ErrorTypedef SD_CmdResp6Error(SD_HandleTypeDef *hsd, uint8_t SD_CMD, uint16_t *pRCA) 02914 { 02915 HAL_SD_ErrorTypedef errorstate = SD_OK; 02916 uint32_t response_r1; 02917 02918 while(!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT)) 02919 { 02920 } 02921 02922 if(__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CTIMEOUT)) 02923 { 02924 errorstate = SD_CMD_RSP_TIMEOUT; 02925 02926 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_CTIMEOUT); 02927 02928 return errorstate; 02929 } 02930 else if(__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CCRCFAIL)) 02931 { 02932 errorstate = SD_CMD_CRC_FAIL; 02933 02934 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_CCRCFAIL); 02935 02936 return errorstate; 02937 } 02938 else 02939 { 02940 /* No error flag set */ 02941 } 02942 02943 /* Check response received is of desired command */ 02944 if(SDMMC_GetCommandResponse(hsd->Instance) != SD_CMD) 02945 { 02946 errorstate = SD_ILLEGAL_CMD; 02947 02948 return errorstate; 02949 } 02950 02951 /* Clear all the static flags */ 02952 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 02953 02954 /* We have received response, retrieve it. */ 02955 response_r1 = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1); 02956 02957 if((response_r1 & (SD_R6_GENERAL_UNKNOWN_ERROR | SD_R6_ILLEGAL_CMD | SD_R6_COM_CRC_FAILED)) == SD_ALLZERO) 02958 { 02959 *pRCA = (uint16_t) (response_r1 >> 16); 02960 02961 return errorstate; 02962 } 02963 02964 if((response_r1 & SD_R6_GENERAL_UNKNOWN_ERROR) == SD_R6_GENERAL_UNKNOWN_ERROR) 02965 { 02966 return(SD_GENERAL_UNKNOWN_ERROR); 02967 } 02968 02969 if((response_r1 & SD_R6_ILLEGAL_CMD) == SD_R6_ILLEGAL_CMD) 02970 { 02971 return(SD_ILLEGAL_CMD); 02972 } 02973 02974 if((response_r1 & SD_R6_COM_CRC_FAILED) == SD_R6_COM_CRC_FAILED) 02975 { 02976 return(SD_COM_CRC_FAILED); 02977 } 02978 02979 return errorstate; 02980 } 02981 02982 /** 02983 * @brief Enables the SDMMC wide bus mode. 02984 * @param hsd: SD handle 02985 * @retval SD Card error state 02986 */ 02987 static HAL_SD_ErrorTypedef SD_WideBus_Enable(SD_HandleTypeDef *hsd) 02988 { 02989 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure; 02990 HAL_SD_ErrorTypedef errorstate = SD_OK; 02991 02992 uint32_t scr[2] = {0, 0}; 02993 02994 if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SD_CARD_LOCKED) == SD_CARD_LOCKED) 02995 { 02996 errorstate = SD_LOCK_UNLOCK_FAILED; 02997 02998 return errorstate; 02999 } 03000 03001 /* Get SCR Register */ 03002 errorstate = SD_FindSCR(hsd, scr); 03003 03004 if(errorstate != SD_OK) 03005 { 03006 return errorstate; 03007 } 03008 03009 /* If requested card supports wide bus operation */ 03010 if((scr[1] & SD_WIDE_BUS_SUPPORT) != SD_ALLZERO) 03011 { 03012 /* Send CMD55 APP_CMD with argument as card's RCA.*/ 03013 sdmmc_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16); 03014 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD; 03015 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT; 03016 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO; 03017 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE; 03018 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 03019 03020 /* Check for error conditions */ 03021 errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD); 03022 03023 if(errorstate != SD_OK) 03024 { 03025 return errorstate; 03026 } 03027 03028 /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */ 03029 sdmmc_cmdinitstructure.Argument = 2; 03030 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH; 03031 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 03032 03033 /* Check for error conditions */ 03034 errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_SD_SET_BUSWIDTH); 03035 03036 if(errorstate != SD_OK) 03037 { 03038 return errorstate; 03039 } 03040 03041 return errorstate; 03042 } 03043 else 03044 { 03045 errorstate = SD_REQUEST_NOT_APPLICABLE; 03046 03047 return errorstate; 03048 } 03049 } 03050 03051 /** 03052 * @brief Disables the SDMMC wide bus mode. 03053 * @param hsd: SD handle 03054 * @retval SD Card error state 03055 */ 03056 static HAL_SD_ErrorTypedef SD_WideBus_Disable(SD_HandleTypeDef *hsd) 03057 { 03058 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure; 03059 HAL_SD_ErrorTypedef errorstate = SD_OK; 03060 03061 uint32_t scr[2] = {0, 0}; 03062 03063 if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SD_CARD_LOCKED) == SD_CARD_LOCKED) 03064 { 03065 errorstate = SD_LOCK_UNLOCK_FAILED; 03066 03067 return errorstate; 03068 } 03069 03070 /* Get SCR Register */ 03071 errorstate = SD_FindSCR(hsd, scr); 03072 03073 if(errorstate != SD_OK) 03074 { 03075 return errorstate; 03076 } 03077 03078 /* If requested card supports 1 bit mode operation */ 03079 if((scr[1] & SD_SINGLE_BUS_SUPPORT) != SD_ALLZERO) 03080 { 03081 /* Send CMD55 APP_CMD with argument as card's RCA */ 03082 sdmmc_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16); 03083 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD; 03084 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT; 03085 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO; 03086 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE; 03087 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 03088 03089 /* Check for error conditions */ 03090 errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD); 03091 03092 if(errorstate != SD_OK) 03093 { 03094 return errorstate; 03095 } 03096 03097 /* Send ACMD6 APP_CMD with argument as 0 for single bus mode */ 03098 sdmmc_cmdinitstructure.Argument = 0; 03099 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH; 03100 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 03101 03102 /* Check for error conditions */ 03103 errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_SD_SET_BUSWIDTH); 03104 03105 if(errorstate != SD_OK) 03106 { 03107 return errorstate; 03108 } 03109 03110 return errorstate; 03111 } 03112 else 03113 { 03114 errorstate = SD_REQUEST_NOT_APPLICABLE; 03115 03116 return errorstate; 03117 } 03118 } 03119 03120 03121 /** 03122 * @brief Finds the SD card SCR register value. 03123 * @param hsd: SD handle 03124 * @param pSCR: pointer to the buffer that will contain the SCR value 03125 * @retval SD Card error state 03126 */ 03127 static HAL_SD_ErrorTypedef SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR) 03128 { 03129 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure; 03130 SDMMC_DataInitTypeDef sdmmc_datainitstructure; 03131 HAL_SD_ErrorTypedef errorstate = SD_OK; 03132 uint32_t index = 0; 03133 uint32_t tempscr[2] = {0, 0}; 03134 03135 /* Set Block Size To 8 Bytes */ 03136 /* Send CMD55 APP_CMD with argument as card's RCA */ 03137 sdmmc_cmdinitstructure.Argument = (uint32_t)8; 03138 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN; 03139 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT; 03140 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO; 03141 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE; 03142 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 03143 03144 /* Check for error conditions */ 03145 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN); 03146 03147 if(errorstate != SD_OK) 03148 { 03149 return errorstate; 03150 } 03151 03152 /* Send CMD55 APP_CMD with argument as card's RCA */ 03153 sdmmc_cmdinitstructure.Argument = (uint32_t)((hsd->RCA) << 16); 03154 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD; 03155 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 03156 03157 /* Check for error conditions */ 03158 errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD); 03159 03160 if(errorstate != SD_OK) 03161 { 03162 return errorstate; 03163 } 03164 sdmmc_datainitstructure.DataTimeOut = SD_DATATIMEOUT; 03165 sdmmc_datainitstructure.DataLength = 8; 03166 sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_8B; 03167 sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC; 03168 sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK; 03169 sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE; 03170 SDMMC_DataConfig(hsd->Instance, &sdmmc_datainitstructure); 03171 03172 /* Send ACMD51 SD_APP_SEND_SCR with argument as 0 */ 03173 sdmmc_cmdinitstructure.Argument = 0; 03174 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SD_APP_SEND_SCR; 03175 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 03176 03177 /* Check for error conditions */ 03178 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SD_APP_SEND_SCR); 03179 03180 if(errorstate != SD_OK) 03181 { 03182 return errorstate; 03183 } 03184 03185 while(!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND)) 03186 { 03187 if(__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL)) 03188 { 03189 *(tempscr + index) = SDMMC_ReadFIFO(hsd->Instance); 03190 index++; 03191 } 03192 } 03193 03194 if(__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT)) 03195 { 03196 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT); 03197 03198 errorstate = SD_DATA_TIMEOUT; 03199 03200 return errorstate; 03201 } 03202 else if(__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL)) 03203 { 03204 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL); 03205 03206 errorstate = SD_DATA_CRC_FAIL; 03207 03208 return errorstate; 03209 } 03210 else if(__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR)) 03211 { 03212 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR); 03213 03214 errorstate = SD_RX_OVERRUN; 03215 03216 return errorstate; 03217 } 03218 else 03219 { 03220 /* No error flag set */ 03221 } 03222 03223 /* Clear all the static flags */ 03224 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 03225 03226 *(pSCR + 1) = ((tempscr[0] & SD_0TO7BITS) << 24) | ((tempscr[0] & SD_8TO15BITS) << 8) |\ 03227 ((tempscr[0] & SD_16TO23BITS) >> 8) | ((tempscr[0] & SD_24TO31BITS) >> 24); 03228 03229 *(pSCR) = ((tempscr[1] & SD_0TO7BITS) << 24) | ((tempscr[1] & SD_8TO15BITS) << 8) |\ 03230 ((tempscr[1] & SD_16TO23BITS) >> 8) | ((tempscr[1] & SD_24TO31BITS) >> 24); 03231 03232 return errorstate; 03233 } 03234 03235 /** 03236 * @brief Checks if the SD card is in programming state. 03237 * @param hsd: SD handle 03238 * @param pStatus: pointer to the variable that will contain the SD card state 03239 * @retval SD Card error state 03240 */ 03241 static HAL_SD_ErrorTypedef SD_IsCardProgramming(SD_HandleTypeDef *hsd, uint8_t *pStatus) 03242 { 03243 SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure; 03244 HAL_SD_ErrorTypedef errorstate = SD_OK; 03245 __IO uint32_t responseR1 = 0; 03246 03247 sdmmc_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16); 03248 sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SEND_STATUS; 03249 sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT; 03250 sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO; 03251 sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE; 03252 SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure); 03253 03254 while(!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT)) 03255 { 03256 } 03257 03258 if(__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CTIMEOUT)) 03259 { 03260 errorstate = SD_CMD_RSP_TIMEOUT; 03261 03262 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_CTIMEOUT); 03263 03264 return errorstate; 03265 } 03266 else if(__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CCRCFAIL)) 03267 { 03268 errorstate = SD_CMD_CRC_FAIL; 03269 03270 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_CCRCFAIL); 03271 03272 return errorstate; 03273 } 03274 else 03275 { 03276 /* No error flag set */ 03277 } 03278 03279 /* Check response received is of desired command */ 03280 if((uint32_t)SDMMC_GetCommandResponse(hsd->Instance) != SD_CMD_SEND_STATUS) 03281 { 03282 errorstate = SD_ILLEGAL_CMD; 03283 03284 return errorstate; 03285 } 03286 03287 /* Clear all the static flags */ 03288 __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 03289 03290 03291 /* We have received response, retrieve it for analysis */ 03292 responseR1 = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1); 03293 03294 /* Find out card status */ 03295 *pStatus = (uint8_t)((responseR1 >> 9) & 0x0000000F); 03296 03297 if((responseR1 & SD_OCR_ERRORBITS) == SD_ALLZERO) 03298 { 03299 return errorstate; 03300 } 03301 03302 if((responseR1 & SD_OCR_ADDR_OUT_OF_RANGE) == SD_OCR_ADDR_OUT_OF_RANGE) 03303 { 03304 return(SD_ADDR_OUT_OF_RANGE); 03305 } 03306 03307 if((responseR1 & SD_OCR_ADDR_MISALIGNED) == SD_OCR_ADDR_MISALIGNED) 03308 { 03309 return(SD_ADDR_MISALIGNED); 03310 } 03311 03312 if((responseR1 & SD_OCR_BLOCK_LEN_ERR) == SD_OCR_BLOCK_LEN_ERR) 03313 { 03314 return(SD_BLOCK_LEN_ERR); 03315 } 03316 03317 if((responseR1 & SD_OCR_ERASE_SEQ_ERR) == SD_OCR_ERASE_SEQ_ERR) 03318 { 03319 return(SD_ERASE_SEQ_ERR); 03320 } 03321 03322 if((responseR1 & SD_OCR_BAD_ERASE_PARAM) == SD_OCR_BAD_ERASE_PARAM) 03323 { 03324 return(SD_BAD_ERASE_PARAM); 03325 } 03326 03327 if((responseR1 & SD_OCR_WRITE_PROT_VIOLATION) == SD_OCR_WRITE_PROT_VIOLATION) 03328 { 03329 return(SD_WRITE_PROT_VIOLATION); 03330 } 03331 03332 if((responseR1 & SD_OCR_LOCK_UNLOCK_FAILED) == SD_OCR_LOCK_UNLOCK_FAILED) 03333 { 03334 return(SD_LOCK_UNLOCK_FAILED); 03335 } 03336 03337 if((responseR1 & SD_OCR_COM_CRC_FAILED) == SD_OCR_COM_CRC_FAILED) 03338 { 03339 return(SD_COM_CRC_FAILED); 03340 } 03341 03342 if((responseR1 & SD_OCR_ILLEGAL_CMD) == SD_OCR_ILLEGAL_CMD) 03343 { 03344 return(SD_ILLEGAL_CMD); 03345 } 03346 03347 if((responseR1 & SD_OCR_CARD_ECC_FAILED) == SD_OCR_CARD_ECC_FAILED) 03348 { 03349 return(SD_CARD_ECC_FAILED); 03350 } 03351 03352 if((responseR1 & SD_OCR_CC_ERROR) == SD_OCR_CC_ERROR) 03353 { 03354 return(SD_CC_ERROR); 03355 } 03356 03357 if((responseR1 & SD_OCR_GENERAL_UNKNOWN_ERROR) == SD_OCR_GENERAL_UNKNOWN_ERROR) 03358 { 03359 return(SD_GENERAL_UNKNOWN_ERROR); 03360 } 03361 03362 if((responseR1 & SD_OCR_STREAM_READ_UNDERRUN) == SD_OCR_STREAM_READ_UNDERRUN) 03363 { 03364 return(SD_STREAM_READ_UNDERRUN); 03365 } 03366 03367 if((responseR1 & SD_OCR_STREAM_WRITE_OVERRUN) == SD_OCR_STREAM_WRITE_OVERRUN) 03368 { 03369 return(SD_STREAM_WRITE_OVERRUN); 03370 } 03371 03372 if((responseR1 & SD_OCR_CID_CSD_OVERWRITE) == SD_OCR_CID_CSD_OVERWRITE) 03373 { 03374 return(SD_CID_CSD_OVERWRITE); 03375 } 03376 03377 if((responseR1 & SD_OCR_WP_ERASE_SKIP) == SD_OCR_WP_ERASE_SKIP) 03378 { 03379 return(SD_WP_ERASE_SKIP); 03380 } 03381 03382 if((responseR1 & SD_OCR_CARD_ECC_DISABLED) == SD_OCR_CARD_ECC_DISABLED) 03383 { 03384 return(SD_CARD_ECC_DISABLED); 03385 } 03386 03387 if((responseR1 & SD_OCR_ERASE_RESET) == SD_OCR_ERASE_RESET) 03388 { 03389 return(SD_ERASE_RESET); 03390 } 03391 03392 if((responseR1 & SD_OCR_AKE_SEQ_ERROR) == SD_OCR_AKE_SEQ_ERROR) 03393 { 03394 return(SD_AKE_SEQ_ERROR); 03395 } 03396 03397 return errorstate; 03398 } 03399 03400 /** 03401 * @} 03402 */ 03403 03404 #endif /* HAL_SD_MODULE_ENABLED */ 03405 03406 /** 03407 * @} 03408 */ 03409 03410 /** 03411 * @} 03412 */ 03413 03414 #endif /* SDMMC1 */ 03415 03416 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Tue Jul 12 2022 10:59:58 by
