comparison stm32_eval_sdio_sd.c @ 49:ace431a0d0f5

Add SDIO code poached from STM. Use FatFS to read from SD card. LFN doesn't work reliably so it's disabled for now.
author Daniel O'Connor <darius@dons.net.au>
date Wed, 03 Apr 2013 23:34:20 +1030
parents
children 9521be9b391e
comparison
equal deleted inserted replaced
48:2f336d212c74 49:ace431a0d0f5
1 /**
2 ******************************************************************************
3 * @file stm32_eval_sdio_sd.c
4 * @author MCD Application Team
5 * @version V4.5.0
6 * @date 07-March-2011
7 * @brief This file provides a set of functions needed to manage the SDIO SD
8 * Card memory mounted on STM32xx-EVAL board (refer to stm32_eval.h
9 * to know about the boards supporting this memory).
10 *
11 *
12 * @verbatim
13 *
14 * ===================================================================
15 * How to use this driver
16 * ===================================================================
17 * It implements a high level communication layer for read and write
18 * from/to this memory. The needed STM32 hardware resources (SDIO and
19 * GPIO) are defined in stm32xx_eval.h file, and the initialization is
20 * performed in SD_LowLevel_Init() function declared in stm32xx_eval.c
21 * file.
22 * You can easily tailor this driver to any other development board,
23 * by just adapting the defines for hardware resources and
24 * SD_LowLevel_Init() function.
25 *
26 * A - SD Card Initialization and configuration
27 * ============================================
28 * - To initialize the SD Card, use the SD_Init() function. It
29 * Initializes the SD Card and put it into StandBy State (Ready
30 * for data transfer). This function provide the following operations:
31 *
32 * 1 - Apply the SD Card initialization process at 400KHz and check
33 * the SD Card type (Standard Capacity or High Capacity). You
34 * can change or adapt this frequency by adjusting the
35 * "SDIO_INIT_CLK_DIV" define inside the stm32xx_eval.h file.
36 * The SD Card frequency (SDIO_CK) is computed as follows:
37 *
38 * +---------------------------------------------+
39 * | SDIO_CK = SDIOCLK / (SDIO_INIT_CLK_DIV + 2) |
40 * +---------------------------------------------+
41 *
42 * In initialization mode and according to the SD Card standard,
43 * make sure that the SDIO_CK frequency don't exceed 400KHz.
44 *
45 * 2 - Get the SD CID and CSD data. All these information are
46 * managed by the SDCardInfo structure. This structure provide
47 * also ready computed SD Card capacity and Block size.
48 *
49 * 3 - Configure the SD Card Data transfer frequency. By Default,
50 * the card transfer frequency is set to 24MHz. You can change
51 * or adapt this frequency by adjusting the "SDIO_TRANSFER_CLK_DIV"
52 * define inside the stm32xx_eval.h file.
53 * The SD Card frequency (SDIO_CK) is computed as follows:
54 *
55 * +---------------------------------------------+
56 * | SDIO_CK = SDIOCLK / (SDIO_INIT_CLK_DIV + 2) |
57 * +---------------------------------------------+
58 *
59 * In transfer mode and according to the SD Card standard,
60 * make sure that the SDIO_CK frequency don't exceed 25MHz
61 * and 50MHz in High-speed mode switch.
62 * To be able to use a frequency higher than 24MHz, you should
63 * use the SDIO peripheral in bypass mode. Refer to the
64 * corresponding reference manual for more details.
65 *
66 * 4 - Select the corresponding SD Card according to the address
67 * read with the step 2.
68 *
69 * 5 - Configure the SD Card in wide bus mode: 4-bits data.
70 *
71 * B - SD Card Read operation
72 * ==========================
73 * - You can read SD card by using two function: SD_ReadBlock() and
74 * SD_ReadMultiBlocks() functions. These functions support only
75 * 512-byte block length.
76 * - The SD_ReadBlock() function read only one block (512-byte). This
77 * function can transfer the data using DMA controller or using
78 * polling mode. To select between DMA or polling mode refer to
79 * "SD_DMA_MODE" or "SD_POLLING_MODE" inside the stm32_eval_sdio_sd.h
80 * file and uncomment the corresponding line. By default the SD DMA
81 * mode is selected
82 * - The SD_ReadMultiBlocks() function read only mutli blocks (multiple
83 * of 512-byte).
84 * - Any read operation should be followed by two functions to check
85 * if the DMA Controller and SD Card status.
86 * - SD_ReadWaitOperation(): this function insure that the DMA
87 * controller has finished all data transfer.
88 * - SD_GetStatus(): to check that the SD Card has finished the
89 * data transfer and it is ready for data.
90 *
91 * - The DMA transfer is finished by the SDIO Data End interrupt. User
92 * has to call the SD_ProcessIRQ() function inside the SDIO_IRQHandler().
93 * Don't forget to enable the SDIO_IRQn interrupt using the NVIC controller.
94 *
95 * C - SD Card Write operation
96 * ===========================
97 * - You can write SD card by using two function: SD_WriteBlock() and
98 * SD_WriteMultiBlocks() functions. These functions support only
99 * 512-byte block length.
100 * - The SD_WriteBlock() function write only one block (512-byte). This
101 * function can transfer the data using DMA controller or using
102 * polling mode. To select between DMA or polling mode refer to
103 * "SD_DMA_MODE" or "SD_POLLING_MODE" inside the stm32_eval_sdio_sd.h
104 * file and uncomment the corresponding line. By default the SD DMA
105 * mode is selected
106 * - The SD_WriteMultiBlocks() function write only mutli blocks (multiple
107 * of 512-byte).
108 * - Any write operation should be followed by two functions to check
109 * if the DMA Controller and SD Card status.
110 * - SD_ReadWaitOperation(): this function insure that the DMA
111 * controller has finished all data transfer.
112 * - SD_GetStatus(): to check that the SD Card has finished the
113 * data transfer and it is ready for data.
114 *
115 * - The DMA transfer is finished by the SDIO Data End interrupt. User
116 * has to call the SD_ProcessIRQ() function inside the SDIO_IRQHandler().
117 * Don't forget to enable the SDIO_IRQn interrupt using the NVIC controller.
118
119 *
120 * D - SD card status
121 * ==================
122 * - At any time, you can check the SD Card status and get the SD card
123 * state by using the SD_GetStatus() function. This function checks
124 * first if the SD card is still connected and then get the internal
125 * SD Card transfer state.
126 * - You can also get the SD card SD Status register by using the
127 * SD_SendSDStatus() function.
128 *
129 * E - Programming Model
130 * =====================
131 * Status = SD_Init(); // Initialization Step as described in section A
132 *
133 * // SDIO Interrupt ENABLE
134 * NVIC_InitStructure.NVIC_IRQChannel = SDIO_IRQn;
135 * NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
136 * NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
137 * NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
138 * NVIC_Init(&NVIC_InitStructure);
139 *
140 * // Write operation as described in Section C
141 * Status = SD_WriteBlock(buffer, address, 512);
142 * Status = SD_WaitWriteOperation();
143 * while(SD_GetStatus() != SD_TRANSFER_OK);
144 *
145 * Status = SD_WriteMultiBlocks(buffer, address, 512, NUMBEROFBLOCKS);
146 * Status = SD_WaitWriteOperation();
147 * while(SD_GetStatus() != SD_TRANSFER_OK);
148 *
149 * // Read operation as described in Section B
150 * Status = SD_ReadBlock(buffer, address, 512);
151 * Status = SD_WaitReadOperation();
152 * while(SD_GetStatus() != SD_TRANSFER_OK);
153 *
154 * Status = SD_ReadMultiBlocks(buffer, address, 512, NUMBEROFBLOCKS);
155 * Status = SD_WaitReadOperation();
156 * while(SD_GetStatus() != SD_TRANSFER_OK);
157 *
158 *
159 * STM32 SDIO Pin assignment
160 * =========================
161 * +-----------------------------------------------------------+
162 * | Pin assignment |
163 * +-----------------------------+---------------+-------------+
164 * | STM32 SDIO Pins | SD | Pin |
165 * +-----------------------------+---------------+-------------+
166 * | SDIO D2 | D2 | 1 |
167 * | SDIO D3 | D3 | 2 |
168 * | SDIO CMD | CMD | 3 |
169 * | | VCC | 4 (3.3 V)|
170 * | SDIO CLK | CLK | 5 |
171 * | | GND | 6 (0 V) |
172 * | SDIO D0 | D0 | 7 |
173 * | SDIO D1 | D1 | 8 |
174 * +-----------------------------+---------------+-------------+
175 *
176 * @endverbatim
177 *
178 ******************************************************************************
179 * @attention
180 *
181 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
182 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
183 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
184 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
185 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
186 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
187 *
188 * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
189 ******************************************************************************
190 */
191
192 /* Includes ------------------------------------------------------------------*/
193 #include <stdint.h>
194
195 #include "stm32f10x.h"
196 #include "stm32_eval_sdio_sd.h"
197
198 /** @addtogroup Utilities
199 * @{
200 */
201
202 /** @addtogroup STM32_EVAL
203 * @{
204 */
205
206 /** @addtogroup Common
207 * @{
208 */
209
210 /** @addtogroup STM32_EVAL_SDIO_SD
211 * @brief This file provides all the SD Card driver firmware functions.
212 * @{
213 */
214
215 /** @defgroup STM32_EVAL_SDIO_SD_Private_Types
216 * @{
217 */
218 /**
219 * @}
220 */
221
222
223 /** @defgroup STM32_EVAL_SDIO_SD_Private_Defines
224 * @{
225 */
226 /**
227 * @brief SDIO Static flags, TimeOut, FIFO Address
228 */
229 #define SDIO_STATIC_FLAGS ((uint32_t)0x000005FF)
230 #define SDIO_CMD0TIMEOUT ((uint32_t)0x00010000)
231
232 /**
233 * @brief Mask for errors Card Status R1 (OCR Register)
234 */
235 #define SD_OCR_ADDR_OUT_OF_RANGE ((uint32_t)0x80000000)
236 #define SD_OCR_ADDR_MISALIGNED ((uint32_t)0x40000000)
237 #define SD_OCR_BLOCK_LEN_ERR ((uint32_t)0x20000000)
238 #define SD_OCR_ERASE_SEQ_ERR ((uint32_t)0x10000000)
239 #define SD_OCR_BAD_ERASE_PARAM ((uint32_t)0x08000000)
240 #define SD_OCR_WRITE_PROT_VIOLATION ((uint32_t)0x04000000)
241 #define SD_OCR_LOCK_UNLOCK_FAILED ((uint32_t)0x01000000)
242 #define SD_OCR_COM_CRC_FAILED ((uint32_t)0x00800000)
243 #define SD_OCR_ILLEGAL_CMD ((uint32_t)0x00400000)
244 #define SD_OCR_CARD_ECC_FAILED ((uint32_t)0x00200000)
245 #define SD_OCR_CC_ERROR ((uint32_t)0x00100000)
246 #define SD_OCR_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00080000)
247 #define SD_OCR_STREAM_READ_UNDERRUN ((uint32_t)0x00040000)
248 #define SD_OCR_STREAM_WRITE_OVERRUN ((uint32_t)0x00020000)
249 #define SD_OCR_CID_CSD_OVERWRIETE ((uint32_t)0x00010000)
250 #define SD_OCR_WP_ERASE_SKIP ((uint32_t)0x00008000)
251 #define SD_OCR_CARD_ECC_DISABLED ((uint32_t)0x00004000)
252 #define SD_OCR_ERASE_RESET ((uint32_t)0x00002000)
253 #define SD_OCR_AKE_SEQ_ERROR ((uint32_t)0x00000008)
254 #define SD_OCR_ERRORBITS ((uint32_t)0xFDFFE008)
255
256 /**
257 * @brief Masks for R6 Response
258 */
259 #define SD_R6_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00002000)
260 #define SD_R6_ILLEGAL_CMD ((uint32_t)0x00004000)
261 #define SD_R6_COM_CRC_FAILED ((uint32_t)0x00008000)
262
263 #define SD_VOLTAGE_WINDOW_SD ((uint32_t)0x80100000)
264 #define SD_HIGH_CAPACITY ((uint32_t)0x40000000)
265 #define SD_STD_CAPACITY ((uint32_t)0x00000000)
266 #define SD_CHECK_PATTERN ((uint32_t)0x000001AA)
267
268 #define SD_MAX_VOLT_TRIAL ((uint32_t)0x0000FFFF)
269 #define SD_ALLZERO ((uint32_t)0x00000000)
270
271 #define SD_WIDE_BUS_SUPPORT ((uint32_t)0x00040000)
272 #define SD_SINGLE_BUS_SUPPORT ((uint32_t)0x00010000)
273 #define SD_CARD_LOCKED ((uint32_t)0x02000000)
274
275 #define SD_DATATIMEOUT ((uint32_t)0xFFFFFFFF)
276 #define SD_0TO7BITS ((uint32_t)0x000000FF)
277 #define SD_8TO15BITS ((uint32_t)0x0000FF00)
278 #define SD_16TO23BITS ((uint32_t)0x00FF0000)
279 #define SD_24TO31BITS ((uint32_t)0xFF000000)
280 #define SD_MAX_DATA_LENGTH ((uint32_t)0x01FFFFFF)
281
282 #define SD_HALFFIFO ((uint32_t)0x00000008)
283 #define SD_HALFFIFOBYTES ((uint32_t)0x00000020)
284
285 /**
286 * @brief Command Class Supported
287 */
288 #define SD_CCCC_LOCK_UNLOCK ((uint32_t)0x00000080)
289 #define SD_CCCC_WRITE_PROT ((uint32_t)0x00000040)
290 #define SD_CCCC_ERASE ((uint32_t)0x00000020)
291
292 /**
293 * @brief Following commands are SD Card Specific commands.
294 * SDIO_APP_CMD should be sent before sending these commands.
295 */
296 #define SDIO_SEND_IF_COND ((uint32_t)0x00000008)
297
298 /**
299 * @}
300 */
301
302
303 /** @defgroup STM32_EVAL_SDIO_SD_Private_Macros
304 * @{
305 */
306 /**
307 * @}
308 */
309
310
311 /** @defgroup STM32_EVAL_SDIO_SD_Private_Variables
312 * @{
313 */
314 static uint32_t CardType = SDIO_STD_CAPACITY_SD_CARD_V1_1;
315 static uint32_t CSD_Tab[4], CID_Tab[4], RCA = 0;
316 static uint8_t SDSTATUS_Tab[16];
317 __IO uint32_t StopCondition = 0;
318 __IO SD_Error TransferError = SD_OK;
319 __IO uint32_t TransferEnd = 0;
320 SD_CardInfo SDCardInfo;
321
322 SDIO_InitTypeDef SDIO_InitStructure;
323 SDIO_CmdInitTypeDef SDIO_CmdInitStructure;
324 SDIO_DataInitTypeDef SDIO_DataInitStructure;
325 /**
326 * @}
327 */
328
329
330 /** @defgroup STM32_EVAL_SDIO_SD_Private_Function_Prototypes
331 * @{
332 */
333 static SD_Error CmdError(void);
334 static SD_Error CmdResp1Error(uint8_t cmd);
335 static SD_Error CmdResp7Error(void);
336 static SD_Error CmdResp3Error(void);
337 static SD_Error CmdResp2Error(void);
338 static SD_Error CmdResp6Error(uint8_t cmd, uint16_t *prca);
339 static SD_Error SDEnWideBus(FunctionalState NewState);
340 static SD_Error IsCardProgramming(uint8_t *pstatus);
341 static SD_Error FindSCR(uint16_t rca, uint32_t *pscr);
342 uint8_t convert_from_bytes_to_power_of_two(uint16_t NumberOfBytes);
343
344 /**
345 * @}
346 */
347
348
349 /** @defgroup STM32_EVAL_SDIO_SD_Private_Functions
350 * @{
351 */
352
353 /**
354 * @brief DeInitializes the SDIO interface.
355 * @param None
356 * @retval None
357 */
358 void SD_DeInit(void)
359 {
360 SD_LowLevel_DeInit();
361 }
362
363 /**
364 * @brief Initializes the SD Card and put it into StandBy State (Ready for data
365 * transfer).
366 * @param None
367 * @retval SD_Error: SD Card Error code.
368 */
369 SD_Error SD_Init(void)
370 {
371 SD_Error errorstatus = SD_OK;
372
373 /* SDIO Peripheral Low Level Init */
374 SD_LowLevel_Init();
375
376 SDIO_DeInit();
377
378 errorstatus = SD_PowerON();
379
380 if (errorstatus != SD_OK)
381 {
382 /*!< CMD Response TimeOut (wait for CMDSENT flag) */
383 return(errorstatus);
384 }
385
386 errorstatus = SD_InitializeCards();
387
388 if (errorstatus != SD_OK)
389 {
390 /*!< CMD Response TimeOut (wait for CMDSENT flag) */
391 return(errorstatus);
392 }
393
394 /*!< Configure the SDIO peripheral */
395 /*!< SDIOCLK = HCLK, SDIO_CK = HCLK/(2 + SDIO_TRANSFER_CLK_DIV) */
396 /*!< on STM32F2xx devices, SDIOCLK is fixed to 48MHz */
397 SDIO_InitStructure.SDIO_ClockDiv = SDIO_TRANSFER_CLK_DIV;
398 SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_Rising;
399 SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_Disable;
400 SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable;
401 SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_1b;
402 SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable;
403 SDIO_Init(&SDIO_InitStructure);
404
405 if (errorstatus == SD_OK)
406 {
407 /*----------------- Read CSD/CID MSD registers ------------------*/
408 errorstatus = SD_GetCardInfo(&SDCardInfo);
409 }
410
411 if (errorstatus == SD_OK)
412 {
413 /*----------------- Select Card --------------------------------*/
414 errorstatus = SD_SelectDeselect((uint32_t) (SDCardInfo.RCA << 16));
415 }
416 #ifndef SD_NO_4BIT
417 if (errorstatus == SD_OK)
418 {
419 errorstatus = SD_EnableWideBusOperation(SDIO_BusWide_4b);
420 }
421 #endif
422 return(errorstatus);
423 }
424
425 /**
426 * @brief Gets the cuurent sd card data transfer status.
427 * @param None
428 * @retval SDTransferState: Data Transfer state.
429 * This value can be:
430 * - SD_TRANSFER_OK: No data transfer is acting
431 * - SD_TRANSFER_BUSY: Data transfer is acting
432 */
433 SDTransferState SD_GetStatus(void)
434 {
435 SDCardState cardstate = SD_CARD_TRANSFER;
436
437 cardstate = SD_GetState();
438
439 if (cardstate == SD_CARD_TRANSFER)
440 {
441 return(SD_TRANSFER_OK);
442 }
443 else if(cardstate == SD_CARD_ERROR)
444 {
445 return (SD_TRANSFER_ERROR);
446 }
447 else
448 {
449 return(SD_TRANSFER_BUSY);
450 }
451 }
452
453 /**
454 * @brief Returns the current card's state.
455 * @param None
456 * @retval SDCardState: SD Card Error or SD Card Current State.
457 */
458 SDCardState SD_GetState(void)
459 {
460 uint32_t resp1 = 0;
461
462 if(SD_Detect()== SD_PRESENT)
463 {
464 if (SD_SendStatus(&resp1) != SD_OK)
465 {
466 return SD_CARD_ERROR;
467 }
468 else
469 {
470 return (SDCardState)((resp1 >> 9) & 0x0F);
471 }
472 }
473 else
474 {
475 return SD_CARD_ERROR;
476 }
477 }
478
479 /**
480 * @brief Detect if SD card is correctly plugged in the memory slot.
481 * @param None
482 * @retval Return if SD is detected or not
483 */
484 uint8_t SD_Detect(void)
485 {
486 __IO uint8_t status = SD_PRESENT;
487 #ifdef SD_HAVE_DETECT
488 /*!< Check GPIO to detect SD */
489 if (GPIO_ReadInputDataBit(SD_DETECT_GPIO_PORT, SD_DETECT_PIN) != Bit_RESET)
490 {
491 status = SD_NOT_PRESENT;
492 }
493 #endif
494 return status;
495 }
496
497 /**
498 * @brief Enquires cards about their operating voltage and configures
499 * clock controls.
500 * @param None
501 * @retval SD_Error: SD Card Error code.
502 */
503 SD_Error SD_PowerON(void)
504 {
505 SD_Error errorstatus = SD_OK;
506 uint32_t response = 0, count = 0, validvoltage = 0;
507 uint32_t SDType = SD_STD_CAPACITY;
508
509 /*!< Power ON Sequence -----------------------------------------------------*/
510 /*!< Configure the SDIO peripheral */
511 /*!< SDIOCLK = HCLK, SDIO_CK = HCLK/(2 + SDIO_INIT_CLK_DIV) */
512 /*!< on STM32F2xx devices, SDIOCLK is fixed to 48MHz */
513 /*!< SDIO_CK for initialization should not exceed 400 KHz */
514 SDIO_InitStructure.SDIO_ClockDiv = SDIO_INIT_CLK_DIV;
515 SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_Rising;
516 SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_Disable;
517 SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable;
518 SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_1b;
519 SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable;
520 SDIO_Init(&SDIO_InitStructure);
521
522 /*!< Set Power State to ON */
523 SDIO_SetPowerState(SDIO_PowerState_ON);
524
525 /*!< Enable SDIO Clock */
526 SDIO_ClockCmd(ENABLE);
527
528 /*!< CMD0: GO_IDLE_STATE ---------------------------------------------------*/
529 /*!< No CMD response required */
530 SDIO_CmdInitStructure.SDIO_Argument = 0x0;
531 SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_GO_IDLE_STATE;
532 SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_No;
533 SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
534 SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
535 SDIO_SendCommand(&SDIO_CmdInitStructure);
536
537 errorstatus = CmdError();
538
539 if (errorstatus != SD_OK)
540 {
541 /*!< CMD Response TimeOut (wait for CMDSENT flag) */
542 return(errorstatus);
543 }
544
545 /*!< CMD8: SEND_IF_COND ----------------------------------------------------*/
546 /*!< Send CMD8 to verify SD card interface operating condition */
547 /*!< Argument: - [31:12]: Reserved (shall be set to '0')
548 - [11:8]: Supply Voltage (VHS) 0x1 (Range: 2.7-3.6 V)
549 - [7:0]: Check Pattern (recommended 0xAA) */
550 /*!< CMD Response: R7 */
551 SDIO_CmdInitStructure.SDIO_Argument = SD_CHECK_PATTERN;
552 SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_SEND_IF_COND;
553 SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
554 SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
555 SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
556 SDIO_SendCommand(&SDIO_CmdInitStructure);
557
558 errorstatus = CmdResp7Error();
559
560 if (errorstatus == SD_OK)
561 {
562 CardType = SDIO_STD_CAPACITY_SD_CARD_V2_0; /*!< SD Card 2.0 */
563 SDType = SD_HIGH_CAPACITY;
564 }
565 else
566 {
567 /*!< CMD55 */
568 SDIO_CmdInitStructure.SDIO_Argument = 0x00;
569 SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
570 SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
571 SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
572 SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
573 SDIO_SendCommand(&SDIO_CmdInitStructure);
574 errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
575 }
576 /*!< CMD55 */
577 SDIO_CmdInitStructure.SDIO_Argument = 0x00;
578 SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
579 SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
580 SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
581 SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
582 SDIO_SendCommand(&SDIO_CmdInitStructure);
583 errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
584
585 /*!< If errorstatus is Command TimeOut, it is a MMC card */
586 /*!< If errorstatus is SD_OK it is a SD card: SD card 2.0 (voltage range mismatch)
587 or SD card 1.x */
588 if (errorstatus == SD_OK)
589 {
590 /*!< SD CARD */
591 /*!< Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
592 while ((!validvoltage) && (count < SD_MAX_VOLT_TRIAL))
593 {
594
595 /*!< SEND CMD55 APP_CMD with RCA as 0 */
596 SDIO_CmdInitStructure.SDIO_Argument = 0x00;
597 SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
598 SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
599 SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
600 SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
601 SDIO_SendCommand(&SDIO_CmdInitStructure);
602
603 errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
604
605 if (errorstatus != SD_OK)
606 {
607 return(errorstatus);
608 }
609 SDIO_CmdInitStructure.SDIO_Argument = SD_VOLTAGE_WINDOW_SD | SDType;
610 SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_APP_OP_COND;
611 SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
612 SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
613 SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
614 SDIO_SendCommand(&SDIO_CmdInitStructure);
615
616 errorstatus = CmdResp3Error();
617 if (errorstatus != SD_OK)
618 {
619 return(errorstatus);
620 }
621
622 response = SDIO_GetResponse(SDIO_RESP1);
623 validvoltage = (((response >> 31) == 1) ? 1 : 0);
624 count++;
625 }
626 if (count >= SD_MAX_VOLT_TRIAL)
627 {
628 errorstatus = SD_INVALID_VOLTRANGE;
629 return(errorstatus);
630 }
631
632 if (response &= SD_HIGH_CAPACITY)
633 {
634 CardType = SDIO_HIGH_CAPACITY_SD_CARD;
635 }
636
637 }/*!< else MMC Card */
638
639 return(errorstatus);
640 }
641
642 /**
643 * @brief Turns the SDIO output signals off.
644 * @param None
645 * @retval SD_Error: SD Card Error code.
646 */
647 SD_Error SD_PowerOFF(void)
648 {
649 SD_Error errorstatus = SD_OK;
650
651 /*!< Set Power State to OFF */
652 SDIO_SetPowerState(SDIO_PowerState_OFF);
653
654 return(errorstatus);
655 }
656
657 /**
658 * @brief Intialises all cards or single card as the case may be Card(s) come
659 * into standby state.
660 * @param None
661 * @retval SD_Error: SD Card Error code.
662 */
663 SD_Error SD_InitializeCards(void)
664 {
665 SD_Error errorstatus = SD_OK;
666 uint16_t rca = 0x01;
667
668 if (SDIO_GetPowerState() == SDIO_PowerState_OFF)
669 {
670 errorstatus = SD_REQUEST_NOT_APPLICABLE;
671 return(errorstatus);
672 }
673
674 if (SDIO_SECURE_DIGITAL_IO_CARD != CardType)
675 {
676 /*!< Send CMD2 ALL_SEND_CID */
677 SDIO_CmdInitStructure.SDIO_Argument = 0x0;
678 SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_ALL_SEND_CID;
679 SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Long;
680 SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
681 SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
682 SDIO_SendCommand(&SDIO_CmdInitStructure);
683
684 errorstatus = CmdResp2Error();
685
686 if (SD_OK != errorstatus)
687 {
688 return(errorstatus);
689 }
690
691 CID_Tab[0] = SDIO_GetResponse(SDIO_RESP1);
692 CID_Tab[1] = SDIO_GetResponse(SDIO_RESP2);
693 CID_Tab[2] = SDIO_GetResponse(SDIO_RESP3);
694 CID_Tab[3] = SDIO_GetResponse(SDIO_RESP4);
695 }
696 if ((SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType) || (SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType) || (SDIO_SECURE_DIGITAL_IO_COMBO_CARD == CardType)
697 || (SDIO_HIGH_CAPACITY_SD_CARD == CardType))
698 {
699 /*!< Send CMD3 SET_REL_ADDR with argument 0 */
700 /*!< SD Card publishes its RCA. */
701 SDIO_CmdInitStructure.SDIO_Argument = 0x00;
702 SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_REL_ADDR;
703 SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
704 SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
705 SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
706 SDIO_SendCommand(&SDIO_CmdInitStructure);
707
708 errorstatus = CmdResp6Error(SD_CMD_SET_REL_ADDR, &rca);
709
710 if (SD_OK != errorstatus)
711 {
712 return(errorstatus);
713 }
714 }
715
716 if (SDIO_SECURE_DIGITAL_IO_CARD != CardType)
717 {
718 RCA = rca;
719
720 /*!< Send CMD9 SEND_CSD with argument as card's RCA */
721 SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)(rca << 16);
722 SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_CSD;
723 SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Long;
724 SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
725 SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
726 SDIO_SendCommand(&SDIO_CmdInitStructure);
727
728 errorstatus = CmdResp2Error();
729
730 if (SD_OK != errorstatus)
731 {
732 return(errorstatus);
733 }
734
735 CSD_Tab[0] = SDIO_GetResponse(SDIO_RESP1);
736 CSD_Tab[1] = SDIO_GetResponse(SDIO_RESP2);
737 CSD_Tab[2] = SDIO_GetResponse(SDIO_RESP3);
738 CSD_Tab[3] = SDIO_GetResponse(SDIO_RESP4);
739 }
740
741 errorstatus = SD_OK; /*!< All cards get intialized */
742
743 return(errorstatus);
744 }
745
746 /**
747 * @brief Returns information about specific card.
748 * @param cardinfo: pointer to a SD_CardInfo structure that contains all SD card
749 * information.
750 * @retval SD_Error: SD Card Error code.
751 */
752 SD_Error SD_GetCardInfo(SD_CardInfo *cardinfo)
753 {
754 SD_Error errorstatus = SD_OK;
755 uint8_t tmp = 0;
756
757 cardinfo->CardType = (uint8_t)CardType;
758 cardinfo->RCA = (uint16_t)RCA;
759
760 /*!< Byte 0 */
761 tmp = (uint8_t)((CSD_Tab[0] & 0xFF000000) >> 24);
762 cardinfo->SD_csd.CSDStruct = (tmp & 0xC0) >> 6;
763 cardinfo->SD_csd.SysSpecVersion = (tmp & 0x3C) >> 2;
764 cardinfo->SD_csd.Reserved1 = tmp & 0x03;
765
766 /*!< Byte 1 */
767 tmp = (uint8_t)((CSD_Tab[0] & 0x00FF0000) >> 16);
768 cardinfo->SD_csd.TAAC = tmp;
769
770 /*!< Byte 2 */
771 tmp = (uint8_t)((CSD_Tab[0] & 0x0000FF00) >> 8);
772 cardinfo->SD_csd.NSAC = tmp;
773
774 /*!< Byte 3 */
775 tmp = (uint8_t)(CSD_Tab[0] & 0x000000FF);
776 cardinfo->SD_csd.MaxBusClkFrec = tmp;
777
778 /*!< Byte 4 */
779 tmp = (uint8_t)((CSD_Tab[1] & 0xFF000000) >> 24);
780 cardinfo->SD_csd.CardComdClasses = tmp << 4;
781
782 /*!< Byte 5 */
783 tmp = (uint8_t)((CSD_Tab[1] & 0x00FF0000) >> 16);
784 cardinfo->SD_csd.CardComdClasses |= (tmp & 0xF0) >> 4;
785 cardinfo->SD_csd.RdBlockLen = tmp & 0x0F;
786
787 /*!< Byte 6 */
788 tmp = (uint8_t)((CSD_Tab[1] & 0x0000FF00) >> 8);
789 cardinfo->SD_csd.PartBlockRead = (tmp & 0x80) >> 7;
790 cardinfo->SD_csd.WrBlockMisalign = (tmp & 0x40) >> 6;
791 cardinfo->SD_csd.RdBlockMisalign = (tmp & 0x20) >> 5;
792 cardinfo->SD_csd.DSRImpl = (tmp & 0x10) >> 4;
793 cardinfo->SD_csd.Reserved2 = 0; /*!< Reserved */
794
795 if ((CardType == SDIO_STD_CAPACITY_SD_CARD_V1_1) || (CardType == SDIO_STD_CAPACITY_SD_CARD_V2_0))
796 {
797 cardinfo->SD_csd.DeviceSize = (tmp & 0x03) << 10;
798
799 /*!< Byte 7 */
800 tmp = (uint8_t)(CSD_Tab[1] & 0x000000FF);
801 cardinfo->SD_csd.DeviceSize |= (tmp) << 2;
802
803 /*!< Byte 8 */
804 tmp = (uint8_t)((CSD_Tab[2] & 0xFF000000) >> 24);
805 cardinfo->SD_csd.DeviceSize |= (tmp & 0xC0) >> 6;
806
807 cardinfo->SD_csd.MaxRdCurrentVDDMin = (tmp & 0x38) >> 3;
808 cardinfo->SD_csd.MaxRdCurrentVDDMax = (tmp & 0x07);
809
810 /*!< Byte 9 */
811 tmp = (uint8_t)((CSD_Tab[2] & 0x00FF0000) >> 16);
812 cardinfo->SD_csd.MaxWrCurrentVDDMin = (tmp & 0xE0) >> 5;
813 cardinfo->SD_csd.MaxWrCurrentVDDMax = (tmp & 0x1C) >> 2;
814 cardinfo->SD_csd.DeviceSizeMul = (tmp & 0x03) << 1;
815 /*!< Byte 10 */
816 tmp = (uint8_t)((CSD_Tab[2] & 0x0000FF00) >> 8);
817 cardinfo->SD_csd.DeviceSizeMul |= (tmp & 0x80) >> 7;
818
819 cardinfo->CardCapacity = (cardinfo->SD_csd.DeviceSize + 1) ;
820 cardinfo->CardCapacity *= (1 << (cardinfo->SD_csd.DeviceSizeMul + 2));
821 cardinfo->CardBlockSize = 1 << (cardinfo->SD_csd.RdBlockLen);
822 cardinfo->CardCapacity *= cardinfo->CardBlockSize;
823 }
824 else if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
825 {
826 /*!< Byte 7 */
827 tmp = (uint8_t)(CSD_Tab[1] & 0x000000FF);
828 cardinfo->SD_csd.DeviceSize = (tmp & 0x3F) << 16;
829
830 /*!< Byte 8 */
831 tmp = (uint8_t)((CSD_Tab[2] & 0xFF000000) >> 24);
832
833 cardinfo->SD_csd.DeviceSize |= (tmp << 8);
834
835 /*!< Byte 9 */
836 tmp = (uint8_t)((CSD_Tab[2] & 0x00FF0000) >> 16);
837
838 cardinfo->SD_csd.DeviceSize |= (tmp);
839
840 /*!< Byte 10 */
841 tmp = (uint8_t)((CSD_Tab[2] & 0x0000FF00) >> 8);
842
843 cardinfo->CardCapacity = (cardinfo->SD_csd.DeviceSize + 1) * 512 * 1024;
844 cardinfo->CardBlockSize = 512;
845 }
846
847
848 cardinfo->SD_csd.EraseGrSize = (tmp & 0x40) >> 6;
849 cardinfo->SD_csd.EraseGrMul = (tmp & 0x3F) << 1;
850
851 /*!< Byte 11 */
852 tmp = (uint8_t)(CSD_Tab[2] & 0x000000FF);
853 cardinfo->SD_csd.EraseGrMul |= (tmp & 0x80) >> 7;
854 cardinfo->SD_csd.WrProtectGrSize = (tmp & 0x7F);
855
856 /*!< Byte 12 */
857 tmp = (uint8_t)((CSD_Tab[3] & 0xFF000000) >> 24);
858 cardinfo->SD_csd.WrProtectGrEnable = (tmp & 0x80) >> 7;
859 cardinfo->SD_csd.ManDeflECC = (tmp & 0x60) >> 5;
860 cardinfo->SD_csd.WrSpeedFact = (tmp & 0x1C) >> 2;
861 cardinfo->SD_csd.MaxWrBlockLen = (tmp & 0x03) << 2;
862
863 /*!< Byte 13 */
864 tmp = (uint8_t)((CSD_Tab[3] & 0x00FF0000) >> 16);
865 cardinfo->SD_csd.MaxWrBlockLen |= (tmp & 0xC0) >> 6;
866 cardinfo->SD_csd.WriteBlockPaPartial = (tmp & 0x20) >> 5;
867 cardinfo->SD_csd.Reserved3 = 0;
868 cardinfo->SD_csd.ContentProtectAppli = (tmp & 0x01);
869
870 /*!< Byte 14 */
871 tmp = (uint8_t)((CSD_Tab[3] & 0x0000FF00) >> 8);
872 cardinfo->SD_csd.FileFormatGrouop = (tmp & 0x80) >> 7;
873 cardinfo->SD_csd.CopyFlag = (tmp & 0x40) >> 6;
874 cardinfo->SD_csd.PermWrProtect = (tmp & 0x20) >> 5;
875 cardinfo->SD_csd.TempWrProtect = (tmp & 0x10) >> 4;
876 cardinfo->SD_csd.FileFormat = (tmp & 0x0C) >> 2;
877 cardinfo->SD_csd.ECC = (tmp & 0x03);
878
879 /*!< Byte 15 */
880 tmp = (uint8_t)(CSD_Tab[3] & 0x000000FF);
881 cardinfo->SD_csd.CSD_CRC = (tmp & 0xFE) >> 1;
882 cardinfo->SD_csd.Reserved4 = 1;
883
884
885 /*!< Byte 0 */
886 tmp = (uint8_t)((CID_Tab[0] & 0xFF000000) >> 24);
887 cardinfo->SD_cid.ManufacturerID = tmp;
888
889 /*!< Byte 1 */
890 tmp = (uint8_t)((CID_Tab[0] & 0x00FF0000) >> 16);
891 cardinfo->SD_cid.OEM_AppliID = tmp << 8;
892
893 /*!< Byte 2 */
894 tmp = (uint8_t)((CID_Tab[0] & 0x000000FF00) >> 8);
895 cardinfo->SD_cid.OEM_AppliID |= tmp;
896
897 /*!< Byte 3 */
898 tmp = (uint8_t)(CID_Tab[0] & 0x000000FF);
899 cardinfo->SD_cid.ProdName1 = tmp << 24;
900
901 /*!< Byte 4 */
902 tmp = (uint8_t)((CID_Tab[1] & 0xFF000000) >> 24);
903 cardinfo->SD_cid.ProdName1 |= tmp << 16;
904
905 /*!< Byte 5 */
906 tmp = (uint8_t)((CID_Tab[1] & 0x00FF0000) >> 16);
907 cardinfo->SD_cid.ProdName1 |= tmp << 8;
908
909 /*!< Byte 6 */
910 tmp = (uint8_t)((CID_Tab[1] & 0x0000FF00) >> 8);
911 cardinfo->SD_cid.ProdName1 |= tmp;
912
913 /*!< Byte 7 */
914 tmp = (uint8_t)(CID_Tab[1] & 0x000000FF);
915 cardinfo->SD_cid.ProdName2 = tmp;
916
917 /*!< Byte 8 */
918 tmp = (uint8_t)((CID_Tab[2] & 0xFF000000) >> 24);
919 cardinfo->SD_cid.ProdRev = tmp;
920
921 /*!< Byte 9 */
922 tmp = (uint8_t)((CID_Tab[2] & 0x00FF0000) >> 16);
923 cardinfo->SD_cid.ProdSN = tmp << 24;
924
925 /*!< Byte 10 */
926 tmp = (uint8_t)((CID_Tab[2] & 0x0000FF00) >> 8);
927 cardinfo->SD_cid.ProdSN |= tmp << 16;
928
929 /*!< Byte 11 */
930 tmp = (uint8_t)(CID_Tab[2] & 0x000000FF);
931 cardinfo->SD_cid.ProdSN |= tmp << 8;
932
933 /*!< Byte 12 */
934 tmp = (uint8_t)((CID_Tab[3] & 0xFF000000) >> 24);
935 cardinfo->SD_cid.ProdSN |= tmp;
936
937 /*!< Byte 13 */
938 tmp = (uint8_t)((CID_Tab[3] & 0x00FF0000) >> 16);
939 cardinfo->SD_cid.Reserved1 |= (tmp & 0xF0) >> 4;
940 cardinfo->SD_cid.ManufactDate = (tmp & 0x0F) << 8;
941
942 /*!< Byte 14 */
943 tmp = (uint8_t)((CID_Tab[3] & 0x0000FF00) >> 8);
944 cardinfo->SD_cid.ManufactDate |= tmp;
945
946 /*!< Byte 15 */
947 tmp = (uint8_t)(CID_Tab[3] & 0x000000FF);
948 cardinfo->SD_cid.CID_CRC = (tmp & 0xFE) >> 1;
949 cardinfo->SD_cid.Reserved2 = 1;
950
951 return(errorstatus);
952 }
953
954 /**
955 * @brief Enables wide bus opeartion for the requeseted card if supported by
956 * card.
957 * @param WideMode: Specifies the SD card wide bus mode.
958 * This parameter can be one of the following values:
959 * @arg SDIO_BusWide_8b: 8-bit data transfer (Only for MMC)
960 * @arg SDIO_BusWide_4b: 4-bit data transfer
961 * @arg SDIO_BusWide_1b: 1-bit data transfer
962 * @retval SD_Error: SD Card Error code.
963 */
964 SD_Error SD_GetCardStatus(SD_CardStatus *cardstatus)
965 {
966 SD_Error errorstatus = SD_OK;
967 uint8_t tmp = 0;
968
969 errorstatus = SD_SendSDStatus((uint32_t *)SDSTATUS_Tab);
970
971 if (errorstatus != SD_OK)
972 {
973 return(errorstatus);
974 }
975
976 /*!< Byte 0 */
977 tmp = (uint8_t)((SDSTATUS_Tab[0] & 0xC0) >> 6);
978 cardstatus->DAT_BUS_WIDTH = tmp;
979
980 /*!< Byte 0 */
981 tmp = (uint8_t)((SDSTATUS_Tab[0] & 0x20) >> 5);
982 cardstatus->SECURED_MODE = tmp;
983
984 /*!< Byte 2 */
985 tmp = (uint8_t)((SDSTATUS_Tab[2] & 0xFF));
986 cardstatus->SD_CARD_TYPE = tmp << 8;
987
988 /*!< Byte 3 */
989 tmp = (uint8_t)((SDSTATUS_Tab[3] & 0xFF));
990 cardstatus->SD_CARD_TYPE |= tmp;
991
992 /*!< Byte 4 */
993 tmp = (uint8_t)(SDSTATUS_Tab[4] & 0xFF);
994 cardstatus->SIZE_OF_PROTECTED_AREA = tmp << 24;
995
996 /*!< Byte 5 */
997 tmp = (uint8_t)(SDSTATUS_Tab[5] & 0xFF);
998 cardstatus->SIZE_OF_PROTECTED_AREA |= tmp << 16;
999
1000 /*!< Byte 6 */
1001 tmp = (uint8_t)(SDSTATUS_Tab[6] & 0xFF);
1002 cardstatus->SIZE_OF_PROTECTED_AREA |= tmp << 8;
1003
1004 /*!< Byte 7 */
1005 tmp = (uint8_t)(SDSTATUS_Tab[7] & 0xFF);
1006 cardstatus->SIZE_OF_PROTECTED_AREA |= tmp;
1007
1008 /*!< Byte 8 */
1009 tmp = (uint8_t)((SDSTATUS_Tab[8] & 0xFF));
1010 cardstatus->SPEED_CLASS = tmp;
1011
1012 /*!< Byte 9 */
1013 tmp = (uint8_t)((SDSTATUS_Tab[9] & 0xFF));
1014 cardstatus->PERFORMANCE_MOVE = tmp;
1015
1016 /*!< Byte 10 */
1017 tmp = (uint8_t)((SDSTATUS_Tab[10] & 0xF0) >> 4);
1018 cardstatus->AU_SIZE = tmp;
1019
1020 /*!< Byte 11 */
1021 tmp = (uint8_t)(SDSTATUS_Tab[11] & 0xFF);
1022 cardstatus->ERASE_SIZE = tmp << 8;
1023
1024 /*!< Byte 12 */
1025 tmp = (uint8_t)(SDSTATUS_Tab[12] & 0xFF);
1026 cardstatus->ERASE_SIZE |= tmp;
1027
1028 /*!< Byte 13 */
1029 tmp = (uint8_t)((SDSTATUS_Tab[13] & 0xFC) >> 2);
1030 cardstatus->ERASE_TIMEOUT = tmp;
1031
1032 /*!< Byte 13 */
1033 tmp = (uint8_t)((SDSTATUS_Tab[13] & 0x3));
1034 cardstatus->ERASE_OFFSET = tmp;
1035
1036 return(errorstatus);
1037 }
1038
1039 /**
1040 * @brief Enables wide bus opeartion for the requeseted card if supported by
1041 * card.
1042 * @param WideMode: Specifies the SD card wide bus mode.
1043 * This parameter can be one of the following values:
1044 * @arg SDIO_BusWide_8b: 8-bit data transfer (Only for MMC)
1045 * @arg SDIO_BusWide_4b: 4-bit data transfer
1046 * @arg SDIO_BusWide_1b: 1-bit data transfer
1047 * @retval SD_Error: SD Card Error code.
1048 */
1049 SD_Error SD_EnableWideBusOperation(uint32_t WideMode)
1050 {
1051 SD_Error errorstatus = SD_OK;
1052
1053 /*!< MMC Card doesn't support this feature */
1054 if (SDIO_MULTIMEDIA_CARD == CardType)
1055 {
1056 errorstatus = SD_UNSUPPORTED_FEATURE;
1057 return(errorstatus);
1058 }
1059 else if ((SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType) || (SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType) || (SDIO_HIGH_CAPACITY_SD_CARD == CardType))
1060 {
1061 if (SDIO_BusWide_8b == WideMode)
1062 {
1063 errorstatus = SD_UNSUPPORTED_FEATURE;
1064 return(errorstatus);
1065 }
1066 else if (SDIO_BusWide_4b == WideMode)
1067 {
1068 errorstatus = SDEnWideBus(ENABLE);
1069
1070 if (SD_OK == errorstatus)
1071 {
1072 /*!< Configure the SDIO peripheral */
1073 SDIO_InitStructure.SDIO_ClockDiv = SDIO_TRANSFER_CLK_DIV;
1074 SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_Rising;
1075 SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_Disable;
1076 SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable;
1077 SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_4b;
1078 SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable;
1079 SDIO_Init(&SDIO_InitStructure);
1080 }
1081 }
1082 else
1083 {
1084 errorstatus = SDEnWideBus(DISABLE);
1085
1086 if (SD_OK == errorstatus)
1087 {
1088 /*!< Configure the SDIO peripheral */
1089 SDIO_InitStructure.SDIO_ClockDiv = SDIO_TRANSFER_CLK_DIV;
1090 SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_Rising;
1091 SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_Disable;
1092 SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable;
1093 SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_1b;
1094 SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable;
1095 SDIO_Init(&SDIO_InitStructure);
1096 }
1097 }
1098 }
1099
1100 return(errorstatus);
1101 }
1102
1103 /**
1104 * @brief Selects od Deselects the corresponding card.
1105 * @param addr: Address of the Card to be selected.
1106 * @retval SD_Error: SD Card Error code.
1107 */
1108 SD_Error SD_SelectDeselect(uint32_t addr)
1109 {
1110 SD_Error errorstatus = SD_OK;
1111
1112 /*!< Send CMD7 SDIO_SEL_DESEL_CARD */
1113 SDIO_CmdInitStructure.SDIO_Argument = addr;
1114 SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEL_DESEL_CARD;
1115 SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1116 SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1117 SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1118 SDIO_SendCommand(&SDIO_CmdInitStructure);
1119
1120 errorstatus = CmdResp1Error(SD_CMD_SEL_DESEL_CARD);
1121
1122 return(errorstatus);
1123 }
1124
1125 /**
1126 * @brief Allows to read one block from a specified address in a card. The Data
1127 * transfer can be managed by DMA mode or Polling mode.
1128 * @note This operation should be followed by two functions to check if the
1129 * DMA Controller and SD Card status.
1130 * - SD_ReadWaitOperation(): this function insure that the DMA
1131 * controller has finished all data transfer.
1132 * - SD_GetStatus(): to check that the SD Card has finished the
1133 * data transfer and it is ready for data.
1134 * @param readbuff: pointer to the buffer that will contain the received data
1135 * @param ReadAddr: Address from where data are to be read.
1136 * @param BlockSize: the SD card Data block size. The Block size should be 512.
1137 * @retval SD_Error: SD Card Error code.
1138 */
1139 SD_Error SD_ReadBlock(uint8_t *readbuff, uint32_t ReadAddr, uint16_t BlockSize)
1140 {
1141 SD_Error errorstatus = SD_OK;
1142 #if defined (SD_POLLING_MODE)
1143 uint32_t count = 0, *tempbuff = (uint32_t *)readbuff;
1144 #endif
1145
1146 TransferError = SD_OK;
1147 TransferEnd = 0;
1148 StopCondition = 0;
1149
1150 SDIO->DCTRL = 0x0;
1151
1152
1153 if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
1154 {
1155 BlockSize = 512;
1156 ReadAddr /= 512;
1157 }
1158
1159 SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
1160 SDIO_DataInitStructure.SDIO_DataLength = BlockSize;
1161 SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t) 9 << 4;
1162 SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO;
1163 SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
1164 SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
1165 SDIO_DataConfig(&SDIO_DataInitStructure);
1166
1167 /*!< Send CMD17 READ_SINGLE_BLOCK */
1168 SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)ReadAddr;
1169 SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_READ_SINGLE_BLOCK;
1170 SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1171 SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1172 SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1173 SDIO_SendCommand(&SDIO_CmdInitStructure);
1174
1175 errorstatus = CmdResp1Error(SD_CMD_READ_SINGLE_BLOCK);
1176
1177 if (errorstatus != SD_OK)
1178 {
1179 return(errorstatus);
1180 }
1181
1182 #if defined (SD_POLLING_MODE)
1183 /*!< In case of single block transfer, no need of stop transfer at all.*/
1184 /*!< Polling mode */
1185 while (!(SDIO->STA &(SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR)))
1186 {
1187 if (SDIO_GetFlagStatus(SDIO_FLAG_RXFIFOHF) != RESET)
1188 {
1189 for (count = 0; count < 8; count++)
1190 {
1191 *(tempbuff + count) = SDIO_ReadData();
1192 }
1193 tempbuff += 8;
1194 }
1195 }
1196
1197 if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET)
1198 {
1199 SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT);
1200 errorstatus = SD_DATA_TIMEOUT;
1201 return(errorstatus);
1202 }
1203 else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET)
1204 {
1205 SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL);
1206 errorstatus = SD_DATA_CRC_FAIL;
1207 return(errorstatus);
1208 }
1209 else if (SDIO_GetFlagStatus(SDIO_FLAG_RXOVERR) != RESET)
1210 {
1211 SDIO_ClearFlag(SDIO_FLAG_RXOVERR);
1212 errorstatus = SD_RX_OVERRUN;
1213 return(errorstatus);
1214 }
1215 else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET)
1216 {
1217 SDIO_ClearFlag(SDIO_FLAG_STBITERR);
1218 errorstatus = SD_START_BIT_ERR;
1219 return(errorstatus);
1220 }
1221 while (SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET)
1222 {
1223 *tempbuff = SDIO_ReadData();
1224 tempbuff++;
1225 }
1226
1227 /*!< Clear all the static flags */
1228 SDIO_ClearFlag(SDIO_STATIC_FLAGS);
1229
1230 #elif defined (SD_DMA_MODE)
1231 SDIO_ITConfig(SDIO_IT_DATAEND, ENABLE);
1232 SDIO_DMACmd(ENABLE);
1233 SD_LowLevel_DMA_RxConfig((uint32_t *)readbuff, BlockSize);
1234 #endif
1235
1236 return(errorstatus);
1237 }
1238
1239 /**
1240 * @brief Allows to read blocks from a specified address in a card. The Data
1241 * transfer can be managed by DMA mode or Polling mode.
1242 * @note This operation should be followed by two functions to check if the
1243 * DMA Controller and SD Card status.
1244 * - SD_ReadWaitOperation(): this function insure that the DMA
1245 * controller has finished all data transfer.
1246 * - SD_GetStatus(): to check that the SD Card has finished the
1247 * data transfer and it is ready for data.
1248 * @param readbuff: pointer to the buffer that will contain the received data.
1249 * @param ReadAddr: Address from where data are to be read.
1250 * @param BlockSize: the SD card Data block size. The Block size should be 512.
1251 * @param NumberOfBlocks: number of blocks to be read.
1252 * @retval SD_Error: SD Card Error code.
1253 */
1254 SD_Error SD_ReadMultiBlocks(uint8_t *readbuff, uint32_t ReadAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)
1255 {
1256 SD_Error errorstatus = SD_OK;
1257 TransferError = SD_OK;
1258 TransferEnd = 0;
1259 StopCondition = 1;
1260
1261 SDIO->DCTRL = 0x0;
1262
1263 if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
1264 {
1265 BlockSize = 512;
1266 ReadAddr /= 512;
1267 }
1268
1269 /*!< Set Block Size for Card */
1270 SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) BlockSize;
1271 SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
1272 SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1273 SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1274 SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1275 SDIO_SendCommand(&SDIO_CmdInitStructure);
1276
1277 errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN);
1278
1279 if (SD_OK != errorstatus)
1280 {
1281 return(errorstatus);
1282 }
1283
1284 SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
1285 SDIO_DataInitStructure.SDIO_DataLength = NumberOfBlocks * BlockSize;
1286 SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t) 9 << 4;
1287 SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO;
1288 SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
1289 SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
1290 SDIO_DataConfig(&SDIO_DataInitStructure);
1291
1292 /*!< Send CMD18 READ_MULT_BLOCK with argument data address */
1293 SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)ReadAddr;
1294 SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_READ_MULT_BLOCK;
1295 SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1296 SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1297 SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1298 SDIO_SendCommand(&SDIO_CmdInitStructure);
1299
1300 errorstatus = CmdResp1Error(SD_CMD_READ_MULT_BLOCK);
1301
1302 if (errorstatus != SD_OK)
1303 {
1304 return(errorstatus);
1305 }
1306
1307 SDIO_ITConfig(SDIO_IT_DATAEND, ENABLE);
1308 SDIO_DMACmd(ENABLE);
1309 SD_LowLevel_DMA_RxConfig((uint32_t *)readbuff, (NumberOfBlocks * BlockSize));
1310
1311 return(errorstatus);
1312 }
1313
1314 /**
1315 * @brief This function waits until the SDIO DMA data transfer is finished.
1316 * This function should be called after SDIO_ReadMultiBlocks() function
1317 * to insure that all data sent by the card are already transferred by
1318 * the DMA controller.
1319 * @param None.
1320 * @retval SD_Error: SD Card Error code.
1321 */
1322 SD_Error SD_WaitReadOperation(void)
1323 {
1324 SD_Error errorstatus = SD_OK;
1325
1326 while ((SD_DMAEndOfTransferStatus() == RESET) && (TransferEnd == 0) && (TransferError == SD_OK))
1327 {}
1328
1329 if (TransferError != SD_OK)
1330 {
1331 return(TransferError);
1332 }
1333
1334 return(errorstatus);
1335 }
1336
1337 /**
1338 * @brief Allows to write one block starting from a specified address in a card.
1339 * The Data transfer can be managed by DMA mode or Polling mode.
1340 * @note This operation should be followed by two functions to check if the
1341 * DMA Controller and SD Card status.
1342 * - SD_ReadWaitOperation(): this function insure that the DMA
1343 * controller has finished all data transfer.
1344 * - SD_GetStatus(): to check that the SD Card has finished the
1345 * data transfer and it is ready for data.
1346 * @param writebuff: pointer to the buffer that contain the data to be transferred.
1347 * @param WriteAddr: Address from where data are to be read.
1348 * @param BlockSize: the SD card Data block size. The Block size should be 512.
1349 * @retval SD_Error: SD Card Error code.
1350 */
1351 SD_Error SD_WriteBlock(uint8_t *writebuff, uint32_t WriteAddr, uint16_t BlockSize)
1352 {
1353 SD_Error errorstatus = SD_OK;
1354
1355 #if defined (SD_POLLING_MODE)
1356 uint32_t bytestransferred = 0, count = 0, restwords = 0;
1357 uint32_t *tempbuff = (uint32_t *)writebuff;
1358 #endif
1359
1360 TransferError = SD_OK;
1361 TransferEnd = 0;
1362 StopCondition = 0;
1363
1364 SDIO->DCTRL = 0x0;
1365
1366
1367 if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
1368 {
1369 BlockSize = 512;
1370 WriteAddr /= 512;
1371 }
1372
1373 /*!< Send CMD24 WRITE_SINGLE_BLOCK */
1374 SDIO_CmdInitStructure.SDIO_Argument = WriteAddr;
1375 SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_WRITE_SINGLE_BLOCK;
1376 SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1377 SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1378 SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1379 SDIO_SendCommand(&SDIO_CmdInitStructure);
1380
1381 errorstatus = CmdResp1Error(SD_CMD_WRITE_SINGLE_BLOCK);
1382
1383 if (errorstatus != SD_OK)
1384 {
1385 return(errorstatus);
1386 }
1387
1388 SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
1389 SDIO_DataInitStructure.SDIO_DataLength = BlockSize;
1390 SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t) 9 << 4;
1391 SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToCard;
1392 SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
1393 SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
1394 SDIO_DataConfig(&SDIO_DataInitStructure);
1395
1396 /*!< In case of single data block transfer no need of stop command at all */
1397 #if defined (SD_POLLING_MODE)
1398 while (!(SDIO->STA & (SDIO_FLAG_DBCKEND | SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_STBITERR)))
1399 {
1400 if (SDIO_GetFlagStatus(SDIO_FLAG_TXFIFOHE) != RESET)
1401 {
1402 if ((512 - bytestransferred) < 32)
1403 {
1404 restwords = ((512 - bytestransferred) % 4 == 0) ? ((512 - bytestransferred) / 4) : (( 512 - bytestransferred) / 4 + 1);
1405 for (count = 0; count < restwords; count++, tempbuff++, bytestransferred += 4)
1406 {
1407 SDIO_WriteData(*tempbuff);
1408 }
1409 }
1410 else
1411 {
1412 for (count = 0; count < 8; count++)
1413 {
1414 SDIO_WriteData(*(tempbuff + count));
1415 }
1416 tempbuff += 8;
1417 bytestransferred += 32;
1418 }
1419 }
1420 }
1421 if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET)
1422 {
1423 SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT);
1424 errorstatus = SD_DATA_TIMEOUT;
1425 return(errorstatus);
1426 }
1427 else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET)
1428 {
1429 SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL);
1430 errorstatus = SD_DATA_CRC_FAIL;
1431 return(errorstatus);
1432 }
1433 else if (SDIO_GetFlagStatus(SDIO_FLAG_TXUNDERR) != RESET)
1434 {
1435 SDIO_ClearFlag(SDIO_FLAG_TXUNDERR);
1436 errorstatus = SD_TX_UNDERRUN;
1437 return(errorstatus);
1438 }
1439 else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET)
1440 {
1441 SDIO_ClearFlag(SDIO_FLAG_STBITERR);
1442 errorstatus = SD_START_BIT_ERR;
1443 return(errorstatus);
1444 }
1445 #elif defined (SD_DMA_MODE)
1446 SDIO_ITConfig(SDIO_IT_DATAEND, ENABLE);
1447 SD_LowLevel_DMA_TxConfig((uint32_t *)writebuff, BlockSize);
1448 SDIO_DMACmd(ENABLE);
1449 #endif
1450
1451 return(errorstatus);
1452 }
1453
1454 /**
1455 * @brief Allows to write blocks starting from a specified address in a card.
1456 * The Data transfer can be managed by DMA mode only.
1457 * @note This operation should be followed by two functions to check if the
1458 * DMA Controller and SD Card status.
1459 * - SD_ReadWaitOperation(): this function insure that the DMA
1460 * controller has finished all data transfer.
1461 * - SD_GetStatus(): to check that the SD Card has finished the
1462 * data transfer and it is ready for data.
1463 * @param WriteAddr: Address from where data are to be read.
1464 * @param writebuff: pointer to the buffer that contain the data to be transferred.
1465 * @param BlockSize: the SD card Data block size. The Block size should be 512.
1466 * @param NumberOfBlocks: number of blocks to be written.
1467 * @retval SD_Error: SD Card Error code.
1468 */
1469 SD_Error SD_WriteMultiBlocks(uint8_t *writebuff, uint32_t WriteAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)
1470 {
1471 SD_Error errorstatus = SD_OK;
1472 __IO uint32_t count = 0;
1473
1474 TransferError = SD_OK;
1475 TransferEnd = 0;
1476 StopCondition = 1;
1477
1478 SDIO->DCTRL = 0x0;
1479
1480 if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
1481 {
1482 BlockSize = 512;
1483 WriteAddr /= 512;
1484 }
1485
1486 /*!< To improve performance */
1487 SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) (RCA << 16);
1488 SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
1489 SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1490 SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1491 SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1492 SDIO_SendCommand(&SDIO_CmdInitStructure);
1493
1494
1495 errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
1496
1497 if (errorstatus != SD_OK)
1498 {
1499 return(errorstatus);
1500 }
1501 /*!< To improve performance */
1502 SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)NumberOfBlocks;
1503 SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCK_COUNT;
1504 SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1505 SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1506 SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1507 SDIO_SendCommand(&SDIO_CmdInitStructure);
1508
1509 errorstatus = CmdResp1Error(SD_CMD_SET_BLOCK_COUNT);
1510
1511 if (errorstatus != SD_OK)
1512 {
1513 return(errorstatus);
1514 }
1515
1516
1517 /*!< Send CMD25 WRITE_MULT_BLOCK with argument data address */
1518 SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)WriteAddr;
1519 SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_WRITE_MULT_BLOCK;
1520 SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1521 SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1522 SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1523 SDIO_SendCommand(&SDIO_CmdInitStructure);
1524
1525 errorstatus = CmdResp1Error(SD_CMD_WRITE_MULT_BLOCK);
1526
1527 if (SD_OK != errorstatus)
1528 {
1529 return(errorstatus);
1530 }
1531
1532 SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
1533 SDIO_DataInitStructure.SDIO_DataLength = NumberOfBlocks * BlockSize;
1534 SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t) 9 << 4;
1535 SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToCard;
1536 SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
1537 SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
1538 SDIO_DataConfig(&SDIO_DataInitStructure);
1539
1540 SDIO_ITConfig(SDIO_IT_DATAEND, ENABLE);
1541 SDIO_DMACmd(ENABLE);
1542 SD_LowLevel_DMA_TxConfig((uint32_t *)writebuff, (NumberOfBlocks * BlockSize));
1543
1544 return(errorstatus);
1545 }
1546
1547 /**
1548 * @brief This function waits until the SDIO DMA data transfer is finished.
1549 * This function should be called after SDIO_WriteBlock() and
1550 * SDIO_WriteMultiBlocks() function to insure that all data sent by the
1551 * card are already transferred by the DMA controller.
1552 * @param None.
1553 * @retval SD_Error: SD Card Error code.
1554 */
1555 SD_Error SD_WaitWriteOperation(void)
1556 {
1557 SD_Error errorstatus = SD_OK;
1558
1559 while ((SD_DMAEndOfTransferStatus() == RESET) && (TransferEnd == 0) && (TransferError == SD_OK))
1560 {}
1561
1562 if (TransferError != SD_OK)
1563 {
1564 return(TransferError);
1565 }
1566
1567 /*!< Clear all the static flags */
1568 SDIO_ClearFlag(SDIO_STATIC_FLAGS);
1569
1570 return(errorstatus);
1571 }
1572
1573 /**
1574 * @brief Gets the cuurent data transfer state.
1575 * @param None
1576 * @retval SDTransferState: Data Transfer state.
1577 * This value can be:
1578 * - SD_TRANSFER_OK: No data transfer is acting
1579 * - SD_TRANSFER_BUSY: Data transfer is acting
1580 */
1581 SDTransferState SD_GetTransferState(void)
1582 {
1583 if (SDIO->STA & (SDIO_FLAG_TXACT | SDIO_FLAG_RXACT))
1584 {
1585 return(SD_TRANSFER_BUSY);
1586 }
1587 else
1588 {
1589 return(SD_TRANSFER_OK);
1590 }
1591 }
1592
1593 /**
1594 * @brief Aborts an ongoing data transfer.
1595 * @param None
1596 * @retval SD_Error: SD Card Error code.
1597 */
1598 SD_Error SD_StopTransfer(void)
1599 {
1600 SD_Error errorstatus = SD_OK;
1601
1602 /*!< Send CMD12 STOP_TRANSMISSION */
1603 SDIO->ARG = 0x0;
1604 SDIO->CMD = 0x44C;
1605 errorstatus = CmdResp1Error(SD_CMD_STOP_TRANSMISSION);
1606
1607 return(errorstatus);
1608 }
1609
1610 /**
1611 * @brief Allows to erase memory area specified for the given card.
1612 * @param startaddr: the start address.
1613 * @param endaddr: the end address.
1614 * @retval SD_Error: SD Card Error code.
1615 */
1616 SD_Error SD_Erase(uint32_t startaddr, uint32_t endaddr)
1617 {
1618 SD_Error errorstatus = SD_OK;
1619 uint32_t delay = 0;
1620 __IO uint32_t maxdelay = 0;
1621 uint8_t cardstate = 0;
1622
1623 /*!< Check if the card coomnd class supports erase command */
1624 if (((CSD_Tab[1] >> 20) & SD_CCCC_ERASE) == 0)
1625 {
1626 errorstatus = SD_REQUEST_NOT_APPLICABLE;
1627 return(errorstatus);
1628 }
1629
1630 maxdelay = 120000 / ((SDIO->CLKCR & 0xFF) + 2);
1631
1632 if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED)
1633 {
1634 errorstatus = SD_LOCK_UNLOCK_FAILED;
1635 return(errorstatus);
1636 }
1637
1638 if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
1639 {
1640 startaddr /= 512;
1641 endaddr /= 512;
1642 }
1643
1644 /*!< According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
1645 if ((SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType) || (SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType) || (SDIO_HIGH_CAPACITY_SD_CARD == CardType))
1646 {
1647 /*!< Send CMD32 SD_ERASE_GRP_START with argument as addr */
1648 SDIO_CmdInitStructure.SDIO_Argument = startaddr;
1649 SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_ERASE_GRP_START;
1650 SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1651 SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1652 SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1653 SDIO_SendCommand(&SDIO_CmdInitStructure);
1654
1655 errorstatus = CmdResp1Error(SD_CMD_SD_ERASE_GRP_START);
1656 if (errorstatus != SD_OK)
1657 {
1658 return(errorstatus);
1659 }
1660
1661 /*!< Send CMD33 SD_ERASE_GRP_END with argument as addr */
1662 SDIO_CmdInitStructure.SDIO_Argument = endaddr;
1663 SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_ERASE_GRP_END;
1664 SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1665 SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1666 SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1667 SDIO_SendCommand(&SDIO_CmdInitStructure);
1668
1669 errorstatus = CmdResp1Error(SD_CMD_SD_ERASE_GRP_END);
1670 if (errorstatus != SD_OK)
1671 {
1672 return(errorstatus);
1673 }
1674 }
1675
1676 /*!< Send CMD38 ERASE */
1677 SDIO_CmdInitStructure.SDIO_Argument = 0;
1678 SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_ERASE;
1679 SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1680 SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1681 SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1682 SDIO_SendCommand(&SDIO_CmdInitStructure);
1683
1684 errorstatus = CmdResp1Error(SD_CMD_ERASE);
1685
1686 if (errorstatus != SD_OK)
1687 {
1688 return(errorstatus);
1689 }
1690
1691 for (delay = 0; delay < maxdelay; delay++)
1692 {}
1693
1694 /*!< Wait till the card is in programming state */
1695 errorstatus = IsCardProgramming(&cardstate);
1696
1697 while ((errorstatus == SD_OK) && ((SD_CARD_PROGRAMMING == cardstate) || (SD_CARD_RECEIVING == cardstate)))
1698 {
1699 errorstatus = IsCardProgramming(&cardstate);
1700 }
1701
1702 return(errorstatus);
1703 }
1704
1705 /**
1706 * @brief Returns the current card's status.
1707 * @param pcardstatus: pointer to the buffer that will contain the SD card
1708 * status (Card Status register).
1709 * @retval SD_Error: SD Card Error code.
1710 */
1711 SD_Error SD_SendStatus(uint32_t *pcardstatus)
1712 {
1713 SD_Error errorstatus = SD_OK;
1714
1715 SDIO->ARG = (uint32_t) RCA << 16;
1716 SDIO->CMD = 0x44D;
1717
1718 errorstatus = CmdResp1Error(SD_CMD_SEND_STATUS);
1719
1720 if (errorstatus != SD_OK)
1721 {
1722 return(errorstatus);
1723 }
1724
1725 *pcardstatus = SDIO->RESP1;
1726 return(errorstatus);
1727 }
1728
1729 /**
1730 * @brief Returns the current SD card's status.
1731 * @param psdstatus: pointer to the buffer that will contain the SD card status
1732 * (SD Status register).
1733 * @retval SD_Error: SD Card Error code.
1734 */
1735 SD_Error SD_SendSDStatus(uint32_t *psdstatus)
1736 {
1737 SD_Error errorstatus = SD_OK;
1738 uint32_t count = 0;
1739
1740 if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED)
1741 {
1742 errorstatus = SD_LOCK_UNLOCK_FAILED;
1743 return(errorstatus);
1744 }
1745
1746 /*!< Set block size for card if it is not equal to current block size for card. */
1747 SDIO_CmdInitStructure.SDIO_Argument = 64;
1748 SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
1749 SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1750 SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1751 SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1752 SDIO_SendCommand(&SDIO_CmdInitStructure);
1753
1754 errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN);
1755
1756 if (errorstatus != SD_OK)
1757 {
1758 return(errorstatus);
1759 }
1760
1761 /*!< CMD55 */
1762 SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16;
1763 SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
1764 SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1765 SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1766 SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1767 SDIO_SendCommand(&SDIO_CmdInitStructure);
1768 errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
1769
1770 if (errorstatus != SD_OK)
1771 {
1772 return(errorstatus);
1773 }
1774
1775 SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
1776 SDIO_DataInitStructure.SDIO_DataLength = 64;
1777 SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_64b;
1778 SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO;
1779 SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
1780 SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
1781 SDIO_DataConfig(&SDIO_DataInitStructure);
1782
1783 /*!< Send ACMD13 SD_APP_STAUS with argument as card's RCA.*/
1784 SDIO_CmdInitStructure.SDIO_Argument = 0;
1785 SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_APP_STAUS;
1786 SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1787 SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1788 SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1789 SDIO_SendCommand(&SDIO_CmdInitStructure);
1790 errorstatus = CmdResp1Error(SD_CMD_SD_APP_STAUS);
1791
1792 if (errorstatus != SD_OK)
1793 {
1794 return(errorstatus);
1795 }
1796
1797 while (!(SDIO->STA &(SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR)))
1798 {
1799 if (SDIO_GetFlagStatus(SDIO_FLAG_RXFIFOHF) != RESET)
1800 {
1801 for (count = 0; count < 8; count++)
1802 {
1803 *(psdstatus + count) = SDIO_ReadData();
1804 }
1805 psdstatus += 8;
1806 }
1807 }
1808
1809 if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET)
1810 {
1811 SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT);
1812 errorstatus = SD_DATA_TIMEOUT;
1813 return(errorstatus);
1814 }
1815 else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET)
1816 {
1817 SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL);
1818 errorstatus = SD_DATA_CRC_FAIL;
1819 return(errorstatus);
1820 }
1821 else if (SDIO_GetFlagStatus(SDIO_FLAG_RXOVERR) != RESET)
1822 {
1823 SDIO_ClearFlag(SDIO_FLAG_RXOVERR);
1824 errorstatus = SD_RX_OVERRUN;
1825 return(errorstatus);
1826 }
1827 else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET)
1828 {
1829 SDIO_ClearFlag(SDIO_FLAG_STBITERR);
1830 errorstatus = SD_START_BIT_ERR;
1831 return(errorstatus);
1832 }
1833
1834 while (SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET)
1835 {
1836 *psdstatus = SDIO_ReadData();
1837 psdstatus++;
1838 }
1839
1840 /*!< Clear all the static status flags*/
1841 SDIO_ClearFlag(SDIO_STATIC_FLAGS);
1842
1843 return(errorstatus);
1844 }
1845
1846 /**
1847 * @brief Allows to process all the interrupts that are high.
1848 * @param None
1849 * @retval SD_Error: SD Card Error code.
1850 */
1851 SD_Error SD_ProcessIRQSrc(void)
1852 {
1853 if (StopCondition == 1)
1854 {
1855 SDIO->ARG = 0x0;
1856 SDIO->CMD = 0x44C;
1857 TransferError = CmdResp1Error(SD_CMD_STOP_TRANSMISSION);
1858 }
1859 else
1860 {
1861 TransferError = SD_OK;
1862 }
1863 SDIO_ClearITPendingBit(SDIO_IT_DATAEND);
1864 SDIO_ITConfig(SDIO_IT_DATAEND, DISABLE);
1865 TransferEnd = 1;
1866 return(TransferError);
1867 }
1868
1869 /**
1870 * @brief Checks for error conditions for CMD0.
1871 * @param None
1872 * @retval SD_Error: SD Card Error code.
1873 */
1874 static SD_Error CmdError(void)
1875 {
1876 SD_Error errorstatus = SD_OK;
1877 uint32_t timeout;
1878
1879 timeout = SDIO_CMD0TIMEOUT; /*!< 10000 */
1880
1881 while ((timeout > 0) && (SDIO_GetFlagStatus(SDIO_FLAG_CMDSENT) == RESET))
1882 {
1883 timeout--;
1884 }
1885
1886 if (timeout == 0)
1887 {
1888 errorstatus = SD_CMD_RSP_TIMEOUT;
1889 return(errorstatus);
1890 }
1891
1892 /*!< Clear all the static flags */
1893 SDIO_ClearFlag(SDIO_STATIC_FLAGS);
1894
1895 return(errorstatus);
1896 }
1897
1898 /**
1899 * @brief Checks for error conditions for R7 response.
1900 * @param None
1901 * @retval SD_Error: SD Card Error code.
1902 */
1903 static SD_Error CmdResp7Error(void)
1904 {
1905 SD_Error errorstatus = SD_OK;
1906 uint32_t status;
1907 uint32_t timeout = SDIO_CMD0TIMEOUT;
1908
1909 status = SDIO->STA;
1910
1911 while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT)) && (timeout > 0))
1912 {
1913 timeout--;
1914 status = SDIO->STA;
1915 }
1916
1917 if ((timeout == 0) || (status & SDIO_FLAG_CTIMEOUT))
1918 {
1919 /*!< Card is not V2.0 complient or card does not support the set voltage range */
1920 errorstatus = SD_CMD_RSP_TIMEOUT;
1921 SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
1922 return(errorstatus);
1923 }
1924
1925 if (status & SDIO_FLAG_CMDREND)
1926 {
1927 /*!< Card is SD V2.0 compliant */
1928 errorstatus = SD_OK;
1929 SDIO_ClearFlag(SDIO_FLAG_CMDREND);
1930 return(errorstatus);
1931 }
1932 return(errorstatus);
1933 }
1934
1935 /**
1936 * @brief Checks for error conditions for R1 response.
1937 * @param cmd: The sent command index.
1938 * @retval SD_Error: SD Card Error code.
1939 */
1940 static SD_Error CmdResp1Error(uint8_t cmd)
1941 {
1942 while (!(SDIO->STA & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT)))
1943 {
1944 }
1945
1946 SDIO->ICR = SDIO_STATIC_FLAGS;
1947
1948 return (SD_Error)(SDIO->RESP1 & SD_OCR_ERRORBITS);
1949 }
1950
1951 /**
1952 * @brief Checks for error conditions for R3 (OCR) response.
1953 * @param None
1954 * @retval SD_Error: SD Card Error code.
1955 */
1956 static SD_Error CmdResp3Error(void)
1957 {
1958 SD_Error errorstatus = SD_OK;
1959 uint32_t status;
1960
1961 status = SDIO->STA;
1962
1963 while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT)))
1964 {
1965 status = SDIO->STA;
1966 }
1967
1968 if (status & SDIO_FLAG_CTIMEOUT)
1969 {
1970 errorstatus = SD_CMD_RSP_TIMEOUT;
1971 SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
1972 return(errorstatus);
1973 }
1974 /*!< Clear all the static flags */
1975 SDIO_ClearFlag(SDIO_STATIC_FLAGS);
1976 return(errorstatus);
1977 }
1978
1979 /**
1980 * @brief Checks for error conditions for R2 (CID or CSD) response.
1981 * @param None
1982 * @retval SD_Error: SD Card Error code.
1983 */
1984 static SD_Error CmdResp2Error(void)
1985 {
1986 SD_Error errorstatus = SD_OK;
1987 uint32_t status;
1988
1989 status = SDIO->STA;
1990
1991 while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CTIMEOUT | SDIO_FLAG_CMDREND)))
1992 {
1993 status = SDIO->STA;
1994 }
1995
1996 if (status & SDIO_FLAG_CTIMEOUT)
1997 {
1998 errorstatus = SD_CMD_RSP_TIMEOUT;
1999 SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
2000 return(errorstatus);
2001 }
2002 else if (status & SDIO_FLAG_CCRCFAIL)
2003 {
2004 errorstatus = SD_CMD_CRC_FAIL;
2005 SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL);
2006 return(errorstatus);
2007 }
2008
2009 /*!< Clear all the static flags */
2010 SDIO_ClearFlag(SDIO_STATIC_FLAGS);
2011
2012 return(errorstatus);
2013 }
2014
2015 /**
2016 * @brief Checks for error conditions for R6 (RCA) response.
2017 * @param cmd: The sent command index.
2018 * @param prca: pointer to the variable that will contain the SD card relative
2019 * address RCA.
2020 * @retval SD_Error: SD Card Error code.
2021 */
2022 static SD_Error CmdResp6Error(uint8_t cmd, uint16_t *prca)
2023 {
2024 SD_Error errorstatus = SD_OK;
2025 uint32_t status;
2026 uint32_t response_r1;
2027
2028 status = SDIO->STA;
2029
2030 while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CTIMEOUT | SDIO_FLAG_CMDREND)))
2031 {
2032 status = SDIO->STA;
2033 }
2034
2035 if (status & SDIO_FLAG_CTIMEOUT)
2036 {
2037 errorstatus = SD_CMD_RSP_TIMEOUT;
2038 SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
2039 return(errorstatus);
2040 }
2041 else if (status & SDIO_FLAG_CCRCFAIL)
2042 {
2043 errorstatus = SD_CMD_CRC_FAIL;
2044 SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL);
2045 return(errorstatus);
2046 }
2047
2048 /*!< Check response received is of desired command */
2049 if (SDIO_GetCommandResponse() != cmd)
2050 {
2051 errorstatus = SD_ILLEGAL_CMD;
2052 return(errorstatus);
2053 }
2054
2055 /*!< Clear all the static flags */
2056 SDIO_ClearFlag(SDIO_STATIC_FLAGS);
2057
2058 /*!< We have received response, retrieve it. */
2059 response_r1 = SDIO_GetResponse(SDIO_RESP1);
2060
2061 if (SD_ALLZERO == (response_r1 & (SD_R6_GENERAL_UNKNOWN_ERROR | SD_R6_ILLEGAL_CMD | SD_R6_COM_CRC_FAILED)))
2062 {
2063 *prca = (uint16_t) (response_r1 >> 16);
2064 return(errorstatus);
2065 }
2066
2067 if (response_r1 & SD_R6_GENERAL_UNKNOWN_ERROR)
2068 {
2069 return(SD_GENERAL_UNKNOWN_ERROR);
2070 }
2071
2072 if (response_r1 & SD_R6_ILLEGAL_CMD)
2073 {
2074 return(SD_ILLEGAL_CMD);
2075 }
2076
2077 if (response_r1 & SD_R6_COM_CRC_FAILED)
2078 {
2079 return(SD_COM_CRC_FAILED);
2080 }
2081
2082 return(errorstatus);
2083 }
2084
2085 /**
2086 * @brief Enables or disables the SDIO wide bus mode.
2087 * @param NewState: new state of the SDIO wide bus mode.
2088 * This parameter can be: ENABLE or DISABLE.
2089 * @retval SD_Error: SD Card Error code.
2090 */
2091 static SD_Error SDEnWideBus(FunctionalState NewState)
2092 {
2093 SD_Error errorstatus = SD_OK;
2094
2095 uint32_t scr[2] = {0, 0};
2096
2097 if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED)
2098 {
2099 errorstatus = SD_LOCK_UNLOCK_FAILED;
2100 return(errorstatus);
2101 }
2102
2103 /*!< Get SCR Register */
2104 errorstatus = FindSCR(RCA, scr);
2105
2106 if (errorstatus != SD_OK)
2107 {
2108 return(errorstatus);
2109 }
2110
2111 /*!< If wide bus operation to be enabled */
2112 if (NewState == ENABLE)
2113 {
2114 /*!< If requested card supports wide bus operation */
2115 if ((scr[1] & SD_WIDE_BUS_SUPPORT) != SD_ALLZERO)
2116 {
2117 /*!< Send CMD55 APP_CMD with argument as card's RCA.*/
2118 SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16;
2119 SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
2120 SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
2121 SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
2122 SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
2123 SDIO_SendCommand(&SDIO_CmdInitStructure);
2124
2125 errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
2126
2127 if (errorstatus != SD_OK)
2128 {
2129 return(errorstatus);
2130 }
2131
2132 /*!< Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
2133 SDIO_CmdInitStructure.SDIO_Argument = 0x2;
2134 SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH;
2135 SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
2136 SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
2137 SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
2138 SDIO_SendCommand(&SDIO_CmdInitStructure);
2139
2140 errorstatus = CmdResp1Error(SD_CMD_APP_SD_SET_BUSWIDTH);
2141
2142 if (errorstatus != SD_OK)
2143 {
2144 return(errorstatus);
2145 }
2146 return(errorstatus);
2147 }
2148 else
2149 {
2150 errorstatus = SD_REQUEST_NOT_APPLICABLE;
2151 return(errorstatus);
2152 }
2153 } /*!< If wide bus operation to be disabled */
2154 else
2155 {
2156 /*!< If requested card supports 1 bit mode operation */
2157 if ((scr[1] & SD_SINGLE_BUS_SUPPORT) != SD_ALLZERO)
2158 {
2159 /*!< Send CMD55 APP_CMD with argument as card's RCA.*/
2160 SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16;
2161 SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
2162 SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
2163 SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
2164 SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
2165 SDIO_SendCommand(&SDIO_CmdInitStructure);
2166
2167
2168 errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
2169
2170 if (errorstatus != SD_OK)
2171 {
2172 return(errorstatus);
2173 }
2174
2175 /*!< Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
2176 SDIO_CmdInitStructure.SDIO_Argument = 0x00;
2177 SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH;
2178 SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
2179 SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
2180 SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
2181 SDIO_SendCommand(&SDIO_CmdInitStructure);
2182
2183 errorstatus = CmdResp1Error(SD_CMD_APP_SD_SET_BUSWIDTH);
2184
2185 if (errorstatus != SD_OK)
2186 {
2187 return(errorstatus);
2188 }
2189
2190 return(errorstatus);
2191 }
2192 else
2193 {
2194 errorstatus = SD_REQUEST_NOT_APPLICABLE;
2195 return(errorstatus);
2196 }
2197 }
2198 }
2199
2200 /**
2201 * @brief Checks if the SD card is in programming state.
2202 * @param pstatus: pointer to the variable that will contain the SD card state.
2203 * @retval SD_Error: SD Card Error code.
2204 */
2205 static SD_Error IsCardProgramming(uint8_t *pstatus)
2206 {
2207 SD_Error errorstatus = SD_OK;
2208 __IO uint32_t respR1 = 0, status = 0;
2209
2210 SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16;
2211 SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_STATUS;
2212 SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
2213 SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
2214 SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
2215 SDIO_SendCommand(&SDIO_CmdInitStructure);
2216
2217 status = SDIO->STA;
2218 while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT)))
2219 {
2220 status = SDIO->STA;
2221 }
2222
2223 if (status & SDIO_FLAG_CTIMEOUT)
2224 {
2225 errorstatus = SD_CMD_RSP_TIMEOUT;
2226 SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
2227 return(errorstatus);
2228 }
2229 else if (status & SDIO_FLAG_CCRCFAIL)
2230 {
2231 errorstatus = SD_CMD_CRC_FAIL;
2232 SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL);
2233 return(errorstatus);
2234 }
2235
2236 status = (uint32_t)SDIO_GetCommandResponse();
2237
2238 /*!< Check response received is of desired command */
2239 if (status != SD_CMD_SEND_STATUS)
2240 {
2241 errorstatus = SD_ILLEGAL_CMD;
2242 return(errorstatus);
2243 }
2244
2245 /*!< Clear all the static flags */
2246 SDIO_ClearFlag(SDIO_STATIC_FLAGS);
2247
2248
2249 /*!< We have received response, retrieve it for analysis */
2250 respR1 = SDIO_GetResponse(SDIO_RESP1);
2251
2252 /*!< Find out card status */
2253 *pstatus = (uint8_t) ((respR1 >> 9) & 0x0000000F);
2254
2255 if ((respR1 & SD_OCR_ERRORBITS) == SD_ALLZERO)
2256 {
2257 return(errorstatus);
2258 }
2259
2260 if (respR1 & SD_OCR_ADDR_OUT_OF_RANGE)
2261 {
2262 return(SD_ADDR_OUT_OF_RANGE);
2263 }
2264
2265 if (respR1 & SD_OCR_ADDR_MISALIGNED)
2266 {
2267 return(SD_ADDR_MISALIGNED);
2268 }
2269
2270 if (respR1 & SD_OCR_BLOCK_LEN_ERR)
2271 {
2272 return(SD_BLOCK_LEN_ERR);
2273 }
2274
2275 if (respR1 & SD_OCR_ERASE_SEQ_ERR)
2276 {
2277 return(SD_ERASE_SEQ_ERR);
2278 }
2279
2280 if (respR1 & SD_OCR_BAD_ERASE_PARAM)
2281 {
2282 return(SD_BAD_ERASE_PARAM);
2283 }
2284
2285 if (respR1 & SD_OCR_WRITE_PROT_VIOLATION)
2286 {
2287 return(SD_WRITE_PROT_VIOLATION);
2288 }
2289
2290 if (respR1 & SD_OCR_LOCK_UNLOCK_FAILED)
2291 {
2292 return(SD_LOCK_UNLOCK_FAILED);
2293 }
2294
2295 if (respR1 & SD_OCR_COM_CRC_FAILED)
2296 {
2297 return(SD_COM_CRC_FAILED);
2298 }
2299
2300 if (respR1 & SD_OCR_ILLEGAL_CMD)
2301 {
2302 return(SD_ILLEGAL_CMD);
2303 }
2304
2305 if (respR1 & SD_OCR_CARD_ECC_FAILED)
2306 {
2307 return(SD_CARD_ECC_FAILED);
2308 }
2309
2310 if (respR1 & SD_OCR_CC_ERROR)
2311 {
2312 return(SD_CC_ERROR);
2313 }
2314
2315 if (respR1 & SD_OCR_GENERAL_UNKNOWN_ERROR)
2316 {
2317 return(SD_GENERAL_UNKNOWN_ERROR);
2318 }
2319
2320 if (respR1 & SD_OCR_STREAM_READ_UNDERRUN)
2321 {
2322 return(SD_STREAM_READ_UNDERRUN);
2323 }
2324
2325 if (respR1 & SD_OCR_STREAM_WRITE_OVERRUN)
2326 {
2327 return(SD_STREAM_WRITE_OVERRUN);
2328 }
2329
2330 if (respR1 & SD_OCR_CID_CSD_OVERWRIETE)
2331 {
2332 return(SD_CID_CSD_OVERWRITE);
2333 }
2334
2335 if (respR1 & SD_OCR_WP_ERASE_SKIP)
2336 {
2337 return(SD_WP_ERASE_SKIP);
2338 }
2339
2340 if (respR1 & SD_OCR_CARD_ECC_DISABLED)
2341 {
2342 return(SD_CARD_ECC_DISABLED);
2343 }
2344
2345 if (respR1 & SD_OCR_ERASE_RESET)
2346 {
2347 return(SD_ERASE_RESET);
2348 }
2349
2350 if (respR1 & SD_OCR_AKE_SEQ_ERROR)
2351 {
2352 return(SD_AKE_SEQ_ERROR);
2353 }
2354
2355 return(errorstatus);
2356 }
2357
2358 /**
2359 * @brief Find the SD card SCR register value.
2360 * @param rca: selected card address.
2361 * @param pscr: pointer to the buffer that will contain the SCR value.
2362 * @retval SD_Error: SD Card Error code.
2363 */
2364 static SD_Error FindSCR(uint16_t rca, uint32_t *pscr)
2365 {
2366 uint32_t index = 0;
2367 SD_Error errorstatus = SD_OK;
2368 uint32_t tempscr[2] = {0, 0};
2369
2370 /*!< Set Block Size To 8 Bytes */
2371 /*!< Send CMD55 APP_CMD with argument as card's RCA */
2372 SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)8;
2373 SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
2374 SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
2375 SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
2376 SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
2377 SDIO_SendCommand(&SDIO_CmdInitStructure);
2378
2379 errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN);
2380
2381 if (errorstatus != SD_OK)
2382 {
2383 return(errorstatus);
2384 }
2385
2386 /*!< Send CMD55 APP_CMD with argument as card's RCA */
2387 SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16;
2388 SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
2389 SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
2390 SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
2391 SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
2392 SDIO_SendCommand(&SDIO_CmdInitStructure);
2393
2394 errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
2395
2396 if (errorstatus != SD_OK)
2397 {
2398 return(errorstatus);
2399 }
2400 SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
2401 SDIO_DataInitStructure.SDIO_DataLength = 8;
2402 SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_8b;
2403 SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO;
2404 SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
2405 SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
2406 SDIO_DataConfig(&SDIO_DataInitStructure);
2407
2408
2409 /*!< Send ACMD51 SD_APP_SEND_SCR with argument as 0 */
2410 SDIO_CmdInitStructure.SDIO_Argument = 0x0;
2411 SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_APP_SEND_SCR;
2412 SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
2413 SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
2414 SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
2415 SDIO_SendCommand(&SDIO_CmdInitStructure);
2416
2417 errorstatus = CmdResp1Error(SD_CMD_SD_APP_SEND_SCR);
2418
2419 if (errorstatus != SD_OK)
2420 {
2421 return(errorstatus);
2422 }
2423
2424 while (!(SDIO->STA & (SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR)))
2425 {
2426 if (SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET)
2427 {
2428 *(tempscr + index) = SDIO_ReadData();
2429 index++;
2430 }
2431 }
2432
2433 if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET)
2434 {
2435 SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT);
2436 errorstatus = SD_DATA_TIMEOUT;
2437 return(errorstatus);
2438 }
2439 else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET)
2440 {
2441 SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL);
2442 errorstatus = SD_DATA_CRC_FAIL;
2443 return(errorstatus);
2444 }
2445 else if (SDIO_GetFlagStatus(SDIO_FLAG_RXOVERR) != RESET)
2446 {
2447 SDIO_ClearFlag(SDIO_FLAG_RXOVERR);
2448 errorstatus = SD_RX_OVERRUN;
2449 return(errorstatus);
2450 }
2451 else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET)
2452 {
2453 SDIO_ClearFlag(SDIO_FLAG_STBITERR);
2454 errorstatus = SD_START_BIT_ERR;
2455 return(errorstatus);
2456 }
2457
2458 /*!< Clear all the static flags */
2459 SDIO_ClearFlag(SDIO_STATIC_FLAGS);
2460
2461 *(pscr + 1) = ((tempscr[0] & SD_0TO7BITS) << 24) | ((tempscr[0] & SD_8TO15BITS) << 8) | ((tempscr[0] & SD_16TO23BITS) >> 8) | ((tempscr[0] & SD_24TO31BITS) >> 24);
2462
2463 *(pscr) = ((tempscr[1] & SD_0TO7BITS) << 24) | ((tempscr[1] & SD_8TO15BITS) << 8) | ((tempscr[1] & SD_16TO23BITS) >> 8) | ((tempscr[1] & SD_24TO31BITS) >> 24);
2464
2465 return(errorstatus);
2466 }
2467
2468 /**
2469 * @brief Converts the number of bytes in power of two and returns the power.
2470 * @param NumberOfBytes: number of bytes.
2471 * @retval None
2472 */
2473 uint8_t convert_from_bytes_to_power_of_two(uint16_t NumberOfBytes)
2474 {
2475 uint8_t count = 0;
2476
2477 while (NumberOfBytes != 1)
2478 {
2479 NumberOfBytes >>= 1;
2480 count++;
2481 }
2482 return(count);
2483 }
2484
2485 /**
2486 * @}
2487 */
2488
2489 /**
2490 * @}
2491 */
2492
2493 /**
2494 * @}
2495 */
2496
2497 /**
2498 * @}
2499 */
2500
2501 /**
2502 * @}
2503 */
2504
2505 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/