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