STM32F439xx HAL User Manual
|
00001 /** 00002 ****************************************************************************** 00003 * @file stm32f4xx_hal_sd.c 00004 * @author MCD Application Team 00005 * @brief SD card HAL module driver. 00006 * This file provides firmware functions to manage the following 00007 * functionalities of the Secure Digital (SD) peripheral: 00008 * + Initialization and de-initialization functions 00009 * + IO operation functions 00010 * + Peripheral Control functions 00011 * + SD card Control functions 00012 * 00013 @verbatim 00014 ============================================================================== 00015 ##### How to use this driver ##### 00016 ============================================================================== 00017 [..] 00018 This driver implements a high level communication layer for read and write from/to 00019 this memory. The needed STM32 hardware resources (SDIO and GPIO) are performed by 00020 the user in HAL_SD_MspInit() function (MSP layer). 00021 Basically, the MSP layer configuration should be the same as we provide in the 00022 examples. 00023 You can easily tailor this configuration according to hardware resources. 00024 00025 [..] 00026 This driver is a generic layered driver for SDIO memories which uses the HAL 00027 SDIO driver functions to interface with SD and uSD cards devices. 00028 It is used as follows: 00029 00030 (#)Initialize the SDIO low level resources by implement the HAL_SD_MspInit() API: 00031 (##) Enable the SDIO interface clock using __HAL_RCC_SDIO_CLK_ENABLE(); 00032 (##) SDIO pins configuration for SD card 00033 (+++) Enable the clock for the SDIO GPIOs using the functions __HAL_RCC_GPIOx_CLK_ENABLE(); 00034 (+++) Configure these SDIO pins as alternate function pull-up using HAL_GPIO_Init() 00035 and according to your pin assignment; 00036 (##) DMA Configuration if you need to use DMA process (HAL_SD_ReadBlocks_DMA() 00037 and HAL_SD_WriteBlocks_DMA() APIs). 00038 (+++) Enable the DMAx interface clock using __HAL_RCC_DMAx_CLK_ENABLE(); 00039 (+++) Configure the DMA using the function HAL_DMA_Init() with predeclared and filled. 00040 (##) NVIC configuration if you need to use interrupt process when using DMA transfer. 00041 (+++) Configure the SDIO and DMA interrupt priorities using functions 00042 HAL_NVIC_SetPriority(); DMA priority is superior to SDIO's priority 00043 (+++) Enable the NVIC DMA and SDIO IRQs using function HAL_NVIC_EnableIRQ() 00044 (+++) SDIO interrupts are managed using the macros __HAL_SD_ENABLE_IT() 00045 and __HAL_SD_DISABLE_IT() inside the communication process. 00046 (+++) SDIO interrupts pending bits are managed using the macros __HAL_SD_GET_IT() 00047 and __HAL_SD_CLEAR_IT() 00048 (##) NVIC configuration if you need to use interrupt process (HAL_SD_ReadBlocks_IT() 00049 and HAL_SD_WriteBlocks_IT() APIs). 00050 (+++) Configure the SDIO interrupt priorities using function 00051 HAL_NVIC_SetPriority(); 00052 (+++) Enable the NVIC SDIO IRQs using function HAL_NVIC_EnableIRQ() 00053 (+++) SDIO interrupts are managed using the macros __HAL_SD_ENABLE_IT() 00054 and __HAL_SD_DISABLE_IT() inside the communication process. 00055 (+++) SDIO interrupts pending bits are managed using the macros __HAL_SD_GET_IT() 00056 and __HAL_SD_CLEAR_IT() 00057 (#) At this stage, you can perform SD read/write/erase operations after SD card initialization 00058 00059 00060 *** SD Card Initialization and configuration *** 00061 ================================================ 00062 [..] 00063 To initialize the SD Card, use the HAL_SD_Init() function. It Initializes 00064 SDIO IP(STM32 side) and the SD Card, and put it into StandBy State (Ready for data transfer). 00065 This function provide the following operations: 00066 00067 (#) Initialize the SDIO peripheral interface with defaullt configuration. 00068 The initialization process is done at 400KHz. You can change or adapt 00069 this frequency by adjusting the "ClockDiv" field. 00070 The SD Card frequency (SDIO_CK) is computed as follows: 00071 00072 SDIO_CK = SDIOCLK / (ClockDiv + 2) 00073 00074 In initialization mode and according to the SD Card standard, 00075 make sure that the SDIO_CK frequency doesn't exceed 400KHz. 00076 00077 This phase of initialization is done through SDIO_Init() and 00078 SDIO_PowerState_ON() SDIO low level APIs. 00079 00080 (#) Initialize the SD card. The API used is HAL_SD_InitCard(). 00081 This phase allows the card initialization and identification 00082 and check the SD Card type (Standard Capacity or High Capacity) 00083 The initialization flow is compatible with SD standard. 00084 00085 This API (HAL_SD_InitCard()) could be used also to reinitialize the card in case 00086 of plug-off plug-in. 00087 00088 (#) Configure the SD Card Data transfer frequency. By Default, the card transfer 00089 frequency is set to 24MHz. You can change or adapt this frequency by adjusting 00090 the "ClockDiv" field. 00091 In transfer mode and according to the SD Card standard, make sure that the 00092 SDIO_CK frequency doesn't exceed 25MHz and 50MHz in High-speed mode switch. 00093 To be able to use a frequency higher than 24MHz, you should use the SDIO 00094 peripheral in bypass mode. Refer to the corresponding reference manual 00095 for more details. 00096 00097 (#) Select the corresponding SD Card according to the address read with the step 2. 00098 00099 (#) Configure the SD Card in wide bus mode: 4-bits data. 00100 00101 *** SD Card Read operation *** 00102 ============================== 00103 [..] 00104 (+) You can read from SD card in polling mode by using function HAL_SD_ReadBlocks(). 00105 This function allows the read of 512 bytes blocks. 00106 You can choose either one block read operation or multiple block read operation 00107 by adjusting the "NumberOfBlocks" parameter. 00108 After this, you have to ensure that the transfer is done correctly. The check is done 00109 through HAL_SD_GetCardState() function for SD card state. 00110 00111 (+) You can read from SD card in DMA mode by using function HAL_SD_ReadBlocks_DMA(). 00112 This function allows the read of 512 bytes blocks. 00113 You can choose either one block read operation or multiple block read operation 00114 by adjusting the "NumberOfBlocks" parameter. 00115 After this, you have to ensure that the transfer is done correctly. The check is done 00116 through HAL_SD_GetCardState() function for SD card state. 00117 You could also check the DMA transfer process through the SD Rx interrupt event. 00118 00119 (+) You can read from SD card in Interrupt mode by using function HAL_SD_ReadBlocks_IT(). 00120 This function allows the read of 512 bytes blocks. 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 ensure that the transfer is done correctly. The check is done 00124 through HAL_SD_GetCardState() function for SD card state. 00125 You could also check the IT transfer process through the SD Rx interrupt event. 00126 00127 *** SD Card Write operation *** 00128 =============================== 00129 [..] 00130 (+) You can write to SD card in polling mode by using function HAL_SD_WriteBlocks(). 00131 This function allows the read of 512 bytes blocks. 00132 You can choose either one block read operation or multiple block read operation 00133 by adjusting the "NumberOfBlocks" parameter. 00134 After this, you have to ensure that the transfer is done correctly. The check is done 00135 through HAL_SD_GetCardState() function for SD card state. 00136 00137 (+) You can write to SD card in DMA mode by using function HAL_SD_WriteBlocks_DMA(). 00138 This function allows the read of 512 bytes blocks. 00139 You can choose either one block read operation or multiple block read operation 00140 by adjusting the "NumberOfBlocks" parameter. 00141 After this, you have to ensure that the transfer is done correctly. The check is done 00142 through HAL_SD_GetCardState() function for SD card state. 00143 You could also check the DMA transfer process through the SD Tx interrupt event. 00144 00145 (+) You can write to SD card in Interrupt mode by using function HAL_SD_WriteBlocks_IT(). 00146 This function allows the read of 512 bytes blocks. 00147 You can choose either one block read operation or multiple block read operation 00148 by adjusting the "NumberOfBlocks" parameter. 00149 After this, you have to ensure that the transfer is done correctly. The check is done 00150 through HAL_SD_GetCardState() function for SD card state. 00151 You could also check the IT transfer process through the SD Tx interrupt event. 00152 00153 *** SD card status *** 00154 ====================== 00155 [..] 00156 (+) The SD Status contains status bits that are related to the SD Memory 00157 Card proprietary features. To get SD card status use the HAL_SD_GetCardStatus(). 00158 00159 *** SD card information *** 00160 =========================== 00161 [..] 00162 (+) To get SD card information, you can use the function HAL_SD_GetCardInfo(). 00163 It returns useful information about the SD card such as block size, card type, 00164 block number ... 00165 00166 *** SD card CSD register *** 00167 ============================ 00168 [..] 00169 (+) The HAL_SD_GetCardCSD() API allows to get the parameters of the CSD register. 00170 Some of the CSD parameters are useful for card initialization and identification. 00171 00172 *** SD card CID register *** 00173 ============================ 00174 [..] 00175 (+) The HAL_SD_GetCardCID() API allows to get the parameters of the CID register. 00176 Some of the CSD parameters are useful for card initialization and identification. 00177 00178 *** SD HAL driver macros list *** 00179 ================================== 00180 [..] 00181 Below the list of most used macros in SD HAL driver. 00182 00183 (+) __HAL_SD_ENABLE : Enable the SD device 00184 (+) __HAL_SD_DISABLE : Disable the SD device 00185 (+) __HAL_SD_DMA_ENABLE: Enable the SDIO DMA transfer 00186 (+) __HAL_SD_DMA_DISABLE: Disable the SDIO DMA transfer 00187 (+) __HAL_SD_ENABLE_IT: Enable the SD device interrupt 00188 (+) __HAL_SD_DISABLE_IT: Disable the SD device interrupt 00189 (+) __HAL_SD_GET_FLAG:Check whether the specified SD flag is set or not 00190 (+) __HAL_SD_CLEAR_FLAG: Clear the SD's pending flags 00191 00192 [..] 00193 (@) You can refer to the SD HAL driver header file for more useful macros 00194 00195 @endverbatim 00196 ****************************************************************************** 00197 * @attention 00198 * 00199 * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> 00200 * 00201 * Redistribution and use in source and binary forms, with or without modification, 00202 * are permitted provided that the following conditions are met: 00203 * 1. Redistributions of source code must retain the above copyright notice, 00204 * this list of conditions and the following disclaimer. 00205 * 2. Redistributions in binary form must reproduce the above copyright notice, 00206 * this list of conditions and the following disclaimer in the documentation 00207 * and/or other materials provided with the distribution. 00208 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00209 * may be used to endorse or promote products derived from this software 00210 * without specific prior written permission. 00211 * 00212 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00213 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00214 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00215 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00216 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00217 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00218 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00219 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00220 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00221 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00222 * 00223 ****************************************************************************** 00224 */ 00225 00226 /* Includes ------------------------------------------------------------------*/ 00227 #include "stm32f4xx_hal.h" 00228 00229 /** @addtogroup STM32F4xx_HAL_Driver 00230 * @{ 00231 */ 00232 00233 /** @addtogroup SD 00234 * @{ 00235 */ 00236 00237 #ifdef HAL_SD_MODULE_ENABLED 00238 00239 #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) || \ 00240 defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || \ 00241 defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F411xE) || defined(STM32F446xx) || \ 00242 defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || \ 00243 defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx) 00244 00245 /* Private typedef -----------------------------------------------------------*/ 00246 /* Private define ------------------------------------------------------------*/ 00247 /** @addtogroup SD_Private_Defines 00248 * @{ 00249 */ 00250 00251 /** 00252 * @} 00253 */ 00254 00255 /* Private macro -------------------------------------------------------------*/ 00256 /* Private variables ---------------------------------------------------------*/ 00257 /* Private function prototypes -----------------------------------------------*/ 00258 /* Private functions ---------------------------------------------------------*/ 00259 /** @defgroup SD_Private_Functions SD Private Functions 00260 * @{ 00261 */ 00262 static uint32_t SD_InitCard(SD_HandleTypeDef *hsd); 00263 static uint32_t SD_PowerON(SD_HandleTypeDef *hsd); 00264 static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus); 00265 static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus); 00266 static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd); 00267 static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd); 00268 static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR); 00269 static HAL_StatusTypeDef SD_PowerOFF(SD_HandleTypeDef *hsd); 00270 static HAL_StatusTypeDef SD_Write_IT(SD_HandleTypeDef *hsd); 00271 static HAL_StatusTypeDef SD_Read_IT(SD_HandleTypeDef *hsd); 00272 static void SD_DMATransmitCplt(DMA_HandleTypeDef *hdma); 00273 static void SD_DMAReceiveCplt(DMA_HandleTypeDef *hdma); 00274 static void SD_DMAError(DMA_HandleTypeDef *hdma); 00275 static void SD_DMATxAbort(DMA_HandleTypeDef *hdma); 00276 static void SD_DMARxAbort(DMA_HandleTypeDef *hdma); 00277 /** 00278 * @} 00279 */ 00280 00281 /* Exported functions --------------------------------------------------------*/ 00282 /** @addtogroup SD_Exported_Functions 00283 * @{ 00284 */ 00285 00286 /** @addtogroup SD_Exported_Functions_Group1 00287 * @brief Initialization and de-initialization functions 00288 * 00289 @verbatim 00290 ============================================================================== 00291 ##### Initialization and de-initialization functions ##### 00292 ============================================================================== 00293 [..] 00294 This section provides functions allowing to initialize/de-initialize the SD 00295 card device to be ready for use. 00296 00297 @endverbatim 00298 * @{ 00299 */ 00300 00301 /** 00302 * @brief Initializes the SD according to the specified parameters in the 00303 SD_HandleTypeDef and create the associated handle. 00304 * @param hsd Pointer to the SD handle 00305 * @retval HAL status 00306 */ 00307 HAL_StatusTypeDef HAL_SD_Init(SD_HandleTypeDef *hsd) 00308 { 00309 /* Check the SD handle allocation */ 00310 if(hsd == NULL) 00311 { 00312 return HAL_ERROR; 00313 } 00314 00315 /* Check the parameters */ 00316 assert_param(IS_SDIO_ALL_INSTANCE(hsd->Instance)); 00317 assert_param(IS_SDIO_CLOCK_EDGE(hsd->Init.ClockEdge)); 00318 assert_param(IS_SDIO_CLOCK_BYPASS(hsd->Init.ClockBypass)); 00319 assert_param(IS_SDIO_CLOCK_POWER_SAVE(hsd->Init.ClockPowerSave)); 00320 assert_param(IS_SDIO_BUS_WIDE(hsd->Init.BusWide)); 00321 assert_param(IS_SDIO_HARDWARE_FLOW_CONTROL(hsd->Init.HardwareFlowControl)); 00322 assert_param(IS_SDIO_CLKDIV(hsd->Init.ClockDiv)); 00323 00324 if(hsd->State == HAL_SD_STATE_RESET) 00325 { 00326 /* Allocate lock resource and initialize it */ 00327 hsd->Lock = HAL_UNLOCKED; 00328 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */ 00329 HAL_SD_MspInit(hsd); 00330 } 00331 00332 hsd->State = HAL_SD_STATE_BUSY; 00333 00334 /* Initialize the Card parameters */ 00335 HAL_SD_InitCard(hsd); 00336 00337 /* Initialize the error code */ 00338 hsd->ErrorCode = HAL_DMA_ERROR_NONE; 00339 00340 /* Initialize the SD operation */ 00341 hsd->Context = SD_CONTEXT_NONE; 00342 00343 /* Initialize the SD state */ 00344 hsd->State = HAL_SD_STATE_READY; 00345 00346 return HAL_OK; 00347 } 00348 00349 /** 00350 * @brief Initializes the SD Card. 00351 * @param hsd Pointer to SD handle 00352 * @note This function initializes the SD card. It could be used when a card 00353 re-initialization is needed. 00354 * @retval HAL status 00355 */ 00356 HAL_StatusTypeDef HAL_SD_InitCard(SD_HandleTypeDef *hsd) 00357 { 00358 uint32_t errorstate = HAL_SD_ERROR_NONE; 00359 SD_InitTypeDef Init; 00360 00361 /* Default SDIO peripheral configuration for SD card initialization */ 00362 Init.ClockEdge = SDIO_CLOCK_EDGE_RISING; 00363 Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE; 00364 Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE; 00365 Init.BusWide = SDIO_BUS_WIDE_1B; 00366 Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE; 00367 Init.ClockDiv = SDIO_INIT_CLK_DIV; 00368 00369 /* Initialize SDIO peripheral interface with default configuration */ 00370 SDIO_Init(hsd->Instance, Init); 00371 00372 /* Disable SDIO Clock */ 00373 __HAL_SD_DISABLE(hsd); 00374 00375 /* Set Power State to ON */ 00376 SDIO_PowerState_ON(hsd->Instance); 00377 00378 /* Enable SDIO Clock */ 00379 __HAL_SD_ENABLE(hsd); 00380 00381 /* Required power up waiting time before starting the SD initialization 00382 sequence */ 00383 HAL_Delay(2U); 00384 00385 /* Identify card operating voltage */ 00386 errorstate = SD_PowerON(hsd); 00387 if(errorstate != HAL_SD_ERROR_NONE) 00388 { 00389 hsd->State = HAL_SD_STATE_READY; 00390 hsd->ErrorCode |= errorstate; 00391 return HAL_ERROR; 00392 } 00393 00394 /* Card initialization */ 00395 errorstate = SD_InitCard(hsd); 00396 if(errorstate != HAL_SD_ERROR_NONE) 00397 { 00398 hsd->State = HAL_SD_STATE_READY; 00399 hsd->ErrorCode |= errorstate; 00400 return HAL_ERROR; 00401 } 00402 00403 return HAL_OK; 00404 } 00405 00406 /** 00407 * @brief De-Initializes the SD card. 00408 * @param hsd Pointer to SD handle 00409 * @retval HAL status 00410 */ 00411 HAL_StatusTypeDef HAL_SD_DeInit(SD_HandleTypeDef *hsd) 00412 { 00413 /* Check the SD handle allocation */ 00414 if(hsd == NULL) 00415 { 00416 return HAL_ERROR; 00417 } 00418 00419 /* Check the parameters */ 00420 assert_param(IS_SDIO_ALL_INSTANCE(hsd->Instance)); 00421 00422 hsd->State = HAL_SD_STATE_BUSY; 00423 00424 /* Set SD power state to off */ 00425 SD_PowerOFF(hsd); 00426 00427 /* De-Initialize the MSP layer */ 00428 HAL_SD_MspDeInit(hsd); 00429 00430 hsd->ErrorCode = HAL_SD_ERROR_NONE; 00431 hsd->State = HAL_SD_STATE_RESET; 00432 00433 return HAL_OK; 00434 } 00435 00436 00437 /** 00438 * @brief Initializes the SD MSP. 00439 * @param hsd Pointer to SD handle 00440 * @retval None 00441 */ 00442 __weak void HAL_SD_MspInit(SD_HandleTypeDef *hsd) 00443 { 00444 /* Prevent unused argument(s) compilation warning */ 00445 UNUSED(hsd); 00446 00447 /* NOTE : This function Should not be modified, when the callback is needed, 00448 the HAL_SD_MspInit could be implemented in the user file 00449 */ 00450 } 00451 00452 /** 00453 * @brief De-Initialize SD MSP. 00454 * @param hsd Pointer to SD handle 00455 * @retval None 00456 */ 00457 __weak void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd) 00458 { 00459 /* Prevent unused argument(s) compilation warning */ 00460 UNUSED(hsd); 00461 00462 /* NOTE : This function Should not be modified, when the callback is needed, 00463 the HAL_SD_MspDeInit could be implemented in the user file 00464 */ 00465 } 00466 00467 /** 00468 * @} 00469 */ 00470 00471 /** @addtogroup SD_Exported_Functions_Group2 00472 * @brief Data transfer functions 00473 * 00474 @verbatim 00475 ============================================================================== 00476 ##### IO operation functions ##### 00477 ============================================================================== 00478 [..] 00479 This subsection provides a set of functions allowing to manage the data 00480 transfer from/to SD card. 00481 00482 @endverbatim 00483 * @{ 00484 */ 00485 00486 /** 00487 * @brief Reads block(s) from a specified address in a card. The Data transfer 00488 * is managed by polling mode. 00489 * @note This API should be followed by a check on the card state through 00490 * HAL_SD_GetCardState(). 00491 * @param hsd Pointer to SD handle 00492 * @param pData pointer to the buffer that will contain the received data 00493 * @param BlockAdd Block Address from where data is to be read 00494 * @param NumberOfBlocks Number of SD blocks to read 00495 * @param Timeout Specify timeout value 00496 * @retval HAL status 00497 */ 00498 HAL_StatusTypeDef HAL_SD_ReadBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout) 00499 { 00500 SDIO_DataInitTypeDef config; 00501 uint32_t errorstate = HAL_SD_ERROR_NONE; 00502 uint32_t tickstart = HAL_GetTick(); 00503 uint32_t count = 0U, *tempbuff = (uint32_t *)pData; 00504 00505 if(NULL == pData) 00506 { 00507 hsd->ErrorCode |= HAL_SD_ERROR_PARAM; 00508 return HAL_ERROR; 00509 } 00510 00511 if(hsd->State == HAL_SD_STATE_READY) 00512 { 00513 hsd->ErrorCode = HAL_DMA_ERROR_NONE; 00514 00515 if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr)) 00516 { 00517 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE; 00518 return HAL_ERROR; 00519 } 00520 00521 hsd->State = HAL_SD_STATE_BUSY; 00522 00523 /* Initialize data control register */ 00524 hsd->Instance->DCTRL = 0U; 00525 00526 if(hsd->SdCard.CardType != CARD_SDHC_SDXC) 00527 { 00528 BlockAdd *= 512U; 00529 } 00530 00531 /* Set Block Size for Card */ 00532 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE); 00533 if(errorstate != HAL_SD_ERROR_NONE) 00534 { 00535 /* Clear all the static flags */ 00536 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 00537 hsd->ErrorCode |= errorstate; 00538 hsd->State = HAL_SD_STATE_READY; 00539 return HAL_ERROR; 00540 } 00541 00542 /* Configure the SD DPSM (Data Path State Machine) */ 00543 config.DataTimeOut = SDMMC_DATATIMEOUT; 00544 config.DataLength = NumberOfBlocks * BLOCKSIZE; 00545 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B; 00546 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO; 00547 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK; 00548 config.DPSM = SDIO_DPSM_ENABLE; 00549 SDIO_ConfigData(hsd->Instance, &config); 00550 00551 /* Read block(s) in polling mode */ 00552 if(NumberOfBlocks > 1U) 00553 { 00554 hsd->Context = SD_CONTEXT_READ_MULTIPLE_BLOCK; 00555 00556 /* Read Multi Block command */ 00557 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, BlockAdd); 00558 } 00559 else 00560 { 00561 hsd->Context = SD_CONTEXT_READ_SINGLE_BLOCK; 00562 00563 /* Read Single Block command */ 00564 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, BlockAdd); 00565 } 00566 if(errorstate != HAL_SD_ERROR_NONE) 00567 { 00568 /* Clear all the static flags */ 00569 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 00570 hsd->ErrorCode |= errorstate; 00571 hsd->State = HAL_SD_STATE_READY; 00572 return HAL_ERROR; 00573 } 00574 00575 /* Poll on SDIO flags */ 00576 #ifdef SDIO_STA_STBITERR 00577 while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_STA_STBITERR)) 00578 #else /* SDIO_STA_STBITERR not defined */ 00579 while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND)) 00580 #endif /* SDIO_STA_STBITERR */ 00581 { 00582 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF)) 00583 { 00584 /* Read data from SDIO Rx FIFO */ 00585 for(count = 0U; count < 8U; count++) 00586 { 00587 *(tempbuff + count) = SDIO_ReadFIFO(hsd->Instance); 00588 } 00589 tempbuff += 8U; 00590 } 00591 00592 if((Timeout == 0U)||((HAL_GetTick()-tickstart) >= Timeout)) 00593 { 00594 /* Clear all the static flags */ 00595 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 00596 hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT; 00597 hsd->State= HAL_SD_STATE_READY; 00598 return HAL_TIMEOUT; 00599 } 00600 } 00601 00602 /* Send stop transmission command in case of multiblock read */ 00603 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1U)) 00604 { 00605 if(hsd->SdCard.CardType != CARD_SECURED) 00606 { 00607 /* Send stop transmission command */ 00608 errorstate = SDMMC_CmdStopTransfer(hsd->Instance); 00609 if(errorstate != HAL_SD_ERROR_NONE) 00610 { 00611 /* Clear all the static flags */ 00612 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 00613 hsd->ErrorCode |= errorstate; 00614 hsd->State = HAL_SD_STATE_READY; 00615 return HAL_ERROR; 00616 } 00617 } 00618 } 00619 00620 /* Get error state */ 00621 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT)) 00622 { 00623 /* Clear all the static flags */ 00624 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 00625 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT; 00626 hsd->State = HAL_SD_STATE_READY; 00627 return HAL_ERROR; 00628 } 00629 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL)) 00630 { 00631 /* Clear all the static flags */ 00632 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 00633 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL; 00634 hsd->State = HAL_SD_STATE_READY; 00635 return HAL_ERROR; 00636 } 00637 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR)) 00638 { 00639 /* Clear all the static flags */ 00640 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 00641 hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN; 00642 hsd->State = HAL_SD_STATE_READY; 00643 return HAL_ERROR; 00644 } 00645 00646 /* Empty FIFO if there is still any data */ 00647 while ((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXDAVL))) 00648 { 00649 *tempbuff = SDIO_ReadFIFO(hsd->Instance); 00650 tempbuff++; 00651 00652 if((Timeout == 0U)||((HAL_GetTick()-tickstart) >= Timeout)) 00653 { 00654 /* Clear all the static flags */ 00655 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 00656 hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT; 00657 hsd->State= HAL_SD_STATE_READY; 00658 return HAL_ERROR; 00659 } 00660 } 00661 00662 /* Clear all the static flags */ 00663 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 00664 00665 hsd->State = HAL_SD_STATE_READY; 00666 00667 return HAL_OK; 00668 } 00669 else 00670 { 00671 hsd->ErrorCode |= HAL_SD_ERROR_BUSY; 00672 return HAL_ERROR; 00673 } 00674 } 00675 00676 /** 00677 * @brief Allows to write block(s) to a specified address in a card. The Data 00678 * transfer is managed by polling mode. 00679 * @note This API should be followed by a check on the card state through 00680 * HAL_SD_GetCardState(). 00681 * @param hsd Pointer to SD handle 00682 * @param pData pointer to the buffer that will contain the data to transmit 00683 * @param BlockAdd Block Address where data will be written 00684 * @param NumberOfBlocks Number of SD blocks to write 00685 * @param Timeout Specify timeout value 00686 * @retval HAL status 00687 */ 00688 HAL_StatusTypeDef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout) 00689 { 00690 SDIO_DataInitTypeDef config; 00691 uint32_t errorstate = HAL_SD_ERROR_NONE; 00692 uint32_t tickstart = HAL_GetTick(); 00693 uint32_t count = 0U; 00694 uint32_t *tempbuff = (uint32_t *)pData; 00695 00696 if(NULL == pData) 00697 { 00698 hsd->ErrorCode |= HAL_SD_ERROR_PARAM; 00699 return HAL_ERROR; 00700 } 00701 00702 if(hsd->State == HAL_SD_STATE_READY) 00703 { 00704 hsd->ErrorCode = HAL_DMA_ERROR_NONE; 00705 00706 if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr)) 00707 { 00708 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE; 00709 return HAL_ERROR; 00710 } 00711 00712 hsd->State = HAL_SD_STATE_BUSY; 00713 00714 /* Initialize data control register */ 00715 hsd->Instance->DCTRL = 0U; 00716 00717 if(hsd->SdCard.CardType != CARD_SDHC_SDXC) 00718 { 00719 BlockAdd *= 512U; 00720 } 00721 00722 /* Set Block Size for Card */ 00723 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE); 00724 if(errorstate != HAL_SD_ERROR_NONE) 00725 { 00726 /* Clear all the static flags */ 00727 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 00728 hsd->ErrorCode |= errorstate; 00729 hsd->State = HAL_SD_STATE_READY; 00730 return HAL_ERROR; 00731 } 00732 00733 /* Write Blocks in Polling mode */ 00734 if(NumberOfBlocks > 1U) 00735 { 00736 hsd->Context = SD_CONTEXT_WRITE_MULTIPLE_BLOCK; 00737 00738 /* Write Multi Block command */ 00739 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, BlockAdd); 00740 } 00741 else 00742 { 00743 hsd->Context = SD_CONTEXT_WRITE_SINGLE_BLOCK; 00744 00745 /* Write Single Block command */ 00746 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, BlockAdd); 00747 } 00748 if(errorstate != HAL_SD_ERROR_NONE) 00749 { 00750 /* Clear all the static flags */ 00751 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 00752 hsd->ErrorCode |= errorstate; 00753 hsd->State = HAL_SD_STATE_READY; 00754 return HAL_ERROR; 00755 } 00756 00757 /* Configure the SD DPSM (Data Path State Machine) */ 00758 config.DataTimeOut = SDMMC_DATATIMEOUT; 00759 config.DataLength = NumberOfBlocks * BLOCKSIZE; 00760 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B; 00761 config.TransferDir = SDIO_TRANSFER_DIR_TO_CARD; 00762 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK; 00763 config.DPSM = SDIO_DPSM_ENABLE; 00764 SDIO_ConfigData(hsd->Instance, &config); 00765 00766 /* Write block(s) in polling mode */ 00767 #ifdef SDIO_STA_STBITERR 00768 while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_FLAG_STBITERR)) 00769 #else /* SDIO_STA_STBITERR not defined */ 00770 while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND)) 00771 #endif /* SDIO_STA_STBITERR */ 00772 { 00773 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXFIFOHE)) 00774 { 00775 /* Write data to SDIO Tx FIFO */ 00776 for(count = 0U; count < 8U; count++) 00777 { 00778 SDIO_WriteFIFO(hsd->Instance, (tempbuff + count)); 00779 } 00780 tempbuff += 8U; 00781 } 00782 00783 if((Timeout == 0U)||((HAL_GetTick()-tickstart) >= Timeout)) 00784 { 00785 /* Clear all the static flags */ 00786 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 00787 hsd->ErrorCode |= errorstate; 00788 hsd->State = HAL_SD_STATE_READY; 00789 return HAL_TIMEOUT; 00790 } 00791 } 00792 00793 /* Send stop transmission command in case of multiblock write */ 00794 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1U)) 00795 { 00796 if(hsd->SdCard.CardType != CARD_SECURED) 00797 { 00798 /* Send stop transmission command */ 00799 errorstate = SDMMC_CmdStopTransfer(hsd->Instance); 00800 if(errorstate != HAL_SD_ERROR_NONE) 00801 { 00802 /* Clear all the static flags */ 00803 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 00804 hsd->ErrorCode |= errorstate; 00805 hsd->State = HAL_SD_STATE_READY; 00806 return HAL_ERROR; 00807 } 00808 } 00809 } 00810 00811 /* Get error state */ 00812 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT)) 00813 { 00814 /* Clear all the static flags */ 00815 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 00816 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT; 00817 hsd->State = HAL_SD_STATE_READY; 00818 return HAL_ERROR; 00819 } 00820 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL)) 00821 { 00822 /* Clear all the static flags */ 00823 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 00824 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL; 00825 hsd->State = HAL_SD_STATE_READY; 00826 return HAL_ERROR; 00827 } 00828 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR)) 00829 { 00830 /* Clear all the static flags */ 00831 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 00832 hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN; 00833 hsd->State = HAL_SD_STATE_READY; 00834 return HAL_ERROR; 00835 } 00836 00837 /* Clear all the static flags */ 00838 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 00839 00840 hsd->State = HAL_SD_STATE_READY; 00841 00842 return HAL_OK; 00843 } 00844 else 00845 { 00846 hsd->ErrorCode |= HAL_SD_ERROR_BUSY; 00847 return HAL_ERROR; 00848 } 00849 } 00850 00851 /** 00852 * @brief Reads block(s) from a specified address in a card. The Data transfer 00853 * is managed in interrupt mode. 00854 * @note This API should be followed by a check on the card state through 00855 * HAL_SD_GetCardState(). 00856 * @note You could also check the IT transfer process through the SD Rx 00857 * interrupt event. 00858 * @param hsd Pointer to SD handle 00859 * @param pData Pointer to the buffer that will contain the received data 00860 * @param BlockAdd Block Address from where data is to be read 00861 * @param NumberOfBlocks Number of blocks to read. 00862 * @retval HAL status 00863 */ 00864 HAL_StatusTypeDef HAL_SD_ReadBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks) 00865 { 00866 SDIO_DataInitTypeDef config; 00867 uint32_t errorstate = HAL_SD_ERROR_NONE; 00868 00869 if(NULL == pData) 00870 { 00871 hsd->ErrorCode |= HAL_SD_ERROR_PARAM; 00872 return HAL_ERROR; 00873 } 00874 00875 if(hsd->State == HAL_SD_STATE_READY) 00876 { 00877 hsd->ErrorCode = HAL_DMA_ERROR_NONE; 00878 00879 if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr)) 00880 { 00881 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE; 00882 return HAL_ERROR; 00883 } 00884 00885 hsd->State = HAL_SD_STATE_BUSY; 00886 00887 /* Initialize data control register */ 00888 hsd->Instance->DCTRL = 0U; 00889 00890 hsd->pRxBuffPtr = (uint32_t *)pData; 00891 hsd->RxXferSize = BLOCKSIZE * NumberOfBlocks; 00892 00893 #ifdef SDIO_STA_STBITER 00894 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND | SDIO_FLAG_RXFIFOHF | SDIO_IT_STBITERR)); 00895 #else /* SDIO_STA_STBITERR not defined */ 00896 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND | SDIO_FLAG_RXFIFOHF)); 00897 #endif /* SDIO_STA_STBITERR */ 00898 00899 if(hsd->SdCard.CardType != CARD_SDHC_SDXC) 00900 { 00901 BlockAdd *= 512U; 00902 } 00903 00904 /* Configure the SD DPSM (Data Path State Machine) */ 00905 config.DataTimeOut = SDMMC_DATATIMEOUT; 00906 config.DataLength = BLOCKSIZE * NumberOfBlocks; 00907 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B; 00908 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO; 00909 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK; 00910 config.DPSM = SDIO_DPSM_ENABLE; 00911 SDIO_ConfigData(hsd->Instance, &config); 00912 00913 /* Set Block Size for Card */ 00914 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE); 00915 if(errorstate != HAL_SD_ERROR_NONE) 00916 { 00917 /* Clear all the static flags */ 00918 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 00919 hsd->ErrorCode |= errorstate; 00920 hsd->State = HAL_SD_STATE_READY; 00921 return HAL_ERROR; 00922 } 00923 00924 /* Read Blocks in IT mode */ 00925 if(NumberOfBlocks > 1U) 00926 { 00927 hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_IT); 00928 00929 /* Read Multi Block command */ 00930 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, BlockAdd); 00931 } 00932 else 00933 { 00934 hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_IT); 00935 00936 /* Read Single Block command */ 00937 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, BlockAdd); 00938 } 00939 if(errorstate != HAL_SD_ERROR_NONE) 00940 { 00941 /* Clear all the static flags */ 00942 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 00943 hsd->ErrorCode |= errorstate; 00944 hsd->State = HAL_SD_STATE_READY; 00945 return HAL_ERROR; 00946 } 00947 00948 return HAL_OK; 00949 } 00950 else 00951 { 00952 return HAL_BUSY; 00953 } 00954 } 00955 00956 /** 00957 * @brief Writes block(s) to a specified address in a card. The Data transfer 00958 * is managed in interrupt mode. 00959 * @note This API should be followed by a check on the card state through 00960 * HAL_SD_GetCardState(). 00961 * @note You could also check the IT transfer process through the SD Tx 00962 * interrupt event. 00963 * @param hsd Pointer to SD handle 00964 * @param pData Pointer to the buffer that will contain the data to transmit 00965 * @param BlockAdd Block Address where data will be written 00966 * @param NumberOfBlocks Number of blocks to write 00967 * @retval HAL status 00968 */ 00969 HAL_StatusTypeDef HAL_SD_WriteBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks) 00970 { 00971 SDIO_DataInitTypeDef config; 00972 uint32_t errorstate = HAL_SD_ERROR_NONE; 00973 00974 if(NULL == pData) 00975 { 00976 hsd->ErrorCode |= HAL_SD_ERROR_PARAM; 00977 return HAL_ERROR; 00978 } 00979 00980 if(hsd->State == HAL_SD_STATE_READY) 00981 { 00982 hsd->ErrorCode = HAL_DMA_ERROR_NONE; 00983 00984 if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr)) 00985 { 00986 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE; 00987 return HAL_ERROR; 00988 } 00989 00990 hsd->State = HAL_SD_STATE_BUSY; 00991 00992 /* Initialize data control register */ 00993 hsd->Instance->DCTRL = 0U; 00994 00995 hsd->pTxBuffPtr = (uint32_t *)pData; 00996 hsd->TxXferSize = BLOCKSIZE * NumberOfBlocks; 00997 00998 /* Enable transfer interrupts */ 00999 #ifdef SDIO_STA_STBITER 01000 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_DATAEND | SDIO_FLAG_TXFIFOHE | SDIO_IT_STBITERR)); 01001 #else /* SDIO_STA_STBITERR not defined */ 01002 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_DATAEND | SDIO_FLAG_TXFIFOHE)); 01003 #endif /* SDIO_STA_STBITERR */ 01004 01005 if(hsd->SdCard.CardType != CARD_SDHC_SDXC) 01006 { 01007 BlockAdd *= 512U; 01008 } 01009 01010 /* Set Block Size for Card */ 01011 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE); 01012 if(errorstate != HAL_SD_ERROR_NONE) 01013 { 01014 /* Clear all the static flags */ 01015 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 01016 hsd->ErrorCode |= errorstate; 01017 hsd->State = HAL_SD_STATE_READY; 01018 return HAL_ERROR; 01019 } 01020 01021 /* Write Blocks in Polling mode */ 01022 if(NumberOfBlocks > 1U) 01023 { 01024 hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK| SD_CONTEXT_IT); 01025 01026 /* Write Multi Block command */ 01027 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, BlockAdd); 01028 } 01029 else 01030 { 01031 hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_IT); 01032 01033 /* Write Single Block command */ 01034 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, BlockAdd); 01035 } 01036 if(errorstate != HAL_SD_ERROR_NONE) 01037 { 01038 /* Clear all the static flags */ 01039 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 01040 hsd->ErrorCode |= errorstate; 01041 hsd->State = HAL_SD_STATE_READY; 01042 return HAL_ERROR; 01043 } 01044 01045 /* Configure the SD DPSM (Data Path State Machine) */ 01046 config.DataTimeOut = SDMMC_DATATIMEOUT; 01047 config.DataLength = BLOCKSIZE * NumberOfBlocks; 01048 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B; 01049 config.TransferDir = SDIO_TRANSFER_DIR_TO_CARD; 01050 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK; 01051 config.DPSM = SDIO_DPSM_ENABLE; 01052 SDIO_ConfigData(hsd->Instance, &config); 01053 01054 return HAL_OK; 01055 } 01056 else 01057 { 01058 return HAL_BUSY; 01059 } 01060 } 01061 01062 /** 01063 * @brief Reads block(s) from a specified address in a card. The Data transfer 01064 * is managed by DMA mode. 01065 * @note This API should be followed by a check on the card state through 01066 * HAL_SD_GetCardState(). 01067 * @note You could also check the DMA transfer process through the SD Rx 01068 * interrupt event. 01069 * @param hsd Pointer SD handle 01070 * @param pData Pointer to the buffer that will contain the received data 01071 * @param BlockAdd Block Address from where data is to be read 01072 * @param NumberOfBlocks Number of blocks to read. 01073 * @retval HAL status 01074 */ 01075 HAL_StatusTypeDef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks) 01076 { 01077 SDIO_DataInitTypeDef config; 01078 uint32_t errorstate = HAL_SD_ERROR_NONE; 01079 01080 if(NULL == pData) 01081 { 01082 hsd->ErrorCode |= HAL_SD_ERROR_PARAM; 01083 return HAL_ERROR; 01084 } 01085 01086 if(hsd->State == HAL_SD_STATE_READY) 01087 { 01088 hsd->ErrorCode = HAL_DMA_ERROR_NONE; 01089 01090 if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr)) 01091 { 01092 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE; 01093 return HAL_ERROR; 01094 } 01095 01096 hsd->State = HAL_SD_STATE_BUSY; 01097 01098 /* Initialize data control register */ 01099 hsd->Instance->DCTRL = 0U; 01100 01101 #ifdef SDIO_STA_STBITER 01102 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND | SDIO_IT_STBITERR)); 01103 #else /* SDIO_STA_STBITERR not defined */ 01104 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND)); 01105 #endif /* SDIO_STA_STBITERR */ 01106 01107 /* Set the DMA transfer complete callback */ 01108 hsd->hdmarx->XferCpltCallback = SD_DMAReceiveCplt; 01109 01110 /* Set the DMA error callback */ 01111 hsd->hdmarx->XferErrorCallback = SD_DMAError; 01112 01113 /* Set the DMA Abort callback */ 01114 hsd->hdmarx->XferAbortCallback = NULL; 01115 01116 /* Enable the DMA Channel */ 01117 HAL_DMA_Start_IT(hsd->hdmarx, (uint32_t)&hsd->Instance->FIFO, (uint32_t)pData, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4); 01118 01119 /* Enable SD DMA transfer */ 01120 __HAL_SD_DMA_ENABLE(hsd); 01121 01122 if(hsd->SdCard.CardType != CARD_SDHC_SDXC) 01123 { 01124 BlockAdd *= 512U; 01125 } 01126 01127 /* Configure the SD DPSM (Data Path State Machine) */ 01128 config.DataTimeOut = SDMMC_DATATIMEOUT; 01129 config.DataLength = BLOCKSIZE * NumberOfBlocks; 01130 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B; 01131 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO; 01132 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK; 01133 config.DPSM = SDIO_DPSM_ENABLE; 01134 SDIO_ConfigData(hsd->Instance, &config); 01135 01136 /* Set Block Size for Card */ 01137 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE); 01138 if(errorstate != HAL_SD_ERROR_NONE) 01139 { 01140 /* Clear all the static flags */ 01141 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 01142 hsd->ErrorCode |= errorstate; 01143 hsd->State = HAL_SD_STATE_READY; 01144 return HAL_ERROR; 01145 } 01146 01147 /* Read Blocks in DMA mode */ 01148 if(NumberOfBlocks > 1U) 01149 { 01150 hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA); 01151 01152 /* Read Multi Block command */ 01153 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, BlockAdd); 01154 } 01155 else 01156 { 01157 hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_DMA); 01158 01159 /* Read Single Block command */ 01160 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, BlockAdd); 01161 } 01162 if(errorstate != HAL_SD_ERROR_NONE) 01163 { 01164 /* Clear all the static flags */ 01165 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 01166 hsd->ErrorCode |= errorstate; 01167 hsd->State = HAL_SD_STATE_READY; 01168 return HAL_ERROR; 01169 } 01170 01171 return HAL_OK; 01172 } 01173 else 01174 { 01175 return HAL_BUSY; 01176 } 01177 } 01178 01179 /** 01180 * @brief Writes block(s) to a specified address in a card. The Data transfer 01181 * is managed by DMA mode. 01182 * @note This API should be followed by a check on the card state through 01183 * HAL_SD_GetCardState(). 01184 * @note You could also check the DMA transfer process through the SD Tx 01185 * interrupt event. 01186 * @param hsd Pointer to SD handle 01187 * @param pData Pointer to the buffer that will contain the data to transmit 01188 * @param BlockAdd Block Address where data will be written 01189 * @param NumberOfBlocks Number of blocks to write 01190 * @retval HAL status 01191 */ 01192 HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks) 01193 { 01194 SDIO_DataInitTypeDef config; 01195 uint32_t errorstate = HAL_SD_ERROR_NONE; 01196 01197 if(NULL == pData) 01198 { 01199 hsd->ErrorCode |= HAL_SD_ERROR_PARAM; 01200 return HAL_ERROR; 01201 } 01202 01203 if(hsd->State == HAL_SD_STATE_READY) 01204 { 01205 hsd->ErrorCode = HAL_DMA_ERROR_NONE; 01206 01207 if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr)) 01208 { 01209 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE; 01210 return HAL_ERROR; 01211 } 01212 01213 hsd->State = HAL_SD_STATE_BUSY; 01214 01215 /* Initialize data control register */ 01216 hsd->Instance->DCTRL = 0U; 01217 01218 /* Enable SD Error interrupts */ 01219 #ifdef SDIO_STA_STBITER 01220 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_STBITERR)); 01221 #else /* SDIO_STA_STBITERR not defined */ 01222 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR)); 01223 #endif /* SDIO_STA_STBITERR */ 01224 01225 /* Set the DMA transfer complete callback */ 01226 hsd->hdmatx->XferCpltCallback = SD_DMATransmitCplt; 01227 01228 /* Set the DMA error callback */ 01229 hsd->hdmatx->XferErrorCallback = SD_DMAError; 01230 01231 /* Set the DMA Abort callback */ 01232 hsd->hdmatx->XferAbortCallback = NULL; 01233 01234 if(hsd->SdCard.CardType != CARD_SDHC_SDXC) 01235 { 01236 BlockAdd *= 512U; 01237 } 01238 01239 /* Set Block Size for Card */ 01240 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE); 01241 if(errorstate != HAL_SD_ERROR_NONE) 01242 { 01243 /* Clear all the static flags */ 01244 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 01245 hsd->ErrorCode |= errorstate; 01246 hsd->State = HAL_SD_STATE_READY; 01247 return HAL_ERROR; 01248 } 01249 01250 /* Write Blocks in Polling mode */ 01251 if(NumberOfBlocks > 1U) 01252 { 01253 hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK | SD_CONTEXT_DMA); 01254 01255 /* Write Multi Block command */ 01256 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, BlockAdd); 01257 } 01258 else 01259 { 01260 hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_DMA); 01261 01262 /* Write Single Block command */ 01263 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, BlockAdd); 01264 } 01265 if(errorstate != HAL_SD_ERROR_NONE) 01266 { 01267 /* Clear all the static flags */ 01268 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 01269 hsd->ErrorCode |= errorstate; 01270 hsd->State = HAL_SD_STATE_READY; 01271 return HAL_ERROR; 01272 } 01273 01274 /* Enable SDIO DMA transfer */ 01275 __HAL_SD_DMA_ENABLE(hsd); 01276 01277 /* Enable the DMA Channel */ 01278 HAL_DMA_Start_IT(hsd->hdmatx, (uint32_t)pData, (uint32_t)&hsd->Instance->FIFO, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4); 01279 01280 /* Configure the SD DPSM (Data Path State Machine) */ 01281 config.DataTimeOut = SDMMC_DATATIMEOUT; 01282 config.DataLength = BLOCKSIZE * NumberOfBlocks; 01283 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B; 01284 config.TransferDir = SDIO_TRANSFER_DIR_TO_CARD; 01285 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK; 01286 config.DPSM = SDIO_DPSM_ENABLE; 01287 SDIO_ConfigData(hsd->Instance, &config); 01288 01289 return HAL_OK; 01290 } 01291 else 01292 { 01293 return HAL_BUSY; 01294 } 01295 } 01296 01297 /** 01298 * @brief Erases the specified memory area of the given SD card. 01299 * @note This API should be followed by a check on the card state through 01300 * HAL_SD_GetCardState(). 01301 * @param hsd Pointer to SD handle 01302 * @param BlockStartAdd Start Block address 01303 * @param BlockEndAdd End Block address 01304 * @retval HAL status 01305 */ 01306 HAL_StatusTypeDef HAL_SD_Erase(SD_HandleTypeDef *hsd, uint32_t BlockStartAdd, uint32_t BlockEndAdd) 01307 { 01308 uint32_t errorstate = HAL_SD_ERROR_NONE; 01309 01310 if(hsd->State == HAL_SD_STATE_READY) 01311 { 01312 hsd->ErrorCode = HAL_DMA_ERROR_NONE; 01313 01314 if(BlockEndAdd < BlockStartAdd) 01315 { 01316 hsd->ErrorCode |= HAL_SD_ERROR_PARAM; 01317 return HAL_ERROR; 01318 } 01319 01320 if(BlockEndAdd > (hsd->SdCard.LogBlockNbr)) 01321 { 01322 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE; 01323 return HAL_ERROR; 01324 } 01325 01326 hsd->State = HAL_SD_STATE_BUSY; 01327 01328 /* Check if the card command class supports erase command */ 01329 if(((hsd->SdCard.Class) & SDIO_CCCC_ERASE) == 0U) 01330 { 01331 /* Clear all the static flags */ 01332 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 01333 hsd->ErrorCode |= HAL_SD_ERROR_REQUEST_NOT_APPLICABLE; 01334 hsd->State = HAL_SD_STATE_READY; 01335 return HAL_ERROR; 01336 } 01337 01338 if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED) 01339 { 01340 /* Clear all the static flags */ 01341 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 01342 hsd->ErrorCode |= HAL_SD_ERROR_LOCK_UNLOCK_FAILED; 01343 hsd->State = HAL_SD_STATE_READY; 01344 return HAL_ERROR; 01345 } 01346 01347 /* Get start and end block for high capacity cards */ 01348 if(hsd->SdCard.CardType != CARD_SDHC_SDXC) 01349 { 01350 BlockStartAdd *= 512U; 01351 BlockEndAdd *= 512U; 01352 } 01353 01354 /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */ 01355 if(hsd->SdCard.CardType != CARD_SECURED) 01356 { 01357 /* Send CMD32 SD_ERASE_GRP_START with argument as addr */ 01358 errorstate = SDMMC_CmdSDEraseStartAdd(hsd->Instance, BlockStartAdd); 01359 if(errorstate != HAL_SD_ERROR_NONE) 01360 { 01361 /* Clear all the static flags */ 01362 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 01363 hsd->ErrorCode |= errorstate; 01364 hsd->State = HAL_SD_STATE_READY; 01365 return HAL_ERROR; 01366 } 01367 01368 /* Send CMD33 SD_ERASE_GRP_END with argument as addr */ 01369 errorstate = SDMMC_CmdSDEraseEndAdd(hsd->Instance, BlockEndAdd); 01370 if(errorstate != HAL_SD_ERROR_NONE) 01371 { 01372 /* Clear all the static flags */ 01373 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 01374 hsd->ErrorCode |= errorstate; 01375 hsd->State = HAL_SD_STATE_READY; 01376 return HAL_ERROR; 01377 } 01378 } 01379 01380 /* Send CMD38 ERASE */ 01381 errorstate = SDMMC_CmdErase(hsd->Instance); 01382 if(errorstate != HAL_SD_ERROR_NONE) 01383 { 01384 /* Clear all the static flags */ 01385 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 01386 hsd->ErrorCode |= errorstate; 01387 hsd->State = HAL_SD_STATE_READY; 01388 return HAL_ERROR; 01389 } 01390 01391 hsd->State = HAL_SD_STATE_READY; 01392 01393 return HAL_OK; 01394 } 01395 else 01396 { 01397 return HAL_BUSY; 01398 } 01399 } 01400 01401 /** 01402 * @brief This function handles SD card interrupt request. 01403 * @param hsd Pointer to SD handle 01404 * @retval None 01405 */ 01406 void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd) 01407 { 01408 uint32_t errorstate = HAL_SD_ERROR_NONE; 01409 01410 /* Check for SDIO interrupt flags */ 01411 if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_DATAEND) != RESET) 01412 { 01413 __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_DATAEND); 01414 01415 #ifdef SDIO_STA_STBITERR 01416 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\ 01417 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR | SDIO_IT_STBITERR); 01418 #else /* SDIO_STA_STBITERR not defined */ 01419 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\ 01420 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR); 01421 #endif 01422 01423 if((hsd->Context & SD_CONTEXT_IT) != RESET) 01424 { 01425 if(((hsd->Context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != RESET) || ((hsd->Context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != RESET)) 01426 { 01427 errorstate = SDMMC_CmdStopTransfer(hsd->Instance); 01428 if(errorstate != HAL_SD_ERROR_NONE) 01429 { 01430 hsd->ErrorCode |= errorstate; 01431 HAL_SD_ErrorCallback(hsd); 01432 } 01433 } 01434 01435 /* Clear all the static flags */ 01436 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 01437 01438 hsd->State = HAL_SD_STATE_READY; 01439 if(((hsd->Context & SD_CONTEXT_READ_SINGLE_BLOCK) != RESET) || ((hsd->Context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != RESET)) 01440 { 01441 HAL_SD_RxCpltCallback(hsd); 01442 } 01443 else 01444 { 01445 HAL_SD_TxCpltCallback(hsd); 01446 } 01447 } 01448 else if((hsd->Context & SD_CONTEXT_DMA) != RESET) 01449 { 01450 if((hsd->Context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != RESET) 01451 { 01452 errorstate = SDMMC_CmdStopTransfer(hsd->Instance); 01453 if(errorstate != HAL_SD_ERROR_NONE) 01454 { 01455 hsd->ErrorCode |= errorstate; 01456 HAL_SD_ErrorCallback(hsd); 01457 } 01458 } 01459 if(((hsd->Context & SD_CONTEXT_READ_SINGLE_BLOCK) == RESET) && ((hsd->Context & SD_CONTEXT_READ_MULTIPLE_BLOCK) == RESET)) 01460 { 01461 /* Disable the DMA transfer for transmit request by setting the DMAEN bit 01462 in the SD DCTRL register */ 01463 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN); 01464 01465 hsd->State = HAL_SD_STATE_READY; 01466 01467 HAL_SD_TxCpltCallback(hsd); 01468 } 01469 } 01470 } 01471 01472 else if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_TXFIFOHE) != RESET) 01473 { 01474 __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_TXFIFOHE); 01475 01476 SD_Write_IT(hsd); 01477 } 01478 01479 else if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_RXFIFOHF) != RESET) 01480 { 01481 __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_RXFIFOHF); 01482 01483 SD_Read_IT(hsd); 01484 } 01485 01486 #ifdef SDIO_STA_STBITERR 01487 else if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_TXUNDERR | SDIO_IT_STBITERR) != RESET) 01488 { 01489 /* Set Error code */ 01490 if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_DCRCFAIL) != RESET) 01491 { 01492 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL; 01493 } 01494 if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_DTIMEOUT) != RESET) 01495 { 01496 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT; 01497 } 01498 if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_RXOVERR) != RESET) 01499 { 01500 hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN; 01501 } 01502 if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_TXUNDERR) != RESET) 01503 { 01504 hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN; 01505 } 01506 if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_STBITERR) != RESET) 01507 { 01508 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT; 01509 } 01510 01511 /* Clear All flags */ 01512 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS | SDIO_FLAG_STBITERR); 01513 01514 /* Disable all interrupts */ 01515 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\ 01516 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR |SDIO_IT_STBITERR); 01517 01518 if((hsd->Context & SD_CONTEXT_DMA) != RESET) 01519 { 01520 /* Abort the SD DMA Streams */ 01521 if(hsd->hdmatx != NULL) 01522 { 01523 /* Set the DMA Tx abort callback */ 01524 hsd->hdmatx->XferAbortCallback = SD_DMATxAbort; 01525 /* Abort DMA in IT mode */ 01526 if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK) 01527 { 01528 SD_DMATxAbort(hsd->hdmatx); 01529 } 01530 } 01531 else if(hsd->hdmarx != NULL) 01532 { 01533 /* Set the DMA Rx abort callback */ 01534 hsd->hdmarx->XferAbortCallback = SD_DMARxAbort; 01535 /* Abort DMA in IT mode */ 01536 if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK) 01537 { 01538 SD_DMARxAbort(hsd->hdmarx); 01539 } 01540 } 01541 else 01542 { 01543 hsd->ErrorCode = HAL_SD_ERROR_NONE; 01544 hsd->State = HAL_SD_STATE_READY; 01545 HAL_SD_AbortCallback(hsd); 01546 } 01547 } 01548 else if((hsd->Context & SD_CONTEXT_IT) != RESET) 01549 { 01550 /* Set the SD state to ready to be able to start again the process */ 01551 hsd->State = HAL_SD_STATE_READY; 01552 HAL_SD_ErrorCallback(hsd); 01553 } 01554 } 01555 #else /* SDIO_STA_STBITERR not defined */ 01556 else if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_TXUNDERR) != RESET) 01557 { 01558 /* Set Error code */ 01559 if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_DCRCFAIL) != RESET) 01560 { 01561 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL; 01562 } 01563 if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_DTIMEOUT) != RESET) 01564 { 01565 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT; 01566 } 01567 if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_RXOVERR) != RESET) 01568 { 01569 hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN; 01570 } 01571 if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_TXUNDERR) != RESET) 01572 { 01573 hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN; 01574 } 01575 01576 /* Clear All flags */ 01577 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 01578 01579 /* Disable all interrupts */ 01580 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\ 01581 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR); 01582 01583 if((hsd->Context & SD_CONTEXT_DMA) != RESET) 01584 { 01585 /* Abort the SD DMA Streams */ 01586 if(hsd->hdmatx != NULL) 01587 { 01588 /* Set the DMA Tx abort callback */ 01589 hsd->hdmatx->XferAbortCallback = SD_DMATxAbort; 01590 /* Abort DMA in IT mode */ 01591 if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK) 01592 { 01593 SD_DMATxAbort(hsd->hdmatx); 01594 } 01595 } 01596 else if(hsd->hdmarx != NULL) 01597 { 01598 /* Set the DMA Rx abort callback */ 01599 hsd->hdmarx->XferAbortCallback = SD_DMARxAbort; 01600 /* Abort DMA in IT mode */ 01601 if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK) 01602 { 01603 SD_DMARxAbort(hsd->hdmarx); 01604 } 01605 } 01606 else 01607 { 01608 hsd->ErrorCode = HAL_SD_ERROR_NONE; 01609 hsd->State = HAL_SD_STATE_READY; 01610 HAL_SD_AbortCallback(hsd); 01611 } 01612 } 01613 else if((hsd->Context & SD_CONTEXT_IT) != RESET) 01614 { 01615 /* Set the SD state to ready to be able to start again the process */ 01616 hsd->State = HAL_SD_STATE_READY; 01617 HAL_SD_ErrorCallback(hsd); 01618 } 01619 } 01620 #endif 01621 } 01622 01623 /** 01624 * @brief return the SD state 01625 * @param hsd Pointer to sd handle 01626 * @retval HAL state 01627 */ 01628 HAL_SD_StateTypeDef HAL_SD_GetState(SD_HandleTypeDef *hsd) 01629 { 01630 return hsd->State; 01631 } 01632 01633 /** 01634 * @brief Return the SD error code 01635 * @param hsd Pointer to a SD_HandleTypeDef structure that contains 01636 * the configuration information. 01637 * @retval SD Error Code 01638 */ 01639 uint32_t HAL_SD_GetError(SD_HandleTypeDef *hsd) 01640 { 01641 return hsd->ErrorCode; 01642 } 01643 01644 /** 01645 * @brief Tx Transfer completed callbacks 01646 * @param hsd Pointer to SD handle 01647 * @retval None 01648 */ 01649 __weak void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd) 01650 { 01651 /* Prevent unused argument(s) compilation warning */ 01652 UNUSED(hsd); 01653 01654 /* NOTE : This function should not be modified, when the callback is needed, 01655 the HAL_SD_TxCpltCallback can be implemented in the user file 01656 */ 01657 } 01658 01659 /** 01660 * @brief Rx Transfer completed callbacks 01661 * @param hsd Pointer SD handle 01662 * @retval None 01663 */ 01664 __weak void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd) 01665 { 01666 /* Prevent unused argument(s) compilation warning */ 01667 UNUSED(hsd); 01668 01669 /* NOTE : This function should not be modified, when the callback is needed, 01670 the HAL_SD_RxCpltCallback can be implemented in the user file 01671 */ 01672 } 01673 01674 /** 01675 * @brief SD error callbacks 01676 * @param hsd Pointer SD handle 01677 * @retval None 01678 */ 01679 __weak void HAL_SD_ErrorCallback(SD_HandleTypeDef *hsd) 01680 { 01681 /* Prevent unused argument(s) compilation warning */ 01682 UNUSED(hsd); 01683 01684 /* NOTE : This function should not be modified, when the callback is needed, 01685 the HAL_SD_ErrorCallback can be implemented in the user file 01686 */ 01687 } 01688 01689 /** 01690 * @brief SD Abort callbacks 01691 * @param hsd Pointer SD handle 01692 * @retval None 01693 */ 01694 __weak void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd) 01695 { 01696 /* Prevent unused argument(s) compilation warning */ 01697 UNUSED(hsd); 01698 01699 /* NOTE : This function should not be modified, when the callback is needed, 01700 the HAL_SD_ErrorCallback can be implemented in the user file 01701 */ 01702 } 01703 01704 01705 /** 01706 * @} 01707 */ 01708 01709 /** @addtogroup SD_Exported_Functions_Group3 01710 * @brief management functions 01711 * 01712 @verbatim 01713 ============================================================================== 01714 ##### Peripheral Control functions ##### 01715 ============================================================================== 01716 [..] 01717 This subsection provides a set of functions allowing to control the SD card 01718 operations and get the related information 01719 01720 @endverbatim 01721 * @{ 01722 */ 01723 01724 /** 01725 * @brief Returns information the information of the card which are stored on 01726 * the CID register. 01727 * @param hsd Pointer to SD handle 01728 * @param pCID Pointer to a HAL_SD_CIDTypeDef structure that 01729 * contains all CID register parameters 01730 * @retval HAL status 01731 */ 01732 HAL_StatusTypeDef HAL_SD_GetCardCID(SD_HandleTypeDef *hsd, HAL_SD_CardCIDTypeDef *pCID) 01733 { 01734 uint32_t tmp = 0U; 01735 01736 /* Byte 0 */ 01737 tmp = (uint8_t)((hsd->CID[0U] & 0xFF000000U) >> 24U); 01738 pCID->ManufacturerID = tmp; 01739 01740 /* Byte 1 */ 01741 tmp = (uint8_t)((hsd->CID[0U] & 0x00FF0000U) >> 16U); 01742 pCID->OEM_AppliID = tmp << 8U; 01743 01744 /* Byte 2 */ 01745 tmp = (uint8_t)((hsd->CID[0U] & 0x000000FF00U) >> 8U); 01746 pCID->OEM_AppliID |= tmp; 01747 01748 /* Byte 3 */ 01749 tmp = (uint8_t)(hsd->CID[0U] & 0x000000FFU); 01750 pCID->ProdName1 = tmp << 24U; 01751 01752 /* Byte 4 */ 01753 tmp = (uint8_t)((hsd->CID[1U] & 0xFF000000U) >> 24U); 01754 pCID->ProdName1 |= tmp << 16; 01755 01756 /* Byte 5 */ 01757 tmp = (uint8_t)((hsd->CID[1U] & 0x00FF0000U) >> 16U); 01758 pCID->ProdName1 |= tmp << 8U; 01759 01760 /* Byte 6 */ 01761 tmp = (uint8_t)((hsd->CID[1U] & 0x0000FF00U) >> 8U); 01762 pCID->ProdName1 |= tmp; 01763 01764 /* Byte 7 */ 01765 tmp = (uint8_t)(hsd->CID[1U] & 0x000000FFU); 01766 pCID->ProdName2 = tmp; 01767 01768 /* Byte 8 */ 01769 tmp = (uint8_t)((hsd->CID[2U] & 0xFF000000U) >> 24U); 01770 pCID->ProdRev = tmp; 01771 01772 /* Byte 9 */ 01773 tmp = (uint8_t)((hsd->CID[2U] & 0x00FF0000U) >> 16U); 01774 pCID->ProdSN = tmp << 24U; 01775 01776 /* Byte 10 */ 01777 tmp = (uint8_t)((hsd->CID[2U] & 0x0000FF00U) >> 8U); 01778 pCID->ProdSN |= tmp << 16U; 01779 01780 /* Byte 11 */ 01781 tmp = (uint8_t)(hsd->CID[2U] & 0x000000FFU); 01782 pCID->ProdSN |= tmp << 8U; 01783 01784 /* Byte 12 */ 01785 tmp = (uint8_t)((hsd->CID[3U] & 0xFF000000U) >> 24U); 01786 pCID->ProdSN |= tmp; 01787 01788 /* Byte 13 */ 01789 tmp = (uint8_t)((hsd->CID[3U] & 0x00FF0000U) >> 16U); 01790 pCID->Reserved1 |= (tmp & 0xF0U) >> 4U; 01791 pCID->ManufactDate = (tmp & 0x0FU) << 8U; 01792 01793 /* Byte 14 */ 01794 tmp = (uint8_t)((hsd->CID[3U] & 0x0000FF00U) >> 8U); 01795 pCID->ManufactDate |= tmp; 01796 01797 /* Byte 15 */ 01798 tmp = (uint8_t)(hsd->CID[3U] & 0x000000FFU); 01799 pCID->CID_CRC = (tmp & 0xFEU) >> 1U; 01800 pCID->Reserved2 = 1U; 01801 01802 return HAL_OK; 01803 } 01804 01805 /** 01806 * @brief Returns information the information of the card which are stored on 01807 * the CSD register. 01808 * @param hsd Pointer to SD handle 01809 * @param pCSD Pointer to a HAL_SD_CardCSDTypeDef structure that 01810 * contains all CSD register parameters 01811 * @retval HAL status 01812 */ 01813 HAL_StatusTypeDef HAL_SD_GetCardCSD(SD_HandleTypeDef *hsd, HAL_SD_CardCSDTypeDef *pCSD) 01814 { 01815 uint32_t tmp = 0U; 01816 01817 /* Byte 0 */ 01818 tmp = (hsd->CSD[0U] & 0xFF000000U) >> 24U; 01819 pCSD->CSDStruct = (uint8_t)((tmp & 0xC0U) >> 6U); 01820 pCSD->SysSpecVersion = (uint8_t)((tmp & 0x3CU) >> 2U); 01821 pCSD->Reserved1 = tmp & 0x03U; 01822 01823 /* Byte 1 */ 01824 tmp = (hsd->CSD[0U] & 0x00FF0000U) >> 16U; 01825 pCSD->TAAC = (uint8_t)tmp; 01826 01827 /* Byte 2 */ 01828 tmp = (hsd->CSD[0U] & 0x0000FF00U) >> 8U; 01829 pCSD->NSAC = (uint8_t)tmp; 01830 01831 /* Byte 3 */ 01832 tmp = hsd->CSD[0U] & 0x000000FFU; 01833 pCSD->MaxBusClkFrec = (uint8_t)tmp; 01834 01835 /* Byte 4 */ 01836 tmp = (hsd->CSD[1U] & 0xFF000000U) >> 24U; 01837 pCSD->CardComdClasses = (uint16_t)(tmp << 4U); 01838 01839 /* Byte 5 */ 01840 tmp = (hsd->CSD[1U] & 0x00FF0000U) >> 16U; 01841 pCSD->CardComdClasses |= (uint16_t)((tmp & 0xF0U) >> 4U); 01842 pCSD->RdBlockLen = (uint8_t)(tmp & 0x0FU); 01843 01844 /* Byte 6 */ 01845 tmp = (hsd->CSD[1U] & 0x0000FF00U) >> 8U; 01846 pCSD->PartBlockRead = (uint8_t)((tmp & 0x80U) >> 7U); 01847 pCSD->WrBlockMisalign = (uint8_t)((tmp & 0x40U) >> 6U); 01848 pCSD->RdBlockMisalign = (uint8_t)((tmp & 0x20U) >> 5U); 01849 pCSD->DSRImpl = (uint8_t)((tmp & 0x10U) >> 4U); 01850 pCSD->Reserved2 = 0U; /*!< Reserved */ 01851 01852 if(hsd->SdCard.CardType == CARD_SDSC) 01853 { 01854 pCSD->DeviceSize = (tmp & 0x03U) << 10U; 01855 01856 /* Byte 7 */ 01857 tmp = (uint8_t)(hsd->CSD[1U] & 0x000000FFU); 01858 pCSD->DeviceSize |= (tmp) << 2U; 01859 01860 /* Byte 8 */ 01861 tmp = (uint8_t)((hsd->CSD[2U] & 0xFF000000U) >> 24U); 01862 pCSD->DeviceSize |= (tmp & 0xC0U) >> 6U; 01863 01864 pCSD->MaxRdCurrentVDDMin = (tmp & 0x38U) >> 3U; 01865 pCSD->MaxRdCurrentVDDMax = (tmp & 0x07U); 01866 01867 /* Byte 9 */ 01868 tmp = (uint8_t)((hsd->CSD[2U] & 0x00FF0000U) >> 16U); 01869 pCSD->MaxWrCurrentVDDMin = (tmp & 0xE0U) >> 5U; 01870 pCSD->MaxWrCurrentVDDMax = (tmp & 0x1CU) >> 2U; 01871 pCSD->DeviceSizeMul = (tmp & 0x03U) << 1U; 01872 /* Byte 10 */ 01873 tmp = (uint8_t)((hsd->CSD[2U] & 0x0000FF00U) >> 8U); 01874 pCSD->DeviceSizeMul |= (tmp & 0x80U) >> 7U; 01875 01876 hsd->SdCard.BlockNbr = (pCSD->DeviceSize + 1U) ; 01877 hsd->SdCard.BlockNbr *= (1U << (pCSD->DeviceSizeMul + 2U)); 01878 hsd->SdCard.BlockSize = 1U << (pCSD->RdBlockLen); 01879 01880 hsd->SdCard.LogBlockNbr = (hsd->SdCard.BlockNbr) * ((hsd->SdCard.BlockSize) / 512U); 01881 hsd->SdCard.LogBlockSize = 512U; 01882 } 01883 else if(hsd->SdCard.CardType == CARD_SDHC_SDXC) 01884 { 01885 /* Byte 7 */ 01886 tmp = (uint8_t)(hsd->CSD[1U] & 0x000000FFU); 01887 pCSD->DeviceSize = (tmp & 0x3FU) << 16U; 01888 01889 /* Byte 8 */ 01890 tmp = (uint8_t)((hsd->CSD[2U] & 0xFF000000U) >> 24U); 01891 01892 pCSD->DeviceSize |= (tmp << 8U); 01893 01894 /* Byte 9 */ 01895 tmp = (uint8_t)((hsd->CSD[2U] & 0x00FF0000U) >> 16U); 01896 01897 pCSD->DeviceSize |= (tmp); 01898 01899 /* Byte 10 */ 01900 tmp = (uint8_t)((hsd->CSD[2U] & 0x0000FF00U) >> 8U); 01901 01902 hsd->SdCard.LogBlockNbr = hsd->SdCard.BlockNbr = (((uint64_t)pCSD->DeviceSize + 1U) * 1024U); 01903 hsd->SdCard.LogBlockSize = hsd->SdCard.BlockSize = 512U; 01904 } 01905 else 01906 { 01907 /* Clear all the static flags */ 01908 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 01909 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE; 01910 hsd->State = HAL_SD_STATE_READY; 01911 return HAL_ERROR; 01912 } 01913 01914 pCSD->EraseGrSize = (tmp & 0x40U) >> 6U; 01915 pCSD->EraseGrMul = (tmp & 0x3FU) << 1U; 01916 01917 /* Byte 11 */ 01918 tmp = (uint8_t)(hsd->CSD[2U] & 0x000000FFU); 01919 pCSD->EraseGrMul |= (tmp & 0x80U) >> 7U; 01920 pCSD->WrProtectGrSize = (tmp & 0x7FU); 01921 01922 /* Byte 12 */ 01923 tmp = (uint8_t)((hsd->CSD[3U] & 0xFF000000U) >> 24U); 01924 pCSD->WrProtectGrEnable = (tmp & 0x80U) >> 7U; 01925 pCSD->ManDeflECC = (tmp & 0x60U) >> 5U; 01926 pCSD->WrSpeedFact = (tmp & 0x1CU) >> 2U; 01927 pCSD->MaxWrBlockLen = (tmp & 0x03U) << 2U; 01928 01929 /* Byte 13 */ 01930 tmp = (uint8_t)((hsd->CSD[3U] & 0x00FF0000U) >> 16U); 01931 pCSD->MaxWrBlockLen |= (tmp & 0xC0U) >> 6U; 01932 pCSD->WriteBlockPaPartial = (tmp & 0x20U) >> 5U; 01933 pCSD->Reserved3 = 0U; 01934 pCSD->ContentProtectAppli = (tmp & 0x01U); 01935 01936 /* Byte 14 */ 01937 tmp = (uint8_t)((hsd->CSD[3U] & 0x0000FF00U) >> 8U); 01938 pCSD->FileFormatGrouop = (tmp & 0x80U) >> 7U; 01939 pCSD->CopyFlag = (tmp & 0x40U) >> 6U; 01940 pCSD->PermWrProtect = (tmp & 0x20U) >> 5U; 01941 pCSD->TempWrProtect = (tmp & 0x10U) >> 4U; 01942 pCSD->FileFormat = (tmp & 0x0CU) >> 2U; 01943 pCSD->ECC = (tmp & 0x03U); 01944 01945 /* Byte 15 */ 01946 tmp = (uint8_t)(hsd->CSD[3U] & 0x000000FFU); 01947 pCSD->CSD_CRC = (tmp & 0xFEU) >> 1U; 01948 pCSD->Reserved4 = 1U; 01949 01950 return HAL_OK; 01951 } 01952 01953 /** 01954 * @brief Gets the SD status info. 01955 * @param hsd Pointer to SD handle 01956 * @param pStatus Pointer to the HAL_SD_CardStatusTypeDef structure that 01957 * will contain the SD card status information 01958 * @retval HAL status 01959 */ 01960 HAL_StatusTypeDef HAL_SD_GetCardStatus(SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypeDef *pStatus) 01961 { 01962 uint32_t tmp = 0U; 01963 uint32_t sd_status[16U]; 01964 uint32_t errorstate = HAL_SD_ERROR_NONE; 01965 01966 errorstate = SD_SendSDStatus(hsd, sd_status); 01967 if(errorstate != HAL_OK) 01968 { 01969 /* Clear all the static flags */ 01970 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 01971 hsd->ErrorCode |= errorstate; 01972 hsd->State = HAL_SD_STATE_READY; 01973 return HAL_ERROR; 01974 } 01975 else 01976 { 01977 /* Byte 0 */ 01978 tmp = (sd_status[0U] & 0xC0U) >> 6U; 01979 pStatus->DataBusWidth = (uint8_t)tmp; 01980 01981 /* Byte 0 */ 01982 tmp = (sd_status[0U] & 0x20U) >> 5U; 01983 pStatus->SecuredMode = (uint8_t)tmp; 01984 01985 /* Byte 2 */ 01986 tmp = (sd_status[0U] & 0x00FF0000U) >> 16U; 01987 pStatus->CardType = (uint16_t)(tmp << 8U); 01988 01989 /* Byte 3 */ 01990 tmp = (sd_status[0U] & 0xFF000000U) >> 24U; 01991 pStatus->CardType |= (uint16_t)tmp; 01992 01993 /* Byte 4 */ 01994 tmp = (sd_status[1U] & 0xFFU); 01995 pStatus->ProtectedAreaSize = (uint32_t)(tmp << 24U); 01996 01997 /* Byte 5 */ 01998 tmp = (sd_status[1U] & 0xFF00U) >> 8U; 01999 pStatus->ProtectedAreaSize |= (uint32_t)(tmp << 16U); 02000 02001 /* Byte 6 */ 02002 tmp = (sd_status[1U] & 0xFF0000U) >> 16U; 02003 pStatus->ProtectedAreaSize |= (uint32_t)(tmp << 8U); 02004 02005 /* Byte 7 */ 02006 tmp = (sd_status[1U] & 0xFF000000U) >> 24U; 02007 pStatus->ProtectedAreaSize |= (uint32_t)tmp; 02008 02009 /* Byte 8 */ 02010 tmp = (sd_status[2U] & 0xFFU); 02011 pStatus->SpeedClass = (uint8_t)tmp; 02012 02013 /* Byte 9 */ 02014 tmp = (sd_status[2U] & 0xFF00U) >> 8U; 02015 pStatus->PerformanceMove = (uint8_t)tmp; 02016 02017 /* Byte 10 */ 02018 tmp = (sd_status[2U] & 0xF00000U) >> 20U; 02019 pStatus->AllocationUnitSize = (uint8_t)tmp; 02020 02021 /* Byte 11 */ 02022 tmp = (sd_status[2U] & 0xFF000000U) >> 24U; 02023 pStatus->EraseSize = (uint16_t)(tmp << 8U); 02024 02025 /* Byte 12 */ 02026 tmp = (sd_status[3U] & 0xFFU); 02027 pStatus->EraseSize |= (uint16_t)tmp; 02028 02029 /* Byte 13 */ 02030 tmp = (sd_status[3U] & 0xFC00U) >> 10U; 02031 pStatus->EraseTimeout = (uint8_t)tmp; 02032 02033 /* Byte 13 */ 02034 tmp = (sd_status[3U] & 0x0300U) >> 8U; 02035 pStatus->EraseOffset = (uint8_t)tmp; 02036 } 02037 02038 return HAL_OK; 02039 } 02040 02041 /** 02042 * @brief Gets the SD card info. 02043 * @param hsd Pointer to SD handle 02044 * @param pCardInfo Pointer to the HAL_SD_CardInfoTypeDef structure that 02045 * will contain the SD card status information 02046 * @retval HAL status 02047 */ 02048 HAL_StatusTypeDef HAL_SD_GetCardInfo(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypeDef *pCardInfo) 02049 { 02050 pCardInfo->CardType = (uint32_t)(hsd->SdCard.CardType); 02051 pCardInfo->CardVersion = (uint32_t)(hsd->SdCard.CardVersion); 02052 pCardInfo->Class = (uint32_t)(hsd->SdCard.Class); 02053 pCardInfo->RelCardAdd = (uint32_t)(hsd->SdCard.RelCardAdd); 02054 pCardInfo->BlockNbr = (uint32_t)(hsd->SdCard.BlockNbr); 02055 pCardInfo->BlockSize = (uint32_t)(hsd->SdCard.BlockSize); 02056 pCardInfo->LogBlockNbr = (uint32_t)(hsd->SdCard.LogBlockNbr); 02057 pCardInfo->LogBlockSize = (uint32_t)(hsd->SdCard.LogBlockSize); 02058 02059 return HAL_OK; 02060 } 02061 02062 /** 02063 * @brief Enables wide bus operation for the requested card if supported by 02064 * card. 02065 * @param hsd Pointer to SD handle 02066 * @param WideMode Specifies the SD card wide bus mode 02067 * This parameter can be one of the following values: 02068 * @arg SDIO_BUS_WIDE_8B: 8-bit data transfer 02069 * @arg SDIO_BUS_WIDE_4B: 4-bit data transfer 02070 * @arg SDIO_BUS_WIDE_1B: 1-bit data transfer 02071 * @retval HAL status 02072 */ 02073 HAL_StatusTypeDef HAL_SD_ConfigWideBusOperation(SD_HandleTypeDef *hsd, uint32_t WideMode) 02074 { 02075 SDIO_InitTypeDef Init; 02076 uint32_t errorstate = HAL_SD_ERROR_NONE; 02077 02078 /* Check the parameters */ 02079 assert_param(IS_SDIO_BUS_WIDE(WideMode)); 02080 02081 /* Chnage Satte */ 02082 hsd->State = HAL_SD_STATE_BUSY; 02083 02084 if(hsd->SdCard.CardType != CARD_SECURED) 02085 { 02086 if(WideMode == SDIO_BUS_WIDE_8B) 02087 { 02088 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE; 02089 } 02090 else if(WideMode == SDIO_BUS_WIDE_4B) 02091 { 02092 errorstate = SD_WideBus_Enable(hsd); 02093 02094 hsd->ErrorCode |= errorstate; 02095 } 02096 else if(WideMode == SDIO_BUS_WIDE_1B) 02097 { 02098 errorstate = SD_WideBus_Disable(hsd); 02099 02100 hsd->ErrorCode |= errorstate; 02101 } 02102 else 02103 { 02104 /* WideMode is not a valid argument*/ 02105 hsd->ErrorCode |= HAL_SD_ERROR_PARAM; 02106 } 02107 } 02108 else 02109 { 02110 /* MMC Card does not support this feature */ 02111 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE; 02112 } 02113 02114 if(hsd->ErrorCode != HAL_SD_ERROR_NONE) 02115 { 02116 /* Clear all the static flags */ 02117 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 02118 hsd->State = HAL_SD_STATE_READY; 02119 return HAL_ERROR; 02120 } 02121 else 02122 { 02123 /* Configure the SDIO peripheral */ 02124 Init.ClockEdge = hsd->Init.ClockEdge; 02125 Init.ClockBypass = hsd->Init.ClockBypass; 02126 Init.ClockPowerSave = hsd->Init.ClockPowerSave; 02127 Init.BusWide = WideMode; 02128 Init.HardwareFlowControl = hsd->Init.HardwareFlowControl; 02129 Init.ClockDiv = hsd->Init.ClockDiv; 02130 SDIO_Init(hsd->Instance, Init); 02131 } 02132 02133 /* Change State */ 02134 hsd->State = HAL_SD_STATE_READY; 02135 02136 return HAL_OK; 02137 } 02138 02139 02140 /** 02141 * @brief Gets the current sd card data state. 02142 * @param hsd pointer to SD handle 02143 * @retval Card state 02144 */ 02145 HAL_SD_CardStateTypeDef HAL_SD_GetCardState(SD_HandleTypeDef *hsd) 02146 { 02147 HAL_SD_CardStateTypeDef cardstate = HAL_SD_CARD_TRANSFER; 02148 uint32_t errorstate = HAL_SD_ERROR_NONE; 02149 uint32_t resp1 = 0; 02150 02151 errorstate = SD_SendStatus(hsd, &resp1); 02152 if(errorstate != HAL_OK) 02153 { 02154 hsd->ErrorCode |= errorstate; 02155 } 02156 02157 cardstate = (HAL_SD_CardStateTypeDef)((resp1 >> 9U) & 0x0FU); 02158 02159 return cardstate; 02160 } 02161 02162 /** 02163 * @brief Abort the current transfer and disable the SD. 02164 * @param hsd pointer to a SD_HandleTypeDef structure that contains 02165 * the configuration information for SD module. 02166 * @retval HAL status 02167 */ 02168 HAL_StatusTypeDef HAL_SD_Abort(SD_HandleTypeDef *hsd) 02169 { 02170 HAL_SD_CardStateTypeDef CardState; 02171 02172 /* DIsable All interrupts */ 02173 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\ 02174 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR); 02175 02176 /* Clear All flags */ 02177 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 02178 02179 if((hsd->hdmatx != NULL) || (hsd->hdmarx != NULL)) 02180 { 02181 /* Disable the SD DMA request */ 02182 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN); 02183 02184 /* Abort the SD DMA Tx Stream */ 02185 if(hsd->hdmatx != NULL) 02186 { 02187 HAL_DMA_Abort(hsd->hdmatx); 02188 } 02189 /* Abort the SD DMA Rx Stream */ 02190 if(hsd->hdmarx != NULL) 02191 { 02192 HAL_DMA_Abort(hsd->hdmarx); 02193 } 02194 } 02195 02196 hsd->State = HAL_SD_STATE_READY; 02197 CardState = HAL_SD_GetCardState(hsd); 02198 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING)) 02199 { 02200 hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance); 02201 } 02202 if(hsd->ErrorCode != HAL_SD_ERROR_NONE) 02203 { 02204 return HAL_ERROR; 02205 } 02206 return HAL_OK; 02207 } 02208 02209 /** 02210 * @brief Abort the current transfer and disable the SD (IT mode). 02211 * @param hsd pointer to a SD_HandleTypeDef structure that contains 02212 * the configuration information for SD module. 02213 * @retval HAL status 02214 */ 02215 HAL_StatusTypeDef HAL_SD_Abort_IT(SD_HandleTypeDef *hsd) 02216 { 02217 HAL_SD_CardStateTypeDef CardState; 02218 02219 /* DIsable All interrupts */ 02220 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\ 02221 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR); 02222 02223 /* Clear All flags */ 02224 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 02225 02226 if((hsd->hdmatx != NULL) || (hsd->hdmarx != NULL)) 02227 { 02228 /* Disable the SD DMA request */ 02229 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN); 02230 02231 /* Abort the SD DMA Tx Stream */ 02232 if(hsd->hdmatx != NULL) 02233 { 02234 hsd->hdmatx->XferAbortCallback = SD_DMATxAbort; 02235 if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK) 02236 { 02237 hsd->hdmatx = NULL; 02238 } 02239 } 02240 /* Abort the SD DMA Rx Stream */ 02241 if(hsd->hdmarx != NULL) 02242 { 02243 hsd->hdmarx->XferAbortCallback = SD_DMARxAbort; 02244 if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK) 02245 { 02246 hsd->hdmarx = NULL; 02247 } 02248 } 02249 } 02250 02251 /* No transfer ongoing on both DMA channels*/ 02252 if((hsd->hdmatx == NULL) && (hsd->hdmarx == NULL)) 02253 { 02254 CardState = HAL_SD_GetCardState(hsd); 02255 hsd->State = HAL_SD_STATE_READY; 02256 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING)) 02257 { 02258 hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance); 02259 } 02260 if(hsd->ErrorCode != HAL_SD_ERROR_NONE) 02261 { 02262 return HAL_ERROR; 02263 } 02264 else 02265 { 02266 HAL_SD_AbortCallback(hsd); 02267 } 02268 } 02269 02270 return HAL_OK; 02271 } 02272 02273 /** 02274 * @} 02275 */ 02276 02277 /** 02278 * @} 02279 */ 02280 02281 /* Private function ----------------------------------------------------------*/ 02282 /** @addtogroup SD_Private_Functions 02283 * @{ 02284 */ 02285 02286 /** 02287 * @brief DMA SD transmit process complete callback 02288 * @param hdma DMA handle 02289 * @retval None 02290 */ 02291 static void SD_DMATransmitCplt(DMA_HandleTypeDef *hdma) 02292 { 02293 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent); 02294 02295 /* Enable DATAEND Interrupt */ 02296 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DATAEND)); 02297 } 02298 02299 /** 02300 * @brief DMA SD receive process complete callback 02301 * @param hdma DMA handle 02302 * @retval None 02303 */ 02304 static void SD_DMAReceiveCplt(DMA_HandleTypeDef *hdma) 02305 { 02306 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent); 02307 uint32_t errorstate = HAL_SD_ERROR_NONE; 02308 02309 /* Send stop command in multiblock write */ 02310 if(hsd->Context == (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA)) 02311 { 02312 errorstate = SDMMC_CmdStopTransfer(hsd->Instance); 02313 if(errorstate != HAL_SD_ERROR_NONE) 02314 { 02315 hsd->ErrorCode |= errorstate; 02316 HAL_SD_ErrorCallback(hsd); 02317 } 02318 } 02319 02320 /* Disable the DMA transfer for transmit request by setting the DMAEN bit 02321 in the SD DCTRL register */ 02322 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN); 02323 02324 /* Clear all the static flags */ 02325 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 02326 02327 hsd->State = HAL_SD_STATE_READY; 02328 02329 HAL_SD_RxCpltCallback(hsd); 02330 } 02331 02332 /** 02333 * @brief DMA SD communication error callback 02334 * @param hdma DMA handle 02335 * @retval None 02336 */ 02337 static void SD_DMAError(DMA_HandleTypeDef *hdma) 02338 { 02339 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent); 02340 HAL_SD_CardStateTypeDef CardState; 02341 02342 if((hsd->hdmarx->ErrorCode == HAL_DMA_ERROR_TE) || (hsd->hdmatx->ErrorCode == HAL_DMA_ERROR_TE)) 02343 { 02344 /* Clear All flags */ 02345 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 02346 02347 /* Disable All interrupts */ 02348 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\ 02349 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR); 02350 02351 hsd->ErrorCode |= HAL_SD_ERROR_DMA; 02352 CardState = HAL_SD_GetCardState(hsd); 02353 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING)) 02354 { 02355 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance); 02356 } 02357 02358 hsd->State= HAL_SD_STATE_READY; 02359 } 02360 02361 HAL_SD_ErrorCallback(hsd); 02362 } 02363 02364 /** 02365 * @brief DMA SD Tx Abort callback 02366 * @param hdma DMA handle 02367 * @retval None 02368 */ 02369 static void SD_DMATxAbort(DMA_HandleTypeDef *hdma) 02370 { 02371 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent); 02372 HAL_SD_CardStateTypeDef CardState; 02373 02374 if(hsd->hdmatx != NULL) 02375 { 02376 hsd->hdmatx = NULL; 02377 } 02378 02379 /* All DMA channels are aborted */ 02380 if(hsd->hdmarx == NULL) 02381 { 02382 CardState = HAL_SD_GetCardState(hsd); 02383 hsd->ErrorCode = HAL_SD_ERROR_NONE; 02384 hsd->State = HAL_SD_STATE_READY; 02385 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING)) 02386 { 02387 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance); 02388 02389 if(hsd->ErrorCode != HAL_SD_ERROR_NONE) 02390 { 02391 HAL_SD_AbortCallback(hsd); 02392 } 02393 else 02394 { 02395 HAL_SD_ErrorCallback(hsd); 02396 } 02397 } 02398 } 02399 } 02400 02401 /** 02402 * @brief DMA SD Rx Abort callback 02403 * @param hdma DMA handle 02404 * @retval None 02405 */ 02406 static void SD_DMARxAbort(DMA_HandleTypeDef *hdma) 02407 { 02408 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent); 02409 HAL_SD_CardStateTypeDef CardState; 02410 02411 if(hsd->hdmarx != NULL) 02412 { 02413 hsd->hdmarx = NULL; 02414 } 02415 02416 /* All DMA channels are aborted */ 02417 if(hsd->hdmatx == NULL) 02418 { 02419 CardState = HAL_SD_GetCardState(hsd); 02420 hsd->ErrorCode = HAL_SD_ERROR_NONE; 02421 hsd->State = HAL_SD_STATE_READY; 02422 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING)) 02423 { 02424 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance); 02425 02426 if(hsd->ErrorCode != HAL_SD_ERROR_NONE) 02427 { 02428 HAL_SD_AbortCallback(hsd); 02429 } 02430 else 02431 { 02432 HAL_SD_ErrorCallback(hsd); 02433 } 02434 } 02435 } 02436 } 02437 02438 02439 /** 02440 * @brief Initializes the sd card. 02441 * @param hsd Pointer to SD handle 02442 * @retval SD Card error state 02443 */ 02444 static uint32_t SD_InitCard(SD_HandleTypeDef *hsd) 02445 { 02446 HAL_SD_CardCSDTypeDef CSD; 02447 uint32_t errorstate = HAL_SD_ERROR_NONE; 02448 uint16_t sd_rca = 1U; 02449 02450 /* Check the power State */ 02451 if(SDIO_GetPowerState(hsd->Instance) == 0U) 02452 { 02453 /* Power off */ 02454 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE; 02455 } 02456 02457 if(hsd->SdCard.CardType != CARD_SECURED) 02458 { 02459 /* Send CMD2 ALL_SEND_CID */ 02460 errorstate = SDMMC_CmdSendCID(hsd->Instance); 02461 if(errorstate != HAL_SD_ERROR_NONE) 02462 { 02463 return errorstate; 02464 } 02465 else 02466 { 02467 /* Get Card identification number data */ 02468 hsd->CID[0U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP1); 02469 hsd->CID[1U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP2); 02470 hsd->CID[2U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP3); 02471 hsd->CID[3U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP4); 02472 } 02473 } 02474 02475 if(hsd->SdCard.CardType != CARD_SECURED) 02476 { 02477 /* Send CMD3 SET_REL_ADDR with argument 0 */ 02478 /* SD Card publishes its RCA. */ 02479 errorstate = SDMMC_CmdSetRelAdd(hsd->Instance, &sd_rca); 02480 if(errorstate != HAL_SD_ERROR_NONE) 02481 { 02482 return errorstate; 02483 } 02484 } 02485 if(hsd->SdCard.CardType != CARD_SECURED) 02486 { 02487 /* Get the SD card RCA */ 02488 hsd->SdCard.RelCardAdd = sd_rca; 02489 02490 /* Send CMD9 SEND_CSD with argument as card's RCA */ 02491 errorstate = SDMMC_CmdSendCSD(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U)); 02492 if(errorstate != HAL_SD_ERROR_NONE) 02493 { 02494 return errorstate; 02495 } 02496 else 02497 { 02498 /* Get Card Specific Data */ 02499 hsd->CSD[0U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP1); 02500 hsd->CSD[1U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP2); 02501 hsd->CSD[2U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP3); 02502 hsd->CSD[3U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP4); 02503 } 02504 } 02505 02506 /* Get the Card Class */ 02507 hsd->SdCard.Class = (SDIO_GetResponse(hsd->Instance, SDIO_RESP2) >> 20U); 02508 02509 /* Get CSD parameters */ 02510 HAL_SD_GetCardCSD(hsd, &CSD); 02511 02512 /* Select the Card */ 02513 errorstate = SDMMC_CmdSelDesel(hsd->Instance, (uint32_t)(((uint32_t)hsd->SdCard.RelCardAdd) << 16U)); 02514 if(errorstate != HAL_SD_ERROR_NONE) 02515 { 02516 return errorstate; 02517 } 02518 02519 /* Configure SDIO peripheral interface */ 02520 SDIO_Init(hsd->Instance, hsd->Init); 02521 02522 /* All cards are initialized */ 02523 return HAL_SD_ERROR_NONE; 02524 } 02525 02526 /** 02527 * @brief Enquires cards about their operating voltage and configures clock 02528 * controls and stores SD information that will be needed in future 02529 * in the SD handle. 02530 * @param hsd Pointer to SD handle 02531 * @retval error state 02532 */ 02533 static uint32_t SD_PowerON(SD_HandleTypeDef *hsd) 02534 { 02535 __IO uint32_t count = 0U; 02536 uint32_t response = 0U, validvoltage = 0U; 02537 uint32_t errorstate = HAL_SD_ERROR_NONE; 02538 02539 /* CMD0: GO_IDLE_STATE */ 02540 errorstate = SDMMC_CmdGoIdleState(hsd->Instance); 02541 if(errorstate != HAL_SD_ERROR_NONE) 02542 { 02543 return errorstate; 02544 } 02545 02546 /* CMD8: SEND_IF_COND: Command available only on V2.0 cards */ 02547 errorstate = SDMMC_CmdOperCond(hsd->Instance); 02548 if(errorstate != HAL_SD_ERROR_NONE) 02549 { 02550 hsd->SdCard.CardVersion = CARD_V1_X; 02551 02552 /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */ 02553 while(validvoltage == 0U) 02554 { 02555 if(count++ == SDMMC_MAX_VOLT_TRIAL) 02556 { 02557 return HAL_SD_ERROR_INVALID_VOLTRANGE; 02558 } 02559 02560 /* SEND CMD55 APP_CMD with RCA as 0 */ 02561 errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0U); 02562 if(errorstate != HAL_SD_ERROR_NONE) 02563 { 02564 return HAL_SD_ERROR_UNSUPPORTED_FEATURE; 02565 } 02566 02567 /* Send CMD41 */ 02568 errorstate = SDMMC_CmdAppOperCommand(hsd->Instance, SDMMC_STD_CAPACITY); 02569 if(errorstate != HAL_SD_ERROR_NONE) 02570 { 02571 return HAL_SD_ERROR_UNSUPPORTED_FEATURE; 02572 } 02573 02574 /* Get command response */ 02575 response = SDIO_GetResponse(hsd->Instance, SDIO_RESP1); 02576 02577 /* Get operating voltage*/ 02578 validvoltage = (((response >> 31U) == 1U) ? 1U : 0U); 02579 } 02580 /* Card type is SDSC */ 02581 hsd->SdCard.CardType = CARD_SDSC; 02582 } 02583 else 02584 { 02585 hsd->SdCard.CardVersion = CARD_V2_X; 02586 02587 /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */ 02588 while(validvoltage == 0U) 02589 { 02590 if(count++ == SDMMC_MAX_VOLT_TRIAL) 02591 { 02592 return HAL_SD_ERROR_INVALID_VOLTRANGE; 02593 } 02594 02595 /* SEND CMD55 APP_CMD with RCA as 0 */ 02596 errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0U); 02597 if(errorstate != HAL_SD_ERROR_NONE) 02598 { 02599 return errorstate; 02600 } 02601 02602 /* Send CMD41 */ 02603 errorstate = SDMMC_CmdAppOperCommand(hsd->Instance, SDMMC_HIGH_CAPACITY); 02604 if(errorstate != HAL_SD_ERROR_NONE) 02605 { 02606 return errorstate; 02607 } 02608 02609 /* Get command response */ 02610 response = SDIO_GetResponse(hsd->Instance, SDIO_RESP1); 02611 02612 /* Get operating voltage*/ 02613 validvoltage = (((response >> 31U) == 1U) ? 1U : 0U); 02614 } 02615 02616 if((response & SDMMC_HIGH_CAPACITY) == SDMMC_HIGH_CAPACITY) /* (response &= SD_HIGH_CAPACITY) */ 02617 { 02618 hsd->SdCard.CardType = CARD_SDHC_SDXC; 02619 } 02620 else 02621 { 02622 hsd->SdCard.CardType = CARD_SDSC; 02623 } 02624 } 02625 02626 return HAL_SD_ERROR_NONE; 02627 } 02628 02629 /** 02630 * @brief Turns the SDIO output signals off. 02631 * @param hsd Pointer to SD handle 02632 * @retval HAL status 02633 */ 02634 static HAL_StatusTypeDef SD_PowerOFF(SD_HandleTypeDef *hsd) 02635 { 02636 /* Set Power State to OFF */ 02637 SDIO_PowerState_OFF(hsd->Instance); 02638 02639 return HAL_OK; 02640 } 02641 02642 /** 02643 * @brief Send Status info command. 02644 * @param hsd pointer to SD handle 02645 * @param pSDstatus Pointer to the buffer that will contain the SD card status 02646 * SD Status register) 02647 * @retval error state 02648 */ 02649 static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus) 02650 { 02651 SDIO_DataInitTypeDef config; 02652 uint32_t errorstate = HAL_SD_ERROR_NONE; 02653 uint32_t tickstart = HAL_GetTick(); 02654 uint32_t count = 0U; 02655 02656 /* Check SD response */ 02657 if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED) 02658 { 02659 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED; 02660 } 02661 02662 /* Set block size for card if it is not equal to current block size for card */ 02663 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U); 02664 if(errorstate != HAL_SD_ERROR_NONE) 02665 { 02666 hsd->ErrorCode |= HAL_SD_ERROR_NONE; 02667 return errorstate; 02668 } 02669 02670 /* Send CMD55 */ 02671 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U)); 02672 if(errorstate != HAL_SD_ERROR_NONE) 02673 { 02674 hsd->ErrorCode |= HAL_SD_ERROR_NONE; 02675 return errorstate; 02676 } 02677 02678 /* Configure the SD DPSM (Data Path State Machine) */ 02679 config.DataTimeOut = SDMMC_DATATIMEOUT; 02680 config.DataLength = 64U; 02681 config.DataBlockSize = SDIO_DATABLOCK_SIZE_64B; 02682 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO; 02683 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK; 02684 config.DPSM = SDIO_DPSM_ENABLE; 02685 SDIO_ConfigData(hsd->Instance, &config); 02686 02687 /* Send ACMD13 (SD_APP_STAUS) with argument as card's RCA */ 02688 errorstate = SDMMC_CmdStatusRegister(hsd->Instance); 02689 if(errorstate != HAL_SD_ERROR_NONE) 02690 { 02691 hsd->ErrorCode |= HAL_SD_ERROR_NONE; 02692 return errorstate; 02693 } 02694 02695 /* Get status data */ 02696 while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND)) 02697 { 02698 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF)) 02699 { 02700 for(count = 0U; count < 8U; count++) 02701 { 02702 *(pSDstatus + count) = SDIO_ReadFIFO(hsd->Instance); 02703 } 02704 02705 pSDstatus += 8U; 02706 } 02707 02708 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT) 02709 { 02710 return HAL_SD_ERROR_TIMEOUT; 02711 } 02712 } 02713 02714 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT)) 02715 { 02716 return HAL_SD_ERROR_DATA_TIMEOUT; 02717 } 02718 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL)) 02719 { 02720 return HAL_SD_ERROR_DATA_CRC_FAIL; 02721 } 02722 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR)) 02723 { 02724 return HAL_SD_ERROR_RX_OVERRUN; 02725 } 02726 02727 while ((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXDAVL))) 02728 { 02729 *pSDstatus = SDIO_ReadFIFO(hsd->Instance); 02730 pSDstatus++; 02731 02732 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT) 02733 { 02734 return HAL_SD_ERROR_TIMEOUT; 02735 } 02736 } 02737 02738 /* Clear all the static status flags*/ 02739 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 02740 02741 return HAL_SD_ERROR_NONE; 02742 } 02743 02744 /** 02745 * @brief Returns the current card's status. 02746 * @param hsd Pointer to SD handle 02747 * @param pCardStatus pointer to the buffer that will contain the SD card 02748 * status (Card Status register) 02749 * @retval error state 02750 */ 02751 static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus) 02752 { 02753 uint32_t errorstate = HAL_SD_ERROR_NONE; 02754 02755 if(pCardStatus == NULL) 02756 { 02757 return HAL_SD_ERROR_PARAM; 02758 } 02759 02760 /* Send Status command */ 02761 errorstate = SDMMC_CmdSendStatus(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U)); 02762 if(errorstate != HAL_OK) 02763 { 02764 return errorstate; 02765 } 02766 02767 /* Get SD card status */ 02768 *pCardStatus = SDIO_GetResponse(hsd->Instance, SDIO_RESP1); 02769 02770 return HAL_SD_ERROR_NONE; 02771 } 02772 02773 /** 02774 * @brief Enables the SDIO wide bus mode. 02775 * @param hsd pointer to SD handle 02776 * @retval error state 02777 */ 02778 static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd) 02779 { 02780 uint32_t scr[2U] = {0U, 0U}; 02781 uint32_t errorstate = HAL_SD_ERROR_NONE; 02782 02783 if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED) 02784 { 02785 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED; 02786 } 02787 02788 /* Get SCR Register */ 02789 errorstate = SD_FindSCR(hsd, scr); 02790 if(errorstate != HAL_OK) 02791 { 02792 return errorstate; 02793 } 02794 02795 /* If requested card supports wide bus operation */ 02796 if((scr[1U] & SDMMC_WIDE_BUS_SUPPORT) != SDMMC_ALLZERO) 02797 { 02798 /* Send CMD55 APP_CMD with argument as card's RCA.*/ 02799 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U)); 02800 if(errorstate != HAL_OK) 02801 { 02802 return errorstate; 02803 } 02804 02805 /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */ 02806 errorstate = SDMMC_CmdBusWidth(hsd->Instance, 2U); 02807 if(errorstate != HAL_OK) 02808 { 02809 return errorstate; 02810 } 02811 02812 return HAL_SD_ERROR_NONE; 02813 } 02814 else 02815 { 02816 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE; 02817 } 02818 } 02819 02820 /** 02821 * @brief Disables the SDIO wide bus mode. 02822 * @param hsd Pointer to SD handle 02823 * @retval error state 02824 */ 02825 static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd) 02826 { 02827 uint32_t scr[2U] = {0U, 0U}; 02828 uint32_t errorstate = HAL_SD_ERROR_NONE; 02829 02830 if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED) 02831 { 02832 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED; 02833 } 02834 02835 /* Get SCR Register */ 02836 errorstate = SD_FindSCR(hsd, scr); 02837 if(errorstate != HAL_OK) 02838 { 02839 return errorstate; 02840 } 02841 02842 /* If requested card supports 1 bit mode operation */ 02843 if((scr[1U] & SDMMC_SINGLE_BUS_SUPPORT) != SDMMC_ALLZERO) 02844 { 02845 /* Send CMD55 APP_CMD with argument as card's RCA */ 02846 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U)); 02847 if(errorstate != HAL_OK) 02848 { 02849 return errorstate; 02850 } 02851 02852 /* Send ACMD6 APP_CMD with argument as 0 for single bus mode */ 02853 errorstate = SDMMC_CmdBusWidth(hsd->Instance, 0U); 02854 if(errorstate != HAL_OK) 02855 { 02856 return errorstate; 02857 } 02858 02859 return HAL_SD_ERROR_NONE; 02860 } 02861 else 02862 { 02863 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE; 02864 } 02865 } 02866 02867 02868 /** 02869 * @brief Finds the SD card SCR register value. 02870 * @param hsd Pointer to SD handle 02871 * @param pSCR pointer to the buffer that will contain the SCR value 02872 * @retval error state 02873 */ 02874 static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR) 02875 { 02876 SDIO_DataInitTypeDef config; 02877 uint32_t errorstate = HAL_SD_ERROR_NONE; 02878 uint32_t tickstart = HAL_GetTick(); 02879 uint32_t index = 0U; 02880 uint32_t tempscr[2U] = {0U, 0U}; 02881 02882 /* Set Block Size To 8 Bytes */ 02883 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 8U); 02884 if(errorstate != HAL_OK) 02885 { 02886 return errorstate; 02887 } 02888 02889 /* Send CMD55 APP_CMD with argument as card's RCA */ 02890 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)((hsd->SdCard.RelCardAdd) << 16U)); 02891 if(errorstate != HAL_OK) 02892 { 02893 return errorstate; 02894 } 02895 02896 config.DataTimeOut = SDMMC_DATATIMEOUT; 02897 config.DataLength = 8U; 02898 config.DataBlockSize = SDIO_DATABLOCK_SIZE_8B; 02899 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO; 02900 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK; 02901 config.DPSM = SDIO_DPSM_ENABLE; 02902 SDIO_ConfigData(hsd->Instance, &config); 02903 02904 /* Send ACMD51 SD_APP_SEND_SCR with argument as 0 */ 02905 errorstate = SDMMC_CmdSendSCR(hsd->Instance); 02906 if(errorstate != HAL_OK) 02907 { 02908 return errorstate; 02909 } 02910 02911 while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND)) 02912 { 02913 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)) 02914 { 02915 *(tempscr + index) = SDIO_ReadFIFO(hsd->Instance); 02916 index++; 02917 } 02918 02919 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT) 02920 { 02921 return HAL_SD_ERROR_TIMEOUT; 02922 } 02923 } 02924 02925 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT)) 02926 { 02927 __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT); 02928 02929 return HAL_SD_ERROR_DATA_TIMEOUT; 02930 } 02931 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL)) 02932 { 02933 __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL); 02934 02935 return HAL_SD_ERROR_DATA_CRC_FAIL; 02936 } 02937 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR)) 02938 { 02939 __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR); 02940 02941 return HAL_SD_ERROR_RX_OVERRUN; 02942 } 02943 else 02944 { 02945 /* No error flag set */ 02946 /* Clear all the static flags */ 02947 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 02948 02949 *(pSCR + 1U) = ((tempscr[0U] & SDMMC_0TO7BITS) << 24U) | ((tempscr[0U] & SDMMC_8TO15BITS) << 8U) |\ 02950 ((tempscr[0U] & SDMMC_16TO23BITS) >> 8U) | ((tempscr[0U] & SDMMC_24TO31BITS) >> 24U); 02951 02952 *(pSCR) = ((tempscr[1U] & SDMMC_0TO7BITS) << 24U) | ((tempscr[1U] & SDMMC_8TO15BITS) << 8U) |\ 02953 ((tempscr[1U] & SDMMC_16TO23BITS) >> 8U) | ((tempscr[1U] & SDMMC_24TO31BITS) >> 24U); 02954 } 02955 02956 return HAL_SD_ERROR_NONE; 02957 } 02958 02959 /** 02960 * @brief Wrap up reading in non-blocking mode. 02961 * @param hsd pointer to a SD_HandleTypeDef structure that contains 02962 * the configuration information. 02963 * @retval HAL status 02964 */ 02965 static HAL_StatusTypeDef SD_Read_IT(SD_HandleTypeDef *hsd) 02966 { 02967 uint32_t count = 0U; 02968 uint32_t* tmp; 02969 02970 tmp = (uint32_t*)hsd->pRxBuffPtr; 02971 02972 /* Read data from SDIO Rx FIFO */ 02973 for(count = 0U; count < 8U; count++) 02974 { 02975 *(tmp + count) = SDIO_ReadFIFO(hsd->Instance); 02976 } 02977 02978 hsd->pRxBuffPtr += 8U; 02979 02980 return HAL_OK; 02981 } 02982 02983 /** 02984 * @brief Wrap up writing in non-blocking mode. 02985 * @param hsd pointer to a SD_HandleTypeDef structure that contains 02986 * the configuration information. 02987 * @retval HAL status 02988 */ 02989 static HAL_StatusTypeDef SD_Write_IT(SD_HandleTypeDef *hsd) 02990 { 02991 uint32_t count = 0U; 02992 uint32_t* tmp; 02993 02994 tmp = (uint32_t*)hsd->pTxBuffPtr; 02995 02996 /* Write data to SDIO Tx FIFO */ 02997 for(count = 0U; count < 8U; count++) 02998 { 02999 SDIO_WriteFIFO(hsd->Instance, (tmp + count)); 03000 } 03001 03002 hsd->pTxBuffPtr += 8U; 03003 03004 return HAL_OK; 03005 } 03006 03007 /** 03008 * @} 03009 */ 03010 03011 #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || 03012 STM32F401xC || STM32F401xE || STM32F411xE || STM32F446xx || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Vx || 03013 STM32F412Rx || STM32F412Cx || STM32F413xx || STM32F423xx */ 03014 03015 #endif /* HAL_SD_MODULE_ENABLED */ 03016 03017 /** 03018 * @} 03019 */ 03020 03021 /** 03022 * @} 03023 */ 03024 03025 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/