stm32_eval_sdio_sd.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32_eval_sdio_sd.c
00004   * @author  MCD Application Team
00005   * @version V4.3.0
00006   * @date    10/15/2010
00007   * @brief   This file provides a set of functions needed to manage the SDIO SD 
00008   *          Card memory mounted on STM32xx-EVAL board (refer to stm32_eval.h
00009   *          to know about the boards supporting this memory). 
00010   *          It implements a high level communication layer for read and write 
00011   *          from/to this memory. The needed STM32 hardware resources (SDIO and 
00012   *          GPIO) are defined in stm32xx_eval.h file, and the initialization is 
00013   *          performed in SD_LowLevel_Init() function declared in stm32xx_eval.c 
00014   *          file.
00015   *          You can easily tailor this driver to any other development board, 
00016   *          by just adapting the defines for hardware resources and 
00017   *          SD_LowLevel_Init() function.
00018   *            
00019   *          +-----------------------------------------------------------+
00020   *          |                     Pin assignment                        |
00021   *          +-----------------------------+---------------+-------------+
00022   *          |  STM32 SDIO Pins            |     SD        |    Pin      |
00023   *          +-----------------------------+---------------+-------------+
00024   *          |      SDIO D2                |   D2          |    1        |
00025   *          |      SDIO D3                |   D3          |    2        |
00026   *          |      SDIO CMD               |   CMD         |    3        |
00027   *          |                             |   VCC         |    4 (3.3 V)|
00028   *          |      SDIO CLK               |   CLK         |    5        |
00029   *          |                             |   GND         |    6 (0 V)  |
00030   *          |      SDIO D0                |   D0          |    7        |
00031   *          |      SDIO D1                |   D1          |    8        |  
00032   *          +-----------------------------+---------------+-------------+   
00033   ******************************************************************************
00034   * @copy
00035   *
00036   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
00037   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
00038   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
00039   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
00040   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
00041   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
00042   *
00043   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
00044   */ 
00045 
00046 /* Includes ------------------------------------------------------------------*/
00047 #include "stm32_eval_sdio_sd.h"
00048 
00049 /** @addtogroup Utilities
00050   * @{
00051   */
00052   
00053 /** @addtogroup STM32_EVAL
00054   * @{
00055   */ 
00056 
00057 /** @addtogroup Common
00058   * @{
00059   */
00060   
00061 /** @addtogroup STM32_EVAL_SDIO_SD
00062   * @brief      This file provides all the SD Card driver firmware functions.
00063   * @{
00064   */ 
00065 
00066 /** @defgroup STM32_EVAL_SDIO_SD_Private_Types
00067   * @{
00068   */ 
00069 /**
00070   * @}
00071   */ 
00072 
00073 
00074 /** @defgroup STM32_EVAL_SDIO_SD_Private_Defines
00075   * @{
00076   */ 
00077 /** 
00078   * @brief  SDIO Static flags, TimeOut, FIFO Address  
00079   */
00080 #define NULL 0
00081 #define SDIO_STATIC_FLAGS               ((uint32_t)0x000005FF)
00082 #define SDIO_CMD0TIMEOUT                ((uint32_t)0x00010000)
00083 
00084 /** 
00085   * @brief  Mask for errors Card Status R1 (OCR Register) 
00086   */
00087 #define SD_OCR_ADDR_OUT_OF_RANGE        ((uint32_t)0x80000000)
00088 #define SD_OCR_ADDR_MISALIGNED          ((uint32_t)0x40000000)
00089 #define SD_OCR_BLOCK_LEN_ERR            ((uint32_t)0x20000000)
00090 #define SD_OCR_ERASE_SEQ_ERR            ((uint32_t)0x10000000)
00091 #define SD_OCR_BAD_ERASE_PARAM          ((uint32_t)0x08000000)
00092 #define SD_OCR_WRITE_PROT_VIOLATION     ((uint32_t)0x04000000)
00093 #define SD_OCR_LOCK_UNLOCK_FAILED       ((uint32_t)0x01000000)
00094 #define SD_OCR_COM_CRC_FAILED           ((uint32_t)0x00800000)
00095 #define SD_OCR_ILLEGAL_CMD              ((uint32_t)0x00400000)
00096 #define SD_OCR_CARD_ECC_FAILED          ((uint32_t)0x00200000)
00097 #define SD_OCR_CC_ERROR                 ((uint32_t)0x00100000)
00098 #define SD_OCR_GENERAL_UNKNOWN_ERROR    ((uint32_t)0x00080000)
00099 #define SD_OCR_STREAM_READ_UNDERRUN     ((uint32_t)0x00040000)
00100 #define SD_OCR_STREAM_WRITE_OVERRUN     ((uint32_t)0x00020000)
00101 #define SD_OCR_CID_CSD_OVERWRIETE       ((uint32_t)0x00010000)
00102 #define SD_OCR_WP_ERASE_SKIP            ((uint32_t)0x00008000)
00103 #define SD_OCR_CARD_ECC_DISABLED        ((uint32_t)0x00004000)
00104 #define SD_OCR_ERASE_RESET              ((uint32_t)0x00002000)
00105 #define SD_OCR_AKE_SEQ_ERROR            ((uint32_t)0x00000008)
00106 #define SD_OCR_ERRORBITS                ((uint32_t)0xFDFFE008)
00107 
00108 /** 
00109   * @brief  Masks for R6 Response 
00110   */
00111 #define SD_R6_GENERAL_UNKNOWN_ERROR     ((uint32_t)0x00002000)
00112 #define SD_R6_ILLEGAL_CMD               ((uint32_t)0x00004000)
00113 #define SD_R6_COM_CRC_FAILED            ((uint32_t)0x00008000)
00114 
00115 #define SD_VOLTAGE_WINDOW_SD            ((uint32_t)0x80100000)
00116 #define SD_HIGH_CAPACITY                ((uint32_t)0x40000000)
00117 #define SD_STD_CAPACITY                 ((uint32_t)0x00000000)
00118 #define SD_CHECK_PATTERN                ((uint32_t)0x000001AA)
00119 
00120 #define SD_MAX_VOLT_TRIAL               ((uint32_t)0x0000FFFF)
00121 #define SD_ALLZERO                      ((uint32_t)0x00000000)
00122 
00123 #define SD_WIDE_BUS_SUPPORT             ((uint32_t)0x00040000)
00124 #define SD_SINGLE_BUS_SUPPORT           ((uint32_t)0x00010000)
00125 #define SD_CARD_LOCKED                  ((uint32_t)0x02000000)
00126 
00127 #define SD_DATATIMEOUT                  ((uint32_t)0x000FFFFF)
00128 #define SD_0TO7BITS                     ((uint32_t)0x000000FF)
00129 #define SD_8TO15BITS                    ((uint32_t)0x0000FF00)
00130 #define SD_16TO23BITS                   ((uint32_t)0x00FF0000)
00131 #define SD_24TO31BITS                   ((uint32_t)0xFF000000)
00132 #define SD_MAX_DATA_LENGTH              ((uint32_t)0x01FFFFFF)
00133 
00134 #define SD_HALFFIFO                     ((uint32_t)0x00000008)
00135 #define SD_HALFFIFOBYTES                ((uint32_t)0x00000020)
00136 
00137 /** 
00138   * @brief  Command Class Supported 
00139   */
00140 #define SD_CCCC_LOCK_UNLOCK             ((uint32_t)0x00000080)
00141 #define SD_CCCC_WRITE_PROT              ((uint32_t)0x00000040)
00142 #define SD_CCCC_ERASE                   ((uint32_t)0x00000020)
00143 
00144 /** 
00145   * @brief  Following commands are SD Card Specific commands.
00146   *         SDIO_APP_CMD should be sent before sending these commands. 
00147   */
00148 #define SDIO_SEND_IF_COND               ((uint32_t)0x00000008)
00149 
00150 /**
00151   * @}
00152   */ 
00153 
00154 
00155 /** @defgroup STM32_EVAL_SDIO_SD_Private_Macros
00156   * @{
00157   */
00158 /**
00159   * @}
00160   */ 
00161   
00162 
00163 /** @defgroup STM32_EVAL_SDIO_SD_Private_Variables
00164   * @{
00165   */
00166 static uint32_t CardType =  SDIO_STD_CAPACITY_SD_CARD_V1_1;
00167 static uint32_t CSD_Tab[4], CID_Tab[4], RCA = 0;
00168 static uint32_t DeviceMode = SD_POLLING_MODE;
00169 static uint32_t TotalNumberOfBytes = 0, StopCondition = 0;
00170 uint32_t *SrcBuffer, *DestBuffer;
00171 __IO SD_Error TransferError = SD_OK;
00172 __IO uint32_t TransferEnd = 0;
00173 __IO uint32_t NumberOfBytes = 0;
00174 SD_CardInfo SDCardInfo;
00175 SDIO_InitTypeDef SDIO_InitStructure;
00176 SDIO_CmdInitTypeDef SDIO_CmdInitStructure;
00177 SDIO_DataInitTypeDef SDIO_DataInitStructure;   
00178 /**
00179   * @}
00180   */ 
00181 
00182 
00183 /** @defgroup STM32_EVAL_SDIO_SD_Private_Function_Prototypes
00184   * @{
00185   */
00186 static SD_Error CmdError(void);
00187 static SD_Error CmdResp1Error(uint8_t cmd);
00188 static SD_Error CmdResp7Error(void);
00189 static SD_Error CmdResp3Error(void);
00190 static SD_Error CmdResp2Error(void);
00191 static SD_Error CmdResp6Error(uint8_t cmd, uint16_t *prca);
00192 static SD_Error SDEnWideBus(FunctionalState NewState);
00193 static SD_Error IsCardProgramming(uint8_t *pstatus);
00194 static SD_Error FindSCR(uint16_t rca, uint32_t *pscr);
00195 static uint8_t convert_from_bytes_to_power_of_two(uint16_t NumberOfBytes);
00196   
00197 /**
00198   * @}
00199   */ 
00200 
00201 
00202 /** @defgroup STM32_EVAL_SDIO_SD_Private_Functions
00203   * @{
00204   */  
00205 
00206 /**
00207   * @brief  DeInitializes the SDIO interface.
00208   * @param  None
00209   * @retval None
00210   */
00211 void SD_DeInit(void)
00212 { 
00213   SD_LowLevel_DeInit();
00214 }
00215 
00216 /**
00217   * @brief  Initializes the SD Card and put it into StandBy State (Ready for data 
00218   *         transfer).
00219   * @param  None
00220   * @retval SD_Error: SD Card Error code.
00221   */
00222 SD_Error SD_Init(void)
00223 {
00224   SD_Error errorstatus = SD_OK;
00225   
00226   /* SDIO Peripheral Low Level Init */
00227   SD_LowLevel_Init();
00228 
00229   SDIO_DeInit();
00230 
00231   errorstatus = SD_PowerON();
00232 
00233   if (errorstatus != SD_OK)
00234   {
00235     /*!< CMD Response TimeOut (wait for CMDSENT flag) */
00236     return(errorstatus);
00237   }
00238 
00239   errorstatus = SD_InitializeCards();
00240 
00241   if (errorstatus != SD_OK)
00242   {
00243     /*!< CMD Response TimeOut (wait for CMDSENT flag) */
00244     return(errorstatus);
00245   }
00246 
00247   /*!< Configure the SDIO peripheral */
00248   /*!< SDIOCLK = HCLK, SDIO_CK = HCLK/(2 + SDIO_TRANSFER_CLK_DIV) */ 
00249   SDIO_InitStructure.SDIO_ClockDiv = SDIO_TRANSFER_CLK_DIV; 
00250   SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_Rising;
00251   SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_Disable;
00252   SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable;
00253   SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_1b;
00254   SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable;
00255   SDIO_Init(&SDIO_InitStructure);
00256   
00257   if (errorstatus == SD_OK)
00258   {
00259     /*----------------- Read CSD/CID MSD registers ------------------*/
00260     errorstatus = SD_GetCardInfo(&SDCardInfo);
00261   }
00262 
00263   if (errorstatus == SD_OK)
00264   {
00265     /*----------------- Select Card --------------------------------*/
00266     errorstatus = SD_SelectDeselect((uint32_t) (SDCardInfo.RCA << 16));
00267   }
00268 
00269   if (errorstatus == SD_OK)
00270   {
00271     errorstatus = SD_EnableWideBusOperation(SDIO_BusWide_4b);
00272   }  
00273 
00274   /* Set Device Transfer Mode to DMA */
00275   if (errorstatus == SD_OK)
00276   {  
00277     errorstatus = SD_SetDeviceMode(SD_DMA_MODE);
00278   }
00279   
00280   return(errorstatus);
00281 }
00282 
00283 /**
00284   * @brief  Gets the cuurent sd card data transfer status.
00285   * @param  None
00286   * @retval SDTransferState: Data Transfer state.
00287   *   This value can be: 
00288   *        - SD_TRANSFER_OK: No data transfer is acting
00289   *        - SD_TRANSFER_BUSY: Data transfer is acting
00290   */
00291 SDTransferState SD_GetStatus(void)
00292 {
00293   SDCardState cardstate =  SD_CARD_TRANSFER;
00294 
00295   cardstate = SD_GetState();
00296   
00297   if (cardstate == SD_CARD_TRANSFER)
00298   {
00299     return(SD_TRANSFER_OK);
00300   }
00301   else if(cardstate == SD_CARD_ERROR)
00302   {
00303     return (SD_TRANSFER_ERROR);
00304   }
00305   else
00306   {
00307     return(SD_TRANSFER_BUSY);
00308   }
00309 }
00310 
00311 /**
00312   * @brief  Returns the current card's state.
00313   * @param  None
00314   * @retval SDCardState: SD Card Error or SD Card Current State.
00315   */
00316 SDCardState SD_GetState(void)
00317 {
00318   uint32_t resp1 = 0;
00319   
00320   if(SD_Detect()== SD_PRESENT)
00321   {
00322     if (SD_SendStatus(&resp1) != SD_OK)
00323     {
00324       return SD_CARD_ERROR;
00325     }
00326     else
00327     {
00328       return (SDCardState)((resp1 >> 9) & 0x0F);
00329     }
00330   }
00331   else
00332   {
00333     return SD_CARD_ERROR;
00334   }
00335 }
00336 
00337 /**
00338  * @brief  Detect if SD card is correctly plugged in the memory slot.
00339  * @param  None
00340  * @retval Return if SD is detected or not
00341  */
00342 uint8_t SD_Detect(void)
00343 {
00344   __IO uint8_t status = SD_PRESENT;
00345 
00346   /*!< Check GPIO to detect SD */
00347   if (GPIO_ReadInputDataBit(SD_DETECT_GPIO_PORT, SD_DETECT_PIN) != Bit_RESET)
00348   {
00349     status = SD_NOT_PRESENT;
00350   }
00351   return status;
00352 }
00353 
00354 /**
00355   * @brief  Enquires cards about their operating voltage and configures 
00356   *   clock controls.
00357   * @param  None
00358   * @retval SD_Error: SD Card Error code.
00359   */
00360 SD_Error SD_PowerON(void)
00361 {
00362   SD_Error errorstatus = SD_OK;
00363   uint32_t response = 0, count = 0, validvoltage = 0;
00364   uint32_t SDType = SD_STD_CAPACITY;
00365 
00366   /*!< Power ON Sequence -----------------------------------------------------*/
00367   /*!< Configure the SDIO peripheral */
00368   /*!< SDIOCLK = HCLK, SDIO_CK = HCLK/(2 + SDIO_INIT_CLK_DIV) */
00369   /*!< SDIO_CK for initialization should not exceed 400 KHz */  
00370   SDIO_InitStructure.SDIO_ClockDiv = SDIO_INIT_CLK_DIV;
00371   SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_Rising;
00372   SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_Disable;
00373   SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable;
00374   SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_1b;
00375   SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable;
00376   SDIO_Init(&SDIO_InitStructure);
00377 
00378   /*!< Set Power State to ON */
00379   SDIO_SetPowerState(SDIO_PowerState_ON);
00380 
00381   /*!< Enable SDIO Clock */
00382   SDIO_ClockCmd(ENABLE);
00383 
00384   /*!< CMD0: GO_IDLE_STATE ---------------------------------------------------*/
00385   /*!< No CMD response required */
00386   SDIO_CmdInitStructure.SDIO_Argument = 0x0;
00387   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_GO_IDLE_STATE;
00388   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_No;
00389   SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
00390   SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
00391   SDIO_SendCommand(&SDIO_CmdInitStructure);
00392 
00393   errorstatus = CmdError();
00394 
00395   if (errorstatus != SD_OK)
00396   {
00397     /*!< CMD Response TimeOut (wait for CMDSENT flag) */
00398     return(errorstatus);
00399   }
00400 
00401   /*!< CMD8: SEND_IF_COND ----------------------------------------------------*/
00402   /*!< Send CMD8 to verify SD card interface operating condition */
00403   /*!< Argument: - [31:12]: Reserved (shall be set to '0')
00404                - [11:8]: Supply Voltage (VHS) 0x1 (Range: 2.7-3.6 V)
00405                - [7:0]: Check Pattern (recommended 0xAA) */
00406   /*!< CMD Response: R7 */
00407   SDIO_CmdInitStructure.SDIO_Argument = SD_CHECK_PATTERN;
00408   SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_SEND_IF_COND;
00409   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
00410   SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
00411   SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
00412   SDIO_SendCommand(&SDIO_CmdInitStructure);
00413 
00414   errorstatus = CmdResp7Error();
00415 
00416   if (errorstatus == SD_OK)
00417   {
00418     CardType = SDIO_STD_CAPACITY_SD_CARD_V2_0; /*!< SD Card 2.0 */
00419     SDType = SD_HIGH_CAPACITY;
00420   }
00421   else
00422   {
00423     /*!< CMD55 */
00424     SDIO_CmdInitStructure.SDIO_Argument = 0x00;
00425     SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
00426     SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
00427     SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
00428     SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
00429     SDIO_SendCommand(&SDIO_CmdInitStructure);
00430     errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
00431   }
00432   /*!< CMD55 */
00433   SDIO_CmdInitStructure.SDIO_Argument = 0x00;
00434   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
00435   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
00436   SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
00437   SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
00438   SDIO_SendCommand(&SDIO_CmdInitStructure);
00439   errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
00440 
00441   /*!< If errorstatus is Command TimeOut, it is a MMC card */
00442   /*!< If errorstatus is SD_OK it is a SD card: SD card 2.0 (voltage range mismatch)
00443      or SD card 1.x */
00444   if (errorstatus == SD_OK)
00445   {
00446     /*!< SD CARD */
00447     /*!< Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
00448     while ((!validvoltage) && (count < SD_MAX_VOLT_TRIAL))
00449     {
00450 
00451       /*!< SEND CMD55 APP_CMD with RCA as 0 */
00452       SDIO_CmdInitStructure.SDIO_Argument = 0x00;
00453       SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
00454       SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
00455       SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
00456       SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
00457       SDIO_SendCommand(&SDIO_CmdInitStructure);
00458 
00459       errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
00460 
00461       if (errorstatus != SD_OK)
00462       {
00463         return(errorstatus);
00464       }
00465       SDIO_CmdInitStructure.SDIO_Argument = SD_VOLTAGE_WINDOW_SD | SDType;
00466       SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_APP_OP_COND;
00467       SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
00468       SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
00469       SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
00470       SDIO_SendCommand(&SDIO_CmdInitStructure);
00471 
00472       errorstatus = CmdResp3Error();
00473       if (errorstatus != SD_OK)
00474       {
00475         return(errorstatus);
00476       }
00477 
00478       response = SDIO_GetResponse(SDIO_RESP1);
00479       validvoltage = (((response >> 31) == 1) ? 1 : 0);
00480       count++;
00481     }
00482     if (count >= SD_MAX_VOLT_TRIAL)
00483     {
00484       errorstatus = SD_INVALID_VOLTRANGE;
00485       return(errorstatus);
00486     }
00487 
00488     if (response &= SD_HIGH_CAPACITY)
00489     {
00490       CardType = SDIO_HIGH_CAPACITY_SD_CARD;
00491     }
00492 
00493   }/*!< else MMC Card */
00494 
00495   return(errorstatus);
00496 }
00497 
00498 /**
00499   * @brief  Turns the SDIO output signals off.
00500   * @param  None
00501   * @retval SD_Error: SD Card Error code.
00502   */
00503 SD_Error SD_PowerOFF(void)
00504 {
00505   SD_Error errorstatus = SD_OK;
00506 
00507   /*!< Set Power State to OFF */
00508   SDIO_SetPowerState(SDIO_PowerState_OFF);
00509 
00510   return(errorstatus);
00511 }
00512 
00513 /**
00514   * @brief  Intialises all cards or single card as the case may be Card(s) come 
00515   *         into standby state.
00516   * @param  None
00517   * @retval SD_Error: SD Card Error code.
00518   */
00519 SD_Error SD_InitializeCards(void)
00520 {
00521   SD_Error errorstatus = SD_OK;
00522   uint16_t rca = 0x01;
00523 
00524   if (SDIO_GetPowerState() == SDIO_PowerState_OFF)
00525   {
00526     errorstatus = SD_REQUEST_NOT_APPLICABLE;
00527     return(errorstatus);
00528   }
00529 
00530   if (SDIO_SECURE_DIGITAL_IO_CARD != CardType)
00531   {
00532     /*!< Send CMD2 ALL_SEND_CID */
00533     SDIO_CmdInitStructure.SDIO_Argument = 0x0;
00534     SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_ALL_SEND_CID;
00535     SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Long;
00536     SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
00537     SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
00538     SDIO_SendCommand(&SDIO_CmdInitStructure);
00539 
00540     errorstatus = CmdResp2Error();
00541 
00542     if (SD_OK != errorstatus)
00543     {
00544       return(errorstatus);
00545     }
00546 
00547     CID_Tab[0] = SDIO_GetResponse(SDIO_RESP1);
00548     CID_Tab[1] = SDIO_GetResponse(SDIO_RESP2);
00549     CID_Tab[2] = SDIO_GetResponse(SDIO_RESP3);
00550     CID_Tab[3] = SDIO_GetResponse(SDIO_RESP4);
00551   }
00552   if ((SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType) ||  (SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType) ||  (SDIO_SECURE_DIGITAL_IO_COMBO_CARD == CardType)
00553       ||  (SDIO_HIGH_CAPACITY_SD_CARD == CardType))
00554   {
00555     /*!< Send CMD3 SET_REL_ADDR with argument 0 */
00556     /*!< SD Card publishes its RCA. */
00557     SDIO_CmdInitStructure.SDIO_Argument = 0x00;
00558     SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_REL_ADDR;
00559     SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
00560     SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
00561     SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
00562     SDIO_SendCommand(&SDIO_CmdInitStructure);
00563 
00564     errorstatus = CmdResp6Error(SD_CMD_SET_REL_ADDR, &rca);
00565 
00566     if (SD_OK != errorstatus)
00567     {
00568       return(errorstatus);
00569     }
00570   }
00571 
00572   if (SDIO_SECURE_DIGITAL_IO_CARD != CardType)
00573   {
00574     RCA = rca;
00575 
00576     /*!< Send CMD9 SEND_CSD with argument as card's RCA */
00577     SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)(rca << 16);
00578     SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_CSD;
00579     SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Long;
00580     SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
00581     SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
00582     SDIO_SendCommand(&SDIO_CmdInitStructure);
00583 
00584     errorstatus = CmdResp2Error();
00585 
00586     if (SD_OK != errorstatus)
00587     {
00588       return(errorstatus);
00589     }
00590 
00591     CSD_Tab[0] = SDIO_GetResponse(SDIO_RESP1);
00592     CSD_Tab[1] = SDIO_GetResponse(SDIO_RESP2);
00593     CSD_Tab[2] = SDIO_GetResponse(SDIO_RESP3);
00594     CSD_Tab[3] = SDIO_GetResponse(SDIO_RESP4);
00595   }
00596 
00597   errorstatus = SD_OK; /*!< All cards get intialized */
00598 
00599   return(errorstatus);
00600 }
00601 
00602 /**
00603   * @brief  Returns information about specific card.
00604   * @param  cardinfo: pointer to a SD_CardInfo structure that contains all SD card 
00605   *         information.
00606   * @retval SD_Error: SD Card Error code.
00607   */
00608 SD_Error SD_GetCardInfo(SD_CardInfo *cardinfo)
00609 {
00610   SD_Error errorstatus = SD_OK;
00611   uint8_t tmp = 0;
00612 
00613   cardinfo->CardType = (uint8_t)CardType;
00614   cardinfo->RCA = (uint16_t)RCA;
00615 
00616   /*!< Byte 0 */
00617   tmp = (uint8_t)((CSD_Tab[0] & 0xFF000000) >> 24);
00618   cardinfo->SD_csd.CSDStruct = (tmp & 0xC0) >> 6;
00619   cardinfo->SD_csd.SysSpecVersion = (tmp & 0x3C) >> 2;
00620   cardinfo->SD_csd.Reserved1 = tmp & 0x03;
00621 
00622   /*!< Byte 1 */
00623   tmp = (uint8_t)((CSD_Tab[0] & 0x00FF0000) >> 16);
00624   cardinfo->SD_csd.TAAC = tmp;
00625 
00626   /*!< Byte 2 */
00627   tmp = (uint8_t)((CSD_Tab[0] & 0x0000FF00) >> 8);
00628   cardinfo->SD_csd.NSAC = tmp;
00629 
00630   /*!< Byte 3 */
00631   tmp = (uint8_t)(CSD_Tab[0] & 0x000000FF);
00632   cardinfo->SD_csd.MaxBusClkFrec = tmp;
00633 
00634   /*!< Byte 4 */
00635   tmp = (uint8_t)((CSD_Tab[1] & 0xFF000000) >> 24);
00636   cardinfo->SD_csd.CardComdClasses = tmp << 4;
00637 
00638   /*!< Byte 5 */
00639   tmp = (uint8_t)((CSD_Tab[1] & 0x00FF0000) >> 16);
00640   cardinfo->SD_csd.CardComdClasses |= (tmp & 0xF0) >> 4;
00641   cardinfo->SD_csd.RdBlockLen = tmp & 0x0F;
00642 
00643   /*!< Byte 6 */
00644   tmp = (uint8_t)((CSD_Tab[1] & 0x0000FF00) >> 8);
00645   cardinfo->SD_csd.PartBlockRead = (tmp & 0x80) >> 7;
00646   cardinfo->SD_csd.WrBlockMisalign = (tmp & 0x40) >> 6;
00647   cardinfo->SD_csd.RdBlockMisalign = (tmp & 0x20) >> 5;
00648   cardinfo->SD_csd.DSRImpl = (tmp & 0x10) >> 4;
00649   cardinfo->SD_csd.Reserved2 = 0; /*!< Reserved */
00650 
00651   if ((CardType == SDIO_STD_CAPACITY_SD_CARD_V1_1) || (CardType == SDIO_STD_CAPACITY_SD_CARD_V2_0))
00652   {
00653     cardinfo->SD_csd.DeviceSize = (tmp & 0x03) << 10;
00654 
00655     /*!< Byte 7 */
00656     tmp = (uint8_t)(CSD_Tab[1] & 0x000000FF);
00657     cardinfo->SD_csd.DeviceSize |= (tmp) << 2;
00658 
00659     /*!< Byte 8 */
00660     tmp = (uint8_t)((CSD_Tab[2] & 0xFF000000) >> 24);
00661     cardinfo->SD_csd.DeviceSize |= (tmp & 0xC0) >> 6;
00662 
00663     cardinfo->SD_csd.MaxRdCurrentVDDMin = (tmp & 0x38) >> 3;
00664     cardinfo->SD_csd.MaxRdCurrentVDDMax = (tmp & 0x07);
00665 
00666     /*!< Byte 9 */
00667     tmp = (uint8_t)((CSD_Tab[2] & 0x00FF0000) >> 16);
00668     cardinfo->SD_csd.MaxWrCurrentVDDMin = (tmp & 0xE0) >> 5;
00669     cardinfo->SD_csd.MaxWrCurrentVDDMax = (tmp & 0x1C) >> 2;
00670     cardinfo->SD_csd.DeviceSizeMul = (tmp & 0x03) << 1;
00671     /*!< Byte 10 */
00672     tmp = (uint8_t)((CSD_Tab[2] & 0x0000FF00) >> 8);
00673     cardinfo->SD_csd.DeviceSizeMul |= (tmp & 0x80) >> 7;
00674     
00675     cardinfo->CardCapacity = (cardinfo->SD_csd.DeviceSize + 1) ;
00676     cardinfo->CardCapacity *= (1 << (cardinfo->SD_csd.DeviceSizeMul + 2));
00677     cardinfo->CardBlockSize = 1 << (cardinfo->SD_csd.RdBlockLen);
00678     cardinfo->CardCapacity *= cardinfo->CardBlockSize;
00679   }
00680   else if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
00681   {
00682     /*!< Byte 7 */
00683     tmp = (uint8_t)(CSD_Tab[1] & 0x000000FF);
00684     cardinfo->SD_csd.DeviceSize = (tmp & 0x3F) << 16;
00685 
00686     /*!< Byte 8 */
00687     tmp = (uint8_t)((CSD_Tab[2] & 0xFF000000) >> 24);
00688 
00689     cardinfo->SD_csd.DeviceSize |= (tmp << 8);
00690 
00691     /*!< Byte 9 */
00692     tmp = (uint8_t)((CSD_Tab[2] & 0x00FF0000) >> 16);
00693 
00694     cardinfo->SD_csd.DeviceSize |= (tmp);
00695 
00696     /*!< Byte 10 */
00697     tmp = (uint8_t)((CSD_Tab[2] & 0x0000FF00) >> 8);
00698     
00699     cardinfo->CardCapacity = (cardinfo->SD_csd.DeviceSize + 1) * 512 * 1024;
00700     cardinfo->CardBlockSize = 512;    
00701   }
00702 
00703 
00704   cardinfo->SD_csd.EraseGrSize = (tmp & 0x40) >> 6;
00705   cardinfo->SD_csd.EraseGrMul = (tmp & 0x3F) << 1;
00706 
00707   /*!< Byte 11 */
00708   tmp = (uint8_t)(CSD_Tab[2] & 0x000000FF);
00709   cardinfo->SD_csd.EraseGrMul |= (tmp & 0x80) >> 7;
00710   cardinfo->SD_csd.WrProtectGrSize = (tmp & 0x7F);
00711 
00712   /*!< Byte 12 */
00713   tmp = (uint8_t)((CSD_Tab[3] & 0xFF000000) >> 24);
00714   cardinfo->SD_csd.WrProtectGrEnable = (tmp & 0x80) >> 7;
00715   cardinfo->SD_csd.ManDeflECC = (tmp & 0x60) >> 5;
00716   cardinfo->SD_csd.WrSpeedFact = (tmp & 0x1C) >> 2;
00717   cardinfo->SD_csd.MaxWrBlockLen = (tmp & 0x03) << 2;
00718 
00719   /*!< Byte 13 */
00720   tmp = (uint8_t)((CSD_Tab[3] & 0x00FF0000) >> 16);
00721   cardinfo->SD_csd.MaxWrBlockLen |= (tmp & 0xC0) >> 6;
00722   cardinfo->SD_csd.WriteBlockPaPartial = (tmp & 0x20) >> 5;
00723   cardinfo->SD_csd.Reserved3 = 0;
00724   cardinfo->SD_csd.ContentProtectAppli = (tmp & 0x01);
00725 
00726   /*!< Byte 14 */
00727   tmp = (uint8_t)((CSD_Tab[3] & 0x0000FF00) >> 8);
00728   cardinfo->SD_csd.FileFormatGrouop = (tmp & 0x80) >> 7;
00729   cardinfo->SD_csd.CopyFlag = (tmp & 0x40) >> 6;
00730   cardinfo->SD_csd.PermWrProtect = (tmp & 0x20) >> 5;
00731   cardinfo->SD_csd.TempWrProtect = (tmp & 0x10) >> 4;
00732   cardinfo->SD_csd.FileFormat = (tmp & 0x0C) >> 2;
00733   cardinfo->SD_csd.ECC = (tmp & 0x03);
00734 
00735   /*!< Byte 15 */
00736   tmp = (uint8_t)(CSD_Tab[3] & 0x000000FF);
00737   cardinfo->SD_csd.CSD_CRC = (tmp & 0xFE) >> 1;
00738   cardinfo->SD_csd.Reserved4 = 1;
00739 
00740 
00741   /*!< Byte 0 */
00742   tmp = (uint8_t)((CID_Tab[0] & 0xFF000000) >> 24);
00743   cardinfo->SD_cid.ManufacturerID = tmp;
00744 
00745   /*!< Byte 1 */
00746   tmp = (uint8_t)((CID_Tab[0] & 0x00FF0000) >> 16);
00747   cardinfo->SD_cid.OEM_AppliID = tmp << 8;
00748 
00749   /*!< Byte 2 */
00750   tmp = (uint8_t)((CID_Tab[0] & 0x000000FF00) >> 8);
00751   cardinfo->SD_cid.OEM_AppliID |= tmp;
00752 
00753   /*!< Byte 3 */
00754   tmp = (uint8_t)(CID_Tab[0] & 0x000000FF);
00755   cardinfo->SD_cid.ProdName1 = tmp << 24;
00756 
00757   /*!< Byte 4 */
00758   tmp = (uint8_t)((CID_Tab[1] & 0xFF000000) >> 24);
00759   cardinfo->SD_cid.ProdName1 |= tmp << 16;
00760 
00761   /*!< Byte 5 */
00762   tmp = (uint8_t)((CID_Tab[1] & 0x00FF0000) >> 16);
00763   cardinfo->SD_cid.ProdName1 |= tmp << 8;
00764 
00765   /*!< Byte 6 */
00766   tmp = (uint8_t)((CID_Tab[1] & 0x0000FF00) >> 8);
00767   cardinfo->SD_cid.ProdName1 |= tmp;
00768 
00769   /*!< Byte 7 */
00770   tmp = (uint8_t)(CID_Tab[1] & 0x000000FF);
00771   cardinfo->SD_cid.ProdName2 = tmp;
00772 
00773   /*!< Byte 8 */
00774   tmp = (uint8_t)((CID_Tab[2] & 0xFF000000) >> 24);
00775   cardinfo->SD_cid.ProdRev = tmp;
00776 
00777   /*!< Byte 9 */
00778   tmp = (uint8_t)((CID_Tab[2] & 0x00FF0000) >> 16);
00779   cardinfo->SD_cid.ProdSN = tmp << 24;
00780 
00781   /*!< Byte 10 */
00782   tmp = (uint8_t)((CID_Tab[2] & 0x0000FF00) >> 8);
00783   cardinfo->SD_cid.ProdSN |= tmp << 16;
00784 
00785   /*!< Byte 11 */
00786   tmp = (uint8_t)(CID_Tab[2] & 0x000000FF);
00787   cardinfo->SD_cid.ProdSN |= tmp << 8;
00788 
00789   /*!< Byte 12 */
00790   tmp = (uint8_t)((CID_Tab[3] & 0xFF000000) >> 24);
00791   cardinfo->SD_cid.ProdSN |= tmp;
00792 
00793   /*!< Byte 13 */
00794   tmp = (uint8_t)((CID_Tab[3] & 0x00FF0000) >> 16);
00795   cardinfo->SD_cid.Reserved1 |= (tmp & 0xF0) >> 4;
00796   cardinfo->SD_cid.ManufactDate = (tmp & 0x0F) << 8;
00797 
00798   /*!< Byte 14 */
00799   tmp = (uint8_t)((CID_Tab[3] & 0x0000FF00) >> 8);
00800   cardinfo->SD_cid.ManufactDate |= tmp;
00801 
00802   /*!< Byte 15 */
00803   tmp = (uint8_t)(CID_Tab[3] & 0x000000FF);
00804   cardinfo->SD_cid.CID_CRC = (tmp & 0xFE) >> 1;
00805   cardinfo->SD_cid.Reserved2 = 1;
00806   
00807   return(errorstatus);
00808 }
00809 
00810 /**
00811   * @brief  Enables wide bus opeartion for the requeseted card if supported by 
00812   *         card.
00813   * @param  WideMode: Specifies the SD card wide bus mode. 
00814   *   This parameter can be one of the following values:
00815   *     @arg SDIO_BusWide_8b: 8-bit data transfer (Only for MMC)
00816   *     @arg SDIO_BusWide_4b: 4-bit data transfer
00817   *     @arg SDIO_BusWide_1b: 1-bit data transfer
00818   * @retval SD_Error: SD Card Error code.
00819   */
00820 SD_Error SD_EnableWideBusOperation(uint32_t WideMode)
00821 {
00822   SD_Error errorstatus = SD_OK;
00823 
00824   /*!< MMC Card doesn't support this feature */
00825   if (SDIO_MULTIMEDIA_CARD == CardType)
00826   {
00827     errorstatus = SD_UNSUPPORTED_FEATURE;
00828     return(errorstatus);
00829   }
00830   else if ((SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType) || (SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType) || (SDIO_HIGH_CAPACITY_SD_CARD == CardType))
00831   {
00832     if (SDIO_BusWide_8b == WideMode)
00833     {
00834       errorstatus = SD_UNSUPPORTED_FEATURE;
00835       return(errorstatus);
00836     }
00837     else if (SDIO_BusWide_4b == WideMode)
00838     {
00839       errorstatus = SDEnWideBus(ENABLE);
00840 
00841       if (SD_OK == errorstatus)
00842       {
00843         /*!< Configure the SDIO peripheral */
00844         SDIO_InitStructure.SDIO_ClockDiv = SDIO_TRANSFER_CLK_DIV; 
00845         SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_Rising;
00846         SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_Disable;
00847         SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable;
00848         SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_4b;
00849         SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable;
00850         SDIO_Init(&SDIO_InitStructure);
00851       }
00852     }
00853     else
00854     {
00855       errorstatus = SDEnWideBus(DISABLE);
00856 
00857       if (SD_OK == errorstatus)
00858       {
00859         /*!< Configure the SDIO peripheral */
00860         SDIO_InitStructure.SDIO_ClockDiv = SDIO_TRANSFER_CLK_DIV; 
00861         SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_Rising;
00862         SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_Disable;
00863         SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable;
00864         SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_1b;
00865         SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable;
00866         SDIO_Init(&SDIO_InitStructure);
00867       }
00868     }
00869   }
00870 
00871   return(errorstatus);
00872 }
00873 
00874 /**
00875   * @brief  Sets device mode whether to operate in Polling, Interrupt or DMA mode.
00876   * @param  Mode: Specifies the Data Transfer mode.
00877   *   This parameter can be one of the following values:
00878   *     @arg SD_DMA_MODE: Data transfer using DMA.
00879   *     @arg SD_INTERRUPT_MODE: Data transfer using interrupts.
00880   *     @arg SD_POLLING_MODE: Data transfer using flags.
00881   * @retval SD_Error: SD Card Error code.
00882   */
00883 SD_Error SD_SetDeviceMode(uint32_t Mode)
00884 {
00885   SD_Error errorstatus = SD_OK;
00886 
00887   if ((Mode == SD_DMA_MODE) || (Mode == SD_INTERRUPT_MODE) || (Mode == SD_POLLING_MODE))
00888   {
00889     DeviceMode = Mode;
00890   }
00891   else
00892   {
00893     errorstatus = SD_INVALID_PARAMETER;
00894   }
00895   return(errorstatus);
00896 
00897 }
00898 
00899 /**
00900   * @brief  Selects od Deselects the corresponding card.
00901   * @param  addr: Address of the Card to be selected.
00902   * @retval SD_Error: SD Card Error code.
00903   */
00904 SD_Error SD_SelectDeselect(uint32_t addr)
00905 {
00906   SD_Error errorstatus = SD_OK;
00907 
00908   /*!< Send CMD7 SDIO_SEL_DESEL_CARD */
00909   SDIO_CmdInitStructure.SDIO_Argument =  addr;
00910   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEL_DESEL_CARD;
00911   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
00912   SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
00913   SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
00914   SDIO_SendCommand(&SDIO_CmdInitStructure);
00915 
00916   errorstatus = CmdResp1Error(SD_CMD_SEL_DESEL_CARD);
00917 
00918   return(errorstatus);
00919 }
00920 
00921 /**
00922   * @brief  Allows to read one block from a specified address in a card.
00923   * @param  readbuff: pointer to the buffer that will contain the received data
00924   * @param  ReadAddr: Address from where data are to be read.  
00925   * @param  BlockSize: the SD card Data block size.
00926   * @retval SD_Error: SD Card Error code.
00927   */
00928 SD_Error SD_ReadBlock(uint8_t *readbuff, uint32_t ReadAddr, uint16_t BlockSize)
00929 {
00930   SD_Error errorstatus = SD_OK;
00931   uint32_t count = 0, *tempbuff = (uint32_t *)readbuff;
00932   uint8_t power = 0;
00933 
00934   if (NULL == readbuff)
00935   {
00936     errorstatus = SD_INVALID_PARAMETER;
00937     return(errorstatus);
00938   }
00939 
00940   TransferError = SD_OK;
00941   TransferEnd = 0;
00942   TotalNumberOfBytes = 0;
00943 
00944   /*!< Clear all DPSM configuration */
00945   SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
00946   SDIO_DataInitStructure.SDIO_DataLength = 0;
00947   SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_1b;
00948   SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToCard;
00949   SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
00950   SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Disable;
00951   SDIO_DataConfig(&SDIO_DataInitStructure);
00952   SDIO_DMACmd(DISABLE);
00953 
00954   if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED)
00955   {
00956     errorstatus = SD_LOCK_UNLOCK_FAILED;
00957     return(errorstatus);
00958   }
00959   
00960   if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
00961   {
00962     BlockSize = 512;
00963     ReadAddr /= 512;
00964   }
00965   if ((BlockSize > 0) && (BlockSize <= 2048) && ((BlockSize & (BlockSize - 1)) == 0))
00966   {
00967     power = convert_from_bytes_to_power_of_two(BlockSize);
00968 
00969     /*!< Set Block Size for Card */
00970     SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) BlockSize;
00971     SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
00972     SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
00973     SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
00974     SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
00975     SDIO_SendCommand(&SDIO_CmdInitStructure);
00976 
00977     errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN);
00978 
00979     if (SD_OK != errorstatus)
00980     {
00981       return(errorstatus);
00982     }
00983   }
00984   else
00985   {
00986     errorstatus = SD_INVALID_PARAMETER;
00987     return(errorstatus);
00988   }
00989 
00990   SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
00991   SDIO_DataInitStructure.SDIO_DataLength = BlockSize;
00992   SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t) power << 4;
00993   SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO;
00994   SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
00995   SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
00996   SDIO_DataConfig(&SDIO_DataInitStructure);
00997 
00998   TotalNumberOfBytes = BlockSize;
00999   StopCondition = 0;
01000   DestBuffer = (uint32_t *)readbuff;
01001 
01002   /*!< Send CMD17 READ_SINGLE_BLOCK */
01003   SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)ReadAddr;
01004   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_READ_SINGLE_BLOCK;
01005   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
01006   SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
01007   SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
01008   SDIO_SendCommand(&SDIO_CmdInitStructure);
01009 
01010   errorstatus = CmdResp1Error(SD_CMD_READ_SINGLE_BLOCK);
01011 
01012   if (errorstatus != SD_OK)
01013   {
01014     return(errorstatus);
01015   }
01016   /*!< In case of single block transfer, no need of stop transfer at all.*/
01017   if (DeviceMode == SD_POLLING_MODE)
01018   {
01019     /*!< Polling mode */
01020     while (!(SDIO->STA &(SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR)))
01021     {
01022       if (SDIO_GetFlagStatus(SDIO_FLAG_RXFIFOHF) != RESET)
01023       {
01024         for (count = 0; count < 8; count++)
01025         {
01026           *(tempbuff + count) = SDIO_ReadData();
01027         }
01028         tempbuff += 8;
01029       }
01030     }
01031 
01032     if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET)
01033     {
01034       SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT);
01035       errorstatus = SD_DATA_TIMEOUT;
01036       return(errorstatus);
01037     }
01038     else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET)
01039     {
01040       SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL);
01041       errorstatus = SD_DATA_CRC_FAIL;
01042       return(errorstatus);
01043     }
01044     else if (SDIO_GetFlagStatus(SDIO_FLAG_RXOVERR) != RESET)
01045     {
01046       SDIO_ClearFlag(SDIO_FLAG_RXOVERR);
01047       errorstatus = SD_RX_OVERRUN;
01048       return(errorstatus);
01049     }
01050     else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET)
01051     {
01052       SDIO_ClearFlag(SDIO_FLAG_STBITERR);
01053       errorstatus = SD_START_BIT_ERR;
01054       return(errorstatus);
01055     }
01056     while (SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET)
01057     {
01058       *tempbuff = SDIO_ReadData();
01059       tempbuff++;
01060     }
01061 
01062     /*!< Clear all the static flags */
01063     SDIO_ClearFlag(SDIO_STATIC_FLAGS);
01064   }
01065   else if (DeviceMode == SD_INTERRUPT_MODE)
01066   {
01067     SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_RXFIFOHF | SDIO_IT_STBITERR, ENABLE);
01068     while ((TransferEnd == 0) && (TransferError == SD_OK))
01069     {}
01070     if (TransferError != SD_OK)
01071     {
01072       return(TransferError);
01073     }
01074   }
01075   else if (DeviceMode == SD_DMA_MODE)
01076   {
01077     SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_STBITERR, ENABLE);
01078     SDIO_DMACmd(ENABLE);
01079     SD_LowLevel_DMA_RxConfig((uint32_t *)readbuff, BlockSize);
01080     while ((SD_DMAEndOfTransferStatus() == RESET) && (TransferEnd == 0) && (TransferError == SD_OK))
01081     {}
01082     if (TransferError != SD_OK)
01083     {
01084       return(TransferError);
01085     }
01086   }
01087   return(errorstatus);
01088 }
01089 
01090 /**
01091   * @brief  Allows to read blocks from a specified address  in a card.
01092   * @param  readbuff: pointer to the buffer that will contain the received data.
01093   * @param  ReadAddr: Address from where data are to be read.
01094   * @param  BlockSize: the SD card Data block size.
01095   * @param  NumberOfBlocks: number of blocks to be read.
01096   * @retval SD_Error: SD Card Error code.
01097   */
01098 SD_Error SD_ReadMultiBlocks(uint8_t *readbuff, uint32_t ReadAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)
01099 {
01100   SD_Error errorstatus = SD_OK;
01101   uint32_t count = 0, *tempbuff = (uint32_t *)readbuff;
01102   uint8_t power = 0;
01103 
01104   if (NULL == readbuff)
01105   {
01106     errorstatus = SD_INVALID_PARAMETER;
01107     return(errorstatus);
01108   }
01109 
01110   TransferError = SD_OK;
01111   TransferEnd = 0;
01112   TotalNumberOfBytes = 0;
01113 
01114   /*!< Clear all DPSM configuration */
01115   SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
01116   SDIO_DataInitStructure.SDIO_DataLength = 0;
01117   SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_1b;
01118   SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToCard;
01119   SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
01120   SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Disable;
01121   SDIO_DataConfig(&SDIO_DataInitStructure);
01122   SDIO_DMACmd(DISABLE);
01123 
01124   if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED)
01125   {
01126     errorstatus = SD_LOCK_UNLOCK_FAILED;
01127     return(errorstatus);
01128   }
01129 
01130   if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
01131   {
01132     BlockSize = 512;
01133     ReadAddr /= 512;
01134   }
01135   
01136   if ((BlockSize > 0) && (BlockSize <= 2048) && (0 == (BlockSize & (BlockSize - 1))))
01137   {
01138     power = convert_from_bytes_to_power_of_two(BlockSize);
01139 
01140     /*!< Set Block Size for Card */
01141     SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) BlockSize;
01142     SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
01143     SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
01144     SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
01145     SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
01146     SDIO_SendCommand(&SDIO_CmdInitStructure);
01147 
01148     errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN);
01149 
01150     if (SD_OK != errorstatus)
01151     {
01152       return(errorstatus);
01153     }
01154   }
01155   else
01156   {
01157     errorstatus = SD_INVALID_PARAMETER;
01158     return(errorstatus);
01159   }
01160 
01161   if (NumberOfBlocks > 1)
01162   {
01163     /*!< Common to all modes */
01164     if (NumberOfBlocks * BlockSize > SD_MAX_DATA_LENGTH)
01165     {
01166       errorstatus = SD_INVALID_PARAMETER;
01167       return(errorstatus);
01168     }
01169 
01170     TotalNumberOfBytes = NumberOfBlocks * BlockSize;
01171     StopCondition = 1;
01172     DestBuffer = (uint32_t *)readbuff;
01173 
01174     SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
01175     SDIO_DataInitStructure.SDIO_DataLength = NumberOfBlocks * BlockSize;
01176     SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t) power << 4;
01177     SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO;
01178     SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
01179     SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
01180     SDIO_DataConfig(&SDIO_DataInitStructure);
01181 
01182     /*!< Send CMD18 READ_MULT_BLOCK with argument data address */
01183     SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)ReadAddr;
01184     SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_READ_MULT_BLOCK;
01185     SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
01186     SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
01187     SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
01188     SDIO_SendCommand(&SDIO_CmdInitStructure);
01189 
01190     errorstatus = CmdResp1Error(SD_CMD_READ_MULT_BLOCK);
01191 
01192     if (errorstatus != SD_OK)
01193     {
01194       return(errorstatus);
01195     }
01196 
01197     if (DeviceMode == SD_POLLING_MODE)
01198     {
01199       /*!< Polling mode */
01200       while (!(SDIO->STA &(SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DATAEND | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_STBITERR)))
01201       {
01202         if (SDIO_GetFlagStatus(SDIO_FLAG_RXFIFOHF) != RESET)
01203         {
01204           for (count = 0; count < SD_HALFFIFO; count++)
01205           {
01206             *(tempbuff + count) = SDIO_ReadData();
01207           }
01208           tempbuff += SD_HALFFIFO;
01209         }
01210       }
01211 
01212       if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET)
01213       {
01214         SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT);
01215         errorstatus = SD_DATA_TIMEOUT;
01216         return(errorstatus);
01217       }
01218       else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET)
01219       {
01220         SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL);
01221         errorstatus = SD_DATA_CRC_FAIL;
01222         return(errorstatus);
01223       }
01224       else if (SDIO_GetFlagStatus(SDIO_FLAG_RXOVERR) != RESET)
01225       {
01226         SDIO_ClearFlag(SDIO_FLAG_RXOVERR);
01227         errorstatus = SD_RX_OVERRUN;
01228         return(errorstatus);
01229       }
01230       else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET)
01231       {
01232         SDIO_ClearFlag(SDIO_FLAG_STBITERR);
01233         errorstatus = SD_START_BIT_ERR;
01234         return(errorstatus);
01235       }
01236       while (SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET)
01237       {
01238         *tempbuff = SDIO_ReadData();
01239         tempbuff++;
01240       }
01241 
01242       if (SDIO_GetFlagStatus(SDIO_FLAG_DATAEND) != RESET)
01243       {
01244         /*!< In Case Of SD-CARD Send Command STOP_TRANSMISSION */
01245         if ((SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType) || (SDIO_HIGH_CAPACITY_SD_CARD == CardType) || (SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType))
01246         {
01247           /*!< Send CMD12 STOP_TRANSMISSION */
01248           SDIO_CmdInitStructure.SDIO_Argument = 0x0;
01249           SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_STOP_TRANSMISSION;
01250           SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
01251           SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
01252           SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
01253           SDIO_SendCommand(&SDIO_CmdInitStructure);
01254 
01255           errorstatus = CmdResp1Error(SD_CMD_STOP_TRANSMISSION);
01256 
01257           if (errorstatus != SD_OK)
01258           {
01259             return(errorstatus);
01260           }
01261         }
01262       }
01263       /*!< Clear all the static flags */
01264       SDIO_ClearFlag(SDIO_STATIC_FLAGS);
01265     }
01266     else if (DeviceMode == SD_INTERRUPT_MODE)
01267     {
01268       SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_RXFIFOHF | SDIO_IT_STBITERR, ENABLE);
01269       while ((TransferEnd == 0) && (TransferError == SD_OK))
01270       {}
01271       if (TransferError != SD_OK)
01272       {
01273         return(TransferError);
01274       }
01275     }
01276     else if (DeviceMode == SD_DMA_MODE)
01277     {
01278       SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_STBITERR, ENABLE);
01279       SDIO_DMACmd(ENABLE);
01280       SD_LowLevel_DMA_RxConfig((uint32_t *)readbuff, (NumberOfBlocks * BlockSize));
01281       while ((SD_DMAEndOfTransferStatus() == RESET) && (TransferEnd == 0) && (TransferError == SD_OK))
01282       {}
01283       if (TransferError != SD_OK)
01284       {
01285         return(TransferError);
01286       }
01287     }
01288   }
01289   return(errorstatus);
01290 }
01291 
01292 /**
01293   * @brief  Allows to write one block starting from a specified address in a card.
01294   * @param  writebuff: pointer to the buffer that contain the data to be transferred.
01295   * @param  WriteAddr: Address from where data are to be read.   
01296   * @param  BlockSize: the SD card Data block size.
01297   * @retval SD_Error: SD Card Error code.
01298   */
01299 SD_Error SD_WriteBlock(uint8_t *writebuff, uint32_t WriteAddr, uint16_t BlockSize)
01300 {
01301   SD_Error errorstatus = SD_OK;
01302   uint8_t  power = 0, cardstate = 0;
01303   uint32_t timeout = 0, bytestransferred = 0;
01304   uint32_t cardstatus = 0, count = 0, restwords = 0;
01305   uint32_t *tempbuff = (uint32_t *)writebuff;
01306 
01307   if (writebuff == NULL)
01308   {
01309     errorstatus = SD_INVALID_PARAMETER;
01310     return(errorstatus);
01311   }
01312 
01313   TransferError = SD_OK;
01314   TransferEnd = 0;
01315   TotalNumberOfBytes = 0;
01316 
01317   SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
01318   SDIO_DataInitStructure.SDIO_DataLength = 0;
01319   SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_1b;
01320   SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToCard;
01321   SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
01322   SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Disable;
01323   SDIO_DataConfig(&SDIO_DataInitStructure);
01324   SDIO_DMACmd(DISABLE);
01325 
01326   if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED)
01327   {
01328     errorstatus = SD_LOCK_UNLOCK_FAILED;
01329     return(errorstatus);
01330   }
01331 
01332   if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
01333   {
01334     BlockSize = 512;
01335     WriteAddr /= 512;
01336   }
01337   
01338   /*!< Set the block size, both on controller and card */
01339   if ((BlockSize > 0) && (BlockSize <= 2048) && ((BlockSize & (BlockSize - 1)) == 0))
01340   {
01341     power = convert_from_bytes_to_power_of_two(BlockSize);
01342 
01343     SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) BlockSize;
01344     SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
01345     SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
01346     SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
01347     SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
01348     SDIO_SendCommand(&SDIO_CmdInitStructure);
01349 
01350     errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN);
01351 
01352     if (errorstatus != SD_OK)
01353     {
01354       return(errorstatus);
01355     }
01356   }
01357   else
01358   {
01359     errorstatus = SD_INVALID_PARAMETER;
01360     return(errorstatus);
01361   }
01362 
01363   /*!< Wait till card is ready for data Added */
01364   SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) (RCA << 16);
01365   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_STATUS;
01366   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
01367   SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
01368   SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
01369   SDIO_SendCommand(&SDIO_CmdInitStructure);
01370 
01371   errorstatus = CmdResp1Error(SD_CMD_SEND_STATUS);
01372 
01373   if (errorstatus != SD_OK)
01374   {
01375     return(errorstatus);
01376   }
01377 
01378   cardstatus = SDIO_GetResponse(SDIO_RESP1);
01379 
01380   timeout = SD_DATATIMEOUT;
01381 
01382   while (((cardstatus & 0x00000100) == 0) && (timeout > 0))
01383   {
01384     timeout--;
01385     SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) (RCA << 16);
01386     SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_STATUS;
01387     SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
01388     SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
01389     SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
01390     SDIO_SendCommand(&SDIO_CmdInitStructure);
01391 
01392     errorstatus = CmdResp1Error(SD_CMD_SEND_STATUS);
01393 
01394     if (errorstatus != SD_OK)
01395     {
01396       return(errorstatus);
01397     }
01398     cardstatus = SDIO_GetResponse(SDIO_RESP1);
01399   }
01400 
01401   if (timeout == 0)
01402   {
01403     return(SD_ERROR);
01404   }
01405 
01406   /*!< Send CMD24 WRITE_SINGLE_BLOCK */
01407   SDIO_CmdInitStructure.SDIO_Argument = WriteAddr;
01408   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_WRITE_SINGLE_BLOCK;
01409   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
01410   SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
01411   SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
01412   SDIO_SendCommand(&SDIO_CmdInitStructure);
01413 
01414   errorstatus = CmdResp1Error(SD_CMD_WRITE_SINGLE_BLOCK);
01415 
01416   if (errorstatus != SD_OK)
01417   {
01418     return(errorstatus);
01419   }
01420 
01421   TotalNumberOfBytes = BlockSize;
01422   StopCondition = 0;
01423   SrcBuffer = (uint32_t *)writebuff;
01424 
01425   SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
01426   SDIO_DataInitStructure.SDIO_DataLength = BlockSize;
01427   SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t) power << 4;
01428   SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToCard;
01429   SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
01430   SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
01431   SDIO_DataConfig(&SDIO_DataInitStructure);
01432 
01433   /*!< In case of single data block transfer no need of stop command at all */
01434   if (DeviceMode == SD_POLLING_MODE)
01435   {
01436     while (!(SDIO->STA & (SDIO_FLAG_DBCKEND | SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_STBITERR)))
01437     {
01438       if (SDIO_GetFlagStatus(SDIO_FLAG_TXFIFOHE) != RESET)
01439       {
01440         if ((TotalNumberOfBytes - bytestransferred) < 32)
01441         {
01442           restwords = ((TotalNumberOfBytes - bytestransferred) % 4 == 0) ? ((TotalNumberOfBytes - bytestransferred) / 4) : (( TotalNumberOfBytes -  bytestransferred) / 4 + 1);
01443 
01444           for (count = 0; count < restwords; count++, tempbuff++, bytestransferred += 4)
01445           {
01446             SDIO_WriteData(*tempbuff);
01447           }
01448         }
01449         else
01450         {
01451           for (count = 0; count < 8; count++)
01452           {
01453             SDIO_WriteData(*(tempbuff + count));
01454           }
01455           tempbuff += 8;
01456           bytestransferred += 32;
01457         }
01458       }
01459     }
01460     if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET)
01461     {
01462       SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT);
01463       errorstatus = SD_DATA_TIMEOUT;
01464       return(errorstatus);
01465     }
01466     else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET)
01467     {
01468       SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL);
01469       errorstatus = SD_DATA_CRC_FAIL;
01470       return(errorstatus);
01471     }
01472     else if (SDIO_GetFlagStatus(SDIO_FLAG_TXUNDERR) != RESET)
01473     {
01474       SDIO_ClearFlag(SDIO_FLAG_TXUNDERR);
01475       errorstatus = SD_TX_UNDERRUN;
01476       return(errorstatus);
01477     }
01478     else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET)
01479     {
01480       SDIO_ClearFlag(SDIO_FLAG_STBITERR);
01481       errorstatus = SD_START_BIT_ERR;
01482       return(errorstatus);
01483     }
01484   }
01485   else if (DeviceMode == SD_INTERRUPT_MODE)
01486   {
01487     SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_FLAG_TXFIFOHE | SDIO_IT_TXUNDERR | SDIO_IT_STBITERR, ENABLE);
01488     while ((TransferEnd == 0) && (TransferError == SD_OK))
01489     {}
01490     if (TransferError != SD_OK)
01491     {
01492       return(TransferError);
01493     }
01494   }
01495   else if (DeviceMode == SD_DMA_MODE)
01496   {
01497     SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_TXUNDERR | SDIO_IT_STBITERR, ENABLE);
01498     SD_LowLevel_DMA_TxConfig((uint32_t *)writebuff, BlockSize);
01499     SDIO_DMACmd(ENABLE);
01500     while ((SD_DMAEndOfTransferStatus() == RESET) && (TransferEnd == 0) && (TransferError == SD_OK))
01501     {}
01502     if (TransferError != SD_OK)
01503     {
01504       return(TransferError);
01505     }
01506   }
01507 
01508   /*!< Clear all the static flags */
01509   SDIO_ClearFlag(SDIO_STATIC_FLAGS);
01510 
01511   /*!< Wait till the card is in programming state */
01512   errorstatus = IsCardProgramming(&cardstate);
01513 
01514   while ((errorstatus == SD_OK) && ((cardstate == SD_CARD_PROGRAMMING) || (cardstate == SD_CARD_RECEIVING)))
01515   {
01516     errorstatus = IsCardProgramming(&cardstate);
01517   }
01518 
01519   return(errorstatus);
01520 }
01521 
01522 /**
01523   * @brief  Allows to write blocks starting from a specified address in a card.
01524   * @param  WriteAddr: Address from where data are to be read.
01525   * @param  writebuff: pointer to the buffer that contain the data to be transferred.
01526   * @param  BlockSize: the SD card Data block size.
01527   * @param  NumberOfBlocks: number of blocks to be written.
01528   * @retval SD_Error: SD Card Error code.
01529   */
01530 SD_Error SD_WriteMultiBlocks(uint8_t *writebuff, uint32_t WriteAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)
01531 {
01532   SD_Error errorstatus = SD_OK;
01533   uint8_t  power = 0, cardstate = 0;
01534   uint32_t bytestransferred = 0;
01535   uint32_t restwords = 0;
01536   uint32_t *tempbuff = (uint32_t *)writebuff;
01537   __IO uint32_t count = 0;
01538   
01539   if (writebuff == NULL)
01540   {
01541     errorstatus = SD_INVALID_PARAMETER;
01542     return(errorstatus);
01543   }
01544 
01545   TransferError = SD_OK;
01546   TransferEnd = 0;
01547   TotalNumberOfBytes = 0;
01548 
01549   SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
01550   SDIO_DataInitStructure.SDIO_DataLength = 0;
01551   SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_1b;
01552   SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToCard;
01553   SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
01554   SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Disable;
01555   SDIO_DataConfig(&SDIO_DataInitStructure);
01556   SDIO_DMACmd(DISABLE);
01557 
01558   if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED)
01559   {
01560     errorstatus = SD_LOCK_UNLOCK_FAILED;
01561     return(errorstatus);
01562   }
01563 
01564   if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
01565   {
01566     BlockSize = 512;
01567     WriteAddr /= 512;
01568   }
01569   
01570   /*!< Set the block size, both on controller and card */
01571   if ((BlockSize > 0) && (BlockSize <= 2048) && ((BlockSize & (BlockSize - 1)) == 0))
01572   {
01573     power = convert_from_bytes_to_power_of_two(BlockSize);
01574 
01575     SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) BlockSize;
01576     SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
01577     SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
01578     SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
01579     SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
01580     SDIO_SendCommand(&SDIO_CmdInitStructure);
01581 
01582     errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN);
01583 
01584     if (errorstatus != SD_OK)
01585     {
01586       return(errorstatus);
01587     }
01588   }
01589   else
01590   {
01591     errorstatus = SD_INVALID_PARAMETER;
01592     return(errorstatus);
01593   }
01594 
01595   /*!< Wait till card is ready for data Added */
01596   SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) (RCA << 16);
01597   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_STATUS;
01598   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
01599   SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
01600   SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
01601   SDIO_SendCommand(&SDIO_CmdInitStructure);
01602 
01603   errorstatus = CmdResp1Error(SD_CMD_SEND_STATUS);
01604 
01605   if (errorstatus != SD_OK)
01606   {
01607     return(errorstatus);
01608   }
01609 
01610   if (NumberOfBlocks > 1)
01611   {
01612     /*!< Common to all modes */
01613     if (NumberOfBlocks * BlockSize > SD_MAX_DATA_LENGTH)
01614     {
01615       errorstatus = SD_INVALID_PARAMETER;
01616       return(errorstatus);
01617     }
01618 
01619     if ((SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType) || (SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType) || (SDIO_HIGH_CAPACITY_SD_CARD == CardType))
01620     {
01621       /*!< To improve performance */
01622       SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) (RCA << 16);
01623       SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
01624       SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
01625       SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
01626       SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
01627       SDIO_SendCommand(&SDIO_CmdInitStructure);
01628 
01629 
01630       errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
01631 
01632       if (errorstatus != SD_OK)
01633       {
01634         return(errorstatus);
01635       }
01636       /*!< To improve performance */
01637       SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)NumberOfBlocks;
01638       SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCK_COUNT;
01639       SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
01640       SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
01641       SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
01642       SDIO_SendCommand(&SDIO_CmdInitStructure);
01643 
01644       errorstatus = CmdResp1Error(SD_CMD_SET_BLOCK_COUNT);
01645 
01646       if (errorstatus != SD_OK)
01647       {
01648         return(errorstatus);
01649       }
01650     }
01651 
01652     /*!< Send CMD25 WRITE_MULT_BLOCK with argument data address */
01653     SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)WriteAddr;
01654     SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_WRITE_MULT_BLOCK;
01655     SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
01656     SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
01657     SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
01658     SDIO_SendCommand(&SDIO_CmdInitStructure);
01659 
01660     errorstatus = CmdResp1Error(SD_CMD_WRITE_MULT_BLOCK);
01661 
01662     if (SD_OK != errorstatus)
01663     {
01664       return(errorstatus);
01665     }
01666 
01667     TotalNumberOfBytes = NumberOfBlocks * BlockSize;
01668     StopCondition = 1;
01669     SrcBuffer = (uint32_t *)writebuff;
01670 
01671     SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
01672     SDIO_DataInitStructure.SDIO_DataLength = NumberOfBlocks * BlockSize;
01673     SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t) power << 4;
01674     SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToCard;
01675     SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
01676     SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
01677     SDIO_DataConfig(&SDIO_DataInitStructure);
01678 
01679     if (DeviceMode == SD_POLLING_MODE)
01680     {
01681       while (!(SDIO->STA & (SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DATAEND | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_STBITERR)))
01682       {
01683         if (SDIO_GetFlagStatus(SDIO_FLAG_TXFIFOHE) != RESET)
01684         {
01685           if (!((TotalNumberOfBytes - bytestransferred) < SD_HALFFIFOBYTES))
01686           {
01687             for (count = 0; count < SD_HALFFIFO; count++)
01688             {
01689               SDIO_WriteData(*(tempbuff + count));
01690             }
01691             tempbuff += SD_HALFFIFO;
01692             bytestransferred += SD_HALFFIFOBYTES;
01693           }
01694           else
01695           {
01696             restwords = ((TotalNumberOfBytes - bytestransferred) % 4 == 0) ? ((TotalNumberOfBytes - bytestransferred) / 4) :
01697                         ((TotalNumberOfBytes - bytestransferred) / 4 + 1);
01698 
01699             for (count = 0; count < restwords; count++, tempbuff++, bytestransferred += 4)
01700             {
01701               SDIO_WriteData(*tempbuff);
01702             }
01703           }
01704         }
01705       }
01706 
01707       if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET)
01708       {
01709         SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT);
01710         errorstatus = SD_DATA_TIMEOUT;
01711         return(errorstatus);
01712       }
01713       else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET)
01714       {
01715         SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL);
01716         errorstatus = SD_DATA_CRC_FAIL;
01717         return(errorstatus);
01718       }
01719       else if (SDIO_GetFlagStatus(SDIO_FLAG_TXUNDERR) != RESET)
01720       {
01721         SDIO_ClearFlag(SDIO_FLAG_TXUNDERR);
01722         errorstatus = SD_TX_UNDERRUN;
01723         return(errorstatus);
01724       }
01725       else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET)
01726       {
01727         SDIO_ClearFlag(SDIO_FLAG_STBITERR);
01728         errorstatus = SD_START_BIT_ERR;
01729         return(errorstatus);
01730       }
01731 
01732       if (SDIO_GetFlagStatus(SDIO_FLAG_DATAEND) != RESET)
01733       {
01734        if ((SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType) || (SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType) || (SDIO_HIGH_CAPACITY_SD_CARD == CardType))
01735         {
01736           /*!< Send CMD12 STOP_TRANSMISSION */
01737           SDIO_CmdInitStructure.SDIO_Argument = 0x0;
01738           SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_STOP_TRANSMISSION;
01739           SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
01740           SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
01741           SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
01742           SDIO_SendCommand(&SDIO_CmdInitStructure);
01743 
01744 
01745           errorstatus = CmdResp1Error(SD_CMD_STOP_TRANSMISSION);
01746 
01747           if (errorstatus != SD_OK)
01748           {
01749             return(errorstatus);
01750           }
01751         }
01752       }
01753     }
01754     else if (DeviceMode == SD_INTERRUPT_MODE)
01755     {
01756       SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_TXFIFOHE | SDIO_IT_TXUNDERR | SDIO_IT_STBITERR, ENABLE);
01757       while ((TransferEnd == 0) && (TransferError == SD_OK))
01758       {}
01759       if (TransferError != SD_OK)
01760       {
01761         return(TransferError);
01762       }
01763     }
01764     else if (DeviceMode == SD_DMA_MODE)
01765     {
01766       SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_TXUNDERR | SDIO_IT_STBITERR, ENABLE);
01767       SDIO_DMACmd(ENABLE);
01768       SD_LowLevel_DMA_TxConfig((uint32_t *)writebuff, (NumberOfBlocks * BlockSize));
01769       while ((SD_DMAEndOfTransferStatus() == RESET) && (TransferEnd == 0) && (TransferError == SD_OK))
01770       {}
01771       if (TransferError != SD_OK)
01772       {
01773         return(TransferError);
01774       }
01775     }
01776   }
01777   /*!< Clear all the static flags */
01778   SDIO_ClearFlag(SDIO_STATIC_FLAGS);
01779   
01780   /*!< Add some delay before checking the Card Status */
01781   for(count = 0; count < 0xFFFF; count++)
01782   {
01783   }
01784   /*!< Wait till the card is in programming state */
01785   errorstatus = IsCardProgramming(&cardstate);
01786 
01787   while ((errorstatus == SD_OK) && ((cardstate == SD_CARD_PROGRAMMING) || (cardstate == SD_CARD_RECEIVING)))
01788   {
01789     errorstatus = IsCardProgramming(&cardstate);
01790   }
01791 
01792   return(errorstatus);
01793 }
01794 
01795 /**
01796   * @brief  Gets the cuurent data transfer state.
01797   * @param  None
01798   * @retval SDTransferState: Data Transfer state.
01799   *   This value can be: 
01800   *        - SD_TRANSFER_OK: No data transfer is acting
01801   *        - SD_TRANSFER_BUSY: Data transfer is acting
01802   */
01803 SDTransferState SD_GetTransferState(void)
01804 {
01805   if (SDIO->STA & (SDIO_FLAG_TXACT | SDIO_FLAG_RXACT))
01806   {
01807     return(SD_TRANSFER_BUSY);
01808   }
01809   else
01810   {
01811     return(SD_TRANSFER_OK);
01812   }
01813 }
01814 
01815 /**
01816   * @brief  Aborts an ongoing data transfer.
01817   * @param  None
01818   * @retval SD_Error: SD Card Error code.
01819   */
01820 SD_Error SD_StopTransfer(void)
01821 {
01822   SD_Error errorstatus = SD_OK;
01823 
01824   /*!< Send CMD12 STOP_TRANSMISSION  */
01825   SDIO_CmdInitStructure.SDIO_Argument = 0x0;
01826   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_STOP_TRANSMISSION;
01827   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
01828   SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
01829   SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
01830   SDIO_SendCommand(&SDIO_CmdInitStructure);
01831 
01832   errorstatus = CmdResp1Error(SD_CMD_STOP_TRANSMISSION);
01833 
01834   return(errorstatus);
01835 }
01836 
01837 /**
01838   * @brief  Allows to erase memory area specified for the given card.
01839   * @param  startaddr: the start address.
01840   * @param  endaddr: the end address.
01841   * @retval SD_Error: SD Card Error code.
01842   */
01843 SD_Error SD_Erase(uint32_t startaddr, uint32_t endaddr)
01844 {
01845   SD_Error errorstatus = SD_OK;
01846   uint32_t delay = 0;
01847   __IO uint32_t maxdelay = 0;
01848   uint8_t cardstate = 0;
01849 
01850   /*!< Check if the card coomnd class supports erase command */
01851   if (((CSD_Tab[1] >> 20) & SD_CCCC_ERASE) == 0)
01852   {
01853     errorstatus = SD_REQUEST_NOT_APPLICABLE;
01854     return(errorstatus);
01855   }
01856 
01857   maxdelay = 120000 / ((SDIO->CLKCR & 0xFF) + 2);
01858 
01859   if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED)
01860   {
01861     errorstatus = SD_LOCK_UNLOCK_FAILED;
01862     return(errorstatus);
01863   }
01864 
01865   if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
01866   {
01867     startaddr /= 512;
01868     endaddr /= 512;
01869   }
01870   
01871   /*!< According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
01872   if ((SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType) || (SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType) || (SDIO_HIGH_CAPACITY_SD_CARD == CardType))
01873   {
01874     /*!< Send CMD32 SD_ERASE_GRP_START with argument as addr  */
01875     SDIO_CmdInitStructure.SDIO_Argument = startaddr;
01876     SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_ERASE_GRP_START;
01877     SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
01878     SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
01879     SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
01880     SDIO_SendCommand(&SDIO_CmdInitStructure);
01881 
01882     errorstatus = CmdResp1Error(SD_CMD_SD_ERASE_GRP_START);
01883     if (errorstatus != SD_OK)
01884     {
01885       return(errorstatus);
01886     }
01887 
01888     /*!< Send CMD33 SD_ERASE_GRP_END with argument as addr  */
01889     SDIO_CmdInitStructure.SDIO_Argument = endaddr;
01890     SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_ERASE_GRP_END;
01891     SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
01892     SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
01893     SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
01894     SDIO_SendCommand(&SDIO_CmdInitStructure);
01895 
01896     errorstatus = CmdResp1Error(SD_CMD_SD_ERASE_GRP_END);
01897     if (errorstatus != SD_OK)
01898     {
01899       return(errorstatus);
01900     }
01901   }
01902 
01903   /*!< Send CMD38 ERASE */
01904   SDIO_CmdInitStructure.SDIO_Argument = 0;
01905   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_ERASE;
01906   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
01907   SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
01908   SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
01909   SDIO_SendCommand(&SDIO_CmdInitStructure);
01910 
01911   errorstatus = CmdResp1Error(SD_CMD_ERASE);
01912 
01913   if (errorstatus != SD_OK)
01914   {
01915     return(errorstatus);
01916   }
01917 
01918   for (delay = 0; delay < maxdelay; delay++)
01919   {}
01920 
01921   /*!< Wait till the card is in programming state */
01922   errorstatus = IsCardProgramming(&cardstate);
01923 
01924   while ((errorstatus == SD_OK) && ((SD_CARD_PROGRAMMING == cardstate) || (SD_CARD_RECEIVING == cardstate)))
01925   {
01926     errorstatus = IsCardProgramming(&cardstate);
01927   }
01928 
01929   return(errorstatus);
01930 }
01931 
01932 /**
01933   * @brief  Returns the current card's status.
01934   * @param  pcardstatus: pointer to the buffer that will contain the SD card 
01935   *         status (Card Status register).
01936   * @retval SD_Error: SD Card Error code.
01937   */
01938 SD_Error SD_SendStatus(uint32_t *pcardstatus)
01939 {
01940   SD_Error errorstatus = SD_OK;
01941 
01942   if (pcardstatus == NULL)
01943   {
01944     errorstatus = SD_INVALID_PARAMETER;
01945     return(errorstatus);
01946   }
01947 
01948   SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16;
01949   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_STATUS;
01950   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
01951   SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
01952   SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
01953   SDIO_SendCommand(&SDIO_CmdInitStructure);
01954 
01955 
01956   errorstatus = CmdResp1Error(SD_CMD_SEND_STATUS);
01957 
01958   if (errorstatus != SD_OK)
01959   {
01960     return(errorstatus);
01961   }
01962 
01963   *pcardstatus = SDIO_GetResponse(SDIO_RESP1);
01964 
01965   return(errorstatus);
01966 }
01967 
01968 /**
01969   * @brief  Returns the current SD card's status.
01970   * @param  psdstatus: pointer to the buffer that will contain the SD card status 
01971   *         (SD Status register).
01972   * @retval SD_Error: SD Card Error code.
01973   */
01974 SD_Error SD_SendSDStatus(uint32_t *psdstatus)
01975 {
01976   SD_Error errorstatus = SD_OK;
01977   uint32_t count = 0;
01978 
01979   if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED)
01980   {
01981     errorstatus = SD_LOCK_UNLOCK_FAILED;
01982     return(errorstatus);
01983   }
01984 
01985   /*!< Set block size for card if it is not equal to current block size for card. */
01986   SDIO_CmdInitStructure.SDIO_Argument = 64;
01987   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
01988   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
01989   SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
01990   SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
01991   SDIO_SendCommand(&SDIO_CmdInitStructure);
01992 
01993   errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN);
01994 
01995   if (errorstatus != SD_OK)
01996   {
01997     return(errorstatus);
01998   }
01999 
02000   /*!< CMD55 */
02001   SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16;
02002   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
02003   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
02004   SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
02005   SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
02006   SDIO_SendCommand(&SDIO_CmdInitStructure);
02007   errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
02008 
02009   if (errorstatus != SD_OK)
02010   {
02011     return(errorstatus);
02012   }
02013 
02014   SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
02015   SDIO_DataInitStructure.SDIO_DataLength = 64;
02016   SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_64b;
02017   SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO;
02018   SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
02019   SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
02020   SDIO_DataConfig(&SDIO_DataInitStructure);
02021 
02022   /*!< Send ACMD13 SD_APP_STAUS  with argument as card's RCA.*/
02023   SDIO_CmdInitStructure.SDIO_Argument = 0;
02024   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_APP_STAUS;
02025   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
02026   SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
02027   SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
02028   SDIO_SendCommand(&SDIO_CmdInitStructure);
02029   errorstatus = CmdResp1Error(SD_CMD_SD_APP_STAUS);
02030 
02031   if (errorstatus != SD_OK)
02032   {
02033     return(errorstatus);
02034   }
02035 
02036   while (!(SDIO->STA &(SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR)))
02037   {
02038     if (SDIO_GetFlagStatus(SDIO_FLAG_RXFIFOHF) != RESET)
02039     {
02040       for (count = 0; count < 8; count++)
02041       {
02042         *(psdstatus + count) = SDIO_ReadData();
02043       }
02044       psdstatus += 8;
02045     }
02046   }
02047 
02048   if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET)
02049   {
02050     SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT);
02051     errorstatus = SD_DATA_TIMEOUT;
02052     return(errorstatus);
02053   }
02054   else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET)
02055   {
02056     SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL);
02057     errorstatus = SD_DATA_CRC_FAIL;
02058     return(errorstatus);
02059   }
02060   else if (SDIO_GetFlagStatus(SDIO_FLAG_RXOVERR) != RESET)
02061   {
02062     SDIO_ClearFlag(SDIO_FLAG_RXOVERR);
02063     errorstatus = SD_RX_OVERRUN;
02064     return(errorstatus);
02065   }
02066   else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET)
02067   {
02068     SDIO_ClearFlag(SDIO_FLAG_STBITERR);
02069     errorstatus = SD_START_BIT_ERR;
02070     return(errorstatus);
02071   }
02072 
02073   while (SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET)
02074   {
02075     *psdstatus = SDIO_ReadData();
02076     psdstatus++;
02077   }
02078 
02079   /*!< Clear all the static status flags*/
02080   SDIO_ClearFlag(SDIO_STATIC_FLAGS);
02081   psdstatus -= 16;
02082   for (count = 0; count < 16; count++)
02083   {
02084     psdstatus[count] = ((psdstatus[count] & SD_0TO7BITS) << 24) |((psdstatus[count] & SD_8TO15BITS) << 8) |
02085                        ((psdstatus[count] & SD_16TO23BITS) >> 8) |((psdstatus[count] & SD_24TO31BITS) >> 24);
02086   }
02087   return(errorstatus);
02088 }
02089 
02090 /**
02091   * @brief  Allows to process all the interrupts that are high.
02092   * @param  None
02093   * @retval SD_Error: SD Card Error code.
02094   */
02095 SD_Error SD_ProcessIRQSrc(void)
02096 {
02097   uint32_t count = 0, restwords = 0;
02098 
02099   if (DeviceMode == SD_INTERRUPT_MODE)
02100   {
02101     if (SDIO_GetITStatus(SDIO_IT_RXFIFOHF) != RESET)
02102     {
02103       for (count = 0; count < SD_HALFFIFO; count++)
02104       {
02105         *(DestBuffer + count) = SDIO_ReadData();
02106       }
02107       DestBuffer += SD_HALFFIFO;
02108       NumberOfBytes += SD_HALFFIFOBYTES;
02109     }
02110     else if (SDIO_GetITStatus(SDIO_IT_TXFIFOHE) != RESET)
02111     {
02112       if ((TotalNumberOfBytes - NumberOfBytes) < SD_HALFFIFOBYTES)
02113       {
02114         restwords = ((TotalNumberOfBytes - NumberOfBytes) %  4 == 0) ?
02115                     ((TotalNumberOfBytes - NumberOfBytes) / 4) :
02116                     ((TotalNumberOfBytes - NumberOfBytes) / 4 + 1);
02117 
02118         for (count = 0; count < restwords;  count++, SrcBuffer++, NumberOfBytes += 4)
02119         {
02120           SDIO_WriteData(*SrcBuffer);
02121         }
02122       }
02123       else
02124       {
02125         for (count = 0; count < SD_HALFFIFO; count++)
02126         {
02127           SDIO_WriteData(*(SrcBuffer + count));
02128         }
02129 
02130         SrcBuffer += SD_HALFFIFO;
02131         NumberOfBytes += SD_HALFFIFOBYTES;
02132       }
02133     }
02134   }
02135 
02136   if (SDIO_GetITStatus(SDIO_IT_DATAEND) != RESET)
02137   {
02138     if (DeviceMode != SD_DMA_MODE)
02139     {
02140       while ((SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET)  &&  (NumberOfBytes < TotalNumberOfBytes))
02141       {
02142         *DestBuffer = SDIO_ReadData();
02143         DestBuffer++;
02144         NumberOfBytes += 4;
02145       }
02146     }
02147 
02148     if (StopCondition == 1)
02149     {
02150       TransferError = SD_StopTransfer();
02151     }
02152     else
02153     {
02154       TransferError = SD_OK;
02155     }
02156     SDIO_ClearITPendingBit(SDIO_IT_DATAEND);
02157     SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |
02158                   SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR |
02159                   SDIO_IT_RXOVERR | SDIO_IT_STBITERR, DISABLE);
02160     TransferEnd = 1;
02161     NumberOfBytes = 0;
02162     return(TransferError);
02163   }
02164 
02165   if (SDIO_GetITStatus(SDIO_IT_DCRCFAIL) != RESET)
02166   {
02167     SDIO_ClearITPendingBit(SDIO_IT_DCRCFAIL);
02168     SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |
02169                   SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR |
02170                   SDIO_IT_RXOVERR | SDIO_IT_STBITERR, DISABLE);
02171     NumberOfBytes = 0;
02172     TransferError = SD_DATA_CRC_FAIL;
02173     return(SD_DATA_CRC_FAIL);
02174   }
02175 
02176   if (SDIO_GetITStatus(SDIO_IT_DTIMEOUT) != RESET)
02177   {
02178     SDIO_ClearITPendingBit(SDIO_IT_DTIMEOUT);
02179     SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |
02180                   SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR |
02181                   SDIO_IT_RXOVERR | SDIO_IT_STBITERR, DISABLE);
02182     NumberOfBytes = 0;
02183     TransferError = SD_DATA_TIMEOUT;
02184     return(SD_DATA_TIMEOUT);
02185   }
02186 
02187   if (SDIO_GetITStatus(SDIO_IT_RXOVERR) != RESET)
02188   {
02189     SDIO_ClearITPendingBit(SDIO_IT_RXOVERR);
02190     SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |
02191                   SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR |
02192                   SDIO_IT_RXOVERR | SDIO_IT_STBITERR, DISABLE);
02193     NumberOfBytes = 0;
02194     TransferError = SD_RX_OVERRUN;
02195     return(SD_RX_OVERRUN);
02196   }
02197 
02198   if (SDIO_GetITStatus(SDIO_IT_TXUNDERR) != RESET)
02199   {
02200     SDIO_ClearITPendingBit(SDIO_IT_TXUNDERR);
02201     SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |
02202                   SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR |
02203                   SDIO_IT_RXOVERR | SDIO_IT_STBITERR, DISABLE);
02204     NumberOfBytes = 0;
02205     TransferError = SD_TX_UNDERRUN;
02206     return(SD_TX_UNDERRUN);
02207   }
02208 
02209   if (SDIO_GetITStatus(SDIO_IT_STBITERR) != RESET)
02210   {
02211     SDIO_ClearITPendingBit(SDIO_IT_STBITERR);
02212     SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |
02213                   SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR |
02214                   SDIO_IT_RXOVERR | SDIO_IT_STBITERR, DISABLE);
02215     NumberOfBytes = 0;
02216     TransferError = SD_START_BIT_ERR;
02217     return(SD_START_BIT_ERR);
02218   }
02219 
02220   return(SD_OK);
02221 }
02222 
02223 /**
02224   * @brief  Checks for error conditions for CMD0.
02225   * @param  None
02226   * @retval SD_Error: SD Card Error code.
02227   */
02228 static SD_Error CmdError(void)
02229 {
02230   SD_Error errorstatus = SD_OK;
02231   uint32_t timeout;
02232 
02233   timeout = SDIO_CMD0TIMEOUT; /*!< 10000 */
02234 
02235   while ((timeout > 0) && (SDIO_GetFlagStatus(SDIO_FLAG_CMDSENT) == RESET))
02236   {
02237     timeout--;
02238   }
02239 
02240   if (timeout == 0)
02241   {
02242     errorstatus = SD_CMD_RSP_TIMEOUT;
02243     return(errorstatus);
02244   }
02245 
02246   /*!< Clear all the static flags */
02247   SDIO_ClearFlag(SDIO_STATIC_FLAGS);
02248 
02249   return(errorstatus);
02250 }
02251 
02252 /**
02253   * @brief  Checks for error conditions for R7 response.
02254   * @param  None
02255   * @retval SD_Error: SD Card Error code.
02256   */
02257 static SD_Error CmdResp7Error(void)
02258 {
02259   SD_Error errorstatus = SD_OK;
02260   uint32_t status;
02261   uint32_t timeout = SDIO_CMD0TIMEOUT;
02262 
02263   status = SDIO->STA;
02264 
02265   while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT)) && (timeout > 0))
02266   {
02267     timeout--;
02268     status = SDIO->STA;
02269   }
02270 
02271   if ((timeout == 0) || (status & SDIO_FLAG_CTIMEOUT))
02272   {
02273     /*!< Card is not V2.0 complient or card does not support the set voltage range */
02274     errorstatus = SD_CMD_RSP_TIMEOUT;
02275     SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
02276     return(errorstatus);
02277   }
02278 
02279   if (status & SDIO_FLAG_CMDREND)
02280   {
02281     /*!< Card is SD V2.0 compliant */
02282     errorstatus = SD_OK;
02283     SDIO_ClearFlag(SDIO_FLAG_CMDREND);
02284     return(errorstatus);
02285   }
02286   return(errorstatus);
02287 }
02288 
02289 /**
02290   * @brief  Checks for error conditions for R1 response.
02291   * @param  cmd: The sent command index.
02292   * @retval SD_Error: SD Card Error code.
02293   */
02294 static SD_Error CmdResp1Error(uint8_t cmd)
02295 {
02296   SD_Error errorstatus = SD_OK;
02297   uint32_t status;
02298   uint32_t response_r1;
02299 
02300   status = SDIO->STA;
02301 
02302   while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT)))
02303   {
02304     status = SDIO->STA;
02305   }
02306 
02307   if (status & SDIO_FLAG_CTIMEOUT)
02308   {
02309     errorstatus = SD_CMD_RSP_TIMEOUT;
02310     SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
02311     return(errorstatus);
02312   }
02313   else if (status & SDIO_FLAG_CCRCFAIL)
02314   {
02315     errorstatus = SD_CMD_CRC_FAIL;
02316     SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL);
02317     return(errorstatus);
02318   }
02319 
02320   /*!< Check response received is of desired command */
02321   if (SDIO_GetCommandResponse() != cmd)
02322   {
02323     errorstatus = SD_ILLEGAL_CMD;
02324     return(errorstatus);
02325   }
02326 
02327   /*!< Clear all the static flags */
02328   SDIO_ClearFlag(SDIO_STATIC_FLAGS);
02329 
02330   /*!< We have received response, retrieve it for analysis  */
02331   response_r1 = SDIO_GetResponse(SDIO_RESP1);
02332 
02333   if ((response_r1 & SD_OCR_ERRORBITS) == SD_ALLZERO)
02334   {
02335     return(errorstatus);
02336   }
02337 
02338   if (response_r1 & SD_OCR_ADDR_OUT_OF_RANGE)
02339   {
02340     return(SD_ADDR_OUT_OF_RANGE);
02341   }
02342 
02343   if (response_r1 & SD_OCR_ADDR_MISALIGNED)
02344   {
02345     return(SD_ADDR_MISALIGNED);
02346   }
02347 
02348   if (response_r1 & SD_OCR_BLOCK_LEN_ERR)
02349   {
02350     return(SD_BLOCK_LEN_ERR);
02351   }
02352 
02353   if (response_r1 & SD_OCR_ERASE_SEQ_ERR)
02354   {
02355     return(SD_ERASE_SEQ_ERR);
02356   }
02357 
02358   if (response_r1 & SD_OCR_BAD_ERASE_PARAM)
02359   {
02360     return(SD_BAD_ERASE_PARAM);
02361   }
02362 
02363   if (response_r1 & SD_OCR_WRITE_PROT_VIOLATION)
02364   {
02365     return(SD_WRITE_PROT_VIOLATION);
02366   }
02367 
02368   if (response_r1 & SD_OCR_LOCK_UNLOCK_FAILED)
02369   {
02370     return(SD_LOCK_UNLOCK_FAILED);
02371   }
02372 
02373   if (response_r1 & SD_OCR_COM_CRC_FAILED)
02374   {
02375     return(SD_COM_CRC_FAILED);
02376   }
02377 
02378   if (response_r1 & SD_OCR_ILLEGAL_CMD)
02379   {
02380     return(SD_ILLEGAL_CMD);
02381   }
02382 
02383   if (response_r1 & SD_OCR_CARD_ECC_FAILED)
02384   {
02385     return(SD_CARD_ECC_FAILED);
02386   }
02387 
02388   if (response_r1 & SD_OCR_CC_ERROR)
02389   {
02390     return(SD_CC_ERROR);
02391   }
02392 
02393   if (response_r1 & SD_OCR_GENERAL_UNKNOWN_ERROR)
02394   {
02395     return(SD_GENERAL_UNKNOWN_ERROR);
02396   }
02397 
02398   if (response_r1 & SD_OCR_STREAM_READ_UNDERRUN)
02399   {
02400     return(SD_STREAM_READ_UNDERRUN);
02401   }
02402 
02403   if (response_r1 & SD_OCR_STREAM_WRITE_OVERRUN)
02404   {
02405     return(SD_STREAM_WRITE_OVERRUN);
02406   }
02407 
02408   if (response_r1 & SD_OCR_CID_CSD_OVERWRIETE)
02409   {
02410     return(SD_CID_CSD_OVERWRITE);
02411   }
02412 
02413   if (response_r1 & SD_OCR_WP_ERASE_SKIP)
02414   {
02415     return(SD_WP_ERASE_SKIP);
02416   }
02417 
02418   if (response_r1 & SD_OCR_CARD_ECC_DISABLED)
02419   {
02420     return(SD_CARD_ECC_DISABLED);
02421   }
02422 
02423   if (response_r1 & SD_OCR_ERASE_RESET)
02424   {
02425     return(SD_ERASE_RESET);
02426   }
02427 
02428   if (response_r1 & SD_OCR_AKE_SEQ_ERROR)
02429   {
02430     return(SD_AKE_SEQ_ERROR);
02431   }
02432   return(errorstatus);
02433 }
02434 
02435 /**
02436   * @brief  Checks for error conditions for R3 (OCR) response.
02437   * @param  None
02438   * @retval SD_Error: SD Card Error code.
02439   */
02440 static SD_Error CmdResp3Error(void)
02441 {
02442   SD_Error errorstatus = SD_OK;
02443   uint32_t status;
02444 
02445   status = SDIO->STA;
02446 
02447   while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT)))
02448   {
02449     status = SDIO->STA;
02450   }
02451 
02452   if (status & SDIO_FLAG_CTIMEOUT)
02453   {
02454     errorstatus = SD_CMD_RSP_TIMEOUT;
02455     SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
02456     return(errorstatus);
02457   }
02458   /*!< Clear all the static flags */
02459   SDIO_ClearFlag(SDIO_STATIC_FLAGS);
02460   return(errorstatus);
02461 }
02462 
02463 /**
02464   * @brief  Checks for error conditions for R2 (CID or CSD) response.
02465   * @param  None
02466   * @retval SD_Error: SD Card Error code.
02467   */
02468 static SD_Error CmdResp2Error(void)
02469 {
02470   SD_Error errorstatus = SD_OK;
02471   uint32_t status;
02472 
02473   status = SDIO->STA;
02474 
02475   while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CTIMEOUT | SDIO_FLAG_CMDREND)))
02476   {
02477     status = SDIO->STA;
02478   }
02479 
02480   if (status & SDIO_FLAG_CTIMEOUT)
02481   {
02482     errorstatus = SD_CMD_RSP_TIMEOUT;
02483     SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
02484     return(errorstatus);
02485   }
02486   else if (status & SDIO_FLAG_CCRCFAIL)
02487   {
02488     errorstatus = SD_CMD_CRC_FAIL;
02489     SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL);
02490     return(errorstatus);
02491   }
02492 
02493   /*!< Clear all the static flags */
02494   SDIO_ClearFlag(SDIO_STATIC_FLAGS);
02495 
02496   return(errorstatus);
02497 }
02498 
02499 /**
02500   * @brief  Checks for error conditions for R6 (RCA) response.
02501   * @param  cmd: The sent command index.
02502   * @param  prca: pointer to the variable that will contain the SD card relative 
02503   *         address RCA. 
02504   * @retval SD_Error: SD Card Error code.
02505   */
02506 static SD_Error CmdResp6Error(uint8_t cmd, uint16_t *prca)
02507 {
02508   SD_Error errorstatus = SD_OK;
02509   uint32_t status;
02510   uint32_t response_r1;
02511 
02512   status = SDIO->STA;
02513 
02514   while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CTIMEOUT | SDIO_FLAG_CMDREND)))
02515   {
02516     status = SDIO->STA;
02517   }
02518 
02519   if (status & SDIO_FLAG_CTIMEOUT)
02520   {
02521     errorstatus = SD_CMD_RSP_TIMEOUT;
02522     SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
02523     return(errorstatus);
02524   }
02525   else if (status & SDIO_FLAG_CCRCFAIL)
02526   {
02527     errorstatus = SD_CMD_CRC_FAIL;
02528     SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL);
02529     return(errorstatus);
02530   }
02531 
02532   /*!< Check response received is of desired command */
02533   if (SDIO_GetCommandResponse() != cmd)
02534   {
02535     errorstatus = SD_ILLEGAL_CMD;
02536     return(errorstatus);
02537   }
02538 
02539   /*!< Clear all the static flags */
02540   SDIO_ClearFlag(SDIO_STATIC_FLAGS);
02541 
02542   /*!< We have received response, retrieve it.  */
02543   response_r1 = SDIO_GetResponse(SDIO_RESP1);
02544 
02545   if (SD_ALLZERO == (response_r1 & (SD_R6_GENERAL_UNKNOWN_ERROR | SD_R6_ILLEGAL_CMD | SD_R6_COM_CRC_FAILED)))
02546   {
02547     *prca = (uint16_t) (response_r1 >> 16);
02548     return(errorstatus);
02549   }
02550 
02551   if (response_r1 & SD_R6_GENERAL_UNKNOWN_ERROR)
02552   {
02553     return(SD_GENERAL_UNKNOWN_ERROR);
02554   }
02555 
02556   if (response_r1 & SD_R6_ILLEGAL_CMD)
02557   {
02558     return(SD_ILLEGAL_CMD);
02559   }
02560 
02561   if (response_r1 & SD_R6_COM_CRC_FAILED)
02562   {
02563     return(SD_COM_CRC_FAILED);
02564   }
02565 
02566   return(errorstatus);
02567 }
02568 
02569 /**
02570   * @brief  Enables or disables the SDIO wide bus mode.
02571   * @param  NewState: new state of the SDIO wide bus mode.
02572   *   This parameter can be: ENABLE or DISABLE.
02573   * @retval SD_Error: SD Card Error code.
02574   */
02575 static SD_Error SDEnWideBus(FunctionalState NewState)
02576 {
02577   SD_Error errorstatus = SD_OK;
02578 
02579   uint32_t scr[2] = {0, 0};
02580 
02581   if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED)
02582   {
02583     errorstatus = SD_LOCK_UNLOCK_FAILED;
02584     return(errorstatus);
02585   }
02586 
02587   /*!< Get SCR Register */
02588   errorstatus = FindSCR(RCA, scr);
02589 
02590   if (errorstatus != SD_OK)
02591   {
02592     return(errorstatus);
02593   }
02594 
02595   /*!< If wide bus operation to be enabled */
02596   if (NewState == ENABLE)
02597   {
02598     /*!< If requested card supports wide bus operation */
02599     if ((scr[1] & SD_WIDE_BUS_SUPPORT) != SD_ALLZERO)
02600     {
02601       /*!< Send CMD55 APP_CMD with argument as card's RCA.*/
02602       SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16;
02603       SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
02604       SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
02605       SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
02606       SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
02607       SDIO_SendCommand(&SDIO_CmdInitStructure);
02608 
02609       errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
02610 
02611       if (errorstatus != SD_OK)
02612       {
02613         return(errorstatus);
02614       }
02615 
02616       /*!< Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
02617       SDIO_CmdInitStructure.SDIO_Argument = 0x2;
02618       SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH;
02619       SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
02620       SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
02621       SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
02622       SDIO_SendCommand(&SDIO_CmdInitStructure);
02623 
02624       errorstatus = CmdResp1Error(SD_CMD_APP_SD_SET_BUSWIDTH);
02625 
02626       if (errorstatus != SD_OK)
02627       {
02628         return(errorstatus);
02629       }
02630       return(errorstatus);
02631     }
02632     else
02633     {
02634       errorstatus = SD_REQUEST_NOT_APPLICABLE;
02635       return(errorstatus);
02636     }
02637   }   /*!< If wide bus operation to be disabled */
02638   else
02639   {
02640     /*!< If requested card supports 1 bit mode operation */
02641     if ((scr[1] & SD_SINGLE_BUS_SUPPORT) != SD_ALLZERO)
02642     {
02643       /*!< Send CMD55 APP_CMD with argument as card's RCA.*/
02644       SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16;
02645       SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
02646       SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
02647       SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
02648       SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
02649       SDIO_SendCommand(&SDIO_CmdInitStructure);
02650 
02651 
02652       errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
02653 
02654       if (errorstatus != SD_OK)
02655       {
02656         return(errorstatus);
02657       }
02658 
02659       /*!< Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
02660       SDIO_CmdInitStructure.SDIO_Argument = 0x00;
02661       SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH;
02662       SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
02663       SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
02664       SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
02665       SDIO_SendCommand(&SDIO_CmdInitStructure);
02666 
02667       errorstatus = CmdResp1Error(SD_CMD_APP_SD_SET_BUSWIDTH);
02668 
02669       if (errorstatus != SD_OK)
02670       {
02671         return(errorstatus);
02672       }
02673 
02674       return(errorstatus);
02675     }
02676     else
02677     {
02678       errorstatus = SD_REQUEST_NOT_APPLICABLE;
02679       return(errorstatus);
02680     }
02681   }
02682 }
02683 
02684 /**
02685   * @brief  Checks if the SD card is in programming state.
02686   * @param  pstatus: pointer to the variable that will contain the SD card state.
02687   * @retval SD_Error: SD Card Error code.
02688   */
02689 static SD_Error IsCardProgramming(uint8_t *pstatus)
02690 {
02691   SD_Error errorstatus = SD_OK;
02692   __IO uint32_t respR1 = 0, status = 0;
02693 
02694   SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16;
02695   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_STATUS;
02696   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
02697   SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
02698   SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
02699   SDIO_SendCommand(&SDIO_CmdInitStructure);
02700 
02701   status = SDIO->STA;
02702   while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT)))
02703   {
02704     status = SDIO->STA;
02705   }
02706 
02707   if (status & SDIO_FLAG_CTIMEOUT)
02708   {
02709     errorstatus = SD_CMD_RSP_TIMEOUT;
02710     SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
02711     return(errorstatus);
02712   }
02713   else if (status & SDIO_FLAG_CCRCFAIL)
02714   {
02715     errorstatus = SD_CMD_CRC_FAIL;
02716     SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL);
02717     return(errorstatus);
02718   }
02719 
02720   status = (uint32_t)SDIO_GetCommandResponse();
02721 
02722   /*!< Check response received is of desired command */
02723   if (status != SD_CMD_SEND_STATUS)
02724   {
02725     errorstatus = SD_ILLEGAL_CMD;
02726     return(errorstatus);
02727   }
02728 
02729   /*!< Clear all the static flags */
02730   SDIO_ClearFlag(SDIO_STATIC_FLAGS);
02731 
02732 
02733   /*!< We have received response, retrieve it for analysis  */
02734   respR1 = SDIO_GetResponse(SDIO_RESP1);
02735 
02736   /*!< Find out card status */
02737   *pstatus = (uint8_t) ((respR1 >> 9) & 0x0000000F);
02738 
02739   if ((respR1 & SD_OCR_ERRORBITS) == SD_ALLZERO)
02740   {
02741     return(errorstatus);
02742   }
02743 
02744   if (respR1 & SD_OCR_ADDR_OUT_OF_RANGE)
02745   {
02746     return(SD_ADDR_OUT_OF_RANGE);
02747   }
02748 
02749   if (respR1 & SD_OCR_ADDR_MISALIGNED)
02750   {
02751     return(SD_ADDR_MISALIGNED);
02752   }
02753 
02754   if (respR1 & SD_OCR_BLOCK_LEN_ERR)
02755   {
02756     return(SD_BLOCK_LEN_ERR);
02757   }
02758 
02759   if (respR1 & SD_OCR_ERASE_SEQ_ERR)
02760   {
02761     return(SD_ERASE_SEQ_ERR);
02762   }
02763 
02764   if (respR1 & SD_OCR_BAD_ERASE_PARAM)
02765   {
02766     return(SD_BAD_ERASE_PARAM);
02767   }
02768 
02769   if (respR1 & SD_OCR_WRITE_PROT_VIOLATION)
02770   {
02771     return(SD_WRITE_PROT_VIOLATION);
02772   }
02773 
02774   if (respR1 & SD_OCR_LOCK_UNLOCK_FAILED)
02775   {
02776     return(SD_LOCK_UNLOCK_FAILED);
02777   }
02778 
02779   if (respR1 & SD_OCR_COM_CRC_FAILED)
02780   {
02781     return(SD_COM_CRC_FAILED);
02782   }
02783 
02784   if (respR1 & SD_OCR_ILLEGAL_CMD)
02785   {
02786     return(SD_ILLEGAL_CMD);
02787   }
02788 
02789   if (respR1 & SD_OCR_CARD_ECC_FAILED)
02790   {
02791     return(SD_CARD_ECC_FAILED);
02792   }
02793 
02794   if (respR1 & SD_OCR_CC_ERROR)
02795   {
02796     return(SD_CC_ERROR);
02797   }
02798 
02799   if (respR1 & SD_OCR_GENERAL_UNKNOWN_ERROR)
02800   {
02801     return(SD_GENERAL_UNKNOWN_ERROR);
02802   }
02803 
02804   if (respR1 & SD_OCR_STREAM_READ_UNDERRUN)
02805   {
02806     return(SD_STREAM_READ_UNDERRUN);
02807   }
02808 
02809   if (respR1 & SD_OCR_STREAM_WRITE_OVERRUN)
02810   {
02811     return(SD_STREAM_WRITE_OVERRUN);
02812   }
02813 
02814   if (respR1 & SD_OCR_CID_CSD_OVERWRIETE)
02815   {
02816     return(SD_CID_CSD_OVERWRITE);
02817   }
02818 
02819   if (respR1 & SD_OCR_WP_ERASE_SKIP)
02820   {
02821     return(SD_WP_ERASE_SKIP);
02822   }
02823 
02824   if (respR1 & SD_OCR_CARD_ECC_DISABLED)
02825   {
02826     return(SD_CARD_ECC_DISABLED);
02827   }
02828 
02829   if (respR1 & SD_OCR_ERASE_RESET)
02830   {
02831     return(SD_ERASE_RESET);
02832   }
02833 
02834   if (respR1 & SD_OCR_AKE_SEQ_ERROR)
02835   {
02836     return(SD_AKE_SEQ_ERROR);
02837   }
02838 
02839   return(errorstatus);
02840 }
02841 
02842 /**
02843   * @brief  Find the SD card SCR register value.
02844   * @param  rca: selected card address.
02845   * @param  pscr: pointer to the buffer that will contain the SCR value.
02846   * @retval SD_Error: SD Card Error code.
02847   */
02848 static SD_Error FindSCR(uint16_t rca, uint32_t *pscr)
02849 {
02850   uint32_t index = 0;
02851   SD_Error errorstatus = SD_OK;
02852   uint32_t tempscr[2] = {0, 0};
02853 
02854   /*!< Set Block Size To 8 Bytes */
02855   /*!< Send CMD55 APP_CMD with argument as card's RCA */
02856   SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)8;
02857   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
02858   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
02859   SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
02860   SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
02861   SDIO_SendCommand(&SDIO_CmdInitStructure);
02862 
02863   errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN);
02864 
02865   if (errorstatus != SD_OK)
02866   {
02867     return(errorstatus);
02868   }
02869 
02870   /*!< Send CMD55 APP_CMD with argument as card's RCA */
02871   SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16;
02872   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
02873   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
02874   SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
02875   SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
02876   SDIO_SendCommand(&SDIO_CmdInitStructure);
02877 
02878   errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
02879 
02880   if (errorstatus != SD_OK)
02881   {
02882     return(errorstatus);
02883   }
02884   SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
02885   SDIO_DataInitStructure.SDIO_DataLength = 8;
02886   SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_8b;
02887   SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO;
02888   SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
02889   SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
02890   SDIO_DataConfig(&SDIO_DataInitStructure);
02891 
02892 
02893   /*!< Send ACMD51 SD_APP_SEND_SCR with argument as 0 */
02894   SDIO_CmdInitStructure.SDIO_Argument = 0x0;
02895   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_APP_SEND_SCR;
02896   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
02897   SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
02898   SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
02899   SDIO_SendCommand(&SDIO_CmdInitStructure);
02900 
02901   errorstatus = CmdResp1Error(SD_CMD_SD_APP_SEND_SCR);
02902 
02903   if (errorstatus != SD_OK)
02904   {
02905     return(errorstatus);
02906   }
02907 
02908   while (!(SDIO->STA & (SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR)))
02909   {
02910     if (SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET)
02911     {
02912       *(tempscr + index) = SDIO_ReadData();
02913       index++;
02914     }
02915   }
02916 
02917   if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET)
02918   {
02919     SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT);
02920     errorstatus = SD_DATA_TIMEOUT;
02921     return(errorstatus);
02922   }
02923   else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET)
02924   {
02925     SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL);
02926     errorstatus = SD_DATA_CRC_FAIL;
02927     return(errorstatus);
02928   }
02929   else if (SDIO_GetFlagStatus(SDIO_FLAG_RXOVERR) != RESET)
02930   {
02931     SDIO_ClearFlag(SDIO_FLAG_RXOVERR);
02932     errorstatus = SD_RX_OVERRUN;
02933     return(errorstatus);
02934   }
02935   else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET)
02936   {
02937     SDIO_ClearFlag(SDIO_FLAG_STBITERR);
02938     errorstatus = SD_START_BIT_ERR;
02939     return(errorstatus);
02940   }
02941 
02942   /*!< Clear all the static flags */
02943   SDIO_ClearFlag(SDIO_STATIC_FLAGS);
02944 
02945   *(pscr + 1) = ((tempscr[0] & SD_0TO7BITS) << 24) | ((tempscr[0] & SD_8TO15BITS) << 8) | ((tempscr[0] & SD_16TO23BITS) >> 8) | ((tempscr[0] & SD_24TO31BITS) >> 24);
02946 
02947   *(pscr) = ((tempscr[1] & SD_0TO7BITS) << 24) | ((tempscr[1] & SD_8TO15BITS) << 8) | ((tempscr[1] & SD_16TO23BITS) >> 8) | ((tempscr[1] & SD_24TO31BITS) >> 24);
02948 
02949   return(errorstatus);
02950 }
02951 
02952 /**
02953   * @brief  Converts the number of bytes in power of two and returns the power.
02954   * @param  NumberOfBytes: number of bytes.
02955   * @retval None
02956   */
02957 static uint8_t convert_from_bytes_to_power_of_two(uint16_t NumberOfBytes)
02958 {
02959   uint8_t count = 0;
02960 
02961   while (NumberOfBytes != 1)
02962   {
02963     NumberOfBytes >>= 1;
02964     count++;
02965   }
02966   return(count);
02967 }
02968 
02969 /**
02970   * @}
02971   */
02972 
02973 /**
02974   * @}
02975   */
02976 
02977 /**
02978   * @}
02979   */
02980 
02981 /**
02982   * @}
02983   */
02984 
02985 /**
02986   * @}
02987   */  
02988 
02989 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
STM32F10x Standard Peripherals Library: Footer

 

 

 

      For complete documentation on STM32(CORTEX M3) 32-bit Microcontrollers platform visit  www.st.com/STM32