stm32 sdio sdcard fatfs


  1 /****************************************Copyright (c)****************************************************
  2  **
  3  **                                 http://www.powermcu.com
  4  **
  5  **--------------File Info---------------------------------------------------------------------------------
  6  ** File name:               diskio.c
  7  ** Descriptions:            The FATFS Diskio
  8  **
  9  **--------------------------------------------------------------------------------------------------------
 10  ** Created by:              AVRman
 11  ** Created date:            2010-11-4
 12  ** Version:                 v1.0
 13  ** Descriptions:            The original version
 14  **
 15  **--------------------------------------------------------------------------------------------------------
 16  ** Modified by:
 17  ** Modified date:
 18  ** Version:
 19  ** Descriptions:
 20  **
 21  *********************************************************************************************************/
 22 
 23 /* Includes ------------------------------------------------------------------*/
 24 #include "diskio.h"
 25 #include "sdio_sd.h"
 26 
 27 /* Private variables ---------------------------------------------------------*/
 28 extern SD_CardInfo SDCardInfo;
 29 
 30 DSTATUS disk_initialize ( BYTE drv /* Physical drive nmuber (0..) */ )
 31 {
 32   SD_Error Status;
 33 
 34   switch ( drv )
 35   {
 36   case 0:
 37     Status = SD_Init ( ); ;
 38     if ( Status == SD_OK )
 39     {
 40       return 0;
 41     }
 42     else
 43     {
 44       return STA_NOINIT;
 45     }
 46 
 47   case 1:
 48     return STA_NOINIT;
 49 
 50   case 2:
 51     return STA_NOINIT;
 52   }
 53   return STA_NOINIT;
 54 }
 55 
 56 /*-----------------------------------------------------------------------*/
 57 /* Return Disk Status                                                    */
 58 
 59 DSTATUS disk_status ( BYTE drv /* Physical drive nmuber (0..) */ )
 60 {
 61   switch ( drv )
 62   {
 63   case 0:
 64 
 65     /* translate the reslut code here    */
 66 
 67     return 0;
 68 
 69   case 1:
 70 
 71     /* translate the reslut code here    */
 72 
 73     return 0;
 74 
 75   case 2:
 76 
 77     /* translate the reslut code here    */
 78 
 79     return 0;
 80 
 81   default:
 82 
 83     break;
 84   }
 85   return STA_NOINIT;
 86 }
 87 
 88 /*-----------------------------------------------------------------------*/
 89 /* Read Sector(s)                                                        */
 90 
 91 DRESULT disk_read ( BYTE drv, /* Physical drive nmuber (0..) */ BYTE * buff,
 92   /* Data buffer to store read data */ DWORD sector,
 93   /* Sector address (LBA) */ BYTE count
 94   /* Number of sectors to read (1..255) */ )
 95 {
 96   SD_Error Status;
 97   if ( !count )
 98   {
 99     return RES_PARERR; /* count不能等于0,否则返回参数错误 */
100   }
101 
102   switch ( drv )
103   {
104 
105   case 0:
106     if ( count == 1 ) /* 1个sector的读操作 */
107     {
108       Status = SD_ReadBlock ( buff, sector << 9, SDCardInfo.CardBlockSize );
109     }
110     else /* 多个sector的读操作 */
111     {
112       Status = SD_ReadMultiBlocks ( buff, sector << 9, SDCardInfo.CardBlockSize,
113         count );
114     }
115     if ( Status == SD_OK )
116     {
117       return RES_OK;
118     }
119     else
120     {
121       return RES_ERROR;
122     }
123 
124   case 1:
125     break;
126 
127   case 2:
128     break;
129 
130   default:
131     break;
132 
133   }
134 
135   return RES_ERROR;
136 }
137 
138 /*-----------------------------------------------------------------------*/
139 /* Write Sector(s)                                                       */
140 
141 #if _READONLY == 0
142 
143 DRESULT disk_write ( BYTE drv,
144   /* Physical drive nmuber (0..) */ const BYTE * buff,
145   /* Data to be written */ DWORD sector,
146   /* Sector address (LBA) */ BYTE count
147   /* Number of sectors to write (1..255) */ )
148 {
149   SD_Error Status;
150   if ( !count )
151   {
152     return RES_PARERR; /* count不能等于0,否则返回参数错误 */
153   }
154 
155   switch ( drv )
156   {
157   case 0:
158     if ( count == 1 ) /* 1个sector的写操作 */
159     {
160       Status = SD_WriteBlock ( ( uint8_t* )( &buff[ 0 ] ), sector << 9,
161         SDCardInfo.CardBlockSize );
162     }
163     else /* 多个sector的写操作 */
164     {
165       Status = SD_WriteMultiBlocks ( ( uint8_t* )( &buff[ 0 ] ), sector << 9,
166         SDCardInfo.CardBlockSize, count );
167     }
168     if ( Status == SD_OK )
169     {
170       return RES_OK;
171     }
172     else
173     {
174       return RES_ERROR;
175     }
176   case 2:
177     break;
178   default:
179     break;
180   }
181   return RES_ERROR;
182 }
183 #endif /* _READONLY */
184 
185 /*-----------------------------------------------------------------------*/
186 /* Miscellaneous Functions                                               */
187 
188 DRESULT disk_ioctl ( BYTE drv, /* Physical drive nmuber (0..) */ BYTE ctrl,
189   /* Control code */ void * buff /* Buffer to send/receive control data */ )
190 {
191   if ( drv )
192   {
193     return RES_PARERR; /* 仅支持单磁盘操作,否则返回参数错误 */
194   }
195   switch ( ctrl )
196   {
197   case CTRL_SYNC:
198 
199     return RES_OK;
200 
201   case GET_SECTOR_COUNT:
202     *( DWORD* )buff = SDCardInfo.CardCapacity / SDCardInfo.CardBlockSize;
203     return RES_OK;
204 
205   case GET_BLOCK_SIZE:
206     *( WORD* )buff = SDCardInfo.CardBlockSize;
207     return RES_OK;
208 
209   case CTRL_POWER:
210     break;
211 
212   case CTRL_LOCK:
213     break;
214 
215   case CTRL_EJECT:
216     break;
217 
218     /* MMC/SDC command */
219   case MMC_GET_TYPE:
220     break;
221 
222   case MMC_GET_CSD:
223     break;
224 
225   case MMC_GET_CID:
226     break;
227 
228   case MMC_GET_OCR:
229     break;
230 
231   case MMC_GET_SDSTAT:
232     break;
233   }
234   return RES_PARERR;
235 }
236 
237 /* 得到文件Calendar格式的建立日期,是DWORD get_fattime (void) 逆变换 */
238 /*-----------------------------------------------------------------------*/
239 /* User defined function to give a current time to fatfs module          */
240 /* 31-25: Year(0-127 org.1980), 24-21: Month(1-12), 20-16: Day(1-31) */
241 /* 15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */
242 DWORD get_fattime ( void )
243 {
244 
245   return 0;
246 }
247 
248 /*********************************************************************************************************
249  END FILE
250  *********************************************************************************************************/

 

   1 /****************************************Copyright (c)****************************************************
   2  **
   3  **                                 http://www.powermcu.com
   4  **
   5  **--------------File Info---------------------------------------------------------------------------------
   6  ** File name:               sdio_sd.h
   7  ** Descriptions:            sdio sd卡操作函数
   8  **
   9  **--------------------------------------------------------------------------------------------------------
  10  ** Created by:              AVRman
  11  ** Created date:            2010-10-30
  12  ** Version:                 v1.0
  13  ** Descriptions:            The original version
  14  **
  15  **--------------------------------------------------------------------------------------------------------
  16  ** Modified by:
  17  ** Modified date:
  18  ** Version:
  19  ** Descriptions:
  20  **
  21  *********************************************************************************************************/
  22 
  23 /******************************************************************************
  24  *          +-----------------------------------------------------------+
  25  *          |                     Pin assignment                        |
  26  *          +-----------------------------+---------------+-------------+
  27  *          |  STM32 SDIO Pins            |     SD        |    Pin      |
  28  *          +-----------------------------+---------------+-------------+
  29  *          |      SDIO D2                |   D2          |    1        |
  30  *          |      SDIO D3                |   D3          |    2        |
  31  *          |      SDIO CMD               |   CMD         |    3        |
  32  *          |                             |   VCC         |    4 (3.3 V)|
  33  *          |      SDIO CK               |    CLK         |    5        |
  34  *          |                             |   GND         |    6 (0 V)  |
  35  *          |      SDIO D0                |   D0          |    7        |
  36  *          |      SDIO D1                |   D1          |    8        |
  37  *          +-----------------------------+---------------+-------------+
  38  *****************************************************************************/
  39 
  40 /* Includes ------------------------------------------------------------------*/
  41 #include "sdio_sd.h"
  42 
  43 /**
  44  * @brief  SDIO Static flags, TimeOut, FIFO Address
  45  */
  46 #define NULL 0
  47 #define SDIO_STATIC_FLAGS               ((uint32_t)0x000005FF)
  48 #define SDIO_CMD0TIMEOUT                ((uint32_t)0x00010000)
  49 
  50 /**
  51  * @brief  Mask for errors Card Status R1 (OCR Register)
  52  */
  53 #define SD_OCR_ADDR_OUT_OF_RANGE        ((uint32_t)0x80000000)
  54 #define SD_OCR_ADDR_MISALIGNED          ((uint32_t)0x40000000)
  55 #define SD_OCR_BLOCK_LEN_ERR            ((uint32_t)0x20000000)
  56 #define SD_OCR_ERASE_SEQ_ERR            ((uint32_t)0x10000000)
  57 #define SD_OCR_BAD_ERASE_PARAM          ((uint32_t)0x08000000)
  58 #define SD_OCR_WRITE_PROT_VIOLATION     ((uint32_t)0x04000000)
  59 #define SD_OCR_LOCK_UNLOCK_FAILED       ((uint32_t)0x01000000)
  60 #define SD_OCR_COM_CRC_FAILED           ((uint32_t)0x00800000)
  61 #define SD_OCR_ILLEGAL_CMD              ((uint32_t)0x00400000)
  62 #define SD_OCR_CARD_ECC_FAILED          ((uint32_t)0x00200000)
  63 #define SD_OCR_CC_ERROR                 ((uint32_t)0x00100000)
  64 #define SD_OCR_GENERAL_UNKNOWN_ERROR    ((uint32_t)0x00080000)
  65 #define SD_OCR_STREAM_READ_UNDERRUN     ((uint32_t)0x00040000)
  66 #define SD_OCR_STREAM_WRITE_OVERRUN     ((uint32_t)0x00020000)
  67 #define SD_OCR_CID_CSD_OVERWRIETE       ((uint32_t)0x00010000)
  68 #define SD_OCR_WP_ERASE_SKIP            ((uint32_t)0x00008000)
  69 #define SD_OCR_CARD_ECC_DISABLED        ((uint32_t)0x00004000)
  70 #define SD_OCR_ERASE_RESET              ((uint32_t)0x00002000)
  71 #define SD_OCR_AKE_SEQ_ERROR            ((uint32_t)0x00000008)
  72 #define SD_OCR_ERRORBITS                ((uint32_t)0xFDFFE008)
  73 
  74 /**
  75  * @brief  Masks for R6 Response
  76  */
  77 #define SD_R6_GENERAL_UNKNOWN_ERROR     ((uint32_t)0x00002000)
  78 #define SD_R6_ILLEGAL_CMD               ((uint32_t)0x00004000)
  79 #define SD_R6_COM_CRC_FAILED            ((uint32_t)0x00008000)
  80 
  81 #define SD_VOLTAGE_WINDOW_SD            ((uint32_t)0x80100000)
  82 #define SD_HIGH_CAPACITY                ((uint32_t)0x40000000)
  83 #define SD_STD_CAPACITY                 ((uint32_t)0x00000000)
  84 #define SD_CHECK_PATTERN                ((uint32_t)0x000001AA)
  85 
  86 #define SD_MAX_VOLT_TRIAL               ((uint32_t)0x0000FFFF)
  87 #define SD_ALLZERO                      ((uint32_t)0x00000000)
  88 
  89 #define SD_WIDE_BUS_SUPPORT             ((uint32_t)0x00040000)
  90 #define SD_SINGLE_BUS_SUPPORT           ((uint32_t)0x00010000)
  91 #define SD_CARD_LOCKED                  ((uint32_t)0x02000000)
  92 
  93 #define SD_DATATIMEOUT                  ((uint32_t)0x000FFFFF)
  94 #define SD_0TO7BITS                     ((uint32_t)0x000000FF)
  95 #define SD_8TO15BITS                    ((uint32_t)0x0000FF00)
  96 #define SD_16TO23BITS                   ((uint32_t)0x00FF0000)
  97 #define SD_24TO31BITS                   ((uint32_t)0xFF000000)
  98 #define SD_MAX_DATA_LENGTH              ((uint32_t)0x01FFFFFF)
  99 
 100 #define SD_HALFFIFO                     ((uint32_t)0x00000008)
 101 #define SD_HALFFIFOBYTES                ((uint32_t)0x00000020)
 102 
 103 /**
 104  * @brief  Command Class Supported
 105  */
 106 #define SD_CCCC_LOCK_UNLOCK             ((uint32_t)0x00000080)
 107 #define SD_CCCC_WRITE_PROT              ((uint32_t)0x00000040)
 108 #define SD_CCCC_ERASE                   ((uint32_t)0x00000020)
 109 
 110 /**
 111  * @brief  Following commands are SD Card Specific commands.
 112  *         SDIO_APP_CMD should be sent before sending these commands.
 113  */
 114 #define SDIO_SEND_IF_COND               ((uint32_t)0x00000008)
 115 
 116 /**
 117  * @}
 118  */
 119 
 120 /** @defgroup STM32_EVAL_SDIO_SD_Private_Macros
 121  * @{
 122  */
 123 /**
 124  * @}
 125  */
 126 
 127 /** @defgroup STM32_EVAL_SDIO_SD_Private_Variables
 128  * @{
 129  */
 130 static uint32_t CardType = SDIO_STD_CAPACITY_SD_CARD_V1_1;
 131 static uint32_t CSD_Tab[ 4 ], CID_Tab[ 4 ], RCA = 0;
 132 static uint32_t DeviceMode         = SD_POLLING_MODE;
 133 static uint32_t TotalNumberOfBytes = 0, StopCondition = 0;
 134 uint32_t * SrcBuffer, *DestBuffer;
 135 __IO SD_Error TransferError = SD_OK;
 136 __IO uint32_t TransferEnd   = 0;
 137 __IO uint32_t NumberOfBytes = 0;
 138 SD_CardInfo SDCardInfo;
 139 SDIO_InitTypeDef SDIO_InitStructure;
 140 SDIO_CmdInitTypeDef SDIO_CmdInitStructure;
 141 SDIO_DataInitTypeDef SDIO_DataInitStructure;
 142 /**
 143  * @}
 144  */
 145 
 146 /** @defgroup STM32_EVAL_SDIO_SD_Private_Function_Prototypes
 147  * @{
 148  */
 149 static SD_Error CmdError ( void );
 150 static SD_Error CmdResp1Error ( uint8_t cmd );
 151 static SD_Error CmdResp7Error ( void );
 152 static SD_Error CmdResp3Error ( void );
 153 static SD_Error CmdResp2Error ( void );
 154 static SD_Error CmdResp6Error ( uint8_t cmd, uint16_t * prca );
 155 static SD_Error SDEnWideBus ( FunctionalState NewState );
 156 static SD_Error IsCardProgramming ( uint8_t * pstatus );
 157 static SD_Error FindSCR ( uint16_t rca, uint32_t * pscr );
 158 static uint8_t convert_from_bytes_to_power_of_two ( uint16_t NumberOfBytes );
 159 
 160 /**
 161  * @}
 162  */
 163 
 164 /** @defgroup STM32_EVAL_SDIO_SD_Private_Functions
 165  * @{
 166  */
 167 
 168 /**
 169  * @brief  DeInitializes the SDIO interface.
 170  * @param  None
 171  * @retval None
 172  */
 173 void SD_DeInit ( void )
 174 {
 175 
 176 }
 177 
 178 /**
 179  * @brief  Initializes the SD Card and put it into StandBy State (Ready for
 180  *         data transfer).
 181  * @param  None
 182  * @retval None
 183  */
 184 void SD_LowLevel_Init ( void )
 185 {
 186   GPIO_InitTypeDef GPIO_InitStructure;
 187 
 188   /*!< GPIOC and GPIOD Periph clock enable */
 189   RCC_APB2PeriphClockCmd ( RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD |
 190     SD_DETECT_GPIO_CLK, ENABLE );
 191 
 192   /*!< Configure PC.08, PC.09, PC.10, PC.11, PC.12 pin: D0, D1, D2, D3, CLK pin */
 193   GPIO_InitStructure.GPIO_Pin =
 194     GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;
 195   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 196   GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_PP;
 197   GPIO_Init ( GPIOC, & GPIO_InitStructure );
 198 
 199   /*!< Configure PD.02 CMD line */
 200   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
 201   GPIO_Init ( GPIOD, & GPIO_InitStructure );
 202 
 203   /*!< Configure SD_CD pin: SD Card detect pin */
 204   GPIO_InitStructure.GPIO_Pin  = SD_DETECT_PIN;
 205   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
 206   GPIO_Init ( SD_DETECT_GPIO_PORT, & GPIO_InitStructure );
 207 
 208   /*!< Enable the SDIO AHB Clock */
 209   RCC_AHBPeriphClockCmd ( RCC_AHBPeriph_SDIO, ENABLE );
 210 
 211   /*!< Enable the DMA2 Clock */
 212   RCC_AHBPeriphClockCmd ( RCC_AHBPeriph_DMA2, ENABLE );
 213 }
 214 
 215 uint32_t SD_DMAEndOfTransferStatus ( void )
 216 {
 217   return ( uint32_t )DMA_GetFlagStatus ( DMA2_FLAG_TC4 );
 218 }
 219 
 220 /**
 221  * @brief  Configures the DMA2 Channel4 for SDIO Rx request.
 222  * @param  BufferDST: pointer to the destination buffer
 223  * @param  BufferSize: buffer size
 224  * @retval None
 225  */
 226 void SD_LowLevel_DMA_RxConfig ( uint32_t * BufferDST, uint32_t BufferSize )
 227 {
 228   DMA_InitTypeDef DMA_InitStructure;
 229 
 230   DMA_ClearFlag ( DMA2_FLAG_TC4 | DMA2_FLAG_TE4 | DMA2_FLAG_HT4 |
 231     DMA2_FLAG_GL4 );
 232 
 233   /*!< DMA2 Channel4 disable */
 234   DMA_Cmd ( DMA2_Channel4, DISABLE );
 235 
 236   /*!< DMA2 Channel4 Config */
 237   DMA_InitStructure.DMA_PeripheralBaseAddr = ( uint32_t )SDIO_FIFO_ADDRESS;
 238   DMA_InitStructure.DMA_MemoryBaseAddr     = ( uint32_t )BufferDST;
 239   DMA_InitStructure.DMA_DIR                = DMA_DIR_PeripheralSRC;
 240   DMA_InitStructure.DMA_BufferSize         = BufferSize / 4;
 241   DMA_InitStructure.DMA_PeripheralInc      = DMA_PeripheralInc_Disable;
 242   DMA_InitStructure.DMA_MemoryInc          = DMA_MemoryInc_Enable;
 243   DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
 244   DMA_InitStructure.DMA_MemoryDataSize     = DMA_MemoryDataSize_Word;
 245   DMA_InitStructure.DMA_Mode               = DMA_Mode_Normal;
 246   DMA_InitStructure.DMA_Priority           = DMA_Priority_High;
 247   DMA_InitStructure.DMA_M2M                = DMA_M2M_Disable;
 248   DMA_Init ( DMA2_Channel4, & DMA_InitStructure );
 249 
 250   /*!< DMA2 Channel4 enable */
 251   DMA_Cmd ( DMA2_Channel4, ENABLE );
 252 }
 253 
 254 /**
 255  * @brief  Configures the DMA2 Channel4 for SDIO Tx request.
 256  * @param  BufferSRC: pointer to the source buffer
 257  * @param  BufferSize: buffer size
 258  * @retval None
 259  */
 260 void SD_LowLevel_DMA_TxConfig ( uint32_t * BufferSRC, uint32_t BufferSize )
 261 {
 262 
 263   DMA_InitTypeDef DMA_InitStructure;
 264 
 265   DMA_ClearFlag ( DMA2_FLAG_TC4 | DMA2_FLAG_TE4 | DMA2_FLAG_HT4 |
 266     DMA2_FLAG_GL4 );
 267 
 268   /*!< DMA2 Channel4 disable */
 269   DMA_Cmd ( DMA2_Channel4, DISABLE );
 270 
 271   /*!< DMA2 Channel4 Config */
 272   DMA_InitStructure.DMA_PeripheralBaseAddr = ( uint32_t )SDIO_FIFO_ADDRESS;
 273   DMA_InitStructure.DMA_MemoryBaseAddr     = ( uint32_t )BufferSRC;
 274   DMA_InitStructure.DMA_DIR                = DMA_DIR_PeripheralDST;
 275   DMA_InitStructure.DMA_BufferSize         = BufferSize / 4;
 276   DMA_InitStructure.DMA_PeripheralInc      = DMA_PeripheralInc_Disable;
 277   DMA_InitStructure.DMA_MemoryInc          = DMA_MemoryInc_Enable;
 278   DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
 279   DMA_InitStructure.DMA_MemoryDataSize     = DMA_MemoryDataSize_Word;
 280   DMA_InitStructure.DMA_Mode               = DMA_Mode_Normal;
 281   DMA_InitStructure.DMA_Priority           = DMA_Priority_High;
 282   DMA_InitStructure.DMA_M2M                = DMA_M2M_Disable;
 283   DMA_Init ( DMA2_Channel4, & DMA_InitStructure );
 284 
 285   /*!< DMA2 Channel4 enable */
 286   DMA_Cmd ( DMA2_Channel4, ENABLE );
 287 }
 288 
 289 /**
 290  * @brief  Initializes the SD Card and put it into StandBy State (Ready for data
 291  *         transfer).
 292  * @param  None
 293  * @retval SD_Error: SD Card Error code.
 294  */
 295 SD_Error SD_Init ( void )
 296 {
 297   SD_Error errorstatus = SD_OK;
 298 
 299   /* SDIO Peripheral Low Level Init */
 300   SD_LowLevel_Init ( );
 301 
 302   SDIO_DeInit ( );
 303 
 304   errorstatus = SD_PowerON ( );
 305 
 306   if ( errorstatus != SD_OK )
 307   {
 308     /*!< CMD Response TimeOut (wait for CMDSENT flag) */
 309     return ( errorstatus );
 310   }
 311 
 312   errorstatus = SD_InitializeCards ( );
 313 
 314   if ( errorstatus != SD_OK )
 315   {
 316     /*!< CMD Response TimeOut (wait for CMDSENT flag) */
 317     return ( errorstatus );
 318   }
 319 
 320   /*!< Configure the SDIO peripheral */
 321   /*!< SDIOCLK = HCLK, SDIO_CK = HCLK/(2 + SDIO_TRANSFER_CLK_DIV) */
 322   SDIO_InitStructure.SDIO_ClockDiv = SDIO_TRANSFER_CLK_DIV;
 323   SDIO_InitStructure.SDIO_ClockEdge           = SDIO_ClockEdge_Rising;
 324   SDIO_InitStructure.SDIO_ClockBypass         = SDIO_ClockBypass_Disable;
 325   SDIO_InitStructure.SDIO_ClockPowerSave      = SDIO_ClockPowerSave_Disable;
 326   SDIO_InitStructure.SDIO_BusWide             = SDIO_BusWide_1b;
 327   SDIO_InitStructure.SDIO_HardwareFlowControl =
 328     SDIO_HardwareFlowControl_Disable;
 329   SDIO_Init ( & SDIO_InitStructure );
 330 
 331   if ( errorstatus == SD_OK )
 332   {
 333     /*----------------- Read CSD/CID MSD registers ------------------*/
 334     errorstatus = SD_GetCardInfo ( &SDCardInfo );
 335   }
 336 
 337   if ( errorstatus == SD_OK )
 338   {
 339     /*----------------- Select Card --------------------------------*/
 340     errorstatus = SD_SelectDeselect ( ( uint32_t )( SDCardInfo.RCA << 16 ) );
 341   }
 342 
 343   if ( errorstatus == SD_OK )
 344   {
 345     errorstatus = SD_EnableWideBusOperation ( SDIO_BusWide_4b );
 346   }
 347 
 348   /* Set Device Transfer Mode to DMA */
 349   if ( errorstatus == SD_OK )
 350   {
 351     errorstatus = SD_SetDeviceMode ( SD_DMA_MODE );
 352   }
 353 
 354   return ( errorstatus );
 355 }
 356 
 357 /**
 358  * @brief  Gets the cuurent sd card data transfer status.
 359  * @param  None
 360  * @retval SDTransferState: Data Transfer state.
 361  *   This value can be:
 362  *        - SD_TRANSFER_OK: No data transfer is acting
 363  *        - SD_TRANSFER_BUSY: Data transfer is acting
 364  */
 365 SDTransferState SD_GetStatus ( void )
 366 {
 367   SDCardState cardstate = SD_CARD_TRANSFER;
 368 
 369   cardstate = SD_GetState ( );
 370 
 371   if ( cardstate == SD_CARD_TRANSFER )
 372   {
 373     return ( SD_TRANSFER_OK );
 374   }
 375   else if ( cardstate == SD_CARD_ERROR )
 376   {
 377     return ( SD_TRANSFER_ERROR );
 378   }
 379   else
 380   {
 381     return ( SD_TRANSFER_BUSY );
 382   }
 383 }
 384 
 385 /**
 386  * @brief  Returns the current card's state.
 387  * @param  None
 388  * @retval SDCardState: SD Card Error or SD Card Current State.
 389  */
 390 SDCardState SD_GetState ( void )
 391 {
 392   uint32_t resp1 = 0;
 393 
 394   if ( SD_Detect( ) == SD_PRESENT )
 395   {
 396     if ( SD_SendStatus( &resp1 ) != SD_OK )
 397     {
 398       return SD_CARD_ERROR;
 399     }
 400     else
 401     {
 402       return ( SDCardState )( ( resp1 >> 9 ) & 0x0F );
 403     }
 404   }
 405   else
 406   {
 407     return SD_CARD_ERROR;
 408   }
 409 }
 410 
 411 /**
 412  * @brief  Detect if SD card is correctly plugged in the memory slot.
 413  * @param  None
 414  * @retval Return if SD is detected or not
 415  */
 416 uint8_t SD_Detect ( void )
 417 {
 418   __IO uint8_t status = SD_PRESENT;
 419 
 420   /*!< Check GPIO to detect SD */
 421   if ( GPIO_ReadInputDataBit( SD_DETECT_GPIO_PORT, SD_DETECT_PIN )
 422     != Bit_RESET )
 423   {
 424     status = SD_NOT_PRESENT;
 425   }
 426   return status;
 427 }
 428 
 429 /**
 430  * @brief  Enquires cards about their operating voltage and configures
 431  *   clock controls.
 432  * @param  None
 433  * @retval SD_Error: SD Card Error code.
 434  */
 435 SD_Error SD_PowerON ( void )
 436 {
 437   SD_Error errorstatus = SD_OK;
 438   uint32_t response    = 0, count = 0, validvoltage = 0;
 439   uint32_t SDType      = SD_STD_CAPACITY;
 440 
 441   /*!< Power ON Sequence -----------------------------------------------------*/
 442   /*!< Configure the SDIO peripheral */
 443   /*!< SDIOCLK = HCLK, SDIO_CK = HCLK/(2 + SDIO_INIT_CLK_DIV) */
 444   /*!< SDIO_CK for initialization should not exceed 400 KHz */
 445   SDIO_InitStructure.SDIO_ClockDiv            = SDIO_INIT_CLK_DIV;
 446   SDIO_InitStructure.SDIO_ClockEdge           = SDIO_ClockEdge_Rising;
 447   SDIO_InitStructure.SDIO_ClockBypass         = SDIO_ClockBypass_Disable;
 448   SDIO_InitStructure.SDIO_ClockPowerSave      = SDIO_ClockPowerSave_Disable;
 449   SDIO_InitStructure.SDIO_BusWide             = SDIO_BusWide_1b;
 450   SDIO_InitStructure.SDIO_HardwareFlowControl =
 451     SDIO_HardwareFlowControl_Disable;
 452   SDIO_Init ( & SDIO_InitStructure );
 453 
 454   /*!< Set Power State to ON */
 455   SDIO_SetPowerState ( SDIO_PowerState_ON );
 456 
 457   /*!< Enable SDIO Clock */
 458   SDIO_ClockCmd ( ENABLE );
 459 
 460   /*!< CMD0: GO_IDLE_STATE ---------------------------------------------------*/
 461   /*!< No CMD response required */
 462   SDIO_CmdInitStructure.SDIO_Argument = 0x0;
 463   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_GO_IDLE_STATE;
 464   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_No;
 465   SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
 466   SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
 467   SDIO_SendCommand ( & SDIO_CmdInitStructure );
 468 
 469   errorstatus = CmdError ( );
 470 
 471   if ( errorstatus != SD_OK )
 472   {
 473     /*!< CMD Response TimeOut (wait for CMDSENT flag) */
 474     return ( errorstatus );
 475   }
 476 
 477   /*!< CMD8: SEND_IF_COND ----------------------------------------------------*/
 478   /*!< Send CMD8 to verify SD card interface operating condition */
 479   /*!< Argument: - [31:12]: Reserved (shall be set to '0')
 480    - [11:8]: Supply Voltage (VHS) 0x1 (Range: 2.7-3.6 V)
 481    - [7:0]: Check Pattern (recommended 0xAA) */
 482   /*!< CMD Response: R7 */
 483   SDIO_CmdInitStructure.SDIO_Argument = SD_CHECK_PATTERN;
 484   SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_SEND_IF_COND;
 485   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
 486   SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
 487   SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
 488   SDIO_SendCommand ( & SDIO_CmdInitStructure );
 489 
 490   errorstatus = CmdResp7Error ( );
 491 
 492   if ( errorstatus == SD_OK )
 493   {
 494     CardType = SDIO_STD_CAPACITY_SD_CARD_V2_0; /*!< SD Card 2.0 */
 495     SDType   = SD_HIGH_CAPACITY;
 496   }
 497   else
 498   {
 499     /*!< CMD55 */
 500     SDIO_CmdInitStructure.SDIO_Argument = 0x00;
 501     SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
 502     SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
 503     SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
 504     SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
 505     SDIO_SendCommand ( & SDIO_CmdInitStructure );
 506     errorstatus = CmdResp1Error ( SD_CMD_APP_CMD );
 507   }
 508   /*!< CMD55 */
 509   SDIO_CmdInitStructure.SDIO_Argument = 0x00;
 510   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
 511   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
 512   SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
 513   SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
 514   SDIO_SendCommand ( & SDIO_CmdInitStructure );
 515   errorstatus = CmdResp1Error ( SD_CMD_APP_CMD );
 516 
 517   /*!< If errorstatus is Command TimeOut, it is a MMC card */
 518   /*!< If errorstatus is SD_OK it is a SD card: SD card 2.0 (voltage range mismatch)
 519    or SD card 1.x */
 520   if ( errorstatus == SD_OK )
 521   {
 522     /*!< SD CARD */
 523     /*!< Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
 524     while ( ( !validvoltage ) && ( count < SD_MAX_VOLT_TRIAL ) )
 525     {
 526 
 527       /*!< SEND CMD55 APP_CMD with RCA as 0 */
 528       SDIO_CmdInitStructure.SDIO_Argument = 0x00;
 529       SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
 530       SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
 531       SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
 532       SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
 533       SDIO_SendCommand ( & SDIO_CmdInitStructure );
 534 
 535       errorstatus = CmdResp1Error ( SD_CMD_APP_CMD );
 536 
 537       if ( errorstatus != SD_OK )
 538       {
 539         return ( errorstatus );
 540       }
 541       SDIO_CmdInitStructure.SDIO_Argument = SD_VOLTAGE_WINDOW_SD | SDType;
 542       SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_APP_OP_COND;
 543       SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
 544       SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
 545       SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
 546       SDIO_SendCommand ( & SDIO_CmdInitStructure );
 547 
 548       errorstatus = CmdResp3Error ( );
 549       if ( errorstatus != SD_OK )
 550       {
 551         return ( errorstatus );
 552       }
 553 
 554       response = SDIO_GetResponse ( SDIO_RESP1 );
 555       validvoltage = ( ( ( response >> 31 ) == 1 ) ? 1 : 0 );
 556       count++;
 557     }
 558     if ( count >= SD_MAX_VOLT_TRIAL )
 559     {
 560       errorstatus = SD_INVALID_VOLTRANGE;
 561       return ( errorstatus );
 562     }
 563 
 564     if ( response &= SD_HIGH_CAPACITY )
 565     {
 566       CardType = SDIO_HIGH_CAPACITY_SD_CARD;
 567     }
 568 
 569   } /*!< else MMC Card */
 570 
 571   return ( errorstatus );
 572 }
 573 
 574 /**
 575  * @brief  Turns the SDIO output signals off.
 576  * @param  None
 577  * @retval SD_Error: SD Card Error code.
 578  */
 579 SD_Error SD_PowerOFF ( void )
 580 {
 581   SD_Error errorstatus = SD_OK;
 582 
 583   /*!< Set Power State to OFF */
 584   SDIO_SetPowerState ( SDIO_PowerState_OFF );
 585 
 586   return ( errorstatus );
 587 }
 588 
 589 /**
 590  * @brief  Intialises all cards or single card as the case may be Card(s) come
 591  *         into standby state.
 592  * @param  None
 593  * @retval SD_Error: SD Card Error code.
 594  */
 595 SD_Error SD_InitializeCards ( void )
 596 {
 597   SD_Error errorstatus = SD_OK;
 598   uint16_t rca         = 0x01;
 599 
 600   if ( SDIO_GetPowerState( ) == SDIO_PowerState_OFF )
 601   {
 602     errorstatus = SD_REQUEST_NOT_APPLICABLE;
 603     return ( errorstatus );
 604   }
 605 
 606   if ( SDIO_SECURE_DIGITAL_IO_CARD != CardType )
 607   {
 608     /*!< Send CMD2 ALL_SEND_CID */
 609     SDIO_CmdInitStructure.SDIO_Argument = 0x0;
 610     SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_ALL_SEND_CID;
 611     SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Long;
 612     SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
 613     SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
 614     SDIO_SendCommand ( & SDIO_CmdInitStructure );
 615 
 616     errorstatus = CmdResp2Error ( );
 617 
 618     if ( SD_OK != errorstatus )
 619     {
 620       return ( errorstatus );
 621     }
 622 
 623     CID_Tab[ 0 ] = SDIO_GetResponse ( SDIO_RESP1 );
 624     CID_Tab[ 1 ] = SDIO_GetResponse ( SDIO_RESP2 );
 625     CID_Tab[ 2 ] = SDIO_GetResponse ( SDIO_RESP3 );
 626     CID_Tab[ 3 ] = SDIO_GetResponse ( SDIO_RESP4 );
 627   }
 628   if ( ( SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType ) ||
 629     ( SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType ) ||
 630     ( SDIO_SECURE_DIGITAL_IO_COMBO_CARD == CardType ) ||
 631     ( SDIO_HIGH_CAPACITY_SD_CARD == CardType ) )
 632   {
 633     /*!< Send CMD3 SET_REL_ADDR with argument 0 */
 634     /*!< SD Card publishes its RCA. */
 635     SDIO_CmdInitStructure.SDIO_Argument = 0x00;
 636     SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_REL_ADDR;
 637     SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
 638     SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
 639     SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
 640     SDIO_SendCommand ( & SDIO_CmdInitStructure );
 641 
 642     errorstatus = CmdResp6Error ( SD_CMD_SET_REL_ADDR, &rca );
 643 
 644     if ( SD_OK != errorstatus )
 645     {
 646       return ( errorstatus );
 647     }
 648   }
 649 
 650   if ( SDIO_SECURE_DIGITAL_IO_CARD != CardType )
 651   {
 652     RCA = rca;
 653 
 654     /*!< Send CMD9 SEND_CSD with argument as card's RCA */
 655     SDIO_CmdInitStructure.SDIO_Argument = ( uint32_t )( rca << 16 );
 656     SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_CSD;
 657     SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Long;
 658     SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
 659     SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
 660     SDIO_SendCommand ( & SDIO_CmdInitStructure );
 661 
 662     errorstatus = CmdResp2Error ( );
 663 
 664     if ( SD_OK != errorstatus )
 665     {
 666       return ( errorstatus );
 667     }
 668 
 669     CSD_Tab[ 0 ] = SDIO_GetResponse ( SDIO_RESP1 );
 670     CSD_Tab[ 1 ] = SDIO_GetResponse ( SDIO_RESP2 );
 671     CSD_Tab[ 2 ] = SDIO_GetResponse ( SDIO_RESP3 );
 672     CSD_Tab[ 3 ] = SDIO_GetResponse ( SDIO_RESP4 );
 673   }
 674 
 675   errorstatus = SD_OK; /*!< All cards get intialized */
 676 
 677   return ( errorstatus );
 678 }
 679 
 680 /**
 681  * @brief  Returns information about specific card.
 682  * @param  cardinfo: pointer to a SD_CardInfo structure that contains all SD card
 683  *         information.
 684  * @retval SD_Error: SD Card Error code.
 685  */
 686 SD_Error SD_GetCardInfo ( SD_CardInfo * cardinfo )
 687 {
 688   SD_Error errorstatus = SD_OK;
 689   uint8_t  tmp         = 0;
 690 
 691   cardinfo->CardType = ( uint8_t )CardType;
 692   cardinfo->RCA      = ( uint16_t )RCA;
 693 
 694   /*!< Byte 0 */
 695   tmp = ( uint8_t )( ( CSD_Tab[ 0 ] & 0xFF000000 ) >> 24 );
 696   cardinfo->SD_csd.CSDStruct = ( tmp & 0xC0 ) >> 6;
 697   cardinfo->SD_csd.SysSpecVersion = ( tmp & 0x3C ) >> 2;
 698   cardinfo->SD_csd.Reserved1      = tmp & 0x03;
 699 
 700   /*!< Byte 1 */
 701   tmp                   = ( uint8_t )( ( CSD_Tab[ 0 ] & 0x00FF0000 ) >> 16 );
 702   cardinfo->SD_csd.TAAC = tmp;
 703 
 704   /*!< Byte 2 */
 705   tmp                   = ( uint8_t )( ( CSD_Tab[ 0 ] & 0x0000FF00 ) >> 8 );
 706   cardinfo->SD_csd.NSAC = tmp;
 707 
 708   /*!< Byte 3 */
 709   tmp                            = ( uint8_t )( CSD_Tab[ 0 ] & 0x000000FF );
 710   cardinfo->SD_csd.MaxBusClkFrec = tmp;
 711 
 712   /*!< Byte 4 */
 713   tmp = ( uint8_t )( ( CSD_Tab[ 1 ] & 0xFF000000 ) >> 24 );
 714   cardinfo->SD_csd.CardComdClasses = tmp << 4;
 715 
 716   /*!< Byte 5 */
 717   tmp = ( uint8_t )( ( CSD_Tab[ 1 ] & 0x00FF0000 ) >> 16 );
 718   cardinfo->SD_csd.CardComdClasses |= ( tmp & 0xF0 ) >> 4;
 719   cardinfo->SD_csd.RdBlockLen = tmp & 0x0F;
 720 
 721   /*!< Byte 6 */
 722   tmp = ( uint8_t )( ( CSD_Tab[ 1 ] & 0x0000FF00 ) >> 8 );
 723   cardinfo->SD_csd.PartBlockRead = ( tmp & 0x80 ) >> 7;
 724   cardinfo->SD_csd.WrBlockMisalign = ( tmp & 0x40 ) >> 6;
 725   cardinfo->SD_csd.RdBlockMisalign = ( tmp & 0x20 ) >> 5;
 726   cardinfo->SD_csd.DSRImpl         = ( tmp & 0x10 ) >> 4;
 727   cardinfo->SD_csd.Reserved2       = 0; /*!< Reserved */
 728 
 729   if ( ( CardType == SDIO_STD_CAPACITY_SD_CARD_V1_1 ) ||
 730     ( CardType == SDIO_STD_CAPACITY_SD_CARD_V2_0 ) )
 731   {
 732     cardinfo->SD_csd.DeviceSize = ( tmp & 0x03 ) << 10;
 733 
 734     /*!< Byte 7 */
 735     tmp = ( uint8_t )( CSD_Tab[ 1 ] & 0x000000FF );
 736     cardinfo->SD_csd.DeviceSize |= ( tmp ) << 2;
 737 
 738     /*!< Byte 8 */
 739     tmp = ( uint8_t )( ( CSD_Tab[ 2 ] & 0xFF000000 ) >> 24 );
 740     cardinfo->SD_csd.DeviceSize |= ( tmp & 0xC0 ) >> 6;
 741 
 742     cardinfo->SD_csd.MaxRdCurrentVDDMin = ( tmp & 0x38 ) >> 3;
 743     cardinfo->SD_csd.MaxRdCurrentVDDMax = ( tmp & 0x07 );
 744 
 745     /*!< Byte 9 */
 746     tmp = ( uint8_t )( ( CSD_Tab[ 2 ] & 0x00FF0000 ) >> 16 );
 747     cardinfo->SD_csd.MaxWrCurrentVDDMin = ( tmp & 0xE0 ) >> 5;
 748     cardinfo->SD_csd.MaxWrCurrentVDDMax = ( tmp & 0x1C ) >> 2;
 749     cardinfo->SD_csd.DeviceSizeMul      = ( tmp & 0x03 ) << 1;
 750     /*!< Byte 10 */
 751     tmp = ( uint8_t )( ( CSD_Tab[ 2 ] & 0x0000FF00 ) >> 8 );
 752     cardinfo->SD_csd.DeviceSizeMul |= ( tmp & 0x80 ) >> 7;
 753 
 754     cardinfo->CardCapacity = ( cardinfo->SD_csd.DeviceSize + 1 );
 755     cardinfo->CardCapacity *= ( 1 << ( cardinfo->SD_csd.DeviceSizeMul + 2 ) );
 756     cardinfo->CardBlockSize = 1 << ( cardinfo->SD_csd.RdBlockLen );
 757     cardinfo->CardCapacity *= cardinfo->CardBlockSize;
 758   }
 759   else if ( CardType == SDIO_HIGH_CAPACITY_SD_CARD )
 760   {
 761     /*!< Byte 7 */
 762     tmp                         = ( uint8_t )( CSD_Tab[ 1 ] & 0x000000FF );
 763     cardinfo->SD_csd.DeviceSize = ( tmp & 0x3F ) << 16;
 764 
 765     /*!< Byte 8 */
 766     tmp = ( uint8_t )( ( CSD_Tab[ 2 ] & 0xFF000000 ) >> 24 );
 767 
 768     cardinfo->SD_csd.DeviceSize |= ( tmp << 8 );
 769 
 770     /*!< Byte 9 */
 771     tmp = ( uint8_t )( ( CSD_Tab[ 2 ] & 0x00FF0000 ) >> 16 );
 772 
 773     cardinfo->SD_csd.DeviceSize |= ( tmp );
 774 
 775     /*!< Byte 10 */
 776     tmp = ( uint8_t )( ( CSD_Tab[ 2 ] & 0x0000FF00 ) >> 8 );
 777 
 778     cardinfo->CardCapacity  = ( cardinfo->SD_csd.DeviceSize + 1 ) * 512 * 1024;
 779     cardinfo->CardBlockSize = 512;
 780   }
 781 
 782   cardinfo->SD_csd.EraseGrSize = ( tmp & 0x40 ) >> 6;
 783   cardinfo->SD_csd.EraseGrMul = ( tmp & 0x3F ) << 1;
 784 
 785   /*!< Byte 11 */
 786   tmp = ( uint8_t )( CSD_Tab[ 2 ] & 0x000000FF );
 787   cardinfo->SD_csd.EraseGrMul |= ( tmp & 0x80 ) >> 7;
 788   cardinfo->SD_csd.WrProtectGrSize = ( tmp & 0x7F );
 789 
 790   /*!< Byte 12 */
 791   tmp = ( uint8_t )( ( CSD_Tab[ 3 ] & 0xFF000000 ) >> 24 );
 792   cardinfo->SD_csd.WrProtectGrEnable = ( tmp & 0x80 ) >> 7;
 793   cardinfo->SD_csd.ManDeflECC    = ( tmp & 0x60 ) >> 5;
 794   cardinfo->SD_csd.WrSpeedFact   = ( tmp & 0x1C ) >> 2;
 795   cardinfo->SD_csd.MaxWrBlockLen = ( tmp & 0x03 ) << 2;
 796 
 797   /*!< Byte 13 */
 798   tmp = ( uint8_t )( ( CSD_Tab[ 3 ] & 0x00FF0000 ) >> 16 );
 799   cardinfo->SD_csd.MaxWrBlockLen |= ( tmp & 0xC0 ) >> 6;
 800   cardinfo->SD_csd.WriteBlockPaPartial = ( tmp & 0x20 ) >> 5;
 801   cardinfo->SD_csd.Reserved3           = 0;
 802   cardinfo->SD_csd.ContentProtectAppli = ( tmp & 0x01 );
 803 
 804   /*!< Byte 14 */
 805   tmp = ( uint8_t )( ( CSD_Tab[ 3 ] & 0x0000FF00 ) >> 8 );
 806   cardinfo->SD_csd.FileFormatGrouop = ( tmp & 0x80 ) >> 7;
 807   cardinfo->SD_csd.CopyFlag      = ( tmp & 0x40 ) >> 6;
 808   cardinfo->SD_csd.PermWrProtect = ( tmp & 0x20 ) >> 5;
 809   cardinfo->SD_csd.TempWrProtect = ( tmp & 0x10 ) >> 4;
 810   cardinfo->SD_csd.FileFormat    = ( tmp & 0x0C ) >> 2;
 811   cardinfo->SD_csd.ECC           = ( tmp & 0x03 );
 812 
 813   /*!< Byte 15 */
 814   tmp                        = ( uint8_t )( CSD_Tab[ 3 ] & 0x000000FF );
 815   cardinfo->SD_csd.CSD_CRC   = ( tmp & 0xFE ) >> 1;
 816   cardinfo->SD_csd.Reserved4 = 1;
 817 
 818   /*!< Byte 0 */
 819   tmp = ( uint8_t )( ( CID_Tab[ 0 ] & 0xFF000000 ) >> 24 );
 820   cardinfo->SD_cid.ManufacturerID = tmp;
 821 
 822   /*!< Byte 1 */
 823   tmp = ( uint8_t )( ( CID_Tab[ 0 ] & 0x00FF0000 ) >> 16 );
 824   cardinfo->SD_cid.OEM_AppliID = tmp << 8;
 825 
 826   /*!< Byte 2 */
 827   tmp = ( uint8_t )( ( CID_Tab[ 0 ] & 0x000000FF00 ) >> 8 );
 828   cardinfo->SD_cid.OEM_AppliID |= tmp;
 829 
 830   /*!< Byte 3 */
 831   tmp                        = ( uint8_t )( CID_Tab[ 0 ] & 0x000000FF );
 832   cardinfo->SD_cid.ProdName1 = tmp << 24;
 833 
 834   /*!< Byte 4 */
 835   tmp = ( uint8_t )( ( CID_Tab[ 1 ] & 0xFF000000 ) >> 24 );
 836   cardinfo->SD_cid.ProdName1 |= tmp << 16;
 837 
 838   /*!< Byte 5 */
 839   tmp = ( uint8_t )( ( CID_Tab[ 1 ] & 0x00FF0000 ) >> 16 );
 840   cardinfo->SD_cid.ProdName1 |= tmp << 8;
 841 
 842   /*!< Byte 6 */
 843   tmp = ( uint8_t )( ( CID_Tab[ 1 ] & 0x0000FF00 ) >> 8 );
 844   cardinfo->SD_cid.ProdName1 |= tmp;
 845 
 846   /*!< Byte 7 */
 847   tmp                        = ( uint8_t )( CID_Tab[ 1 ] & 0x000000FF );
 848   cardinfo->SD_cid.ProdName2 = tmp;
 849 
 850   /*!< Byte 8 */
 851   tmp = ( uint8_t )( ( CID_Tab[ 2 ] & 0xFF000000 ) >> 24 );
 852   cardinfo->SD_cid.ProdRev = tmp;
 853 
 854   /*!< Byte 9 */
 855   tmp                     = ( uint8_t )( ( CID_Tab[ 2 ] & 0x00FF0000 ) >> 16 );
 856   cardinfo->SD_cid.ProdSN = tmp << 24;
 857 
 858   /*!< Byte 10 */
 859   tmp = ( uint8_t )( ( CID_Tab[ 2 ] & 0x0000FF00 ) >> 8 );
 860   cardinfo->SD_cid.ProdSN |= tmp << 16;
 861 
 862   /*!< Byte 11 */
 863   tmp = ( uint8_t )( CID_Tab[ 2 ] & 0x000000FF );
 864   cardinfo->SD_cid.ProdSN |= tmp << 8;
 865 
 866   /*!< Byte 12 */
 867   tmp = ( uint8_t )( ( CID_Tab[ 3 ] & 0xFF000000 ) >> 24 );
 868   cardinfo->SD_cid.ProdSN |= tmp;
 869 
 870   /*!< Byte 13 */
 871   tmp = ( uint8_t )( ( CID_Tab[ 3 ] & 0x00FF0000 ) >> 16 );
 872   cardinfo->SD_cid.Reserved1 |= ( tmp & 0xF0 ) >> 4;
 873   cardinfo->SD_cid.ManufactDate = ( tmp & 0x0F ) << 8;
 874 
 875   /*!< Byte 14 */
 876   tmp = ( uint8_t )( ( CID_Tab[ 3 ] & 0x0000FF00 ) >> 8 );
 877   cardinfo->SD_cid.ManufactDate |= tmp;
 878 
 879   /*!< Byte 15 */
 880   tmp                        = ( uint8_t )( CID_Tab[ 3 ] & 0x000000FF );
 881   cardinfo->SD_cid.CID_CRC   = ( tmp & 0xFE ) >> 1;
 882   cardinfo->SD_cid.Reserved2 = 1;
 883 
 884   return ( errorstatus );
 885 }
 886 
 887 /**
 888  * @brief  Enables wide bus opeartion for the requeseted card if supported by
 889  *         card.
 890  * @param  WideMode: Specifies the SD card wide bus mode.
 891  *   This parameter can be one of the following values:
 892  *     @arg SDIO_BusWide_8b: 8-bit data transfer (Only for MMC)
 893  *     @arg SDIO_BusWide_4b: 4-bit data transfer
 894  *     @arg SDIO_BusWide_1b: 1-bit data transfer
 895  * @retval SD_Error: SD Card Error code.
 896  */
 897 SD_Error SD_EnableWideBusOperation ( uint32_t WideMode )
 898 {
 899   SD_Error errorstatus = SD_OK;
 900 
 901   /*!< MMC Card doesn't support this feature */
 902   if ( SDIO_MULTIMEDIA_CARD == CardType )
 903   {
 904     errorstatus = SD_UNSUPPORTED_FEATURE;
 905     return ( errorstatus );
 906   }
 907   else if ( ( SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType ) ||
 908     ( SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType ) ||
 909     ( SDIO_HIGH_CAPACITY_SD_CARD == CardType ) )
 910   {
 911     if ( SDIO_BusWide_8b == WideMode )
 912     {
 913       errorstatus = SD_UNSUPPORTED_FEATURE;
 914       return ( errorstatus );
 915     }
 916     else if ( SDIO_BusWide_4b == WideMode )
 917     {
 918       errorstatus = SDEnWideBus ( ENABLE );
 919 
 920       if ( SD_OK == errorstatus )
 921       {
 922         /*!< Configure the SDIO peripheral */
 923         SDIO_InitStructure.SDIO_ClockDiv       = SDIO_TRANSFER_CLK_DIV;
 924         SDIO_InitStructure.SDIO_ClockEdge      = SDIO_ClockEdge_Rising;
 925         SDIO_InitStructure.SDIO_ClockBypass    = SDIO_ClockBypass_Disable;
 926         SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable;
 927         SDIO_InitStructure.SDIO_BusWide        = SDIO_BusWide_4b;
 928         SDIO_InitStructure.SDIO_HardwareFlowControl =
 929           SDIO_HardwareFlowControl_Disable;
 930         SDIO_Init ( & SDIO_InitStructure );
 931       }
 932     }
 933     else
 934     {
 935       errorstatus = SDEnWideBus ( DISABLE );
 936 
 937       if ( SD_OK == errorstatus )
 938       {
 939         /*!< Configure the SDIO peripheral */
 940         SDIO_InitStructure.SDIO_ClockDiv       = SDIO_TRANSFER_CLK_DIV;
 941         SDIO_InitStructure.SDIO_ClockEdge      = SDIO_ClockEdge_Rising;
 942         SDIO_InitStructure.SDIO_ClockBypass    = SDIO_ClockBypass_Disable;
 943         SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable;
 944         SDIO_InitStructure.SDIO_BusWide        = SDIO_BusWide_1b;
 945         SDIO_InitStructure.SDIO_HardwareFlowControl =
 946           SDIO_HardwareFlowControl_Disable;
 947         SDIO_Init ( & SDIO_InitStructure );
 948       }
 949     }
 950   }
 951 
 952   return ( errorstatus );
 953 }
 954 
 955 /**
 956  * @brief  Sets device mode whether to operate in Polling, Interrupt or DMA mode.
 957  * @param  Mode: Specifies the Data Transfer mode.
 958  *   This parameter can be one of the following values:
 959  *     @arg SD_DMA_MODE: Data transfer using DMA.
 960  *     @arg SD_INTERRUPT_MODE: Data transfer using interrupts.
 961  *     @arg SD_POLLING_MODE: Data transfer using flags.
 962  * @retval SD_Error: SD Card Error code.
 963  */
 964 SD_Error SD_SetDeviceMode ( uint32_t Mode )
 965 {
 966   SD_Error errorstatus = SD_OK;
 967 
 968   if ( ( Mode == SD_DMA_MODE ) || ( Mode == SD_INTERRUPT_MODE ) ||
 969     ( Mode == SD_POLLING_MODE ) )
 970   {
 971     DeviceMode = Mode;
 972   }
 973   else
 974   {
 975     errorstatus = SD_INVALID_PARAMETER;
 976   }
 977   return ( errorstatus );
 978 
 979 }
 980 
 981 /**
 982  * @brief  Selects od Deselects the corresponding card.
 983  * @param  addr: Address of the Card to be selected.
 984  * @retval SD_Error: SD Card Error code.
 985  */
 986 SD_Error SD_SelectDeselect ( uint32_t addr )
 987 {
 988   SD_Error errorstatus = SD_OK;
 989 
 990   /*!< Send CMD7 SDIO_SEL_DESEL_CARD */
 991   SDIO_CmdInitStructure.SDIO_Argument = addr;
 992   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEL_DESEL_CARD;
 993   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
 994   SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
 995   SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
 996   SDIO_SendCommand ( & SDIO_CmdInitStructure );
 997 
 998   errorstatus = CmdResp1Error ( SD_CMD_SEL_DESEL_CARD );
 999 
1000   return ( errorstatus );
1001 }
1002 
1003 /**
1004  * @brief  Allows to read one block from a specified address in a card.
1005  * @param  readbuff: pointer to the buffer that will contain the received data
1006  * @param  ReadAddr: Address from where data are to be read.
1007  * @param  BlockSize: the SD card Data block size.
1008  * @retval SD_Error: SD Card Error code.
1009  */
1010 SD_Error SD_ReadBlock ( uint8_t * readbuff, uint32_t ReadAddr,
1011   uint16_t BlockSize )
1012 {
1013   SD_Error errorstatus = SD_OK;
1014   uint32_t count       = 0, *tempbuff = ( uint32_t* )readbuff;
1015   uint8_t  power       = 0;
1016 
1017   if ( NULL == readbuff )
1018   {
1019     errorstatus = SD_INVALID_PARAMETER;
1020     return ( errorstatus );
1021   }
1022 
1023   TransferError = SD_OK;
1024   TransferEnd        = 0;
1025   TotalNumberOfBytes = 0;
1026 
1027   /*!< Clear all DPSM configuration */
1028   SDIO_DataInitStructure.SDIO_DataTimeOut   = SD_DATATIMEOUT;
1029   SDIO_DataInitStructure.SDIO_DataLength    = 0;
1030   SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_1b;
1031   SDIO_DataInitStructure.SDIO_TransferDir   = SDIO_TransferDir_ToCard;
1032   SDIO_DataInitStructure.SDIO_TransferMode  = SDIO_TransferMode_Block;
1033   SDIO_DataInitStructure.SDIO_DPSM          = SDIO_DPSM_Disable;
1034   SDIO_DataConfig ( & SDIO_DataInitStructure );
1035   SDIO_DMACmd ( DISABLE );
1036 
1037   if ( SDIO_GetResponse( SDIO_RESP1 ) & SD_CARD_LOCKED )
1038   {
1039     errorstatus = SD_LOCK_UNLOCK_FAILED;
1040     return ( errorstatus );
1041   }
1042 
1043   if ( CardType == SDIO_HIGH_CAPACITY_SD_CARD )
1044   {
1045     BlockSize = 512;
1046     ReadAddr /= 512;
1047   }
1048   if ( ( BlockSize > 0 ) && ( BlockSize <= 2048 ) &&
1049     ( ( BlockSize & ( BlockSize - 1 ) ) == 0 ) )
1050   {
1051     power = convert_from_bytes_to_power_of_two ( BlockSize );
1052 
1053     /*!< Set Block Size for Card */
1054     SDIO_CmdInitStructure.SDIO_Argument = ( uint32_t ) BlockSize;
1055     SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
1056     SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1057     SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
1058     SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
1059     SDIO_SendCommand ( & SDIO_CmdInitStructure );
1060 
1061     errorstatus = CmdResp1Error ( SD_CMD_SET_BLOCKLEN );
1062 
1063     if ( SD_OK != errorstatus )
1064     {
1065       return ( errorstatus );
1066     }
1067   }
1068   else
1069   {
1070     errorstatus = SD_INVALID_PARAMETER;
1071     return ( errorstatus );
1072   }
1073 
1074   SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
1075   SDIO_DataInitStructure.SDIO_DataLength    = BlockSize;
1076   SDIO_DataInitStructure.SDIO_DataBlockSize = ( uint32_t ) power << 4;
1077   SDIO_DataInitStructure.SDIO_TransferDir   = SDIO_TransferDir_ToSDIO;
1078   SDIO_DataInitStructure.SDIO_TransferMode  = SDIO_TransferMode_Block;
1079   SDIO_DataInitStructure.SDIO_DPSM          = SDIO_DPSM_Enable;
1080   SDIO_DataConfig ( & SDIO_DataInitStructure );
1081 
1082   TotalNumberOfBytes = BlockSize;
1083   StopCondition      = 0;
1084   DestBuffer         = ( uint32_t* )readbuff;
1085 
1086   /*!< Send CMD17 READ_SINGLE_BLOCK */
1087   SDIO_CmdInitStructure.SDIO_Argument = ( uint32_t )ReadAddr;
1088   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_READ_SINGLE_BLOCK;
1089   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1090   SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
1091   SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
1092   SDIO_SendCommand ( & SDIO_CmdInitStructure );
1093 
1094   errorstatus = CmdResp1Error ( SD_CMD_READ_SINGLE_BLOCK );
1095 
1096   if ( errorstatus != SD_OK )
1097   {
1098     return ( errorstatus );
1099   }
1100   /*!< In case of single block transfer, no need of stop transfer at all.*/
1101   if ( DeviceMode == SD_POLLING_MODE )
1102   {
1103     /*!< Polling mode */
1104     while ( !( SDIO->STA & ( SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL |
1105           SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR ) ) )
1106     {
1107       if ( SDIO_GetFlagStatus( SDIO_FLAG_RXFIFOHF ) != RESET )
1108       {
1109         for ( count = 0; count < 8; count++ )
1110         {
1111           *( tempbuff + count ) = SDIO_ReadData ( );
1112         }
1113         tempbuff += 8;
1114       }
1115     }
1116 
1117     if ( SDIO_GetFlagStatus( SDIO_FLAG_DTIMEOUT ) != RESET )
1118     {
1119       SDIO_ClearFlag ( SDIO_FLAG_DTIMEOUT );
1120       errorstatus = SD_DATA_TIMEOUT;
1121       return ( errorstatus );
1122     }
1123     else if ( SDIO_GetFlagStatus( SDIO_FLAG_DCRCFAIL ) != RESET )
1124     {
1125       SDIO_ClearFlag ( SDIO_FLAG_DCRCFAIL );
1126       errorstatus = SD_DATA_CRC_FAIL;
1127       return ( errorstatus );
1128     }
1129     else if ( SDIO_GetFlagStatus( SDIO_FLAG_RXOVERR ) != RESET )
1130     {
1131       SDIO_ClearFlag ( SDIO_FLAG_RXOVERR );
1132       errorstatus = SD_RX_OVERRUN;
1133       return ( errorstatus );
1134     }
1135     else if ( SDIO_GetFlagStatus( SDIO_FLAG_STBITERR ) != RESET )
1136     {
1137       SDIO_ClearFlag ( SDIO_FLAG_STBITERR );
1138       errorstatus = SD_START_BIT_ERR;
1139       return ( errorstatus );
1140     }
1141     while ( SDIO_GetFlagStatus( SDIO_FLAG_RXDAVL ) != RESET )
1142     {
1143       *tempbuff = SDIO_ReadData ( );
1144       tempbuff++;
1145     }
1146 
1147     /*!< Clear all the static flags */
1148     SDIO_ClearFlag ( SDIO_STATIC_FLAGS );
1149   }
1150   else if ( DeviceMode == SD_INTERRUPT_MODE )
1151   {
1152     SDIO_ITConfig ( SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |
1153       SDIO_IT_RXOVERR | SDIO_IT_RXFIFOHF | SDIO_IT_STBITERR, ENABLE );
1154     while ( ( TransferEnd == 0 ) && ( TransferError == SD_OK ) )
1155     {
1156     }
1157     if ( TransferError != SD_OK )
1158     {
1159       return ( TransferError );
1160     }
1161   }
1162   else if ( DeviceMode == SD_DMA_MODE )
1163   {
1164     SDIO_ITConfig ( SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |
1165       SDIO_IT_RXOVERR | SDIO_IT_STBITERR, ENABLE );
1166     SDIO_DMACmd ( ENABLE );
1167     SD_LowLevel_DMA_RxConfig ( ( uint32_t * )readbuff, BlockSize );
1168     while ( ( SD_DMAEndOfTransferStatus( ) == RESET ) && ( TransferEnd == 0 ) &&
1169       ( TransferError == SD_OK ) )
1170     {
1171     }
1172     if ( TransferError != SD_OK )
1173     {
1174       return ( TransferError );
1175     }
1176   }
1177   return ( errorstatus );
1178 }
1179 
1180 /**
1181  * @brief  Allows to read blocks from a specified address  in a card.
1182  * @param  readbuff: pointer to the buffer that will contain the received data.
1183  * @param  ReadAddr: Address from where data are to be read.
1184  * @param  BlockSize: the SD card Data block size.
1185  * @param  NumberOfBlocks: number of blocks to be read.
1186  * @retval SD_Error: SD Card Error code.
1187  */
1188 SD_Error SD_ReadMultiBlocks ( uint8_t * readbuff, uint32_t ReadAddr,
1189   uint16_t BlockSize, uint32_t NumberOfBlocks )
1190 {
1191   SD_Error errorstatus = SD_OK;
1192   uint32_t count       = 0, *tempbuff = ( uint32_t* )readbuff;
1193   uint8_t  power       = 0;
1194 
1195   if ( NULL == readbuff )
1196   {
1197     errorstatus = SD_INVALID_PARAMETER;
1198     return ( errorstatus );
1199   }
1200 
1201   TransferError = SD_OK;
1202   TransferEnd        = 0;
1203   TotalNumberOfBytes = 0;
1204 
1205   /*!< Clear all DPSM configuration */
1206   SDIO_DataInitStructure.SDIO_DataTimeOut   = SD_DATATIMEOUT;
1207   SDIO_DataInitStructure.SDIO_DataLength    = 0;
1208   SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_1b;
1209   SDIO_DataInitStructure.SDIO_TransferDir   = SDIO_TransferDir_ToCard;
1210   SDIO_DataInitStructure.SDIO_TransferMode  = SDIO_TransferMode_Block;
1211   SDIO_DataInitStructure.SDIO_DPSM          = SDIO_DPSM_Disable;
1212   SDIO_DataConfig ( & SDIO_DataInitStructure );
1213   SDIO_DMACmd ( DISABLE );
1214 
1215   if ( SDIO_GetResponse( SDIO_RESP1 ) & SD_CARD_LOCKED )
1216   {
1217     errorstatus = SD_LOCK_UNLOCK_FAILED;
1218     return ( errorstatus );
1219   }
1220 
1221   if ( CardType == SDIO_HIGH_CAPACITY_SD_CARD )
1222   {
1223     BlockSize = 512;
1224     ReadAddr /= 512;
1225   }
1226 
1227   if ( ( BlockSize > 0 ) && ( BlockSize <= 2048 ) &&
1228     ( 0 == ( BlockSize & ( BlockSize - 1 ) ) ) )
1229   {
1230     power = convert_from_bytes_to_power_of_two ( BlockSize );
1231 
1232     /*!< Set Block Size for Card */
1233     SDIO_CmdInitStructure.SDIO_Argument = ( uint32_t ) BlockSize;
1234     SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
1235     SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1236     SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
1237     SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
1238     SDIO_SendCommand ( & SDIO_CmdInitStructure );
1239 
1240     errorstatus = CmdResp1Error ( SD_CMD_SET_BLOCKLEN );
1241 
1242     if ( SD_OK != errorstatus )
1243     {
1244       return ( errorstatus );
1245     }
1246   }
1247   else
1248   {
1249     errorstatus = SD_INVALID_PARAMETER;
1250     return ( errorstatus );
1251   }
1252 
1253   if ( NumberOfBlocks > 1 )
1254   {
1255     /*!< Common to all modes */
1256     if ( NumberOfBlocks * BlockSize > SD_MAX_DATA_LENGTH )
1257     {
1258       errorstatus = SD_INVALID_PARAMETER;
1259       return ( errorstatus );
1260     }
1261 
1262     TotalNumberOfBytes = NumberOfBlocks * BlockSize;
1263     StopCondition = 1;
1264     DestBuffer    = ( uint32_t* )readbuff;
1265 
1266     SDIO_DataInitStructure.SDIO_DataTimeOut   = SD_DATATIMEOUT;
1267     SDIO_DataInitStructure.SDIO_DataLength    = NumberOfBlocks * BlockSize;
1268     SDIO_DataInitStructure.SDIO_DataBlockSize = ( uint32_t ) power << 4;
1269     SDIO_DataInitStructure.SDIO_TransferDir   = SDIO_TransferDir_ToSDIO;
1270     SDIO_DataInitStructure.SDIO_TransferMode  = SDIO_TransferMode_Block;
1271     SDIO_DataInitStructure.SDIO_DPSM          = SDIO_DPSM_Enable;
1272     SDIO_DataConfig ( & SDIO_DataInitStructure );
1273 
1274     /*!< Send CMD18 READ_MULT_BLOCK with argument data address */
1275     SDIO_CmdInitStructure.SDIO_Argument = ( uint32_t )ReadAddr;
1276     SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_READ_MULT_BLOCK;
1277     SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1278     SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
1279     SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
1280     SDIO_SendCommand ( & SDIO_CmdInitStructure );
1281 
1282     errorstatus = CmdResp1Error ( SD_CMD_READ_MULT_BLOCK );
1283 
1284     if ( errorstatus != SD_OK )
1285     {
1286       return ( errorstatus );
1287     }
1288 
1289     if ( DeviceMode == SD_POLLING_MODE )
1290     {
1291       /*!< Polling mode */
1292       while ( !( SDIO->STA &
1293           ( SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DATAEND |
1294             SDIO_FLAG_DTIMEOUT | SDIO_FLAG_STBITERR ) ) )
1295       {
1296         if ( SDIO_GetFlagStatus( SDIO_FLAG_RXFIFOHF ) != RESET )
1297         {
1298           for ( count = 0; count < SD_HALFFIFO; count++ )
1299           {
1300             *( tempbuff + count ) = SDIO_ReadData ( );
1301           }
1302           tempbuff += SD_HALFFIFO;
1303         }
1304       }
1305 
1306       if ( SDIO_GetFlagStatus( SDIO_FLAG_DTIMEOUT ) != RESET )
1307       {
1308         SDIO_ClearFlag ( SDIO_FLAG_DTIMEOUT );
1309         errorstatus = SD_DATA_TIMEOUT;
1310         return ( errorstatus );
1311       }
1312       else if ( SDIO_GetFlagStatus( SDIO_FLAG_DCRCFAIL ) != RESET )
1313       {
1314         SDIO_ClearFlag ( SDIO_FLAG_DCRCFAIL );
1315         errorstatus = SD_DATA_CRC_FAIL;
1316         return ( errorstatus );
1317       }
1318       else if ( SDIO_GetFlagStatus( SDIO_FLAG_RXOVERR ) != RESET )
1319       {
1320         SDIO_ClearFlag ( SDIO_FLAG_RXOVERR );
1321         errorstatus = SD_RX_OVERRUN;
1322         return ( errorstatus );
1323       }
1324       else if ( SDIO_GetFlagStatus( SDIO_FLAG_STBITERR ) != RESET )
1325       {
1326         SDIO_ClearFlag ( SDIO_FLAG_STBITERR );
1327         errorstatus = SD_START_BIT_ERR;
1328         return ( errorstatus );
1329       }
1330       while ( SDIO_GetFlagStatus( SDIO_FLAG_RXDAVL ) != RESET )
1331       {
1332         *tempbuff = SDIO_ReadData ( );
1333         tempbuff++;
1334       }
1335 
1336       if ( SDIO_GetFlagStatus( SDIO_FLAG_DATAEND ) != RESET )
1337       {
1338         /*!< In Case Of SD-CARD Send Command STOP_TRANSMISSION */
1339         if ( ( SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType ) ||
1340           ( SDIO_HIGH_CAPACITY_SD_CARD == CardType ) ||
1341           ( SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType ) )
1342         {
1343           /*!< Send CMD12 STOP_TRANSMISSION */
1344           SDIO_CmdInitStructure.SDIO_Argument = 0x0;
1345           SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_STOP_TRANSMISSION;
1346           SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1347           SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
1348           SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
1349           SDIO_SendCommand ( & SDIO_CmdInitStructure );
1350 
1351           errorstatus = CmdResp1Error ( SD_CMD_STOP_TRANSMISSION );
1352 
1353           if ( errorstatus != SD_OK )
1354           {
1355             return ( errorstatus );
1356           }
1357         }
1358       }
1359       /*!< Clear all the static flags */
1360       SDIO_ClearFlag ( SDIO_STATIC_FLAGS );
1361     }
1362     else if ( DeviceMode == SD_INTERRUPT_MODE )
1363     {
1364       SDIO_ITConfig ( SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |
1365         SDIO_IT_RXOVERR | SDIO_IT_RXFIFOHF | SDIO_IT_STBITERR, ENABLE );
1366       while ( ( TransferEnd == 0 ) && ( TransferError == SD_OK ) )
1367       {
1368       }
1369       if ( TransferError != SD_OK )
1370       {
1371         return ( TransferError );
1372       }
1373     }
1374     else if ( DeviceMode == SD_DMA_MODE )
1375     {
1376       SDIO_ITConfig ( SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |
1377         SDIO_IT_RXOVERR | SDIO_IT_STBITERR, ENABLE );
1378       SDIO_DMACmd ( ENABLE );
1379       SD_LowLevel_DMA_RxConfig ( ( uint32_t * )readbuff,
1380         ( NumberOfBlocks * BlockSize ) );
1381       while ( ( SD_DMAEndOfTransferStatus( ) == RESET ) && ( TransferEnd == 0 )
1382         && ( TransferError == SD_OK ) )
1383       {
1384       }
1385       if ( TransferError != SD_OK )
1386       {
1387         return ( TransferError );
1388       }
1389     }
1390   }
1391   return ( errorstatus );
1392 }
1393 
1394 /**
1395  * @brief  Allows to write one block starting from a specified address in a card.
1396  * @param  writebuff: pointer to the buffer that contain the data to be transferred.
1397  * @param  WriteAddr: Address from where data are to be read.
1398  * @param  BlockSize: the SD card Data block size.
1399  * @retval SD_Error: SD Card Error code.
1400  */
1401 SD_Error SD_WriteBlock ( uint8_t * writebuff, uint32_t WriteAddr,
1402   uint16_t BlockSize )
1403 {
1404   SD_Error   errorstatus = SD_OK;
1405   uint8_t    power       = 0, cardstate = 0;
1406   uint32_t   timeout     = 0, bytestransferred = 0;
1407   uint32_t   cardstatus  = 0, count = 0, restwords = 0;
1408   uint32_t * tempbuff    = ( uint32_t* )writebuff;
1409 
1410   if ( writebuff == NULL )
1411   {
1412     errorstatus = SD_INVALID_PARAMETER;
1413     return ( errorstatus );
1414   }
1415 
1416   TransferError = SD_OK;
1417   TransferEnd        = 0;
1418   TotalNumberOfBytes = 0;
1419 
1420   SDIO_DataInitStructure.SDIO_DataTimeOut   = SD_DATATIMEOUT;
1421   SDIO_DataInitStructure.SDIO_DataLength    = 0;
1422   SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_1b;
1423   SDIO_DataInitStructure.SDIO_TransferDir   = SDIO_TransferDir_ToCard;
1424   SDIO_DataInitStructure.SDIO_TransferMode  = SDIO_TransferMode_Block;
1425   SDIO_DataInitStructure.SDIO_DPSM          = SDIO_DPSM_Disable;
1426   SDIO_DataConfig ( & SDIO_DataInitStructure );
1427   SDIO_DMACmd ( DISABLE );
1428 
1429   if ( SDIO_GetResponse( SDIO_RESP1 ) & SD_CARD_LOCKED )
1430   {
1431     errorstatus = SD_LOCK_UNLOCK_FAILED;
1432     return ( errorstatus );
1433   }
1434 
1435   if ( CardType == SDIO_HIGH_CAPACITY_SD_CARD )
1436   {
1437     BlockSize = 512;
1438     WriteAddr /= 512;
1439   }
1440 
1441   /*!< Set the block size, both on controller and card */
1442   if ( ( BlockSize > 0 ) && ( BlockSize <= 2048 ) &&
1443     ( ( BlockSize & ( BlockSize - 1 ) ) == 0 ) )
1444   {
1445     power = convert_from_bytes_to_power_of_two ( BlockSize );
1446 
1447     SDIO_CmdInitStructure.SDIO_Argument = ( uint32_t ) BlockSize;
1448     SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
1449     SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1450     SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
1451     SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
1452     SDIO_SendCommand ( & SDIO_CmdInitStructure );
1453 
1454     errorstatus = CmdResp1Error ( SD_CMD_SET_BLOCKLEN );
1455 
1456     if ( errorstatus != SD_OK )
1457     {
1458       return ( errorstatus );
1459     }
1460   }
1461   else
1462   {
1463     errorstatus = SD_INVALID_PARAMETER;
1464     return ( errorstatus );
1465   }
1466 
1467   /*!< Wait till card is ready for data Added */
1468   SDIO_CmdInitStructure.SDIO_Argument = ( uint32_t )( RCA << 16 );
1469   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_STATUS;
1470   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1471   SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
1472   SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
1473   SDIO_SendCommand ( & SDIO_CmdInitStructure );
1474 
1475   errorstatus = CmdResp1Error ( SD_CMD_SEND_STATUS );
1476 
1477   if ( errorstatus != SD_OK )
1478   {
1479     return ( errorstatus );
1480   }
1481 
1482   cardstatus = SDIO_GetResponse ( SDIO_RESP1 );
1483 
1484   timeout = SD_DATATIMEOUT;
1485 
1486   while ( ( ( cardstatus & 0x00000100 ) == 0 ) && ( timeout > 0 ) )
1487   {
1488     timeout--;
1489     SDIO_CmdInitStructure.SDIO_Argument = ( uint32_t )( RCA << 16 );
1490     SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_STATUS;
1491     SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1492     SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
1493     SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
1494     SDIO_SendCommand ( & SDIO_CmdInitStructure );
1495 
1496     errorstatus = CmdResp1Error ( SD_CMD_SEND_STATUS );
1497 
1498     if ( errorstatus != SD_OK )
1499     {
1500       return ( errorstatus );
1501     }
1502     cardstatus = SDIO_GetResponse ( SDIO_RESP1 );
1503   }
1504 
1505   if ( timeout == 0 )
1506   {
1507     return ( SD_ERROR );
1508   }
1509 
1510   /*!< Send CMD24 WRITE_SINGLE_BLOCK */
1511   SDIO_CmdInitStructure.SDIO_Argument = WriteAddr;
1512   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_WRITE_SINGLE_BLOCK;
1513   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1514   SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
1515   SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
1516   SDIO_SendCommand ( & SDIO_CmdInitStructure );
1517 
1518   errorstatus = CmdResp1Error ( SD_CMD_WRITE_SINGLE_BLOCK );
1519 
1520   if ( errorstatus != SD_OK )
1521   {
1522     return ( errorstatus );
1523   }
1524 
1525   TotalNumberOfBytes = BlockSize;
1526   StopCondition = 0;
1527   SrcBuffer     = ( uint32_t* )writebuff;
1528 
1529   SDIO_DataInitStructure.SDIO_DataTimeOut   = SD_DATATIMEOUT;
1530   SDIO_DataInitStructure.SDIO_DataLength    = BlockSize;
1531   SDIO_DataInitStructure.SDIO_DataBlockSize = ( uint32_t ) power << 4;
1532   SDIO_DataInitStructure.SDIO_TransferDir   = SDIO_TransferDir_ToCard;
1533   SDIO_DataInitStructure.SDIO_TransferMode  = SDIO_TransferMode_Block;
1534   SDIO_DataInitStructure.SDIO_DPSM          = SDIO_DPSM_Enable;
1535   SDIO_DataConfig ( & SDIO_DataInitStructure );
1536 
1537   /*!< In case of single data block transfer no need of stop command at all */
1538   if ( DeviceMode == SD_POLLING_MODE )
1539   {
1540     while ( !( SDIO->STA & ( SDIO_FLAG_DBCKEND | SDIO_FLAG_TXUNDERR |
1541           SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_STBITERR ) ) )
1542     {
1543       if ( SDIO_GetFlagStatus( SDIO_FLAG_TXFIFOHE ) != RESET )
1544       {
1545         if ( ( TotalNumberOfBytes - bytestransferred ) < 32 )
1546         {
1547           restwords = ( ( TotalNumberOfBytes - bytestransferred ) % 4 == 0 ) ?
1548             ( ( TotalNumberOfBytes - bytestransferred ) / 4 ) :
1549             ( ( TotalNumberOfBytes - bytestransferred ) / 4 + 1 );
1550 
1551           for ( count = 0; count < restwords;
1552             count++, tempbuff++, bytestransferred += 4 )
1553           {
1554             SDIO_WriteData ( * tempbuff );
1555           }
1556         }
1557         else
1558         {
1559           for ( count = 0; count < 8; count++ )
1560           {
1561             SDIO_WriteData ( * ( tempbuff + count ) );
1562           }
1563           tempbuff += 8;
1564           bytestransferred += 32;
1565         }
1566       }
1567     }
1568     if ( SDIO_GetFlagStatus( SDIO_FLAG_DTIMEOUT ) != RESET )
1569     {
1570       SDIO_ClearFlag ( SDIO_FLAG_DTIMEOUT );
1571       errorstatus = SD_DATA_TIMEOUT;
1572       return ( errorstatus );
1573     }
1574     else if ( SDIO_GetFlagStatus( SDIO_FLAG_DCRCFAIL ) != RESET )
1575     {
1576       SDIO_ClearFlag ( SDIO_FLAG_DCRCFAIL );
1577       errorstatus = SD_DATA_CRC_FAIL;
1578       return ( errorstatus );
1579     }
1580     else if ( SDIO_GetFlagStatus( SDIO_FLAG_TXUNDERR ) != RESET )
1581     {
1582       SDIO_ClearFlag ( SDIO_FLAG_TXUNDERR );
1583       errorstatus = SD_TX_UNDERRUN;
1584       return ( errorstatus );
1585     }
1586     else if ( SDIO_GetFlagStatus( SDIO_FLAG_STBITERR ) != RESET )
1587     {
1588       SDIO_ClearFlag ( SDIO_FLAG_STBITERR );
1589       errorstatus = SD_START_BIT_ERR;
1590       return ( errorstatus );
1591     }
1592   }
1593   else if ( DeviceMode == SD_INTERRUPT_MODE )
1594   {
1595     SDIO_ITConfig ( SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |
1596       SDIO_FLAG_TXFIFOHE | SDIO_IT_TXUNDERR | SDIO_IT_STBITERR, ENABLE );
1597     while ( ( TransferEnd == 0 ) && ( TransferError == SD_OK ) )
1598     {
1599     }
1600     if ( TransferError != SD_OK )
1601     {
1602       return ( TransferError );
1603     }
1604   }
1605   else if ( DeviceMode == SD_DMA_MODE )
1606   {
1607     SDIO_ITConfig ( SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |
1608       SDIO_IT_TXUNDERR | SDIO_IT_STBITERR, ENABLE );
1609     SD_LowLevel_DMA_TxConfig ( ( uint32_t * )writebuff, BlockSize );
1610     SDIO_DMACmd ( ENABLE );
1611     while ( ( SD_DMAEndOfTransferStatus( ) == RESET ) && ( TransferEnd == 0 ) &&
1612       ( TransferError == SD_OK ) )
1613     {
1614     }
1615     if ( TransferError != SD_OK )
1616     {
1617       return ( TransferError );
1618     }
1619   }
1620 
1621   /*!< Clear all the static flags */
1622   SDIO_ClearFlag ( SDIO_STATIC_FLAGS );
1623 
1624   /*!< Wait till the card is in programming state */
1625   errorstatus = IsCardProgramming ( &cardstate );
1626 
1627   while ( ( errorstatus == SD_OK ) && ( ( cardstate == SD_CARD_PROGRAMMING ) ||
1628       ( cardstate == SD_CARD_RECEIVING ) ) )
1629   {
1630     errorstatus = IsCardProgramming ( &cardstate );
1631   }
1632 
1633   return ( errorstatus );
1634 }
1635 
1636 /**
1637  * @brief  Allows to write blocks starting from a specified address in a card.
1638  * @param  WriteAddr: Address from where data are to be read.
1639  * @param  writebuff: pointer to the buffer that contain the data to be transferred.
1640  * @param  BlockSize: the SD card Data block size.
1641  * @param  NumberOfBlocks: number of blocks to be written.
1642  * @retval SD_Error: SD Card Error code.
1643  */
1644 SD_Error SD_WriteMultiBlocks ( uint8_t * writebuff, uint32_t WriteAddr,
1645   uint16_t BlockSize, uint32_t NumberOfBlocks )
1646 {
1647   SD_Error      errorstatus      = SD_OK;
1648   uint8_t       power            = 0, cardstate = 0;
1649   uint32_t      bytestransferred = 0;
1650   uint32_t      restwords        = 0;
1651   uint32_t *    tempbuff         = ( uint32_t* )writebuff;
1652   __IO uint32_t count            = 0;
1653 
1654   if ( writebuff == NULL )
1655   {
1656     errorstatus = SD_INVALID_PARAMETER;
1657     return ( errorstatus );
1658   }
1659 
1660   TransferError = SD_OK;
1661   TransferEnd        = 0;
1662   TotalNumberOfBytes = 0;
1663 
1664   SDIO_DataInitStructure.SDIO_DataTimeOut   = SD_DATATIMEOUT;
1665   SDIO_DataInitStructure.SDIO_DataLength    = 0;
1666   SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_1b;
1667   SDIO_DataInitStructure.SDIO_TransferDir   = SDIO_TransferDir_ToCard;
1668   SDIO_DataInitStructure.SDIO_TransferMode  = SDIO_TransferMode_Block;
1669   SDIO_DataInitStructure.SDIO_DPSM          = SDIO_DPSM_Disable;
1670   SDIO_DataConfig ( & SDIO_DataInitStructure );
1671   SDIO_DMACmd ( DISABLE );
1672 
1673   if ( SDIO_GetResponse( SDIO_RESP1 ) & SD_CARD_LOCKED )
1674   {
1675     errorstatus = SD_LOCK_UNLOCK_FAILED;
1676     return ( errorstatus );
1677   }
1678 
1679   if ( CardType == SDIO_HIGH_CAPACITY_SD_CARD )
1680   {
1681     BlockSize = 512;
1682     WriteAddr /= 512;
1683   }
1684 
1685   /*!< Set the block size, both on controller and card */
1686   if ( ( BlockSize > 0 ) && ( BlockSize <= 2048 ) &&
1687     ( ( BlockSize & ( BlockSize - 1 ) ) == 0 ) )
1688   {
1689     power = convert_from_bytes_to_power_of_two ( BlockSize );
1690 
1691     SDIO_CmdInitStructure.SDIO_Argument = ( uint32_t ) BlockSize;
1692     SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
1693     SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1694     SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
1695     SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
1696     SDIO_SendCommand ( & SDIO_CmdInitStructure );
1697 
1698     errorstatus = CmdResp1Error ( SD_CMD_SET_BLOCKLEN );
1699 
1700     if ( errorstatus != SD_OK )
1701     {
1702       return ( errorstatus );
1703     }
1704   }
1705   else
1706   {
1707     errorstatus = SD_INVALID_PARAMETER;
1708     return ( errorstatus );
1709   }
1710 
1711   /*!< Wait till card is ready for data Added */
1712   SDIO_CmdInitStructure.SDIO_Argument = ( uint32_t )( RCA << 16 );
1713   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_STATUS;
1714   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1715   SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
1716   SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
1717   SDIO_SendCommand ( & SDIO_CmdInitStructure );
1718 
1719   errorstatus = CmdResp1Error ( SD_CMD_SEND_STATUS );
1720 
1721   if ( errorstatus != SD_OK )
1722   {
1723     return ( errorstatus );
1724   }
1725 
1726   if ( NumberOfBlocks > 1 )
1727   {
1728     /*!< Common to all modes */
1729     if ( NumberOfBlocks * BlockSize > SD_MAX_DATA_LENGTH )
1730     {
1731       errorstatus = SD_INVALID_PARAMETER;
1732       return ( errorstatus );
1733     }
1734 
1735     if ( ( SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType ) ||
1736       ( SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType ) ||
1737       ( SDIO_HIGH_CAPACITY_SD_CARD == CardType ) )
1738     {
1739       /*!< To improve performance */
1740       SDIO_CmdInitStructure.SDIO_Argument = ( uint32_t )( RCA << 16 );
1741       SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
1742       SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1743       SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
1744       SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
1745       SDIO_SendCommand ( & SDIO_CmdInitStructure );
1746 
1747       errorstatus = CmdResp1Error ( SD_CMD_APP_CMD );
1748 
1749       if ( errorstatus != SD_OK )
1750       {
1751         return ( errorstatus );
1752       }
1753       /*!< To improve performance */
1754       SDIO_CmdInitStructure.SDIO_Argument = ( uint32_t )NumberOfBlocks;
1755       SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCK_COUNT;
1756       SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1757       SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
1758       SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
1759       SDIO_SendCommand ( & SDIO_CmdInitStructure );
1760 
1761       errorstatus = CmdResp1Error ( SD_CMD_SET_BLOCK_COUNT );
1762 
1763       if ( errorstatus != SD_OK )
1764       {
1765         return ( errorstatus );
1766       }
1767     }
1768 
1769     /*!< Send CMD25 WRITE_MULT_BLOCK with argument data address */
1770     SDIO_CmdInitStructure.SDIO_Argument = ( uint32_t )WriteAddr;
1771     SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_WRITE_MULT_BLOCK;
1772     SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1773     SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
1774     SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
1775     SDIO_SendCommand ( & SDIO_CmdInitStructure );
1776 
1777     errorstatus = CmdResp1Error ( SD_CMD_WRITE_MULT_BLOCK );
1778 
1779     if ( SD_OK != errorstatus )
1780     {
1781       return ( errorstatus );
1782     }
1783 
1784     TotalNumberOfBytes = NumberOfBlocks * BlockSize;
1785     StopCondition = 1;
1786     SrcBuffer     = ( uint32_t* )writebuff;
1787 
1788     SDIO_DataInitStructure.SDIO_DataTimeOut   = SD_DATATIMEOUT;
1789     SDIO_DataInitStructure.SDIO_DataLength    = NumberOfBlocks * BlockSize;
1790     SDIO_DataInitStructure.SDIO_DataBlockSize = ( uint32_t ) power << 4;
1791     SDIO_DataInitStructure.SDIO_TransferDir   = SDIO_TransferDir_ToCard;
1792     SDIO_DataInitStructure.SDIO_TransferMode  = SDIO_TransferMode_Block;
1793     SDIO_DataInitStructure.SDIO_DPSM          = SDIO_DPSM_Enable;
1794     SDIO_DataConfig ( & SDIO_DataInitStructure );
1795 
1796     if ( DeviceMode == SD_POLLING_MODE )
1797     {
1798       while ( !( SDIO->STA &
1799           ( SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DATAEND |
1800             SDIO_FLAG_DTIMEOUT | SDIO_FLAG_STBITERR ) ) )
1801       {
1802         if ( SDIO_GetFlagStatus( SDIO_FLAG_TXFIFOHE ) != RESET )
1803         {
1804           if ( !( ( TotalNumberOfBytes - bytestransferred ) <
1805               SD_HALFFIFOBYTES ) )
1806           {
1807             for ( count = 0; count < SD_HALFFIFO; count++ )
1808             {
1809               SDIO_WriteData ( * ( tempbuff + count ) );
1810             }
1811             tempbuff += SD_HALFFIFO;
1812             bytestransferred += SD_HALFFIFOBYTES;
1813           }
1814           else
1815           {
1816             restwords = ( ( TotalNumberOfBytes - bytestransferred ) % 4 == 0 ) ?
1817               ( ( TotalNumberOfBytes - bytestransferred ) / 4 ) :
1818               ( ( TotalNumberOfBytes - bytestransferred ) / 4 + 1 );
1819 
1820             for ( count = 0; count < restwords;
1821               count++, tempbuff++, bytestransferred += 4 )
1822             {
1823               SDIO_WriteData ( * tempbuff );
1824             }
1825           }
1826         }
1827       }
1828 
1829       if ( SDIO_GetFlagStatus( SDIO_FLAG_DTIMEOUT ) != RESET )
1830       {
1831         SDIO_ClearFlag ( SDIO_FLAG_DTIMEOUT );
1832         errorstatus = SD_DATA_TIMEOUT;
1833         return ( errorstatus );
1834       }
1835       else if ( SDIO_GetFlagStatus( SDIO_FLAG_DCRCFAIL ) != RESET )
1836       {
1837         SDIO_ClearFlag ( SDIO_FLAG_DCRCFAIL );
1838         errorstatus = SD_DATA_CRC_FAIL;
1839         return ( errorstatus );
1840       }
1841       else if ( SDIO_GetFlagStatus( SDIO_FLAG_TXUNDERR ) != RESET )
1842       {
1843         SDIO_ClearFlag ( SDIO_FLAG_TXUNDERR );
1844         errorstatus = SD_TX_UNDERRUN;
1845         return ( errorstatus );
1846       }
1847       else if ( SDIO_GetFlagStatus( SDIO_FLAG_STBITERR ) != RESET )
1848       {
1849         SDIO_ClearFlag ( SDIO_FLAG_STBITERR );
1850         errorstatus = SD_START_BIT_ERR;
1851         return ( errorstatus );
1852       }
1853 
1854       if ( SDIO_GetFlagStatus( SDIO_FLAG_DATAEND ) != RESET )
1855       {
1856         if ( ( SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType ) ||
1857           ( SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType ) ||
1858           ( SDIO_HIGH_CAPACITY_SD_CARD == CardType ) )
1859         {
1860           /*!< Send CMD12 STOP_TRANSMISSION */
1861           SDIO_CmdInitStructure.SDIO_Argument = 0x0;
1862           SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_STOP_TRANSMISSION;
1863           SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1864           SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
1865           SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
1866           SDIO_SendCommand ( & SDIO_CmdInitStructure );
1867 
1868           errorstatus = CmdResp1Error ( SD_CMD_STOP_TRANSMISSION );
1869 
1870           if ( errorstatus != SD_OK )
1871           {
1872             return ( errorstatus );
1873           }
1874         }
1875       }
1876     }
1877     else if ( DeviceMode == SD_INTERRUPT_MODE )
1878     {
1879       SDIO_ITConfig ( SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |
1880         SDIO_IT_TXFIFOHE | SDIO_IT_TXUNDERR | SDIO_IT_STBITERR, ENABLE );
1881       while ( ( TransferEnd == 0 ) && ( TransferError == SD_OK ) )
1882       {
1883       }
1884       if ( TransferError != SD_OK )
1885       {
1886         return ( TransferError );
1887       }
1888     }
1889     else if ( DeviceMode == SD_DMA_MODE )
1890     {
1891       SDIO_ITConfig ( SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |
1892         SDIO_IT_TXUNDERR | SDIO_IT_STBITERR, ENABLE );
1893       SDIO_DMACmd ( ENABLE );
1894       SD_LowLevel_DMA_TxConfig ( ( uint32_t * )writebuff,
1895         ( NumberOfBlocks * BlockSize ) );
1896       while ( ( SD_DMAEndOfTransferStatus( ) == RESET ) && ( TransferEnd == 0 )
1897         && ( TransferError == SD_OK ) )
1898       {
1899       }
1900       if ( TransferError != SD_OK )
1901       {
1902         return ( TransferError );
1903       }
1904     }
1905   }
1906   /*!< Clear all the static flags */
1907   SDIO_ClearFlag ( SDIO_STATIC_FLAGS );
1908 
1909   /*!< Add some delay before checking the Card Status */
1910   for ( count = 0; count < 0xFFFF; count++ )
1911   {
1912   }
1913   /*!< Wait till the card is in programming state */
1914   errorstatus = IsCardProgramming ( &cardstate );
1915 
1916   while ( ( errorstatus == SD_OK ) && ( ( cardstate == SD_CARD_PROGRAMMING ) ||
1917       ( cardstate == SD_CARD_RECEIVING ) ) )
1918   {
1919     errorstatus = IsCardProgramming ( &cardstate );
1920   }
1921 
1922   return ( errorstatus );
1923 }
1924 
1925 /**
1926  * @brief  Gets the cuurent data transfer state.
1927  * @param  None
1928  * @retval SDTransferState: Data Transfer state.
1929  *   This value can be:
1930  *        - SD_TRANSFER_OK: No data transfer is acting
1931  *        - SD_TRANSFER_BUSY: Data transfer is acting
1932  */
1933 SDTransferState SD_GetTransferState ( void )
1934 {
1935   if ( SDIO->STA & ( SDIO_FLAG_TXACT | SDIO_FLAG_RXACT ) )
1936   {
1937     return ( SD_TRANSFER_BUSY );
1938   }
1939   else
1940   {
1941     return ( SD_TRANSFER_OK );
1942   }
1943 }
1944 
1945 /**
1946  * @brief  Aborts an ongoing data transfer.
1947  * @param  None
1948  * @retval SD_Error: SD Card Error code.
1949  */
1950 SD_Error SD_StopTransfer ( void )
1951 {
1952   SD_Error errorstatus = SD_OK;
1953 
1954   /*!< Send CMD12 STOP_TRANSMISSION  */
1955   SDIO_CmdInitStructure.SDIO_Argument = 0x0;
1956   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_STOP_TRANSMISSION;
1957   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1958   SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
1959   SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
1960   SDIO_SendCommand ( & SDIO_CmdInitStructure );
1961 
1962   errorstatus = CmdResp1Error ( SD_CMD_STOP_TRANSMISSION );
1963 
1964   return ( errorstatus );
1965 }
1966 
1967 /**
1968  * @brief  Allows to erase memory area specified for the given card.
1969  * @param  startaddr: the start address.
1970  * @param  endaddr: the end address.
1971  * @retval SD_Error: SD Card Error code.
1972  */
1973 SD_Error SD_Erase ( uint32_t startaddr, uint32_t endaddr )
1974 {
1975   SD_Error      errorstatus = SD_OK;
1976   uint32_t      delay       = 0;
1977   __IO uint32_t maxdelay    = 0;
1978   uint8_t       cardstate   = 0;
1979 
1980   /*!< Check if the card coomnd class supports erase command */
1981   if ( ( ( CSD_Tab[ 1 ] >> 20 ) & SD_CCCC_ERASE ) == 0 )
1982   {
1983     errorstatus = SD_REQUEST_NOT_APPLICABLE;
1984     return ( errorstatus );
1985   }
1986 
1987   maxdelay = 120000 / ( ( SDIO->CLKCR & 0xFF ) + 2 );
1988 
1989   if ( SDIO_GetResponse( SDIO_RESP1 ) & SD_CARD_LOCKED )
1990   {
1991     errorstatus = SD_LOCK_UNLOCK_FAILED;
1992     return ( errorstatus );
1993   }
1994 
1995   if ( CardType == SDIO_HIGH_CAPACITY_SD_CARD )
1996   {
1997     startaddr /= 512;
1998     endaddr /= 512;
1999   }
2000 
2001   /*!< According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
2002   if ( ( SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType ) ||
2003     ( SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType ) ||
2004     ( SDIO_HIGH_CAPACITY_SD_CARD == CardType ) )
2005   {
2006     /*!< Send CMD32 SD_ERASE_GRP_START with argument as addr  */
2007     SDIO_CmdInitStructure.SDIO_Argument = startaddr;
2008     SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_ERASE_GRP_START;
2009     SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
2010     SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
2011     SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
2012     SDIO_SendCommand ( & SDIO_CmdInitStructure );
2013 
2014     errorstatus = CmdResp1Error ( SD_CMD_SD_ERASE_GRP_START );
2015     if ( errorstatus != SD_OK )
2016     {
2017       return ( errorstatus );
2018     }
2019 
2020     /*!< Send CMD33 SD_ERASE_GRP_END with argument as addr  */
2021     SDIO_CmdInitStructure.SDIO_Argument = endaddr;
2022     SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_ERASE_GRP_END;
2023     SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
2024     SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
2025     SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
2026     SDIO_SendCommand ( & SDIO_CmdInitStructure );
2027 
2028     errorstatus = CmdResp1Error ( SD_CMD_SD_ERASE_GRP_END );
2029     if ( errorstatus != SD_OK )
2030     {
2031       return ( errorstatus );
2032     }
2033   }
2034 
2035   /*!< Send CMD38 ERASE */
2036   SDIO_CmdInitStructure.SDIO_Argument = 0;
2037   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_ERASE;
2038   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
2039   SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
2040   SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
2041   SDIO_SendCommand ( & SDIO_CmdInitStructure );
2042 
2043   errorstatus = CmdResp1Error ( SD_CMD_ERASE );
2044 
2045   if ( errorstatus != SD_OK )
2046   {
2047     return ( errorstatus );
2048   }
2049 
2050   for ( delay = 0; delay < maxdelay; delay++ )
2051   {
2052   }
2053 
2054   /*!< Wait till the card is in programming state */
2055   errorstatus = IsCardProgramming ( &cardstate );
2056 
2057   while ( ( errorstatus == SD_OK ) && ( ( SD_CARD_PROGRAMMING == cardstate ) ||
2058       ( SD_CARD_RECEIVING == cardstate ) ) )
2059   {
2060     errorstatus = IsCardProgramming ( &cardstate );
2061   }
2062 
2063   return ( errorstatus );
2064 }
2065 
2066 /**
2067  * @brief  Returns the current card's status.
2068  * @param  pcardstatus: pointer to the buffer that will contain the SD card
2069  *         status (Card Status register).
2070  * @retval SD_Error: SD Card Error code.
2071  */
2072 SD_Error SD_SendStatus ( uint32_t * pcardstatus )
2073 {
2074   SD_Error errorstatus = SD_OK;
2075 
2076   if ( pcardstatus == NULL )
2077   {
2078     errorstatus = SD_INVALID_PARAMETER;
2079     return ( errorstatus );
2080   }
2081 
2082   SDIO_CmdInitStructure.SDIO_Argument = ( uint32_t ) RCA << 16;
2083   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_STATUS;
2084   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
2085   SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
2086   SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
2087   SDIO_SendCommand ( & SDIO_CmdInitStructure );
2088 
2089   errorstatus = CmdResp1Error ( SD_CMD_SEND_STATUS );
2090 
2091   if ( errorstatus != SD_OK )
2092   {
2093     return ( errorstatus );
2094   }
2095 
2096   *pcardstatus = SDIO_GetResponse ( SDIO_RESP1 );
2097 
2098   return ( errorstatus );
2099 }
2100 
2101 /**
2102  * @brief  Returns the current SD card's status.
2103  * @param  psdstatus: pointer to the buffer that will contain the SD card status
2104  *         (SD Status register).
2105  * @retval SD_Error: SD Card Error code.
2106  */
2107 SD_Error SD_SendSDStatus ( uint32_t * psdstatus )
2108 {
2109   SD_Error errorstatus = SD_OK;
2110   uint32_t count       = 0;
2111 
2112   if ( SDIO_GetResponse( SDIO_RESP1 ) & SD_CARD_LOCKED )
2113   {
2114     errorstatus = SD_LOCK_UNLOCK_FAILED;
2115     return ( errorstatus );
2116   }
2117 
2118   /*!< Set block size for card if it is not equal to current block size for card. */
2119   SDIO_CmdInitStructure.SDIO_Argument = 64;
2120   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
2121   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
2122   SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
2123   SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
2124   SDIO_SendCommand ( & SDIO_CmdInitStructure );
2125 
2126   errorstatus = CmdResp1Error ( SD_CMD_SET_BLOCKLEN );
2127 
2128   if ( errorstatus != SD_OK )
2129   {
2130     return ( errorstatus );
2131   }
2132 
2133   /*!< CMD55 */
2134   SDIO_CmdInitStructure.SDIO_Argument = ( uint32_t ) RCA << 16;
2135   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
2136   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
2137   SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
2138   SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
2139   SDIO_SendCommand ( & SDIO_CmdInitStructure );
2140   errorstatus = CmdResp1Error ( SD_CMD_APP_CMD );
2141 
2142   if ( errorstatus != SD_OK )
2143   {
2144     return ( errorstatus );
2145   }
2146 
2147   SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
2148   SDIO_DataInitStructure.SDIO_DataLength    = 64;
2149   SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_64b;
2150   SDIO_DataInitStructure.SDIO_TransferDir   = SDIO_TransferDir_ToSDIO;
2151   SDIO_DataInitStructure.SDIO_TransferMode  = SDIO_TransferMode_Block;
2152   SDIO_DataInitStructure.SDIO_DPSM          = SDIO_DPSM_Enable;
2153   SDIO_DataConfig ( & SDIO_DataInitStructure );
2154 
2155   /*!< Send ACMD13 SD_APP_STAUS  with argument as card's RCA.*/
2156   SDIO_CmdInitStructure.SDIO_Argument = 0;
2157   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_APP_STAUS;
2158   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
2159   SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
2160   SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
2161   SDIO_SendCommand ( & SDIO_CmdInitStructure );
2162   errorstatus = CmdResp1Error ( SD_CMD_SD_APP_STAUS );
2163 
2164   if ( errorstatus != SD_OK )
2165   {
2166     return ( errorstatus );
2167   }
2168 
2169   while ( !( SDIO->STA & ( SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL |
2170         SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR ) ) )
2171   {
2172     if ( SDIO_GetFlagStatus( SDIO_FLAG_RXFIFOHF ) != RESET )
2173     {
2174       for ( count = 0; count < 8; count++ )
2175       {
2176         *( psdstatus + count ) = SDIO_ReadData ( );
2177       }
2178       psdstatus += 8;
2179     }
2180   }
2181 
2182   if ( SDIO_GetFlagStatus( SDIO_FLAG_DTIMEOUT ) != RESET )
2183   {
2184     SDIO_ClearFlag ( SDIO_FLAG_DTIMEOUT );
2185     errorstatus = SD_DATA_TIMEOUT;
2186     return ( errorstatus );
2187   }
2188   else if ( SDIO_GetFlagStatus( SDIO_FLAG_DCRCFAIL ) != RESET )
2189   {
2190     SDIO_ClearFlag ( SDIO_FLAG_DCRCFAIL );
2191     errorstatus = SD_DATA_CRC_FAIL;
2192     return ( errorstatus );
2193   }
2194   else if ( SDIO_GetFlagStatus( SDIO_FLAG_RXOVERR ) != RESET )
2195   {
2196     SDIO_ClearFlag ( SDIO_FLAG_RXOVERR );
2197     errorstatus = SD_RX_OVERRUN;
2198     return ( errorstatus );
2199   }
2200   else if ( SDIO_GetFlagStatus( SDIO_FLAG_STBITERR ) != RESET )
2201   {
2202     SDIO_ClearFlag ( SDIO_FLAG_STBITERR );
2203     errorstatus = SD_START_BIT_ERR;
2204     return ( errorstatus );
2205   }
2206 
2207   while ( SDIO_GetFlagStatus( SDIO_FLAG_RXDAVL ) != RESET )
2208   {
2209     *psdstatus = SDIO_ReadData ( );
2210     psdstatus++;
2211   }
2212 
2213   /*!< Clear all the static status flags*/
2214   SDIO_ClearFlag ( SDIO_STATIC_FLAGS );
2215   psdstatus -= 16;
2216   for ( count = 0; count < 16; count++ )
2217   {
2218     psdstatus[ count ] = ( ( psdstatus[ count ] & SD_0TO7BITS ) << 24 ) |
2219       ( ( psdstatus[ count ] & SD_8TO15BITS ) << 8 ) |
2220       ( ( psdstatus[ count ] & SD_16TO23BITS ) >> 8 ) |
2221       ( ( psdstatus[ count ] & SD_24TO31BITS ) >> 24 );
2222   }
2223   return ( errorstatus );
2224 }
2225 
2226 /**
2227  * @brief  Allows to process all the interrupts that are high.
2228  * @param  None
2229  * @retval SD_Error: SD Card Error code.
2230  */
2231 SD_Error SD_ProcessIRQSrc ( void )
2232 {
2233   uint32_t count = 0, restwords = 0;
2234 
2235   if ( DeviceMode == SD_INTERRUPT_MODE )
2236   {
2237     if ( SDIO_GetITStatus( SDIO_IT_RXFIFOHF ) != RESET )
2238     {
2239       for ( count = 0; count < SD_HALFFIFO; count++ )
2240       {
2241         *( DestBuffer + count ) = SDIO_ReadData ( );
2242       }
2243       DestBuffer += SD_HALFFIFO;
2244       NumberOfBytes += SD_HALFFIFOBYTES;
2245     }
2246     else if ( SDIO_GetITStatus( SDIO_IT_TXFIFOHE ) != RESET )
2247     {
2248       if ( ( TotalNumberOfBytes - NumberOfBytes ) < SD_HALFFIFOBYTES )
2249       {
2250         restwords = ( ( TotalNumberOfBytes - NumberOfBytes ) % 4 == 0 ) ?
2251           ( ( TotalNumberOfBytes - NumberOfBytes ) / 4 ) :
2252           ( ( TotalNumberOfBytes - NumberOfBytes ) / 4 + 1 );
2253 
2254         for ( count = 0; count < restwords; count++, SrcBuffer++,
2255           NumberOfBytes += 4 )
2256         {
2257           SDIO_WriteData ( * SrcBuffer );
2258         }
2259       }
2260       else
2261       {
2262         for ( count = 0; count < SD_HALFFIFO; count++ )
2263         {
2264           SDIO_WriteData ( * ( SrcBuffer + count ) );
2265         }
2266 
2267         SrcBuffer += SD_HALFFIFO;
2268         NumberOfBytes += SD_HALFFIFOBYTES;
2269       }
2270     }
2271   }
2272 
2273   if ( SDIO_GetITStatus( SDIO_IT_DATAEND ) != RESET )
2274   {
2275     if ( DeviceMode != SD_DMA_MODE )
2276     {
2277       while ( ( SDIO_GetFlagStatus( SDIO_FLAG_RXDAVL ) != RESET ) &&
2278         ( NumberOfBytes < TotalNumberOfBytes ) )
2279       {
2280         *DestBuffer = SDIO_ReadData ( );
2281         DestBuffer++;
2282         NumberOfBytes += 4;
2283       }
2284     }
2285 
2286     if ( StopCondition == 1 )
2287     {
2288       TransferError = SD_StopTransfer ( );
2289     }
2290     else
2291     {
2292       TransferError = SD_OK;
2293     }
2294     SDIO_ClearITPendingBit ( SDIO_IT_DATAEND );
2295     SDIO_ITConfig ( SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |
2296       SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR | SDIO_IT_RXOVERR |
2297       SDIO_IT_STBITERR, DISABLE );
2298     TransferEnd   = 1;
2299     NumberOfBytes = 0;
2300     return ( TransferError );
2301   }
2302 
2303   if ( SDIO_GetITStatus( SDIO_IT_DCRCFAIL ) != RESET )
2304   {
2305     SDIO_ClearITPendingBit ( SDIO_IT_DCRCFAIL );
2306     SDIO_ITConfig ( SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |
2307       SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR | SDIO_IT_RXOVERR |
2308       SDIO_IT_STBITERR, DISABLE );
2309     NumberOfBytes = 0;
2310     TransferError = SD_DATA_CRC_FAIL;
2311     return ( SD_DATA_CRC_FAIL );
2312   }
2313 
2314   if ( SDIO_GetITStatus( SDIO_IT_DTIMEOUT ) != RESET )
2315   {
2316     SDIO_ClearITPendingBit ( SDIO_IT_DTIMEOUT );
2317     SDIO_ITConfig ( SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |
2318       SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR | SDIO_IT_RXOVERR |
2319       SDIO_IT_STBITERR, DISABLE );
2320     NumberOfBytes = 0;
2321     TransferError = SD_DATA_TIMEOUT;
2322     return ( SD_DATA_TIMEOUT );
2323   }
2324 
2325   if ( SDIO_GetITStatus( SDIO_IT_RXOVERR ) != RESET )
2326   {
2327     SDIO_ClearITPendingBit ( SDIO_IT_RXOVERR );
2328     SDIO_ITConfig ( SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |
2329       SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR | SDIO_IT_RXOVERR |
2330       SDIO_IT_STBITERR, DISABLE );
2331     NumberOfBytes = 0;
2332     TransferError = SD_RX_OVERRUN;
2333     return ( SD_RX_OVERRUN );
2334   }
2335 
2336   if ( SDIO_GetITStatus( SDIO_IT_TXUNDERR ) != RESET )
2337   {
2338     SDIO_ClearITPendingBit ( SDIO_IT_TXUNDERR );
2339     SDIO_ITConfig ( SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |
2340       SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR | SDIO_IT_RXOVERR |
2341       SDIO_IT_STBITERR, DISABLE );
2342     NumberOfBytes = 0;
2343     TransferError = SD_TX_UNDERRUN;
2344     return ( SD_TX_UNDERRUN );
2345   }
2346 
2347   if ( SDIO_GetITStatus( SDIO_IT_STBITERR ) != RESET )
2348   {
2349     SDIO_ClearITPendingBit ( SDIO_IT_STBITERR );
2350     SDIO_ITConfig ( SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |
2351       SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR | SDIO_IT_RXOVERR |
2352       SDIO_IT_STBITERR, DISABLE );
2353     NumberOfBytes = 0;
2354     TransferError = SD_START_BIT_ERR;
2355     return ( SD_START_BIT_ERR );
2356   }
2357 
2358   return ( SD_OK );
2359 }
2360 
2361 /**
2362  * @brief  Checks for error conditions for CMD0.
2363  * @param  None
2364  * @retval SD_Error: SD Card Error code.
2365  */
2366 static SD_Error CmdError ( void )
2367 {
2368   SD_Error errorstatus = SD_OK;
2369   uint32_t timeout;
2370 
2371   timeout = SDIO_CMD0TIMEOUT; /*!< 10000 */
2372 
2373   while ( ( timeout > 0 ) && ( SDIO_GetFlagStatus( SDIO_FLAG_CMDSENT )
2374       == RESET ) )
2375   {
2376     timeout--;
2377   }
2378 
2379   if ( timeout == 0 )
2380   {
2381     errorstatus = SD_CMD_RSP_TIMEOUT;
2382     return ( errorstatus );
2383   }
2384 
2385   /*!< Clear all the static flags */
2386   SDIO_ClearFlag ( SDIO_STATIC_FLAGS );
2387 
2388   return ( errorstatus );
2389 }
2390 
2391 /**
2392  * @brief  Checks for error conditions for R7 response.
2393  * @param  None
2394  * @retval SD_Error: SD Card Error code.
2395  */
2396 static SD_Error CmdResp7Error ( void )
2397 {
2398   SD_Error errorstatus = SD_OK;
2399   uint32_t status;
2400   uint32_t timeout = SDIO_CMD0TIMEOUT;
2401 
2402   status = SDIO->STA;
2403 
2404   while ( !( status & ( SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND |
2405         SDIO_FLAG_CTIMEOUT ) ) && ( timeout > 0 ) )
2406   {
2407     timeout--;
2408     status = SDIO->STA;
2409   }
2410 
2411   if ( ( timeout == 0 ) || ( status & SDIO_FLAG_CTIMEOUT ) )
2412   {
2413     /*!< Card is not V2.0 complient or card does not support the set voltage range */
2414     errorstatus = SD_CMD_RSP_TIMEOUT;
2415     SDIO_ClearFlag ( SDIO_FLAG_CTIMEOUT );
2416     return ( errorstatus );
2417   }
2418 
2419   if ( status & SDIO_FLAG_CMDREND )
2420   {
2421     /*!< Card is SD V2.0 compliant */
2422     errorstatus = SD_OK;
2423     SDIO_ClearFlag ( SDIO_FLAG_CMDREND );
2424     return ( errorstatus );
2425   }
2426   return ( errorstatus );
2427 }
2428 
2429 /**
2430  * @brief  Checks for error conditions for R1 response.
2431  * @param  cmd: The sent command index.
2432  * @retval SD_Error: SD Card Error code.
2433  */
2434 static SD_Error CmdResp1Error ( uint8_t cmd )
2435 {
2436   SD_Error errorstatus = SD_OK;
2437   uint32_t status;
2438   uint32_t response_r1;
2439 
2440   status = SDIO->STA;
2441 
2442   while ( !( status & ( SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND |
2443         SDIO_FLAG_CTIMEOUT ) ) )
2444   {
2445     status = SDIO->STA;
2446   }
2447 
2448   if ( status & SDIO_FLAG_CTIMEOUT )
2449   {
2450     errorstatus = SD_CMD_RSP_TIMEOUT;
2451     SDIO_ClearFlag ( SDIO_FLAG_CTIMEOUT );
2452     return ( errorstatus );
2453   }
2454   else if ( status & SDIO_FLAG_CCRCFAIL )
2455   {
2456     errorstatus = SD_CMD_CRC_FAIL;
2457     SDIO_ClearFlag ( SDIO_FLAG_CCRCFAIL );
2458     return ( errorstatus );
2459   }
2460 
2461   /*!< Check response received is of desired command */
2462   if ( SDIO_GetCommandResponse( ) != cmd )
2463   {
2464     errorstatus = SD_ILLEGAL_CMD;
2465     return ( errorstatus );
2466   }
2467 
2468   /*!< Clear all the static flags */
2469   SDIO_ClearFlag ( SDIO_STATIC_FLAGS );
2470 
2471   /*!< We have received response, retrieve it for analysis  */
2472   response_r1 = SDIO_GetResponse ( SDIO_RESP1 );
2473 
2474   if ( ( response_r1 & SD_OCR_ERRORBITS ) == SD_ALLZERO )
2475   {
2476     return ( errorstatus );
2477   }
2478 
2479   if ( response_r1 & SD_OCR_ADDR_OUT_OF_RANGE )
2480   {
2481     return ( SD_ADDR_OUT_OF_RANGE );
2482   }
2483 
2484   if ( response_r1 & SD_OCR_ADDR_MISALIGNED )
2485   {
2486     return ( SD_ADDR_MISALIGNED );
2487   }
2488 
2489   if ( response_r1 & SD_OCR_BLOCK_LEN_ERR )
2490   {
2491     return ( SD_BLOCK_LEN_ERR );
2492   }
2493 
2494   if ( response_r1 & SD_OCR_ERASE_SEQ_ERR )
2495   {
2496     return ( SD_ERASE_SEQ_ERR );
2497   }
2498 
2499   if ( response_r1 & SD_OCR_BAD_ERASE_PARAM )
2500   {
2501     return ( SD_BAD_ERASE_PARAM );
2502   }
2503 
2504   if ( response_r1 & SD_OCR_WRITE_PROT_VIOLATION )
2505   {
2506     return ( SD_WRITE_PROT_VIOLATION );
2507   }
2508 
2509   if ( response_r1 & SD_OCR_LOCK_UNLOCK_FAILED )
2510   {
2511     return ( SD_LOCK_UNLOCK_FAILED );
2512   }
2513 
2514   if ( response_r1 & SD_OCR_COM_CRC_FAILED )
2515   {
2516     return ( SD_COM_CRC_FAILED );
2517   }
2518 
2519   if ( response_r1 & SD_OCR_ILLEGAL_CMD )
2520   {
2521     return ( SD_ILLEGAL_CMD );
2522   }
2523 
2524   if ( response_r1 & SD_OCR_CARD_ECC_FAILED )
2525   {
2526     return ( SD_CARD_ECC_FAILED );
2527   }
2528 
2529   if ( response_r1 & SD_OCR_CC_ERROR )
2530   {
2531     return ( SD_CC_ERROR );
2532   }
2533 
2534   if ( response_r1 & SD_OCR_GENERAL_UNKNOWN_ERROR )
2535   {
2536     return ( SD_GENERAL_UNKNOWN_ERROR );
2537   }
2538 
2539   if ( response_r1 & SD_OCR_STREAM_READ_UNDERRUN )
2540   {
2541     return ( SD_STREAM_READ_UNDERRUN );
2542   }
2543 
2544   if ( response_r1 & SD_OCR_STREAM_WRITE_OVERRUN )
2545   {
2546     return ( SD_STREAM_WRITE_OVERRUN );
2547   }
2548 
2549   if ( response_r1 & SD_OCR_CID_CSD_OVERWRIETE )
2550   {
2551     return ( SD_CID_CSD_OVERWRITE );
2552   }
2553 
2554   if ( response_r1 & SD_OCR_WP_ERASE_SKIP )
2555   {
2556     return ( SD_WP_ERASE_SKIP );
2557   }
2558 
2559   if ( response_r1 & SD_OCR_CARD_ECC_DISABLED )
2560   {
2561     return ( SD_CARD_ECC_DISABLED );
2562   }
2563 
2564   if ( response_r1 & SD_OCR_ERASE_RESET )
2565   {
2566     return ( SD_ERASE_RESET );
2567   }
2568 
2569   if ( response_r1 & SD_OCR_AKE_SEQ_ERROR )
2570   {
2571     return ( SD_AKE_SEQ_ERROR );
2572   }
2573   return ( errorstatus );
2574 }
2575 
2576 /**
2577  * @brief  Checks for error conditions for R3 (OCR) response.
2578  * @param  None
2579  * @retval SD_Error: SD Card Error code.
2580  */
2581 static SD_Error CmdResp3Error ( void )
2582 {
2583   SD_Error errorstatus = SD_OK;
2584   uint32_t status;
2585 
2586   status = SDIO->STA;
2587 
2588   while ( !( status & ( SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND |
2589         SDIO_FLAG_CTIMEOUT ) ) )
2590   {
2591     status = SDIO->STA;
2592   }
2593 
2594   if ( status & SDIO_FLAG_CTIMEOUT )
2595   {
2596     errorstatus = SD_CMD_RSP_TIMEOUT;
2597     SDIO_ClearFlag ( SDIO_FLAG_CTIMEOUT );
2598     return ( errorstatus );
2599   }
2600   /*!< Clear all the static flags */
2601   SDIO_ClearFlag ( SDIO_STATIC_FLAGS );
2602   return ( errorstatus );
2603 }
2604 
2605 /**
2606  * @brief  Checks for error conditions for R2 (CID or CSD) response.
2607  * @param  None
2608  * @retval SD_Error: SD Card Error code.
2609  */
2610 static SD_Error CmdResp2Error ( void )
2611 {
2612   SD_Error errorstatus = SD_OK;
2613   uint32_t status;
2614 
2615   status = SDIO->STA;
2616 
2617   while ( !( status & ( SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CTIMEOUT |
2618         SDIO_FLAG_CMDREND ) ) )
2619   {
2620     status = SDIO->STA;
2621   }
2622 
2623   if ( status & SDIO_FLAG_CTIMEOUT )
2624   {
2625     errorstatus = SD_CMD_RSP_TIMEOUT;
2626     SDIO_ClearFlag ( SDIO_FLAG_CTIMEOUT );
2627     return ( errorstatus );
2628   }
2629   else if ( status & SDIO_FLAG_CCRCFAIL )
2630   {
2631     errorstatus = SD_CMD_CRC_FAIL;
2632     SDIO_ClearFlag ( SDIO_FLAG_CCRCFAIL );
2633     return ( errorstatus );
2634   }
2635 
2636   /*!< Clear all the static flags */
2637   SDIO_ClearFlag ( SDIO_STATIC_FLAGS );
2638 
2639   return ( errorstatus );
2640 }
2641 
2642 /**
2643  * @brief  Checks for error conditions for R6 (RCA) response.
2644  * @param  cmd: The sent command index.
2645  * @param  prca: pointer to the variable that will contain the SD card relative
2646  *         address RCA.
2647  * @retval SD_Error: SD Card Error code.
2648  */
2649 static SD_Error CmdResp6Error ( uint8_t cmd, uint16_t * prca )
2650 {
2651   SD_Error errorstatus = SD_OK;
2652   uint32_t status;
2653   uint32_t response_r1;
2654 
2655   status = SDIO->STA;
2656 
2657   while ( !( status & ( SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CTIMEOUT |
2658         SDIO_FLAG_CMDREND ) ) )
2659   {
2660     status = SDIO->STA;
2661   }
2662 
2663   if ( status & SDIO_FLAG_CTIMEOUT )
2664   {
2665     errorstatus = SD_CMD_RSP_TIMEOUT;
2666     SDIO_ClearFlag ( SDIO_FLAG_CTIMEOUT );
2667     return ( errorstatus );
2668   }
2669   else if ( status & SDIO_FLAG_CCRCFAIL )
2670   {
2671     errorstatus = SD_CMD_CRC_FAIL;
2672     SDIO_ClearFlag ( SDIO_FLAG_CCRCFAIL );
2673     return ( errorstatus );
2674   }
2675 
2676   /*!< Check response received is of desired command */
2677   if ( SDIO_GetCommandResponse( ) != cmd )
2678   {
2679     errorstatus = SD_ILLEGAL_CMD;
2680     return ( errorstatus );
2681   }
2682 
2683   /*!< Clear all the static flags */
2684   SDIO_ClearFlag ( SDIO_STATIC_FLAGS );
2685 
2686   /*!< We have received response, retrieve it.  */
2687   response_r1 = SDIO_GetResponse ( SDIO_RESP1 );
2688 
2689   if ( SD_ALLZERO == ( response_r1 &
2690       ( SD_R6_GENERAL_UNKNOWN_ERROR | SD_R6_ILLEGAL_CMD |
2691         SD_R6_COM_CRC_FAILED ) ) )
2692   {
2693     *prca = ( uint16_t )( response_r1 >> 16 );
2694     return ( errorstatus );
2695   }
2696 
2697   if ( response_r1 & SD_R6_GENERAL_UNKNOWN_ERROR )
2698   {
2699     return ( SD_GENERAL_UNKNOWN_ERROR );
2700   }
2701 
2702   if ( response_r1 & SD_R6_ILLEGAL_CMD )
2703   {
2704     return ( SD_ILLEGAL_CMD );
2705   }
2706 
2707   if ( response_r1 & SD_R6_COM_CRC_FAILED )
2708   {
2709     return ( SD_COM_CRC_FAILED );
2710   }
2711 
2712   return ( errorstatus );
2713 }
2714 
2715 /**
2716  * @brief  Enables or disables the SDIO wide bus mode.
2717  * @param  NewState: new state of the SDIO wide bus mode.
2718  *   This parameter can be: ENABLE or DISABLE.
2719  * @retval SD_Error: SD Card Error code.
2720  */
2721 static SD_Error SDEnWideBus ( FunctionalState NewState )
2722 {
2723   SD_Error errorstatus = SD_OK;
2724 
2725   uint32_t scr[ 2 ] =
2726   {
2727     0,
2728     0
2729   } ;
2730 
2731   if ( SDIO_GetResponse( SDIO_RESP1 ) & SD_CARD_LOCKED )
2732   {
2733     errorstatus = SD_LOCK_UNLOCK_FAILED;
2734     return ( errorstatus );
2735   }
2736 
2737   /*!< Get SCR Register */
2738   errorstatus = FindSCR ( RCA, scr );
2739 
2740   if ( errorstatus != SD_OK )
2741   {
2742     return ( errorstatus );
2743   }
2744 
2745   /*!< If wide bus operation to be enabled */
2746   if ( NewState == ENABLE )
2747   {
2748     /*!< If requested card supports wide bus operation */
2749     if ( ( scr[ 1 ] & SD_WIDE_BUS_SUPPORT ) != SD_ALLZERO )
2750     {
2751       /*!< Send CMD55 APP_CMD with argument as card's RCA.*/
2752       SDIO_CmdInitStructure.SDIO_Argument = ( uint32_t ) RCA << 16;
2753       SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
2754       SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
2755       SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
2756       SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
2757       SDIO_SendCommand ( & SDIO_CmdInitStructure );
2758 
2759       errorstatus = CmdResp1Error ( SD_CMD_APP_CMD );
2760 
2761       if ( errorstatus != SD_OK )
2762       {
2763         return ( errorstatus );
2764       }
2765 
2766       /*!< Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
2767       SDIO_CmdInitStructure.SDIO_Argument = 0x2;
2768       SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH;
2769       SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
2770       SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
2771       SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
2772       SDIO_SendCommand ( & SDIO_CmdInitStructure );
2773 
2774       errorstatus = CmdResp1Error ( SD_CMD_APP_SD_SET_BUSWIDTH );
2775 
2776       if ( errorstatus != SD_OK )
2777       {
2778         return ( errorstatus );
2779       }
2780       return ( errorstatus );
2781     }
2782     else
2783     {
2784       errorstatus = SD_REQUEST_NOT_APPLICABLE;
2785       return ( errorstatus );
2786     }
2787   } /*!< If wide bus operation to be disabled */
2788   else
2789   {
2790     /*!< If requested card supports 1 bit mode operation */
2791     if ( ( scr[ 1 ] & SD_SINGLE_BUS_SUPPORT ) != SD_ALLZERO )
2792     {
2793       /*!< Send CMD55 APP_CMD with argument as card's RCA.*/
2794       SDIO_CmdInitStructure.SDIO_Argument = ( uint32_t ) RCA << 16;
2795       SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
2796       SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
2797       SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
2798       SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
2799       SDIO_SendCommand ( & SDIO_CmdInitStructure );
2800 
2801       errorstatus = CmdResp1Error ( SD_CMD_APP_CMD );
2802 
2803       if ( errorstatus != SD_OK )
2804       {
2805         return ( errorstatus );
2806       }
2807 
2808       /*!< Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
2809       SDIO_CmdInitStructure.SDIO_Argument = 0x00;
2810       SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH;
2811       SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
2812       SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
2813       SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
2814       SDIO_SendCommand ( & SDIO_CmdInitStructure );
2815 
2816       errorstatus = CmdResp1Error ( SD_CMD_APP_SD_SET_BUSWIDTH );
2817 
2818       if ( errorstatus != SD_OK )
2819       {
2820         return ( errorstatus );
2821       }
2822 
2823       return ( errorstatus );
2824     }
2825     else
2826     {
2827       errorstatus = SD_REQUEST_NOT_APPLICABLE;
2828       return ( errorstatus );
2829     }
2830   }
2831 }
2832 
2833 /**
2834  * @brief  Checks if the SD card is in programming state.
2835  * @param  pstatus: pointer to the variable that will contain the SD card state.
2836  * @retval SD_Error: SD Card Error code.
2837  */
2838 static SD_Error IsCardProgramming ( uint8_t * pstatus )
2839 {
2840   SD_Error      errorstatus = SD_OK;
2841   __IO uint32_t respR1      = 0, status = 0;
2842 
2843   SDIO_CmdInitStructure.SDIO_Argument = ( uint32_t ) RCA << 16;
2844   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_STATUS;
2845   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
2846   SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
2847   SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
2848   SDIO_SendCommand ( & SDIO_CmdInitStructure );
2849 
2850   status = SDIO->STA;
2851   while ( !( status & ( SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND |
2852         SDIO_FLAG_CTIMEOUT ) ) )
2853   {
2854     status = SDIO->STA;
2855   }
2856 
2857   if ( status & SDIO_FLAG_CTIMEOUT )
2858   {
2859     errorstatus = SD_CMD_RSP_TIMEOUT;
2860     SDIO_ClearFlag ( SDIO_FLAG_CTIMEOUT );
2861     return ( errorstatus );
2862   }
2863   else if ( status & SDIO_FLAG_CCRCFAIL )
2864   {
2865     errorstatus = SD_CMD_CRC_FAIL;
2866     SDIO_ClearFlag ( SDIO_FLAG_CCRCFAIL );
2867     return ( errorstatus );
2868   }
2869 
2870   status = ( uint32_t )SDIO_GetCommandResponse ( );
2871 
2872   /*!< Check response received is of desired command */
2873   if ( status != SD_CMD_SEND_STATUS )
2874   {
2875     errorstatus = SD_ILLEGAL_CMD;
2876     return ( errorstatus );
2877   }
2878 
2879   /*!< Clear all the static flags */
2880   SDIO_ClearFlag ( SDIO_STATIC_FLAGS );
2881 
2882   /*!< We have received response, retrieve it for analysis  */
2883   respR1 = SDIO_GetResponse ( SDIO_RESP1 );
2884 
2885   /*!< Find out card status */
2886   *pstatus = ( uint8_t )( ( respR1 >> 9 ) & 0x0000000F );
2887 
2888   if ( ( respR1 & SD_OCR_ERRORBITS ) == SD_ALLZERO )
2889   {
2890     return ( errorstatus );
2891   }
2892 
2893   if ( respR1 & SD_OCR_ADDR_OUT_OF_RANGE )
2894   {
2895     return ( SD_ADDR_OUT_OF_RANGE );
2896   }
2897 
2898   if ( respR1 & SD_OCR_ADDR_MISALIGNED )
2899   {
2900     return ( SD_ADDR_MISALIGNED );
2901   }
2902 
2903   if ( respR1 & SD_OCR_BLOCK_LEN_ERR )
2904   {
2905     return ( SD_BLOCK_LEN_ERR );
2906   }
2907 
2908   if ( respR1 & SD_OCR_ERASE_SEQ_ERR )
2909   {
2910     return ( SD_ERASE_SEQ_ERR );
2911   }
2912 
2913   if ( respR1 & SD_OCR_BAD_ERASE_PARAM )
2914   {
2915     return ( SD_BAD_ERASE_PARAM );
2916   }
2917 
2918   if ( respR1 & SD_OCR_WRITE_PROT_VIOLATION )
2919   {
2920     return ( SD_WRITE_PROT_VIOLATION );
2921   }
2922 
2923   if ( respR1 & SD_OCR_LOCK_UNLOCK_FAILED )
2924   {
2925     return ( SD_LOCK_UNLOCK_FAILED );
2926   }
2927 
2928   if ( respR1 & SD_OCR_COM_CRC_FAILED )
2929   {
2930     return ( SD_COM_CRC_FAILED );
2931   }
2932 
2933   if ( respR1 & SD_OCR_ILLEGAL_CMD )
2934   {
2935     return ( SD_ILLEGAL_CMD );
2936   }
2937 
2938   if ( respR1 & SD_OCR_CARD_ECC_FAILED )
2939   {
2940     return ( SD_CARD_ECC_FAILED );
2941   }
2942 
2943   if ( respR1 & SD_OCR_CC_ERROR )
2944   {
2945     return ( SD_CC_ERROR );
2946   }
2947 
2948   if ( respR1 & SD_OCR_GENERAL_UNKNOWN_ERROR )
2949   {
2950     return ( SD_GENERAL_UNKNOWN_ERROR );
2951   }
2952 
2953   if ( respR1 & SD_OCR_STREAM_READ_UNDERRUN )
2954   {
2955     return ( SD_STREAM_READ_UNDERRUN );
2956   }
2957 
2958   if ( respR1 & SD_OCR_STREAM_WRITE_OVERRUN )
2959   {
2960     return ( SD_STREAM_WRITE_OVERRUN );
2961   }
2962 
2963   if ( respR1 & SD_OCR_CID_CSD_OVERWRIETE )
2964   {
2965     return ( SD_CID_CSD_OVERWRITE );
2966   }
2967 
2968   if ( respR1 & SD_OCR_WP_ERASE_SKIP )
2969   {
2970     return ( SD_WP_ERASE_SKIP );
2971   }
2972 
2973   if ( respR1 & SD_OCR_CARD_ECC_DISABLED )
2974   {
2975     return ( SD_CARD_ECC_DISABLED );
2976   }
2977 
2978   if ( respR1 & SD_OCR_ERASE_RESET )
2979   {
2980     return ( SD_ERASE_RESET );
2981   }
2982 
2983   if ( respR1 & SD_OCR_AKE_SEQ_ERROR )
2984   {
2985     return ( SD_AKE_SEQ_ERROR );
2986   }
2987 
2988   return ( errorstatus );
2989 }
2990 
2991 /**
2992  * @brief  Find the SD card SCR register value.
2993  * @param  rca: selected card address.
2994  * @param  pscr: pointer to the buffer that will contain the SCR value.
2995  * @retval SD_Error: SD Card Error code.
2996  */
2997 static SD_Error FindSCR ( uint16_t rca, uint32_t * pscr )
2998 {
2999   uint32_t index        = 0;
3000   SD_Error errorstatus  = SD_OK;
3001   uint32_t tempscr[ 2 ] =
3002   {
3003     0,
3004     0
3005   } ;
3006 
3007   /*!< Set Block Size To 8 Bytes */
3008   /*!< Send CMD55 APP_CMD with argument as card's RCA */
3009   SDIO_CmdInitStructure.SDIO_Argument = ( uint32_t )8;
3010   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
3011   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
3012   SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
3013   SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
3014   SDIO_SendCommand ( & SDIO_CmdInitStructure );
3015 
3016   errorstatus = CmdResp1Error ( SD_CMD_SET_BLOCKLEN );
3017 
3018   if ( errorstatus != SD_OK )
3019   {
3020     return ( errorstatus );
3021   }
3022 
3023   /*!< Send CMD55 APP_CMD with argument as card's RCA */
3024   SDIO_CmdInitStructure.SDIO_Argument = ( uint32_t ) RCA << 16;
3025   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
3026   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
3027   SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
3028   SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
3029   SDIO_SendCommand ( & SDIO_CmdInitStructure );
3030 
3031   errorstatus = CmdResp1Error ( SD_CMD_APP_CMD );
3032 
3033   if ( errorstatus != SD_OK )
3034   {
3035     return ( errorstatus );
3036   }
3037   SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
3038   SDIO_DataInitStructure.SDIO_DataLength    = 8;
3039   SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_8b;
3040   SDIO_DataInitStructure.SDIO_TransferDir   = SDIO_TransferDir_ToSDIO;
3041   SDIO_DataInitStructure.SDIO_TransferMode  = SDIO_TransferMode_Block;
3042   SDIO_DataInitStructure.SDIO_DPSM          = SDIO_DPSM_Enable;
3043   SDIO_DataConfig ( & SDIO_DataInitStructure );
3044 
3045   /*!< Send ACMD51 SD_APP_SEND_SCR with argument as 0 */
3046   SDIO_CmdInitStructure.SDIO_Argument = 0x0;
3047   SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_APP_SEND_SCR;
3048   SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
3049   SDIO_CmdInitStructure.SDIO_Wait     = SDIO_Wait_No;
3050   SDIO_CmdInitStructure.SDIO_CPSM     = SDIO_CPSM_Enable;
3051   SDIO_SendCommand ( & SDIO_CmdInitStructure );
3052 
3053   errorstatus = CmdResp1Error ( SD_CMD_SD_APP_SEND_SCR );
3054 
3055   if ( errorstatus != SD_OK )
3056   {
3057     return ( errorstatus );
3058   }
3059 
3060   while ( !( SDIO->STA & ( SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL |
3061         SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR ) ) )
3062   {
3063     if ( SDIO_GetFlagStatus( SDIO_FLAG_RXDAVL ) != RESET )
3064     {
3065       *( tempscr + index ) = SDIO_ReadData ( );
3066       index++;
3067     }
3068   }
3069 
3070   if ( SDIO_GetFlagStatus( SDIO_FLAG_DTIMEOUT ) != RESET )
3071   {
3072     SDIO_ClearFlag ( SDIO_FLAG_DTIMEOUT );
3073     errorstatus = SD_DATA_TIMEOUT;
3074     return ( errorstatus );
3075   }
3076   else if ( SDIO_GetFlagStatus( SDIO_FLAG_DCRCFAIL ) != RESET )
3077   {
3078     SDIO_ClearFlag ( SDIO_FLAG_DCRCFAIL );
3079     errorstatus = SD_DATA_CRC_FAIL;
3080     return ( errorstatus );
3081   }
3082   else if ( SDIO_GetFlagStatus( SDIO_FLAG_RXOVERR ) != RESET )
3083   {
3084     SDIO_ClearFlag ( SDIO_FLAG_RXOVERR );
3085     errorstatus = SD_RX_OVERRUN;
3086     return ( errorstatus );
3087   }
3088   else if ( SDIO_GetFlagStatus( SDIO_FLAG_STBITERR ) != RESET )
3089   {
3090     SDIO_ClearFlag ( SDIO_FLAG_STBITERR );
3091     errorstatus = SD_START_BIT_ERR;
3092     return ( errorstatus );
3093   }
3094 
3095   /*!< Clear all the static flags */
3096   SDIO_ClearFlag ( SDIO_STATIC_FLAGS );
3097 
3098   *( pscr + 1 ) = ( ( tempscr[ 0 ] & SD_0TO7BITS ) << 24 ) |
3099     ( ( tempscr[ 0 ] & SD_8TO15BITS ) << 8 ) |
3100     ( ( tempscr[ 0 ] & SD_16TO23BITS ) >> 8 ) |
3101     ( ( tempscr[ 0 ] & SD_24TO31BITS ) >> 24 );
3102 
3103   *( pscr ) = ( ( tempscr[ 1 ] & SD_0TO7BITS ) << 24 ) |
3104     ( ( tempscr[ 1 ] & SD_8TO15BITS ) << 8 ) |
3105     ( ( tempscr[ 1 ] & SD_16TO23BITS ) >> 8 ) |
3106     ( ( tempscr[ 1 ] & SD_24TO31BITS ) >> 24 );
3107 
3108   return ( errorstatus );
3109 }
3110 
3111 /**
3112  * @brief  Converts the number of bytes in power of two and returns the power.
3113  * @param  NumberOfBytes: number of bytes.
3114  * @retval None
3115  */
3116 static uint8_t convert_from_bytes_to_power_of_two ( uint16_t NumberOfBytes )
3117 {
3118   uint8_t count = 0;
3119 
3120   while ( NumberOfBytes != 1 )
3121   {
3122     NumberOfBytes >>= 1;
3123     count++;
3124   }
3125   return ( count );
3126 }
3127 
3128 /**
3129  * @}
3130  */
3131 
3132 /**
3133  * @}
3134  */
3135 
3136 /**
3137  * @}
3138  */
3139 
3140 /**
3141  * @}
3142  */
3143 
3144 /**
3145  * @}
3146  */
3147 
3148 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
  1 /****************************************Copyright (c)****************************************************
  2  **
  3  **                                 http://www.powermcu.com
  4  **
  5  **--------------File Info---------------------------------------------------------------------------------
  6  ** File name:               sdio_sd.h
  7  ** Descriptions:            sdio sd卡操作头文件
  8  **
  9  **--------------------------------------------------------------------------------------------------------
 10  ** Created by:              AVRman
 11  ** Created date:            2010-10-30
 12  ** Version:                 v1.0
 13  ** Descriptions:            The original version
 14  **
 15  **--------------------------------------------------------------------------------------------------------
 16  ** Modified by:
 17  ** Modified date:
 18  ** Version:
 19  ** Descriptions:
 20  **
 21  *********************************************************************************************************/
 22 
 23 /* Define to prevent recursive inclusion -------------------------------------*/
 24 #ifndef __SDIO_SD_H
 25 #define __SDIO_SD_H
 26 
 27 #ifdef __cplusplus
 28 extern "C"
 29 {
 30   #endif
 31 
 32   /* Includes ------------------------------------------------------------------*/
 33   #include "stm32f10x.h"
 34 
 35   typedef enum
 36   {
 37     /**
 38      * @brief  SDIO specific error defines
 39      */
 40     SD_CMD_CRC_FAIL = ( 1 ),
 41     /*!< Command response received (but CRC check failed) */
 42       SD_DATA_CRC_FAIL = ( 2 ),
 43     /*!< Data bock sent/received (CRC check Failed) */
 44       SD_CMD_RSP_TIMEOUT = ( 3 ), /*!< Command response timeout */
 45       SD_DATA_TIMEOUT = ( 4 ),    /*!< Data time out */
 46       SD_TX_UNDERRUN = ( 5 ),     /*!< Transmit FIFO under-run */
 47       SD_RX_OVERRUN = ( 6 ),      /*!< Receive FIFO over-run */
 48       SD_START_BIT_ERR = ( 7 ),
 49     /*!< Start bit not detected on all data signals in widE bus mode */
 50       SD_CMD_OUT_OF_RANGE = ( 8 ), /*!< CMD's argument was out of range.*/
 51       SD_ADDR_MISALIGNED = ( 9 ),  /*!< Misaligned address */
 52       SD_BLOCK_LEN_ERR = ( 10 ),
 53     /*!< Transferred block length is not allowed for the card or the number of transferred bytes does not match the block length */
 54       SD_ERASE_SEQ_ERR = ( 11 ),
 55     /*!< An error in the sequence of erase command occurs.*/
 56       SD_BAD_ERASE_PARAM = ( 12 ), /*!< An Invalid selection for erase groups */
 57       SD_WRITE_PROT_VIOLATION = ( 13 ),
 58     /*!< Attempt to program a write protect block */
 59       SD_LOCK_UNLOCK_FAILED = ( 14 ),
 60     /*!< Sequence or password error has been detected in unlock command or if there was an attempt to access a locked card */
 61       SD_COM_CRC_FAILED = ( 15 ),
 62     /*!< CRC check of the previous command failed */
 63       SD_ILLEGAL_CMD = ( 16 ), /*!< Command is not legal for the card state */
 64       SD_CARD_ECC_FAILED = ( 17 ),
 65     /*!< Card internal ECC was applied but failed to correct the data */
 66       SD_CC_ERROR = ( 18 ),              /*!< Internal card controller error */
 67       SD_GENERAL_UNKNOWN_ERROR = ( 19 ), /*!< General or Unknown error */
 68       SD_STREAM_READ_UNDERRUN = ( 20 ),
 69     /*!< The card could not sustain data transfer in stream read operation. */
 70       SD_STREAM_WRITE_OVERRUN = ( 21 ),
 71     /*!< The card could not sustain data programming in stream mode */
 72       SD_CID_CSD_OVERWRITE = ( 22 ), /*!< CID/CSD overwrite error */
 73       SD_WP_ERASE_SKIP = ( 23 ), /*!< only partial address space was erased */
 74       SD_CARD_ECC_DISABLED = ( 24 ),
 75     /*!< Command has been executed without using internal ECC */
 76       SD_ERASE_RESET = ( 25 ),
 77     /*!< Erase sequence was cleared before executing because an out of erase sequence command was received */
 78       SD_AKE_SEQ_ERROR = ( 26 ), /*!< Error in sequence of authentication. */
 79       SD_INVALID_VOLTRANGE = ( 27 ), SD_ADDR_OUT_OF_RANGE = ( 28 ),
 80     SD_SWITCH_ERROR = ( 29 ), SD_SDIO_DISABLED = ( 30 ), SD_SDIO_FUNCTION_BUSY =
 81       ( 31 ), SD_SDIO_FUNCTION_FAILED = ( 32 ),
 82     SD_SDIO_UNKNOWN_FUNCTION = ( 33 ),
 83 
 84     /**
 85      * @brief  Standard error defines
 86      */
 87       SD_INTERNAL_ERROR, SD_NOT_CONFIGURED, SD_REQUEST_PENDING,
 88     SD_REQUEST_NOT_APPLICABLE, SD_INVALID_PARAMETER, SD_UNSUPPORTED_FEATURE,
 89     SD_UNSUPPORTED_HW, SD_ERROR, SD_OK
 90   } SD_Error;
 91 
 92   /**
 93    * @brief  SDIO Transfer state
 94    */
 95   typedef enum
 96   {
 97     SD_TRANSFER_OK = 0, SD_TRANSFER_BUSY = 1, SD_TRANSFER_ERROR
 98   } SDTransferState;
 99 
100   /**
101    * @brief  SD Card States
102    */
103   typedef enum
104   {
105     SD_CARD_READY = ( ( uint32_t )0x00000001 ), SD_CARD_IDENTIFICATION =
106       ( ( uint32_t )0x00000002 ), SD_CARD_STANDBY = ( ( uint32_t )0x00000003 ),
107     SD_CARD_TRANSFER = ( ( uint32_t )0x00000004 ), SD_CARD_SENDING =
108       ( ( uint32_t )0x00000005 ), SD_CARD_RECEIVING =
109       ( ( uint32_t )0x00000006 ), SD_CARD_PROGRAMMING =
110       ( ( uint32_t )0x00000007 ), SD_CARD_DISCONNECTED =
111       ( ( uint32_t )0x00000008 ), SD_CARD_ERROR = ( ( uint32_t )0x000000FF )
112   } SDCardState;
113 
114   /**
115    * @brief  Card Specific Data: CSD Register
116    */
117   typedef struct
118   {
119     __IO uint8_t CSDStruct;      /*!< CSD structure */
120     __IO uint8_t SysSpecVersion; /*!< System specification version */
121     __IO uint8_t Reserved1;      /*!< Reserved */
122     __IO uint8_t TAAC;           /*!< Data read access-time 1 */
123     __IO uint8_t NSAC;           /*!< Data read access-time 2 in CLK cycles */
124     __IO uint8_t MaxBusClkFrec;  /*!< Max. bus clock frequency */
125     __IO uint16_t CardComdClasses; /*!< Card command classes */
126     __IO uint8_t RdBlockLen;          /*!< Max. read data block length */
127     __IO uint8_t PartBlockRead;       /*!< Partial blocks for read allowed */
128     __IO uint8_t WrBlockMisalign;     /*!< Write block misalignment */
129     __IO uint8_t RdBlockMisalign;     /*!< Read block misalignment */
130     __IO uint8_t DSRImpl;             /*!< DSR implemented */
131     __IO uint8_t Reserved2;           /*!< Reserved */
132     __IO uint32_t DeviceSize;         /*!< Device Size */
133     __IO uint8_t MaxRdCurrentVDDMin;  /*!< Max. read current @ VDD min */
134     __IO uint8_t MaxRdCurrentVDDMax;  /*!< Max. read current @ VDD max */
135     __IO uint8_t MaxWrCurrentVDDMin;  /*!< Max. write current @ VDD min */
136     __IO uint8_t MaxWrCurrentVDDMax;  /*!< Max. write current @ VDD max */
137     __IO uint8_t DeviceSizeMul;       /*!< Device size multiplier */
138     __IO uint8_t EraseGrSize;         /*!< Erase group size */
139     __IO uint8_t EraseGrMul;          /*!< Erase group size multiplier */
140     __IO uint8_t WrProtectGrSize;     /*!< Write protect group size */
141     __IO uint8_t WrProtectGrEnable;   /*!< Write protect group enable */
142     __IO uint8_t ManDeflECC;          /*!< Manufacturer default ECC */
143     __IO uint8_t WrSpeedFact;         /*!< Write speed factor */
144     __IO uint8_t MaxWrBlockLen;       /*!< Max. write data block length */
145     __IO uint8_t WriteBlockPaPartial; /*!< Partial blocks for write allowed */
146     __IO uint8_t Reserved3;           /*!< Reserded */
147     __IO uint8_t ContentProtectAppli; /*!< Content protection application */
148     __IO uint8_t FileFormatGrouop;    /*!< File format group */
149     __IO uint8_t CopyFlag;            /*!< Copy flag (OTP) */
150     __IO uint8_t PermWrProtect;       /*!< Permanent write protection */
151     __IO uint8_t TempWrProtect;       /*!< Temporary write protection */
152     __IO uint8_t FileFormat;          /*!< File Format */
153     __IO uint8_t ECC;                 /*!< ECC code */
154     __IO uint8_t CSD_CRC;             /*!< CSD CRC */
155     __IO uint8_t Reserved4;           /*!< always 1*/
156   } SD_CSD;
157 
158   /**
159    * @brief  Card Identification Data: CID Register
160    */
161   typedef struct
162   {
163     __IO uint8_t ManufacturerID; /*!< ManufacturerID */
164     __IO uint16_t OEM_AppliID;   /*!< OEM/Application ID */
165     __IO uint32_t ProdName1;     /*!< Product Name part1 */
166     __IO uint8_t ProdName2;      /*!< Product Name part2*/
167     __IO uint8_t ProdRev;        /*!< Product Revision */
168     __IO uint32_t ProdSN;        /*!< Product Serial Number */
169     __IO uint8_t Reserved1;      /*!< Reserved1 */
170     __IO uint16_t ManufactDate;  /*!< Manufacturing Date */
171     __IO uint8_t CID_CRC;        /*!< CID CRC */
172     __IO uint8_t Reserved2;      /*!< always 1 */
173   } SD_CID;
174 
175   /**
176    * @brief SD Card information
177    */
178   typedef struct
179   {
180     SD_CSD SD_csd;
181     SD_CID SD_cid;
182     uint32_t CardCapacity;  /*!< Card Capacity */
183     uint32_t CardBlockSize; /*!< Card Block Size */
184     uint16_t RCA;
185     uint8_t CardType;
186   } SD_CardInfo;
187 
188   /**
189    * @brief  SDIO Data Transfer Frequency (25MHz max)
190    */
191   #define SDIO_TRANSFER_CLK_DIV            ((uint8_t)0x1)
192 
193   #define SD_DETECT_PIN                    GPIO_Pin_14                 /* PB.14 */
194   #define SD_DETECT_GPIO_PORT              GPIOB                       /* GPIOB */
195   #define SD_DETECT_GPIO_CLK               RCC_APB2Periph_GPIOB
196 
197   /**
198    * @brief  SDIO Intialization Frequency (400KHz max)
199    */
200   #define SDIO_INIT_CLK_DIV                ((uint8_t)0xB2)
201   #define SDIO_FIFO_ADDRESS                ((uint32_t)0x40018080)
202 
203   /**
204    * @brief SDIO Commands  Index
205    */
206   #define SD_CMD_GO_IDLE_STATE                       ((uint8_t)0)
207   #define SD_CMD_SEND_OP_COND                        ((uint8_t)1)
208   #define SD_CMD_ALL_SEND_CID                        ((uint8_t)2)
209   #define SD_CMD_SET_REL_ADDR                        ((uint8_t)3) /*!< SDIO_SEND_REL_ADDR for SD Card */
210   #define SD_CMD_SET_DSR                             ((uint8_t)4)
211   #define SD_CMD_SDIO_SEN_OP_COND                    ((uint8_t)5)
212   #define SD_CMD_HS_SWITCH                           ((uint8_t)6)
213   #define SD_CMD_SEL_DESEL_CARD                      ((uint8_t)7)
214   #define SD_CMD_HS_SEND_EXT_CSD                     ((uint8_t)8)
215   #define SD_CMD_SEND_CSD                            ((uint8_t)9)
216   #define SD_CMD_SEND_CID                            ((uint8_t)10)
217   #define SD_CMD_READ_DAT_UNTIL_STOP                 ((uint8_t)11) /*!< SD Card doesn't support it */
218   #define SD_CMD_STOP_TRANSMISSION                   ((uint8_t)12)
219   #define SD_CMD_SEND_STATUS                         ((uint8_t)13)
220   #define SD_CMD_HS_BUSTEST_READ                     ((uint8_t)14)
221   #define SD_CMD_GO_INACTIVE_STATE                   ((uint8_t)15)
222   #define SD_CMD_SET_BLOCKLEN                        ((uint8_t)16)
223   #define SD_CMD_READ_SINGLE_BLOCK                   ((uint8_t)17)
224   #define SD_CMD_READ_MULT_BLOCK                     ((uint8_t)18)
225   #define SD_CMD_HS_BUSTEST_WRITE                    ((uint8_t)19)
226   #define SD_CMD_WRITE_DAT_UNTIL_STOP                ((uint8_t)20) /*!< SD Card doesn't support it */
227   #define SD_CMD_SET_BLOCK_COUNT                     ((uint8_t)23) /*!< SD Card doesn't support it */
228   #define SD_CMD_WRITE_SINGLE_BLOCK                  ((uint8_t)24)
229   #define SD_CMD_WRITE_MULT_BLOCK                    ((uint8_t)25)
230   #define SD_CMD_PROG_CID                            ((uint8_t)26) /*!< reserved for manufacturers */
231   #define SD_CMD_PROG_CSD                            ((uint8_t)27)
232   #define SD_CMD_SET_WRITE_PROT                      ((uint8_t)28)
233   #define SD_CMD_CLR_WRITE_PROT                      ((uint8_t)29)
234   #define SD_CMD_SEND_WRITE_PROT                     ((uint8_t)30)
235   #define SD_CMD_SD_ERASE_GRP_START                  ((uint8_t)32) /*!< To set the address of the first write
236   block to be erased.( For SD card only )* /
237     #define SD_CMD_SD_ERASE_GRP_END                    ((uint8_t)33) /*!< To set the address of the last write block of the
238   continuous range to be erased.( For SD card only )* /
239     #define SD_CMD_ERASE_GRP_START                     ((uint8_t)35) /*!< To set the address of the first write block to be erased.
240   ( For MMC card only spec 3.31 )* /
241 
242     #define SD_CMD_ERASE_GRP_END                       ((uint8_t)36) /*!< To set the address of the last write block of the
243   continuous range to be erased.( For MMC card only spec 3.31 )* /
244 
245     #define SD_CMD_ERASE                               ((uint8_t)38)
246     #define SD_CMD_FAST_IO                             ((uint8_t)39) /*!< SD Card doesn't support it */
247     #define SD_CMD_GO_IRQ_STATE                        ((uint8_t)40) /*!< SD Card doesn't support it */
248     #define SD_CMD_LOCK_UNLOCK                         ((uint8_t)42)
249     #define SD_CMD_APP_CMD                             ((uint8_t)55)
250     #define SD_CMD_GEN_CMD                             ((uint8_t)56)
251     #define SD_CMD_NO_CMD                              ((uint8_t)64)
252 
253     /**
254      * @brief Following commands are SD Card Specific commands.
255      *        SDIO_APP_CMD should be sent before sending these commands.
256      */
257     #define SD_CMD_APP_SD_SET_BUSWIDTH                 ((uint8_t)6)  /*!< For SD Card only */
258     #define SD_CMD_SD_APP_STAUS                        ((uint8_t)13) /*!< For SD Card only */
259     #define SD_CMD_SD_APP_SEND_NUM_WRITE_BLOCKS        ((uint8_t)22) /*!< For SD Card only */
260     #define SD_CMD_SD_APP_OP_COND                      ((uint8_t)41) /*!< For SD Card only */
261     #define SD_CMD_SD_APP_SET_CLR_CARD_DETECT          ((uint8_t)42) /*!< For SD Card only */
262     #define SD_CMD_SD_APP_SEND_SCR                     ((uint8_t)51) /*!< For SD Card only */
263     #define SD_CMD_SDIO_RW_DIRECT                      ((uint8_t)52) /*!< For SD I/O Card only */
264     #define SD_CMD_SDIO_RW_EXTENDED                    ((uint8_t)53) /*!< For SD I/O Card only */
265 
266     /**
267      * @brief Following commands are SD Card Specific security commands.
268      *        SDIO_APP_CMD should be sent before sending these commands.
269      */
270     #define SD_CMD_SD_APP_GET_MKB                      ((uint8_t)43) /*!< For SD Card only */
271     #define SD_CMD_SD_APP_GET_MID                      ((uint8_t)44) /*!< For SD Card only */
272     #define SD_CMD_SD_APP_SET_CER_RN1                  ((uint8_t)45) /*!< For SD Card only */
273     #define SD_CMD_SD_APP_GET_CER_RN2                  ((uint8_t)46) /*!< For SD Card only */
274     #define SD_CMD_SD_APP_SET_CER_RES2                 ((uint8_t)47) /*!< For SD Card only */
275     #define SD_CMD_SD_APP_GET_CER_RES1                 ((uint8_t)48) /*!< For SD Card only */
276     #define SD_CMD_SD_APP_SECURE_READ_MULTIPLE_BLOCK   ((uint8_t)18) /*!< For SD Card only */
277     #define SD_CMD_SD_APP_SECURE_WRITE_MULTIPLE_BLOCK  ((uint8_t)25) /*!< For SD Card only */
278     #define SD_CMD_SD_APP_SECURE_ERASE                 ((uint8_t)38) /*!< For SD Card only */
279     #define SD_CMD_SD_APP_CHANGE_SECURE_AREA           ((uint8_t)49) /*!< For SD Card only */
280     #define SD_CMD_SD_APP_SECURE_WRITE_MKB             ((uint8_t)48) /*!< For SD Card only */
281 
282     #define SD_DMA_MODE                                ((uint32_t)0x00000000)
283     #define SD_INTERRUPT_MODE                          ((uint32_t)0x00000001)
284     #define SD_POLLING_MODE                            ((uint32_t)0x00000002)
285 
286     /**
287      * @brief  SD detection on its memory slot
288      */
289     #define SD_PRESENT                                 ((uint8_t)0x01)
290     #define SD_NOT_PRESENT                             ((uint8_t)0x00)
291 
292     /**
293      * @brief Supported SD Memory Cards
294      */
295     #define SDIO_STD_CAPACITY_SD_CARD_V1_1             ((uint32_t)0x00000000)
296     #define SDIO_STD_CAPACITY_SD_CARD_V2_0             ((uint32_t)0x00000001)
297     #define SDIO_HIGH_CAPACITY_SD_CARD                 ((uint32_t)0x00000002)
298     #define SDIO_MULTIMEDIA_CARD                       ((uint32_t)0x00000003)
299     #define SDIO_SECURE_DIGITAL_IO_CARD                ((uint32_t)0x00000004)
300     #define SDIO_HIGH_SPEED_MULTIMEDIA_CARD            ((uint32_t)0x00000005)
301     #define SDIO_SECURE_DIGITAL_IO_COMBO_CARD          ((uint32_t)0x00000006)
302     #define SDIO_HIGH_CAPACITY_MMC_CARD                ((uint32_t)0x00000007)
303 
304     /* Private function prototypes -----------------------------------------------*/
305   void SD_DeInit ( void );
306   SD_Error SD_Init ( void );
307   SDTransferState SD_GetStatus ( void );
308   SDCardState SD_GetState ( void );
309   uint8_t SD_Detect ( void );
310   SD_Error SD_PowerON ( void );
311   SD_Error SD_PowerOFF ( void );
312   SD_Error SD_InitializeCards ( void );
313   SD_Error SD_GetCardInfo ( SD_CardInfo * cardinfo );
314   SD_Error SD_EnableWideBusOperation ( uint32_t WideMode );
315   SD_Error SD_SetDeviceMode ( uint32_t Mode );
316   SD_Error SD_SelectDeselect ( uint32_t addr );
317   SD_Error SD_ReadBlock ( uint8_t * readbuff, uint32_t ReadAddr,
318     uint16_t BlockSize );
319   SD_Error SD_ReadMultiBlocks ( uint8_t * readbuff, uint32_t ReadAddr,
320     uint16_t BlockSize, uint32_t NumberOfBlocks );
321   SD_Error SD_WriteBlock ( uint8_t * writebuff, uint32_t WriteAddr,
322     uint16_t BlockSize );
323   SD_Error SD_WriteMultiBlocks ( uint8_t * writebuff, uint32_t WriteAddr,
324     uint16_t BlockSize, uint32_t NumberOfBlocks );
325   SDTransferState SD_GetTransferState ( void );
326   SD_Error SD_StopTransfer ( void );
327   SD_Error SD_Erase ( uint32_t startaddr, uint32_t endaddr );
328   SD_Error SD_SendStatus ( uint32_t * pcardstatus );
329   SD_Error SD_SendSDStatus ( uint32_t * psdstatus );
330   SD_Error SD_ProcessIRQSrc ( void );
331 
332   #ifdef __cplusplus
333 }
334 #endif
335 
336 #endif /* __SDIO_SD_H */
337 
338 /*********************************************************************************************************
339  END FILE
340  *********************************************************************************************************/
posted @ 2012-07-31 14:41  IAmAProgrammer  阅读(3695)  评论(0编辑  收藏  举报